memory.c (882B)
1 #include "memory.h" 2 #include "logger.h" 3 4 #include <stdlib.h> 5 #include <string.h> 6 7 struct MemoryStats { 8 u64 allocated; 9 u64 allocated_tagged[MEMORY_TAG_MAX_TAGS]; 10 }; 11 12 static struct MemoryStats stats; 13 14 void *s_alloc(u64 size, memory_tag tag) 15 { 16 if (tag == MEMORY_TAG_UNKNOWN) 17 { 18 LOGW("s_alloc MEMORY_TAG_UNKNOWN"); 19 } 20 21 stats.allocated += size; 22 stats.allocated_tagged[tag] += size; 23 24 void *mem = malloc(size); 25 26 s_zeroMemory(mem, size); 27 28 return mem; 29 } 30 31 void s_free(void *mem, u64 size, memory_tag tag) 32 { 33 if (tag == MEMORY_TAG_UNKNOWN) 34 { 35 LOGW("s_free MEMORY_TAG_UNKNOWN"); 36 } 37 38 stats.allocated -= size; 39 stats.allocated_tagged[tag] -= size; 40 41 free(mem); 42 } 43 44 void s_zeroMemory(void *mem, u64 size) 45 { 46 memset(mem, 0, size); 47 } 48 49 void s_copyMemory(void *dst, const void *src, u64 size) 50 { 51 memcpy(dst, src, size); 52 } 53