mi-malloc  1.0
mimalloc-doc.h
1 /* ----------------------------------------------------------------------------
2 Copyright (c) 2018, Microsoft Research, Daan Leijen
3 This is free software; you can redistribute it and/or modify it under the
4 terms of the MIT license. A copy of the license can be found in the file
5 "license.txt" at the root of this distribution.
6 -----------------------------------------------------------------------------*/
7 
8 #error "documentation file only!"
9 
10 
78 
82 
86 void mi_free(void* p);
87 
92 void* mi_malloc(size_t size);
93 
98 void* mi_zalloc(size_t size);
99 
109 void* mi_calloc(size_t count, size_t size);
110 
123 void* mi_realloc(void* p, size_t newsize);
124 
133 void* mi_rezalloc(void* p, size_t newsize);
134 
146 void* mi_recalloc(void* p, size_t count, size_t size);
147 
148 
162 void* mi_expand(void* p, size_t newsize);
163 
173 void* mi_mallocn(size_t count, size_t size);
174 
185 void* mi_reallocn(void* p, size_t count, size_t size);
186 
203 void* mi_reallocf(void* p, size_t newsize);
204 
205 
214 char* mi_strdup(const char* s);
215 
225 char* mi_strndup(const char* s, size_t n);
226 
239 char* mi_realpath(const char* fname, char* resolved_name);
240 
242 
243 // ------------------------------------------------------
244 // Extended functionality
245 // ------------------------------------------------------
246 
250 
253 #define MI_SMALL_SIZE_MAX (128*sizeof(void*))
254 
262 void* mi_malloc_small(size_t size);
263 
271 void* mi_zalloc_small(size_t size);
272 
287 size_t mi_usable_size(void* p);
288 
298 size_t mi_good_size(size_t size);
299 
307 void mi_collect(bool force);
308 
313 void mi_stats_print(FILE* out);
314 
316 void mi_stats_reset();
317 
321 void mi_thread_init();
322 
327 void mi_thread_done();
328 
333 void mi_thread_stats_print(FILE* out);
334 
340 typedef void (mi_deferred_free_fun)(bool force, unsigned long long heartbeat);
341 
357 
359 
360 // ------------------------------------------------------
361 // Aligned allocation
362 // ------------------------------------------------------
363 
369 
382 void* mi_malloc_aligned(size_t size, size_t alignment);
383 void* mi_zalloc_aligned(size_t size, size_t alignment);
384 void* mi_calloc_aligned(size_t count, size_t size, size_t alignment);
385 void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment);
386 void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment);
387 void* mi_recalloc_aligned(void* p, size_t count, size_t size, size_t alignment);
388 
399 void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset);
400 void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset);
401 void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset);
402 void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
403 void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
404 void* mi_recalloc_aligned_at(void* p, size_t count, size_t size, size_t alignment, size_t offset);
405 
407 
413 
418 struct mi_heap_s;
419 
424 typedef struct mi_heap_s mi_heap_t;
425 
428 
436 void mi_heap_delete(mi_heap_t* heap);
437 
445 void mi_heap_destroy(mi_heap_t* heap);
446 
451 
455 
462 
465 void* mi_heap_malloc(mi_heap_t* heap, size_t size);
466 
469 void* mi_heap_zalloc(mi_heap_t* heap, size_t size);
470 
473 void* mi_heap_calloc(mi_heap_t* heap, size_t count, size_t size);
474 
477 void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size);
478 
481 char* mi_heap_strdup(mi_heap_t* heap, const char* s);
482 
485 char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n);
486 
489 char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name);
490 
491 
493 
499 
511 #define mi_malloc_tp(tp) ((tp*)mi_malloc(sizeof(tp)))
512 
514 #define mi_zalloc_tp(tp) ((tp*)mi_zalloc(sizeof(tp)))
515 
517 #define mi_calloc_tp(tp,count) ((tp*)mi_calloc(count,sizeof(tp)))
518 
520 #define mi_mallocn_tp(tp,count) ((tp*)mi_mallocn(count,sizeof(tp)))
521 
523 #define mi_reallocn_tp(p,tp,count) ((tp*)mi_reallocn(p,count,sizeof(tp)))
524 
526 #define mi_recalloc_tp(p,tp,count) ((tp*)mi_recalloc(p,count,sizeof(tp)))
527 
529 #define mi_heap_malloc_tp(hp,tp) ((tp*)mi_malloc(hp,sizeof(tp)))
530 
532 #define mi_heap_zalloc_tp(hp,tp) ((tp*)mi_zalloc(hp,sizeof(tp)))
533 
535 #define mi_heap_calloc_tp(hp,tp,count) ((tp*)mi_calloc(hp,count,sizeof(tp)))
536 
538 #define mi_heap_mallocn_tp(hp,tp,count) ((tp*)mi_mallocn(hp,count,sizeof(tp)))
539 
541 
547 
554 bool mi_heap_contains_block(mi_heap_t* heap, const void* p);
555 
564 bool mi_heap_check_owned(mi_heap_t* heap, const void* p);
565 
573 bool mi_check_owned(const void* p);
574 
577 typedef struct mi_heap_area_s {
578  void* blocks;
579  size_t reserved;
580  size_t committed;
581  size_t used;
582  size_t block_size;
584 
592 typedef bool (mi_block_visit_fun)(const mi_heap_t* heap, const mi_heap_area_t* area, void* block, size_t block_size, void* arg);
593 
605 bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_all_blocks, mi_block_visit_fun* visitor, void* arg);
606 
608 
614 
616 typedef enum mi_option_e {
624 } mi_option_t;
625 
626 bool mi_option_enabled(mi_option_t option);
627 void mi_option_enable(mi_option_t option, bool enable);
628 void mi_option_enable_default(mi_option_t option, bool enable);
629 
630 long mi_option_get(mi_option_t option);
631 void mi_option_set(mi_option_t option, long value);
632 void mi_option_set_default(mi_option_t option, long value);
633 
634 
636 
void mi_stats_print(FILE *out)
Print statistics.
void mi_option_enable_default(mi_option_t option, bool enable)
size_t mi_usable_size(void *p)
Return the available bytes in a memory block.
void * mi_reallocn(void *p, size_t count, size_t size)
Re-allocate memory to count elements of size bytes.
void * mi_malloc_aligned(size_t size, size_t alignment)
Allocate size bytes aligned by alignment.
void * mi_mallocn(size_t count, size_t size)
Allocate count elements of size bytes.
void mi_thread_done()
Uninitialize mimalloc on a thread.
Reset segment memory when a segment is cached.
Definition: mimalloc-doc.h:618
Commit segments in large pools.
Definition: mimalloc-doc.h:619
void mi_option_set_default(mi_option_t option, long value)
void * mi_heap_zalloc(mi_heap_t *heap, size_t size)
Allocate zero-initialized in a specific heap.
void mi_option_set(mi_option_t option, long value)
void mi_register_deferred_free(mi_deferred_free_fun *deferred_free)
Register a deferred free function.
void mi_thread_stats_print(FILE *out)
Print out heap statistics for this thread.
Definition: mimalloc-doc.h:623
void * mi_realloc_aligned_at(void *p, size_t newsize, size_t alignment, size_t offset)
void * blocks
start of the area containing heap blocks
Definition: mimalloc-doc.h:578
void * mi_realloc_aligned(void *p, size_t newsize, size_t alignment)
void mi_free(void *p)
Free previously allocated memory.
char * mi_heap_strdup(mi_heap_t *heap, const char *s)
Duplicate a string in a specific heap.
char * mi_heap_realpath(mi_heap_t *heap, const char *fname, char *resolved_name)
Resolve a file path name using a specific heap to allocate the result.
void * mi_calloc_aligned(size_t count, size_t size, size_t alignment)
void * mi_zalloc_small(size_t size)
Allocate a zero initialized small object.
char * mi_strndup(const char *s, size_t n)
Allocate and duplicate a string up to n bytes.
void * mi_expand(void *p, size_t newsize)
Try to re-allocate memory to newsize bytes in place.
void * mi_zalloc(size_t size)
Allocate zero-initialized size bytes.
void mi_thread_init()
Initialize mimalloc on a thread.
void * mi_heap_calloc(mi_heap_t *heap, size_t count, size_t size)
Allocate count zero-initialized elements in a specific heap.
void * mi_rezalloc_aligned_at(void *p, size_t newsize, size_t alignment, size_t offset)
size_t block_size
size in bytes of one block
Definition: mimalloc-doc.h:582
void mi_option_enable(mi_option_t option, bool enable)
void * mi_rezalloc_aligned(void *p, size_t newsize, size_t alignment)
void * mi_realloc(void *p, size_t newsize)
Re-allocate memory to newsize bytes.
Reset page memory when it becomes free.
Definition: mimalloc-doc.h:617
bool mi_heap_visit_blocks(const mi_heap_t *heap, bool visit_all_blocks, mi_block_visit_fun *visitor, void *arg)
Visit all areas and blocks in a heap.
void * mi_malloc(size_t size)
Allocate size bytes.
bool mi_option_enabled(mi_option_t option)
char * mi_heap_strndup(mi_heap_t *heap, const char *s, size_t n)
Duplicate a string of at most length n in a specific heap.
bool() mi_block_visit_fun(const mi_heap_t *heap, const mi_heap_area_t *area, void *block, size_t block_size, void *arg)
Visitor function passed to mi_heap_visit_blocks()
Definition: mimalloc-doc.h:592
void * mi_recalloc_aligned(void *p, size_t count, size_t size, size_t alignment)
char * mi_realpath(const char *fname, char *resolved_name)
Resolve a file path name.
Print error messages to stderr.
Definition: mimalloc-doc.h:621
bool mi_heap_contains_block(mi_heap_t *heap, const void *p)
Does a heap contain a pointer to a previously allocated block?
Print verbose messages to stderr.
Definition: mimalloc-doc.h:622
void * mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset)
void * mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset)
Allocate size bytes aligned by alignment at a specified offset.
void mi_heap_delete(mi_heap_t *heap)
Delete a previously allocated heap.
void * mi_rezalloc(void *p, size_t newsize)
Reallocate memory to newsize bytes, with extra memory initialized to zero.
mi_heap_t * mi_heap_get_default()
Get the default heap that is used for mi_malloc() et al.
size_t mi_good_size(size_t size)
Return the used allocation size.
void * mi_heap_mallocn(mi_heap_t *heap, size_t count, size_t size)
Allocate count elements in a specific heap.
An area of heap space contains blocks of a single size.
Definition: mimalloc-doc.h:577
Print statistics to stderr when the program is done.
Definition: mimalloc-doc.h:620
void * mi_zalloc_aligned(size_t size, size_t alignment)
size_t reserved
bytes reserved for this area
Definition: mimalloc-doc.h:579
struct mi_heap_s mi_heap_t
Type of first-class heaps.
Definition: mimalloc-doc.h:424
size_t used
bytes in use by allocated blocks
Definition: mimalloc-doc.h:581
void() mi_deferred_free_fun(bool force, unsigned long long heartbeat)
Type of deferred free functions.
Definition: mimalloc-doc.h:340
void * mi_recalloc(void *p, size_t count, size_t size)
Re-allocate memory to count elements of size bytes, with extra memory initialized to zero...
void mi_collect(bool force)
Eagerly free memory.
void mi_heap_destroy(mi_heap_t *heap)
Destroy a heap, freeing all its still allocated blocks.
void * mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset)
void * mi_recalloc_aligned_at(void *p, size_t count, size_t size, size_t alignment, size_t offset)
void mi_stats_reset()
Reset statistics.
char * mi_strdup(const char *s)
Allocate and duplicate a string.
void * mi_reallocf(void *p, size_t newsize)
Re-allocate memory to newsize bytes,.
void * mi_calloc(size_t count, size_t size)
Allocate zero-initialized count elements of size bytes.
void * mi_malloc_small(size_t size)
Allocate a small object.
bool mi_check_owned(const void *p)
Check safely if any pointer is part of the default heap of this thread.
long mi_option_get(mi_option_t option)
mi_heap_t * mi_heap_get_backing()
Get the backing heap.
mi_heap_t * mi_heap_new()
Create a new heap that can be used for allocation.
void * mi_heap_malloc(mi_heap_t *heap, size_t size)
Allocate in a specific heap.
size_t committed
current committed bytes of this area
Definition: mimalloc-doc.h:580
mi_option_t
Runtime options.
Definition: mimalloc-doc.h:616
bool mi_heap_check_owned(mi_heap_t *heap, const void *p)
Check safely if any pointer is part of a heap.
mi_heap_t * mi_heap_set_default(mi_heap_t *heap)
Set the default heap to use for mi_malloc() et al.