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" 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 
135 void* mi_recalloc(void* p, size_t count, size_t size);
136 
150 void* mi_expand(void* p, size_t newsize);
151 
161 void* mi_mallocn(size_t count, size_t size);
162 
172 void* mi_reallocn(void* p, size_t count, size_t size);
173 
190 void* mi_reallocf(void* p, size_t newsize);
191 
192 
201 char* mi_strdup(const char* s);
202 
212 char* mi_strndup(const char* s, size_t n);
213 
226 char* mi_realpath(const char* fname, char* resolved_name);
227 
229 
230 // ------------------------------------------------------
231 // Extended functionality
232 // ------------------------------------------------------
233 
237 
240 #define MI_SMALL_SIZE_MAX (128*sizeof(void*))
241 
249 void* mi_malloc_small(size_t size);
250 
258 void* mi_zalloc_small(size_t size);
259 
274 size_t mi_usable_size(void* p);
275 
285 size_t mi_good_size(size_t size);
286 
294 void mi_collect(bool force);
295 
300 void mi_stats_print(FILE* out);
301 
303 void mi_stats_reset();
304 
308 void mi_thread_init();
309 
314 void mi_thread_done();
315 
320 void mi_thread_stats_print(FILE* out);
321 
327 typedef void (mi_deferred_free_fun)(bool force, unsigned long long heartbeat);
328 
344 
346 
347 // ------------------------------------------------------
348 // Aligned allocation
349 // ------------------------------------------------------
350 
356 
369 void* mi_malloc_aligned(size_t size, size_t alignment);
370 void* mi_zalloc_aligned(size_t size, size_t alignment);
371 void* mi_calloc_aligned(size_t count, size_t size, size_t alignment);
372 void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment);
373 
384 void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset);
385 void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset);
386 void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset);
387 void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset);
388 
390 
396 
401 struct mi_heap_s;
402 
407 typedef struct mi_heap_s mi_heap_t;
408 
411 
419 void mi_heap_delete(mi_heap_t* heap);
420 
428 void mi_heap_destroy(mi_heap_t* heap);
429 
434 
438 
445 
448 void* mi_heap_malloc(mi_heap_t* heap, size_t size);
449 
452 void* mi_heap_zalloc(mi_heap_t* heap, size_t size);
453 
456 void* mi_heap_calloc(mi_heap_t* heap, size_t count, size_t size);
457 
460 void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size);
461 
464 char* mi_heap_strdup(mi_heap_t* heap, const char* s);
465 
468 char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n);
469 
472 char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name);
473 
474 void* mi_heap_realloc(mi_heap_t* heap, void* p, size_t newsize);
475 void* mi_heap_reallocn(mi_heap_t* heap, void* p, size_t count, size_t size);
476 void* mi_heap_reallocf(mi_heap_t* heap, void* p, size_t newsize);
477 
478 void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment);
479 void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset);
480 void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment);
481 void* mi_heap_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset);
482 void* mi_heap_calloc_aligned(mi_heap_t* heap, size_t count, size_t size, size_t alignment);
483 void* mi_heap_calloc_aligned_at(mi_heap_t* heap, size_t count, size_t size, size_t alignment, size_t offset);
484 void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment);
485 void* mi_heap_realloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset);
486 
488 
494 
506 #define mi_malloc_tp(tp) ((tp*)mi_malloc(sizeof(tp)))
507 
509 #define mi_zalloc_tp(tp) ((tp*)mi_zalloc(sizeof(tp)))
510 
512 #define mi_calloc_tp(tp,count) ((tp*)mi_calloc(count,sizeof(tp)))
513 
515 #define mi_mallocn_tp(tp,count) ((tp*)mi_mallocn(count,sizeof(tp)))
516 
518 #define mi_reallocn_tp(p,tp,count) ((tp*)mi_reallocn(p,count,sizeof(tp)))
519 
521 #define mi_heap_malloc_tp(hp,tp) ((tp*)mi_heap_malloc(hp,sizeof(tp)))
522 
524 #define mi_heap_zalloc_tp(hp,tp) ((tp*)mi_heap_zalloc(hp,sizeof(tp)))
525 
527 #define mi_heap_calloc_tp(hp,tp,count) ((tp*)mi_heap_calloc(hp,count,sizeof(tp)))
528 
530 #define mi_heap_mallocn_tp(hp,tp,count) ((tp*)mi_heap_mallocn(hp,count,sizeof(tp)))
531 
533 #define mi_heap_reallocn_tp(hp,p,tp,count) ((tp*)mi_heap_reallocn(p,count,sizeof(tp)))
534 
536 
542 
549 bool mi_heap_contains_block(mi_heap_t* heap, const void* p);
550 
559 bool mi_heap_check_owned(mi_heap_t* heap, const void* p);
560 
568 bool mi_check_owned(const void* p);
569 
572 typedef struct mi_heap_area_s {
573  void* blocks;
574  size_t reserved;
575  size_t committed;
576  size_t used;
577  size_t block_size;
579 
587 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);
588 
600 bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_all_blocks, mi_block_visit_fun* visitor, void* arg);
601 
603 
609 
611 typedef enum mi_option_e {
619 } mi_option_t;
620 
621 bool mi_option_enabled(mi_option_t option);
622 void mi_option_enable(mi_option_t option, bool enable);
623 void mi_option_enable_default(mi_option_t option, bool enable);
624 
625 long mi_option_get(mi_option_t option);
626 void mi_option_set(mi_option_t option, long value);
627 void mi_option_set_default(mi_option_t option, long value);
628 
629 
631 
638 
639 void* mi_recalloc(void* p, size_t count, size_t size);
640 size_t mi_malloc_size(const void* p);
641 size_t mi_malloc_usable_size(const void *p);
642 void mi_cfree(void* p);
643 
644 int mi_posix_memalign(void** p, size_t alignment, size_t size);
645 int mi__posix_memalign(void** p, size_t alignment, size_t size);
646 void* mi_memalign(size_t alignment, size_t size);
647 void* mi_valloc(size_t size);
648 
649 void* mi_pvalloc(size_t size);
650 void* mi_aligned_alloc(size_t alignment, size_t size);
651 void* mi_reallocarray(void* p, size_t count, size_t size);
652 
653 void mi_free_size(void* p, size_t size);
654 void mi_free_size_aligned(void* p, size_t size, size_t alignment);
655 void mi_free_aligned(void* p, size_t alignment);
656 
658 void* mi_new(std::size_t n) noexcept(false);
659 
661 void* mi_new_aligned(std::size_t n, std::align_val_t alignment) noexcept(false);
662 
663 
665 
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_heap_realloc_aligned(mi_heap_t *heap, void *p, size_t newsize, size_t alignment)
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_mallocn(size_t count, size_t size)
Allocate count elements of size bytes.
size_t mi_malloc_size(const void *p)
void mi_thread_done()
Uninitialize mimalloc on a thread.
Reset segment memory when a segment is cached.
Definition: mimalloc-doc.h:613
int mi_posix_memalign(void **p, size_t alignment, size_t size)
Commit segments in large pools.
Definition: mimalloc-doc.h:614
void mi_option_set_default(mi_option_t option, long value)
void * mi_new_aligned(std::size_t n, std::align_val_t alignment) noexcept(false)
Only defined in C++ compilation; raise std::bad_alloc exception on failure.
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_cfree(void *p)
void mi_thread_stats_print(FILE *out)
Print out heap statistics for this thread.
Definition: mimalloc-doc.h:618
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:573
void * mi_realloc_aligned(void *p, size_t newsize, size_t alignment)
int mi__posix_memalign(void **p, size_t alignment, size_t size)
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_heap_calloc_aligned_at(mi_heap_t *heap, size_t count, size_t size, size_t alignment, size_t offset)
void * mi_calloc_aligned(size_t count, size_t size, size_t alignment)
void * mi_heap_zalloc_aligned(mi_heap_t *heap, 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_pvalloc(size_t size)
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_new(std::size_t n) noexcept(false)
Only defined in C++ compilation; raise std::bad_alloc exception on failure.
void * mi_heap_calloc_aligned(mi_heap_t *heap, size_t count, size_t size, size_t alignment)
size_t block_size
size in bytes of one block
Definition: mimalloc-doc.h:577
void * mi_reallocarray(void *p, size_t count, size_t size)
void mi_option_enable(mi_option_t option, bool enable)
void * mi_realloc(void *p, size_t newsize)
Re-allocate memory to newsize bytes.
void * mi_heap_reallocf(mi_heap_t *heap, void *p, size_t newsize)
void mi_free_size_aligned(void *p, size_t size, size_t alignment)
Reset page memory when it becomes free.
Definition: mimalloc-doc.h:612
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:587
void * mi_heap_malloc_aligned_at(mi_heap_t *heap, size_t size, size_t alignment, size_t offset)
char * mi_realpath(const char *fname, char *resolved_name)
Resolve a file path name.
Print error messages to stderr.
Definition: mimalloc-doc.h:616
void * mi_memalign(size_t alignment, size_t size)
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:617
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.
mi_heap_t * mi_heap_get_default()
Get the default heap that is used for mi_malloc() et al.
void * mi_aligned_alloc(size_t alignment, size_t size)
void * mi_valloc(size_t size)
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:572
Print statistics to stderr when the program is done.
Definition: mimalloc-doc.h:615
void * mi_zalloc_aligned(size_t size, size_t alignment)
size_t reserved
bytes reserved for this area
Definition: mimalloc-doc.h:574
struct mi_heap_s mi_heap_t
Type of first-class heaps.
Definition: mimalloc-doc.h:407
size_t used
bytes in use by allocated blocks
Definition: mimalloc-doc.h:576
void() mi_deferred_free_fun(bool force, unsigned long long heartbeat)
Type of deferred free functions.
Definition: mimalloc-doc.h:327
void mi_free_size(void *p, size_t size)
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_stats_reset()
Reset statistics.
void * mi_heap_reallocn(mi_heap_t *heap, void *p, size_t count, size_t size)
void * mi_heap_realloc(mi_heap_t *heap, void *p, size_t newsize)
size_t mi_malloc_usable_size(const void *p)
char * mi_strdup(const char *s)
Allocate and duplicate a string.
void * mi_heap_realloc_aligned_at(mi_heap_t *heap, void *p, size_t newsize, size_t alignment, size_t offset)
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_heap_zalloc_aligned_at(mi_heap_t *heap, size_t size, size_t alignment, size_t offset)
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.
void * mi_heap_malloc_aligned(mi_heap_t *heap, size_t size, size_t alignment)
long mi_option_get(mi_option_t option)
mi_heap_t * mi_heap_get_backing()
Get the backing heap.
void mi_free_aligned(void *p, size_t alignment)
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:575
mi_option_t
Runtime options.
Definition: mimalloc-doc.h:611
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.