Reworked, no version bump
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ extern int test_arena_allocator();
|
||||
|
||||
int main(void) {
|
||||
return !(
|
||||
test_libc_allocator() &&
|
||||
test_libc_allocator() &&
|
||||
test_platform_allocator() &&
|
||||
test_arena_allocator()
|
||||
);
|
||||
|
||||
+196
-311
@@ -1,104 +1,115 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators/arena.h"
|
||||
|
||||
const malunal_size_t page_bytes = 4096;
|
||||
const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4;
|
||||
const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8;
|
||||
#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 \
|
||||
}; \
|
||||
}
|
||||
|
||||
#define assert_equals(condition) \
|
||||
if (!(condition)) { \
|
||||
fprintf( \
|
||||
stderr, \
|
||||
format, \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
|
||||
const malunal_cstr_t format = "[arena_allocator(%s:%d)]: %s failed\n";
|
||||
|
||||
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;
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
test_arena_acquires(allocator_mptr_t arena) {
|
||||
allocator_acquire_res_t result =
|
||||
allocator_acquire((allocator_acquire_req_t) {
|
||||
.allocator = arena,
|
||||
.size = init_bytes
|
||||
});
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Upstream checks.
|
||||
if (arena->upstream->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->reserved);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals_result(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
if (arena->upstream->allocated != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->allocated);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals_result(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
|
||||
if (arena->upstream->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->acquires);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
assert_equals_result(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
|
||||
if (arena->upstream->releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->releases != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->releases);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
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.
|
||||
if (arena->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->reserved);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
assert_equals_result(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == page_bytes);
|
||||
|
||||
if (arena->allocated != init_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, init_bytes, arena->allocated);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
assert_equals_result(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals_result(count == init_with_pre);
|
||||
|
||||
if (arena->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->acquires);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
|
||||
if (arena->releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->releases != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->releases);
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_FAILURE
|
||||
};
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -114,77 +125,45 @@ test_arena_reacquire(
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
|
||||
allocator_acquire_res_t result =
|
||||
allocator_reacquire((allocator_reacquire_req_t) {
|
||||
.allocator = arena,
|
||||
.prev = result.address,
|
||||
.oldsz = init_bytes,
|
||||
.newsz = final_bytes
|
||||
});
|
||||
allocation_result_t result = allocator_reacquire(
|
||||
arena,
|
||||
address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
// Upstream checks.
|
||||
if (arena->upstream->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->reserved);
|
||||
return 1;
|
||||
}
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
if (arena->upstream->allocated != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->allocated);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->releases != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->releases);
|
||||
return 1;
|
||||
}
|
||||
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.
|
||||
if (arena->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
malunal_int32_t all_bytes = init_bytes + final_bytes;
|
||||
if (arena->allocated != all_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, all_bytes, arena->allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == all_bytes);
|
||||
|
||||
if (arena->acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (arena->releases != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->releases != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->releases);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -200,220 +179,126 @@ test_arena_release(
|
||||
if (vector[index] != index + 1)
|
||||
return 1;
|
||||
|
||||
allocator_exception_t relexc =
|
||||
allocator_release((allocator_release_req_t) {
|
||||
.allocator = arena,
|
||||
.address = address,
|
||||
.size = final_bytes
|
||||
});
|
||||
allocation_error_t relexc =
|
||||
allocator_release(arena, address, final_bytes);
|
||||
|
||||
if (relexc != ALLOCATOR_EXCEPTION_SUCCESS)
|
||||
if (relexc != ALLOCATION_ERROR_SUCCESS)
|
||||
return relexc;
|
||||
|
||||
// Upstream checks.
|
||||
if (arena->upstream->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->reserved);
|
||||
return 1;
|
||||
}
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
if (arena->upstream->allocated != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->allocated);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->releases != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->releases);
|
||||
return 1;
|
||||
}
|
||||
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.
|
||||
if (arena->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
malunal_int32_t all_bytes = init_bytes + final_bytes;
|
||||
if (arena->allocated != all_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, all_bytes, arena->allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == all_bytes);
|
||||
|
||||
if (arena->acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (arena->releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->releases != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->releases);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
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);
|
||||
|
||||
// Upstream checks.
|
||||
if (arena->upstream->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->reserved);
|
||||
return 1;
|
||||
}
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
if (arena->upstream->allocated != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->upstream->allocated);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->upstream->releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->releases != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->releases);
|
||||
return 1;
|
||||
}
|
||||
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.
|
||||
if (arena->reserved != page_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, page_bytes, arena->reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == page_bytes);
|
||||
|
||||
if (arena->allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->allocated != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (arena->acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (arena->releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->releases != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->releases);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
assert_equals(allocator_releases_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
test_arena_free(allocator_mptr_t arena) {
|
||||
arena_free(arena);
|
||||
test_arena_finalize(allocator_mptr_t arena) {
|
||||
allocator_finalize(arena);
|
||||
|
||||
// Upstream checks.
|
||||
if (arena->upstream->reserved != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->reserved != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->reserved);
|
||||
return 1;
|
||||
}
|
||||
allocator_mptr_t upstream;
|
||||
assert_equals(allocator_upstream(arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
if (arena->upstream->allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->allocated);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (arena->upstream->acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(upstream, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (arena->upstream->releases != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->upstream->releases != 1, actual == %d";
|
||||
fprintf(stderr, format, arena->upstream->releases);
|
||||
return 1;
|
||||
}
|
||||
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.
|
||||
if (arena->reserved != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, arena->reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (arena->allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->allocated != 0, actual == %d";
|
||||
fprintf(stderr, format, arena->allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (arena->acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(arena, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (arena->releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[arena_allocator]: arena->releases != 2, actual == %d";
|
||||
fprintf(stderr, format, arena->releases);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
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(&upstream, page_bytes);
|
||||
allocator_t arena = arena_allocator();
|
||||
assert_equals(test_arena_initialize(&arena, &upstream) == ALLOCATION_ERROR_SUCCESS);
|
||||
|
||||
allocator_acquire_res_t result =
|
||||
allocation_result_t result =
|
||||
test_arena_acquires(&arena);
|
||||
|
||||
return !(
|
||||
!result.threw &&
|
||||
test_arena_reacquire(result.address, &arena) &&
|
||||
test_arena_release(result.address, &arena) &&
|
||||
test_arena_reacquire(&arena, result.address) &&
|
||||
test_arena_release(&arena, result.address) &&
|
||||
test_arena_reset(&arena) &&
|
||||
test_arena_free(&arena)
|
||||
test_arena_finalize(&arena)
|
||||
);
|
||||
}
|
||||
|
||||
+54
-93
@@ -1,49 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.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)
|
||||
|
||||
|
||||
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;
|
||||
|
||||
allocator_acquire_res_t result;
|
||||
allocator_t allocator = libc_allocator();
|
||||
allocation_result_t result;
|
||||
|
||||
result = allocator_acquire((allocator_acquire_req_t) {
|
||||
.allocator = &allocator,
|
||||
.size = init_bytes
|
||||
});
|
||||
allocator_t libc_alloc = libc_allocator();
|
||||
allocator_mptr_t allocator = &libc_alloc;
|
||||
result = allocator_acquire(allocator, init_bytes);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
if (allocator.reserved != init_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, init_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
if (allocator.allocated != init_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, init_bytes, allocator.allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
if (allocator.acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
if (allocator.releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
@@ -52,83 +46,50 @@ int test_libc_allocator() {
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
|
||||
result = allocator_reacquire((allocator_reacquire_req_t) {
|
||||
.allocator = &allocator,
|
||||
.prev = result.address,
|
||||
.oldsz = init_bytes,
|
||||
.newsz = final_bytes
|
||||
});
|
||||
result = allocator_reacquire(
|
||||
allocator,
|
||||
result.address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
if (allocator.reserved != final_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, final_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
if (allocator.allocated != final_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.allocator != %d, actual == %d";
|
||||
fprintf(stderr, format, final_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
|
||||
allocator_exception_t relexc =
|
||||
allocator_release((allocator_release_req_t) {
|
||||
.allocator = &allocator,
|
||||
.address = result.address,
|
||||
.size = final_bytes
|
||||
});
|
||||
allocation_error_t relexc = allocator_release(
|
||||
allocator,
|
||||
result.address,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (allocator.reserved != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.reserved != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.allocator != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
return relexc == ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
return relexc == ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
+54
-93
@@ -1,49 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.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)
|
||||
|
||||
|
||||
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;
|
||||
|
||||
allocator_acquire_res_t result;
|
||||
allocator_t allocator = platform_allocator();
|
||||
allocation_result_t result;
|
||||
|
||||
result = allocator_acquire((allocator_acquire_req_t) {
|
||||
.allocator = &allocator,
|
||||
.size = init_bytes
|
||||
});
|
||||
allocator_t plat_alloc = platform_allocator();
|
||||
allocator_mptr_t allocator = &plat_alloc;
|
||||
result = allocator_acquire(allocator, init_bytes);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
if (allocator.reserved != init_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, init_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
if (allocator.allocated != init_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.allocated != %d, actual == %d";
|
||||
fprintf(stderr, format, init_bytes, allocator.allocated);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
if (allocator.acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
if (allocator.releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.releases != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
@@ -52,83 +46,50 @@ int test_platform_allocator() {
|
||||
for (index = 0; index < 4; index++)
|
||||
vector[index] = index + 1;
|
||||
|
||||
result = allocator_reacquire((allocator_reacquire_req_t) {
|
||||
.allocator = &allocator,
|
||||
.prev = result.address,
|
||||
.oldsz = init_bytes,
|
||||
.newsz = final_bytes
|
||||
});
|
||||
result = allocator_reacquire(
|
||||
allocator,
|
||||
result.address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
if (allocator.reserved != final_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.reserved != %d, actual == %d";
|
||||
fprintf(stderr, format, final_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
if (allocator.allocated != final_bytes) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.allocator != %d, actual == %d";
|
||||
fprintf(stderr, format, final_bytes, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.releases != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
|
||||
allocator_exception_t relexc =
|
||||
allocator_release((allocator_release_req_t) {
|
||||
.allocator = &allocator,
|
||||
.address = result.address,
|
||||
.size = final_bytes
|
||||
});
|
||||
allocation_error_t relexc = allocator_release(
|
||||
allocator,
|
||||
result.address,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (allocator.reserved != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.reserved != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.allocator != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[platform_allocator]: allocator.releases != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
return relexc == ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
return relexc == ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user