WEBVTT 1 00:00:02.310 --> 00:00:05.609 William Cheng: Okay, this is part two of lecture eight 2 00:00:06.690 --> 00:00:15.719 William Cheng: So, so let's talk about how to implement the first fit memory alligator. So you need to keep track of the free blocks, you might have to keep track of the 3 00:00:16.230 --> 00:00:27.450 William Cheng: Block that are returned to the application. So the one that are returned to the application. We call it the allocated blog or the busy blog so there. Those are the one that has been returned to the application and now your hands off. 4 00:00:28.050 --> 00:00:33.330 William Cheng: The one that are free. We call them free blogs over here it says struck free block. So this is the data structure for free, blah. 5 00:00:34.260 --> 00:00:43.830 William Cheng: So the way this is done is that you know all the free blogs are linked together in a, you know, linguists that and typically this is done using a web link list. 6 00:00:44.370 --> 00:00:51.750 William Cheng: The reason we prefer web link list and a single link list is that insertion and deletion, you know, for double of English is what I want. 7 00:00:52.080 --> 00:00:56.790 William Cheng: Okay, so if you if you implement your warm up one correctly, we implement my for two lists. 8 00:00:57.240 --> 00:01:03.210 William Cheng: If you think about the amount of computation you need to perform every time, will you perform it is certain that the lead. 9 00:01:03.630 --> 00:01:11.580 William Cheng: You will see that their order one. There's always the order one order one. That means that, you know, so, so, so it the length of the list of yours is equal to an 10 00:01:11.940 --> 00:01:21.750 William Cheng: Order one means that it's independent of that. Okay, and can be a million and can be a billion, the number of operation that either you are doing over here. It's going to be independent of the length of the list. 11 00:01:22.680 --> 00:01:30.210 William Cheng: Okay, so in that case, or the one is really good. So that's what, that's the reason we like W link list right if you use a single link. Bless you. We try to insert something or 12 00:01:30.450 --> 00:01:37.050 William Cheng: Something. Then, on the average, you're going to traverse have to list, you're going to be order. And so in that case the performance will be really bad. Okay. 13 00:01:37.440 --> 00:01:45.300 William Cheng: So that's why we like the W linked list. Whether you circle the list or not so subtle get circular. Listen, the traditional web link. There's, there's really not much difference. 14 00:01:45.630 --> 00:01:53.940 William Cheng: As I mentioned in the in warm up one. The reason we like a circular list is that maybe your code. It's going to be a little more elegant compared to a traditional, you know, 15 00:01:54.690 --> 00:02:05.520 William Cheng: Traditional web Nicholas net alright so here we're going to put me in a linked list. And over here, the one of the data structure over here is going to be the link right so again the link 16 00:02:06.030 --> 00:02:09.990 William Cheng: I don't remember what I mentioned that before. So in the textbook when it hopefully I think I did. 17 00:02:10.530 --> 00:02:16.380 William Cheng: That in the textbook when they, when they mentioned a link. So this is the next point or end the previous point it right so he puts a W think list. 18 00:02:16.860 --> 00:02:20.190 William Cheng: Everything content to point, there's one point forward and do one or the other one point. 19 00:02:20.730 --> 00:02:26.070 William Cheng: Back backwards. If it turns out to be a simply link Liz, what then the link over here is the next point to that point to the next guy. 20 00:02:26.490 --> 00:02:32.310 William Cheng: OK. So the test. We'll just we'll just use the word link so you can use it to represent a single a linked list or web request again. 21 00:02:32.760 --> 00:02:38.760 William Cheng: For every free bra, you need to know the block size right in the example that we saw before the block size 1300 and 1200 22 00:02:39.150 --> 00:02:47.190 William Cheng: So therefore you need to have the size of the block guys are the size over here is going to be the size of the entire block, including all these data structure that you put inside to a free blog. 23 00:02:47.880 --> 00:02:50.910 William Cheng: Okay, so please understand that, you know, the memory allocated when 24 00:02:51.210 --> 00:03:01.200 William Cheng: The memory alligator is there to manage the free blocks. It doesn't keep track of the one that are in use. Okay. It doesn't keep track of the one that are busy. It doesn't keep track of the one that allocated to the application. 25 00:03:01.560 --> 00:03:07.140 William Cheng: Okay, you just keep track of all the free ones like link them together in a sorted list and this list is sorta 26 00:03:07.320 --> 00:03:21.390 William Cheng: By the virtual address of these three blocks right every virtual address every free blog over here has a beginning address. So this list is sorted according to their to their addresses and that's how we implement first fit algorithm. Okay. By be because the list is sorted. Yeah. 27 00:03:23.010 --> 00:03:26.190 William Cheng: Alright, so, so, so 28 00:03:27.510 --> 00:03:35.250 William Cheng: So, so that that's what my life right if you if you link them in LinkedIn ads and you have the block size of this way you can run the Malik algorithm. What about free 29 00:03:35.550 --> 00:03:41.040 William Cheng: Okay so free. So typically, the opposite where you free you return a huge return an allocated block. 30 00:03:41.340 --> 00:03:45.420 William Cheng: Back into the free list, guys. So in this example over here it says we're going to call this blog a 31 00:03:45.600 --> 00:03:51.750 William Cheng: Because the base address of this book is a rock, which means that the first act is over here has a address right. That's why we call this block aid. 32 00:03:51.990 --> 00:03:57.480 William Cheng: So when you call Malik the return value is going to be a and then when you're done with this boy. Are you gonna. Are you gonna call free a 33 00:03:57.900 --> 00:04:05.490 William Cheng: Okay, so when you go free what we're supposed to do over here is that what you call free a we need to look at the blog via Baba, and there was blood right below. 34 00:04:05.910 --> 00:04:11.220 William Cheng: Below it to see if there are both three if they're both three while they are you going to end up with a giant free blah. 35 00:04:11.880 --> 00:04:18.750 William Cheng: Okay, so therefore every, every opportunity that we get when somebody returned a free blah, we have to look at the blog right right above it right below that. 36 00:04:19.320 --> 00:04:23.820 William Cheng: If they're free. We need to create as big of a block as possible. Okay, why do we need to do that well. 37 00:04:24.480 --> 00:04:35.250 William Cheng: If you keep them as separate blocks that you folks are gonna get smaller and smaller and smaller, and the end or you won't be able to satisfy a Malik request. Okay. So, therefore, every chance we get, we need to make it as big of a blog as possible. 38 00:04:35.790 --> 00:04:37.380 William Cheng: Okay. So when somebody called free so 39 00:04:37.770 --> 00:04:41.190 William Cheng: So this technique is known as memory coalescing memory coalescing over here. 40 00:04:41.400 --> 00:04:50.340 William Cheng: You need to look at the blog above you. If the blog above you is free and the bottom, the one below you. It's not free, then this guy is going to make a bigger blog by combining your blog and the blog above you. 41 00:04:50.670 --> 00:04:59.700 William Cheng: If it turns out the barber, but above you is not free, but the block below is free. So again, you need to combine these two boxes are bigger, blah, if it turns out all these blocks are free, you need to combine them together into a big box. 42 00:05:00.090 --> 00:05:08.010 William Cheng: Okay, and then into the biggest blog over here. So in this example over here, this is the best situation where you, you know, a coalesce memory going to end up with a really big block. 43 00:05:08.760 --> 00:05:15.000 William Cheng: Yeah. Alright. So the question over here is that, you know, when you return a block like this. How do you know the previous block is free. 44 00:05:16.380 --> 00:05:23.850 William Cheng: Okay, so one thing that you can do is that you can actually, you know, search through the free block list and try to sort of find out whether the pre block is free or not, but that will be really expensive. 45 00:05:24.450 --> 00:05:28.830 William Cheng: OK, so the trick that the memory allocation and we use that they use something called a boundary tax, then 46 00:05:29.250 --> 00:05:39.930 William Cheng: The boundary that looks like this. So, so for a free blog over here, right. So we saw that in the free blog over here while we're going to keep track of the size of the blog and then we have a backwards, way before lane. So that's the link pointer over here. 47 00:05:40.800 --> 00:05:45.810 William Cheng: We also need at the bottom of the free blog. We also need to have a copy of the size of that block. 48 00:05:46.560 --> 00:05:54.390 William Cheng: Okay. So right now it doesn't make sense. Why do you need that. So they know we're going to see some example, why do you need to make a copy of the size, both at the top of the blog and also at the bottom and 49 00:05:55.260 --> 00:06:02.850 William Cheng: Then similarly for the alligator blog over here, we also need to know something about the alligator blog. So this guy's we also need to keep track of the size of the allocated blog. 50 00:06:03.000 --> 00:06:07.440 William Cheng: So there's there's that we also need to right at the top, the size of this blog and the bottom of the size of the spot. 51 00:06:07.950 --> 00:06:13.620 William Cheng: Okay. So in this case, when somebody called Malik 400 and something called my leg. You know 400 over here that 52 00:06:14.010 --> 00:06:22.200 William Cheng: The size of yours for buys law. So in this case, we're required to find a blog. That's 408 bytes long because we need to keep track of the boundary that the boundary tab. 53 00:06:22.620 --> 00:06:34.320 William Cheng: Okay, so this will be 408 bytes long from top to the bottom. Over here, which means that the size over here is going to go to 408 and the bottom over here is also that when you go to 400 k and the middle part over here is going to be 400 bytes. 54 00:06:35.010 --> 00:06:45.900 William Cheng: That so default when you call Malik Malik 400 we're going to find a blog. That's 408 bytes law and allocate that to you and the return address will be four bytes into the state of law. 55 00:06:46.710 --> 00:06:49.650 William Cheng: Okay, so this way, the return address is actually going to be right here. 56 00:06:50.070 --> 00:07:04.500 William Cheng: So, so again the application will have a pointer point two 400 bytes of free blah. What about the boundary tag over here the boundary toggle here still belong to the memory alligator. Okay. And the application promise not to touch anything above or beyond this 100 bytes. 57 00:07:05.880 --> 00:07:14.820 William Cheng: Okay, so therefore it so. So again, even though the allocated block is returned to the application. We still reserve four bytes at the top of fulbright's at the bottom and they still belong to the memory alligator. 58 00:07:15.000 --> 00:07:23.010 William Cheng: The memory allocators lot of manipulate them, but the application has made sign a contract to say I will not. I will not touch the those eight bytes. 59 00:07:23.850 --> 00:07:37.950 William Cheng: Okay, well, so they don't win the application need to free this blog over here again we kind of call free a over here is right here when you subtract it by four bytes over here to get a top of this blog and then we return this entire 400 day bye bye bye block back to the memory alligator. 60 00:07:39.090 --> 00:07:45.480 William Cheng: Okay, so, so, so later I'm going to sort of see why we actually would have to have, you know, both about the tag at the top of the bottom. Yeah. 61 00:07:49.410 --> 00:07:57.180 William Cheng: Well, I'm going to see a more detailed example so so for these two blocks over here, we also need to be able to tell whether this blog is free. And then this blog is 62 00:07:58.050 --> 00:08:05.400 William Cheng: That this block is allocated this block is free. So we need additional bit to say, you know, single bit to say either they're free or they're allocated. 63 00:08:05.820 --> 00:08:12.150 William Cheng: Okay. So in our example we're going to be really, really, you know, expensive. We're going to use four bytes to to remember 64 00:08:12.990 --> 00:08:19.140 William Cheng: Remember, whether a block is allocated or it's free. Yeah. So in your example over here. This is what a free Bronco cry. 65 00:08:19.740 --> 00:08:25.980 William Cheng: The first four bytes over here is going to be equal to the value of zero. Again, we're being wasteful here. Right. But again, just for illustration purposes. 66 00:08:26.310 --> 00:08:34.170 William Cheng: So in this case, you know, this is this is a free Brock this field is known as the in us feel so you're physically. Right. It's not a user that for you to go to zero. 67 00:08:34.500 --> 00:08:43.290 William Cheng: OK, the next, you know, four bytes over here. It's going to be the size of the entire blog. So again, the size of the entire block, including all this 16 by Joe boundary tax that 68 00:08:43.770 --> 00:08:50.670 William Cheng: The bottom over, you're going to have exactly the same thing, or the first four bars over here usually go to false because it's a free blog, followed by the size of the entire block. 69 00:08:51.210 --> 00:09:00.660 William Cheng: Okay, so, so again, since this is a free blog. We also need the previous point you in the next point or over here. Again, they are bite number eight and number 12 starting from the beginning of the block. 70 00:09:01.110 --> 00:09:06.060 William Cheng: Yeah, the middle part over here. I mean, there are free data block over here. So they're just garbage. We don't really care what they are. 71 00:09:06.420 --> 00:09:12.840 William Cheng: There for allocated blog or use blog over here again we have eight bytes of boundary tag at the top of a batch of boundary target at the bottom. 72 00:09:13.170 --> 00:09:16.500 William Cheng: The first four bars over here is going to be equal to the value want to say that this 73 00:09:16.920 --> 00:09:26.460 William Cheng: This one, this block is he us. So, therefore, is equal to one. So again, by looking at the first four bytes over here you you know whether a block is a is a free blog or it's not a free blog. 74 00:09:26.760 --> 00:09:32.640 William Cheng: Then the next four bytes over here is going to be a size for the entire blog over here. Again, it's going to be the size of the user requests. 75 00:09:32.880 --> 00:09:39.690 William Cheng: Plus 16 bites right there's there's a box at the top of a bite to the bottom and the size over here, it's gonna be good. The entire size of the data structure know 76 00:09:39.960 --> 00:09:49.980 William Cheng: The middle part of your eye color blue because this is the one now returned to the application, the application as you're going to get this address. So if the start address it at the beginning. I'll be here at this address is going to be star plus eight. 77 00:09:50.820 --> 00:10:02.070 William Cheng: That I color blue to remind the the memory allocated say hey you know this one. You promise that you will not touch. So this is good. This is considered to be used by the application and then the you know the the the 78 00:10:02.460 --> 00:10:05.190 William Cheng: Memory alligator can only touch the data that's why 79 00:10:05.730 --> 00:10:15.360 William Cheng: Okay, and also at the bottom over here. We're going to have a copy of the boundary time we're going to have four bytes to say that this one is the US and then the next four bytes will be here is going to be size of the three blocks away. 80 00:10:16.530 --> 00:10:22.230 William Cheng: So let's say that we saw, running, running our program. Okay, when we start running our program. The, the, you know, the 81 00:10:23.070 --> 00:10:29.850 William Cheng: The memory manager is the one I said the no memory alligator is managing the entire he. So in that case, what would the free list look like 82 00:10:30.420 --> 00:10:34.770 William Cheng: Okay. At the beginning before you call any Malik while your entire heat is one giant free blah. 83 00:10:35.670 --> 00:10:47.490 William Cheng: Okay. So, therefore, you know, so, so, so we're going to use a traditional w plus, right, which means that we have appointed to the held in the order to the tail. So in this case, we only have one block. So therefore, the head and the tail. They're punting exactly the same block. 84 00:10:47.880 --> 00:10:54.360 William Cheng: Okay, so here's a freelance to have a point to this blog that tail pointed this bar, and this is the first memory location of your heap. 85 00:10:54.750 --> 00:10:59.850 William Cheng: Okay, and then the size over here is going to be the entire size of the heap. So again, this one will be the entire site over here. 86 00:11:00.690 --> 00:11:05.010 William Cheng: So again us over here 00 because this one is a free blog and the size of the empire here. 87 00:11:05.250 --> 00:11:12.330 William Cheng: At the bottom of this data structure will have exactly the same thing us equal to zero and we have the size of entire heat that and also what about the previous 88 00:11:12.600 --> 00:11:18.570 William Cheng: Previous than a neck. Since we don't have a circular list and the previous over here will point to now and the next will be appointed now. 89 00:11:18.870 --> 00:11:30.210 William Cheng: So this way you will know that this will be this is the only block inside free list. Okay. And again, the size of this blog is the entire heat that. So, by the way, if this is the size of the entire he, where is the free list. 90 00:11:31.860 --> 00:11:35.910 William Cheng: I mean, you know, the free those over here has to be part of your address space, right. So where's the free list. 91 00:11:36.330 --> 00:11:45.030 William Cheng: What is it a tech segment of course it's not in the tech segment, because it's not code. So, so the free list might be in the data segment or in the BSS segment, because this is part of your global variable. 92 00:11:45.270 --> 00:11:53.790 William Cheng: Okay, so these are the global variable that you didn't declare so they are just pumped. Probably the memory alligator. So in this case, again, the compiler will actually generate code. 93 00:11:54.240 --> 00:12:06.780 William Cheng: You know, for, you know, because the compiler. See that, oh, you're making now like a free call so therefore you need the you need a memory allocated. So, therefore, you have a free list. Okay. In the beginning, you'll be initialized with this data structure. 94 00:12:07.800 --> 00:12:12.090 William Cheng: Your entire here. It's going to be one giant free blog that's inside for us that 95 00:12:13.740 --> 00:12:14.790 William Cheng: All right, so 96 00:12:15.180 --> 00:12:24.510 William Cheng: Yeah, so it's all these pointer right again, the point is just the address right so we hear this is the beginning of the heap. The head over here will contain the address the first memory location of the heat. 97 00:12:24.690 --> 00:12:29.040 William Cheng: And the tail over here will also contain the address of the first memory location of the heap. Okay. 98 00:12:30.180 --> 00:12:42.540 William Cheng: Alright, so let's take a look at some a concrete example. So I'm going to plug in some values are these value. I just can you know you know just come off the top of my head over here. So let's say that the heat starts at this really weird address. I mean, you know, 99 00:12:43.710 --> 00:12:44.580 William Cheng: So this address 100 00:12:46.080 --> 00:12:50.370 William Cheng: I guess, you know, again, this is doesn't make sense, right, because we know that, you know, 101 00:12:50.820 --> 00:13:00.270 William Cheng: starting at zero XE 00000 they belong to the Colonel. I mean this address actually above that. So this is actually a kernel address. We'll just sort of pretend that this is actually a valid IP address. 102 00:13:00.510 --> 00:13:11.340 William Cheng: Okay, so there's a zero x FTP CPA, not a where I clearly the reversal at night. A PCT. Yes. Okay. So, therefore, I'm just going to just use this address and pretend that this is a valid IP address now. 103 00:13:12.300 --> 00:13:14.400 William Cheng: The size of the he was going to be this really weird size. 104 00:13:14.850 --> 00:13:23.190 William Cheng: So again, you know, when we look at addresses. We're going to get used to looking at addresses using hexadecimal representation. OK. So again, hexadecimal representation 105 00:13:23.490 --> 00:13:28.980 William Cheng: Every character is four beds. Right. So you have eight characters over here is going to be 32 bit. And here's a 32 bit address 106 00:13:29.700 --> 00:13:38.010 William Cheng: Now the size of a heat over here. Again, that's something that we're number zero x, etc. At if you look at the decimal is going to be 60584 107 00:13:39.000 --> 00:13:53.970 William Cheng: Again I you know I just pulled that number out of the hat guy doesn't really mean anything. So in this case, you know, again, the beginning of the heat over here is going to be zero. F. Ed CPA night that so therefore inside the free list the head over here will contain exactly this address 108 00:13:56.490 --> 00:13:59.370 William Cheng: And to tell over here also content exactly that value that 109 00:13:59.670 --> 00:14:04.080 William Cheng: Alright, so the entire what we're just one giant free blog. The first four bars over here, equal to zero. 110 00:14:04.260 --> 00:14:12.540 William Cheng: The next four biases, the size of the heat. So, therefore, to contain the value zero x EC eight right exactly right here, the bottom. Over here we have exactly the same information. The 111 00:14:13.020 --> 00:14:22.560 William Cheng: You know, the last four bytes over here is going to be equal to zero, x etc ECA and the next, you know, the previous four by is going to be used equal to zero. So, therefore, the gonna be 112 00:14:23.070 --> 00:14:24.300 William Cheng: A bunch of zeros that 113 00:14:24.660 --> 00:14:29.730 William Cheng: And then also, you know, eight bytes into the heat right we have the previous as the next pointer over here. 114 00:14:29.850 --> 00:14:37.920 William Cheng: So the previous partner is sitting at address zero x F Ed CPA zero, you take the original characters over here you at a time where you're going to get this address 115 00:14:38.160 --> 00:14:44.400 William Cheng: And if you add four to four more to it, you're going to get the pointer for the next pointer over here. And again, this one points now so they're equal to zero. 116 00:14:44.760 --> 00:14:58.320 William Cheng: That the rest of it, then I'll be here. They're part of the free data part of the free blog so their garbage and you don't really care what they are. Yeah. Alright, so, so when you use this example to run through some some other some other test cases. 117 00:15:02.910 --> 00:15:08.160 William Cheng: Alright. The example is somebody you know the applications on a call Malak 100 118 00:15:09.120 --> 00:15:12.360 William Cheng: Okay, so what would you do, right. So first of all, you know that the boundary tag. 119 00:15:12.570 --> 00:15:21.930 William Cheng: For the allocated blog is eight bytes of the topic eight bytes at the bottom. So you need to walk down the freelance over here, find a blog that's bigger than 106 and you allocate sorry 120 00:15:22.320 --> 00:15:30.300 William Cheng: That's a bigger than 116 right because they're eight bytes of the topic by the bother you find a problem with the monitors 16 by by at least 160 bye bye bye 121 00:15:30.900 --> 00:15:35.250 William Cheng: byes long. Okay. And you're going to allocate the first 100 system, but out of that free blah. 122 00:15:35.550 --> 00:15:44.670 William Cheng: Okay, so in this case we only have one giant free blogging. It's really big. So what we're gonna do is, I'm going to allocate the first 116 buyers right over here. There's gonna be 116 bars over here. 123 00:15:44.940 --> 00:15:54.360 William Cheng: There. And then what about the rest of it. The rest of is going to be a free blog and we know what a free book look like, right. So the next block over here is going to be a free blog. So that means that the first four bars will be go to zero. 124 00:15:54.570 --> 00:16:04.800 William Cheng: The next four by is going to be the previous block size minus 116 right because originally is going to be the size of the heat and now we take away 116 so this one is going to be 125 00:16:05.370 --> 00:16:07.170 William Cheng: Whatever this number is minus 116 126 00:16:07.560 --> 00:16:13.110 William Cheng: Okay. And then the next two partners over here. Still, the previous equal to know and then Nancy wouldn't know of your so there'll be go to zero. 127 00:16:13.290 --> 00:16:22.410 William Cheng: The middle part over here, it will be garbage and we don't care at the bottom. Over here, the EU still going to be 00 and the size of the free blog and now it's going to be this number of minus 116 128 00:16:23.550 --> 00:16:28.800 William Cheng: Well, so that will be the freelance. And we also need to adjust the head of the free list of point right here and the tail. The list. 129 00:16:29.040 --> 00:16:36.270 William Cheng: Free list point right here. And again, there's still pointing to exactly the same place because the free list, we only have one blog. We all have one free blah. Okay. 130 00:16:36.870 --> 00:16:44.520 William Cheng: What about the alligator bug over here. The alligator blog over here, he was going to be equal to one, right, the size of this blog is gonna be 116. I'm going to put one on six in there. 131 00:16:44.700 --> 00:16:48.660 William Cheng: At the bottom over here. He used to go to one and then the size over here is gonna be 116 132 00:16:49.230 --> 00:16:58.140 William Cheng: So in this case, what's going to be the return value for Malik the return value of the mountain is going to be this pointer right here. So it's going to be zero, x F Ed CPA zero 133 00:16:58.800 --> 00:17:14.220 William Cheng: Okay, so in this case Malik will return zero x, f, g DC be a zero. So zero here is going to be the previous address what we are plus eight, because that will be the return address. Okay, so this is what will the future look like it will look like this. 134 00:17:15.330 --> 00:17:21.660 William Cheng: So this will be the alligator block right the boundary time he was equal to one and the size of the here is going to go to 116 135 00:17:22.170 --> 00:17:32.070 William Cheng: So this is in hex rise zero x seven for you need to multiply seven by 16 to get 111 to you for two is going to be warm and six. Right. So again, 136 00:17:32.280 --> 00:17:37.860 William Cheng: This is something as long as six at the bottom. Over here you have exactly the same boundary Tad there and now the return 137 00:17:38.130 --> 00:17:47.070 William Cheng: The return part over here, this is exactly 100 by slaw. How do you know that because I have a box at the top of a box at the bottom and the entire blog over here's 116 bites law. 138 00:17:47.640 --> 00:17:54.630 William Cheng: Okay, so therefore this exactly 100 times over here. So when he returned to address you going to return this value zero x fit CPA zero 139 00:17:55.860 --> 00:18:01.230 William Cheng: Alright, so, so please understand that you know why you're getting the middle address over here, I will what somebody call out 140 00:18:01.770 --> 00:18:12.690 William Cheng: There and the free blog over here. He was equal to zero, the size is going to be the original size over here, minus 116 I've got this number at the bottom, we have to copy their previously go to nowadays they go to know 141 00:18:12.870 --> 00:18:24.780 William Cheng: The free list ahead and tell me here. Well, point two zero ve DC BB BB 04. OK. So again, that would be the original but base articles over here plus 160 yeah 142 00:18:27.120 --> 00:18:35.790 William Cheng: Alright, so, so, so, so what if you call Malik Malik Malik over and over again, if you call Malik k times, how many free blacks are you going to be left with 143 00:18:36.570 --> 00:18:42.480 William Cheng: No guy so so again if you start with this right here called Malik. Again, we're gonna we're going to allocate the top part over here to be to be 144 00:18:42.990 --> 00:18:48.660 William Cheng: Allocated blah, and then if you call Malik. Again, it's going to be top horrible. So at the end, we only going to end up with one free block. 145 00:18:49.500 --> 00:18:53.760 William Cheng: Okay, so, so, so, you know, the Yo yo yo yo yo yo. He's gonna look like this. 146 00:18:54.090 --> 00:19:06.690 William Cheng: Okay. After you can't call K consecutive melons without calling any free. Well, then the first three blocks over here will be allocated right over here. So again, there might be of different size, India, we only going to end up with one free block. 147 00:19:07.350 --> 00:19:11.310 William Cheng: Okay, so therefore the memory is going to look like this in the freelance to have a point here and the 148 00:19:12.030 --> 00:19:24.870 William Cheng: Point here, right, and then the application. They all have pointer into all these free ROC eight bytes into every free, but as I did the application we're putting into the busy block or the news blog eight bytes into every one of these box. 149 00:19:25.980 --> 00:19:32.640 William Cheng: Okay, so, so now we're going to call it that. We're going to start freeing the blogs. So when you allocate the blog, you can feed the blocking any order that you want. 150 00:19:32.970 --> 00:19:37.800 William Cheng: Okay, it doesn't really have to be the inverted reverse order. So if you're free our block number two. Are we here, what's gonna have a 151 00:19:38.010 --> 00:19:48.900 William Cheng: Lot, so you can free up block number to over here, we need to do coalescing. We need to look at the block above it, why it's busy and we need to look at a blob below. It's also busy or so in this case we need to add this one back to the free list. 152 00:19:49.530 --> 00:19:54.450 William Cheng: Okay. So this guy is a fruit is gonna end up with two blocks right one is you know this block number to over here. 153 00:19:54.600 --> 00:20:00.750 William Cheng: And the other ones box number one over here again we need to link them together, you know, using the double linked lists and that will be all free list. Yeah. 154 00:20:00.930 --> 00:20:09.750 William Cheng: And then let's say you free another one over here. Again, if you need to let us look to see if you can actually coalesce memory. If you cannot then again the field is gonna get longer and longer, longer 155 00:20:10.650 --> 00:20:15.870 William Cheng: Okay. Alright, so let's take a concrete example and see what happens over here that so 156 00:20:17.370 --> 00:20:22.260 William Cheng: So let's take a look at example is over here. We're going to call free why and why is this pointer right here. 157 00:20:23.280 --> 00:20:36.360 William Cheng: Okay, so, so how do we know whether the previous block is busy or not is yours or not. Okay. So, by looking at this picture right you can see that if you take ya. If you do some point or math. If I take why and I take y minus 16 158 00:20:36.660 --> 00:20:39.480 William Cheng: I'm going to find out whether the previous block is in us or not. 159 00:20:40.320 --> 00:20:49.050 William Cheng: Right, because if this is why over here. This is why minus four. There's y minus a, this is y minus 10 and this is why minus 12 now you know why do we have to have the 160 00:20:49.320 --> 00:20:58.140 William Cheng: Boundary talk at the bottom of every block, right. So, this way we can take. We can do point your math we take why we minus 16 we're going to find out whether the previous blog is the US or out 161 00:20:58.800 --> 00:21:00.270 William Cheng: Okay, what about the next block. 162 00:21:00.660 --> 00:21:10.530 William Cheng: OK. So again, if you take, why are we here at y minus four is going to be the size of this block. So if you take y minus a plus z over here again Z's the content of y minus four. 163 00:21:10.740 --> 00:21:22.740 William Cheng: Right. And that will give you the value of z z is going to be the size of the entire block. If you take y minus a fuzzy, you're going to get this memory location. And if you read this memory location we those four bytes that you would know that whether the next block is in user free 164 00:21:24.030 --> 00:21:33.000 William Cheng: Okay, so that's why we need to have the boundary type, both at the top of the blog and the bottom of law and this boundary tired, need to be exist both in the free block and also inside the US, Bob. 165 00:21:33.540 --> 00:21:40.350 William Cheng: Okay, so this way you can always use point to math to try to sort of figure out, you know, whether whether whether the next block and the previous bar, whether they're available or not. 166 00:21:41.220 --> 00:21:44.400 William Cheng: Yeah, alright. So, again, the pointer math over here. It's pretty straightforward. 167 00:21:45.150 --> 00:21:54.630 William Cheng: So let's take a look at examples and see what you do, you know, under certain kind of conditions, so that's that's going to call free why and the previous block is free and the next block is busy or use 168 00:21:55.200 --> 00:22:01.230 William Cheng: OK. So again, this guy's right you take why over here you minus 16 the previous block is free. That means that there's one equal to zero. 169 00:22:01.560 --> 00:22:09.120 William Cheng: Okay. And if you take y minus a plus z over here. You don't get this memory location and then this one is going to be one because over here. We're going to assume that the next one is busy. 170 00:22:10.080 --> 00:22:16.650 William Cheng: Well guys, and this guy is. Who do we call this the memory with right. This one is free. So, so when we're done, we're going to create a big blog like this. 171 00:22:17.370 --> 00:22:21.510 William Cheng: Then, so this blog is going to be the coalescing of the previous block in the current blah. 172 00:22:21.720 --> 00:22:30.690 William Cheng: And this will be a free blog. So what is it free block look like right he was over here you go to false. So the bottom part over here is wrong. We need to change this one to be zero, right, because that's going to be used, blah. 173 00:22:30.990 --> 00:22:32.280 William Cheng: What about the size of this bar. 174 00:22:32.730 --> 00:22:42.660 William Cheng: The size of this brought up the size of the two blocks combined right the current Bravo via the size you go to W. The previous blog over here the sizes, sorry, the current role will be the size of people to Z. 175 00:22:42.870 --> 00:22:48.930 William Cheng: And the previous blog over here is going to be the size of w. So how you get the size of the previous block, right. So again, you do point your math. 176 00:22:49.140 --> 00:22:56.430 William Cheng: You take why and you take y minus 12 y minus i was going to be this address you read those for buys you get you get you get W. 177 00:22:57.270 --> 00:23:00.210 William Cheng: OK. So again, y minus four will give you a z y minus 178 00:23:00.540 --> 00:23:11.370 William Cheng: Cloud will give you a W. You add them together, you're going to get a W policy. So, therefore, this memory location over here. You're going to replace it with w plus z and the bottom over here, you're gonna have the same value is going to be the plus w 179 00:23:12.660 --> 00:23:21.030 William Cheng: So what does this memory location over here. Right. Again, if you know if this memory locations y minus a and then this block size, you go to w then the memory location. 180 00:23:21.420 --> 00:23:27.870 William Cheng: You know of the previous block the first memory locations gonna be y minus a minus W. If you afford to it, you're going to get this memory location. 181 00:23:28.350 --> 00:23:36.960 William Cheng: Okay, what about this one, right, if you know this memory location is y minus a plus. See if you subtract for from it, you're going to get this memory location. So again, by simply doing 182 00:23:37.290 --> 00:23:41.610 William Cheng: You know pointer math. You can compute the address of all these memory location and you can put the right there. 183 00:23:42.570 --> 00:23:48.960 William Cheng: Okay. So in the end, this will be zero, this will be zero, right, the size will be z plus w and the bottom over you also busy plus w 184 00:23:49.440 --> 00:23:52.860 William Cheng: The previous partner over here you know this one. Do you have to change it. 185 00:23:53.790 --> 00:24:04.380 William Cheng: That. So this one point to the previous block, right. So, therefore, in this case, I'm making a bigger bar. So this, why don't we change it. The next point over your point to the next free bra against I don't have to change it. Okay. And then I'm done. 186 00:24:05.880 --> 00:24:13.500 William Cheng: Okay, so therefore girl will happen is that when when I finished doing all these modification right so so again you know the the way she's would think about how to implement it is that 187 00:24:13.710 --> 00:24:24.120 William Cheng: I should think about what the final state. The state of charge is going to be. And if I know what the current state is I know the final state. I just need to sort of figure out which memory location. I have to change. And then I write the code to change it. And then I'm done. 188 00:24:25.440 --> 00:24:35.430 William Cheng: Guys, so therefore there's going to be the final state right the rep over here are the memory location. You have to modify. So again, you only have to modify three memory location and you are done with coalescing this memory. 189 00:24:36.840 --> 00:24:41.850 William Cheng: Okay, so again this is this is the best case over here. You want to modify three memory location and then you're done. Right. So again, 190 00:24:42.090 --> 00:24:48.510 William Cheng: This is order one. Because even if we have 1 million of free blocks you know the number operation over here. It's going to be exactly the same. 191 00:24:48.750 --> 00:24:54.090 William Cheng: Right. So in this case, we're gonna we're going to modify three memory location. How many memory locations. We have to read 192 00:24:54.480 --> 00:24:59.670 William Cheng: While so again you know all the places where we are doing, you know, return you know what we're doing pointer math. 193 00:24:59.850 --> 00:25:06.240 William Cheng: We have to read all these memory locations over here. So again, the memory location. You have to read it also constant independent of the length of the previous 194 00:25:06.990 --> 00:25:08.880 William Cheng: Night. So, therefore, this is sort of the best situation. 195 00:25:09.570 --> 00:25:19.590 William Cheng: Right. The second example over here. You want to call free white the previous block is busy as he used and the next block is free, right. So in the end, we know that when we're done, we're going to combine this into a big block. 196 00:25:19.890 --> 00:25:28.020 William Cheng: Right. So, this value is wrong. This needs to be 00 and the next block over here the sizes x, right. So again, you can go to y minus a plus a plus for 197 00:25:28.380 --> 00:25:35.580 William Cheng: To get the value of x. And now when you're done. This will be replaced by the plus x and the bottom over, you will be replaced by x plus z. Right. 198 00:25:36.000 --> 00:25:38.460 William Cheng: He was over here you go to zero. That's correct. That 199 00:25:38.910 --> 00:25:49.920 William Cheng: The next and previous point or the wrong place. Right. But the next of the previous manager needs to be a bite. Starting from the beginning of the block. So you need to copy this eight bytes right here so that the next and previous over your point to the right place. Okay. 200 00:25:50.220 --> 00:25:51.420 William Cheng: What else do we have to modify 201 00:25:52.740 --> 00:25:57.180 William Cheng: Well, you know, he goes to the previous block over here, the previous block. 202 00:25:57.660 --> 00:26:07.740 William Cheng: The previous free Brock has next point here, right. The next point over here is plenty right here okay well that's plenty of the wrong location because now you know this block become bigger so therefore we have to adjust this point to the point right here. 203 00:26:08.190 --> 00:26:13.860 William Cheng: Okay, and also the next point your point that the next block by the next free blog will be also as as the previous previous pointer. 204 00:26:14.040 --> 00:26:20.550 William Cheng: And the previous one, or part right here. You also need to subtract that from, you know, bye, bye, bye, bye, bye, z, so that you will find right here. 205 00:26:21.480 --> 00:26:24.720 William Cheng: Okay. But again, that's only to memory location over here is still order one. 206 00:26:25.380 --> 00:26:28.860 William Cheng: Okay. So in the end, when you're done, you will look like this right over here. These to 207 00:26:29.100 --> 00:26:40.800 William Cheng: Combine to a bigger blot. You have to change this one to zero change those memory location over here and then copy the previous and next point over here. And then there are two or location, you have to change the previous next point two, and the next previous pointer. 208 00:26:41.310 --> 00:26:47.520 William Cheng: That sound familiar. I mean, I should sound familiar because when you're doing your my for to list that's exactly the call that you have to do. 209 00:26:47.970 --> 00:26:57.510 William Cheng: Right, okay. You have to go to the previous you know my for to listen man, go to the next point or and then make some adjustment and then go to the next pointer and point to the previous and do exactly the same adjustment. 210 00:26:57.810 --> 00:27:05.430 William Cheng: That. So in this in the end over here. We're doing this in order in order one because it's independent of the length of the free list. Yeah. 211 00:27:06.840 --> 00:27:16.860 William Cheng: Alright, next example, are we are calling free why and the previous block is free and the next block is free, right. So this one is equal to zero. Just want equal to zero. So, in this case, when you're done over here at the freedom is actually 212 00:27:17.610 --> 00:27:20.310 William Cheng: The free list as you're going to shrink by one free block. 213 00:27:20.550 --> 00:27:28.650 William Cheng: Right, because before they have. There are two three blocks right here and now they're going to be combined into one giant free blah. So this one is correct. And this one is correct, right. What about the size of this block. 214 00:27:28.860 --> 00:27:35.490 William Cheng: The size of this blog is going to be W plus x plus z and at the bottom is going to be x plus the plus w right there will be the same day. 215 00:27:35.700 --> 00:27:46.170 William Cheng: The previous point over here is, is that the right place. Ok. The next point over here is going to be at the wrong place. So therefore you need to copy this one right here. So, this one will be the next pointer and you know the the 216 00:27:46.470 --> 00:27:54.540 William Cheng: Next, the next free broads previous pointer is not pointing right here. So you need to adjust that by subtracting z and subtracting W format. So, to your point right here. 217 00:27:55.500 --> 00:28:05.880 William Cheng: Okay, so again, when you're done, it will look like this is also, you know, these two memory location has changed this memory location has changed and also the next free broads previous point, I need to be adjusted. Again, it's still order one. 218 00:28:07.020 --> 00:28:18.390 William Cheng: Okay, so you can see that, you know, when you call free why in these three cases. I'll be here at the performance is going to be order one. So the performance actually really good. Again, that's why we'd like a W plus okay who 219 00:28:18.690 --> 00:28:24.600 William Cheng: Are you should do this in order one. Right. But what about in the worst case, when you call free why the previous block in the next bar. They're both busy. 220 00:28:25.620 --> 00:28:33.690 William Cheng: While so you know case you need to find the right place to insert the free blog back into the free list in that case is still going to be order end because on the average is going to take you 221 00:28:34.200 --> 00:28:39.900 William Cheng: Over to the search for us to find the place that you to the to the you need to add apply the bob back 222 00:28:40.200 --> 00:28:50.310 William Cheng: OK, so again in my forward to listeners call my phone to listen, sir. Before or after right but I still need to find the spot. So you can either before or after right and finding this ball is going to cost you order at 223 00:28:51.180 --> 00:29:00.210 William Cheng: Okay. So, by the way, you know, if you think about my forward to this. Right. The worst case operation is the fine operation. If you need to find something you need to traverse the entire list or traverse on the average 224 00:29:00.450 --> 00:29:03.720 William Cheng: Halfway through the list and that will give you the worst performance. Okay. 225 00:29:05.370 --> 00:29:05.760 William Cheng: All right. 226 00:29:07.380 --> 00:29:12.210 William Cheng: Okay, so, so, so anyway, you know, over here, it's sort of also demonstrate the importance of 227 00:29:13.260 --> 00:29:19.140 William Cheng: You know, you should also use this to sort of systems. So to understand boundary type. I'm going to clean up this page a little bit. Okay. 228 00:29:19.500 --> 00:29:30.990 William Cheng: So you have a point over here for a while. Over here. So Robin. If you free this point or twice. Okay, so when you free this point, you're over here. Right, we're going to end up with this situation over here if he called free. The second time. What's going to happen. 229 00:29:32.040 --> 00:29:41.430 William Cheng: What's the remember in my for two lists, right, I'm gonna, I asked you, when you try to unlinked something you are not allowed to check whether the elements in the list or not. Okay. Because if you do that, it's going to be order. 230 00:29:42.030 --> 00:29:47.130 William Cheng: If you keep checking the every operation is going to be older than the performance of the memory allocators not gonna be acceptable. 231 00:29:47.460 --> 00:29:54.720 William Cheng: Okay, so in the real memory allocated they do exactly the same thing when you try to free something they're going to assume that the application programmer does not make a mistake. 232 00:29:55.230 --> 00:29:58.890 William Cheng: Okay, so therefore, when you free a pointer, they won't check whether this point has been freed already 233 00:29:59.370 --> 00:30:04.110 William Cheng: Okay, so they will not go to y minus eight over here to check whether this one is in use or not. 234 00:30:04.590 --> 00:30:13.800 William Cheng: Okay, so even though you can actually if you turn on debugging fly to check that for you. But typically don't do that. So, what it will do is that it again. Then we just follow the recipe that I just mentioned before I start modifying memory location over here. 235 00:30:14.040 --> 00:30:16.530 William Cheng: If all these you know every location they have changed. 236 00:30:18.000 --> 00:30:25.500 William Cheng: Up because what happens is once you come back to this big, big block. If your program called Malik well the the data. My allocated from this blog and now all these numbers will be wrong. 237 00:30:26.070 --> 00:30:33.630 William Cheng: Okay, so if you want. If the memory allocated of blindly modify this data structure based on the information there. And now we're going to end up with memory corruption. 238 00:30:34.260 --> 00:30:40.680 William Cheng: Okay, so, so again, in this case, who is causing the memory corruption will consider what is the memory allocation that's causing the memory. 239 00:30:40.950 --> 00:30:46.890 William Cheng: Corruption, because what it does is it will take all these values to all these pointer math and they start modifying memory location left and right. 240 00:30:47.430 --> 00:30:52.620 William Cheng: Okay, even though they're not supposed to do that. Okay. But you are the one that give permission to the memory allocated to all these 241 00:30:53.460 --> 00:31:03.930 William Cheng: All these all these changes because you are freeing this pointer twice that. Another thing that can happen is that, you know, over here, these are the data. The data that 242 00:31:04.470 --> 00:31:13.620 William Cheng: You promise that you only touch. But what if you actually use a little more than that. If you use this more than that. You're going to wipe out these boundary tag over here. Well then, you know, when do you know what 243 00:31:13.890 --> 00:31:18.720 William Cheng: Your application. Yeah, we turn another blog or try to allocate another blog. Then the other blog will get confused. 244 00:31:20.070 --> 00:31:26.700 William Cheng: Okay, so now, because again, if you write beyond you know the this blue region over here. It's also memory corruption. When you start wiping out this data. 245 00:31:26.850 --> 00:31:35.640 William Cheng: And if you wipe out the size over here. It's going to be really bad, right, because you know what we call it. What will we do call Se we just going to take the value. We're going to assume that the values are perfect. 246 00:31:36.690 --> 00:31:43.560 William Cheng: Okay. So, therefore, if you mess this one up while you're going to be in big trouble. Right. So, so those are the husband. So those are we call a memory corruption, but 247 00:31:43.740 --> 00:31:51.090 William Cheng: Because you're corrupting your address space, right, because the entire he'd be able to address space. So if you write beyond your memory brought you are corrupting your own memory. 248 00:31:52.260 --> 00:32:02.880 William Cheng: Okay, so, so, so when you grow up your memory. Is that allowed right, clearly it's a lot because it's how you address visual memory alligator is working for you. So if you say mess this one up anyway you want. Watson, so they have the permission. 249 00:32:03.300 --> 00:32:12.030 William Cheng: That. Similarly, if you have a partner point here, if he if you actually change us negative or a index, you're going to modify location. You know, before you. That's totally allowed 250 00:32:12.390 --> 00:32:14.430 William Cheng: But in this case, again, you're gonna end up with corrupted memory. 251 00:32:15.390 --> 00:32:26.760 William Cheng: Or as a please, please understand what we said corrupted memory is typically to say that you know you have a block of memory that you're supposed to use. But somehow, you know, either the memory alligator or you modify some other memory locations. 252 00:32:27.420 --> 00:32:38.010 William Cheng: Okay, so yeah, why don't why, why does it happen because you have a bug in your code, right. Some people always say, oh, the memory. Okay. It has a bug. Well, no, no memory of it doesn't have a buck. Okay, chances are it's a buggy. Yeah. 253 00:32:39.780 --> 00:32:48.210 William Cheng: Alright, so again. So, so the bad news. Over here is that you are when you modify these location you allowed to do that because they are party address space. So when does your program actually crash. 254 00:32:49.200 --> 00:32:56.790 William Cheng: Okay, so later on when you call Malik and free, right. So, it is possible that you have a very, very long list right you know the memory that you crop is at the bottom over here. 255 00:32:57.060 --> 00:33:06.330 William Cheng: Okay, so from this point on, if you keep calling Malik, if you call me Malik Malik Malik. They're all happen at the top of the memory allocation chain over here. Well, in our case, it's not going to cause any segmentation ball. 256 00:33:06.630 --> 00:33:12.240 William Cheng: Okay, but eventually when you try to traverse the freelance to the bottom over here. All of a sudden, you don't get a segmentation ball. 257 00:33:12.690 --> 00:33:15.540 William Cheng: Okay, so when you corrupt memory. There's no warning. 258 00:33:15.960 --> 00:33:27.150 William Cheng: Okay, just later on when you're calling Malik and free all of a sudden your program crash. So in this case, you actually have no no idea when you corrupt memory. You might you might have current memory 1 million instruction earlier. 259 00:33:27.930 --> 00:33:38.400 William Cheng: Okay, you might crop your memory, long time ago and now you know you know your program crash because it finally reached the best spot. Okay, so that's why memory corruption bugs are very, very difficult. 260 00:33:40.050 --> 00:33:44.610 William Cheng: Or is it again. This is also, you know, when you asked me to help you to look for memory corruption, but 261 00:33:45.210 --> 00:33:57.180 William Cheng: I don't have any special powers. There's no way for me to actually he'll find these bugs right if you corrupt memory over here, long time ago and now you're pulling out all of a sudden crash. I mean, and nobody can actually solve this problem. 262 00:33:58.200 --> 00:34:08.550 William Cheng: Okay, so in the project web page, there's a tool called Val grind. So, Val grind basically what's what he's had to do they try to protect all these memory blah them. The, the memory blog over here so that way you try to 263 00:34:09.000 --> 00:34:15.960 William Cheng: Try to traverse these memory allocation chain, he will try to die as early as possible to hope that it will help you to find these memory corruption, but 264 00:34:16.530 --> 00:34:23.640 William Cheng: Okay, can they find all memory crushing values on the clear, the answer is no, because if they can do that there will be no bugs you know inside any kind of software. 265 00:34:24.120 --> 00:34:31.710 William Cheng: Okay, because then they will be able to find all these books. Okay, so the fact that the bugs are there. That means that there's no to out there that can actually be able to find your these bugs. 266 00:34:32.370 --> 00:34:39.480 William Cheng: OK. So again, please understand that and please understand, I don't have any magical power that can find these kind of bugs for, you know, 267 00:34:41.670 --> 00:34:45.120 William Cheng: All right, so what else is here. 268 00:34:46.320 --> 00:34:53.370 William Cheng: Alright, so, so, so the first one hour them right or when you call Malik, you know, on the average is going to traverse half of the free list. 269 00:34:53.820 --> 00:34:59.850 William Cheng: So therefore, it's going to be order n. And we call free now, you know, in the worst case is going to be order. 270 00:35:00.720 --> 00:35:06.300 William Cheng: So therefore, when you call Malik and free, both in both cases, you know, in the worst case, they're all going to be order. 271 00:35:06.870 --> 00:35:13.770 William Cheng: Okay. These perform this performance is actually okay in the user space program, but when it comes inside the Colonel, it will actually be 272 00:35:14.250 --> 00:35:15.270 William Cheng: Actually be not acceptable. 273 00:35:15.870 --> 00:35:22.560 William Cheng: Okay, why is that right, because when he tried to allocate memory. Is that a colonel. If the performance them to be proportional to the length of the memory allocation chain. 274 00:35:22.770 --> 00:35:29.460 William Cheng: And then later the memory allocation is going to be a very, very long while, then you're kind of will be so slow that that's really not a good idea. 275 00:35:30.120 --> 00:35:36.300 William Cheng: Okay, so typically the memory allocated inside, Colonel. We want to have predictable worst case performance. 276 00:35:37.020 --> 00:35:45.690 William Cheng: Got want to be able to say, well, when it comes out, Colonel. We can actually tell you what is the law, you know, what is the worst case, are you going to stay inside of Colonel before you return back to the user space program. 277 00:35:46.350 --> 00:35:53.790 William Cheng: I mean certainly will be nice if you know that. Okay. So, therefore, you know, typically we're going to see that inside, Colonel. We're going to see different kinds of memory allocated. 278 00:35:54.330 --> 00:36:03.300 William Cheng: Well, so we're going to see two kinds of memory allocation over here and they are. Are they are using our colonel. One is how the buddy system. The other one is something called the buddy List of 279 00:36:04.200 --> 00:36:11.160 William Cheng: Different things and the other one is called the slap alligator. Okay, so we're going to briefly take a look at these two memory allocated and then we're going to move on that. 280 00:36:11.790 --> 00:36:24.000 William Cheng: Right, the buddy system over here. So what I do is that it organized memory block over here into A into a tree right so it's a binary tree and then yeah, why do you organized so binary tree. So in this case we organize things into a binary tree. 281 00:36:24.510 --> 00:36:34.770 William Cheng: So the worst guys performance is going to be order login. OK. So again, it's going to be logged right based to have an right so even and it's a big number. Log base to events at your small number. 282 00:36:35.310 --> 00:36:38.400 William Cheng: Okay, so this way every time. When you come inside of Colonel if it turns out you kind of 283 00:36:38.880 --> 00:36:48.030 William Cheng: Look at memory, we can actually found the performance. Is that a colonel. So we know that in the worst case, how long are you going to stay inside of Colonel because the colonel organizer memory in as a tree. 284 00:36:48.570 --> 00:36:53.670 William Cheng: Then also another characteristic of the buddy system is that there's a minimum allocation size. 285 00:36:54.150 --> 00:37:00.390 William Cheng: So typically, this one is going to be four kilobytes. Okay, which means that whatever size you want. If you say if he said now one bite. 286 00:37:01.050 --> 00:37:08.340 William Cheng: Gary and Chris, how many buyers are going to get, you're going to get 4096 bytes. Okay, we mentioned before, there are two kinds of fragmentation. One is called internal fragmentation. 287 00:37:08.580 --> 00:37:16.350 William Cheng: The other one is called external fragmentation, the first bit memory allocated to the external fragmentation and the buddy system will give you internal fragmentation. 288 00:37:17.130 --> 00:37:25.890 William Cheng: Okay, so if you allocate one bite, you're going to get 4096 bytes. So you think you only have one bite, but the other 4095. Is there a completely unusable because you don't know they're there. 289 00:37:26.850 --> 00:37:40.920 William Cheng: Okay, so therefore that's one downside of buddy list over here. It's going to be a little more wasteful in memory. Okay. But again, that's a typical speed, you know, sort of space time trade off by using more space you're going to end up with sort of faster algorithm. Yeah. 290 00:37:42.420 --> 00:37:54.810 William Cheng: So, so one main thing that the buddy system do is that they do so that so the smallest allocation is four kilobytes. They also they're going to allocate boss in the multiples of to to the i times four kilobytes. 291 00:37:55.110 --> 00:38:04.950 William Cheng: Okay, I was going to be an integer medicine. So that means that you can allocate four kilobytes, which is one times will get advice, two times four kilobytes four times four kilobytes 816 32 292 00:38:05.430 --> 00:38:15.030 William Cheng: The power of tues multiply by four kilobytes. OK. So again, it can be really wasteful in memory. You know, so, so, so for example if you want to ask for, you know, 293 00:38:16.350 --> 00:38:17.280 William Cheng: 11 kilobytes. 294 00:38:17.970 --> 00:38:24.270 William Cheng: Okay. So in this case, you know, you can only allocate four kilobytes eight kilobytes 16 kilobyte one this is going to end up with a 16 kilobyte blah. 295 00:38:24.420 --> 00:38:35.070 William Cheng: You're going to be end are wasting five kilobytes. Okay, if you try to allocate 17 kilobytes blog over here, you got to end up with a 32 kilobytes, blah. Again, you're wasting you know 15 kilobytes of data. 296 00:38:36.120 --> 00:38:40.980 William Cheng: Or is it again speed, you know, speed and space, space time trade off. Yeah. 297 00:38:42.630 --> 00:38:43.380 William Cheng: So so so 298 00:38:44.610 --> 00:38:48.780 William Cheng: So so so the way the buddy system work is I'm going to sort of take a look at example right here now. 299 00:38:49.080 --> 00:38:57.690 William Cheng: I'm going to start out with a big blog and the big blog. Again, it has to be a multiple of to to the I multiply four kilobytes. So example that we see over here is going to be two to the five. Sorry. 300 00:38:58.410 --> 00:39:00.780 William Cheng: To two to three over here are kinds four kilobyte 301 00:39:01.350 --> 00:39:06.210 William Cheng: Two, three, go to a time where you go to 3032. So let's start with a 32 kilobyte block. 302 00:39:06.480 --> 00:39:16.170 William Cheng: And when you call Malik, you know, 4000, what would you do right so the smallest broad that can contain 4000 by is going to be the smallest block which is 4096 bytes. 303 00:39:16.440 --> 00:39:20.760 William Cheng: Okay. So if we start with a big blob over here. So what we're gonna do is, I'm going to split in half. 304 00:39:20.970 --> 00:39:27.510 William Cheng: When you split the hub is not divide into two Brock's each one of them are exactly 16 kilobytes law 16 kilobyte over here is going to be too big. 305 00:39:27.810 --> 00:39:32.700 William Cheng: Okay, we're going to end up with two of them. One of them. We're going to keep and the other one. I'm going to continue to split up. 306 00:39:33.420 --> 00:39:45.450 William Cheng: You know, half at a time that so never going to take the 16 kilobyte back over your split into two again eight kilobytes to to to be when I keep one of them, and the other one. I'm going to split it into two four kilobytes block and one of them. 307 00:39:46.260 --> 00:39:55.830 William Cheng: One of them going to return back to whoever asked for it. Okay. So in this case, this is going to be a kernel memory alligator. So maybe the function is not going to call calm Allah is going to be no as came out. 308 00:39:56.670 --> 00:40:05.130 William Cheng: There. So there's a kernel version amount as a winning call came out leg over here we're going to end up returning a pointer that points to the first by in this 4096 by blah. 309 00:40:06.360 --> 00:40:13.860 William Cheng: OK. So, again, in this case, there'll be 96 bytes over here. It's going to be available to us because we're going to end up with internal fragmentation that 310 00:40:14.550 --> 00:40:17.310 William Cheng: Alright, so we're gonna see some example how the elegant these block. 311 00:40:17.700 --> 00:40:27.330 William Cheng: So the reason that these are called. This is called a buddy system is that, you know, when you break up a blog into these two blocks. These two blocks are known as buddy is some, some people call them twins. 312 00:40:27.600 --> 00:40:31.620 William Cheng: Okay, they're not exactly the same size and, more importantly, they are aware of each other. 313 00:40:32.490 --> 00:40:44.160 William Cheng: Okay, so if you are given one block over here. If you say, who's your buddy, they can actually compute the address using pointer math and try to sort of figure out, you know, who's the body and, more importantly, they can find out whether the buddy is allocated or is free. 314 00:40:44.940 --> 00:40:53.880 William Cheng: Okay. So this guy's when we started off with a 32 kilobytes blog. And when we do all the split it. We're going to end up with one fleet block of 16 kilobyte one free block of eight kilobytes. 315 00:40:54.090 --> 00:40:58.230 William Cheng: One free Bronco four kilobytes, and one one allocated block or four kilobytes. 316 00:40:58.590 --> 00:41:04.410 William Cheng: That. So these two parts over here, aware of each other. So later on when you try to free this particular blah, what should you do 317 00:41:04.740 --> 00:41:13.500 William Cheng: Right. So again, when you try to free your body. You need to coalesce memory. So this block become free. You need to find out whether your body is free or not. If your body is free that you need to call us memory. 318 00:41:14.310 --> 00:41:18.750 William Cheng: Okay, if your body is now free. Why not because you don't do anything. So in this example over here if the body is free. 319 00:41:18.870 --> 00:41:25.440 William Cheng: We're going to combine them into a bigger a kilobyte block right and then as soon as we do that we need to also find out whether it's buddy is free or not. 320 00:41:25.620 --> 00:41:37.320 William Cheng: And then if the, if it's funny. It's free over here again we need to coalesce memory, create a 16 kilobyte blog over here. And then we have to ask whether the buddies is free or not, if the body is also free then again we need to combine them into original 32 kilobytes one 321 00:41:38.580 --> 00:41:46.110 William Cheng: OK, so again every time when you're free. You need to see if your buddies available to call us if they are the you end up with a bigger blog and then you can you do this recursive we 322 00:41:48.150 --> 00:41:56.400 William Cheng: All right, so, you know, so, so we can sort of show you the pointer math phobia, how to figure out what is the address or your buddy. 323 00:41:57.330 --> 00:42:05.460 William Cheng: So, so, okay, I should sort of have a great SEO over here because you are not responsible for the pointer math right here. Okay, all you need to know. I mean, 324 00:42:05.790 --> 00:42:09.090 William Cheng: I didn't really put a grass over here is that because part of the site, you still have to know 325 00:42:09.300 --> 00:42:21.600 William Cheng: The message over here is that given a blog, you can compute the address of the buddy. Okay. And you can also find out whether the buddies free or not. And this way you can perform your memory call. So, that is, OK, you are not responsible for the equation. Okay. 326 00:42:23.130 --> 00:42:34.230 William Cheng: All right, so. So in this case, what, what, what kind of data structure that you have inside the inside the buddy system. So again, you need to have a link class. But now what they what the buddy system will do is that instead of link all the link list. 327 00:42:34.980 --> 00:42:40.440 William Cheng: In order to link all the free blocks together for every free blog of the same size, they will create a linked list. 328 00:42:41.100 --> 00:42:43.140 William Cheng: Okay, so they actually have an array of linked lists. 329 00:42:43.560 --> 00:42:54.510 William Cheng: So it's going to be a linguist for, you know, for free Brock, there are four kilobytes in size. There's going to be also a linguist for eight kilobytes. It also link. This was 16 kilobyte. They're also linked as for 32 gigabytes. So for every power of two. 330 00:42:54.990 --> 00:43:03.660 William Cheng: Most bye bye for kilobytes. They have a linked list. Okay, so we're going to end up with a rail free a free link to us. They all linked together all the free box of the same size. 331 00:43:04.500 --> 00:43:11.220 William Cheng: That all right. And then, and again, everybody container you use bear to sighs all these kind of. So again, we're not going to go into too much detail. 332 00:43:12.960 --> 00:43:18.150 William Cheng: Later, but I'll tell you why we're not going to get into too much detail that, what does it this example how to 333 00:43:19.020 --> 00:43:31.170 William Cheng: How to allocate out of the body, less this example here we go so so so to make the language LITTLE EASIER over here, we're gonna call it a four kilobytes block which is the smallest allocation size. We're going to call it a page. 334 00:43:32.610 --> 00:43:41.130 William Cheng: Okay, so four kilobytes. Right. It's going to equal to one page over here. Okay. So initially, we're going to start with 16 pages, which means that we're going to start with 64 kilobytes. 335 00:43:42.300 --> 00:43:48.360 William Cheng: Okay. So in this case, you know, this is going to be assisting us in kilobytes or again our entire you know he will be here. 336 00:43:49.770 --> 00:43:53.820 William Cheng: Is going to be one giant free block which is 16 pages long, or 64 kilobytes law. 337 00:43:54.450 --> 00:44:01.500 William Cheng: Okay. And again, the minimum allocation over here. It's going to be one page. Yeah. So in this case, what is our free list look like right are free list is 338 00:44:01.830 --> 00:44:11.280 William Cheng: Going to be over here is going to be a ray of free list there, so, so, so, so in this case okay i mean the the page address over here. I'm going to use 0123 all the way to 15 339 00:44:11.910 --> 00:44:22.320 William Cheng: So if I use zero as a null pointer and zero as a page address over here is going to be really confusing. So I'm going to use the the notation over here. The Omega three omega represent a null pointer. 340 00:44:23.460 --> 00:44:28.770 William Cheng: Okay, so over here. What I'm saying over here is that, you know, the to to the to the to the k over here is going to be 341 00:44:29.190 --> 00:44:40.800 William Cheng: The, the size of the free blah, the size of the free blah, right. So over here, this is a 22020 equal to one. So, you know, do I have any free block of size one page. 342 00:44:41.580 --> 00:44:47.880 William Cheng: Okay, so if you look at this picture over here. I only have one free block of size 16 pages. So I have no free block of any other size. 343 00:44:48.210 --> 00:44:54.120 William Cheng: OK, so the free list over here to say for all the other sides of feelers over here. There are empty, so therefore they pointed enough pointer. 344 00:44:55.020 --> 00:45:05.520 William Cheng: Okay, the last one over here to to the floor to to the for over here is 16 right so we have one one free blog of size 16 pages and the location for that free blogs, as the page zero 345 00:45:05.850 --> 00:45:12.300 William Cheng: Okay, so I have a zero here. So when you think about 00 is re index. You can also think about is, is a pointer point to this block right here. 346 00:45:13.170 --> 00:45:19.890 William Cheng: That. So in this example, I'm going to start with one free blog of 16 pages and I don't have a free backup any other sizes. Yeah. 347 00:45:20.700 --> 00:45:25.590 William Cheng: So let's run through this example we had the first thing that I will do over here is to allocate two pages. 348 00:45:25.800 --> 00:45:39.810 William Cheng: So again, I can ask for five kilobytes over here. I'm going to end up with an eight kilobytes page over here. So I need to contiguous four kilobytes pages over here, guys. And this guy's. Well, what would I do, I want you to use a free list over here to see if I have to see if I have 349 00:45:41.190 --> 00:45:44.220 William Cheng: To see if I have a page of that size. Okay, so in this case. 350 00:45:44.460 --> 00:45:56.160 William Cheng: You know, I need a page size to to to the one equal to two. So, therefore, you know, this is the freezers, I need to look at over here. Okay, so this is true to the one upsides two pages. As it turns out, I don't have any, you know, free page of size to 351 00:45:56.940 --> 00:46:00.600 William Cheng: Okay, so next thing I need to do is to see if I have a free page of size for 352 00:46:01.050 --> 00:46:05.760 William Cheng: Okay, so in this case I this as your various important if I have a pointer that point to this location over here. 353 00:46:06.000 --> 00:46:14.340 William Cheng: All I need to do is to income and a pointer to the go to the next location over here to see if I have any page of size for and I don't have any page of size four pages. So therefore get 354 00:46:14.610 --> 00:46:26.580 William Cheng: an incremental point over here. Go to the next wise to see if I have any page a size eight. I don't have any page size AWS keep going over here. The next one over here is going to be the page of size 16 and I have one sitting and memory location zero 355 00:46:27.630 --> 00:46:34.380 William Cheng: Okay, so then I'm going to apply the algorithm that I mentioned before, I'm going to take this blog over here divided in half, right, and then dividing. How are we here. 356 00:46:34.560 --> 00:46:42.360 William Cheng: Right. One of the them over here. I'm going to continue to subdivide the other one over here is going to become a free blog. So therefore, in this case, I need to keep track of it right here. 357 00:46:43.350 --> 00:46:51.570 William Cheng: Okay, so this pointer over here. These have address of a so they will point to this one, right, because now I have a block of size a starting at page address eight 358 00:46:51.900 --> 00:46:56.040 William Cheng: Okay, so I put number eight over here and for the other part over here. I'm going to continue to subdivide 359 00:46:56.550 --> 00:47:05.520 William Cheng: Guys have again. This one I was subdivide over here. I'm going to end up with a block of size for and now that will be a free blog, I need to keep track on the inside my feelers over here. So here is a pointer. 360 00:47:06.180 --> 00:47:14.550 William Cheng: To process number it to page number four. So they will apply right here. Right. And the beginning part over here. I'm going to continue sub divide and divide into two, you know, 361 00:47:15.120 --> 00:47:16.770 William Cheng: Sort of. Sort of to Brock a page to 362 00:47:17.160 --> 00:47:18.660 William Cheng: Write the first one over here. 363 00:47:18.900 --> 00:47:29.940 William Cheng: Now become a free blog so I need to use this entry is over here. And remember appointed appointed this one over here. And the first one over here is going to be the two pages. I'm going to return back to the Colonel, because the colonel want a block of size two pages. 364 00:47:30.630 --> 00:47:42.840 William Cheng: That. So, therefore I'm going to end up with David, you will look like this. Right. The 16 pages over here is going to divide into a block of size eight a blog a size four buckets size two and also a block of allocated size of two pages. 365 00:47:43.140 --> 00:47:54.330 William Cheng: Okay, the freedom is over here. What does it say over here I have no block of size one page. I have one block of size two pages, starting at page address number to over here I have a block of size for starting a page. 366 00:47:54.870 --> 00:48:01.530 William Cheng: Address number four we hear of a block of size eight over here starting at page address number eight, and I have no block of size 16 367 00:48:03.210 --> 00:48:11.760 William Cheng: Okay, so remember these number over here. They're actually pointers. They're actually re index, they are not the size of the blog, the size of the blog over here is the re index. 368 00:48:12.600 --> 00:48:23.730 William Cheng: Okay, alright. So if I continue evolve over here. What are the next time or the Colonel's cause came out and they want a block of size for right so the first thing I need to do is I need to go to the feelers to see if 369 00:48:24.300 --> 00:48:28.200 William Cheng: It's available if it's available. I would just use it right. So in this case, you know, 370 00:48:28.530 --> 00:48:39.210 William Cheng: For equal to, to, to the two. So I go to this entry is over here and I already have a block of size for and that's an all nighter page address four pages dress for is right here. So I'm going to allocate this blog over here. 371 00:48:39.630 --> 00:48:44.820 William Cheng: You know, and then return back in the kernel of. So in this case, this the pointer over here is going to get turned into a null pointer. 372 00:48:45.060 --> 00:48:57.360 William Cheng: Right. So, therefore, when I'm done. It will look like this. I still have one block of sys one free block of size to starting at pages as to over here. I also have a block of size eight starting at page number eight. Right. All the other ones are not available. 373 00:48:58.080 --> 00:49:05.310 William Cheng: That the next thing I would do is I will come, you know, came out of two pages, right. So it again will come over here to this data structure there. 374 00:49:05.820 --> 00:49:13.350 William Cheng: So to to the whiteboard or two over here I have a block of size to already. So, therefore I'm going to follow the point over here. And then, and then returned this blog over here. 375 00:49:13.710 --> 00:49:19.650 William Cheng: To the colonel and now this will go to omega audio pointing out. Okay, so when I'm done. It will look like this. 376 00:49:20.370 --> 00:49:26.790 William Cheng: That. So again, at this point, only have one block of size a starting at page address eight over here and then all the other ones. There are no puncher. 377 00:49:27.330 --> 00:49:35.010 William Cheng: Yeah, so if the colonel here. As for another two pages over here. Again, I'm going to come to this data structures that I have no you know block of size to 378 00:49:35.340 --> 00:49:39.780 William Cheng: Know block of size for I one block of size eight over here. I'm going to divide them into two 379 00:49:40.380 --> 00:49:47.100 William Cheng: This. The second part of the part on the right over here. Go back onto the free list right so so now if a block of size for 380 00:49:47.520 --> 00:49:53.700 William Cheng: Which is equal to, to, to the to over here. So now this one, I need to put a number 12 because the page address for this one is going to be able to 12 381 00:49:54.060 --> 00:49:58.950 William Cheng: That the first one over here. I'm going to continue this up divided into a block of size to at about the size to 382 00:49:59.160 --> 00:50:08.010 William Cheng: This block of size to is free. So, therefore I'm going to change this point here to 10 so that I can keep track of this free blog and then the rest of it. I'm going to return back into the kernel. So they become allocated block. 383 00:50:08.490 --> 00:50:21.990 William Cheng: Well, that's a little when I'm done, I have a block of size to at a page address 10 I also have a block of size 12 pages two pages as 12 sorry about them size size for a page draft while and all the other point over here. They're all know punch it 384 00:50:23.520 --> 00:50:29.100 William Cheng: Alright, so then this is how you perform every application when you perform, you know, people for free. You just have to 385 00:50:29.460 --> 00:50:36.030 William Cheng: You know, again, you know, find out who your buddy is find out whether they're free or not. If they're free then you call this memory well alright 386 00:50:36.810 --> 00:50:46.920 William Cheng: So that's all I want to say about the buddy system. So this weekend, are you going to get the source code, you know, for you know for your Kronos I'm in the kernel assignment has a full blown buddy system. 387 00:50:48.210 --> 00:50:57.690 William Cheng: Okay, so there's a file in the kernel called page Darcy Gabby's I see over here. Okay, I'm gonna sort of warn you, the code is our page, but I see it's almost impossible to understand 388 00:50:59.310 --> 00:51:03.240 William Cheng: Okay, so you can look at it but don't spend too much time on it. Some people say, oh, I have to understand how to use 389 00:51:04.470 --> 00:51:08.820 William Cheng: The entire code. Okay. Give up okay spend five minutes on so to get a flavor of what it is. 390 00:51:09.030 --> 00:51:18.180 William Cheng: What you need to know. What do you need to pay attention to is, which one of those functions are external function that you can use to actually, you know, invoke the page alligator. 391 00:51:18.540 --> 00:51:25.800 William Cheng: Okay, so this way. When you say you know what we asked for a sort of a four kilobytes pages which function should you use to ask for four kilobyte pages. 392 00:51:26.010 --> 00:51:30.660 William Cheng: Which functions you use to ask for eight kilobytes pages. Those are the function that you do to find out. Okay. 393 00:51:30.930 --> 00:51:40.380 William Cheng: Don't try to understand the code over there because you know this this code over here. As it turns out, it's very, very complicated because the kernel has to implement this in the most general purpose way. 394 00:51:40.890 --> 00:51:42.540 William Cheng: Okay, so therefore it's going to be really messy. 395 00:51:42.900 --> 00:51:54.690 William Cheng: Guys so I sort of give you the sort of a simple version of that in reality you know the the the buddy system is very, very complicated, right. So, so after the summer session is over. You know, you want to do the phone. If you want to understand the code, go for it. 396 00:51:55.140 --> 00:52:01.410 William Cheng: Okay, but again, try not to do it in the middle of the semester. Okay, this is impossible to understand every detail now. 397 00:52:02.940 --> 00:52:05.070 William Cheng: The next one over here is called a slob alligator. 398 00:52:05.940 --> 00:52:14.790 William Cheng: So, so, so, you know, again, the buddy system over here at the performance order login. What is faster than Otter login. Right. The only fast see my thought is going to be order one. 399 00:52:15.060 --> 00:52:24.720 William Cheng: Okay, so how do you actually end up with the order one alligator. OK. So the idea here is that what is a slob alligator. Some people actually call it an array alligator. So what we do is that when we try to allocate 400 00:52:25.200 --> 00:52:32.250 William Cheng: You know, memory, we're going to our array of them. Okay. So you saw the Colonel, what would you need an array of that right. What about the presses control, blah. 401 00:52:32.730 --> 00:52:44.040 William Cheng: Okay, so. So you said you're currently going to create a bunch of processes. So we can actually create a slap a process control blah, right. So every one of these data starting over here. They're all PCB right PCB over here. So they're all PCB over here. 402 00:52:44.370 --> 00:52:53.280 William Cheng: That. So, so what if, when somebody when somebody wants to ask for process control blog, the first time. Why don't we allocate 1000 process control blah, in a way. 403 00:52:54.840 --> 00:53:08.310 William Cheng: Okay, so this way. We're going to allocate in one shot at this point on anytime somebody asked for it. We just going to give them a re index and then we can return one of these process control blog and this case it will be an order one you'd be able to order one memory alligator. 404 00:53:09.900 --> 00:53:16.530 William Cheng: Okay. So this guy is going to be really fast, so, so, so certainly for process control blah, we're going to use a slap alligator so Apollo. How do you get a slap 405 00:53:17.160 --> 00:53:22.170 William Cheng: Okay, so this lab is going to be 1000 process control blog. Right. So again, you're going to take the size of the positive mental block. 406 00:53:22.440 --> 00:53:30.270 William Cheng: Multiply by 1000 and you're going to call K Malik on it. So the way you get the slab is by calling you know, bye, bye, bye, bye, bye, bye, calling the buddy system. 407 00:53:31.350 --> 00:53:37.860 William Cheng: Okay, so you call it the buddy system to get a big slap. And then you initialize the the entire data structure by setting up all these process control, blah. 408 00:53:38.040 --> 00:53:44.490 William Cheng: You can you can actually initialize all these positions of law. So anytime somebody asked for it. You just give them an array index, are you going to get a capacitor, blah. 409 00:53:45.240 --> 00:53:49.770 William Cheng: OK, so the average performance, you know, have a memory allocated like this is going to be order one. 410 00:53:50.520 --> 00:53:52.200 William Cheng: Know why the data structure. 411 00:53:52.560 --> 00:54:03.150 William Cheng: That you use often. Is that a colonel. But what about you can implement threads inside of Colonel then you have fear control, blah. So maybe this one's gonna be a 700 block, right. So again, you have an array of their control blog over here. So again, you can allocate 412 00:54:03.660 --> 00:54:06.840 William Cheng: A slab and now anytime you need a snack bar is going to be order one. 413 00:54:07.260 --> 00:54:11.010 William Cheng: Okay. What else do you need a colonel when he's at a kind of, we also need Colonel stack. 414 00:54:11.220 --> 00:54:19.170 William Cheng: So maybe every one of them over here is going to be a colonel stack right so we hear that all Colonel stock over here again with a big slab anytime you want to. Colonel stock or. Here it is. You can have it. 415 00:54:20.520 --> 00:54:26.580 William Cheng: Okay, so in this case we can see that the snap alligator is going to be order one day is going to perform really well. But what about fragmentation. 416 00:54:26.790 --> 00:54:38.160 William Cheng: Doesn't have fragmentation. Why the answer will be yes because he uses the you know the buddy system right the buddy system. A as internal fragmentation. So if 1000 multiplied by the size of the process control blog. 417 00:54:38.580 --> 00:54:43.500 William Cheng: Does not fit the entire the entire slab over here we're going to end up with the internal fragmentation. 418 00:54:44.310 --> 00:54:51.480 William Cheng: That. But you can also see that the internal fraud mutation over here is going to be minimized, because it's only at the end over here. So, so once you allocate a slob over here. 419 00:54:51.600 --> 00:54:59.130 William Cheng: You're not going to allocate exactly 1000 process control blah, you're going to allocate as many as you want and the remaining memory over here is going to be the internal fragmentation. 420 00:55:00.300 --> 00:55:09.090 William Cheng: Okay, so we end up with a little the memory alligator both is as good performance because the other one. And also it has a really good you know space characteristic 421 00:55:09.330 --> 00:55:17.490 William Cheng: Because the other the internal fragmentation over here is minimal. How can it possible we have a memory allocated as both, you know, a fast and also a memory efficient. 422 00:55:18.270 --> 00:55:26.010 William Cheng: Okay, so there's no free lunch, we must be paying. I mean, we see something very suspicious going up, you must say that, hey, you know, there's no free lunch. Where are we paying 423 00:55:27.090 --> 00:55:36.600 William Cheng: Okay, so why are we paying for in this case. Okay. As it turns out, you know, the slap alligator is it's not a general purpose out of the general purpose of memory alligator. 424 00:55:37.230 --> 00:55:46.650 William Cheng: Okay, it's only good for allocating process control blocks that control block or how's that okay but but fortunately for the Colonel. The colonel colonel knows what kind of data structure has 425 00:55:47.490 --> 00:55:54.000 William Cheng: So the Colonel. The Colonel is I'm not like a user program right they use a program you'll before you write the program, you actually, you have no idea what kind of data structure that you need. 426 00:55:54.240 --> 00:56:06.630 William Cheng: For the Colonel. The Colonel know exactly what kind of data structure that they can provide. Okay. They need a process control, blah, blah, then the current status of the current on knows every kind of data blog that any. So therefore, Colonel is suitable to use a slab alligator. 427 00:56:08.220 --> 00:56:12.030 William Cheng: Okay. Is there any things that a kernel that you don't really know how big the data showed us 428 00:56:12.510 --> 00:56:19.500 William Cheng: As it turns out, there is. Okay, so therefore there's still a point. Now, you know, for the buddy system to exist for example inside your 429 00:56:20.130 --> 00:56:26.460 William Cheng: You know your device driver. Okay. Can the colonel predict the device driver, what kind of data structure inside of device driver. 430 00:56:26.880 --> 00:56:35.640 William Cheng: So even though the device driver. They are part of the Colonel, but they are written by the device manufacturer. So therefore, in that case, when the device, man. You know, when the device driver need to allocate memory. 431 00:56:36.030 --> 00:56:46.320 William Cheng: Okay, they will not be able to survive alligator because those are the because just because we don't have those slabs of. So therefore, for the device driver when they need to do to to allocate memory, they still have to use a buddy system. 432 00:56:47.100 --> 00:56:54.540 William Cheng: Now alright so this is all I want to say about their the snap alligator. Again, the reason for that is that your winnings your weaknesses, your current problem is, I'm a 433 00:56:54.750 --> 00:57:07.170 William Cheng: Your witness Colonel calm with a full blown snap alligator. It's impossible to understand or has again spend five minutes on it to know how to use the the the sub alligator. That's how do you want to use outside alligator. 434 00:57:07.650 --> 00:57:11.760 William Cheng: Okay, so what you want to do that. There's a function called that you need to make to allocate an entire slab. 435 00:57:11.910 --> 00:57:20.580 William Cheng: So that function, you need to how to use right because, you know, he said, your kernel where you you know we need to allocate process control blah, you need to first allocate a slap a process of President Obama 436 00:57:21.000 --> 00:57:29.550 William Cheng: OK. So again, you need to look so so I guess he's not a colonel. There's a function called there's a phone call slapped I see got slapped I see you need to find you need to find out that that 437 00:57:29.850 --> 00:57:34.740 William Cheng: Particular file and then look at the function over here, look at the function that our export it that you can use. 438 00:57:34.950 --> 00:57:43.530 William Cheng: And then find out which one is one that you can use to initialize initialize lab or to allocate a slab and then you also need to initialize there so that you can that you can get ready to use. 439 00:57:44.970 --> 00:57:57.150 William Cheng: Alright, so, so we are done with memory alligator over here. So I'm gonna, I guess it's a good point to, to, to pause and then in the third video we're going to look at the next part of chapter three. Yeah.