72#if defined(LMMP_WINDOWS)
74 #if defined(LIBLMMP_EXPORTS)
75 #define LMMP_API __declspec(dllexport)
77 #define LMMP_API __declspec(dllimport)
81 #define LMMP_API __attribute__((visibility("default")))
85#if defined(LMMP_ASM_X64) || defined(LMMP_ASM_ARM64)
87#error "LMMP_ASM is only supported on x86_64 or arm64 platforms"
100#define LMMP_MAX_ALIGN 16
104#define LOG2_LIMB_BITS 6
105#define LIMB_MAX (~(mp_limb_t)0)
108#define LLIMB_MASK ((mp_limb_t)0xffffffff)
110#if defined(LMMP_WINDOWS)
111 #if defined(__clang__)
112 #define LMMP_THREAD_LOCAL __declspec(thread)
114 #define LMMP_THREAD_LOCAL __thread
116#elif defined(__GNUC__) || defined(__clang__)
117 #define LMMP_THREAD_LOCAL __thread
119 #define LMMP_THREAD_LOCAL _Thread_local
122#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
123 #define STATIC_ASSERT _Static_assert
124#elif defined(__cplusplus) && __cplusplus >= 201103L
125 #define STATIC_ASSERT static_assert
128 #define STATIC_ASSERT(cond, msg) typedef char static_assert_##__LINE__[(cond) ? 1 : -1]
137 #if __cplusplus >= 201103L
139 #define LMMP_NORETURN [[noreturn]]
141 #if defined(__GNUC__) || defined(__clang__)
142 #define LMMP_NORETURN __attribute__((noreturn))
144 #define LMMP_NORETURN
149 #if defined(__STDC_VERSION__)
150 #if __STDC_VERSION__ >= 202311L
152 #define LMMP_NORETURN [[noreturn]]
153 #elif __STDC_VERSION__ >= 201112L
155 #define LMMP_NORETURN _Noreturn
160 #ifndef LMMP_NORETURN
161 #if defined(__GNUC__) || defined(__clang__)
162 #define LMMP_NORETURN __attribute__((noreturn))
164 #define LMMP_NORETURN
169#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
171 #define LMMP_ASSUME(expr) do { [[assume(expr)]]; } while(0)
172#elif defined(__clang__)
174 #define LMMP_ASSUME(expr) __builtin_assume(expr)
175#elif defined(__GNUC__)
179 #define LMMP_ASSUME(expr) do { __attribute__((assume(expr))); } while(0)
182 #define LMMP_ASSUME(expr) do { if (!(expr)) __builtin_unreachable(); } while(0)
186 #define LMMP_ASSUME(expr) ((void)(expr))
198#if defined(__GNUC__) && !defined(__clang__)
199 #define LMMP_VECT_CONSERVATIVE _Pragma("GCC optimize(\"vect-cost-model=very-cheap\")")
201 #define LMMP_VECT_CONSERVATIVE
213#if defined(__GNUC__) || defined(__clang__)
214 #define LMMP_NOINLINE __attribute__((noinline))
216 #define LMMP_NOINLINE
235typedef void* (*lmmp_heap_alloc_fn)(
size_t size);
237typedef void* (*lmmp_realloc_fn )(
void* ptr,
size_t size);
354#if LMMP_DEBUG_MEMORY_CHECK == 1
363#define lmmp_alloc(size) lmmp_alloc(size, __func__, __LINE__)
375#if LMMP_DEBUG_MEMORY_CHECK == 1
386#define lmmp_realloc(ptr, size) lmmp_realloc(ptr, size, __func__, __LINE__)
400#if LMMP_DEBUG_MEMORY_CHECK == 1
407#define lmmp_free(ptr) lmmp_free(ptr, __func__, __LINE__)
434#if LMMP_DEBUG_MEMORY_LEAK == 1
438#define lmmp_leak_tracker lmmp_leak_tracker(__func__, __LINE__)
440#define lmmp_leak_tracker ((void)0)
446#define LMMP_ABS(x) ((x) >= 0 ? (x) : -(x))
450#define LMMP_MIN(l, o) ((l) < (o) ? (l) : (o))
454#define LMMP_MAX(h, i) ((h) > (i) ? (h) : (i))
458#define LMMP_SWAP(x, y, type) \
467#define LMMP_POW2_Q(n) (((n) & ((n) - 1)) == 0)
471#define LMMP_ROUND_UP_MULTIPLE(a, m) ((a) + (m) - 1 - ((a) + (m) - 1) % (m))
474#define lmmp_copy(dst, src, n) memmove((dst), (src), ((size_t)(n) << 3))
476#define lmmp_zero(dst, n) memset((dst), 0, ((size_t)(n) << 3))
481#define lmmp_assert(x) \
484 lmmp_abort(LMMP_ERROR_ASSERT_FAILURE, #x, __func__, __LINE__); \
488#if LMMP_DEBUG_ASSERT_CHECK == 1
490#define lmmp_debug_assert(x) \
493 lmmp_abort(LMMP_ERROR_DEBUG_ASSERT_FAILURE, #x, __func__, __LINE__); \
498#define lmmp_debug_assert(x) LMMP_ASSUME(x)
501#if LMMP_DEBUG_PARAM_ASSERT_CHECK == 1
502#define lmmp_param_assert(x) \
505 lmmp_abort(LMMP_ERROR_PARAM_ASSERT_FAILURE, #x, __func__, __LINE__); \
509#define lmmp_param_assert(x) LMMP_ASSUME(x)
void * lmmp_realloc(void *ptr, size_t size)
内存重分配函数(调用lmmp_realloc_fn)
void lmmp_fill(mp_ptr begin, mp_ptr end, mp_limb_t val)
填充[begin,end)内的数组,值为val
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LMMP 全局堆内存分配函数
void *(*) lmmp_heap_alloc_fn(size_t size)
void(*) lmmp_heap_free_fn(void *ptr)
#define LMMP_API
Copyright (C) 2026 HJimmyK (Jericho Knox)
void lmmp_fill_n(mp_ptr dst, mp_size_t len, mp_limb_t val)
填充从dst开始的长度为len的数组,值为val
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
const mp_limb_t * mp_srcptr
lmmp_abort_fn lmmp_set_abort_fn(lmmp_abort_fn func)
设置 LMMP 全局退出函数(所有线程均生效)
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
int lmmp_stack_deinit(void)
LMMP 全局栈释放函数(通常不需要手动调用)
#define STATIC_ASSERT(cond, msg)
void(*) lmmp_abort_fn(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数指针类型
int lmmp_stack_init(size_t size)
LMMP 全局栈初始化函数(通常不需要手动调用)
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
void lmmp_global_init(void)
全局初始化函数(线程局部的)
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
void *(*) lmmp_realloc_fn(void *ptr, size_t size)
@ LMMP_ERROR_MEMORY_ALLOC_FAILURE
@ LMMP_ERROR_ASSERT_FAILURE
@ LMMP_ERROR_PARAM_ASSERT_FAILURE
@ LMMP_ERROR_MEMORY_FREE_FAILURE
@ LMMP_ERROR_UNEXPECTED_ERROR
@ LMMP_ERROR_DEBUG_ASSERT_FAILURE
@ LMMP_ERROR_OUT_OF_BOUNDS
#define lmmp_leak_tracker
int lmmp_alloc_count(int cnt)
堆内存分配计数器(线程局部)