Hey,
I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.
Thanks...
Printable View
Hey,
I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.
Thanks...
I tried this a few days ago.
What I did was to get a chunk of memory from the os (that would require other functions), and give the new function..say memalloc the liberty to allocate from that region.
Is that what you wanted to do?
If not, (for the first part), I don't know of any portable way of doing so.
See A Memory Allocator for an example.
And it's easy to replace the functions provided by compiler or OS libraries - just link in an object file with the correct function definition that you've written and it will be used instead of the default ones. Not that I'd recommend it except for educational purposes.
The moment you step away from malloc, you're in the realm of needing to use OS specific options, so you might want to specify your OS.
GlobalAlloc, HeapAlloc, and VirtualAlloc are other options on Windows.
Of course it is better and safer to use malloc and free, I just made it for a (sort of) virtual machine I was trying to make.
It wasn't actually a new memory manager.
I still used malloc to get the large chunk from the system.
After that I made a huge char array from it, and gave the sizeof(required) amount from it to the required object by casting it to a void*.
I read that this process(except some small details) was an old tradition in unix programming before the advent of the standards.
Of course guestcheap needs to do that.Quote:
Originally Posted by iMalc
:DQuote:
Originally Posted by Ken Thompson
Unfortunately, it's also a fact of life that most people can't trust code that they did totally create themselves. ;)
hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......
That was actually my goal; to provide an abstraction for allocating memory to objects from the memory owned by the program(there, the virtual machine).
Is that possible with malloc?
First I tried it with realloc but couldn't get the address of the remaining pool(if not returned to the system) after it was done.
I thought that is what 'guestcheap' had in mind; since the other alternative wasn't very viable.
Thanks for the replies...
I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how. Any help?
PS:
You know, some people actually 'need' to do things. Of course, I don't personally know any of them.Quote:
I'd just like to point out that that statement is false. You never 'need' to do such a thing. You might 'want' to do such a thing, which is your own foolish choice, but you never 'need' to do such a thing.
Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...
Heap Functions (Windows)
Basically you create a private heap... but it also gives you far better functionality than malloc() and free()... for example you can discover the size of any memory block with HeapSize() and you can confirm HeapFree() with HeapValidate()...
I've often noticed that people seem naturally prone to go in the wrong direction ... they avoide the OS and it's first level APIs like they're going to die if they use them, when in fact getting closer to the core OS very often reveals increased power and capability.
The only reason I can think of to actually write your own memory allocation library would be to provide something similar to a slab allocator (a memory manager that can allocate and deallocate identical chunks of memory, presumably for objects that are being allocated and freed very often).
More about it here: Slab allocation - Wikipedia, the free encyclopedia
However you are most certainly going to need to use OS specific stuff as most common sense slab allocators reside in kernel space.
The big question is "Why?" ... if it's a class assignment or even idle curiosity, ok I get that... but if you think you can second guess a well tested and optimized library you are probably in for a rude awakening. No matter how you do it, you're going to end up calling the underlying OS APIs at some point...
The Linux kernel api is still a little complicated for me because my unfortunate ignorance of most of the C standard library. I'm working to rectify that.
Also, I'd be in a bad situation if someday, I need to demonstrate my program to someone coming from another platform.
Are you writing an OS kernel? If so, I have no idea how you would start dealing with memory, but the first place I would look for clues would be the linux kernel. I imagine there could be some architecture specific issues (have a look at the link in that article to Paging, pretty sure you have to deal with that first).
If you are not writing an OS kernel, then you are writing userspace code, which means you have no other choice except to use the routines provided by the operating system, the most fundamental of which is probably malloc.
Ie,
1) Short of writing kernel space code, which is going to be very platform specific (and you still haven't told us what platform!) the answer to your question is you CAN'T.
2) Your question is more ambiguous than perhaps you perceive it to be. Are you wanting to write a memory pool? If you can't help us to understand why you want to do this and what it is for -- so that we can better understand the nature of the task -- very likely we can't help you at all.
actually, i am making an OS kernel, on Ubuntu 11 running as a Virtual Machine on windows...
Any updates?
How can I allocate memory? I've told you why I need it and what OS I'm using? Could you guys at least give me a breakdown of the steps involved?
Thanks
I haven't done anything except a printf, OSDEV wiki recommends memory management as the next step.
Yes, I think its weird too, but that's how I'm running itQuote:
Hmmm -- so where is the kernel going to run? Inside a virtual machine on ubuntu running inside a virtual machine on windows?
Okay...Quote:
And you should not be surprised if it doesn't work when you un-nest it from all that virtualization...
Virtual machines are pretty good, but they're not perfect...
But how do I allocate memory? I mean there are various tutorials on the net on how the process should be done, and the dangers of making one(which is kind of why I'm using a Virtual Machine), but not one line of code. What function/command in C allocates memory? Or do I need to use assembly's mov command?
The only C function that allocates memory is malloc() and it is dependent upon the underlying operating system.
If you are working on an OS kernel you need to write a complete memory manager, that is, the underlying stuff behind malloc()...
Usually memory is allocated by placing frames around blocks of memory... they form a sort of linked list (loose analogy, but close enough for illustration purposes) of allocated blocks, each pointing to the next. You start at the bottom (or top) of memory and allocate 100 bytes, so you put a frame there, the frame contains a pointer to the next block and the size of the current block and you return a pointer to the first byte of the block to the program... to allocate another block you simply add a nother frame...
Then to deallocate a block you fix the pointers and remove the frame...Code:*
*allocated block
*
size of this block
pointer to next block
===================
*
* allocated block
*
Size of this block
pointer to next block
====================
You know where each block begins and ends, so as you add and remove blocks you can "manage" the memory by filling gaps.
Of course it's a little bit more complicated than this --well, more than a little actually-- but that's the jist of it... If you understand linked lists you understand memory management.
Where exactly are you at with regard to this OS development? Is your boatloader done or are you using something like GRUB? When you say, "I made my own printf function", what do you mean by that? I hope you understand that when creating your own OS you leave behind the comfort of the standard libraries when it comes to input/output, memory management, disk access, ect. All these libraries implement there functions by OS specific API calls that conform to the standard; however they are specific to the OS they were made for.
The reason I am saying this is because of your question asking for what "C function to allocate memory". There is no standard C function available to you because you are creating your own OS. You have left the comfort zone of the standard libraries and you are on your own in the woods so to say.
Keep this in mind when you decide to come back here and ask for what C function to make files or access the harddrive. There are none for you. You are going to need to make them yourself.
That being said take a look at these tutorials. This should get you going with your project. I would suggest you start at the top and read your way through.
I've already checked out the link before posting here, but I've already built my kernel structure specifically to avoid the amount of extra assembly he's using. Additionally, with every article he's specified a multiboot header for his own bootloader, while I'm using GRUB. By printf command, I mean a basic function that prints to 0Xb80000(the video display). Both numbers and strings can be printed. Any other suggestions?
Because they assume that if you think you are capable of writing your own kernel you shouldn't need to be hand held by copying finished code.
The OSDev page you yourself linked mentions two different approaches: Bitmaps or headers. The same site also has explanations of what those two words mean.
First of all I didn't 'myself' link this page AndrewHunter did. That being said, I found help here I understand how the code works, but I can't identify the line of code which actually assigns the memory. Any help?
It's all assigning memory... he's doing what I showed you in message 35 of the thread... writing frames into memory.
One problem I had with the script here:i386/boot/cdboot/malloc.c Source is that I have no idea what &end is. It hasn't been declared in this piece of code, so I assume its in boot.h. How do I define end?
end is probably the end of the allocated block of memory; but as you say, it is not defined here (nor is it changed here!) so if you want to know what it is you'll have to find it elsewhere.
Are we really going to build an OS one post at a time here? This is starting to verge on ridiculous.........
Perhaps, but we've all started projects that we never finished because we did not have the resources to do so (whether that is time or expertise or whatever).
That doesn't make the attempt pointless. The OP will gain experience whether s/he succeeds or not. For example, "Learning to quit while you are ahead" appropriately is something you learn thru experience, but beyond that there's lots worthwhile in studying how an OS kernel works, and doing it hands on might be a good idea. I'm jealous ;)
Actually I think end is a marker set by the linker (see below) for where the memory occupied by the bootloader code section ends, so everything between &end and MAXBRK is available for use by the allocator.
You could, maybe, look at boot.h?
Code:/* linker stuff */
extern void end;
I'm not quite really sure what OP is upto and from what I can understand after reading the post is either he's trying learn and develop malloc or OS i'm not quite sure. But if its all about creating his own malloc from scratch I say its pretty much starting to develop his OS. But its about memory management like develop your version of malloc with some help OS then there are a few system calls, which you could use to allocate and deallocate. Which effectively what malloc and free does when it gets called. There are system calls like brk and mmap which is used handle memory at the OS level. There is tutorial which I followed while back to understand how malloc actually works. Perhaps worth a read?
ssharish
It took some time, but I've finally done it.
Here's the link that helped me out: Creating A Custom Malloc() And Free() In C - C And C++ | Dream.In.Code
Thanks again to everyone who posted!