LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
test_lifecycle.c 文件参考
#include "lmmp/lmmp.h"
#include "lmmp/lmmpn.h"
#include "lmmp/numth.h"
#include "cases.h"
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <process.h>
test_lifecycle.c 的引用(Include)关系图:

浏览该文件的源代码.

结构体

struct  thread_case

枚举

enum  { BIG_N = 64 , DECIMAL_CAP = 20 * BIG_N + 8 , FACTORIAL_N = 65537 }
 Copyright (C) 2026 HJimmyK (Jericho Knox) 更多...

函数

static mp_size_t big_to_decimal (mp_byte_t *out, mp_srcptr a)
static void * count_alloc (size_t n)
static void count_free (void *p)
static void * count_realloc (void *p, size_t n)
static mp_ptr factorial_buffer (mp_size_t n)
static void fill_big (mp_ptr a)
static void test_fn_heap_balance_full_cycle (void)
static void test_fn_lifecycle_idempotent (void)
static void test_fn_thread_isolation (void)
void test_lifecycle (void)
 Copyright (C) 2026 HJimmyK (Jericho Knox)
static unsigned __stdcall thread_worker (void *arg)

变量

static atomic_long live_allocations

结构体说明

◆ thread_case

struct thread_case

在文件 test_lifecycle.c 第 147 行定义.

thread_case 的协作图:
成员变量
mp_bitcnt_t bits
const mp_byte_t * decimal
mp_srcptr input
mp_size_t len
bool pass
mp_size_t rn

枚举类型说明

◆ anonymous enum

anonymous enum

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

枚举值
BIG_N 64 
DECIMAL_CAP 20 * BIG_N + 8 
FACTORIAL_N 65537 

在文件 test_lifecycle.c 第 46 行定义.

46{ BIG_N = 64, DECIMAL_CAP = 20 * BIG_N + 8, FACTORIAL_N = 65537 };
@ DECIMAL_CAP
@ FACTORIAL_N

函数说明

◆ big_to_decimal()

mp_size_t big_to_decimal ( mp_byte_t * out,
mp_srcptr a )
static

在文件 test_lifecycle.c 第 54 行定义.

54 {
55 return lmmp_to_str_(out, a, BIG_N, 10);
56}
mp_size_t lmmp_to_str_(mp_byte_t *dst, mp_srcptr numa, mp_size_t na, int base)
limb数组转字节数组操作 [numa,na,B] to [dst,return value,base]

引用了 BIG_N, big_to_decimal() , 以及 lmmp_to_str_().

被这些函数引用 big_to_decimal(), test_fn_heap_balance_full_cycle(), test_fn_lifecycle_idempotent(), test_fn_thread_isolation() , 以及 thread_worker().

函数调用图:
这是这个函数的调用关系图:

◆ count_alloc()

void * count_alloc ( size_t n)
static

在文件 test_lifecycle.c 第 102 行定义.

102 {
103 void *p = malloc(n);
104 if (p)
106 return p;
107}
#define n
static atomic_long live_allocations

引用了 count_alloc(), live_allocations , 以及 n.

被这些函数引用 count_alloc(), count_realloc() , 以及 test_fn_heap_balance_full_cycle().

函数调用图:
这是这个函数的调用关系图:

◆ count_free()

void count_free ( void * p)
static

在文件 test_lifecycle.c 第 109 行定义.

109 {
110 if (p)
112 free(p);
113}

引用了 count_free() , 以及 live_allocations.

被这些函数引用 count_free() , 以及 test_fn_heap_balance_full_cycle().

函数调用图:
这是这个函数的调用关系图:

◆ count_realloc()

void * count_realloc ( void * p,
size_t n )
static

在文件 test_lifecycle.c 第 115 行定义.

115 {
116 return p ? realloc(p, n) : count_alloc(n);
117}
static void * count_alloc(size_t n)

引用了 count_alloc(), count_realloc() , 以及 n.

被这些函数引用 count_realloc() , 以及 test_fn_heap_balance_full_cycle().

函数调用图:
这是这个函数的调用关系图:

◆ factorial_buffer()

mp_ptr factorial_buffer ( mp_size_t n)
static

在文件 test_lifecycle.c 第 58 行定义.

58 {
59 mp_ptr buffer = calloc(n + 1, sizeof(*buffer));
60 if (!buffer)
61 abort();
62 return buffer;
63}
mp_limb_t * mp_ptr
定义 lmmp.h:96

引用了 factorial_buffer() , 以及 n.

被这些函数引用 factorial_buffer(), test_fn_heap_balance_full_cycle(), test_fn_lifecycle_idempotent() , 以及 thread_worker().

函数调用图:
这是这个函数的调用关系图:

◆ fill_big()

void fill_big ( mp_ptr a)
static

在文件 test_lifecycle.c 第 48 行定义.

48 {
49 for (mp_size_t i = 0; i < BIG_N; ++i)
50 a[i] = 0x9e3779b97f4a7c15ull * (i + 1) + 0x123456789abcdefull;
51 a[BIG_N - 1] |= 1;
52}
uint64_t mp_size_t
定义 lmmp.h:93

引用了 BIG_N , 以及 fill_big().

被这些函数引用 fill_big(), test_fn_heap_balance_full_cycle(), test_fn_lifecycle_idempotent() , 以及 test_fn_thread_isolation().

函数调用图:
这是这个函数的调用关系图:

◆ test_fn_heap_balance_full_cycle()

void test_fn_heap_balance_full_cycle ( void )
static

在文件 test_lifecycle.c 第 119 行定义.

119 {
121 const lmmp_heap_allocator_t libc = {malloc, free, realloc};
122 lmmp_set_heap_allocator(&counting);
123 long baseline = atomic_load(&live_allocations);
124 TEST_CHECK(baseline > 0);
125
126 mp_limb_t a[BIG_N], dst[2 * BIG_N];
127 mp_byte_t decimal[DECIMAL_CAP];
128 fill_big(a);
129 lmmp_mul_(dst, a, BIG_N, a, BIG_N);
130 big_to_decimal(decimal, a);
131
132 mp_bitcnt_t bits = 0;
134 mp_ptr fac = factorial_buffer(rn);
135 lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N);
136 free(fac);
137 TEST_CHECK(atomic_load(&live_allocations) > baseline);
138
140 TEST_CHECK(atomic_load(&live_allocations) == 0);
142 TEST_CHECK(atomic_load(&live_allocations) == baseline);
144 TEST_CHECK(atomic_load(&live_allocations) == 0);
145}
uint8_t mp_byte_t
定义 lmmp.h:91
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LMMP 全局堆内存分配函数
size_t mp_bitcnt_t
定义 lmmp.h:98
void lmmp_global_init(void)
全局初始化函数(线程局部的)
uint64_t mp_limb_t
定义 lmmp.h:92
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
void lmmp_mul_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
不等长乘法操作 [dst,na+nb] = [numa,na] * [numb,nb]
mp_size_t lmmp_factorial_size_(uint n, mp_bitcnt_t *bits)
计算 n!
mp_size_t lmmp_factorial_(mp_ptr dst, mp_bitcnt_t bits, mp_size_t rn, uint n)
计算 n!
#define TEST_CHECK(cond)
定义 test.h:37
static mp_size_t big_to_decimal(mp_byte_t *out, mp_srcptr a)
static void fill_big(mp_ptr a)
static mp_ptr factorial_buffer(mp_size_t n)
static void * count_realloc(void *p, size_t n)
static void count_free(void *p)

引用了 BIG_N, big_to_decimal(), count_alloc(), count_free(), count_realloc(), DECIMAL_CAP, factorial_buffer(), FACTORIAL_N, fill_big(), live_allocations, lmmp_factorial_(), lmmp_factorial_size_(), lmmp_global_deinit(), lmmp_global_init(), lmmp_mul_(), lmmp_set_heap_allocator(), TEST_CASE , 以及 TEST_CHECK.

函数调用图:

◆ test_fn_lifecycle_idempotent()

void test_fn_lifecycle_idempotent ( void )
static

在文件 test_lifecycle.c 第 65 行定义.

65 {
66 mp_limb_t a[BIG_N], dst[2 * BIG_N], dst_ref[2 * BIG_N];
67 mp_byte_t before[DECIMAL_CAP], after[DECIMAL_CAP];
68 fill_big(a);
69
72
73 mp_size_t len = big_to_decimal(before, a);
74 TEST_CHECK(len > BIG_N * 15);
75 lmmp_mul_(dst_ref, a, BIG_N, a, BIG_N);
76
77 mp_bitcnt_t bits = 0;
79 mp_ptr fac = factorial_buffer(rn);
80 TEST_CHECK(lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N) > 0);
81
85
86 TEST_CHECK(big_to_decimal(after, a) == len && memcmp(after, before, len) == 0);
87 lmmp_mul_(dst, a, BIG_N, a, BIG_N);
88 TEST_CHECK(memcmp(dst, dst_ref, sizeof(dst)) == 0);
89
90 mp_bitcnt_t bits2 = 0;
92 TEST_CHECK(rn2 == rn && bits2 == bits);
93 mp_ptr fac2 = factorial_buffer(rn2);
94 TEST_CHECK(lmmp_factorial_(fac2, bits2, rn2 + 1, FACTORIAL_N) > 0);
95 TEST_CHECK(rn == rn2 && memcmp(fac, fac2, (rn + 1) * sizeof(*fac)) == 0);
96 free(fac2);
97 free(fac);
98}

引用了 BIG_N, big_to_decimal(), DECIMAL_CAP, factorial_buffer(), FACTORIAL_N, fill_big(), lmmp_factorial_(), lmmp_factorial_size_(), lmmp_global_deinit(), lmmp_global_init(), lmmp_mul_(), TEST_CASE , 以及 TEST_CHECK.

函数调用图:

◆ test_fn_thread_isolation()

void test_fn_thread_isolation ( void )
static

在文件 test_lifecycle.c 第 177 行定义.

177 {
178 enum { WORKERS = 4 };
179 mp_limb_t a[BIG_N];
180 mp_byte_t expected[DECIMAL_CAP], decimal[DECIMAL_CAP];
181 fill_big(a);
182 mp_size_t len = big_to_decimal(expected, a);
183 mp_bitcnt_t bits = 0;
185 thread_case cases[WORKERS];
186#if defined(LMMP_WINDOWS)
187 HANDLE workers[WORKERS];
188#else
189 pthread_t workers[WORKERS];
190#endif
191 size_t started = 0;
192 for (; started < WORKERS; ++started) {
193 cases[started] = (thread_case){a, expected, len, rn, bits, false};
194#if defined(LMMP_WINDOWS)
195 workers[started] = (HANDLE)_beginthreadex(NULL, 0, thread_worker, &cases[started], 0, NULL);
196 if (!workers[started])
197 break;
198#else
199 if (pthread_create(&workers[started], NULL, thread_worker, &cases[started]) != 0)
200 break;
201#endif
202 }
203 TEST_CHECK_EQ(started, WORKERS);
204
205 for (int repeat = 0; repeat < 8; ++repeat)
206 TEST_CHECK(big_to_decimal(decimal, a) == len && memcmp(decimal, expected, len) == 0);
207
208 for (size_t i = 0; i < started; ++i) {
209#if defined(LMMP_WINDOWS)
210 if (WaitForSingleObject(workers[i], INFINITE) != WAIT_OBJECT_0)
211 abort();
212 CloseHandle(workers[i]);
213#else
214 if (pthread_join(workers[i], NULL) != 0)
215 abort();
216#endif
217 TEST_CHECK_MSG(cases[i].pass, "worker thread result mismatch");
218 }
219 TEST_CHECK(big_to_decimal(decimal, a) == len && memcmp(decimal, expected, len) == 0);
220}
#define TEST_CHECK_MSG(cond, msg)
定义 test.h:38
#define TEST_CHECK_EQ(a, b)
定义 test.h:39
static unsigned __stdcall thread_worker(void *arg)

引用了 BIG_N, big_to_decimal(), DECIMAL_CAP, FACTORIAL_N, fill_big(), lmmp_factorial_size_(), TEST_CASE, TEST_CHECK, TEST_CHECK_EQ, TEST_CHECK_MSG , 以及 thread_worker().

函数调用图:

◆ test_lifecycle()

void test_lifecycle ( void )

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

在文件 test_lifecycle.c 第 222 行定义.

222 {
223 TEST_RUN("global/lifecycle", lifecycle_idempotent);
224 TEST_RUN("global/lifecycle", heap_balance_full_cycle);
225 TEST_RUN("global/thread", thread_isolation);
226}
#define TEST_RUN(category, name)
定义 test.h:36

引用了 test_lifecycle() , 以及 TEST_RUN.

被这些函数引用 main() , 以及 test_lifecycle().

函数调用图:
这是这个函数的调用关系图:

◆ thread_worker()

unsigned __stdcall thread_worker ( void * arg)
static

在文件 test_lifecycle.c 第 156 行定义.

156 {
157#else
158static void *thread_worker(void *arg) {
159#endif
160 thread_case *test = arg;
162
163 mp_byte_t decimal[DECIMAL_CAP];
164 bool match = big_to_decimal(decimal, test->input) == test->len &&
165 memcmp(decimal, test->decimal, test->len) == 0;
166 mp_bitcnt_t bits = 0;
168 mp_ptr fac = factorial_buffer(rn);
169 test->pass = match && rn == test->rn && bits == test->bits &&
170 lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N) > 0;
171 free(fac);
172
174 return 0;
175}
const mp_byte_t * decimal

引用了 big_to_decimal(), thread_case::bits, thread_case::decimal, DECIMAL_CAP, factorial_buffer(), FACTORIAL_N, thread_case::input, thread_case::len, lmmp_factorial_(), lmmp_factorial_size_(), lmmp_global_deinit(), lmmp_global_init(), thread_case::pass, thread_case::rn , 以及 thread_worker().

被这些函数引用 test_fn_thread_isolation() , 以及 thread_worker().

函数调用图:
这是这个函数的调用关系图:

变量说明

◆ live_allocations

atomic_long live_allocations
static

在文件 test_lifecycle.c 第 100 行定义.

被这些函数引用 count_alloc(), count_free() , 以及 test_fn_heap_balance_full_cycle().