Thread: Create malloc and free function in C?

  1. #31
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    I don't know if I'd go that far... I think I'd settle on "theoretically entertaining".


    Quzah.
    rofl....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  2. #32
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by quzah View Post
    I don't know if I'd go that far... I think I'd settle on "theoretically entertaining".
    "Potentially entertaining in theory" covers most code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #33
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CommonTater View Post
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)
    It would be quite a bizarre exercise to implement your own malloc() on top of HeapAlloc(), because that's exactly what the MS runtime does already. Not only reinventing the wheel, but reinventing it exactly the same as it already is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #34
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    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...
    Okay...
    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?

  5. #35
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guestcheap View Post
    Okay...
    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...
    Code:
    *
    *allocated block
    *
    size of this block
    pointer to next block
    ===================
    *
    *  allocated block
    *
    Size of this block
    pointer to next block
    ====================
    Then to deallocate a block you fix the pointers and remove the frame...

    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.

  6. #36
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Last edited by AndrewHunter; 08-12-2011 at 09:46 PM. Reason: Made nice.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #37
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Quote Originally Posted by AndrewHunter View Post
    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?

  8. #38
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by guestcheap View Post
    Okay...
    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?
    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.

  9. #39
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Quote Originally Posted by _Mike View Post
    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?

  10. #40
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's all assigning memory... he's doing what I showed you in message 35 of the thread... writing frames into memory.

  11. #41
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    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...
    Code:
    *
    *allocated block
    *
    size of this block
    pointer to next block
    ===================
    *
    *  allocated block
    *
    Size of this block
    pointer to next block
    ====================
    Then to deallocate a block you fix the pointers and remove the frame...

    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.
    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?

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  13. #43
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Are we really going to build an OS one post at a time here? This is starting to verge on ridiculous.........
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #44
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Are we really going to build an OS one post at a time here? This is starting to verge on ridiculous.........
    "Starting" ???

    Is it not obvious by now that our OP friend is in way over his head?
    Seriously, no offense is intended, but I'm thinking that a lot more than his experience is required here...

  15. #45
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Is it not obvious by now that our OP friend is in way over his head?
    Seriously, no offense is intended, but I'm thinking that a lot more than his experience is required here...
    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
    Last edited by MK27; 08-14-2011 at 11:59 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc/free help
    By stan4d2012 in forum C Programming
    Replies: 3
    Last Post: 04-13-2009, 07:04 PM
  2. malloc() and free()
    By someprogr in forum C Programming
    Replies: 1
    Last Post: 12-28-2007, 07:16 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. need help with malloc-free
    By sleith in forum C Programming
    Replies: 4
    Last Post: 08-22-2007, 08:07 PM
  5. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM

Tags for this Thread