1
0
mirror of https://github.com/ErnyTech/neomimalloc synced 2024-06-08 22:28:00 +02:00

Improve the low-level bindings

Improves the wrapper by adding attributes to each method (@nogc, as well etc.) and providing a minimal description for the undocumented methods of mimalloc upstream
This commit is contained in:
Ernesto Castellotti 2019-07-03 23:36:55 +02:00
parent f171ceba2e
commit 394451947e

View File

@ -93,7 +93,7 @@ extern(C) {
* Returns:
* pointer to the allocated memory or NULL if out of memory. Returns a unique pointer if called with size 0.
*/
void* mi_malloc(size_t size);
@nogc pure @system nothrow void* mi_malloc(size_t size);
/**
* Allocate count elements of size bytes.
@ -107,7 +107,7 @@ extern(C) {
*
* If there is no overflow, it behaves exactly like mi_malloc(p,count*size).
*/
void* mi_mallocn(size_t count, size_t size);
@nogc pure @system nothrow void* mi_mallocn(size_t count, size_t size);
/**
* Allocate zero-initialized count elements of size bytes.
@ -121,7 +121,7 @@ extern(C) {
*
* Returns a unique pointer if called with either size or count of 0.
*/
void* mi_calloc(size_t count, size_t size);
@nogc pure @system nothrow void* mi_calloc(size_t count, size_t size);
/**
* Re-allocate memory to newsize bytes.
@ -133,7 +133,7 @@ extern(C) {
* Returns:
* pointer to the re-allocated memory of newsize bytes, or NULL if out of memory. If NULL is returned, the pointer p is not freed. Otherwise the original pointer is either freed or returned as the reallocated result (in case it fits in-place with the new size). If the pointer p is NULL, it behaves as mi_malloc(newsize). If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.
*/
void* mi_realloc(void* p, size_t newsize);
@nogc pure @system nothrow void* mi_realloc(void* p, size_t newsize);
/**
* Re-allocate memory to newsize bytes.
@ -147,7 +147,7 @@ extern(C) {
*
* In contrast to mi_realloc(), if NULL is returned, the original pointer p is freed (if it was not NULL itself). Otherwise the original pointer is either freed or returned as the reallocated result (in case it fits in-place with the new size). If the pointer p is NULL, it behaves as mi_malloc(newsize). If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.
*/
void* mi_reallocf(void* p, size_t newsize);
@nogc pure @system nothrow void* mi_reallocf(void* p, size_t newsize);
/**
* Re-allocate memory to count elements of size bytes.
@ -162,7 +162,7 @@ extern(C) {
*
* If there is no overflow, it behaves exactly like mi_realloc(p,count*size).
*/
void* mi_reallocn(void* p, size_t count, size_t size);
@nogc pure @system nothrow void* mi_reallocn(void* p, size_t count, size_t size);
/**
* Allocate zero-initialized size bytes.
@ -173,7 +173,7 @@ extern(C) {
* Returns:
* Pointer to newly allocated zero initialized memory, or NULL if out of memory.
*/
void* mi_zalloc(size_t size);
@nogc pure @system nothrow void* mi_zalloc(size_t size);
/**
* Reallocate memory to newsize bytes, with extra memory initialized to zero.
@ -187,7 +187,7 @@ extern(C) {
*
* If the newsize is larger than the original allocated size of p, the extra bytes are initialized to zero.
*/
void* mi_rezalloc(void* p, size_t newsize);
@nogc pure @system nothrow void* mi_rezalloc(void* p, size_t newsize);
/**
* Re-allocate memory to newsize bytes.
@ -199,7 +199,7 @@ extern(C) {
* Returns:
* pointer to the re-allocated memory of newsize bytes, or NULL if out of memory. If NULL is returned, the pointer p is not freed. Otherwise the original pointer is either freed or returned as the reallocated result (in case it fits in-place with the new size). If the pointer p is NULL, it behaves as mi_malloc(newsize). If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.
*/
void* mi_recalloc(void* p, size_t count, size_t size);
@nogc pure @system nothrow void* mi_recalloc(void* p, size_t count, size_t size);
/**
* Try to re-allocate memory to newsize bytes in place.
@ -211,7 +211,7 @@ extern(C) {
* Returns:
* pointer to the re-allocated memory of newsize bytes (always equal to p), or NULL if either out of memory or if the memory could not be expanded in place. If NULL is returned, the pointer p is not freed. Otherwise the original pointer is returned as the reallocated result since it fits in-place with the new size. If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.
*/
void* mi_expand(void* p, size_t newsize);
@nogc pure @system nothrow void* mi_expand(void* p, size_t newsize);
/**
* Free previously allocated memory.
@ -221,7 +221,7 @@ extern(C) {
* Params:
* p = pointer to free, or NULL.
*/
void mi_free(void* p);
@nogc pure @system nothrow void mi_free(void* p);
/**
* Allocate and duplicate a string.
@ -234,7 +234,7 @@ extern(C) {
*
* Replacement for the standard strdup() such that mi_free() can be used on the returned result.
*/
char* mi_strdup(const(char)* s);
@nogc pure @system nothrow char* mi_strdup(const(char)* s);
/**
* Allocate and duplicate a string up to n bytes.
@ -248,7 +248,7 @@ extern(C) {
*
* Replacement for the standard strndup() such that mi_free() can be used on the returned result.
*/
char* mi_strndup(const(char)* s, size_t n);
@nogc pure @system nothrow char* mi_strndup(const(char)* s, size_t n);
/**
* Resolve a file path name.
@ -264,7 +264,7 @@ extern(C) {
*
* Replacement for the standard realpath() such that mi_free() can be used on the returned result (if resolved_name was NULL).
*/
char* mi_realpath(const(char)* fname, char* resolved_name);
@nogc pure @system nothrow char* mi_realpath(const(char)* fname, char* resolved_name);
/**
* Allocate a small object.
@ -275,7 +275,7 @@ extern(C) {
* Returns:
* a pointer to newly allocated memory of at least size bytes, or NULL if out of memory. This function is meant for use in run-time systems for best performance and does not check if size was indeed small use with care!
*/
void* mi_malloc_small(size_t size);
@nogc pure @system nothrow void* mi_malloc_small(size_t size);
/**
* Allocate a zero initialized small object.
@ -286,7 +286,7 @@ extern(C) {
* Returns:
* a pointer to newly allocated zero-initialized memory of at least size bytes, or NULL if out of memory. This function is meant for use in run-time systems for best performance and does not check if size was indeed small use with care!
*/
void* mi_zalloc_small (size_t size);
@nogc pure @system nothrow void* mi_zalloc_small (size_t size);
/**
* Return the available bytes in a memory block.
@ -299,7 +299,7 @@ extern(C) {
*
* The returned size can be used to call mi_expand successfully. The returned size is always at least equal to the allocated size of p, and, in the current design, should be less than 16.7% more.
*/
size_t mi_usable_size(void* p);
@nogc pure @system nothrow size_t mi_usable_size(void* p);
/**
* Return the used allocation size.
@ -312,7 +312,7 @@ extern(C) {
*
* Generally, mi_usable_size(mi_malloc(size)) == mi_good_size(size). This can be used to reduce internal wasted space when allocating buffers for example.
*/
size_t mi_good_size(size_t size);
@nogc pure @system nothrow size_t mi_good_size(size_t size);
/**
* Eagerly free memory.
@ -322,7 +322,7 @@ extern(C) {
*
* Regular code should not have to call this function. It can be beneficial in very narrow circumstances; in particular, when a long running thread allocates a lot of blocks that are freed by other threads it may improve resource usage by calling this every once in a while.
*/
void mi_collect(bool force);
@nogc pure @system nothrow void mi_collect(bool force);
/**
* Print statistics.
@ -332,28 +332,31 @@ extern(C) {
*
* Most detailed when using a debug build.
*/
void mi_stats_print (FILE* out_);
@nogc pure @system nothrow void mi_stats_print(FILE* out_);
/**
* Reset statistics.
*/
void mi_stats_reset();
@nogc @system nothrow void mi_stats_reset();
void mi_process_init();
/**
* Initialize mimalloc on a process
*/
@nogc @system nothrow void mi_process_init();
/**
* Initialize mimalloc on a thread.
*
* Should not be used as on most systems (pthreads, windows) this is done automatically.
*/
void mi_thread_init();
@nogc @system nothrow void mi_thread_init();
/**
* Uninitialize mimalloc on a thread.
*
* Should not be used as on most systems (pthreads, windows) this is done automatically. Ensures that any memory that is not freed yet (but will be freed by other threads in the future) is properly handled.
*/
void mi_thread_done();
@nogc @system nothrow void mi_thread_done();
/**
* Print out heap statistics for this thread.
@ -363,7 +366,7 @@ extern(C) {
*
* Most detailed when using a debug build.
*/
void mi_thread_stats_print(FILE* out_);
@nogc pure @system nothrow void mi_thread_stats_print(FILE* out_);
/**
* Register a deferred free function.
@ -373,7 +376,7 @@ extern(C) {
*
* Some runtime systems use deferred free-ing, for example when using reference counting to limit the worst case free time. Such systems can register (re-entrant) deferred free function to free more memory on demand. When the force parameter is true all possible memory should be freed. The per-thread heartbeat parameter is monotonically increasing and guaranteed to be deterministic if the program allocates deterministically. The deferred_free function is guaranteed to be called deterministically after some number of allocations (regardless of freeing or available free memory). At most one deferred_free function can be active.
*/
void mi_register_deferred_free(mi_deferred_free_fun deferred_free);
@nogc @system nothrow void mi_register_deferred_free(mi_deferred_free_fun deferred_free);
/**
* Allocate size bytes aligned by alignment.
@ -387,7 +390,7 @@ extern(C) {
*
* Returns a unique pointer if called with size 0.
*/
void* mi_malloc_aligned(size_t size, size_t alignment);
@nogc pure @system nothrow void* mi_malloc_aligned(size_t size, size_t alignment);
/**
* Allocate size bytes aligned by alignment at a specified offset.
@ -402,32 +405,62 @@ extern(C) {
*
* Returns a unique pointer if called with size 0.
*/
void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset);
@nogc pure @system nothrow void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset);
void* mi_zalloc_aligned(size_t size, size_t alignment);
/**
* Allocate zero-initialized size bytes aligned by alignment.
*/
@nogc pure @system nothrow void* mi_zalloc_aligned(size_t size, size_t alignment);
void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset);
/**
* Allocate zero-initialized size bytes aligned by alignment at a specified offset.
*/
@nogc pure @system nothrow void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset);
void* mi_calloc_aligned(size_t count, size_t size, size_t alignment);
/**
* Allocate zero-initialized count elements of size bytes aligned by alignment.
*/
@nogc pure @system nothrow void* mi_calloc_aligned(size_t count, size_t size, size_t alignment);
/**
* Allocate zero-initialized count elements of size bytes aligned by alignment at a specified offset.
*/
void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset);
void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment);
/**
* Re-allocate memory to newsize bytes aligned by alignment.
*/
@nogc pure @system nothrow void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment);
void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
/**
* Re-allocate memory to newsize bytes aligned by alignment at a specified offset.
*/
@nogc pure @system nothrow void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
void* mi_rezalloc_aligned (void* p, size_t newsize, size_t alignment);
/**
* Reallocate memory to newsize bytes, with extra memory initialized to zero aligned by alignment.
*/
@nogc pure @system nothrow void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment);
void* mi_rezalloc_aligned_at (void* p, size_t newsize, size_t alignment, size_t offset);
/**
* Reallocate memory to newsize bytes, with extra memory initialized to zero aligned by alignment at a specified offset.
*/
@nogc pure @system nothrow void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
void* mi_recalloc_aligned (void* p, size_t count, size_t size, size_t alignment);
/**
* Re-allocate memory to newsize bytes aligned by alignment.
*/
@nogc pure @system nothrow void* mi_recalloc_aligned(void* p, size_t count, size_t size, size_t alignment);
void* mi_recalloc_aligned_at (void* p, size_t count, size_t size, size_t alignment, size_t offset);
/**
* Re-allocate memory to newsize bytes aligned by alignment at a specified offset.
*/
@nogc pure @system nothrow void* mi_recalloc_aligned_at(void* p, size_t count, size_t size, size_t alignment, size_t offset);
/**
* Create a new heap that can be used for allocation.
*/
mi_heap_t* mi_heap_new();
@nogc pure @system nothrow mi_heap_t* mi_heap_new();
/**
* Delete a previously allocated heap.
@ -436,7 +469,7 @@ extern(C) {
*
* If heap is the default heap, the default heap is set to the backing heap.
*/
void mi_heap_delete(mi_heap_t* heap);
@nogc pure @system nothrow void mi_heap_delete(mi_heap_t* heap);
/**
* Destroy a heap, freeing all its still allocated blocks.
@ -445,7 +478,7 @@ extern(C) {
*
* If heap is the default heap, the default heap is set to the backing heap.
*/
void mi_heap_destroy (mi_heap_t* heap);
@nogc pure @system nothrow void mi_heap_destroy(mi_heap_t* heap);
/**
* Set the default heap to use for mi_malloc() et al.
@ -456,7 +489,7 @@ extern(C) {
* Returns:
* The previous default heap.
*/
mi_heap_t* mi_heap_set_default(mi_heap_t* heap);
@nogc pure @system nothrow mi_heap_t* mi_heap_set_default(mi_heap_t* heap);
/**
* Get the default heap that is used for mi_malloc() et al.
@ -464,53 +497,59 @@ extern(C) {
* Returns:
* The current default heap.
*/
mi_heap_t* mi_heap_get_default();
@nogc pure @system nothrow mi_heap_t* mi_heap_get_default();
/**
* Get the backing heap.
*
* The backing heap is the initial default heap for a thread and always available for allocations. It cannot be destroyed or deleted except by exiting the thread.
*/
mi_heap_t* mi_heap_get_backing();
@nogc pure @system nothrow mi_heap_t* mi_heap_get_backing();
void mi_heap_collect(mi_heap_t* heap, bool force);
/**
* Eagerly free memory in specific heap.
*/
@nogc pure @system nothrow void mi_heap_collect(mi_heap_t* heap, bool force);
/**
* Allocate in a specific heap.
*/
void* mi_heap_malloc(mi_heap_t* heap, size_t size);
@nogc pure @system nothrow void* mi_heap_malloc(mi_heap_t* heap, size_t size);
/**
* Allocate count elements in a specific heap.
*/
void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size);
@nogc pure @system nothrow void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size);
/**
* Allocate zero-initialized in a specific heap.
*/
void* mi_heap_zalloc(mi_heap_t* heap, size_t size);
@nogc pure @system nothrow void* mi_heap_zalloc(mi_heap_t* heap, size_t size);
/**
* Allocate count zero-initialized elements in a specific heap.
*/
void* mi_heap_calloc(mi_heap_t* heap, size_t count, size_t size);
@nogc pure @system nothrow void* mi_heap_calloc(mi_heap_t* heap, size_t count, size_t size);
void* mi_heap_malloc_small(mi_heap_t* heap, size_t size);
/**
* Allocate a small object in a specific heap.
*/
@nogc pure @system nothrow void* mi_heap_malloc_small(mi_heap_t* heap, size_t size);
/**
* Duplicate a string in a specific heap.
*/
char* mi_heap_strdup(mi_heap_t* heap, const(char)* s);
@nogc pure @system nothrow char* mi_heap_strdup(mi_heap_t* heap, const(char)* s);
/**
* Duplicate a string of at most length n in a specific heap.
*/
char* mi_heap_strndup(mi_heap_t* heap, const(char)* s, size_t n);
@nogc pure @system nothrow char* mi_heap_strndup(mi_heap_t* heap, const(char)* s, size_t n);
/**
* Resolve a file path name using a specific heap to allocate the result.
*/
char* mi_heap_realpath(mi_heap_t* heap, const(char)* fname, char* resolved_name);
@nogc pure @system nothrow char* mi_heap_realpath(mi_heap_t* heap, const(char)* fname, char* resolved_name);
/**
* Does a heap contain a pointer to a previously allocated block?
@ -522,7 +561,7 @@ extern(C) {
* Returns:
* true if the block pointed to by p is in the heap.
*/
bool mi_heap_contains_block(mi_heap_t* heap, const(void)* p);
@nogc pure @system nothrow bool mi_heap_contains_block(mi_heap_t* heap, const(void)* p);
/**
* Check safely if any pointer is part of a heap.
@ -536,7 +575,7 @@ extern(C) {
*
* Note: expensive function, linear in the pages in the heap.
*/
bool mi_heap_check_owned(mi_heap_t* heap, const(void)* p);
@nogc pure @system nothrow bool mi_heap_check_owned(mi_heap_t* heap, const(void)* p);
/**
* Check safely if any pointer is part of the default heap of this thread.
@ -549,7 +588,7 @@ extern(C) {
*
* Note: expensive function, linear in the pages in the heap.
*/
bool mi_check_owned(const(void)* p);
@nogc pure @system nothrow bool mi_check_owned(const(void)* p);
/**
* Visit all areas and blocks in a heap.
@ -563,17 +602,35 @@ extern(C) {
* Returns:
* true if all areas and blocks were visited.
*/
bool mi_heap_visit_blocks(const(mi_heap_t)* heap, bool visit_all_blocks, mi_block_visit_fun visitor, void* arg);
@nogc pure @system nothrow bool mi_heap_visit_blocks(const(mi_heap_t)* heap, bool visit_all_blocks, mi_block_visit_fun visitor, void* arg);
bool mi_option_is_enabled(mi_option_t option);
/**
* Set runtime behavior.
*/
@nogc pure @system nothrow bool mi_option_is_enabled(mi_option_t option);
void mi_option_enable(mi_option_t option, bool enable);
/**
* Set runtime behavior.
*/
@nogc @system nothrow void mi_option_enable(mi_option_t option, bool enable);
void mi_option_enable_default(mi_option_t option, bool enable);
/**
* Set runtime behavior.
*/
@nogc @system nothrow void mi_option_enable_default(mi_option_t option, bool enable);
c_long mi_option_get(mi_option_t option);
/**
* Set runtime behavior.
*/
@nogc pure @system nothrow c_long mi_option_get(mi_option_t option);
void mi_option_set(mi_option_t option, c_long value);
/**
* Set runtime behavior.
*/
@nogc @system nothrow void mi_option_set(mi_option_t option, c_long value);
void mi_option_set_default(mi_option_t option, c_long value);
/**
* Set runtime behavior.
*/
@nogc @system nothrow void mi_option_set_default(mi_option_t option, c_long value);
}