Thread: Create malloc and free function in C?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guestcheap View Post
    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.
    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.
    Last edited by MK27; 08-11-2011 at 06:33 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

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    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

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guestcheap View Post
    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?
    Isn't that totally dependent on how your kernel works? How much of it do you have done so far?

    Quote Originally Posted by guestcheap View Post
    actually, i am making an OS kernel, on Ubuntu 11 running as a Virtual Machine on windows...
    Hmmm -- so where is the kernel going to run? Inside a virtual machine on ubuntu running inside a virtual machine on windows?
    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

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    I haven't done anything except a printf, OSDEV wiki recommends memory management as the next step.
    Hmmm -- so where is the kernel going to run? Inside a virtual machine on ubuntu running inside a virtual machine on windows?
    Yes, I think its weird too, but that's how I'm running it

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by krishnaswathi View Post
    hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......
    Don't talk with your mouth full and/or your mind away for a vacation.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    actually, i am making an OS kernel, on Ubuntu 11 running as a Virtual Machine on windows...

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

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

  11. #11
    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.

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

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by tabstop View Post
    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.
    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.

    Quote Originally Posted by guestcheap View Post
    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?
    You could, maybe, look at boot.h?
    Code:
    /* linker stuff */
    extern void end;

  14. #14
    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.

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

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