Reworked, no version bump
This commit is contained in:
+116
-325
@@ -1,56 +1,130 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "malunal/allocators.h"
|
||||
#include "malunal/assert.h"
|
||||
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
# include <sys/mman.h>
|
||||
# include <unistd.h>
|
||||
#elif MALUNAL_PLATFORM_WIN32
|
||||
# include <memoryapi.h>
|
||||
#endif /* Platform headers */
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
allocator_acquire_res_t
|
||||
allocator_acquire(allocator_acquire_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS)
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
|
||||
};
|
||||
allocation_error_t
|
||||
allocator_upstream(
|
||||
allocator_mptr_t allocator,
|
||||
allocator_mptr_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->upstream;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
return request.allocator->vtable.acquire != NULL_ADDRESS
|
||||
? request.allocator->vtable.acquire(request)
|
||||
: (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = NULL_ADDRESS
|
||||
allocation_error_t
|
||||
allocator_allocated_bytes(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->allocated;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_reserved_bytes(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->reserved;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_acquires_count(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->acquires;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_releases_count(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->releases;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_initialize(
|
||||
allocator_mptr_t allocator,
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t capacity
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->initialize(allocator, upstream, capacity)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_finalize(
|
||||
allocator_mptr_t allocator
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->finalize(allocator)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
}
|
||||
|
||||
allocation_result_t
|
||||
allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->acquire(allocator, size)
|
||||
: (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
allocator_acquire_res_t
|
||||
allocator_reacquire(allocator_reacquire_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS)
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
return request.allocator->vtable.reacquire != NULL_ADDRESS
|
||||
? request.allocator->vtable.reacquire(request)
|
||||
: (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = NULL_ADDRESS
|
||||
allocation_result_t
|
||||
allocator_reacquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t oldsize,
|
||||
malunal_size_t newsize
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->reacquire(allocator, address, oldsize, newsize)
|
||||
: (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
allocator_exception_t
|
||||
allocator_release(allocator_release_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
|
||||
|
||||
return request.allocator->vtable.release != NULL_ADDRESS
|
||||
? request.allocator->vtable.release(request)
|
||||
: ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
allocation_error_t
|
||||
allocator_release(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->release(allocator, address, size)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
}
|
||||
|
||||
malunal_size_t
|
||||
@@ -66,111 +140,6 @@ align_to_page(malunal_size_t size) {
|
||||
return align_to(size, platform_page_size());
|
||||
}
|
||||
|
||||
static
|
||||
malunal_size_t
|
||||
g_platform_page_size = 0;
|
||||
|
||||
malunal_size_t
|
||||
platform_page_size() {
|
||||
if (g_platform_page_size != 0)
|
||||
return g_platform_page_size;
|
||||
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
g_platform_page_size = sysconf(_SC_PAGESIZE);
|
||||
#elif MALUNAL_PLATFORM_WIN32
|
||||
SYSTEM_INFO si;
|
||||
GetSystemInfo(&si);
|
||||
g_platform_page_size = si.dwPageSize;
|
||||
#endif
|
||||
|
||||
return g_platform_page_size;
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
libc_acquire(allocator_acquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
libc_reacquire(allocator_reacquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
libc_release(allocator_release_req_t request);
|
||||
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
libc_acquire(allocator_acquire_req_t request) {
|
||||
allocator_acquire_res_t result;
|
||||
|
||||
malunal_mptr_t addr = malloc(request.size);
|
||||
if (addr == NULL_ADDRESS) {
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
|
||||
};
|
||||
}
|
||||
|
||||
request.allocator->allocated += request.size;
|
||||
request.allocator->reserved += request.size;
|
||||
request.allocator->acquires += 1;
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = addr
|
||||
};
|
||||
}
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
libc_reacquire(allocator_reacquire_req_t request) {
|
||||
allocator_acquire_res_t result =
|
||||
libc_acquire((allocator_acquire_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.size = request.newsz
|
||||
});
|
||||
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
result.address = memcpy(
|
||||
result.address,
|
||||
request.prev,
|
||||
request.oldsz
|
||||
);
|
||||
|
||||
libc_release((allocator_release_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.address = request.prev,
|
||||
.size = request.oldsz
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
libc_release(allocator_release_req_t request) {
|
||||
free(request.address);
|
||||
request.allocator->allocated -= request.size;
|
||||
request.allocator->reserved -= request.size;
|
||||
request.allocator->releases += 1;
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
}
|
||||
|
||||
allocator_t
|
||||
libc_allocator() {
|
||||
return (allocator_t) {
|
||||
.vtable = {
|
||||
.acquire = &libc_acquire,
|
||||
.reacquire = &libc_reacquire,
|
||||
.release = &libc_release
|
||||
},
|
||||
.context = NULL_ADDRESS
|
||||
};
|
||||
}
|
||||
|
||||
allocator_t
|
||||
platform_allocator() {
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
@@ -179,181 +148,3 @@ platform_allocator() {
|
||||
return win32_allocator();
|
||||
#endif /* Platform specific allocators */
|
||||
}
|
||||
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
linux_acquire(allocator_acquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
linux_reacquire(allocator_reacquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
linux_release(allocator_release_req_t request);
|
||||
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
linux_acquire(allocator_acquire_req_t request) {
|
||||
const malunal_int32_t memops = PROT_READ | PROT_WRITE;
|
||||
const malunal_int32_t memprms = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||
|
||||
allocator_acquire_res_t result;
|
||||
|
||||
// TODO:
|
||||
malunal_size_t size = request.size;
|
||||
malunal_mptr_t ptr = mmap(0, size, memops, memprms, -1, 0);
|
||||
if (ptr == MAP_FAILED || ptr == NULL_ADDRESS) {
|
||||
result = (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
|
||||
};
|
||||
} else {
|
||||
result = (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = ptr
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
linux_reacquire(allocator_reacquire_req_t request) {
|
||||
allocator_acquire_res_t result =
|
||||
linux_acquire((allocator_acquire_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.size = request.newsz
|
||||
});
|
||||
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
result.address = memcpy(
|
||||
result.address,
|
||||
request.prev,
|
||||
request.oldsz
|
||||
);
|
||||
|
||||
linux_release((allocator_release_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.address = request.prev,
|
||||
.size = request.oldsz
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
linux_release(allocator_release_req_t request) {
|
||||
munmap(request.address, request.size);
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
}
|
||||
|
||||
allocator_t
|
||||
linux_allocator() {
|
||||
return (allocator_t) {
|
||||
.vtable = {
|
||||
.acquire = &linux_acquire,
|
||||
.reacquire = &linux_reacquire,
|
||||
.release = &linux_release
|
||||
},
|
||||
.context = NULL_ADDRESS
|
||||
};
|
||||
}
|
||||
#elif MALUNAL_PLATFORM_WIN32
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
win32_acquire(allocator_acquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
win32_reacquire(allocator_reacquire_req_t request);
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
win32_release(allocator_release_req_t request);
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
win32_acquire(allocator_acquire_req_t request) {
|
||||
const malunal_int32_t memops = MEM_COMMIT | MEM_RESERVE;
|
||||
const malunal_int32_t pageops = PAGE_READWRITE;
|
||||
|
||||
malunal_mptr_t addr = VirtualAlloc(
|
||||
NULL_ADDRESS,
|
||||
request.size,
|
||||
memops,
|
||||
pageops
|
||||
);
|
||||
|
||||
if (addr == NULL_ADDRESS) {
|
||||
result = (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
|
||||
};
|
||||
} else {
|
||||
result = (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = addr
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
allocator_acquire_res_t
|
||||
win32_reacquire(allocator_reacquire_req_t request) {
|
||||
allocator_acquire_res_t result =
|
||||
win32_acquire((allocator_acquire_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.size = request.newsz
|
||||
});
|
||||
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
result.address = memcpy(
|
||||
result.address,
|
||||
request.prev,
|
||||
request.oldsz
|
||||
);
|
||||
|
||||
win32_release((allocator_release_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.address = request.prev,
|
||||
.size = request.oldsz
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
allocator_exception_t
|
||||
win32_release(allocator_release_req_t request) {
|
||||
VirtualFree(
|
||||
request.address,
|
||||
request.size,
|
||||
MEM_RELEASE
|
||||
);
|
||||
|
||||
return ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
}
|
||||
|
||||
allocator_t
|
||||
win32_allocator() {
|
||||
return (allocator_t) {
|
||||
.vtable = {
|
||||
.acquire = &win32_acquire,
|
||||
.reacquire = &win32_reacquire,
|
||||
.release = &win32_release
|
||||
},
|
||||
.context = NULL_ADDRESS
|
||||
};
|
||||
}
|
||||
#endif /* Platform specific allocators */
|
||||
|
||||
Reference in New Issue
Block a user