92 lines
1.9 KiB
C
92 lines
1.9 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "malunal/allocator.h"
|
|
|
|
|
|
const uuid_t UUID_ALLOCATOR_T = {
|
|
.tl = 0xEB14C3C3,
|
|
.tm = 0x5F0E,
|
|
.thv = 0x48A4,
|
|
.csr = 0x8C,
|
|
.csl = 0xFD,
|
|
.nbs = {
|
|
0x58, 0x04, 0x13,
|
|
0x19, 0xF9, 0xB2
|
|
}
|
|
};
|
|
|
|
|
|
static
|
|
malunal_cstr_t
|
|
describe(malunal_int32_t code) {
|
|
switch (code) {
|
|
case ALLOCATOR_ERROR_FAILURE:
|
|
return "Generic allocator error";
|
|
case ALLOCATOR_ERROR_NULL_ALLOCATOR:
|
|
return "Allocator provided was null";
|
|
case ALLOCATOR_ERROR_OUT_OF_MEMORY:
|
|
return "Allocator out of memory";
|
|
case ALLOCATOR_ERROR_NOT_MY_ADDRESS:
|
|
return "Address does not belong to allocator";
|
|
case ALLOCATOR_ERROR_LEAKY_MEMORY:
|
|
return "Allocator is leaking memory";
|
|
}
|
|
return "Unknown allocator error";
|
|
}
|
|
|
|
const error_domain_t ERROR_DOMAIN_ALLOCATOR_T = {
|
|
.describe = &describe,
|
|
.name = "malunal.allocator.error"
|
|
};
|
|
|
|
|
|
error_t
|
|
allocator_acquire(
|
|
allocator_mptr_t allocator,
|
|
malunal_size_t size,
|
|
malunal_mptr_t* out
|
|
) {
|
|
return allocator != null
|
|
? allocator->vtable->acquire(allocator, size, out)
|
|
: (error_t) {
|
|
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
|
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
|
};
|
|
}
|
|
|
|
error_t
|
|
allocator_dispose(
|
|
allocator_mptr_t allocator,
|
|
malunal_mptr_t address,
|
|
malunal_size_t size
|
|
) {
|
|
return allocator != null
|
|
? allocator->vtable->dispose(allocator, address, size)
|
|
: (error_t) {
|
|
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
|
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
|
};
|
|
}
|
|
|
|
malunal_size_t
|
|
align_to(
|
|
malunal_size_t size,
|
|
malunal_size_t alignment
|
|
) {
|
|
return (size + alignment - 1) & ~(alignment - 1);
|
|
}
|
|
|
|
malunal_size_t
|
|
align_to_page(malunal_size_t size) {
|
|
return align_to(size, platform_page_size());
|
|
}
|
|
|
|
allocator_mptr_t
|
|
platform_allocator() {
|
|
#if MALUNAL_PLATFORM_LINUX
|
|
return linux_allocator();
|
|
#elif MALUNAL_PLATFORM_WIN32
|
|
return win32_allocator();
|
|
#endif /* Platform specific allocators */
|
|
}
|