Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* pool_new(Pool& pool, int size)
- {
- char *ptr = pool.buffer;
- int size_counter = 0; // Blocks amount
- while (size_counter + size < pool.size) { // Execute while we are below the memory limit.
- int block_size = *reinterpret_cast<int*>(ptr); // Non-free block size
- if (block_size > 0) {
- ptr += block_size;
- size_counter += block_size; // Increase size_counter until we reach a "free" block
- continue;
- } else if (block_size < 0) { // When we reach a "free" block
- if (size + sizeof(int) <= -block_size) {
- *reinterpret_cast<int*>(ptr) = size + sizeof(int);
- *reinterpret_cast<int*>(ptr + size + sizeof(int)) = block_size + size + sizeof(int);
- return ptr + sizeof(int);
- } else {
- size_counter += (-block_size);
- ptr += block_size;
- continue;
- }
- } else {
- return nullptr;
- }
- }
- return nullptr;
- }
- void pool_delete(char* at)
- {
- at -= sizeof(int);
- int size = *reinterpret_cast<int*>(at);
- *reinterpret_cast<int*>(at) = -size;
- for (int i = sizeof(int); i < size; i++) at[i] = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement