Go to index, previous page, next page

libmem.cc

This module implements the __builtin_new() and __builtin_delete() functions needed by the C++ language.

All the allocations are done inside a big array, make sure you create it big enough to fit the memory needs of the kernel.

The routines maintain two lists: one for allocated memory blocks and one for free blocks. The latter is kept sorted by size in order to minimize the fragmentation of the heap.

This module exports:

void * __builtin_new( int memsiz ) explained below
void __builtin_delete( void * ) explained below


Look at the header file: libmem.e


Local data:

char heap[] The array inside which allocations are done.
struct MemHeader *FreeList, *AllocList Respectively, the free memory list and the allocated blocks one.

The second list is useful to check for wrong deallocations.


Module functions:

void * __builtin_new( int memsiz ) The function searches for a free memory block of at least memsiz bytes. If it doesn't find it, it returns NULL, otherwise it returns a pointer to the allocated memory.
If the block is big enough to fit other allocations, it's split and the second halve is placed back into the free memory list.
void __builtin_delete( void *ptr ) Free the memory block pointed by ptr, after checking that it has really been allocated.
void InsertBySize( struct MemHeader **list, struct MemHeader *node ) Inserts the memory node into the list, keeping it sorted by size.


Look at the code: libmem.cc


Go to index, previous page, next page