From ab53a73cbdff604c0fa8b336030bd8d4e5f706a8 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 11 Dec 2024 14:29:06 -0800 Subject: [PATCH] small updates --- include/mimalloc/atomic.h | 6 ++-- include/mimalloc/internal.h | 55 +++++++++++++++++----------- include/mimalloc/prim.h | 6 ++-- include/mimalloc/track.h | 6 ++-- include/mimalloc/types.h | 72 ++++++++++++++++--------------------- 5 files changed, 74 insertions(+), 71 deletions(-) diff --git a/include/mimalloc/atomic.h b/include/mimalloc/atomic.h index 3b0ff559..95c1aefd 100644 --- a/include/mimalloc/atomic.h +++ b/include/mimalloc/atomic.h @@ -5,8 +5,8 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ #pragma once -#ifndef MIMALLOC_ATOMIC_H -#define MIMALLOC_ATOMIC_H +#ifndef MI_ATOMIC_H +#define MI_ATOMIC_H // include windows.h or pthreads.h #if defined(_WIN32) @@ -509,4 +509,4 @@ static inline void mi_lock_done(mi_lock_t* lock) { -#endif // __MIMALLOC_ATOMIC_H +#endif // MI_ATOMIC_H diff --git a/include/mimalloc/internal.h b/include/mimalloc/internal.h index 3c5bd486..4b211d71 100644 --- a/include/mimalloc/internal.h +++ b/include/mimalloc/internal.h @@ -5,8 +5,8 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ #pragma once -#ifndef MIMALLOC_INTERNAL_H -#define MIMALLOC_INTERNAL_H +#ifndef MI_INTERNAL_H +#define MI_INTERNAL_H // -------------------------------------------------------------------------- @@ -239,27 +239,42 @@ bool _mi_page_is_valid(mi_page_t* page); #endif +// ------------------------------------------------------ +// Debug +// ------------------------------------------------------ + +#if !defined(MI_DEBUG_UNINIT) +#define MI_DEBUG_UNINIT (0xD0) +#endif +#if !defined(MI_DEBUG_FREED) +#define MI_DEBUG_FREED (0xDF) +#endif +#if !defined(MI_DEBUG_PADDING) +#define MI_DEBUG_PADDING (0xDE) +#endif + /* ----------------------------------------------------------- - Error codes passed to `_mi_fatal_error` - All are recoverable but EFAULT is a serious error and aborts by default in secure mode. - For portability define undefined error codes using common Unix codes: - + Assertions ----------------------------------------------------------- */ -#include -#ifndef EAGAIN // double free -#define EAGAIN (11) + +#if (MI_DEBUG) +// use our own assertion to print without memory allocation +void _mi_assert_fail(const char* assertion, const char* fname, unsigned int line, const char* func); +#define mi_assert(expr) ((expr) ? (void)0 : _mi_assert_fail(#expr,__FILE__,__LINE__,__func__)) +#else +#define mi_assert(x) #endif -#ifndef ENOMEM // out of memory -#define ENOMEM (12) + +#if (MI_DEBUG>1) +#define mi_assert_internal mi_assert +#else +#define mi_assert_internal(x) #endif -#ifndef EFAULT // corrupted free-list or meta-data -#define EFAULT (14) -#endif -#ifndef EINVAL // trying to free an invalid pointer -#define EINVAL (22) -#endif -#ifndef EOVERFLOW // count*size overflow -#define EOVERFLOW (75) + +#if (MI_DEBUG>2) +#define mi_assert_expensive mi_assert +#else +#define mi_assert_expensive(x) #endif @@ -1023,4 +1038,4 @@ static inline void _mi_memzero_aligned(void* dst, size_t n) { } -#endif +#endif // MI_INTERNAL_H diff --git a/include/mimalloc/prim.h b/include/mimalloc/prim.h index 65f65376..99791585 100644 --- a/include/mimalloc/prim.h +++ b/include/mimalloc/prim.h @@ -5,8 +5,8 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ #pragma once -#ifndef MIMALLOC_PRIM_H -#define MIMALLOC_PRIM_H +#ifndef MI_PRIM_H +#define MI_PRIM_H // -------------------------------------------------------------------------- @@ -370,4 +370,4 @@ static inline mi_heap_t* mi_prim_get_default_heap(void) { #endif // mi_prim_get_default_heap() -#endif // MIMALLOC_PRIM_H +#endif // MI_PRIM_H diff --git a/include/mimalloc/track.h b/include/mimalloc/track.h index 4b5709e2..199308a6 100644 --- a/include/mimalloc/track.h +++ b/include/mimalloc/track.h @@ -5,8 +5,8 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ #pragma once -#ifndef MIMALLOC_TRACK_H -#define MIMALLOC_TRACK_H +#ifndef MI_TRACK_H +#define MI_TRACK_H /* ------------------------------------------------------------------------------------------------------ Track memory ranges with macros for tools like Valgrind address sanitizer, or other memory checkers. @@ -142,4 +142,4 @@ defined, undefined, or not accessible at all: } #endif -#endif +#endif // MI_TRACK_H diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h index cc64a400..03d522b5 100644 --- a/include/mimalloc/types.h +++ b/include/mimalloc/types.h @@ -5,8 +5,8 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ #pragma once -#ifndef MIMALLOC_TYPES_H -#define MIMALLOC_TYPES_H +#ifndef MI_TYPES_H +#define MI_TYPES_H // -------------------------------------------------------------------------- // This file contains the main type definitions for mimalloc: @@ -21,12 +21,9 @@ terms of the MIT license. A copy of the license can be found in the file #include // ptrdiff_t #include // uintptr_t, uint16_t, etc -#include "bits.h" // bit ops, size defines -#include "atomic.h" // _Atomic - -#ifdef _MSC_VER -#pragma warning(disable:4214) // bitfield is not int -#endif +#include // error codes +#include "bits.h" // size defines (MI_INTPTR_SIZE etc), bit operations +#include "atomic.h" // _Atomic primitives // Minimal alignment necessary. On most platforms 16 bytes are needed // due to SSE registers for example. This must be at least `sizeof(void*)` @@ -351,6 +348,7 @@ typedef enum mi_page_kind_e { // ------------------------------------------------------ // Heaps +// // Provide first-class heaps to allocate from. // A heap just owns a set of pages for allocation and // can only be allocate/reallocate from the thread that created it. @@ -426,40 +424,6 @@ struct mi_heap_s { }; -// ------------------------------------------------------ -// Debug -// ------------------------------------------------------ - -#if !defined(MI_DEBUG_UNINIT) -#define MI_DEBUG_UNINIT (0xD0) -#endif -#if !defined(MI_DEBUG_FREED) -#define MI_DEBUG_FREED (0xDF) -#endif -#if !defined(MI_DEBUG_PADDING) -#define MI_DEBUG_PADDING (0xDE) -#endif - -#if (MI_DEBUG) -// use our own assertion to print without memory allocation -void _mi_assert_fail(const char* assertion, const char* fname, unsigned int line, const char* func ); -#define mi_assert(expr) ((expr) ? (void)0 : _mi_assert_fail(#expr,__FILE__,__LINE__,__func__)) -#else -#define mi_assert(x) -#endif - -#if (MI_DEBUG>1) -#define mi_assert_internal mi_assert -#else -#define mi_assert_internal(x) -#endif - -#if (MI_DEBUG>2) -#define mi_assert_expensive mi_assert -#else -#define mi_assert_expensive(x) -#endif - // ------------------------------------------------------ // Statistics // ------------------------------------------------------ @@ -575,4 +539,28 @@ struct mi_tld_s { mi_stats_t stats; // statistics }; +/* ----------------------------------------------------------- + Error codes passed to `_mi_fatal_error` + All are recoverable but EFAULT is a serious error and aborts by default in secure mode. + For portability define undefined error codes using common Unix codes: + +----------------------------------------------------------- */ + +#ifndef EAGAIN // double free +#define EAGAIN (11) #endif +#ifndef ENOMEM // out of memory +#define ENOMEM (12) +#endif +#ifndef EFAULT // corrupted free-list or meta-data +#define EFAULT (14) +#endif +#ifndef EINVAL // trying to free an invalid pointer +#define EINVAL (22) +#endif +#ifndef EOVERFLOW // count*size overflow +#define EOVERFLOW (75) +#endif + + +#endif // MI_TYPES_H