Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- class FixedAllocator {
- public:
- FixedAllocator(size_t chunk_size, size_t page_size);
- void *Allocate();
- void Deallocate(void *value);
- private:
- size_t chunk_size;
- size_t page_size;
- std::vector<std::vector<unsigned char>> pool;
- size_t free_memory = 0;
- unsigned char *memory;
- };
- FixedAllocator::FixedAllocator(size_t chunk_size, size_t page_size) {
- this->chunk_size = chunk_size;
- this->page_size = page_size;
- }
- void *FixedAllocator::Allocate() {
- if (chunk_size > free_memory) {
- pool.emplace_back(page_size * chunk_size);
- free_memory = pool.back().size();
- memory = &pool.back().front();
- }
- auto front_memory = memory;
- free_memory -= chunk_size;
- memory += chunk_size;
- return front_memory;
- }
- void FixedAllocator::Deallocate(void *value) {
- auto front_memory = (unsigned char *) value;
- memory = front_memory;
- free_memory += chunk_size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement