| | |
| | | #include "sem_util.h" |
| | | #define MEM_POOL_COND_KEY 0x8801 |
| | | |
| | | static int mem_pool_cond = SemUtil::get(MEM_POOL_COND_KEY, 0); |
| | | |
| | | // static int mem_pool_mutex = SemUtil::get(MEM_POOL_COND_KEY, 1); |
| | | |
| | | static inline void mem_pool_init(size_t heap_size) { |
| | | if(mm_init(heap_size)) { |
| | | |
| | | } |
| | | mm_init(heap_size); |
| | | } |
| | | |
| | | static inline void mem_pool_destroy(void) { |
| | | if(mm_destroy()) { |
| | | SemUtil::remove(mem_pool_cond); |
| | | } |
| | | mm_destroy(); |
| | | |
| | | } |
| | | |
| | | static inline void *mem_pool_malloc (size_t size) { |
| | | void *ptr; |
| | | while( (ptr = mm_malloc(size)) == NULL ) { |
| | | err_msg(0, "There is not enough memery to allocate, waiting someone else to free."); |
| | | SemUtil::set(mem_pool_cond, 0); |
| | | // wait for someone else to free space |
| | | SemUtil::dec(mem_pool_cond); |
| | | |
| | | } |
| | | |
| | | return ptr; |
| | | return mm_malloc(size); |
| | | } |
| | | |
| | | |
| | | static inline void mem_pool_free (void *ptr) { |
| | | mm_free(ptr); |
| | | // notify malloc |
| | | SemUtil::set(mem_pool_cond, 1); |
| | | |
| | | } |
| | | |
| | | |