Cleanups
This commit is contained in:
+14
-9
@@ -3,10 +3,10 @@
|
|||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
|
||||||
|
|
||||||
MemoryBlock *memoryblock_allocate(size_t size, size_t commit_size)
|
MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size)
|
||||||
{
|
{
|
||||||
size_t aligned_size = AlignToPageEnd(size);
|
size_t aligned_size = AlignUpToPage(size);
|
||||||
size_t aligned_commit_size = AlignToPageEnd(commit_size);
|
size_t aligned_commit_size = AlignUpToPage(commit_size);
|
||||||
|
|
||||||
MemoryBlock *block = memory_reserve(aligned_size);
|
MemoryBlock *block = memory_reserve(aligned_size);
|
||||||
Assert(block != NULL);
|
Assert(block != NULL);
|
||||||
@@ -27,6 +27,8 @@ void arena_init(Arena *arena, const ArenaParams *params)
|
|||||||
Assert(params->size > MemoryBlock_Header_Size);
|
Assert(params->size > MemoryBlock_Header_Size);
|
||||||
Assert(params->size < (U32_Max - platform_page_size())); // Reserving last piece so our math doesn't overflow
|
Assert(params->size < (U32_Max - platform_page_size())); // Reserving last piece so our math doesn't overflow
|
||||||
Assert(params->commit_size > 0);
|
Assert(params->commit_size > 0);
|
||||||
|
Assert(params->commit_size <= params->size);
|
||||||
|
|
||||||
|
|
||||||
MemoryBlock *block = memoryblock_allocate(params->size, params->commit_size);
|
MemoryBlock *block = memoryblock_allocate(params->size, params->commit_size);
|
||||||
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
||||||
@@ -43,12 +45,13 @@ void arena_init(Arena *arena, const ArenaParams *params)
|
|||||||
void arena_free(Arena *arena)
|
void arena_free(Arena *arena)
|
||||||
{
|
{
|
||||||
MemoryBlock *block = arena->current;
|
MemoryBlock *block = arena->current;
|
||||||
|
Assert(arena->checkpoint_level == 0);
|
||||||
while (block != NULL) {
|
while (block != NULL) {
|
||||||
MemoryBlock *prev = block->prev;
|
MemoryBlock *prev = block->prev;
|
||||||
memory_free(block->base, block->size);
|
memory_free(block->base, block->size);
|
||||||
block = prev;
|
block = prev;
|
||||||
}
|
}
|
||||||
|
arena->current = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *arena_push(Arena *arena, U32 size, U32 align)
|
void *arena_push(Arena *arena, U32 size, U32 align)
|
||||||
@@ -69,6 +72,8 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
}
|
}
|
||||||
|
|
||||||
MemoryBlock *new_block = memoryblock_allocate(block_size, new_commit_size);
|
MemoryBlock *new_block = memoryblock_allocate(block_size, new_commit_size);
|
||||||
|
// base_pos tracks the virtual absolute position within an arena
|
||||||
|
// This is not a contiguous space, we use it to free blocks when popping the arena
|
||||||
new_block->base_pos = block->base_pos + block->size;
|
new_block->base_pos = block->base_pos + block->size;
|
||||||
new_block->prev = block;
|
new_block->prev = block;
|
||||||
arena->current = new_block;
|
arena->current = new_block;
|
||||||
@@ -82,9 +87,9 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
U8 *mem = &block->base[start_pos];
|
U8 *mem = &block->base[start_pos];
|
||||||
if (block->committed_pos < end_pos) {
|
if (block->committed_pos < end_pos) {
|
||||||
// Commit more memory
|
// Commit more memory
|
||||||
U32 aligned_end_pos = AlignToPageEnd(start_pos + size);
|
U32 aligned_end_pos = AlignUpToPage(start_pos + size);
|
||||||
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_pos, arena->commit_size);
|
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_pos, arena->commit_size);
|
||||||
aligned_size = Min(aligned_size, block->size - block->committed_pos); // Note(nos): Is this actually right ?
|
aligned_size = Min(aligned_size, block->size - block->committed_pos);
|
||||||
|
|
||||||
memory_commit(&block->base[block->committed_pos], aligned_size);
|
memory_commit(&block->base[block->committed_pos], aligned_size);
|
||||||
block->committed_pos += aligned_size;
|
block->committed_pos += aligned_size;
|
||||||
@@ -120,9 +125,9 @@ void arena_pop_to(Arena *arena, U32 abs_pos)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArenaCheckPoint arena_temp_begin(Arena *arena)
|
ArenaCheckpoint arena_temp_begin(Arena *arena)
|
||||||
{
|
{
|
||||||
ArenaCheckPoint checkpoint = {};
|
ArenaCheckpoint checkpoint = {};
|
||||||
checkpoint.arena = arena;
|
checkpoint.arena = arena;
|
||||||
checkpoint.absolute_pos = arena_used(arena);
|
checkpoint.absolute_pos = arena_used(arena);
|
||||||
arena->checkpoint_level++;
|
arena->checkpoint_level++;
|
||||||
@@ -130,7 +135,7 @@ ArenaCheckPoint arena_temp_begin(Arena *arena)
|
|||||||
return checkpoint;
|
return checkpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_temp_end(ArenaCheckPoint *checkpoint)
|
void arena_temp_end(ArenaCheckpoint *checkpoint)
|
||||||
{
|
{
|
||||||
Arena *arena = checkpoint->arena;
|
Arena *arena = checkpoint->arena;
|
||||||
Assert(arena->checkpoint_level > 0);
|
Assert(arena->checkpoint_level > 0);
|
||||||
|
|||||||
+6
-6
@@ -2,7 +2,7 @@
|
|||||||
#include "base_core.h"
|
#include "base_core.h"
|
||||||
|
|
||||||
|
|
||||||
typedef U32 ArenaFlags; // no flags defined yets
|
typedef U32 ArenaFlags; // no flags defined yet
|
||||||
|
|
||||||
typedef struct ArenaParams ArenaParams;
|
typedef struct ArenaParams ArenaParams;
|
||||||
struct ArenaParams {
|
struct ArenaParams {
|
||||||
@@ -31,8 +31,8 @@ struct Arena {
|
|||||||
S32 checkpoint_level;
|
S32 checkpoint_level;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ArenaCheckPoint ArenaCheckPoint;
|
typedef struct ArenaCheckpoint ArenaCheckpoint;
|
||||||
struct ArenaCheckPoint {
|
struct ArenaCheckpoint {
|
||||||
Arena *arena;
|
Arena *arena;
|
||||||
U32 absolute_pos;
|
U32 absolute_pos;
|
||||||
};
|
};
|
||||||
@@ -47,11 +47,11 @@ void *arena_push(Arena *arena, U32 size, U32 align);
|
|||||||
#define arena_push_type(arena, type) arena_push(arena, (U32)sizeof(type), alignof(type))
|
#define arena_push_type(arena, type) arena_push(arena, (U32)sizeof(type), alignof(type))
|
||||||
#define arena_push_array_of(arena, type, len) arena_push(arena, (U32) (sizeof(type) * (len)), alignof(type))
|
#define arena_push_array_of(arena, type, len) arena_push(arena, (U32) (sizeof(type) * (len)), alignof(type))
|
||||||
|
|
||||||
ArenaCheckPoint arena_temp_begin(Arena *arena);
|
ArenaCheckpoint arena_temp_begin(Arena *arena);
|
||||||
void arena_temp_end(ArenaCheckPoint *checkpoint);
|
void arena_temp_end(ArenaCheckpoint *checkpoint);
|
||||||
void arena_pop_to(Arena *arena, U32 abs_pos);
|
void arena_pop_to(Arena *arena, U32 abs_pos);
|
||||||
void arena_reset(Arena *arena);
|
void arena_reset(Arena *arena);
|
||||||
MemoryBlock *memoryblock_allocate(size_t size, size_t commit_size);
|
MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size);
|
||||||
|
|
||||||
|
|
||||||
static INLINE U32 memoryblock_available(const MemoryBlock *block)
|
static INLINE U32 memoryblock_available(const MemoryBlock *block)
|
||||||
|
|||||||
+6
-5
@@ -41,10 +41,11 @@ typedef unsigned __int128 U128;
|
|||||||
#define U32_Max ((U32)0xffffffff)
|
#define U32_Max ((U32)0xffffffff)
|
||||||
#define U64_Max ((U64)0xffffffffffffffffULL)
|
#define U64_Max ((U64)0xffffffffffffffffULL)
|
||||||
|
|
||||||
#define U8_Min ((U8) 0x80)
|
// Duh ..
|
||||||
#define U16_Min ((U16)0x8000)
|
#define U8_Min ((U8)0x0)
|
||||||
#define U32_Min ((U32)0x80000000)
|
#define U16_Min ((U16)0x0)
|
||||||
#define U64_Min ((U64)0x8000000000000000ULL)
|
#define U32_Min ((U32)0x0)
|
||||||
|
#define U64_Min ((U64)0x0)
|
||||||
|
|
||||||
#define S8_Max ((S8) 0x7f)
|
#define S8_Max ((S8) 0x7f)
|
||||||
#define S16_Max ((S16)0x7fff)
|
#define S16_Max ((S16)0x7fff)
|
||||||
@@ -163,7 +164,7 @@ typedef unsigned __int128 U128;
|
|||||||
* @param y denominator
|
* @param y denominator
|
||||||
* @return x rounded down nearest multiple of y
|
* @return x rounded down nearest multiple of y
|
||||||
*/
|
*/
|
||||||
#define RounDownToMultiple(x, y) ((x) / (y) * (y))
|
#define RoundDownToMultiple(x, y) ((x) / (y) * (y))
|
||||||
#define Clamp(val, min, max) (((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))
|
#define Clamp(val, min, max) (((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
|
|||||||
+2
-2
@@ -65,7 +65,7 @@ static void arena_test_2(void)
|
|||||||
printf("Allocated ptr %p\n", ptr);
|
printf("Allocated ptr %p\n", ptr);
|
||||||
|
|
||||||
arena_dump(&arena);
|
arena_dump(&arena);
|
||||||
ArenaCheckPoint checkpoint = arena_temp_begin(&arena);
|
ArenaCheckpoint checkpoint = arena_temp_begin(&arena);
|
||||||
ptr = arena_push(&arena, MB(2), 8);
|
ptr = arena_push(&arena, MB(2), 8);
|
||||||
MemoryZero(ptr, MB(2));
|
MemoryZero(ptr, MB(2));
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ static void arena_test_2(void)
|
|||||||
ptr = arena_push(&arena, commit_size , 8);
|
ptr = arena_push(&arena, commit_size , 8);
|
||||||
MemoryZero(ptr, commit_size );
|
MemoryZero(ptr, commit_size );
|
||||||
|
|
||||||
ArenaCheckPoint checkpoint2 = arena_temp_begin(&arena);
|
ArenaCheckpoint checkpoint2 = arena_temp_begin(&arena);
|
||||||
ptr = arena_push(&arena, 64, 8);
|
ptr = arena_push(&arena, 64, 8);
|
||||||
MemoryZero(ptr, 64);
|
MemoryZero(ptr, 64);
|
||||||
|
|
||||||
|
|||||||
+9
-4
@@ -13,7 +13,6 @@ U32 platform_page_size(void)
|
|||||||
return getpagesize();
|
return getpagesize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *platform_malloc(size_t size)
|
void *platform_malloc(size_t size)
|
||||||
{
|
{
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
@@ -24,16 +23,22 @@ void platform_free(void *mem)
|
|||||||
free(mem);
|
free(mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *memory_allocate_and_commit(size_t size)
|
||||||
void *memory_allocate(size_t size)
|
|
||||||
{
|
{
|
||||||
Assert(IsPow2(size));
|
Assert(IsPageAligned(size));
|
||||||
|
|
||||||
void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||||
TRACEF("memory_allocate size=%7zu mem=%p\n", size, mem);
|
TRACEF("memory_allocate size=%7zu mem=%p\n", size, mem);
|
||||||
if (mem == MAP_FAILED) {
|
if (mem == MAP_FAILED) {
|
||||||
mem = NULL;
|
mem = NULL;
|
||||||
|
} else {
|
||||||
|
int rc = madvise(mem, size, MADV_POPULATE_WRITE);
|
||||||
|
if (rc != 0) {
|
||||||
|
munmap(mem, size);
|
||||||
|
mem = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -4,7 +4,7 @@
|
|||||||
void *platform_malloc(size_t size);
|
void *platform_malloc(size_t size);
|
||||||
U32 platform_page_size(void);
|
U32 platform_page_size(void);
|
||||||
|
|
||||||
void *memory_allocate(size_t size);
|
void *memory_allocate_and_commit(size_t size);
|
||||||
void *memory_reserve(size_t size);
|
void *memory_reserve(size_t size);
|
||||||
void memory_commit(void *mem, size_t size);
|
void memory_commit(void *mem, size_t size);
|
||||||
void memory_decommit(void *mem, size_t size);
|
void memory_decommit(void *mem, size_t size);
|
||||||
@@ -12,7 +12,7 @@ void memory_decommit(void *mem, size_t size);
|
|||||||
void memory_free(void *mem, size_t size);
|
void memory_free(void *mem, size_t size);
|
||||||
|
|
||||||
|
|
||||||
static INLINE size_t AlignToPageEnd(size_t value)
|
static INLINE size_t AlignUpToPage(size_t value)
|
||||||
{
|
{
|
||||||
U32 page_size = platform_page_size();
|
U32 page_size = platform_page_size();
|
||||||
size_t aligned_value = AlignUpPow2(value, page_size);
|
size_t aligned_value = AlignUpPow2(value, page_size);
|
||||||
@@ -20,7 +20,7 @@ static INLINE size_t AlignToPageEnd(size_t value)
|
|||||||
return aligned_value;
|
return aligned_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static INLINE size_t AlignToPageStart(size_t value)
|
static INLINE size_t AlignDownToPage(size_t value)
|
||||||
{
|
{
|
||||||
U32 page_size = platform_page_size();
|
U32 page_size = platform_page_size();
|
||||||
size_t aligned_value= AlignDownPow2(value, page_size);
|
size_t aligned_value= AlignDownPow2(value, page_size);
|
||||||
|
|||||||
Reference in New Issue
Block a user