Initial commit
This commit is contained in:
@@ -0,0 +1,359 @@
|
||||
#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 */
|
||||
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
return request.allocator->vtable.acquire != NULL_ADDRESS
|
||||
? request.allocator->vtable.acquire(request)
|
||||
: (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = NULL_ADDRESS
|
||||
};
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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
|
||||
return linux_allocator();
|
||||
#elif MALUNAL_PLATFORM_WIN32
|
||||
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 */
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
#include <memory.h>
|
||||
#include "malunal/allocators/arena.h"
|
||||
#include "malunal/assert.h"
|
||||
|
||||
|
||||
allocator_acquire_res_t
|
||||
create_page(allocator_acquire_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS) {
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_NULL_UPSTREAM
|
||||
};
|
||||
}
|
||||
|
||||
allocator_acquire_res_t result =
|
||||
allocator_acquire(request);
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
arena_page_mptr_t page = result.address;
|
||||
page->size = request.size;
|
||||
page->used = sizeof(arena_page_t);
|
||||
page->next = NULL_ADDRESS;
|
||||
return result;
|
||||
}
|
||||
|
||||
allocator_exception_t
|
||||
delete_page(
|
||||
allocator_mptr_t allocator,
|
||||
arena_page_mptr_t page
|
||||
) {
|
||||
if (allocator == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_UPSTREAM;
|
||||
|
||||
if (page == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_FAILURE;
|
||||
|
||||
delete_page(allocator, page->next);
|
||||
return allocator_release((allocator_release_req_t) {
|
||||
.allocator = allocator,
|
||||
.address = page,
|
||||
.size = page->size
|
||||
});
|
||||
}
|
||||
|
||||
allocator_acquire_res_t
|
||||
page_acquire(allocator_acquire_req_t request) {
|
||||
arena_page_mptr_t page = request.allocator->context;
|
||||
if (page == NULL_ADDRESS) {
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_NULL_CONTEXT
|
||||
};
|
||||
}
|
||||
|
||||
malunal_uint32_t offset = page->used - sizeof(arena_page_t);
|
||||
malunal_mptr_t result = page->data + offset;
|
||||
page->used += request.size;
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 0,
|
||||
.address = result
|
||||
};
|
||||
}
|
||||
|
||||
allocator_t
|
||||
arena_allocator(
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t size
|
||||
) {
|
||||
allocator_acquire_req_t request =
|
||||
(allocator_acquire_req_t) {
|
||||
.allocator = upstream,
|
||||
.size = platform_page_size()
|
||||
};
|
||||
|
||||
allocator_acquire_res_t result = create_page(request);
|
||||
arena_page_mptr_t currpage =
|
||||
!result.threw ? result.address : NULL_ADDRESS;
|
||||
|
||||
malunal_size_t reserved = 0;
|
||||
if (currpage != NULL_ADDRESS) {
|
||||
reserved += currpage->size;
|
||||
while (reserved > size) {
|
||||
result = create_page(request);
|
||||
if (result.threw)
|
||||
break;
|
||||
currpage = currpage->next = result.address;
|
||||
reserved += currpage->size;
|
||||
}
|
||||
}
|
||||
|
||||
return (allocator_t) {
|
||||
.vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&arena_acquire,
|
||||
.reacquire = (allocator_reacquire_pfn_t)&arena_reacquire,
|
||||
.release = (allocator_release_pfn_t)&arena_release
|
||||
},
|
||||
.upstream = upstream,
|
||||
.context = currpage
|
||||
};
|
||||
}
|
||||
|
||||
allocator_acquire_res_t
|
||||
arena_acquire(allocator_acquire_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS) {
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
if (request.size > platform_page_size()) {
|
||||
return (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
|
||||
};
|
||||
}
|
||||
|
||||
arena_page_mptr_t page = request.allocator->context;
|
||||
while (page != NULL_ADDRESS) {
|
||||
malunal_uint32_t remaining = page->size - page->used;
|
||||
if (request.size <= remaining)
|
||||
return page_acquire(request);
|
||||
page = page->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
allocator_acquire_res_t result =
|
||||
create_page((allocator_acquire_req_t) {
|
||||
.allocator = request.allocator->upstream,
|
||||
.size = platform_page_size()
|
||||
});
|
||||
|
||||
page = page->next = !result.threw
|
||||
? result.address
|
||||
: NULL_ADDRESS;
|
||||
|
||||
return page != NULL_ADDRESS
|
||||
? page_acquire(request)
|
||||
: (allocator_acquire_res_t) {
|
||||
.threw = 1,
|
||||
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
|
||||
};
|
||||
}
|
||||
|
||||
allocator_acquire_res_t
|
||||
arena_reacquire(allocator_reacquire_req_t request) {
|
||||
return arena_acquire((allocator_acquire_req_t) {
|
||||
.allocator = request.allocator,
|
||||
.size = request.newsz
|
||||
});
|
||||
}
|
||||
|
||||
allocator_exception_t
|
||||
arena_release(allocator_release_req_t request) {
|
||||
if (request.allocator == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
|
||||
|
||||
if (request.allocator->context == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_CONTEXT;
|
||||
}
|
||||
|
||||
allocator_exception_t
|
||||
arena_reset(allocator_mptr_t arena) {
|
||||
if (arena == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
|
||||
|
||||
arena_page_mptr_t page = arena->context;
|
||||
while (page != NULL_ADDRESS) {
|
||||
page->used = 0;
|
||||
page = page->next;
|
||||
}
|
||||
}
|
||||
|
||||
allocator_exception_t
|
||||
arena_free(allocator_mptr_t arena) {
|
||||
if (arena == NULL_ADDRESS)
|
||||
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
|
||||
delete_page(arena->upstream, arena->context);
|
||||
}
|
||||
Reference in New Issue
Block a user