@@ -396,43 +396,6 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_theap_zalloc_aligned(
396396mi_decl_nodiscard mi_decl_export void * mi_theap_realloc (mi_theap_t * theap, void * p, size_t newsize) mi_attr_noexcept mi_attr_alloc_size(3 );
397397mi_decl_nodiscard mi_decl_export void * mi_theap_rezalloc (mi_theap_t * theap, void * p, size_t newsize) mi_attr_noexcept mi_attr_alloc_size(3 );
398398
399- // ------------------------------------------------------
400- // Fast constant size allocations.
401- // ------------------------------------------------------
402-
403- // Machine word size allocation. `wsize` is the allocation size in machine words (`sizeof(size_t)`)
404- mi_decl_nodiscard mi_decl_restrict void * mi_wzalloc_small (size_t wsize) mi_attr_noexcept;
405- mi_decl_nodiscard mi_decl_restrict void * mi_wmalloc_small (size_t wsize) mi_attr_noexcept;
406- mi_decl_nodiscard mi_decl_restrict void * mi_theap_wmalloc_small (mi_theap_t * theap, size_t wsize) mi_attr_noexcept;
407- mi_decl_nodiscard mi_decl_restrict void * mi_theap_wzalloc_small (mi_theap_t * theap, size_t wsize) mi_attr_noexcept;
408-
409- // get the machine word size from a byte size.
410- static inline size_t mi_wsize_from_size (size_t size) {
411- return ((size + sizeof (size_t ) - 1 ) / sizeof (size_t ));
412- }
413-
414- static inline mi_decl_restrict void * mi_malloc_csize (size_t size) mi_attr_noexcept {
415- if (size <= MI_SMALL_SIZE_MAX ) { return mi_wmalloc_small (mi_wsize_from_size (size)); } else { return mi_malloc (size); }
416- }
417- static inline mi_decl_restrict void * mi_zalloc_csize (size_t size) mi_attr_noexcept {
418- if (size <= MI_SMALL_SIZE_MAX ) { return mi_wzalloc_small (mi_wsize_from_size (size)); } else { return mi_zalloc (size); }
419- }
420- static inline mi_decl_restrict void * mi_theap_malloc_csize (mi_theap_t * theap, size_t size) mi_attr_noexcept {
421- assert (theap!=NULL );
422- if (size <= MI_SMALL_SIZE_MAX ) { return mi_theap_wmalloc_small (theap,mi_wsize_from_size (size)); } else { return mi_theap_malloc (theap,size); }
423- }
424- static inline mi_decl_restrict void * mi_theap_zalloc_csize (mi_theap_t * theap, size_t size) mi_attr_noexcept {
425- assert (theap!=NULL );
426- if (size <= MI_SMALL_SIZE_MAX ) { return mi_theap_wzalloc_small (theap,mi_wsize_from_size (size)); } else { return mi_theap_zalloc (theap,size); }
427- }
428- static inline void mi_free_csize (void * p, size_t size) mi_attr_noexcept {
429- if (size <= MI_SMALL_SIZE_MAX ) { mi_free_small (p); } else { mi_free (p); }
430- }
431- static inline void mi_free_csize_nonnull (void * p, size_t size) mi_attr_noexcept {
432- assert (p!=NULL );
433- if (size <= MI_SMALL_SIZE_MAX ) { mi_free_small_nonnull (p); } else { mi_free (p); }
434- }
435-
436399// ------------------------------------------------------
437400// Experimental
438401// ------------------------------------------------------
@@ -588,8 +551,13 @@ mi_decl_export int mi_wdupenv_s(wchar_t** buf, size_t* size, const wchar_t* name
588551mi_decl_nodiscard mi_decl_export mi_decl_restrict wchar_t * mi_wcsdup (const wchar_t * s) mi_attr_noexcept mi_attr_malloc;
589552mi_decl_nodiscard mi_decl_export mi_decl_restrict unsigned char * mi_mbsdup (const unsigned char * s) mi_attr_noexcept mi_attr_malloc;
590553
591- // The `mi_new` wrappers implement C++ semantics on out-of-memory instead of directly returning `NULL`.
592- // (and call `std::get_new_handler` and potentially raise a `std::bad_alloc` exception).
554+ // --------------------------------------------------------
555+ // C++ wrappers
556+ // The `mi_new` wrappers implement C++ semantics on out-of-memory
557+ // instead of directly returning `NULL`. (and call `std::get_new_handler`
558+ // and potentially raise a `std::bad_alloc` exception).
559+ // --------------------------------------------------------
560+
593561mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_new (size_t size) mi_attr_malloc mi_attr_alloc_size(1 );
594562mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_new_aligned (size_t size, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1 ) mi_attr_alloc_align(2 );
595563mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_new_nothrow (size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1 );
@@ -601,6 +569,63 @@ mi_decl_nodiscard mi_decl_export void* mi_new_reallocn(void* p, size_t newcount,
601569mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_heap_alloc_new (mi_heap_t * heap, size_t size) mi_attr_malloc mi_attr_alloc_size(2 );
602570mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_heap_alloc_new_n (mi_heap_t * heap, size_t count, size_t size) mi_attr_malloc mi_attr_alloc_size2(2 , 3 );
603571
572+ mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_theap_alloc_new (mi_theap_t * theap, size_t size) mi_attr_malloc mi_attr_alloc_size(2 );
573+ mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_theap_alloc_new_n (mi_theap_t * theap, size_t count, size_t size) mi_attr_malloc mi_attr_alloc_size2(2 , 3 );
574+ mi_decl_nodiscard mi_decl_export mi_decl_restrict void * mi_theap_alloc_new_nothrow (mi_theap_t * theap, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2 );
575+
576+
577+ // --------------------------------------------------------------------------
578+ // Inlined constant size allocations.
579+ // These are meant for runtime systems, or overrides where we need the best
580+ // performance for small, constant-size allocations.
581+ // These are only better than the regular functions if the size or alignment
582+ // are indeed constant at the call site.
583+ // --------------------------------------------------------------------------
584+
585+ // Internal machine word size allocation. `wsize` is the allocation size in machine words (`sizeof(size_t)`)
586+ mi_decl_nodiscard mi_decl_restrict void * mi_wzalloc_small (size_t wsize) mi_attr_noexcept mi_attr_malloc;
587+ mi_decl_nodiscard mi_decl_restrict void * mi_wmalloc_small (size_t wsize) mi_attr_noexcept mi_attr_malloc;
588+ mi_decl_nodiscard mi_decl_restrict void * mi_theap_wmalloc_small (mi_theap_t * theap, size_t wsize) mi_attr_noexcept mi_attr_malloc;
589+ mi_decl_nodiscard mi_decl_restrict void * mi_theap_wzalloc_small (mi_theap_t * theap, size_t wsize) mi_attr_noexcept mi_attr_malloc;
590+
591+ static inline size_t mi_wsize_from_size (size_t size) mi_attr_noexcept {
592+ return (size + sizeof (size_t ) - 1 ) / sizeof (size_t );
593+ }
594+
595+ static inline mi_decl_restrict void * mi_malloc_csize (size_t size) mi_attr_noexcept {
596+ if (size <= MI_SMALL_SIZE_MAX ) { return mi_wmalloc_small (mi_wsize_from_size (size)); } else { return mi_malloc (size); }
597+ }
598+ static inline mi_decl_restrict void * mi_zalloc_csize (size_t size) mi_attr_noexcept {
599+ if (size <= MI_SMALL_SIZE_MAX ) { return mi_wzalloc_small (mi_wsize_from_size (size)); } else { return mi_zalloc (size); }
600+ }
601+ static inline mi_decl_restrict void * mi_theap_malloc_csize (mi_theap_t * theap, size_t size) mi_attr_noexcept {
602+ assert (theap!=NULL );
603+ if (size <= MI_SMALL_SIZE_MAX ) { return mi_theap_wmalloc_small (theap,mi_wsize_from_size (size)); } else { return mi_theap_malloc (theap,size); }
604+ }
605+ static inline mi_decl_restrict void * mi_theap_zalloc_csize (mi_theap_t * theap, size_t size) mi_attr_noexcept {
606+ assert (theap!=NULL );
607+ if (size <= MI_SMALL_SIZE_MAX ) { return mi_theap_wzalloc_small (theap,mi_wsize_from_size (size)); } else { return mi_theap_zalloc (theap,size); }
608+ }
609+
610+ static inline void mi_free_csize (void * p, size_t size) mi_attr_noexcept {
611+ if (size <= MI_SMALL_SIZE_MAX ) { mi_free_small (p); } else { mi_free (p); }
612+ }
613+ static inline void mi_free_csize_nonnull (void * p, size_t size) mi_attr_noexcept {
614+ assert (p!=NULL );
615+ if (size <= MI_SMALL_SIZE_MAX ) { mi_free_small_nonnull (p); } else { mi_free (p); }
616+ }
617+ static inline void mi_free_csize_aligned (void * p, size_t size, size_t aligned) mi_attr_noexcept {
618+ if (aligned <= size && size <= MI_SMALL_SIZE_MAX ) { mi_free_small (p); } else { mi_free (p); }
619+ }
620+ static inline void mi_free_csize_aligned_nonnull (void * p, size_t size, size_t aligned) mi_attr_noexcept {
621+ assert (p!=NULL );
622+ if (aligned <= size && size <= MI_SMALL_SIZE_MAX ) { mi_free_small_nonnull (p); } else { mi_free (p); }
623+ }
624+
625+ // ------------------------------------------------------
626+ // C++ standard library allocator interface.
627+ // ------------------------------------------------------
628+
604629#ifdef __cplusplus
605630}
606631#endif
0 commit comments