Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created December 11, 2019 16:34
Show Gist options
  • Save untodesu/ac5e57e6c05f2e745815bf799c18c4d3 to your computer and use it in GitHub Desktop.
Save untodesu/ac5e57e6c05f2e745815bf799c18c4d3 to your computer and use it in GitHub Desktop.
//================================
// The AUX2D Engine
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/
//
// File: The memory allocator.
//================================
#include <cstdlib>
#include <cassert>
#include <amalloc.h>
static size_t s_buffer_size = 0;
static size_t s_high_end = 0;
static size_t s_low_end = 0;
static void * s_buffer = nullptr;
//--------------------------------
// Initialize allocator
//--------------------------------
bool H_Initialize(size_t buffer_size)
{
//Initialize only if not initialized.
if(s_buffer) {
//Allocate
s_buffer = malloc(buffer_size);
assert(s_buffer);
//Initialize values
s_buffer_size = buffer_size;
s_high_end = 0; //Grows down
s_low_end = 0;
return true;
}
return false;
}
//--------------------------------
// Shutdown allocator
//--------------------------------
bool H_Shutdown(void)
{
//Shutdown allowed only if initialized
if(s_buffer) {
//De-allocate
free(s_buffer);
s_buffer = nullptr;
//Clear values.
s_buffer_size = 0;
s_high_end = 0;
s_low_end = 0;
return true;
}
return false;
}
//--------------------------------
// Allocate (low area)
//--------------------------------
void * H_Malloc(size_t size)
{
if(!s_buffer) {
return nullptr;
}
//Control variables
size_t old_end = (s_low_end);
size_t new_end = (s_low_end + size);
size_t high_limit = (s_buffer_size - s_high_end);
//New end shouldn't overflow current higher allocation
if(new_end > high_limit) {
return nullptr;
}
//Now we have allocated memory!
s_low_end = new_end;
return (uint8_t *)s_buffer + old_end;
}
//--------------------------------
// Allocate (high area)
//--------------------------------
void * H_HighMalloc(size_t size)
{
if(!s_buffer) {
return nullptr;
}
//Control variables
size_t old_end = (s_high_end);
size_t new_end = (s_high_end + size);
size_t low_limit = (s_buffer_size - s_low_end);
//New end shouldn't overflow current lower allocation
if(new_end > low_limit) {
return nullptr;
}
//Now we have allocated memory in the higher area
s_high_end = new_end;
return (uint8_t *)s_buffer + old_end;
}
//--------------------------------
// Clear low area
//--------------------------------
void H_Free(void)
{
if(!s_buffer) {
return;
}
// Actually clear all stuff
for(size_t i = 0; i < s_low_end; i++) {
*((uint8_t *)s_buffer + i) = 0x00;
}
s_low_end = 0;
}
//--------------------------------
// Clear high area
//--------------------------------
void H_HighFree(void)
{
if(!s_buffer) {
return;
}
// Actually clear all stuff
for(size_t i = 0; i < s_high_end; i++) {
*((uint8_t *)s_buffer + (s_buffer_size - i - 1)) = 0x00;
}
s_high_end = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment