Guest User

Vikram Menon

a guest
Sep 8th, 2009
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. /* MEMORY MANAGER
  2.  * Initially assigned memory is split into partitions with
  3.  * fixed block sizes.
  4.  *
  5.  * Functions are constant-time except when smaller blocks
  6.  * must be merged to fulfil an allocation call.
  7.  */
  8.  
  9. #define MAX_PARTS 20            // Maximum number of partitions allowed
  10. #define NULL_PTR  0
  11.  
  12. struct header           // Header of memory block
  13. {
  14.     unsigned blks_allocated : 22;   // Number of n_blocks allocated under this header
  15.     unsigned part_id : 10;      // Memory partition index of this header
  16.     struct header *back;        // Pointer to header of previous block (NULL for first in list)
  17.     struct header *next;        // Pointer to header of next block     (NULL for last in list)
  18. };
  19.  
  20. /* Returns: int
  21.  * 0  if successful
  22.  * -1 if assignment fails
  23.  *
  24.  * Call once to assign a pool of memory, partition it, set up
  25.  * the Used and Free lists, and set the initialised flag
  26.  */
  27. int nmalloc_init(
  28.         void*,          // sufficiently large pool of free memory
  29.         int*,           // array of block sizes for each partition
  30.         int*,           // array of number of blocks for each partition
  31.         int         // number of blocks
  32.         );
  33.  
  34. /* Returns: None
  35.  *
  36.  * Call once to release initially assigned pool of memory,
  37.  * clear all global variables and flags
  38.  */
  39. void nmalloc_end();
  40.  
  41. /* Returns: int
  42.  * -1 if unsuccessful
  43.  *  0 if successful
  44.  *
  45.  * Frees the passed block of memory back to the pool
  46.  */
  47. int nfree(void*);           // pointer to data location
  48.  
  49. /* Returns: void*
  50.  * Pointer to an empty block, if found
  51.  * NULL if insufficient memory
  52.  *
  53.  * Allocates memory from pool on request
  54.  */
  55. void* nmalloc(int);         // size requested (in bytes)
  56.  
  57. /* Returns: float
  58.  *
  59.  * Calculates percentage of external fragmentation
  60.  */
  61. float nefrag();
  62.  
  63. /* Returns: float
  64.  *
  65.  * Calculates percentage of memory overhead
  66.  */
  67. float noverhead();
  68.  
Advertisement
Add Comment
Please, Sign In to add comment