commit 318c2a4f969edc351e6275448a03e41bf3524a22
parent d6f5175966a33db2a1e5a5e51ec0abeb53f784a7
Author: MikoĊaj Lenczewski <mblenczewski@gmail.com>
Date: Sun, 12 Feb 2023 02:45:27 +0000
Make mem_pool_init() and mem_pool_free() alignment-aware
Diffstat:
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/include/utils.h b/include/utils.h
@@ -94,20 +94,35 @@ mem_pool_resize(struct mem_pool *self, u64 capacity) {
}
static inline bool
-mem_pool_init(struct mem_pool *self, u64 capacity) {
+mem_pool_init(struct mem_pool *self, u64 alignment, u64 capacity) {
assert(self);
+ assert(alignment);
+ assert(alignment == 1 || alignment % 2 == 0);
+ assert(capacity % alignment == 0);
+
+#ifdef _WIN32
+ u8 *ptr = _aligned_malloc(alignment, capacity);
+#else
+ u8 *ptr = aligned_alloc(alignment, capacity);
+#endif
+ if (!ptr) return false;
- self->ptr = NULL;
- self->cap = self->len = 0;
+ self->ptr = ptr;
+ self->cap = capacity;
+ self->len = 0;
- return mem_pool_resize(self, capacity);
+ return true;
}
static inline void
mem_pool_free(struct mem_pool *self) {
assert(self);
+#ifdef _WIN32
+ _aligned_free(self->ptr);
+#else
free(self->ptr);
+#endif
}
static inline void