comparison dowa/dowa.h @ 65:ecb6ee6a22c3

[Misc] I will no longer drink cool aids of capital P.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Dec 2025 06:22:59 -0800
parents a30944e5719e
children 75de5903355c
comparison
equal deleted inserted replaced
64:a30944e5719e 65:ecb6ee6a22c3
43 // --- Arena Allocator --- // 43 // --- Arena Allocator --- //
44 typedef struct { 44 typedef struct {
45 char *buffer; 45 char *buffer;
46 size_t offset; 46 size_t offset;
47 size_t capacity; 47 size_t capacity;
48 } Dowa_Arena, *Dowa_PArena; 48 } Dowa_Arena;
49 49
50 /* Creates a new arena with the specified capacity (in bytes). Returns a pointer to the arena, or NULL on failure. */ 50 /* Creates a new arena with the specified capacity (in bytes). Returns a pointer to the arena, or NULL on failure. */
51 extern Dowa_PArena Dowa_Arena_Create(size_t capacity); 51 extern Dowa_Arena *Dowa_Arena_Create(size_t capacity);
52 /* Allocates a block of memory of the given size from the arena. Returns pointer to the allocated memory, or NULL if there is insufficient space. */ 52 /* Allocates a block of memory of the given size from the arena. Returns pointer to the allocated memory, or NULL if there is insufficient space. */
53 extern void *Dowa_Arena_Allocate(Dowa_PArena arena, size_t size); 53 extern void *Dowa_Arena_Allocate(Dowa_Arena *arena, size_t size);
54 /* Destroys the arena and frees its underlying memory block.*/ 54 /* Destroys the arena and frees its underlying memory block.*/
55 extern void Dowa_Arena_Destroy(Dowa_PArena arena); 55 extern void Dowa_Arena_Destroy(Dowa_Arena *arena);
56 /* Strdup but saves within the arena */ 56 /* Strdup but saves within the arena */
57 extern void *Dowa_Arena_Copy(Dowa_PArena p_arena, const void *src, size_t size); 57 extern void *Dowa_Arena_Copy(Dowa_Arena *p_arena, const void *src, size_t size);
58 /* Resets the arena offset to 0, allowing reuse without freeing */ 58 /* Resets the arena offset to 0, allowing reuse without freeing */
59 extern void Dowa_Arena_Reset(Dowa_PArena p_arena); 59 extern void Dowa_Arena_Reset(Dowa_Arena *p_arena);
60 /* Returns the current number of bytes allocated in the arena */ 60 /* Returns the current number of bytes allocated in the arena */
61 extern size_t Dowa_Arena_Get_Used(Dowa_PArena p_arena); 61 extern size_t Dowa_Arena_Get_Used(Dowa_Arena *p_arena);
62 /* Returns the remaining capacity in bytes */ 62 /* Returns the remaining capacity in bytes */
63 extern size_t Dowa_Arena_Get_Remaining(Dowa_PArena p_arena); 63 extern size_t Dowa_Arena_Get_Remaining(Dowa_Arena *p_arena);
64 64
65 65
66 // --- HashMap --- // 66 // --- HashMap --- //
67 typedef enum { 67 typedef enum {
68 DOWA_HASH_MAP_TYPE_BUFFER, // Raw byte buffer 68 DOWA_HASH_MAP_TYPE_BUFFER, // Raw byte buffer
70 DOWA_HASH_MAP_TYPE_HASHMAP, // Nested hashmap 70 DOWA_HASH_MAP_TYPE_HASHMAP, // Nested hashmap
71 DOWA_HASH_MAP_TYPE_INT // Integer value 71 DOWA_HASH_MAP_TYPE_INT // Integer value
72 } Dowa_HashMap_ValueType; 72 } Dowa_HashMap_ValueType;
73 73
74 typedef struct Dowa_HashEntry { 74 typedef struct Dowa_HashEntry {
75 char *key; 75 char *key;
76 void *buffer; 76 void *buffer;
77 size_t capacity; 77 size_t capacity;
78 Dowa_HashMap_ValueType type; 78 Dowa_HashMap_ValueType type;
79 struct Dowa_HashEntry *next; 79 struct Dowa_HashEntry *next;
80 } Dowa_HashEntry, *Dowa_PHashEntry; 80 } Dowa_HashEntry;
81 81
82 typedef struct { 82 typedef struct {
83 Dowa_PHashEntry *entries; 83 Dowa_HashEntry **entries;
84 size_t capacity; 84 size_t capacity;
85 uint32 current_capacity; 85 uint32 current_capacity;
86 Dowa_PArena p_arena; 86 Dowa_Arena *p_arena;
87 } Dowa_HashMap, *Dowa_PHashMap; 87 } Dowa_HashMap;
88 88
89 /* Creates a new hashmap with the given initial capacity. Returns pointer to the hashmap, or NULL on failure. */ 89 /* Creates a new hashmap with the given initial capacity. Returns pointer to the hashmap, or NULL on failure. */
90 extern Dowa_PHashMap Dowa_HashMap_Create(size_t capacity); 90 extern Dowa_HashMap *Dowa_HashMap_Create(size_t capacity);
91 /* Creates a new hashmap with the given initial capacity, using the provided arena for all internal allocations. Returns pointer to the hashmap, or NULL on failure. */ 91 /* Creates a new hashmap with the given initial capacity, using the provided arena for all internal allocations. Returns pointer to the hashmap, or NULL on failure. */
92 extern Dowa_PHashMap Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_PArena arena); 92 extern Dowa_HashMap *Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_Arena *arena);
93 /* Destroys the hashmap and frees all associated memory */ 93 /* Destroys the hashmap and frees all associated memory */
94 extern void Dowa_HashMap_Destroy(Dowa_PHashMap p_hash_map); 94 extern void Dowa_HashMap_Destroy(Dowa_HashMap *p_hash_map);
95 /* Returns the index of the entry for the specified key, or -1 if the key is not found. */ 95 /* Returns the index of the entry for the specified key, or -1 if the key is not found. */
96 extern int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key); 96 extern int32 Dowa_HashMap_Get_Position(Dowa_HashMap *p_hash_map, const char *key);
97 /* Retrieves the value buffer for the specified key, or NULL if the key does not exist. */ 97 /* Retrieves the value buffer for the specified key, or NULL if the key does not exist. */
98 extern void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, const char *key); 98 extern void *Dowa_HashMap_Get(Dowa_HashMap *p_hash_map, const char *key);
99 /* Inserts a copy of the given value into the hashmap under the specified key. Uses DOWA_HASH_MAP_TYPE_BUFFER as the entry type. */ 99 /* Inserts a copy of the given value into the hashmap under the specified key. Uses DOWA_HASH_MAP_TYPE_BUFFER as the entry type. */
100 extern void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size); 100 extern void Dowa_HashMap_Push_Value(Dowa_HashMap *p_hash_map, const char *key, void *value, size_t value_size);
101 /* Inserts a copy of the given value with the specified type into the hashmap under the key. Returns the index of the new entry, or -1 on failure. */ 101 /* Inserts a copy of the given value with the specified type into the hashmap under the key. Returns the index of the new entry, or -1 on failure. */
102 extern int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); 102 extern int32 Dowa_HashMap_Push_Value_With_Type(Dowa_HashMap *p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type);
103 /* Inserts a value pointer into the hashmap under the specified key without copying data. The caller retains ownership of the pointer. Returns the entry index, or -1 on failure. */ 103 /* Inserts a value pointer into the hashmap under the specified key without copying data. The caller retains ownership of the pointer. Returns the entry index, or -1 on failure. */
104 extern int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); 104 extern int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_HashMap *p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type);
105 /* Removes the entry with the specified key from the hashmap and frees its data if owned. */ 105 /* Removes the entry with the specified key from the hashmap and frees its data if owned. */
106 extern void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, const char *key); 106 extern void Dowa_HashMap_Pop_Key(Dowa_HashMap *p_hash_map, const char *key);
107 /* Returns TRUE if the key exists in the hashmap, FALSE otherwise. */ 107 /* Returns TRUE if the key exists in the hashmap, FALSE otherwise. */
108 extern boolean Dowa_HashMap_Has_Key(Dowa_PHashMap p_hash_map, const char *key); 108 extern boolean Dowa_HashMap_Has_Key(Dowa_HashMap *p_hash_map, const char *key);
109 /* Removes all entries from the hashmap without destroying it. */ 109 /* Removes all entries from the hashmap without destroying it. */
110 extern void Dowa_HashMap_Clear(Dowa_PHashMap p_hash_map); 110 extern void Dowa_HashMap_Clear(Dowa_HashMap *p_hash_map);
111 /* Returns the number of entries currently in the hashmap. */ 111 /* Returns the number of entries currently in the hashmap. */
112 extern uint32 Dowa_HashMap_Get_Count(Dowa_PHashMap p_hash_map); 112 extern uint32 Dowa_HashMap_Get_Count(Dowa_HashMap *p_hash_map);
113 113
114 // --- String manipuliation -- // 114 // --- String manipuliation -- //
115 // Splice string from start to end 115 // Splice string from start to end
116 const char *Dowa_String_Slice(const char *from, size_t start, size_t end); 116 const char *Dowa_String_Slice(const char *from, size_t start, size_t end);
117 // --- Miscellaneous --- // 117 // --- Miscellaneous --- //
118 char *Dowa_Int32ToString(uint32 value, char *buffer); /* Just use atoid lmao*/ 118 char *Dowa_Int32ToString(uint32 value, char *buffer); /* Just use atoid lmao*/
119 119
120 // --- Utility Functions --- // 120 // --- Utility Functions --- //
121 /* Prints all entries in the hashmap to stdout for debugging purposes. */ 121 /* Prints all entries in the hashmap to stdout for debugging purposes. */
122 extern void Dowa_HashMap_Print(Dowa_PHashMap map); 122 extern void Dowa_HashMap_Print(Dowa_HashMap *map);
123 #ifdef DIRECTORY 123 #ifdef DIRECTORY
124 /* Loads all files from the specified folder into the hashmap. Uses file names as keys and file contents as values. Returns 0 on success, or -1 on failure. */ 124 /* Loads all files from the specified folder into the hashmap. Uses file names as keys and file contents as values. Returns 0 on success, or -1 on failure. */
125 extern int Dowa_HashMap_Cache_Folder(Dowa_PHashMap map, const char *folder_path); 125 extern int Dowa_HashMap_Cache_Folder(Dowa_HashMap *map, const char *folder_path);
126 #endif 126 #endif
127 127
128 128
129 #endif 129 #endif