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
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
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?
- 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.
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.
Well, give it a try. It may not turn out as trivial as you seem to think.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.
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.
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.
>> what happens in linux?
Typically uses sbrk() to "ask" the OS for mem.
gg
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; }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).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; }
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.