Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame^] | 1 | // A small, simple heap manager based (loosely) on the startup heap |
| 2 | // based on the startup heap manager from FreeBSD. |
| 3 | // |
| 4 | // Manages a fixed-size memory pool, supports malloc and free only. |
| 5 | // No support for realloc. |
| 6 | // |
| 7 | // Allocates chunks in multiples of four bytes, with a four byte header |
| 8 | // for each chunk. The overhead of each chunk is kept low by keeping pointers |
| 9 | // as two byte offsets within the heap, rather than (4 or 8 byte) pointers. |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 14 | |
| 15 | class mutexor { |
| 16 | public: |
| 17 | mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } |
| 18 | ~mutexor () { pthread_mutex_unlock ( mtx_ ); } |
| 19 | private: |
| 20 | mutexor ( const mutexor &rhs ); |
| 21 | mutexor & operator = ( const mutexor &rhs ); |
| 22 | pthread_mutex_t *mtx_; |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | #define HEAP_SIZE 512 |
| 27 | char heap [ HEAP_SIZE ]; |
| 28 | |
| 29 | typedef unsigned short heap_offset; |
| 30 | typedef unsigned short heap_size; |
| 31 | |
| 32 | struct heap_node { |
| 33 | heap_offset next_node; // offset into heap |
| 34 | heap_size len; // size in units of "sizeof(heap_node)" |
| 35 | }; |
| 36 | |
| 37 | static const heap_node *list_end = (heap_node *) ( &heap [ HEAP_SIZE ] ); // one past the end of the heap |
| 38 | static heap_node *freelist = NULL; |
| 39 | |
| 40 | heap_node *node_from_offset ( const heap_offset offset ) throw() |
| 41 | { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); } |
| 42 | |
| 43 | heap_offset offset_from_node ( const heap_node *ptr ) throw() |
| 44 | { return (((char *) ptr ) - heap) / sizeof (heap_node); } |
| 45 | |
| 46 | void init_heap () throw() { |
| 47 | freelist = (heap_node *) heap; |
| 48 | freelist->next_node = offset_from_node ( list_end ); |
| 49 | freelist->len = HEAP_SIZE / sizeof (heap_node); |
| 50 | } |
| 51 | |
| 52 | // How big a chunk we allocate |
| 53 | size_t alloc_size (size_t len) throw() |
| 54 | { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; } |
| 55 | |
| 56 | bool is_fallback_ptr ( void *ptr ) throw() |
| 57 | { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); } |
| 58 | |
| 59 | void *fallback_malloc(size_t len) throw() { |
| 60 | heap_node *p, *prev; |
| 61 | const size_t nelems = alloc_size ( len ); |
| 62 | mutexor mtx ( &heap_mutex ); |
| 63 | |
| 64 | if ( NULL == freelist ) |
| 65 | init_heap (); |
| 66 | |
| 67 | // Walk the free list, looking for a "big enough" chunk |
| 68 | for (p = freelist, prev = 0; |
| 69 | p && p != list_end; prev = p, p = node_from_offset ( p->next_node)) { |
| 70 | |
| 71 | if (p->len > nelems) { // chunk is larger, shorten, and return the tail |
| 72 | heap_node *q; |
| 73 | |
| 74 | p->len -= nelems; |
| 75 | q = p + p->len; |
| 76 | q->next_node = 0; |
| 77 | q->len = nelems; |
| 78 | return (void *) (q + 1); |
| 79 | } |
| 80 | |
| 81 | if (p->len == nelems) { // exact size match |
| 82 | if (prev == 0) |
| 83 | freelist = node_from_offset(p->next_node); |
| 84 | else |
| 85 | prev->next_node = p->next_node; |
| 86 | p->next_node = 0; |
| 87 | return (void *) (p + 1); |
| 88 | } |
| 89 | } |
| 90 | return NULL; // couldn't find a spot big enough |
| 91 | } |
| 92 | |
| 93 | // Return the start of the next block |
| 94 | heap_node *after ( struct heap_node *p ) throw() { return p + p->len; } |
| 95 | |
| 96 | void fallback_free (void *ptr) throw() { |
| 97 | struct heap_node *cp = ((struct heap_node *) ptr) - 1; // retrieve the chunk |
| 98 | struct heap_node *p, *prev; |
| 99 | |
| 100 | mutexor mtx ( &heap_mutex ); |
| 101 | |
| 102 | #ifdef DEBUG_FALLBACK_MALLOC |
| 103 | std::cout << "Freeing item at " << offset_from_node ( cp ) << " of size " << cp->len << std::endl; |
| 104 | #endif |
| 105 | |
| 106 | for (p = freelist, prev = 0; |
| 107 | p && p != list_end; prev = p, p = node_from_offset (p->next_node)) { |
| 108 | #ifdef DEBUG_FALLBACK_MALLOC |
| 109 | std::cout << " p, cp, after (p), after(cp) " |
| 110 | << offset_from_node ( p ) << ' ' |
| 111 | << offset_from_node ( cp ) << ' ' |
| 112 | << offset_from_node ( after ( p )) << ' ' |
| 113 | << offset_from_node ( after ( cp )) << std::endl; |
| 114 | #endif |
| 115 | if ( after ( p ) == cp ) { |
| 116 | #ifdef DEBUG_FALLBACK_MALLOC |
| 117 | std::cout << " Appending onto chunk at " << offset_from_node ( p ) << std::endl; |
| 118 | #endif |
| 119 | p->len += cp->len; // make the free heap_node larger |
| 120 | return; |
| 121 | } |
| 122 | else if ( after ( cp ) == p ) { // there's a free heap_node right after |
| 123 | #ifdef DEBUG_FALLBACK_MALLOC |
| 124 | std::cout << " Appending free chunk at " << offset_from_node ( p ) << std::endl; |
| 125 | #endif |
| 126 | cp->len += p->len; |
| 127 | if ( prev == 0 ) { |
| 128 | freelist = cp; |
| 129 | cp->next_node = p->next_node; |
| 130 | } |
| 131 | else |
| 132 | prev->next_node = offset_from_node(cp); |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | // Nothing to merge with, add it to the start of the free list |
| 137 | #ifdef DEBUG_FALLBACK_MALLOC |
| 138 | std::cout << " Making new free list entry " << offset_from_node ( cp ) << std::endl; |
| 139 | #endif |
| 140 | cp->next_node = offset_from_node ( freelist ); |
| 141 | freelist = cp; |
| 142 | } |
| 143 | |
| 144 | #ifdef INSTRUMENT_FALLBACK_MALLOC |
| 145 | size_t print_free_list () { |
| 146 | struct heap_node *p, *prev; |
| 147 | heap_size total_free = 0; |
| 148 | if ( NULL == freelist ) |
| 149 | init_heap (); |
| 150 | |
| 151 | for (p = freelist, prev = 0; |
| 152 | p && p != list_end; prev = p, p = node_from_offset (p->next_node)) { |
| 153 | std::cout << ( prev == 0 ? "" : " ") << "Offset: " << offset_from_node ( p ) |
| 154 | << "\tsize: " << p->len << " Next: " << p->next_node << std::endl; |
| 155 | total_free += p->len; |
| 156 | } |
| 157 | std::cout << "Total Free space: " << total_free << std::endl; |
| 158 | return total_free; |
| 159 | } |
| 160 | #endif |
| 161 | } |