/vm/bump_allocator.hpp

http://github.com/abeaumont/factor · C++ Header · 54 lines · 44 code · 9 blank · 1 comment · 3 complexity · 31babdb11095c47f8fc9a58fa383870a MD5 · raw file

  1. namespace factor
  2. {
  3. template<typename Block> struct bump_allocator {
  4. /* offset of 'here' and 'end' is hardcoded in compiler backends */
  5. cell here;
  6. cell start;
  7. cell end;
  8. cell size;
  9. explicit bump_allocator(cell size_, cell start_) :
  10. here(start_), start(start_), end(start_ + size_), size(size_) {}
  11. bool contains_p(Block *block)
  12. {
  13. return ((cell)block - start) < size;
  14. }
  15. Block *allot(cell size)
  16. {
  17. cell h = here;
  18. here = h + align(size,data_alignment);
  19. return (Block *)h;
  20. }
  21. cell occupied_space()
  22. {
  23. return here - start;
  24. }
  25. cell free_space()
  26. {
  27. return end - here;
  28. }
  29. cell next_object_after(cell scan)
  30. {
  31. cell size = ((Block *)scan)->size();
  32. if(scan + size < here)
  33. return scan + size;
  34. else
  35. return 0;
  36. }
  37. cell first_object()
  38. {
  39. if(start != here)
  40. return start;
  41. else
  42. return 0;
  43. }
  44. };
  45. }