#include #include //#include #include "FixedMalloc.h" static std::map< size_t, void * > *freeList = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; inline size_t roundUp(size_t size) { // Round up to an even multiple of 8. This keeps more stuff aligned. This // might help performance. And this might help with atomic access to data, // required for sharing between threads. return ((size+7)>>3)<<3; } void *fixedMalloc(size_t size) { #ifdef SIMPLE_MALLOC return malloc(size); #endif size = std::max(sizeof(void *), sizeof(size_t) + size); size = roundUp(size); pthread_mutex_lock(&mutex); if (!freeList) { freeList = new std::map< size_t, void * >; } void *&top = (*freeList)[size]; if (!top) { //std::cout<<"Requesting 25 more blocks of "< #include "FixedMalloc.h" int main() { void *items[55]; for (int i = 0; i < 55; i += 2) { std::cout<= 0; j--) { fixedFree(items[j]); } std::cout<<'\n'; } } */