| 135 | } |
| 136 | |
| 137 | void *xode_pool_malloc(xode_pool p, int size) |
| 138 | { |
| 139 | void *block; |
| 140 | |
| 141 | if(p == NULL) |
| 142 | { |
| 143 | fprintf(stderr,"Memory Leak! xode_pmalloc received NULL pool, unable to track allocation, exiting]\n"); |
| 144 | abort(); |
| 145 | } |
| 146 | |
| 147 | /* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */ |
| 148 | if(p->heap == NULL || size > (p->heap->size / 2)) |
| 149 | { |
| 150 | while((block = _xode_pool__malloc(size)) == NULL) sleep(1); |
| 151 | p->size += size; |
| 152 | _xode_pool_cleanup_append(p, _xode_pool_free(p, _xode_pool__free, block)); |
| 153 | return block; |
| 154 | } |
| 155 | |
| 156 | /* we have to preserve boundaries, long story :) */ |
| 157 | if(size >= 4) |
| 158 | while(p->heap->used&7) p->heap->used++; |
| 159 | |
| 160 | /* if we don't fit in the old heap, replace it */ |
| 161 | if(size > (p->heap->size - p->heap->used)) |
| 162 | p->heap = _xode_pool_heap(p, p->heap->size); |
| 163 | |
| 164 | /* the current heap has room */ |
| 165 | block = (char *)p->heap->block + p->heap->used; |
| 166 | p->heap->used += size; |
| 167 | return block; |
| 168 | } |
| 169 | |
| 170 | void *xode_pool_mallocx(xode_pool p, int size, char c) |
| 171 | { |
no test coverage detected