Compilation fixes for Bin Allocator

This commit is contained in:
Damjan Adamic
2015-08-15 12:39:27 +02:00
parent 51f46d9a69
commit 330a91441a
2 changed files with 13 additions and 13 deletions
+8 -8
View File
@@ -39,11 +39,11 @@ void * bin_realloc(void * ptr, size_t size)
// if it fits in current slot, return it // if it fits in current slot, return it
// TODO if new size is smaller, try to relocate in smaller slot // TODO if new size is smaller, try to relocate in smaller slot
if ( slots1.can_fit(ptr, size) ) { if ( slots1.can_fit(ptr, size) ) {
// TRACE("OUR realloc %p[%lu] fits in slot1", ptr, size); FLUSH(); // TRACE("OUR realloc %p[%lu] fits in slot1", ptr, size);
return ptr; return ptr;
} }
if ( slots2.can_fit(ptr, size) ) { if ( slots2.can_fit(ptr, size) ) {
// TRACE("OUR realloc %p[%lu] fits in slot2", ptr, size); FLUSH(); // TRACE("OUR realloc %p[%lu] fits in slot2", ptr, size);
return ptr; return ptr;
} }
@@ -51,10 +51,10 @@ void * bin_realloc(void * ptr, size_t size)
void * res = bin_malloc(size); void * res = bin_malloc(size);
if (res == 0) { if (res == 0) {
// we don't have the space, use libc malloc // we don't have the space, use libc malloc
// TRACE("bin_malloc [%lu] FAILURE", size); FLUSH(); // TRACE("bin_malloc [%lu] FAILURE", size);
res = malloc(size); res = malloc(size);
if (res == 0) { if (res == 0) {
TRACE("libc malloc [%lu] FAILURE", size); FLUSH(); TRACE("libc malloc [%lu] FAILURE", size);
return 0; return 0;
} }
} }
@@ -73,7 +73,7 @@ void *bin_l_alloc (void *ud, void *ptr, size_t osize, size_t nsize)
if (ptr) { // avoid a bunch of NULL pointer free calls if (ptr) { // avoid a bunch of NULL pointer free calls
if (!bin_free(ptr)) { if (!bin_free(ptr)) {
// not our range, use libc allocator // not our range, use libc allocator
// TRACE("libc free %p", ptr); FLUSH(); // TRACE("libc free %p", ptr);
free(ptr); free(ptr);
} }
} }
@@ -89,18 +89,18 @@ void *bin_l_alloc (void *ud, void *ptr, size_t osize, size_t nsize)
} }
if ( SimulateMallocFailure > 0) { if ( SimulateMallocFailure > 0) {
// simulate one malloc failure // simulate one malloc failure
TRACE("bin_l_alloc(): simulating malloc failure at %p[%lu]", ptr, nsize); FLUSH(); TRACE("bin_l_alloc(): simulating malloc failure at %p[%lu]", ptr, nsize);
return 0; return 0;
} }
#endif // #if defined(DEBUG) #endif // #if defined(DEBUG)
// try our allocator, if it fails use libc allocator // try our allocator, if it fails use libc allocator
void * res = bin_realloc(ptr, nsize); void * res = bin_realloc(ptr, nsize);
if (res && ptr) { if (res && ptr) {
// TRACE("OUR realloc %p[%lu] -> %p[%lu]", ptr, osize, res, nsize); FLUSH(); // TRACE("OUR realloc %p[%lu] -> %p[%lu]", ptr, osize, res, nsize);
} }
if (res == 0) { if (res == 0) {
res = realloc(ptr, nsize); res = realloc(ptr, nsize);
// TRACE("libc realloc %p[%lu] -> %p[%lu]", ptr, osize, res, nsize); FLUSH(); // TRACE("libc realloc %p[%lu] -> %p[%lu]", ptr, osize, res, nsize);
// if (res == 0 ){ // if (res == 0 ){
// TRACE("realloc FAILURE %lu", nsize); // TRACE("realloc FAILURE %lu", nsize);
// dumpFreeMemory(); // dumpFreeMemory();
+5 -5
View File
@@ -20,7 +20,7 @@ public:
if (ptr == Bins[n].data) { if (ptr == Bins[n].data) {
Bins[n].Used = false; Bins[n].Used = false;
--NoUsedBins; --NoUsedBins;
// TRACE("\tBinAllocator<%d> free %lu ------", SIZE_SLOT, n); FLUSH(); // TRACE("\tBinAllocator<%d> free %lu ------", SIZE_SLOT, n);
return true; return true;
} }
} }
@@ -31,22 +31,22 @@ public:
} }
void * malloc(size_t size) { void * malloc(size_t size) {
if (size > SIZE_SLOT) { if (size > SIZE_SLOT) {
// TRACE("BinAllocator<%d> malloc [%lu] size > SIZE_SLOT", SIZE_SLOT, size); FLUSH(); // TRACE("BinAllocator<%d> malloc [%lu] size > SIZE_SLOT", SIZE_SLOT, size);
return 0; return 0;
} }
if (NoUsedBins >= NUM_BINS) { if (NoUsedBins >= NUM_BINS) {
// TRACE("BinAllocator<%d> malloc [%lu] no free slots", SIZE_SLOT, size); FLUSH(); // TRACE("BinAllocator<%d> malloc [%lu] no free slots", SIZE_SLOT, size);
return 0; return 0;
} }
for (size_t n = 0; n < NUM_BINS; ++n) { for (size_t n = 0; n < NUM_BINS; ++n) {
if (!Bins[n].Used) { if (!Bins[n].Used) {
Bins[n].Used = true; Bins[n].Used = true;
++NoUsedBins; ++NoUsedBins;
// TRACE("\tBinAllocator<%d> malloc %lu[%lu]", SIZE_SLOT, n, size); FLUSH(); // TRACE("\tBinAllocator<%d> malloc %lu[%lu]", SIZE_SLOT, n, size);
return Bins[n].data; return Bins[n].data;
} }
} }
// TRACE("BinAllocator<%d> malloc [%lu] no free slots", SIZE_SLOT , size); FLUSH(); // TRACE("BinAllocator<%d> malloc [%lu] no free slots", SIZE_SLOT , size);
return 0; return 0;
} }
size_t size(void * ptr) { size_t size(void * ptr) {