Created
May 10, 2017 19:35
-
-
Save WilliamBundy/8a0119a942ec98c14a00fc995de3062f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//wb_memory_arena.c | |
//by William Bundy | |
// A simple linear allocator using free and malloc | |
// Should use mmap on linux; calls to VirtualAlloc and VirtualFree are commented out | |
// arena_end_temp is technically broken; can be implemented much better with virtual memory | |
//This is free and unencumbered software released into the public domain. | |
#define LogError(...) | |
typedef const char* string; | |
typedef unsigned char u8; | |
typedef int64_t isize; | |
typedef int32_t i32; | |
#define PAGE_SIZE 4096 | |
typedef struct MemoryArena | |
{ | |
string name; | |
u8* data; | |
u8* head; | |
u8* temp_head; | |
u8* end; | |
isize size; | |
i32 was_bootstrapped; | |
i32 align; | |
} MemoryArena; | |
static inline | |
isize mem_align(isize p, isize n) | |
{ | |
isize mod = p & (n-1); | |
return mod ? p + n - mod : p; | |
} | |
void arena_init(MemoryArena* arena, string name, isize size) | |
{ | |
arena->name = name; | |
arena->align = 4; | |
size = mem_align(size, PAGE_SIZE); | |
arena->size = size; | |
arena->data = malloc(size);//VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); | |
arena->end = arena->data + arena->size; | |
arena->head = arena->data; | |
arena->temp_head = NULL; | |
} | |
MemoryArena* arena_bootstrap(string name, isize size) | |
{ | |
MemoryArena local; | |
size += sizeof(MemoryArena); | |
arena_init(&local, name, size); | |
MemoryArena* arena = arena_push(&local, sizeof(MemoryArena)); | |
*arena = local; | |
arena->was_bootstrapped = 1; | |
return arena; | |
} | |
void* arena_push(MemoryArena* arena, isize size) | |
{ | |
isize head = (isize)arena->head; | |
isize newhead = mem_align(head + size, arena->align); | |
if(newhead >= (isize)arena->end) { | |
LogError(UserError, "[%s] was filled\n", arena->name); | |
return NULL; | |
} | |
arena->head = (void*)newhead; | |
return (void*)head; | |
} | |
void arena_start_temp(MemoryArena* arena) | |
{ | |
if(arena->temp_head != NULL) return; | |
arena->temp_head = arena->head; | |
arena->head = (void*)mem_align((isize)arena->head, PAGE_SIZE); | |
} | |
void arena_end_temp(MemoryArena* arena) | |
{ | |
if(arena->temp_head == NULL) return; | |
isize start = mem_align((isize)arena->temp_head, PAGE_SIZE); | |
isize size = mem_align((isize)arena->head, PAGE_SIZE) - start; | |
//VirtualFree((void*)start, size, MEM_DECOMMIT); | |
//VirtualAlloc((void*)start, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); | |
memset(start, 0, size); | |
arena->head = arena->temp_head; | |
arena->temp_head = NULL; | |
} | |
void arena_clear(MemoryArena** arena) | |
{ | |
MemoryArena local = **arena; | |
//VirtualFree(local.data, local.size, MEM_DECOMMIT); | |
//VirtualAlloc(local.data, local.size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); | |
free(local.data); | |
local.data = malloc(local.size); | |
local.head = local.data; | |
if(local.was_bootstrapped) { | |
*arena = ArenaPush(&local, sizeof(MemoryArena) + 64); | |
**arena = local; | |
} | |
} | |
MemoryArena arena_free(MemoryArena* arena) | |
{ | |
MemoryArena local = *arena; | |
//VirtualFree(local.data, local.size, MEM_RELEASE); | |
free(local.data) | |
return local; | |
} | |
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment