Thread: Writing my own malloc.

  1. #16
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Google for things like "Malloc strategies". Try and find papers that attempt to improve malloc's performance - they will usually explain existing strategies and algorithms before presenting their own.

    gg

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then essentially you would be writing your own memory pool if you try to optimize malloc.
    (We could say malloc IS a memory pool.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    So basically I can't access the heap without the help of malloc or HeapAlloc and the only option I have is to create a memory pool on stack (a large array and slice pieces of it every time the user asks for some room)? Well,if so then how does malloc/HeapAlloc access the heap?

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    • malloc() makes a request from the OS (ie. by calling HeapAlloc() with the default assigned Windows heap object).
    • HeapAlloc() is the OS memory management function to call to allocate memory on Windows if you are not part of the actual OS. It allocates memory internally by performing a malloc()-like operation on a Windows heap object in an unknown manner (unknown to applications, known to Windows), and stores information about what it has allocated somewhere that the OS can hang on to for when you free the memory. This is internal OS stuff, and the internal implementation is probably not something you can bank on. You are not able to do the same thing as this function. This is because Windows API functions are given special permissions to mess about with memory and other such things. Without Windows being able to keep track of what memory you're allowed to use, you won't be able to actually use it.
    • A Windows heap object is created with HeapCreate(). Each process is automatically assigned one heap object when it starts up. You may request more than one heap object, although I believe you should destroy each heap object you are granted.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suspect the object of the exercise is assume that you have a large block of memory, write some functions to manage that space effectively.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Salem View Post
    I suspect the object of the exercise is assume that you have a large block of memory, write some functions to manage that space effectively.
    If so, this "challenge" is a joke and it's just like the memory pool with the large array.
    ---
    McGyver, what happens in linux?

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, give it a try. It may not turn out as trivial as you seem to think.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How so?
    Have you considered the problems of memory fragmentation yet, when memory is freed in a different order to what it's allocated in? If not, then you're nowhere near an answer yet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by eXeCuTeR View Post
    McGyver, what happens in linux?
    It's the same thing. It's how operating systems work!
    You need to lessen your requirements or you'll be getting nowhere, very fast.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    BTW, I'm not claiming HeapAlloc() is the only way to go about it. There may be other Windows API functions that allow you to do the same thing, and possibly with slightly greater control.

    The point, however, is that you must do things in the manner that the OS requires you to do it.

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Wait... I know exactly how to do it without calling a Windows API or using stack memory. Simply write your own OS, then you control all of the memory.

  12. #27
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Elysia View Post
    It's the same thing. It's how operating systems work!
    You need to lessen your requirements or you'll be getting nowhere, very fast.
    Of course it's the same principal but how does it access the heap?
    ---
    Hehe, yeah cpjust, that's certainly an option.

  13. #28
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> what happens in linux?
    Typically uses sbrk() to "ask" the OS for mem.

    gg

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also already believe some people (*cough* MacGyver *cough*) have already told you how it works.
    The names may be different, but the principle is exactly the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This is an example only. It is not meant to compile, but just to give an idea of how you might go about a very simplistic approach to writing a custom version of malloc():

    Code:
    static void *internal_cboard_malloc(size_t size)
    {
    	HANDLE heap;
    	void *p = NULL;
    	
    	heap = GetProcessHeap();
    	if(heap)
    	{
    		p = HeapAlloc(heap, 0, size);
    	}
    	return p;
    }
    Code:
    void *cboard_malloc(size_t size)
    {
    	cbm_datablock *info;
    	void *p = NULL;
    	
    	if(memlist == NULL)
    	{
    		if(!internal_initMemlist())
    		{
    			fputs("Internal error: cb_malloc() unable to allocate memory for internal linked list.", stderr);
    			return NULL;
    		}
    	}
    	info = internal_cboard_malloc(sizeof(*info));
    	if(info)
    	{
    		p = internal_cboard_malloc(size);
    		if(p)
    		{
    			info->size = size;
    			info->memptr = p;
    			addNode(memlist, info);
    		}
    		else internal_cboard_free(info);
    	}
    	p?iSuccessMallocCalls++:iFailureMallocCalls++;
    	return p;
    }
    The idea is that all calls to this new malloc() are forwarded to a new internal_malloc() that does the real work of allocating memory. The new malloc()'s job is really just to organize everything and keep track of it all. Ideally, the linked list (or whatever data structure you use to keep track of everything), will call the new internal_malloc() for allocating new nodes and such (since calling the original malloc() is kind of cheating, and calling the new malloc() will just cause an endless loop or some other problem).

    One thing that is interesting is that the new malloc() is not platform specific. Only the new internal_malloc() is.

    Please note, this is an extremely simple way of going about this, and probably only good for theory. I make no pretense about this being any good at all, and I stress this isn't a real strategy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc for extending the size....!
    By patil.vishwa in forum C Programming
    Replies: 5
    Last Post: 09-11-2008, 03:34 AM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. A bug I cannot see. . . malloc issue
    By Kennedy in forum C Programming
    Replies: 16
    Last Post: 09-26-2007, 09:49 AM
  4. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  5. help with malloc
    By geetee in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 12:07 PM