1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Heap memory and automatically reclaimed storage.
3 *@ TODO Back the _flux_ heap.
4 *@ TODO Add cache for "the youngest" two or three n_MEMORY_AUTOREC_SIZE arenas
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #ifndef HAVE_AMALGAMATION
28 * We use per-execution context memory arenas, to be found in
29 * n_go_data->gdc_mempool; if NULL, set to ->gdc__mempool_buf.
30 * n_memory_reset() that happens on loop ticks reclaims their memory, and
31 * performs debug checks also on the former #ifdef HAVE_MEMORY_DEBUG.
32 * The arena that is used already during program startup is special in that
33 * _pool_fixate() will set "a lower bound" in order not to reclaim memory that
34 * must be kept vivid during the lifetime of the program.
35 * That was so in historical code with the globally shared single string dope
36 * implementation, too. (And it still seems easier than bypassing to normal
37 * heap memory before _fixate() is called, today.)
39 * AutoReclaimedStorage memory is the follow-up to the historical "stringdope"
40 * allocator from 1979 (see [timeline:a7342d9]:src/Mail/strings.c), it is
41 * a steadily growing pool (but _relax_hold()..[:_relax_unroll():]..relax_gut()
42 * can be used to reduce pressure) until n_memory_reset() time.
44 * LastOutFirstIn memory is meant as an alloca(3) replacement but which requires
45 * lofi_free()ing pointers (otherwise growing until n_memory_reset()).
47 * TODO Flux heap memory is like LOFI except that any pointer can be freed (and
48 * TODO reused) at any time, just like normal heap memory. It is notational in
49 * TODO that it clearly states that the allocation will go away after a loop
50 * TODO tick, and also we can use some buffer caches.
53 /* Maximum allocation (directly) handled by A-R-Storage */
54 #define a_MEMORY_ARS_MAX (n_MEMORY_AUTOREC_SIZE / 2 + n_MEMORY_AUTOREC_SIZE / 4)
55 #define a_MEMORY_LOFI_MAX a_MEMORY_ARS_MAX
57 n_CTA(a_MEMORY_ARS_MAX
> 1024,
58 "Auto-reclaimed memory requires a larger buffer size"); /* Anway > 42! */
59 n_CTA(n_ISPOW2(n_MEMORY_AUTOREC_SIZE
),
60 "Buffers should be POW2 (may be wasteful on native allocators otherwise)");
62 /* Alignment of ARS memory. Simply go for pointer alignment */
63 #define a_MEMORY_ARS_ROUNDUP(S) n_ALIGN_SMALL(S)
64 #define a_MEMORY_LOFI_ROUNDUP(S) a_MEMORY_ARS_ROUNDUP(S)
66 #ifdef HAVE_MEMORY_DEBUG
67 n_CTA(sizeof(char) == sizeof(ui8_t
), "But POSIX says a byte is 8 bit");
69 # define a_MEMORY_HOPE_SIZE (2 * 8 * sizeof(char))
70 # define a_MEMORY_HOPE_INC(P) (P) += 8
71 # define a_MEMORY_HOPE_DEC(P) (P) -= 8
73 /* We use address-induced canary values, inspiration (but he didn't invent)
74 * and primes from maxv@netbsd.org, src/sys/kern/subr_kmem.c */
75 # define a_MEMORY_HOPE_LOWER(S,P) \
77 ui64_t __h__ = (uintptr_t)(P);\
78 __h__ *= ((ui64_t)0x9E37FFFFu << 32) | 0xFFFC0000u;\
83 # define a_MEMORY_HOPE_UPPER(S,P) \
86 ui64_t __x__, __h__ = (uintptr_t)(P);\
87 __h__ *= ((ui64_t)0x9E37FFFFu << 32) | 0xFFFC0000u;\
88 for(__i__ = 56; __i__ != 0; __i__ -= 8)\
89 if((__x__ = (__h__ >> __i__)) != 0){\
97 # define a_MEMORY_HOPE_SET(T,C) \
99 union a_memory_ptr __xp;\
100 struct a_memory_chunk *__xc;\
101 __xp.p_vp = (C).p_vp;\
102 __xc = (struct a_memory_chunk*)(__xp.T - 1);\
103 a_MEMORY_HOPE_INC((C).p_cp);\
104 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[0], &__xp.p_ui8p[0]);\
105 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[1], &__xp.p_ui8p[1]);\
106 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[2], &__xp.p_ui8p[2]);\
107 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[3], &__xp.p_ui8p[3]);\
108 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[4], &__xp.p_ui8p[4]);\
109 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[5], &__xp.p_ui8p[5]);\
110 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[6], &__xp.p_ui8p[6]);\
111 a_MEMORY_HOPE_LOWER(__xp.p_ui8p[7], &__xp.p_ui8p[7]);\
112 a_MEMORY_HOPE_INC(__xp.p_ui8p) + __xc->mc_user_size;\
113 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[0], &__xp.p_ui8p[0]);\
114 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[1], &__xp.p_ui8p[1]);\
115 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[2], &__xp.p_ui8p[2]);\
116 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[3], &__xp.p_ui8p[3]);\
117 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[4], &__xp.p_ui8p[4]);\
118 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[5], &__xp.p_ui8p[5]);\
119 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[6], &__xp.p_ui8p[6]);\
120 a_MEMORY_HOPE_UPPER(__xp.p_ui8p[7], &__xp.p_ui8p[7]);\
123 # define a_MEMORY_HOPE_GET_TRACE(T,C,BAD) \
125 a_MEMORY_HOPE_INC((C).p_cp);\
126 a_MEMORY_HOPE_GET(T, C, BAD);\
127 a_MEMORY_HOPE_INC((C).p_cp);\
130 # define a_MEMORY_HOPE_GET(T,C,BAD) \
132 union a_memory_ptr __xp;\
133 struct a_memory_chunk *__xc;\
136 __xp.p_vp = (C).p_vp;\
137 a_MEMORY_HOPE_DEC(__xp.p_cp);\
138 (C).p_cp = __xp.p_cp;\
139 __xc = (struct a_memory_chunk*)(__xp.T - 1);\
142 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[0]);\
143 if(__xp.p_ui8p[0] != __m) __i |= 1<<0;\
144 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[1]);\
145 if(__xp.p_ui8p[1] != __m) __i |= 1<<1;\
146 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[2]);\
147 if(__xp.p_ui8p[2] != __m) __i |= 1<<2;\
148 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[3]);\
149 if(__xp.p_ui8p[3] != __m) __i |= 1<<3;\
150 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[4]);\
151 if(__xp.p_ui8p[4] != __m) __i |= 1<<4;\
152 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[5]);\
153 if(__xp.p_ui8p[5] != __m) __i |= 1<<5;\
154 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[6]);\
155 if(__xp.p_ui8p[6] != __m) __i |= 1<<6;\
156 a_MEMORY_HOPE_LOWER(__m, &__xp.p_ui8p[7]);\
157 if(__xp.p_ui8p[7] != __m) __i |= 1<<7;\
160 a_MEMORY_HOPE_INC((C).p_cp);\
161 n_alert("%p: corrupt lower canary: 0x%02X: %s, line %d",\
162 (C).p_cp, __i, mdbg_file, mdbg_line);\
163 a_MEMORY_HOPE_DEC((C).p_cp);\
165 a_MEMORY_HOPE_INC(__xp.p_ui8p) + __xc->mc_user_size;\
167 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[0]);\
168 if(__xp.p_ui8p[0] != __m) __i |= 1<<0;\
169 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[1]);\
170 if(__xp.p_ui8p[1] != __m) __i |= 1<<1;\
171 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[2]);\
172 if(__xp.p_ui8p[2] != __m) __i |= 1<<2;\
173 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[3]);\
174 if(__xp.p_ui8p[3] != __m) __i |= 1<<3;\
175 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[4]);\
176 if(__xp.p_ui8p[4] != __m) __i |= 1<<4;\
177 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[5]);\
178 if(__xp.p_ui8p[5] != __m) __i |= 1<<5;\
179 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[6]);\
180 if(__xp.p_ui8p[6] != __m) __i |= 1<<6;\
181 a_MEMORY_HOPE_UPPER(__m, &__xp.p_ui8p[7]);\
182 if(__xp.p_ui8p[7] != __m) __i |= 1<<7;\
185 a_MEMORY_HOPE_INC((C).p_cp);\
186 n_alert("%p: corrupt upper canary: 0x%02X: %s, line %d",\
187 (C).p_cp, __i, mdbg_file, mdbg_line);\
188 a_MEMORY_HOPE_DEC((C).p_cp);\
191 n_alert(" ..canary last seen: %s, line %u",\
192 __xc->mc_file, __xc->mc_line);\
194 #endif /* HAVE_MEMORY_DEBUG */
196 #ifdef HAVE_MEMORY_DEBUG
197 struct a_memory_chunk
{
206 /* The heap memory n_free() may become delayed to detect double frees.
207 * It is primitive, but ok: speed and memory usage don't matter here */
208 struct a_memory_heap_chunk
{
209 struct a_memory_chunk mhc_super
;
210 struct a_memory_heap_chunk
*mhc_prev
;
211 struct a_memory_heap_chunk
*mhc_next
;
213 #endif /* HAVE_MEMORY_DEBUG */
215 struct a_memory_ars_lofi_chunk
{
216 #ifdef HAVE_MEMORY_DEBUG
217 struct a_memory_chunk malc_super
;
219 struct a_memory_ars_lofi_chunk
*malc_last
; /* Bit 1 set: it's a heap alloc */
226 #ifdef HAVE_MEMORY_DEBUG
227 struct a_memory_chunk
*p_c
;
228 struct a_memory_heap_chunk
*p_hc
;
230 struct a_memory_ars_lofi_chunk
*p_alc
;
233 struct a_memory_ars_ctx
{
234 struct a_memory_ars_ctx
*mac_outer
;
235 struct a_memory_ars_buffer
*mac_top
; /* Alloc stack */
236 struct a_memory_ars_buffer
*mac_full
; /* Alloc stack, cpl. filled */
237 size_t mac_recur
; /* _relax_create() recursion */
238 struct a_memory_ars_huge
*mac_huge
; /* Huge allocation bypass list */
239 struct a_memory_ars_lofi
*mac_lofi
; /* Pseudo alloca */
240 struct a_memory_ars_lofi_chunk
*mac_lofi_top
;
242 n_CTA(n_MEMORY_POOL_TYPE_SIZEOF
>= sizeof(struct a_memory_ars_ctx
),
243 "struct n_go_data_ctx.gdc_mempool is not large enough for memory pool");
245 struct a_memory_ars_buffer
{
246 struct a_memory_ars_buffer
*mab_last
;
247 char *mab_bot
; /* For _autorec_fixate(): keep startup memory lingering */
248 char *mab_relax
; /* If !NULL, used by _relax_unroll() instead of .mab_bot */
249 char *mab_caster
; /* Point of casting off memory */
250 char mab_buf
[n_MEMORY_AUTOREC_SIZE
- (4 * sizeof(void*))];
252 n_CTA(sizeof(struct a_memory_ars_buffer
) == n_MEMORY_AUTOREC_SIZE
,
253 "Resulting structure size is not the expected one");
254 #ifdef HAVE_MEMORY_DEBUG
255 n_CTA(a_MEMORY_ARS_MAX
+ a_MEMORY_HOPE_SIZE
+ sizeof(struct a_memory_chunk
)
256 < n_SIZEOF_FIELD(struct a_memory_ars_buffer
, mab_buf
),
257 "Memory layout of auto-reclaimed storage does not work out that way");
260 /* Requests that exceed a_MEMORY_ARS_MAX are always served by the normal
261 * memory allocator (which panics if memory cannot be served). This can be
262 * seen as a security fallback bypass only */
263 struct a_memory_ars_huge
{
264 struct a_memory_ars_huge
*mah_last
;
265 char mah_buf
[n_VFIELD_SIZE(a_MEMORY_ARS_ROUNDUP(1))];
268 struct a_memory_ars_lofi
{
269 struct a_memory_ars_lofi
*mal_last
;
272 char mal_buf
[n_VFIELD_SIZE(a_MEMORY_ARS_ROUNDUP(1))];
276 #ifdef HAVE_MEMORY_DEBUG
277 static size_t a_memory_heap_aall
, a_memory_heap_acur
, a_memory_heap_amax
,
278 a_memory_heap_mall
, a_memory_heap_mcur
, a_memory_heap_mmax
;
279 static struct a_memory_heap_chunk
*a_memory_heap_list
, *a_memory_heap_free
;
281 static size_t a_memory_ars_ball
, a_memory_ars_bcur
, a_memory_ars_bmax
,
282 a_memory_ars_hall
, a_memory_ars_hcur
, a_memory_ars_hmax
,
283 a_memory_ars_aall
, a_memory_ars_mall
;
285 static size_t a_memory_lofi_ball
, a_memory_lofi_bcur
, a_memory_lofi_bmax
,
286 a_memory_lofi_aall
, a_memory_lofi_acur
, a_memory_lofi_amax
,
287 a_memory_lofi_mall
, a_memory_lofi_mcur
, a_memory_lofi_mmax
;
291 SINLINE
void a_memory_lofi_free(struct a_memory_ars_ctx
*macp
, void *vp
);
293 /* Reset an ars_ctx */
294 static void a_memory_ars_reset(struct a_memory_ars_ctx
*macp
);
297 a_memory_lofi_free(struct a_memory_ars_ctx
*macp
, void *vp
){
298 struct a_memory_ars_lofi
*malp
;
299 union a_memory_ptr p
;
303 #ifdef HAVE_MEMORY_DEBUG
304 --a_memory_lofi_acur
;
305 a_memory_lofi_mcur
-= p
.p_c
->mc_user_size
;
308 /* The heap allocations are released immediately */
309 if((uintptr_t)p
.p_alc
->malc_last
& 0x1){
310 malp
= macp
->mac_lofi
;
311 macp
->mac_lofi
= malp
->mal_last
;
312 macp
->mac_lofi_top
= (struct a_memory_ars_lofi_chunk
*)
313 ((uintptr_t)p
.p_alc
->malc_last
& ~0x1);
315 #ifdef HAVE_MEMORY_DEBUG
316 --a_memory_lofi_bcur
;
319 macp
->mac_lofi_top
= p
.p_alc
->malc_last
;
321 /* The normal arena ones only if the arena is empty, except for when
322 * it is the last - that we'll keep until _pool_pop() or exit(3) */
323 if(p
.p_cp
== (malp
= macp
->mac_lofi
)->mal_buf
){
324 if(malp
->mal_last
!= NULL
){
325 macp
->mac_lofi
= malp
->mal_last
;
327 #ifdef HAVE_MEMORY_DEBUG
328 --a_memory_lofi_bcur
;
332 malp
->mal_caster
= p
.p_cp
;
338 a_memory_ars_reset(struct a_memory_ars_ctx
*macp
){
340 struct a_memory_ars_lofi_chunk
*alcp
;
341 struct a_memory_ars_lofi
*alp
;
342 struct a_memory_ars_buffer
*abp
;
343 struct a_memory_ars_huge
*ahp
;
347 /* Simply move all buffers away from .mac_full */
348 for(m
.abp
= macp
->mac_full
; m
.abp
!= NULL
; m
.abp
= m2
.abp
){
349 m2
.abp
= m
.abp
->mab_last
;
350 m
.abp
->mab_last
= macp
->mac_top
;
351 macp
->mac_top
= m
.abp
;
353 macp
->mac_full
= NULL
;
355 for(m2
.abp
= NULL
, m
.abp
= macp
->mac_top
; m
.abp
!= NULL
;){
356 struct a_memory_ars_buffer
*x
;
359 m
.abp
= m
.abp
->mab_last
;
361 /* Give away all buffers that are not covered by autorec_fixate() */
362 if(x
->mab_bot
== x
->mab_buf
){
364 macp
->mac_top
= m
.abp
;
366 m2
.abp
->mab_last
= m
.abp
;
368 #ifdef HAVE_MEMORY_DEBUG
373 x
->mab_caster
= x
->mab_bot
;
375 #ifdef HAVE_MEMORY_DEBUG
376 memset(x
->mab_caster
, 0377,
377 PTR2SIZE(&x
->mab_buf
[sizeof(x
->mab_buf
)] - x
->mab_caster
));
382 while((m
.ahp
= macp
->mac_huge
) != NULL
){
383 macp
->mac_huge
= m
.ahp
->mah_last
;
385 #ifdef HAVE_MEMORY_DEBUG
390 /* "alloca(3)" memory goes away, too. XXX Must be last as long we jump */
391 #ifdef HAVE_MEMORY_DEBUG
392 if(macp
->mac_lofi_top
!= NULL
)
393 n_alert("There still is LOFI memory upon ARS reset!");
395 while((m
.alcp
= macp
->mac_lofi_top
) != NULL
)
396 a_memory_lofi_free(macp
, m
.alcp
);
401 n_memory_reset(void){
402 #ifdef HAVE_MEMORY_DEBUG
403 union a_memory_ptr p
;
406 struct a_memory_ars_ctx
*macp
;
411 if((macp
= n_go_data
->gdc_mempool
) != NULL
){
412 /* First of all reset auto-reclaimed storage so that heap freed during
413 * this can be handled in a second step */
414 /* TODO v15 active recursion can only happen after a jump */
415 if(macp
->mac_recur
> 0){
417 n_autorec_relax_gut();
419 a_memory_ars_reset(macp
);
422 /* Now we are ready to deal with heap */
423 #ifdef HAVE_MEMORY_DEBUG
426 for(p
.p_hc
= a_memory_heap_free
; p
.p_hc
!= NULL
;){
432 p
.p_hc
= p
.p_hc
->mhc_next
;
435 a_memory_heap_free
= NULL
;
437 if((n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)) && c
> 0)
438 n_err("memreset: freed %" PRIuZ
" chunks/%" PRIuZ
" bytes\n", c
, s
);
444 n_memory_pool_fixate(void){
445 struct a_memory_ars_buffer
*mabp
;
446 struct a_memory_ars_ctx
*macp
;
449 if((macp
= n_go_data
->gdc_mempool
) != NULL
){
450 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
451 mabp
->mab_bot
= mabp
->mab_caster
;
452 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
453 mabp
->mab_bot
= mabp
->mab_caster
;
459 n_memory_pool_push(void *vp
){
460 struct a_memory_ars_ctx
*macp
;
463 if(n_go_data
->gdc_mempool
== NULL
)
464 n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
466 memset(macp
= vp
, 0, sizeof *macp
);
467 macp
->mac_outer
= n_go_data
->gdc_mempool
;
468 n_go_data
->gdc_mempool
= macp
;
473 n_memory_pool_pop(void *vp
){
474 struct a_memory_ars_buffer
*mabp
;
475 struct a_memory_ars_ctx
*macp
;
480 if((macp
= vp
) == NULL
){
481 macp
= n_go_data
->gdc_mempool
;
482 assert(macp
!= NULL
);
484 /* XXX May not be ARS top upon jump */
485 while(n_go_data
->gdc_mempool
!= macp
){
486 DBG( n_err("ARS pop %p to reach freed context\n",
487 n_go_data
->gdc_mempool
); )
488 n_memory_pool_pop(n_go_data
->gdc_mempool
);
491 n_go_data
->gdc_mempool
= macp
->mac_outer
;
493 a_memory_ars_reset(macp
);
494 assert(macp
->mac_full
== NULL
);
495 assert(macp
->mac_huge
== NULL
);
497 mabp
= macp
->mac_top
;
498 macp
->mac_top
= NULL
;
501 mabp
= mabp
->mab_last
;
505 /* We (may) have kept one buffer for our pseudo alloca(3) */
506 if((vp
= macp
->mac_lofi
) != NULL
){
507 assert(macp
->mac_lofi
->mal_last
== NULL
);
508 macp
->mac_lofi
= NULL
;
509 #ifdef HAVE_MEMORY_DEBUG
510 --a_memory_lofi_bcur
;
517 #ifndef HAVE_MEMORY_DEBUG
525 if((rv
= malloc(s
)) == NULL
)
526 n_panic(_("no memory"));
532 n_realloc(void *vp
, size_t s
){
541 if((rv
= realloc(vp
, s
)) == NULL
)
542 n_panic(_("no memory"));
549 n_calloc(size_t nmemb
, size_t size
){
555 if((rv
= calloc(nmemb
, size
)) == NULL
)
556 n_panic(_("no memory"));
568 #else /* !HAVE_MEMORY_DEBUG */
570 (n_alloc
)(size_t s n_MEMORY_DEBUG_ARGS
){
571 union a_memory_ptr p
;
575 if(s
> UI32_MAX
- sizeof(struct a_memory_heap_chunk
) - a_MEMORY_HOPE_SIZE
)
576 n_panic("n_alloc(): allocation too large: %s, line %d",
577 mdbg_file
, mdbg_line
);
578 if((user_s
= (ui32_t
)s
) == 0)
580 s
+= sizeof(struct a_memory_heap_chunk
) + a_MEMORY_HOPE_SIZE
;
582 if((p
.p_vp
= (malloc
)(s
)) == NULL
)
583 n_panic(_("no memory"));
585 p
.p_hc
->mhc_prev
= NULL
;
586 if((p
.p_hc
->mhc_next
= a_memory_heap_list
) != NULL
)
587 a_memory_heap_list
->mhc_prev
= p
.p_hc
;
589 p
.p_c
->mc_file
= mdbg_file
;
590 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
591 p
.p_c
->mc_isfree
= FAL0
;
592 p
.p_c
->mc_user_size
= user_s
;
593 p
.p_c
->mc_size
= (ui32_t
)s
;
595 a_memory_heap_list
= p
.p_hc
++;
596 a_MEMORY_HOPE_SET(p_hc
, p
);
598 ++a_memory_heap_aall
;
599 ++a_memory_heap_acur
;
600 a_memory_heap_amax
= n_MAX(a_memory_heap_amax
, a_memory_heap_acur
);
601 a_memory_heap_mall
+= user_s
;
602 a_memory_heap_mcur
+= user_s
;
603 a_memory_heap_mmax
= n_MAX(a_memory_heap_mmax
, a_memory_heap_mcur
);
609 (n_realloc
)(void *vp
, size_t s n_MEMORY_DEBUG_ARGS
){
610 union a_memory_ptr p
;
615 if((p
.p_vp
= vp
) == NULL
){
617 p
.p_vp
= (n_alloc
)(s
, mdbg_file
, mdbg_line
);
621 a_MEMORY_HOPE_GET(p_hc
, p
, isbad
);
624 if(p
.p_c
->mc_isfree
){
625 n_err("n_realloc(): region freed! At %s, line %d\n"
626 "\tLast seen: %s, line %" PRIu16
"\n",
627 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
631 if(p
.p_hc
== a_memory_heap_list
)
632 a_memory_heap_list
= p
.p_hc
->mhc_next
;
634 p
.p_hc
->mhc_prev
->mhc_next
= p
.p_hc
->mhc_next
;
635 if (p
.p_hc
->mhc_next
!= NULL
)
636 p
.p_hc
->mhc_next
->mhc_prev
= p
.p_hc
->mhc_prev
;
638 --a_memory_heap_acur
;
639 a_memory_heap_mcur
-= p
.p_c
->mc_user_size
;
641 if(s
> UI32_MAX
- sizeof(struct a_memory_heap_chunk
) - a_MEMORY_HOPE_SIZE
)
642 n_panic("n_realloc(): allocation too large: %s, line %d",
643 mdbg_file
, mdbg_line
);
644 if((user_s
= (ui32_t
)s
) == 0)
646 s
+= sizeof(struct a_memory_heap_chunk
) + a_MEMORY_HOPE_SIZE
;
648 if((p
.p_vp
= (realloc
)(p
.p_c
, s
)) == NULL
)
649 n_panic(_("no memory"));
650 p
.p_hc
->mhc_prev
= NULL
;
651 if((p
.p_hc
->mhc_next
= a_memory_heap_list
) != NULL
)
652 a_memory_heap_list
->mhc_prev
= p
.p_hc
;
654 p
.p_c
->mc_file
= mdbg_file
;
655 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
656 p
.p_c
->mc_isfree
= FAL0
;
657 p
.p_c
->mc_user_size
= user_s
;
658 p
.p_c
->mc_size
= (ui32_t
)s
;
660 a_memory_heap_list
= p
.p_hc
++;
661 a_MEMORY_HOPE_SET(p_hc
, p
);
663 ++a_memory_heap_aall
;
664 ++a_memory_heap_acur
;
665 a_memory_heap_amax
= n_MAX(a_memory_heap_amax
, a_memory_heap_acur
);
666 a_memory_heap_mall
+= user_s
;
667 a_memory_heap_mcur
+= user_s
;
668 a_memory_heap_mmax
= n_MAX(a_memory_heap_mmax
, a_memory_heap_mcur
);
675 (n_calloc
)(size_t nmemb
, size_t size n_MEMORY_DEBUG_ARGS
){
676 union a_memory_ptr p
;
682 if(size
> UI32_MAX
- sizeof(struct a_memory_heap_chunk
) - a_MEMORY_HOPE_SIZE
)
683 n_panic("n_calloc(): allocation size too large: %s, line %d",
684 mdbg_file
, mdbg_line
);
685 if((user_s
= (ui32_t
)size
) == 0)
687 if((UI32_MAX
- sizeof(struct a_memory_heap_chunk
) - a_MEMORY_HOPE_SIZE
) /
689 n_panic("n_calloc(): allocation count too large: %s, line %d",
690 mdbg_file
, mdbg_line
);
693 size
+= sizeof(struct a_memory_heap_chunk
) + a_MEMORY_HOPE_SIZE
;
695 if((p
.p_vp
= (malloc
)(size
)) == NULL
)
696 n_panic(_("no memory"));
697 memset(p
.p_vp
, 0, size
);
699 p
.p_hc
->mhc_prev
= NULL
;
700 if((p
.p_hc
->mhc_next
= a_memory_heap_list
) != NULL
)
701 a_memory_heap_list
->mhc_prev
= p
.p_hc
;
703 p
.p_c
->mc_file
= mdbg_file
;
704 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
705 p
.p_c
->mc_isfree
= FAL0
;
706 p
.p_c
->mc_user_size
= (user_s
> 0) ? user_s
*= nmemb
: 0;
707 p
.p_c
->mc_size
= (ui32_t
)size
;
709 a_memory_heap_list
= p
.p_hc
++;
710 a_MEMORY_HOPE_SET(p_hc
, p
);
712 ++a_memory_heap_aall
;
713 ++a_memory_heap_acur
;
714 a_memory_heap_amax
= n_MAX(a_memory_heap_amax
, a_memory_heap_acur
);
715 a_memory_heap_mall
+= user_s
;
716 a_memory_heap_mcur
+= user_s
;
717 a_memory_heap_mmax
= n_MAX(a_memory_heap_mmax
, a_memory_heap_mcur
);
723 (n_free
)(void *vp n_MEMORY_DEBUG_ARGS
){
724 union a_memory_ptr p
;
728 if((p
.p_vp
= vp
) == NULL
){
729 n_err("n_free(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
733 a_MEMORY_HOPE_GET(p_hc
, p
, isbad
);
736 if(p
.p_c
->mc_isfree
){
737 n_err("n_free(): double-free avoided at %s, line %d\n"
738 "\tLast seen: %s, line %" PRIu16
"\n",
739 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
743 if(p
.p_hc
== a_memory_heap_list
){
744 if((a_memory_heap_list
= p
.p_hc
->mhc_next
) != NULL
)
745 a_memory_heap_list
->mhc_prev
= NULL
;
747 p
.p_hc
->mhc_prev
->mhc_next
= p
.p_hc
->mhc_next
;
748 if(p
.p_hc
->mhc_next
!= NULL
)
749 p
.p_hc
->mhc_next
->mhc_prev
= p
.p_hc
->mhc_prev
;
751 p
.p_c
->mc_isfree
= TRU1
;
752 /* Trash contents (also see [21c05f8]) */
753 memset(vp
, 0377, p
.p_c
->mc_user_size
);
755 --a_memory_heap_acur
;
756 a_memory_heap_mcur
-= p
.p_c
->mc_user_size
;
758 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)){
759 p
.p_hc
->mhc_next
= a_memory_heap_free
;
760 a_memory_heap_free
= p
.p_hc
;
766 #endif /* HAVE_MEMORY_DEBUG */
769 (n_autorec_alloc_from_pool
)(void *vp
, size_t size n_MEMORY_DEBUG_ARGS
){
770 #ifdef HAVE_MEMORY_DEBUG
773 union a_memory_ptr p
;
775 struct a_memory_ars_buffer
*abp
;
776 struct a_memory_ars_huge
*ahp
;
778 struct a_memory_ars_ctx
*macp
;
781 if((macp
= vp
) == NULL
&& (macp
= n_go_data
->gdc_mempool
) == NULL
)
782 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
784 #ifdef HAVE_MEMORY_DEBUG
785 user_s
= (ui32_t
)size
;
789 #ifdef HAVE_MEMORY_DEBUG
790 size
+= sizeof(struct a_memory_chunk
) + a_MEMORY_HOPE_SIZE
;
792 size
= a_MEMORY_ARS_ROUNDUP(size
);
794 /* Huge allocations are special */
795 if(n_UNLIKELY(size
> a_MEMORY_ARS_MAX
)){
796 #ifdef HAVE_MEMORY_DEBUG
797 n_alert("n_autorec_alloc() of %" PRIuZ
" bytes from %s, line %d",
798 size
, mdbg_file
, mdbg_line
);
803 /* Search for a buffer with enough free space to serve request */
804 for(m2
.abp
= NULL
, m
.abp
= macp
->mac_top
; m
.abp
!= NULL
;
805 m2
.abp
= m
.abp
, m
.abp
= m
.abp
->mab_last
){
806 if((p
.p_cp
= m
.abp
->mab_caster
) <=
807 &m
.abp
->mab_buf
[sizeof(m
.abp
->mab_buf
) - size
]){
808 /* Alignment is the one thing, the other is what is usually allocated,
809 * and here about 40 bytes seems to be a good cut to avoid non-usable
810 * casters. Reown buffers supposed to be "full" to .mac_full */
811 if(n_UNLIKELY((m
.abp
->mab_caster
= &p
.p_cp
[size
]) >=
812 &m
.abp
->mab_buf
[sizeof(m
.abp
->mab_buf
) - 42])){
814 macp
->mac_top
= m
.abp
->mab_last
;
816 m2
.abp
->mab_last
= m
.abp
->mab_last
;
817 m
.abp
->mab_last
= macp
->mac_full
;
818 macp
->mac_full
= m
.abp
;
824 /* Need a new buffer XXX "page" pool */
825 m
.abp
= n_alloc(sizeof *m
.abp
);
826 m
.abp
->mab_last
= macp
->mac_top
;
827 m
.abp
->mab_caster
= &(m
.abp
->mab_bot
= m
.abp
->mab_buf
)[size
];
828 m
.abp
->mab_relax
= NULL
; /* Indicates allocation after _relax_create() */
829 macp
->mac_top
= m
.abp
;
830 p
.p_cp
= m
.abp
->mab_bot
;
832 #ifdef HAVE_MEMORY_DEBUG
835 a_memory_ars_bmax
= n_MAX(a_memory_ars_bmax
, a_memory_ars_bcur
);
839 #ifdef HAVE_MEMORY_DEBUG
840 p
.p_c
->mc_file
= mdbg_file
;
841 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
842 p
.p_c
->mc_user_size
= user_s
;
843 p
.p_c
->mc_size
= (ui32_t
)size
;
845 a_MEMORY_HOPE_SET(p_c
, p
);
848 a_memory_ars_mall
+= user_s
;
854 m
.ahp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_memory_ars_huge
, mah_buf
) + size
);
855 m
.ahp
->mah_last
= macp
->mac_huge
;
856 macp
->mac_huge
= m
.ahp
;
857 p
.p_cp
= m
.ahp
->mah_buf
;
858 #ifdef HAVE_MEMORY_DEBUG
861 a_memory_ars_hmax
= n_MAX(a_memory_ars_hmax
, a_memory_ars_hcur
);
867 (n_autorec_calloc_from_pool
)(void *vp
, size_t nmemb
, size_t size
868 n_MEMORY_DEBUG_ARGS
){
872 size
*= nmemb
; /* XXX overflow, but only used for struct inits */
873 rv
= (n_autorec_alloc_from_pool
)(vp
, size n_MEMORY_DEBUG_ARGSCALL
);
880 n_autorec_relax_create(void){
881 struct a_memory_ars_ctx
*macp
;
884 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
885 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
887 if(macp
->mac_recur
++ == 0){
888 struct a_memory_ars_buffer
*mabp
;
890 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
891 mabp
->mab_relax
= mabp
->mab_caster
;
892 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
893 mabp
->mab_relax
= mabp
->mab_caster
;
897 n_err("n_autorec_relax_create(): recursion >0\n");
903 n_autorec_relax_gut(void){
904 struct a_memory_ars_ctx
*macp
;
907 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
908 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
910 assert(macp
->mac_recur
> 0);
912 if(--macp
->mac_recur
== 0){
913 struct a_memory_ars_buffer
*mabp
;
916 n_autorec_relax_unroll();
919 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
920 mabp
->mab_relax
= NULL
;
921 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
922 mabp
->mab_relax
= NULL
;
926 n_err("n_autorec_relax_unroll(): recursion >0\n");
932 n_autorec_relax_unroll(void){
933 /* The purpose of relaxation is only that it is possible to reset the
934 * casters, *not* to give back memory to the system. We are presumably in
935 * an iteration over all messages of a mailbox, and it'd be quite
936 * counterproductive to give the system allocator a chance to waste time */
937 struct a_memory_ars_ctx
*macp
;
940 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
941 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
943 assert(macp
->mac_recur
> 0);
946 if(macp
->mac_recur
== 1){
947 struct a_memory_ars_buffer
*mabp
, *x
, *y
;
949 /* Buffers in the full list may become usable again! */
950 for(x
= NULL
, mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= y
){
953 if(mabp
->mab_relax
== NULL
||
954 mabp
->mab_relax
< &mabp
->mab_buf
[sizeof(mabp
->mab_buf
) - 42]){
959 mabp
->mab_last
= macp
->mac_top
;
960 macp
->mac_top
= mabp
;
965 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
966 mabp
->mab_caster
= (mabp
->mab_relax
!= NULL
)
967 ? mabp
->mab_relax
: mabp
->mab_bot
;
968 #ifdef HAVE_MEMORY_DEBUG
969 memset(mabp
->mab_caster
, 0377,
970 PTR2SIZE(&mabp
->mab_buf
[sizeof(mabp
->mab_buf
)] - mabp
->mab_caster
));
978 (n_lofi_alloc
)(size_t size n_MEMORY_DEBUG_ARGS
){
979 #ifdef HAVE_MEMORY_DEBUG
982 union a_memory_ptr p
;
983 struct a_memory_ars_lofi
*malp
;
985 struct a_memory_ars_ctx
*macp
;
988 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
989 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
991 #ifdef HAVE_MEMORY_DEBUG
992 user_s
= (ui32_t
)size
;
996 size
+= sizeof(struct a_memory_ars_lofi_chunk
);
997 #ifdef HAVE_MEMORY_DEBUG
998 size
+= a_MEMORY_HOPE_SIZE
;
1000 size
= a_MEMORY_LOFI_ROUNDUP(size
);
1002 /* Huge allocations are special */
1003 if(n_UNLIKELY(isheap
= (size
> a_MEMORY_LOFI_MAX
))){
1004 #ifdef HAVE_MEMORY_DEBUG
1005 n_alert("n_lofi_alloc() of %" PRIuZ
" bytes from %s, line %d",
1006 size
, mdbg_file
, mdbg_line
);
1008 }else if((malp
= macp
->mac_lofi
) != NULL
&&
1009 ((p
.p_cp
= malp
->mal_caster
) <= &malp
->mal_max
[-size
])){
1010 malp
->mal_caster
= &p
.p_cp
[size
];
1014 /* Need a new buffer */
1018 i
= n_VSTRUCT_SIZEOF(struct a_memory_ars_lofi
, mal_buf
) + size
;
1019 i
= n_MAX(i
, n_MEMORY_AUTOREC_SIZE
);
1021 malp
->mal_last
= macp
->mac_lofi
;
1022 malp
->mal_caster
= &malp
->mal_buf
[size
];
1023 i
-= n_VSTRUCT_SIZEOF(struct a_memory_ars_lofi
, mal_buf
);
1024 malp
->mal_max
= &malp
->mal_buf
[i
];
1025 macp
->mac_lofi
= malp
;
1026 p
.p_cp
= malp
->mal_buf
;
1028 #ifdef HAVE_MEMORY_DEBUG
1029 ++a_memory_lofi_ball
;
1030 ++a_memory_lofi_bcur
;
1031 a_memory_lofi_bmax
= n_MAX(a_memory_lofi_bmax
, a_memory_lofi_bcur
);
1036 p
.p_alc
->malc_last
= macp
->mac_lofi_top
;
1037 macp
->mac_lofi_top
= p
.p_alc
;
1039 p
.p_alc
->malc_last
= (struct a_memory_ars_lofi_chunk
*)
1040 ((uintptr_t)p
.p_alc
->malc_last
| 0x1);
1042 #ifndef HAVE_MEMORY_DEBUG
1045 p
.p_c
->mc_file
= mdbg_file
;
1046 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
1047 p
.p_c
->mc_isfree
= FAL0
;
1048 p
.p_c
->mc_user_size
= user_s
;
1049 p
.p_c
->mc_size
= (ui32_t
)size
;
1051 a_MEMORY_HOPE_SET(p_alc
, p
);
1053 ++a_memory_lofi_aall
;
1054 ++a_memory_lofi_acur
;
1055 a_memory_lofi_amax
= n_MAX(a_memory_lofi_amax
, a_memory_lofi_acur
);
1056 a_memory_lofi_mall
+= user_s
;
1057 a_memory_lofi_mcur
+= user_s
;
1058 a_memory_lofi_mmax
= n_MAX(a_memory_lofi_mmax
, a_memory_lofi_mcur
);
1065 (n_lofi_free
)(void *vp n_MEMORY_DEBUG_ARGS
){
1066 #ifdef HAVE_MEMORY_DEBUG
1069 union a_memory_ptr p
;
1070 struct a_memory_ars_ctx
*macp
;
1073 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1074 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1076 if((p
.p_vp
= vp
) == NULL
){
1077 #ifdef HAVE_MEMORY_DEBUG
1078 n_err("n_lofi_free(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
1083 #ifdef HAVE_MEMORY_DEBUG
1084 a_MEMORY_HOPE_GET(p_alc
, p
, isbad
);
1087 if(p
.p_c
->mc_isfree
){
1088 n_err("n_lofi_free(): double-free avoided at %s, line %d\n"
1089 "\tLast seen: %s, line %" PRIu16
"\n",
1090 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1093 p
.p_c
->mc_isfree
= TRU1
;
1094 memset(vp
, 0377, p
.p_c
->mc_user_size
);
1096 if(p
.p_alc
!= macp
->mac_lofi_top
){
1097 n_err("n_lofi_free(): this is not alloca top at %s, line %d\n"
1098 "\tLast seen: %s, line %" PRIu16
"\n",
1099 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1104 #endif /* HAVE_MEMORY_DEBUG */
1106 a_memory_lofi_free(macp
, --p
.p_alc
);
1112 n_lofi_snap_create(void){ /* TODO avoid temporary alloc */
1116 rv
= n_lofi_alloc(1);
1122 n_lofi_snap_unroll(void *cookie
){ /* TODO optimise */
1123 union a_memory_ptr p
;
1124 struct a_memory_ars_ctx
*macp
;
1129 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1130 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1133 p
.p_alc
= macp
->mac_lofi_top
;
1134 a_memory_lofi_free(macp
, p
.p_vp
);
1136 #ifdef HAVE_MEMORY_DEBUG
1137 a_MEMORY_HOPE_INC(p
.p_ui8p
);
1139 if(p
.p_vp
== cookie
)
1145 #ifdef HAVE_MEMORY_DEBUG
1147 c_memtrace(void *vp
){
1148 /* For a_MEMORY_HOPE_GET() */
1149 char const * const mdbg_file
= "memtrace()";
1150 int const mdbg_line
= -1;
1151 struct a_memory_ars_buffer
*mabp
;
1152 struct a_memory_ars_lofi_chunk
*malcp
;
1153 struct a_memory_ars_lofi
*malp
;
1154 struct a_memory_ars_ctx
*macp
;
1156 union a_memory_ptr p
, xp
;
1162 if((fp
= Ftmp(NULL
, "memtr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1163 n_perr("tmpfile", 0);
1169 "Last-Out-First-In (alloca) storage:\n"
1170 " Buffer cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1171 " Allocations cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1172 " Bytes cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n\n",
1173 a_memory_lofi_bcur
, a_memory_lofi_bmax
, a_memory_lofi_ball
,
1174 a_memory_lofi_acur
, a_memory_lofi_amax
, a_memory_lofi_aall
,
1175 a_memory_lofi_mcur
, a_memory_lofi_mmax
, a_memory_lofi_mall
);
1178 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1179 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1180 for(; macp
!= NULL
; macp
= macp
->mac_outer
){
1181 fprintf(fp
, " Evaluation stack context %p (outer: %p):\n",
1182 (void*)macp
, (void*)macp
->mac_outer
);
1185 for(malp
= macp
->mac_lofi
; malp
!= NULL
;){
1186 fprintf(fp
, " Buffer %p%s, %" PRIuZ
"/%" PRIuZ
" used/free:\n",
1187 (void*)malp
, ((uintptr_t)malp
->mal_last
& 0x1 ? " (huge)" : ""),
1188 PTR2SIZE(malp
->mal_caster
- &malp
->mal_buf
[0]),
1189 PTR2SIZE(malp
->mal_max
- malp
->mal_caster
));
1191 malp
= malp
->mal_last
;
1192 malp
= (struct a_memory_ars_lofi
*)((uintptr_t)malp
& ~1);
1195 for(malcp
= macp
->mac_lofi_top
; malcp
!= NULL
;){
1197 malcp
= (struct a_memory_ars_lofi_chunk
*)
1198 ((uintptr_t)malcp
->malc_last
& ~0x1);
1201 a_MEMORY_HOPE_GET_TRACE(p_alc
, xp
, isbad
);
1202 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1203 (isbad
? "! CANARY ERROR (LOFI): " : ""), xp
.p_vp
,
1204 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1209 "\nAuto-reclaimed storage:\n"
1210 " Buffers cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1211 " Huge allocations cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1212 " Allocations all: %" PRIuZ
", Bytes all: %" PRIuZ
"\n\n",
1213 a_memory_ars_bcur
, a_memory_ars_bmax
, a_memory_ars_ball
,
1214 a_memory_ars_hcur
, a_memory_ars_hmax
, a_memory_ars_hall
,
1215 a_memory_ars_aall
, a_memory_ars_mall
);
1218 for(macp
= n_go_data
->gdc_mempool
; macp
!= NULL
; macp
= macp
->mac_outer
){
1219 fprintf(fp
, " Evaluation stack context %p (outer: %p):\n",
1220 (void*)macp
, (void*)macp
->mac_outer
);
1223 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1224 fprintf(fp
, " Buffer %p, %" PRIuZ
"/%" PRIuZ
" used/free:\n",
1226 PTR2SIZE(mabp
->mab_caster
- &mabp
->mab_buf
[0]),
1227 PTR2SIZE(&mabp
->mab_buf
[sizeof(mabp
->mab_buf
)] - mabp
->mab_caster
));
1230 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1231 ++lines
, p
.p_cp
+= p
.p_c
->mc_size
){
1234 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1235 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1236 (isbad
? "! CANARY ERROR (ARS, top): " : ""), xp
.p_vp
,
1237 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1242 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1243 fprintf(fp
, " Buffer %p, full:\n", (void*)mabp
);
1246 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1247 ++lines
, p
.p_cp
+= p
.p_c
->mc_size
){
1250 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1251 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1252 (isbad
? "! CANARY ERROR (ARS, full): " : ""), xp
.p_vp
,
1253 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1260 "\nHeap memory buffers:\n"
1261 " Allocation cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1262 " Bytes cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n\n",
1263 a_memory_heap_acur
, a_memory_heap_amax
, a_memory_heap_aall
,
1264 a_memory_heap_mcur
, a_memory_heap_mmax
, a_memory_heap_mall
);
1267 for(p
.p_hc
= a_memory_heap_list
; p
.p_hc
!= NULL
;
1268 ++lines
, p
.p_hc
= p
.p_hc
->mhc_next
){
1271 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1272 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1273 (isbad
? "! CANARY ERROR (heap): " : ""), xp
.p_vp
,
1274 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1277 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)){
1278 fprintf(fp
, "Heap buffers lingering for n_free():\n");
1281 for(p
.p_hc
= a_memory_heap_free
; p
.p_hc
!= NULL
;
1282 ++lines
, p
.p_hc
= p
.p_hc
->mhc_next
){
1285 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1286 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1287 (isbad
? "! CANARY ERROR (free): " : ""), xp
.p_vp
,
1288 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1292 page_or_print(fp
, lines
);
1297 return (vp
!= NULL
);
1301 n__memory_check(char const *mdbg_file
, int mdbg_line
){
1302 union a_memory_ptr p
, xp
;
1303 struct a_memory_ars_buffer
*mabp
;
1304 struct a_memory_ars_lofi_chunk
*malcp
;
1305 struct a_memory_ars_ctx
*macp
;
1306 bool_t anybad
, isbad
;
1311 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1312 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1316 for(malcp
= macp
->mac_lofi_top
; malcp
!= NULL
;){
1318 malcp
= (struct a_memory_ars_lofi_chunk
*)
1319 ((uintptr_t)malcp
->malc_last
& ~0x1);
1322 a_MEMORY_HOPE_GET_TRACE(p_alc
, xp
, isbad
);
1326 "! CANARY ERROR (LOFI): %p (%u bytes): %s, line %u\n",
1327 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1331 /* Auto-reclaimed */
1333 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1334 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1335 p
.p_cp
+= p
.p_c
->mc_size
){
1338 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1342 "! CANARY ERROR (ARS, top): %p (%u bytes): %s, line %u\n",
1343 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1348 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1349 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1350 p
.p_cp
+= p
.p_c
->mc_size
){
1353 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1357 "! CANARY ERROR (ARS, full): %p (%u bytes): %s, line %u\n",
1358 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1365 for(p
.p_hc
= a_memory_heap_list
; p
.p_hc
!= NULL
; p
.p_hc
= p
.p_hc
->mhc_next
){
1368 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1372 "! CANARY ERROR (heap): %p (%u bytes): %s, line %u\n",
1373 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1377 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)){
1378 for(p
.p_hc
= a_memory_heap_free
; p
.p_hc
!= NULL
;
1379 p
.p_hc
= p
.p_hc
->mhc_next
){
1382 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1386 "! CANARY ERROR (free): %p (%u bytes): %s, line %u\n",
1387 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1392 if(anybad
&& ok_blook(memdebug
))
1393 n_panic("Memory errors encountered");
1397 #endif /* HAVE_MEMORY_DEBUG */