Skip to content
Snippets Groups Projects
Commit 617ebd0b authored by j2-pelczar's avatar j2-pelczar :cowboy:
Browse files

Upload New File

parent b8778e65
No related branches found
No related tags found
No related merge requests found
#ifndef BUMP_ALLOCATOR_HPP
#define BUMP_ALLOCATOR_HPP
#include <cstddef>
#include <iostream>
class BumpAllocator {
private:
char* memory;
std::size_t size;
char* current_up; // Pointer for bumping up
char* current_down; // Pointer for bumping down
public:
BumpAllocator(std::size_t totalSize)
: size(totalSize), memory(new char[totalSize]) {
current_up = memory;
current_down = memory + size;
}
~BumpAllocator() {
delete[] memory;
}
// Allocate by bumping up
void* allocateUp(std::size_t allocSize) {
if (current_up + allocSize > current_down) {
throw std::bad_alloc();
}
void* result = current_up;
current_up += allocSize;
return result;
}
// Allocate by bumping down
void* allocateDown(std::size_t allocSize) {
if (current_down - allocSize < current_up) {
throw std::bad_alloc();
}
current_down -= allocSize;
return current_down;
}
// Reset allocator
void reset() {
current_up = memory;
current_down = memory + size;
}
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment