A custom memory allocator can be implemented in C by reserving a large contiguous memory block (such as one gigabyte), dividing it into fixed-size units called 'words' (typically 4 bytes each), and managing allocations through a simple header structure that tracks the number of allocated words, allocation status, and reserved bits; this approach provides fast memory allocation with minimal overhead (one word per allocation) and can be implemented primarily in C with minimal assembly code for memory reservation.
Custom Memory Allocator in C: From Scratch Tutorial for Developers
Added:Welcome to today's show.
So today we are going to do something really exciting, a really low level project.
We're going to code our own memory allocator and we're going to start the code in just a few minutes.
I will just start by showing visually how this is going to work.
So we start out at this point by reserving one gigabyte of process memory.
And what we do is we divide this memory segment, which I call the heap, with a dot in front.
So we will have a load of these different sections, so to speak.
All the way up here.
And so each of these these segments segment is four bytes each or 32 bits.
And I will be calling these words.
So one word is four, but.
And that is the least amount of memory that can be allocated.
So this first one will have index zero and one and two and three all the way up to around 250,000.
So this is our heap.
This is what we have to play with, so to speak.
All right.
And I'm going to clean this up a little bit before we continue.
All right.
So what is going to happen if we want to allocate, say, say that we need a bucket of memory for something that means.
Two words in. There will be a very tight overhead for this memory allocator.
So if we allocate two words, what will happen is.
So this first word will be the header.
So I didn't know this, but make.
And these to others will be the memory that this allocation contains.
The memory that we can use.
And I do know this.
The this purple color is all right.
So let's start by defining how this header should look.
So this is for bytes.
And if we do something like in this arrow down here.
So this is a zoom-in of this header.
So every allocation will have its own header.
And this contains three different things.
This to the left here.
Let's make this a green color.
Then like this.
And and this will be something which I call the W.
This is the number of words that follow.
So in this case, it will be two.
Since we are allocating two words.
Then the next one, which let's make this yellow.
This is a one bit thing.
Maybe I should write this. So this right here is 30 bits.
The green part.
Then we have this, which is one bit.
And this is a boolean.
So let's do this.
It's a Boolean, which I call allocated.
So this basically means that these words that follow this header, they are either allocated if it's true or they are not allocated if it's false.
So when we do an allocation, this becomes true.
And when we free that memory, this becomes false.
Then we have one more thing.
So this second bit, this is also one bit.
And that is reserved.
So this is not used at the moment at least.
But we have one bit in the header that we can play with, so to speak.
This green part right here.
That is the W the number of words that follow this header.
This yellow part is the allocated boolean.
Is the following words allocated or are they de allocated again?
And then we have one bit reserved.
So this one word header is all the overhead that exists in this allocation.
So if we were to make another allocation, let's say we want five words.
Then we start with a with an additional header like so.
And then we follow 1 to 3 for five.
And so we continue.
So we can use almost all of this one gigabyte space.
The only overhead is one word per allocation, which is quite good.
I think so.
If we were to translate this header into C code, we would do something like.
And what does it contain?
Well, it's the lion part of it.
Is this it?
W the number of words.
So then we have the blue allocated one bit.
And then I will make a type declaration.
So we'll do something like typedef struct packed s_header header; Yes.
The header and this data type will be called that.
Like so.
So it's a very simple design.
One small header, then the allocations and then another header and the allocations.
And if we want to, let's say we want a free let's just make this smaller.
Let's say we want to free this first allocation.
So we will zero out these two.
And that's strictly not necessary.
But since we want to have some memory security, I will still do that.
And and the only other thing that we will change is we will allocate that input back to this allocated equal to false.
So if we were to do a third allocation in this instance, and we want to allocate say one word, the only thing we will do is we want to change this. W So currently the W is equal to two because the whole is two words D.
So we will change this value to two one.
So if we were to to do this, then this part will be part of the new allocation.
And then we will change this allocated folds back to true.
So every time we do an allocation, we will traverse this memory heap and find a suitable hold.
And if we don't find a hole when we come to the end right here, we will put the allocation there.
And we will, of course, do a check if this we have here is big enough for the allocation.
Otherwise, we will return with a no memory by setting the ID. Let's see the R number, which is a global variable to this.
And then we will return zero.
And if the allocation succeeded, we will return a pointer to this space right here.
So it will work the same way as.
We will do something like let's say we have an index, a and let's call this string or something.
And we want to allocate say 12 bytes for this.
Then we will do this to R is equal to and we tested to an index. So in and this should be in the star by the way.
Just a pointer.
So in a star and the function name will be called a look.
And then we just give it 12.
So it will work with the same syntax as the regular malloc and return the pointer to that mem space.
And I will also create some aliases.
So I will have something which is called k l0, which will take a number of kilobytes to allocate.
And same with K maybe, maybe malloc is not so well.
Let's say we do alu k and alu the alu for the number of megabytes.
So we will be able to allocate we can make one allocation for almost one gigabyte if we wish to.
Or several big allocations.
And everything will be very fast.
We will code almost all this in c only one little tiny thing, like a 510 minute code of assembly in order to reserve this first block of space in each of these allocations or holds will be called one block.
So a block can be like one word in size, or it can be 250,000 words in size.
So that is the general idea of this.
And it looks pretty complicated when you look through what I have drawn here.
But it's not really very difficult to understand, I think.
And we will also have a free.
I'm not sure what I'm going to call it, but it will work the same way as the free in the C library.
So you give it the pointer to the first memory part of the allocation.
And it will free that.
And we create the whole.
And possibly I will also make some ree which can change the size of existing allocation.
But I'm not sure if I'm going to do this.
And the good thing about this, especially actually for a lot of you guys, is that this is pretty fast to create.
I think it's going to be to it to our episode to code this thing.
And it's a very, very, very good project to have on your resume.
So putting in the those 4 hours of work would probably land you the job that you want to have.
And I also intend to replace the regular malloc with this in all of my code moving forward because everything this is done in use or land.
Usually when we create a memory allocator, we will either use the SBA system call to well allocate memory.
And I think this will run equally well on Windows.
If you create if you use a post-tax compiler like silent, it will run in Merkel OS.
And of course in Linux.
So, yeah, let's get started some guys.
Welcome to today's show.
So today we're going to create our very own memory allocator.
And let's start immediately.
I haven't thought of a name for this project.
A lot before now.
And I'm going to use Bash Spear to create the project.
And you can find this in the repo if you need.
So let's create the Look project.
And we use the default C template.
I will send this to my home and open it up in these calls.
So let's see.
We have a low and I haven't prepared anything in advance for this project.
So we will see how it goes.
I have some general idea in my head though.
All right.
So let's start, as we usually do, by changing a few things.
Let's rename those aliases.
So this is one by two, by four by eight by.
And I also want to have one for int 128.
And since we're probably going to use quite a lot of void pointers and stuff.
Let's create one for void as well.
And I'm going to define a data type, which is called header.
And we're probably going to cast quite a lot to that as well.
So let's create an H for header Star and let's type Def and unsigned.
Let's see B in 128 to int 128.
Mm hmm.
That's also a something of the void type which we are going to call.
Let's call it.
He Right.
And the header is going to be quite simple. So we are only going to use 32 bits.
So let's create a struct and not an and.
This will be a pack struct, which I'm going to call extra.
It had.
So this will contain a 30.
Let's see.
Let's say it's a 30 bit integer which will keep track of the allocate the number of blocks or a number of words or rather.
So let's create the word data type as well.
So one in 32 will be a word.
So we will have word w and this is going to be 30 bits.
And we are also going to have a bool.
Let's call this to locked.
So this says if it's allocated or not.
And this is one bit.
And then we will have one extra bit which is reserved.
So let's say this is also a bool which we will call with served one bit.
And this is going to be used.
So let's see, I think we can put it right here.
And we need to define working unused and where to pack it. Is.
So let's do the.
So packed is going to be an attribute.
And an attribute basically tells the compiler how it's going to handle different things.
And the syntax is quite weird like this.
So that's why I like to make my own definitions.
And we will also have and used it as the same weird format and used.
And that only tell the compiler that it shouldn't give warnings and stuff when we don't use this.
And also to indicate our intent to anyone reading this.
This code.
All right.
So let's type the strip.
I'm not sure if we should have packed here as well.
Let's try and.
Yes. Header And this will be called header like so.
And we're going to create a mem space which is going to be well, it's going to be the heap data type.
So extern heap because we're not going to define this inside of this file.
And this would be a pointer to a men's space like so let's see if this compiles.
We might have to change change it up a little bit and expect the online world shouldn't have these.
Okay.
Compiled without warnings and or anything.
That's good.
So far so good.
Now to the tricky part.
Perhaps I want to define this mem space as one gigabyte of memory.
And that's quite a lot.
Maybe we should make our lives a little bit easier.
No, let's.
Let's do this for now.
And so we need to do this in assembly, I think.
Otherwise, we don't have that granularity, that level of control.
But there won't be many lines of assembly.
So let's call this our He told us in.
And how do we define this?
Well, it's going to be 32 bits and we will make a definition.
Let's say this is the heap size.
So if we do ten, 24 times 1024, then we have a megabyte and then we do time term 24 again.
So now we have a gigabyte and we divide this by four.
So now we have the number of words or a number of 32 bit integers that we need.
So now we define our actual space and we want to explore this as well.
So let's do global namespace.
So first up, we need to define our heap size.
So let's say that he says we're a small age.
It's equal to he says with a big age.
So now we want to reserve w words.
That's integers and we want heap says number of words.
And we want to have a let's jump this out after.
And we want to have a section for this heap.
Let's call it what he and this should be read, right.
I think this is the correct format.
Let's try assemble this thing and see what it says.
So we will do anything else.
And we take the heap and Kate didn't like the read right.
And the space too closed on because she really.
Okay.
So we got a few warnings, but not an error.
And it takes a little while, which is a good sign because it's going to have to create 250,000 of these words.
I wonder if we can specify that this should be treated as a B, as a section.
In that case, it doesn't have to put all those zeros inside of the actual file.
That's see, I think there is a man page for this.
If we go to and Zoom and let's see, that's it for our sections.
Okay.
So this a look.
We want to set the two falls, but how do we do the way where this can be is as is the low.
They can be negated by pretending. No.
Okay.
All right. We want to look, we don't want X we do want right.
Is stored in the disk image.
Okay.
So prog bits should probably be false.
Right.
The threat that this work defines and B, is this like section.
And we can put in an alignment as well.
But it succeeded.
But it's very huge.
And let's check it out if we can.
Can we read elephant under old file?
Yes, we can.
And we want to check out the header and always forget which one it is.
L is for program headers.
Let's create the temporary temporary text section just so we can see what's going on.
So this will be glow?
No, not global.
This would be start.
And we need to export start.
Let's update this thing.
So a look should be true.
It should be full.
Right? Should be true.
Probably it should be full.
No. For all bits.
And a line should be for right.
So let's remove this. Oh, and let's run.
And again I know section attribute.
No prob, it's except for a line they can be negated by pretending.
No, no.
Be okay.
All right.
That went a little bit faster.
And this file is very small, which is good.
And if we were to link this thing so he put.
Oh, and we create temp file.
Can we run this without crash?
No, it will probably crash because we don't have any exit code.
Yeah, but we should still be able to get some information out of it.
So if we were to do it.
Mm hmm.
Mm hmm.
Read a h, t and P k.
We have five section headers.
We have one program headers.
Okay, so this is our heat and this is our many sides.
If we were to take the decimal version of this and we were to hand it to this, we divide it by one megabyte.
Yes. It's one gig in size through eight.
So it works.
I'm not sure about the alignment.
Oh, maybe this is a four, isn't it?
1 to 4?
No, it's a Well, is it?
Well, it's not a big deal.
The important thing is we were able to create this space.
So let's remove this text section and this and let's see if we can link this thing to our C code.
Let's also do something with it.
Let's say we create an in a p and p should be equal to space.
And then we changed the first character into a just so we see if we can do stuff with it.
And let's go to our Makefile so we'll have a heap o which requires close here as we're going to do in some l He Well, the source.
So let's see when we compile this thing, we want to use the he just as well and it's changed this to C 23.
All right let's see what we get, plain and simple.
Let's create this section as world.
Okay.
And what happens when we run this thing?
Does it crash?
It does well, though, because it is a read write memory space.
Let's go to GDP. And just to put the happening.
And so TNT and we go to the T, we call it a low.
So let's just prepare the debugger.
Let's define or F function itself.
And it's a little symbol for the low end and will define it as a three BHP -20.
These are just my default settings and let's also define clear use and we should to clear and All right, so let's start this thing then and run.
Well.
So here it is wide where it crashes.
We try to move into the whatever is inside of E 50, so in four X and x is zero.
Maybe we need to export the pointer instead.
In such data this will be a low key.
No, it's right. It's two. Let's call this underscore min space and let's put the real namespace here and let's make this a pointer instead.
So we will define a double word and the content should be the address of mem space, right?
And now it works very, very nice.
Very, very nice.
All right, so the hard part is done.
I hope so.
No more assembly and the Makefile is done as well.
So now we can start to create this and allocator.
Alright, so how do we do this?
Well, we need to have an A look function to begin with and it's going to work pretty much like the regular malloc.
So it's going to return a void star.
Let's just call this a look without an m and it's going to take a number of by. So in 32, let's see, is that in?
Yes it is number, but it's okay.
So let's create this function below and it's going to take it in 3 to 9.
And we need to convert this bytes to number of words because that is the smallest amount we can allocate.
So let's have a word.
Words.
So first, let's see if we can do like this.
By the way.
So if but it is a multiple of four in that case, we can just take but divided by four.
And if it's not what we need to take but divided by four plus one.
So for instance, if we want to allocate seven, that means that we will need to words because two times four is equal to eight and that has room, which means the seven divided by four will be three and.
This case because we don't do any decimals in three plus one.
No, I mean this should be two.
Let's see, eight divided by four would have been two.
So seven divided by four will be one.
That is the case.
And if we add one to that, we will have two and that is what we want.
So this is good.
So now where words contains how much we want to allocate, now we need to find a whole for this.
And if this is the first allocation, we might want to handle it differently.
Perhaps Not we will see later on, but for now we will.
We will do that.
So let's see how to do this.
I don't want to try something else it's possible to do instead of a regular statement.
Can we do something like this?
So if let's see.
Well, let's create a header first and we need to create some intermediary variable.
So let's call this mem.
So mem should point to the mem space and header should point to mem and now we can shift if header and it's w that is this part how many words that follows and if there is zero it means nothing has been allocated yet.
In that case, what do we want to do?
I'm not if we can use this syntax, but I try and so let's have some code here.
For now I will just exit and let's also print something and and otherwise print something else.
Let's see if this compares. No expression before.
What if we put the parentheses here instead? Hmm.
That actually works.
And if it's not empty?
So that will be.
Let's see. Hmm.
It did not go to the ls clause.
What if we were to amend this?
Do the equal to one?
Yes, it works.
So are we going to use this syntax from now on instead of regular if statement?
It's quite ugly.
Isn't that quite cool that it actually works?
This is just an example of how much freedom you have when you code language like C.
All right, so let's remove this stuff.
So let's see if this is the first allocation and we know the number of words we have.
We want to check if we have room for these words.
So maybe we should create some definitions.
Let's let's words. So how much is the.
Well, it's going to be ten, 24 times ten, 24.
This is one mag, one gig divided by four.
And we get the number of words that we need at least one header as well, which is one word in its.
So let's do minus one like so these are the maximum number of words.
So if words is more than max, words, then we want to return and we need to somehow be able to return different types of words.
So let's say we have an error, which is No, ma'am.
Let's also create a list return error and it takes an X and what do we want to do?
We want to let's see, we want to set our number global variable to the right hand, and then we want to return zero.
And this error number is included in this.
But so if word is more than six words, we want a return of or no, then otherwise we want to make this allocation.
So maybe we should have a different function for the so something like make allocation and we want to give it the number of words and the current point in the heap.
And since this is a new here, it's going to be would have you had to basically.
So if this returns zero, we want to propagate this error to two.
Let's see when we see what this number is, it's defined.
So let's do it in 32 and let's that make the compiler complain.
If this is not the right thing, we should read equal to this and we propagate the no, this is no good.
Let's do this instead.
Let's remember variable to make allocation.
But we pass the words and we pass the header is mem is zero.
We want to propagate our number.
Well, let's just return our number in this case because we cannot set our number to earn number.
This. That doesn't make sense.
But if we have a result so it will return the pointer to this new allocation with the first bite of the allocation.
And we want to return the.
So we'll do this right and this function make allocation is going to take two things.
The first is the number of words and the second is the head.
So we'll have a make a loop, merge words and head or head, have a return value once the head headers words to the number of words, which is this that first we want to check if this is really if we have enough space so we can do some pointer arithmetic because mem space is the first buy of the men's.
So we can calculate where this header is.
So how many words in are we?
So this can be equal to where this would be by and by the way.
So if we take the address of header and we subtract the address of the men's space, then we are left with the number of by in. So then we can calculate the number of words between we can do this.
So words in will be equal to bytes in divided by four.
And just to be say this, we can add one as well.
So, so now we can check if the words we want to allocate is more than the difference between math words and words in than we don't have enough memory to do this.
So in the case we can return no other words.
We should do the allocation.
So we'll set the number of words to the number we want to allocate.
And what else do we have?
This look should be set to true to indicate that these are reserved and now we need to calculate the pointer address of the actual mem space and that is the header.
Thus for the rest is equal to the void and plus for if we return.
Right.
Simple enough.
And if you wonder why we do this, we do this check here and we do the check here.
If we have enough memory, that's because this function may be called from somewhere else.
Maybe we create a real look function at one point.
Then we want to have checks and balances in order to let's see if we can allocate these seven bytes.
We and only had can this shoot the header.
So this should the word assignment to void store from in oh, this should be word that's words and declared should be matched words like this and undeclared should be like this and we should not return the number.
We should just pass it along, which means that we should return to rule instead.
Here.
Okay, compile that we get a zero way to set and we the return.
Our thing does two things.
So we need to put this inside of here.
Okay.
Those macros can fool you.
Laughed sometimes.
So let's check all the places where we have where we use this return thing.
Same here.
Okay. We got a pointer back so allocation succeeded.
Okay, so now we are able to make an initial allocation.
So we need to add the possibility to make several allocations and then we want to make a free function and then we want to make it possible to allocate memory inside of holes that have been freed.
But as far as of yet, everything works.
So we'll be right back after this sub, guys.
And welcome back.
So before the break, we managed to be able to create an allocation, but only one.
So now we're going to improve this program and make it possible to do more than one allocation.
And perhaps we're going to generalize this if statement a little bit.
All right.
So let's see.
And currently we only check if the first block contains zero.
So zero words in that block.
That means that it's the first one and then we a handle it like this.
But it would be better to create some function which locates the first available block and returns that block.
So let's call it find block, perhaps, and it's going to return the head of the block.
So header find block and and I think I'm going to call this fund block prime, actually, because I'm going to have in a macro which calls this thing. So what is it going to take?
Yeah, well we need the current header of the current block and let's see is the all we need.
We could use some kind of counter.
It's not strictly necessary because we can do pointer arithmetic like we did in the make allocation function.
But I think I want the counter in this case because it will make things a little bit easier.
So let's have a word here which counts the number words that we have passed That way we know when we get to the end.
All right, So let's see, maybe we should do the macro first, because I want to have a function like macro, which is called foreign block without the prime.
And it's not going to take any parameters.
And the only thing this is going to do is call fine.
BLOCK Prime And it's going to give the first possible block, which is the mem space.
So we need to cast this into the header.
We have the mem space.
So this is the first block.
So that's where we start searching, so to speak and at this point we have passed zero blocks words.
All right.
And so let's see how we make this function.
So we do do find law and we have a header header and we have a word what's it going to call this one?
Let's just call it and.
Okay, so what do we need to check here?
First of all, we want to check if we have passed too many.
So if an is more than max words minus two and Y minus two, well, if we're going to we want to use this to find never mind.
Maybe we don't need to do that.
Let's just do it like this because we could do it the way I showed because I was thinking the header.
Take the one word and we need at least one word allocation.
So that way too.
But I'm going to need another check as well.
So let's do that there instead.
So if this is the case that, by the way, it's six one thing, this return error function, let's do this instead.
So we do we opened a block, then we did this and then we returned thing and then we do the while false trick.
So the while false makes this run only once.
So it's a loop, but it will not loop run only once.
And then we get this code blocked right here, which means that we can, we can remove this and this, making our code a little bit cleaner and we can do the same here.
We can return the error of no or it does this thing.
Um, so we will have a couple of different possibilities.
One is in the current headers. W So the word count is zero and, and that means that we are at the last block, So let's do our new if statement syntax and so if the header W is zero then what do we want to do?
And well, in this case we are going to use this block but we do need to check if we will write when we use function.
We need one more argument because we need to specify how much we need to allocate. So we need a second word right here and we need this here as well.
Let's put it here.
So this is how many words we wish to allocate. So we will put it right here.
And what are we going to call this allocation.
All right. So this is the number of words we have purchased, plus the allocation.
Maybe we can do it right here instead.
So if and plus a little case and here we can do the first.
If this is more than math words minus two. So the current number of words that we have passed, plus the how much we want to allocate, if that is more than next words minus the header and at least one word allocation, then we return our nomen.
So this is probably the only such check we need.
Or so if this is zero, maybe we can create a bool in. Okay and okay is false as default and if this is zero.
So we are at the last block and we know already that we have room for this allocation.
In this case, we can just maybe we can just do this.
We said okay, is equal to true.
I guess we can do the we do however need let's see right.
We can do this.
So if it's zero then we are okay.
Otherwise we want to check.
We would check the this is no, we don't do that.
Let's see, how is this structured.
We have the W, which is the number of blocks it follows.
We want to see if they are a lot.
So if we were in some block that has been allocated and the allocated, which means the W is positive, but this is false.
So it's not allocated.
And we do also want to check if this block has enough room.
So if this is w is more or equal to the location, we want to do.
So in this case there is a hole and the hole is big enough in that case.
Okay.
It's true.
And I guess these are the only two things we want to check for.
So maybe we can do this a little bit cleaner by doing okay is equal to true or it's equal to true with this is as well and was false.
So either it's zero the W that means that we are at the last block and then we can say that is true because we have already checked that it fits the entire space.
Otherwise we check if it's it's in the allocated and if the hole is big enough that kids we said it's true.
Otherwise false.
Okay.
So if okay is true then we want to then we want to make this allocation or the function name is called find below.
So in this case, we have found our block.
We want to use this for our allocation so we can just return the header.
And otherwise we want to go to the next block and try this again.
So we need to find the next block.
Let's create the void. Mem Let's create the header Prime and maybe also a word and find.
So we set mem equal to the current header and we want to do forward header.
W That's the number of words.
And then we should land on the head of the next block, which is where we want to be so we can search our header.
Prime to the header cast of the that the header or next block.
Um, and we want to set the prime equal to the current and plus this number of blocks.
And now we have all we need so we can simply return and we do a recursion and we do header prime, we push through the allocation and we do n prime.
So and if we were to come here, which we shouldn't, but we never know, then we will return.
And which we need to add or it.
So let's test this function.
Let's start by printing the address of mem space and then we can print this header. P and set p equal to and below.
And let's say we want one 500 words if we don't have a p and we want to print or this term is will.
In other words, we print the address.
So if this works, both of these addresses should be the same in this case because we want to allocate it at the first position.
All right.
Let's see if this compiles maths.
Words should be with the capital M to indicate that is the constant it compiles and we get the same one.
Good. I And what if we do an allocation first?
Then we should get a different result.
Hopefully. Mm.
Oh and they named both be it.
Let's call this header instead.
So let's see, we allocated this in them space.
Is this, let's just see how much different this is.
This should probably be for bytes which is the head.
Right. Good.
And, and our next block is one E how much difference is the from mem space so it to bytes from the mem space is this right because we have one allocation that is one header and we have to words so the three words terms for that 12 and then the next one should be so no it's not calculating properly.
I think.
Why is it two men may be very confused by some words.
So we have memories that had a plus right.
Plus words times four because a word is four bytes.
Let's try this again.
So this is in the flow.
And if we compare that to mem space, it's a and this is what we're looking at.
So this is the first part of the memory space.
So if we take this minus four, we get the address of the header of that block and that's the same as Mum's.
So that is correct that this is just four by first.
This.
So here we take the current header minus mem space.
So how far in are we.
This is zero.
In this case, zero divided by four is zero.
I guess plus one just the two divided by four is zero.
Right.
So with the words to no words is what we passed to this thing make the words and words is in this case it should be two.
So from the first decision to two words, so we have the header and then we have two. But some space and then the next header should be here and we're here 1 to 3.
It should be all right, let's do plus one here.
So we take the header into account as well.
So every new block minus our namespace is nine, but which should be put to one, then we should do four.
It's one word or two.
No words.
Now we want to change our allocation function, so we use our new instead of using.
Let's see, we can still have this, but it's remove this and we search ahead equal to find below and we give it the number of words we want to allocate.
If we get an error, we will just pass that on. So then we don't need her statement here.
Still want to do this check and we allocate based on the current header and that's probably all we need to change and the results should be the same at this point, which is good.
But no, we should be able to do two allocations.
So let's see, 2p2 as well.
Two is equal to a low and we can remove some of this.
So, so let's first will create seven bytes, then we look to kilobytes and let's see what we get.
So this is our second one and it's still three words away, which is true since this first allocation is the same size.
And if we were to do one more, it should probably be 500 there.
Instead, maybe we should try to.
So this should be two, Sheila.
But from this plus, they had which it is nice and by the way not exactly two Sheila but 2000 bytes to be precise to Sheila two kilobytes is to the 2048.
But it works.
It works very well.
So now we can do multiple allocations.
Nice.
And let's also create a simple function which prints out the entire heap.
So let's say voids. So we can do this show prime and we give it a starting point and then we do this same magic as before.
So we do show of nothing is show prime of space.
And I would make this pretty simple.
So void Show Prime.
We have had a starting point.
So let's see.
We can do a y loop.
Let's create another header for as a point.
And as long as we have a P W, we will continue looping.
Maybe we can do the for instead.
So p is equal to head loop as long as we have a p w, and at each iteration p and let's do this mem should be equal to our current position plus number of words plus one times for its and then we set p equal to met.
It's also count if you can set it to.
So every time we iterate we will have one allocation.
The number.
So the first is our n, and then we have our W, and then we have the string.
So we should check if this is allocated.
So we see that it's a little bit the was free.
All right, let's see if this works.
So we should just to show maybe we don't need to print these.
Oh oh for a this should be which to allocate words 500 words one the last words.
All right.
So we have made quite good headway.
Now We can not just allocate one time, we can allocate as many times as we wish.
We have generalized our allocation.
So it gives us a little bit more more freedom.
Then we can print or lower allocations.
And our next is going to be it to be able to free memory.
And we are pretty much done well.
Well, we'll see, won't we?
So if you like this episode place like in subscribe and hit the build button so you know when the next one is released.
And thanks for watching. Thanks for today.
Up Next

Java Garbage Collection Explained: Mechanisms and Trade-offs
@java
50.1K views•2017-10-03

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

Build a Garbage Collector in C from Scratch
@dr-Jonas-Birch
3.7K views•2025-02-21

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science







































