‫Hello and welcome in in this lecture, we're going to implement mallock and free from within the user
‫space applications, so we're going to be able to use malkia and free here as well.
‫So firstly, let's get our reference up for Marlock so we can see this is how the mallock prototype
‫looks like.
‫It takes the size returns of a pointer and it is in the SCD laptop header file.
‫OK, so this is important to note.
‫OK, so and free will be in the same.
‫Had a file I'm sure.
‫So yeah.
‫We're going to implement that today.
‫Now uh so let's begin.
‫So we're going to start by creating a steady laptop.
‫Had a file in the source directory of Jilib and we're just going to go if and festively page to find
‫steelier page hashtag indef and we're just going to go void Marlock uh, size T size.
‫And we're going to include Sadef desktop here as well guys, OK.
‫And we're going to have void free and that's going to take in a pointer.
‫OK.
‫And in return is a void.
‫OK, so these are the two functions we're going to be making today.
‫So just copy and paste those and we're going to go to the source directory again and we're going to
‫create a new file called ASCII lived out.
‫See, OK, and that's going to include AC, laptop, Hajj.
‫And we're just going to paste both of those in there.
‫OK, like that.
‫And this file is going to include Peatross dot org.
‫OK.
‫And inside that, we're going to have the lower level functions for Malcolm free, but we won't make
‫those just yet.
‫We're just going to return zero here for now.
‫So we have a placeholder and we're going to add as to the laptop, C to the Makefile.
‫So just go to the makefile here.
‫We're going to go build
‫astrally live Dotto and then here we're going to go to build Sealab Dotto source
‫Sealab C.
‫OK, and now just go to the banks makefile and we're going to copy and paste the GCC command here into
‫the Makefile because it's just easier just like that, guys.
‫OK, so the includes will be the local directory essentially called the local directory source, I should
‫say.
‫OK, because the make files outside of the source directory and we can get rid of this local include
‫here.
‫We don't need that.
‫Uh, we we're going to have to set Flagg's here.
‫We'll set that to nothing.
‫You know what?
‫We will have a Dashti flag actually for debugging symbols and we're going to compile source S.G. out
‫see into build as we live.
‫Dotto.
‫OK, now everything should work.
‫OK there.
‫Let's just make clean and build that S.H..
‫And yes, we can see that that compound with no errors, so we now have a blank placeholder for us to
‫see.
‫It doesn't do anything, it doesn't allocate any memory.
‫We're going to implement those functions as a colonel command and we're going to make the colonel command
‫impetuous prostatitis.
‫It'll be some sort of lower, lower level mallock we might call it Peatross, Mallak or something.
‫And then instead he lived at sea, will call the Peatross mallock.
‫OK, so it'll be something like that.
‫So once you've done that bit, we can start creating the COL command, which will do now.
‫OK, so if we just head over to Izadi, stop Hajjaj and we're going to make a new system command system
‫command for Malac.
‫OK, and if you just go to ISI d.H Tazi, we're just going to go Isaw NDH Register Command System Command
‫for Mallock, ISI, the command for Marlock and we're just going to create a new file in here called.
‫Well what did we call it here.
‫Let's see.
‫We called it memory heep memory heap.
‫OK, so we're just can create new file in a city called Heap Dot Hage, OK, and we're going to go if
‫and every page or could even be memory.
‫You can even call it memory if you want, but we'll just call it for now.
‫And if and we're going to create a new file in here called Heap Dot see encodes heap dot.
‫Hey guys.
‫And I just go to iodized see and we're just going to copy and paste one of these commands in here.
‫So it's Izadi Command for Mallak I believe is what we called it.
‫OK, and you can get rid of that as well, and, um, yeah, just put this in the header file, guys.
‫Like that in the hip, had a foul and call it Izadi Hate cheap as well, guys, not HelpAge like that.
‫OK, and we go to ISI, the Hajj, we now just need to include keep that Hajj.
‫So we're creating command for now, essentially, guys.
‫OK.
‫Uh, so yeah, as you know, that's all linked up correctly.
‫Now if they call command for they'll go in here and we will return zero.
‫But we want to do something a little better than that.
‫We want to actually allocate some memory for them.
‫Right.
‫We can't just do a simple came mallock like we've done before, because if the process gets killed,
‫that memory will never get freed.
‫So we need to create a special function called Process Mallock, which is responsible for allocating
‫memory and processes.
‫OK, and that will ensure if we got a process, stop Hajj guys.
‫That will ensure that the allocations array is felt OK, and in that way, then when the task is terminated,
‫we can loop through all of the allocations and free the ones that they never bothered to free.
‫OK, perfect.
‫So I decided to come in for mallock.
‫Then we're just going to simply firstly find the sites that they want.
‫So we're going to go in its size equals and we're going to have to include TASC here, guys.
‫Tacitus Stud Hajj.
‫Equals TASC.
‫Uh, get STAC item.
‫And we're going to pass in the test current and we want index zero and Everton's of all point to guy,
‫so we're going to want to cast that to Integer because we know that we're not reading a viewpoint or
‫reading an integer from the stack that they've pushed to us.
‫And that integer is the size, of course, the size that they want to allocate.
‫So for a simple matter, we could just return Kasar lockyear.
‫But like I just explained, if the program gets terminated, we have a memory leak, so that's no good.
‫So in here we're just going to return process malac.
‫We haven't created that yet and it's going to be called Process Malachi's.
‫OK, and we're just going to include task process, start Hage and we're going to pass the process.
‫Currentzis the first argument.
‫So we want to allocate memory for the for the current process.
‫OK, and actually, no, that's wrong.
‫It'll be task.
‫Current process.
‫OK, guys, because process current is the current process we're looking at.
‫OK, so if you if you decide if you decide to further implement your kernel, you might have different
‫videos for different processes.
‫And obviously the current process will be the one you're looking at now.
‫OK, so don't use process, current news task, current process, and that'll ensure that we get the
‫process of the task.
‫That's called mallock because the task is paused once it's running this command.
‫Right, because it was an intrepid team.
‫So, yeah, that's this very simple routine and that's what it will look like.
‫OK, now we need to create process mallock, which we haven't yet done.
‫So just head over to process Stazi and we're going to start making that now.
‫So now in here, we're just going to go up here and we're going to find a nice place to put this.
‫He will do just in the process, which we're going to say viewpoints, a process malac, OK, and that's
‫going to take structure of process.
‫And of course, it's also going to take a size who can use the size there if you want.
‫We're just going to use an integer for the time being just because we have the local variables integer
‫in the size to probably better.
‫Actually, you know what?
‫Let's just call it a SIST.
‫It's probably probably more wise.
‫OK, so then here we're going to go for a pointer pointer equals kazal OK size.
‫And if the pointer was null then we fail to allocate the memory and we're just going to return zero
‫here.
‫So the process knows that we failed to allocate memory for it.
‫If everything was successful, then we just need to add that to our allocations and return it so we'll
‫stop by returning it.
‫OK, so that there we are that ensures we now have a process up, but we now need to put it in an allocation.
‫OK, so inside here, the allocations array.
‫You can see we have we have a possibility of one thousand twenty four injuries, OK, and these are
‫voie points to the Savoy points of Arae.
‫OK, so just by here, then we're going to just go static in it process, find free allocation index.
‫And I think that is a lung function, I guess.
‫And we're going to pass some process here.
‫OK, so we're going to go in it.
‫Wrasse equals minus nomen ma'am.
‫That's the very first thing we're going to do, OK, and we're going to go French equals zero I below
‫program.
‫As max program allocations, I mean, that's what it should be, a plus plus
‫and what I'm going to say, if process allocations I equals zero, then we have a free allocation available.
‫So we go rescue XYZ and then we break from here and then then we return.
‫Wrasse, accept that.
‫That's the algorithm essentially.
‫Now, if there's no allocations available, then obviously this statement will never be true because
‫all of the allocations are anything other than zero, which means they're allocated and it will return
‫Nomen.
‫So that's the idea here, guys.
‫OK, so what we do is we go and it index equals process, fine, free allocation index process.
‫And now we say if the index is below zero, then there's an error and we should return zero.
‫OK, that's what we're saying here.
‫Otherwise there was no error.
‫And we and we know that the index contains the free index.
‫So we go process allocations index equals pointer.
‫And it's that easy, guys.
‫And now if the program crashes or doesn't free the memory, it doesn't matter because, one, when we
‫terminate the program, we can look through all the allocations and free them all because we know the
‫process isn't using them anymore.
‫So the simple algorithm there is, is to loop through all of the allocations and if it's not zero,
‫then kayfabe the memory.
‫OK, so that's done on the termination phase when the process is terminated.
‫Yes.
‫So that's perfect.
‫Well done on there.
‫You've now created a process malac, so just copy and paste the prototype into process stuff Hajj and
‫we'll just put that at the bottom here.
‫And there's your process malac.
‫Now if we go back to that, see everything should run.
‫OK, let's just change that to assist, OK, and we're going to include
‫Astrolabe for the Hajj as well just to resolve that SIST.
‫So if we now go make clean bill that S.H..
‫We can see everything worked apart from this defying reference, that's OK, because we forgot to add
‫heaped of to the Makefile.
‫So just go down here to the Makefile guys, we're going to look for ISSI, the Hajj in here.
‫And there we go.
‫We can see the object file here.
‫So we're just going to add one to this thing now.
‫OK, we're going to go Beeld, ISI, the Hajj.
‫Keep Dotto OK and just copy that, because we're going to need that in a minute and just look for a
‫society that's see in this fall.
‫OK, we see it by there.
‫OK, we can save right by there.
‫That's fine.
‫And we're just going to paste that here.
‫That source, ISI, the Hajj, keep see.
‫OK, and make sure doesn't do clean.
‫We're going to copy and paste the command and we're just going to change this.
‫So we're compiling that see into.
‫He thought, oh, so there's one other thing I want us to do, we can see this ISIS I see is just on
‫his own down here.
‫Let's keep it a bit tidy.
‫Let's let's put it up with the other side.
‫Keep it a bit more organized.
‫So just do that, if you will.
‫One is the same.
‫But, yes, there we go.
‫If we now try to compile that.
‫We can see no such follow directory.
‫That was a minor error, I meant tested death, not AEG Live dot org.
‫That was my mistake as to Def did and that'll resolve sist there for us.
‫OK, so try that again and we can see that works fine.
‫But now it's complaining about the frames.
‫Just got a heap dart and we're just going to forward declare the interim frame just like that guys.
‫OK, and that'll basically say to the compiler, we'll tell you about this later and there we go.
‫We can see that Kampala's perfectly fine and we now have a mallock command for user processes.
‫So that's perfect.
‫So we now need to create the SD version and basically the user side user process version of this function.
‫OK, which talks with the kernel using command force.
‫So when I was going to do that, so just got a post office and down here we're going to go Voyt Pointer,
‫Peatross Marlock size t size.
‫So that's what it's going to look like and we're just going to go Peatross Mallock push the base pointer,
‫we're going to move the stack pointed to the base pointer here.
‫We're going to set the command.
‫To be.
‫Command for I think we said it was, didn't we, let's go in there.
‫Yes, it's common for we skipped that, we skipped out on Pusha in our study, but never mind, we can
‫make that another time if we need to.
‫So we're going to set the extra E for here.
‫OK, manful so command mallock.
‫OK, allocates memory for the process, and we now need to push the size, so we're just going to go
‫push Dewormed SBP plus eight and that'll give us the first argument passed to US variable size and then
‫I'll just push that to the stack.
‫OK, I'll just push the value to the stack of the size provided and now we're just going to go in it.
‫Zaydi pop the base pointer and return.
‫OK, and obviously we need to restore the stack frame.
‫So just before we base pointer we're going to go ADP four, which will ensure that this little push
‫we did here will be restored.
‫OK, so essentially the stack will be restored to the state it was at before this push of the variable
‫size.
‫OK, and then that way then we know that when we do pop, we will be popping off the base point we post
‫here on line twenty nine.
‫OK, so that's all fine.
‫Copy and paste this prototype we wrote in the assembly comment and put it into our study
‫and go to us.
‫Let, let's see.
‫And we're just going to go return Peatross Mallock and we're going to pass in the size that.
‫Perfect.
‫Now we're going to test this and see if that works.
‫One of the thing guys just got a picture of us and we're going to include SAGD Death to the Hajj because
‫it needs to see the size T here for process.
‫Mallak right.
‫OK, that looks OK.
‫I think we can test this actually now.
‫So let's just see if it compiles first.
‫Make clean build that as Hajj.
‫And we see undefined reference to Peatross Myalup, that's because I forgot to explore that again,
‫guys, I keep making that mistake.
‫Go to USA Today and we're going to go global as mallock function.
‫So just remember to always mock the ones you want access to outside of the assembly file as global.
‫OK, so we can rebuild that.
‫And there we go.
‫Work's absolutely perfectly.
‫So what we're going to do now is we're going to modify blank to the ESM to allocate some memory.
‫So, yeah, just by here, we're going to go avoid PTA equals Mallak 512 bytes.
‫And here we're just going to say includes.
‫S.G. Liotard Hajjaj, OK, just like that, so we now just go appointer as well, guys, so the compiler
‫doesn't complain.
‫And now that we've created this, we're almost ready to test it.
‫Just go to the makefile here.
‫We need to ensure that we pass debugging symbols here for Blind Tazi.
‫So here we're just going to go Flagg's equals, uh, minus G and we're just going to go flaks here,
‫OK?
‫Oh, we've already done flags here.
‫Perfect.
‫Just put a space by here then like that.
‫OK, so that's all fine.
‫Let's check the makefile.
‫First celeb we can see we're applying the flags here, Dachis for debugging symbols guys.
‫And we do need them.
‫If we want to debug this, which we're going to have to do because we can't print out images yet.
‫So, OK, now that you've done all that just to make clean build builder S.H..
‫We're going to see the GDP at Simbel File and we're going to go back.
‫So we're in the process directory and then we're going to go programs, blank, blank thought out and
‫we're going to mount that at address zero x four, one, two, three, four, five.
‫OK, just like that, OK.
‫And we should be go brakemen, OK.
‫It will make a break point for us.
‫So let's now go target remote kumu systemis three eight six.
‫Hey stop stop in das JDBC studio Dasch Capitalis.
‫I can see that that keeps happening with where it shows is determined before we can do anything, just
‫close it if that happens.
‫OK, there we are now do see to continue and we can see we now broken on this break point.
‫If we go next next we can see.
‫Hello, how are you as being printed to go next again.
‫And I'll print pointer and look at that.
‫We can see some point to memory.
‫We can see that address returned to us.
‫So we now have a work in Mallak.
‫Congratulations.
‫Now I know that we said that we would do mallock and free in this lecture.
‫However, quite a lot has happened and I don't want to overwhelm you guys.
‫So we're going to make a separate lecture for free.
‫OK, I know we've partially implemented free already on the S3 lab, but I think showing you free in
‫this lecture will be too much for you because you might want to take a break now that you've implemented
‫mallock all you might want to continue, in which case, what's the next election will implement free.
‫Thanks so much for watching and have a great day.