Advertisement
Guest User

Untitled

a guest
Sep 29th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. struct heap {
  2. concurrent_queue<void*> to_free;
  3. void free(void* ptr);
  4. void* alloc(size) {
  5. void* ptr;
  6. while(to_free.try_pop(ptr)) { free(ptr); }
  7. }
  8. };
  9.  
  10. concurrent_queue<unique_ptr<heap>> heaps;
  11.  
  12. void* alloc(size) {
  13. unique_ptr<heap> h;
  14. if (!heaps.try_pop(h))
  15. h = unique_ptr<heap>(new heap); // locked global structure
  16. auto mem = h->alloc(size + sizeof(void*));
  17. // place heap* at the beginning
  18. return mem;
  19. }
  20. void free(void* ptr) {
  21. heap* h = // get heap from mem
  22. h->to_free.push_back(ptr);
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement