New allocators, testing framework, overall improvements
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
extern int test_libc_allocator();
|
||||
extern int test_platform_allocator();
|
||||
extern int test_arena_allocator();
|
||||
|
||||
int main(void) {
|
||||
return !(
|
||||
test_libc_allocator() &&
|
||||
test_platform_allocator() &&
|
||||
test_arena_allocator()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#include "malunal/microtest.h"
|
||||
MICROTEST_MAIN()
|
||||
+57
-283
@@ -1,304 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators/arena.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
#define assert_equals_result(condition) \
|
||||
if (!(condition)) { \
|
||||
fprintf( \
|
||||
stderr, \
|
||||
format, \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
); \
|
||||
return (allocation_result_t) { \
|
||||
.threw = 1, \
|
||||
.error = ALLOCATION_ERROR_FAILURE \
|
||||
}; \
|
||||
}
|
||||
MICROTEST(arena_allocator, can_init_and_free) {
|
||||
error_t result = {};
|
||||
arena_allocator_t allocator = {};
|
||||
malunal_uint32_t temporary = 0;
|
||||
|
||||
#define assert_equals(condition) \
|
||||
if (!(condition)) { \
|
||||
fprintf( \
|
||||
stderr, \
|
||||
format, \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
); \
|
||||
return 0; \
|
||||
}
|
||||
result = arena_allocator_init(500, &allocator);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
result = arena_allocator_size(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, platform_page_size());
|
||||
|
||||
const malunal_cstr_t format = "[arena_allocator(%s:%d)]: %s failed\n";
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 16);
|
||||
|
||||
const malunal_size_t page_bytes = 4096;
|
||||
const malunal_size_t pre_alloc = 16;
|
||||
const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4;
|
||||
const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8;
|
||||
const malunal_size_t init_with_pre = pre_alloc + init_bytes;
|
||||
const malunal_size_t all_bytes = pre_alloc + init_bytes + final_bytes;
|
||||
result = arena_allocator_free(&allocator);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
test_arena_initialize(
|
||||
allocator_mptr_t arena,
|
||||
allocator_mptr_t upstream
|
||||
) {
|
||||
assert_equals(allocator_initialize(arena, upstream, page_bytes) == ALLOCATION_ERROR_SUCCESS);
|
||||
result = arena_allocator_size(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == pre_alloc);
|
||||
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
|
||||
static
|
||||
allocation_result_t
|
||||
test_arena_acquires(allocator_mptr_t arena) {
|
||||
allocation_result_t result = allocator_acquire(arena, init_bytes);
|
||||
if (result.threw)
|
||||
return result;
|
||||
MICROTEST(arena_allocator, can_acquire_and_dispose) {
|
||||
error_t result = {};
|
||||
arena_allocator_t allocator = {};
|
||||
malunal_mptr_t address = null;
|
||||
malunal_uint32_t temporary = 0;
|
||||
|
||||
// Upstream checks.
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals_result(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
arena_allocator_init(500, &allocator);
|
||||
result = arena_allocator_acquire(&allocator, 64, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals_result(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 80);
|
||||
|
||||
assert_equals_result(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
result = arena_allocator_dispose(&allocator, address, 64);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
assert_equals_result(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == 1);
|
||||
|
||||
assert_equals_result(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == 0);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals_result(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
|
||||
assert_equals_result(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == init_with_pre);
|
||||
|
||||
assert_equals_result(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == 1);
|
||||
|
||||
assert_equals_result(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == 0);
|
||||
return result;
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 80);
|
||||
arena_allocator_free(&allocator);
|
||||
}
|
||||
|
||||
static int
|
||||
test_arena_reacquire(
|
||||
allocator_mptr_t arena,
|
||||
malunal_mptr_t address
|
||||
) {
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
MICROTEST(arena_allocator, can_reset_allocator) {
|
||||
error_t result = {};
|
||||
arena_allocator_t allocator = {};
|
||||
malunal_mptr_t address = null;
|
||||
malunal_uint32_t temporary = 0;
|
||||
|
||||
vector = address;
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
arena_allocator_init(500, &allocator);
|
||||
result = arena_allocator_acquire(&allocator, 64, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
allocation_result_t result = allocator_reacquire(
|
||||
arena,
|
||||
address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 80);
|
||||
|
||||
if (result.threw)
|
||||
return result.error;
|
||||
result = arena_allocator_reset(&allocator);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == all_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
test_arena_release(
|
||||
allocator_mptr_t arena,
|
||||
malunal_mptr_t address
|
||||
) {
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
|
||||
vector = address;
|
||||
for (index = 0; index < 4; index++)
|
||||
if (vector[index] != index + 1)
|
||||
return 1;
|
||||
|
||||
allocation_error_t relexc =
|
||||
allocator_release(arena, address, final_bytes);
|
||||
|
||||
if (relexc != ALLOCATION_ERROR_SUCCESS)
|
||||
return relexc;
|
||||
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == all_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
test_arena_reset(allocator_mptr_t arena) {
|
||||
arena_reset(arena);
|
||||
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
test_arena_finalize(allocator_mptr_t arena) {
|
||||
allocator_finalize(arena);
|
||||
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_acquires_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
// Arena checks.
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int test_arena_allocator() {
|
||||
allocator_t upstream = platform_allocator();
|
||||
allocator_t arena = arena_allocator();
|
||||
assert_equals(test_arena_initialize(&arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
allocation_result_t result =
|
||||
test_arena_acquires(&arena);
|
||||
|
||||
return !(
|
||||
!result.threw &&
|
||||
test_arena_reacquire(&arena, result.address) &&
|
||||
test_arena_release(&arena, result.address) &&
|
||||
test_arena_reset(&arena) &&
|
||||
test_arena_finalize(&arena)
|
||||
);
|
||||
result = arena_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 16);
|
||||
arena_allocator_free(&allocator);
|
||||
}
|
||||
|
||||
+10
-92
@@ -1,95 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.h"
|
||||
#include "malunal/allocator.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
#define assert_equals(condition) \
|
||||
do { \
|
||||
if ((condition)) break; \
|
||||
const malunal_cstr_t format = \
|
||||
"[libc_allocator(%s:%d)]: " \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
" failed\n"; \
|
||||
fprintf(stderr, format, __FILE__, __LINE__); \
|
||||
return 0; \
|
||||
} while (0)
|
||||
MICROTEST(libc_allocator, can_acquire_and_dispose) {
|
||||
malunal_mptr_t address = null;
|
||||
allocator_mptr_t allocator = libc_allocator();
|
||||
error_t result = allocator_acquire(allocator, 32, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
|
||||
int test_libc_allocator() {
|
||||
const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4;
|
||||
const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8;
|
||||
|
||||
allocation_result_t result;
|
||||
|
||||
allocator_t libc_alloc = libc_allocator();
|
||||
allocator_mptr_t allocator = &libc_alloc;
|
||||
result = allocator_acquire(allocator, init_bytes);
|
||||
|
||||
if (result.threw)
|
||||
return result.error;
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
|
||||
vector = result.address;
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
|
||||
result = allocator_reacquire(
|
||||
allocator,
|
||||
result.address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.error;
|
||||
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
vector = result.address;
|
||||
for (index = 0; index < 4; index++)
|
||||
if (vector[index] != index + 1)
|
||||
return 1;
|
||||
|
||||
allocation_error_t relexc = allocator_release(
|
||||
allocator,
|
||||
result.address,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
return relexc == ALLOCATION_ERROR_SUCCESS;
|
||||
result = allocator_dispose(allocator, address, 32);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "malunal/allocators/linear.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
MICROTEST(linear_allocator, can_init) {
|
||||
error_t result = {};
|
||||
linear_allocator_t allocator = {};
|
||||
malunal_mptr_t address = null;
|
||||
malunal_uint32_t temporary = 0;
|
||||
|
||||
allocator_acquire(libc_allocator(), 128, &address);
|
||||
result = linear_allocator_init(128, address, &allocator);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
result = linear_allocator_size(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 128);
|
||||
|
||||
result = linear_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
|
||||
MICROTEST(linear_allocator, can_acquire_and_dispose) {
|
||||
error_t result = {};
|
||||
linear_allocator_t allocator = {};
|
||||
malunal_mptr_t original = null;
|
||||
malunal_mptr_t address = null;
|
||||
malunal_uint32_t temporary = 0;
|
||||
|
||||
allocator_acquire(libc_allocator(), 128, &original);
|
||||
linear_allocator_init(128, original, &allocator);
|
||||
result = linear_allocator_acquire(&allocator, 64, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
result = linear_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 64);
|
||||
|
||||
result = linear_allocator_dispose(&allocator, address, 64);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
result = linear_allocator_used(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 64);
|
||||
allocator_dispose(libc_allocator(), original, 128);
|
||||
}
|
||||
+10
-92
@@ -1,95 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.h"
|
||||
#include "malunal/allocator.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
#define assert_equals(condition) \
|
||||
do { \
|
||||
if ((condition)) break; \
|
||||
const malunal_cstr_t format = \
|
||||
"[platform_allocator(%s:%d)]: " \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
" failed\n"; \
|
||||
fprintf(stderr, format, __FILE__, __LINE__); \
|
||||
return 0; \
|
||||
} while (0)
|
||||
MICROTEST(platform_allocator, can_acquire_and_dispose) {
|
||||
malunal_mptr_t address = null;
|
||||
allocator_mptr_t allocator = platform_allocator();
|
||||
error_t result = allocator_acquire(allocator, 32, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
|
||||
int test_platform_allocator() {
|
||||
const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4;
|
||||
const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8;
|
||||
|
||||
allocation_result_t result;
|
||||
|
||||
allocator_t plat_alloc = platform_allocator();
|
||||
allocator_mptr_t allocator = &plat_alloc;
|
||||
result = allocator_acquire(allocator, init_bytes);
|
||||
|
||||
if (result.threw)
|
||||
return result.error;
|
||||
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
|
||||
vector = result.address;
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
|
||||
result = allocator_reacquire(
|
||||
allocator,
|
||||
result.address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.error;
|
||||
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
vector = result.address;
|
||||
for (index = 0; index < 4; index++)
|
||||
if (vector[index] != index + 1)
|
||||
return 1;
|
||||
|
||||
allocation_error_t relexc = allocator_release(
|
||||
allocator,
|
||||
result.address,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
return relexc == ALLOCATION_ERROR_SUCCESS;
|
||||
result = allocator_dispose(allocator, address, 32);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "malunal/allocators/pool.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
MICROTEST(pool_allocator, can_init_and_free) {
|
||||
error_t result = {};
|
||||
pool_allocator_t allocator = {};
|
||||
malunal_size_t temporary = 0;
|
||||
|
||||
result = pool_allocator_init(16, 8, null, &allocator);
|
||||
MICROTEST_ASSERT_NULL(result.domain);
|
||||
|
||||
result = pool_allocator_stride(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 16);
|
||||
|
||||
result = pool_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = pool_allocator_capacity(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 8);
|
||||
|
||||
result = pool_allocator_free(&allocator);
|
||||
MICROTEST_ASSERT_NULL(result.domain);
|
||||
|
||||
result = pool_allocator_stride(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = pool_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = pool_allocator_capacity(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
|
||||
MICROTEST(pool_allocator, can_acquire_and_dispose) {
|
||||
error_t result = {};
|
||||
pool_allocator_t allocator = {};
|
||||
malunal_mptr_t address = null;
|
||||
malunal_size_t temporary = 0;
|
||||
|
||||
pool_allocator_init(16, 8, null, &allocator);
|
||||
result = pool_allocator_acquire(&allocator, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
result = pool_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 1);
|
||||
|
||||
result = pool_allocator_dispose(&allocator, address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
result = pool_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "malunal/allocators/stack.h"
|
||||
#include "malunal/microtest.h"
|
||||
|
||||
MICROTEST(stack_allocator, can_init_and_free) {
|
||||
error_t result = {};
|
||||
stack_allocator_t allocator = {};
|
||||
malunal_size_t temporary = 0;
|
||||
|
||||
result = stack_allocator_init(16, 8, null, &allocator);
|
||||
MICROTEST_ASSERT_NULL(result.domain);
|
||||
|
||||
result = stack_allocator_stride(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 16);
|
||||
|
||||
result = stack_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = stack_allocator_capacity(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 8);
|
||||
|
||||
result = stack_allocator_free(&allocator);
|
||||
MICROTEST_ASSERT_NULL(result.domain);
|
||||
|
||||
result = stack_allocator_stride(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = stack_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
|
||||
result = stack_allocator_capacity(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
|
||||
MICROTEST(stack_allocator, can_acquire_and_dispose) {
|
||||
error_t result = {};
|
||||
stack_allocator_t allocator = {};
|
||||
malunal_mptr_t address = null;
|
||||
malunal_size_t temporary = 0;
|
||||
|
||||
stack_allocator_init(16, 8, null, &allocator);
|
||||
result = stack_allocator_acquire(&allocator, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
result = stack_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 1);
|
||||
|
||||
result = stack_allocator_dispose(&allocator, address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
|
||||
result = stack_allocator_count(&allocator, &temporary);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_EXPECT_EQ(temporary, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user