Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Memory library

rohit2383rohit2383 Member Posts: 80 ✭✭
Hi all,
I neeed to create a memory manager for a module.....sort of a manager which on init time does a big malloc and takes a bunch full of memory...now afterwards it allocates and frees memory from this pool....I wanted to know are there any existing such memory libraries available....something I could use for reference and see what all needs to be done???
Thanks
Rohit

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Hi all,
    : I neeed to create a memory manager for a module.....sort of a
    : manager which on init time does a big malloc and takes a bunch full
    : of memory...now afterwards it allocates and frees memory from this
    : pool....I wanted to know are there any existing such memory
    : libraries available....something I could use for reference and see
    : what all needs to be done???
    : Thanks
    : Rohit
    [color=Blue]
    You can search GOOGLE for "heap manager" or "dynamic memory allocation".

    Here is one algorithm:

    Memory block in the heap contains the following parts:

    1. Header (contains size of the block)
    2. Data area
    3. Tail (for debugging)

    These parts are immediately laid out. Also all blocks in heap are laid out one after other as a chain of train cars.

    You allocate big block (say 8Mb for starters) and then mark it as one giant free block. Then if allocation request comes in - you cut the block in two blocks properly marking first as occupied and second as free - adjust all block structures: headers and tails.

    You risk fragmentation, so you need the defragmentation algorithm too. With a lot of small allocations - even defragmentation can fail and there will be no continuous free memory. In that case you need memory manager with movable blocks - a complex solution.

    It all depends on how much memory your RTOS provides.

    [/color]
  • rohit2383rohit2383 Member Posts: 80 ✭✭
    : : Hi all,
    : : I neeed to create a memory manager for a module.....sort of a
    : : manager which on init time does a big malloc and takes a bunch full
    : : of memory...now afterwards it allocates and frees memory from this
    : : pool....I wanted to know are there any existing such memory
    : : libraries available....something I could use for reference and see
    : : what all needs to be done???
    : : Thanks
    : : Rohit
    : [color=Blue]
    : You can search GOOGLE for "heap manager" or "dynamic memory
    : allocation".
    :
    : Here is one algorithm:
    :
    : Memory block in the heap contains the following parts:
    :
    : 1. Header (contains size of the block)
    : 2. Data area
    : 3. Tail (for debugging)
    :
    : These parts are immediately laid out. Also all blocks in heap are
    : laid out one after other as a chain of train cars.
    :
    : You allocate big block (say 8Mb for starters) and then mark it as
    : one giant free block. Then if allocation request comes in - you cut
    : the block in two blocks properly marking first as occupied and
    : second as free - adjust all block structures: headers and tails.
    :
    : You risk fragmentation, so you need the defragmentation algorithm
    : too. With a lot of small allocations - even defragmentation can fail
    : and there will be no continuous free memory. In that case you need
    : memory manager with movable blocks - a complex solution.
    :
    : It all depends on how much memory your RTOS provides.
    :
    : [/color]
    Thanks a lot......and the reason I asked this was because we had to port some code that had a lot many memory leaks and memory corruptions in it......hence we wrote a dummy memory manager for it.....but now that we have written it we think it would be a good idea to make it generic i.e. so that other modules may also be able to use it but only as a debugging and a diagnostic tool. I have been stressing that finally in the end we must use the Operating System's malloc only i.e. these sort of techniques and bypasses must be used only for debugging and finding out the memory leaks
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : : : Hi all,
    : : : I neeed to create a memory manager for a module.....sort of a
    : : : manager which on init time does a big malloc and takes a bunch full
    : : : of memory...now afterwards it allocates and frees memory from this
    : : : pool....I wanted to know are there any existing such memory
    : : : libraries available....something I could use for reference and see
    : : : what all needs to be done???
    : : : Thanks
    : : : Rohit
    : : [color=Blue]
    : : You can search GOOGLE for "heap manager" or "dynamic memory
    : : allocation".
    : :
    : : Here is one algorithm:
    : :
    : : Memory block in the heap contains the following parts:
    : :
    : : 1. Header (contains size of the block)
    : : 2. Data area
    : : 3. Tail (for debugging)
    : :
    : : These parts are immediately laid out. Also all blocks in heap are
    : : laid out one after other as a chain of train cars.
    : :
    : : You allocate big block (say 8Mb for starters) and then mark it as
    : : one giant free block. Then if allocation request comes in - you cut
    : : the block in two blocks properly marking first as occupied and
    : : second as free - adjust all block structures: headers and tails.
    : :
    : : You risk fragmentation, so you need the defragmentation algorithm
    : : too. With a lot of small allocations - even defragmentation can fail
    : : and there will be no continuous free memory. In that case you need
    : : memory manager with movable blocks - a complex solution.
    : :
    : : It all depends on how much memory your RTOS provides.
    : :
    : : [/color]
    : Thanks a lot......and the reason I asked this was because we had to
    : port some code that had a lot many memory leaks and memory
    : corruptions in it......hence we wrote a dummy memory manager for
    : it.....but now that we have written it we think it would be a good
    : idea to make it generic i.e. so that other modules may also be able
    : to use it but only as a debugging and a diagnostic tool. I have been
    : stressing that finally in the end we must use the Operating System's
    : malloc only i.e. these sort of techniques and bypasses must be used
    : only for debugging and finding out the memory leaks

    [color=Blue]It is possible, that custom memory allocator may be more optimized than the OS malloc() function. I once made faster allocator than one Win32 provides. Not much faster (HeapAlloc() function is awfully fast!), but still.

    If you interested only to replace malloc() just for debugging purposes, here is how I would have done it:
    [/color]
    [code]
    void* AllocateMemory (unsigned int nBytes)
    {
    #ifdef DEBUG_ALLOCATIONS
    return YourAllocator.GetMem (nBytes);
    #else
    return malloc (nBytes);
    #endif
    }
    [/code]
    [color=Blue]This way you can switch between allocators simply by defining global key (DEBUG_ALLOCATIONS) for a compiler, when building all files and projects.[/color]
Sign In or Register to comment.