Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MEMORY MANAGER
- * Initially assigned memory is split into partitions with
- * fixed block sizes.
- *
- * Functions are constant-time except when smaller blocks
- * must be merged to fulfil an allocation call.
- */
- #define MAX_PARTS 20 // Maximum number of partitions allowed
- #define NULL_PTR 0
- struct header // Header of memory block
- {
- unsigned blks_allocated : 22; // Number of n_blocks allocated under this header
- unsigned part_id : 10; // Memory partition index of this header
- struct header *back; // Pointer to header of previous block (NULL for first in list)
- struct header *next; // Pointer to header of next block (NULL for last in list)
- };
- /* Returns: int
- * 0 if successful
- * -1 if assignment fails
- *
- * Call once to assign a pool of memory, partition it, set up
- * the Used and Free lists, and set the initialised flag
- */
- int nmalloc_init(
- void*, // sufficiently large pool of free memory
- int*, // array of block sizes for each partition
- int*, // array of number of blocks for each partition
- int // number of blocks
- );
- /* Returns: None
- *
- * Call once to release initially assigned pool of memory,
- * clear all global variables and flags
- */
- void nmalloc_end();
- /* Returns: int
- * -1 if unsuccessful
- * 0 if successful
- *
- * Frees the passed block of memory back to the pool
- */
- int nfree(void*); // pointer to data location
- /* Returns: void*
- * Pointer to an empty block, if found
- * NULL if insufficient memory
- *
- * Allocates memory from pool on request
- */
- void* nmalloc(int); // size requested (in bytes)
- /* Returns: float
- *
- * Calculates percentage of external fragmentation
- */
- float nefrag();
- /* Returns: float
- *
- * Calculates percentage of memory overhead
- */
- float noverhead();
Advertisement
Add Comment
Please, Sign In to add comment