vn/code/core/core_memory.h

81 lines
2.5 KiB
C
Raw Permalink Normal View History

2023-06-21 16:59:36 +00:00
/* date = June 19th 2023 10:07 pm */
#ifndef CORE_MEMORY_H
#define CORE_MEMORY_H
/*
The arenas can be used in two modes:
1. Fixed Size:
This is primarily intended for when you need a contigous buffer of memory, it reserves a large chunk
of virtual address space (if applicable) and partitions it to the user.
2. Chaining:
This is the workaround for WASM not supporting memory mapping. It allocates in chunks based on the initial
size requested in the ArenaAlloc call. Internally it just consists of a cyclic doubly linked list.
Potential alternatives:
If you pass in a size of 0 in ArenaAlloc, you get a chaining arena with set chunk size that can extend if
a PushSize call requests more than the prespecified standard. (ex. 4KB chunks, but if you load a file then
that becomes its own chunk)
*/
2023-07-19 15:09:41 +00:00
////////////////////////////////
//- sixten: Common Memory Functions
2023-06-21 16:59:36 +00:00
static void Copy(void *Dest, void *Source, umm Count);
2023-07-19 15:09:41 +00:00
static void CopyReverse(void *Dest, void *Source, umm Count);
2023-06-21 16:59:36 +00:00
static void Fill(void *Dest, u8 Value, umm Count);
2023-07-19 15:09:41 +00:00
#define Move(Dest, Source, Count) memmove(Dest, Source, Count)
2023-09-13 04:42:11 +00:00
#define ZeroStruct(Struct) Fill(Struct, 0, sizeof(*(Struct)))
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
////////////////////////////////
//- sixten: Memory Arena Types
2023-06-21 16:59:36 +00:00
struct arena
2023-06-21 16:59:36 +00:00
{
2023-12-23 07:27:22 +00:00
u64 Position;
u64 CommitPosition;
u64 Size;
u64 Align;
b32 Chaining;
b32 NotFirst;
2024-01-20 11:18:57 +00:00
arena *Next;
2023-12-23 07:27:22 +00:00
arena *Prev;
2023-06-21 16:59:36 +00:00
};
2023-12-23 07:27:22 +00:00
struct temp
2023-06-21 16:59:36 +00:00
{
2023-12-23 07:27:22 +00:00
arena *Arena;
u64 Position;
2023-06-21 16:59:36 +00:00
};
2023-07-19 15:09:41 +00:00
////////////////////////////////
//~ sixten: Memory Arena Settings
#define MEMORY_ARENA_COMMIT_SIZE Kilobytes(4)
#define MEMORY_ARENA_DECOMMIT_THRESHOLD Megabytes(64)
////////////////////////////////
//- sixten: Memory Arena Functions
2024-01-20 11:18:57 +00:00
static arena *ArenaAlloc(u64 Size, b32 Chaining, char *Format, ...);
static void ArenaRelease(arena *Arena);
static void *ArenaPushNoClear(arena *Arena, u64 Size);
static void *ArenaPush(arena *Arena, u64 Size);
static void ArenaPopTo(arena *Arena, u64 Position);
static void ArenaPop(arena *Arena, u64 Amount);
static void ArenaClear(arena *Arena);
static void ArenaSetAlign(arena *Arena, u64 Align);
2023-07-19 15:09:41 +00:00
#define PushArray(Arena, type, Count) (type *)ArenaPush((Arena), sizeof(type)*(Count))
#define PushArrayNoClear(Arena, type, Count) (type *)ArenaPushNoClear((Arena), sizeof(type)*(Count))
#define PushStruct(Arena, type) (type *)ArenaPush((Arena), sizeof(type))
#define PushStructNoClear(Arena, type) (type *)ArenaPushNoClear((Arena), sizeof(type))
////////////////////////////////
//- sixten: Temporary Memory Functions
2023-06-21 16:59:36 +00:00
2023-12-23 07:27:22 +00:00
static temp BeginTemp(arena *Arena);
static void EndTemp(temp Temp);
2023-06-21 16:59:36 +00:00
#endif //CORE_MEMORY_H