Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct heap {
- concurrent_queue<void*> to_free;
- void free(void* ptr);
- void* alloc(size) {
- void* ptr;
- while(to_free.try_pop(ptr)) { free(ptr); }
- }
- };
- concurrent_queue<unique_ptr<heap>> heaps;
- void* alloc(size) {
- unique_ptr<heap> h;
- if (!heaps.try_pop(h))
- h = unique_ptr<heap>(new heap); // locked global structure
- auto mem = h->alloc(size + sizeof(void*));
- // place heap* at the beginning
- return mem;
- }
- void free(void* ptr) {
- heap* h = // get heap from mem
- h->to_free.push_back(ptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement