aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/kpl/env.h2
-rw-r--r--source/memory.c4
-rw-r--r--test/memory.c16
3 files changed, 18 insertions, 4 deletions
diff --git a/include/kpl/env.h b/include/kpl/env.h
index 19d9536..f3ad721 100644
--- a/include/kpl/env.h
+++ b/include/kpl/env.h
@@ -47,7 +47,7 @@ typedef struct _kpl_env {
} kpl_env; // kpl state, no globals
/*
- * All objects are retuned by this
+ * All objects are returned by this
*/
inline void kpl_env_object_return(kpl_env *env, void *object) {
kpl_memory_allocator_return(&env->threads[((kpl_object*) object)->thread_id].memory_allocator, object);
diff --git a/source/memory.c b/source/memory.c
index d7618a7..a9bcc30 100644
--- a/source/memory.c
+++ b/source/memory.c
@@ -56,7 +56,7 @@ static const int32_t pages_per_pow2[] = {
[19] = 128,
[20] = 256,
[21] = 512,
- [22] = 1024,
+ [22] = 1024
};
static kpl_memory_allocator_slab *kpl_memory_allocator_slab_init(int32_t size_pow2) {
@@ -94,7 +94,7 @@ static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_
}
void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, int32_t thread_id) {
- if (size < (1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2))
+ if (size < 1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2)
return NULL;
int32_t size_pow2 = kpl_memory_allocator_get_size_pow2(size);
if (size_pow2 < KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2)
diff --git a/test/memory.c b/test/memory.c
index 05db8a2..9308b2b 100644
--- a/test/memory.c
+++ b/test/memory.c
@@ -1,7 +1,7 @@
#include "kpl_test.h"
-KPL_TEST(allocate) {
+KPL_TEST(allocate_and_pool) {
kpl_memory_allocator allocator = {};
kpl_memory_allocator_init(&allocator);
for (int32_t times = 0; times < 700; times++) {
@@ -17,3 +17,17 @@ KPL_TEST(allocate) {
KPL_ASSERT(slab_group32->slab_head == slab_group32->slab_tail, "Only one slab should have been allocated");
kpl_memory_allocator_free(&allocator);
}
+
+KPL_TEST(allocate_multiple_slabs) {
+ kpl_memory_allocator allocator = {};
+ kpl_memory_allocator_init(&allocator);
+ kpl_object *large1 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID);
+ kpl_object *large2 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID);
+ KPL_ASSERT(large1->object_size == 1 << 13, "Invalid object size");
+ KPL_ASSERT(large1->object_size == large2->object_size, "Objects should have the same size");
+ kpl_memory_allocator_slab_group *slab_group = &allocator.slabs[13];
+ KPL_ASSERT(slab_group->slab_head != slab_group->slab_tail, "More the one slab should have been allocated");
+ kpl_memory_allocator_return(&allocator, large1);
+ kpl_memory_allocator_return(&allocator, large2);
+ kpl_memory_allocator_free(&allocator);
+}