Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #ifndef ALLOCATOR_H
  2. #define ALLOCATOR_H
  3.  
  4. namespace Engine
  5. {
  6. struct BlockDescriptor
  7. {
  8. void* blockBase;
  9. size_t blockSize;
  10. BlockDescriptor* next;
  11. #ifdef _DEBUG
  12. size_t ID;
  13. #endif // _DEBUG
  14. };
  15.  
  16. class BlockAllocator
  17. {
  18. public:
  19. // create a new Block Allocator
  20. static BlockAllocator* create(const size_t memorySize, const unsigned int numDescriptors);
  21. // destroy the Block Allocator
  22. static void destroy();
  23.  
  24. inline static BlockAllocator* getInstance();
  25.  
  26. // allocate memory with 4 byte alignment and optional guardband (activate in Debug mode)
  27. void* allocate(const size_t size);
  28.  
  29. void selectionSortLinkedList(BlockDescriptor* list);
  30.  
  31. void swapDescriptors(BlockDescriptor* first, BlockDescriptor* sencond);
  32.  
  33. void sortBlockListByAddress(BlockDescriptor* const list);
  34.  
  35. void writeGuardBand(char* leftStart, char* rightStart);
  36.  
  37. // free memory
  38. bool free(const void* pointer);
  39.  
  40. // run garbage collection
  41. void collect();
  42.  
  43. // check whether a given pointer is within the allocated memory range
  44. bool contains(const void* pointer) const;
  45.  
  46. // check whether a given pointer is an outstanding allocation
  47. bool isAllocated(const void* pointer) const;
  48.  
  49. size_t getLargestFreeBlock() const;
  50. size_t getTotalFreeMemory() const;
  51.  
  52. void initialize(const size_t memorySize, const unsigned int numDescriptors);
  53.  
  54. void PrintDescriptorID();
  55.  
  56. private:
  57. BlockAllocator();
  58. BlockAllocator(const size_t memorySize, const unsigned int numDescriptors);
  59. ~BlockAllocator();
  60. static BlockAllocator* instance;
  61.  
  62. const size_t minSize = 16;
  63. const size_t defaultSize = 1024 * 1024;
  64. const unsigned int defaultNumDes = 2048;
  65. static const size_t alignment = 4;
  66. #ifdef _DEBUG
  67. static const size_t guardband = 4;
  68. #else
  69. static const size_t guardband = 0;
  70. #endif // _DEBUG
  71. static void* initialMemory;
  72. static void* freeMemory;
  73. static void* endMemory;
  74.  
  75. // descriptor head that points to nothing
  76. static BlockDescriptor* unusedDescriptor;
  77. // descriptor head that points to the beginning of free memory
  78. static BlockDescriptor* assignedDescriptor;
  79. // descriptor head that points to the beginning of outstanding memory
  80. static BlockDescriptor* outstandingDescriptor;
  81.  
  82. // number of unused descriptors left
  83. unsigned int numDesUnused;
  84. // number of outstanding descriptors left
  85. unsigned int numDesOutstanding;
  86. };
  87.  
  88. }
  89.  
  90. #include "BlockAllocator.hpp"
  91. #endif // ALLOCATOR_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement