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
&& (n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)))
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 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
))
798 n_alert("n_autorec_alloc() of %" PRIuZ
" bytes from %s, line %d",
799 size
, mdbg_file
, mdbg_line
);
804 /* Search for a buffer with enough free space to serve request */
805 for(m2
.abp
= NULL
, m
.abp
= macp
->mac_top
; m
.abp
!= NULL
;
806 m2
.abp
= m
.abp
, m
.abp
= m
.abp
->mab_last
){
807 if((p
.p_cp
= m
.abp
->mab_caster
) <=
808 &m
.abp
->mab_buf
[sizeof(m
.abp
->mab_buf
) - size
]){
809 /* Alignment is the one thing, the other is what is usually allocated,
810 * and here about 40 bytes seems to be a good cut to avoid non-usable
811 * casters. Reown buffers supposed to be "full" to .mac_full */
812 if(n_UNLIKELY((m
.abp
->mab_caster
= &p
.p_cp
[size
]) >=
813 &m
.abp
->mab_buf
[sizeof(m
.abp
->mab_buf
) - 42])){
815 macp
->mac_top
= m
.abp
->mab_last
;
817 m2
.abp
->mab_last
= m
.abp
->mab_last
;
818 m
.abp
->mab_last
= macp
->mac_full
;
819 macp
->mac_full
= m
.abp
;
825 /* Need a new buffer XXX "page" pool */
826 m
.abp
= n_alloc(sizeof *m
.abp
);
827 m
.abp
->mab_last
= macp
->mac_top
;
828 m
.abp
->mab_caster
= &(m
.abp
->mab_bot
= m
.abp
->mab_buf
)[size
];
829 m
.abp
->mab_relax
= NULL
; /* Indicates allocation after _relax_create() */
830 macp
->mac_top
= m
.abp
;
831 p
.p_cp
= m
.abp
->mab_bot
;
833 #ifdef HAVE_MEMORY_DEBUG
836 a_memory_ars_bmax
= n_MAX(a_memory_ars_bmax
, a_memory_ars_bcur
);
840 #ifdef HAVE_MEMORY_DEBUG
841 p
.p_c
->mc_file
= mdbg_file
;
842 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
843 p
.p_c
->mc_user_size
= user_s
;
844 p
.p_c
->mc_size
= (ui32_t
)size
;
846 a_MEMORY_HOPE_SET(p_c
, p
);
849 a_memory_ars_mall
+= user_s
;
855 m
.ahp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_memory_ars_huge
, mah_buf
) + size
);
856 m
.ahp
->mah_last
= macp
->mac_huge
;
857 macp
->mac_huge
= m
.ahp
;
858 p
.p_cp
= m
.ahp
->mah_buf
;
859 #ifdef HAVE_MEMORY_DEBUG
862 a_memory_ars_hmax
= n_MAX(a_memory_ars_hmax
, a_memory_ars_hcur
);
868 (n_autorec_calloc_from_pool
)(void *vp
, size_t nmemb
, size_t size
869 n_MEMORY_DEBUG_ARGS
){
873 size
*= nmemb
; /* XXX overflow, but only used for struct inits */
874 rv
= (n_autorec_alloc_from_pool
)(vp
, size n_MEMORY_DEBUG_ARGSCALL
);
881 n_autorec_relax_create(void){
882 struct a_memory_ars_ctx
*macp
;
885 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
886 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
888 if(macp
->mac_recur
++ == 0){
889 struct a_memory_ars_buffer
*mabp
;
891 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
892 mabp
->mab_relax
= mabp
->mab_caster
;
893 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
894 mabp
->mab_relax
= mabp
->mab_caster
;
898 n_err("n_autorec_relax_create(): recursion >0\n");
904 n_autorec_relax_gut(void){
905 struct a_memory_ars_ctx
*macp
;
908 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
909 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
911 assert(macp
->mac_recur
> 0);
913 if(--macp
->mac_recur
== 0){
914 struct a_memory_ars_buffer
*mabp
;
917 n_autorec_relax_unroll();
920 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
921 mabp
->mab_relax
= NULL
;
922 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
)
923 mabp
->mab_relax
= NULL
;
927 n_err("n_autorec_relax_unroll(): recursion >0\n");
933 n_autorec_relax_unroll(void){
934 /* The purpose of relaxation is only that it is possible to reset the
935 * casters, *not* to give back memory to the system. We are presumably in
936 * an iteration over all messages of a mailbox, and it'd be quite
937 * counterproductive to give the system allocator a chance to waste time */
938 struct a_memory_ars_ctx
*macp
;
941 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
942 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
944 assert(macp
->mac_recur
> 0);
947 if(macp
->mac_recur
== 1){
948 struct a_memory_ars_buffer
*mabp
, *x
, *y
;
950 /* Buffers in the full list may become usable again! */
951 for(x
= NULL
, mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= y
){
954 if(mabp
->mab_relax
== NULL
||
955 mabp
->mab_relax
< &mabp
->mab_buf
[sizeof(mabp
->mab_buf
) - 42]){
960 mabp
->mab_last
= macp
->mac_top
;
961 macp
->mac_top
= mabp
;
966 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
967 mabp
->mab_caster
= (mabp
->mab_relax
!= NULL
)
968 ? mabp
->mab_relax
: mabp
->mab_bot
;
969 #ifdef HAVE_MEMORY_DEBUG
970 memset(mabp
->mab_caster
, 0377,
971 PTR2SIZE(&mabp
->mab_buf
[sizeof(mabp
->mab_buf
)] - mabp
->mab_caster
));
979 (n_lofi_alloc
)(size_t size n_MEMORY_DEBUG_ARGS
){
980 #ifdef HAVE_MEMORY_DEBUG
983 union a_memory_ptr p
;
984 struct a_memory_ars_lofi
*malp
;
986 struct a_memory_ars_ctx
*macp
;
989 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
990 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
992 #ifdef HAVE_MEMORY_DEBUG
993 user_s
= (ui32_t
)size
;
997 size
+= sizeof(struct a_memory_ars_lofi_chunk
);
998 #ifdef HAVE_MEMORY_DEBUG
999 size
+= a_MEMORY_HOPE_SIZE
;
1001 size
= a_MEMORY_LOFI_ROUNDUP(size
);
1003 /* Huge allocations are special */
1004 if(n_UNLIKELY(isheap
= (size
> a_MEMORY_LOFI_MAX
))){
1005 #ifdef HAVE_MEMORY_DEBUG
1006 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
))
1007 n_alert("n_lofi_alloc() of %" PRIuZ
" bytes from %s, line %d",
1008 size
, mdbg_file
, mdbg_line
);
1010 }else if((malp
= macp
->mac_lofi
) != NULL
&&
1011 ((p
.p_cp
= malp
->mal_caster
) <= &malp
->mal_max
[-size
])){
1012 malp
->mal_caster
= &p
.p_cp
[size
];
1016 /* Need a new buffer */
1020 i
= n_VSTRUCT_SIZEOF(struct a_memory_ars_lofi
, mal_buf
) + size
;
1021 i
= n_MAX(i
, n_MEMORY_AUTOREC_SIZE
);
1023 malp
->mal_last
= macp
->mac_lofi
;
1024 malp
->mal_caster
= &malp
->mal_buf
[size
];
1025 i
-= n_VSTRUCT_SIZEOF(struct a_memory_ars_lofi
, mal_buf
);
1026 malp
->mal_max
= &malp
->mal_buf
[i
];
1027 macp
->mac_lofi
= malp
;
1028 p
.p_cp
= malp
->mal_buf
;
1030 #ifdef HAVE_MEMORY_DEBUG
1031 ++a_memory_lofi_ball
;
1032 ++a_memory_lofi_bcur
;
1033 a_memory_lofi_bmax
= n_MAX(a_memory_lofi_bmax
, a_memory_lofi_bcur
);
1038 p
.p_alc
->malc_last
= macp
->mac_lofi_top
;
1039 macp
->mac_lofi_top
= p
.p_alc
;
1041 p
.p_alc
->malc_last
= (struct a_memory_ars_lofi_chunk
*)
1042 ((uintptr_t)p
.p_alc
->malc_last
| 0x1);
1044 #ifndef HAVE_MEMORY_DEBUG
1047 p
.p_c
->mc_file
= mdbg_file
;
1048 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
1049 p
.p_c
->mc_isfree
= FAL0
;
1050 p
.p_c
->mc_user_size
= user_s
;
1051 p
.p_c
->mc_size
= (ui32_t
)size
;
1053 a_MEMORY_HOPE_SET(p_alc
, p
);
1055 ++a_memory_lofi_aall
;
1056 ++a_memory_lofi_acur
;
1057 a_memory_lofi_amax
= n_MAX(a_memory_lofi_amax
, a_memory_lofi_acur
);
1058 a_memory_lofi_mall
+= user_s
;
1059 a_memory_lofi_mcur
+= user_s
;
1060 a_memory_lofi_mmax
= n_MAX(a_memory_lofi_mmax
, a_memory_lofi_mcur
);
1067 (n_lofi_free
)(void *vp n_MEMORY_DEBUG_ARGS
){
1068 #ifdef HAVE_MEMORY_DEBUG
1071 union a_memory_ptr p
;
1072 struct a_memory_ars_ctx
*macp
;
1075 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1076 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1078 if((p
.p_vp
= vp
) == NULL
){
1079 #ifdef HAVE_MEMORY_DEBUG
1080 n_err("n_lofi_free(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
1085 #ifdef HAVE_MEMORY_DEBUG
1086 a_MEMORY_HOPE_GET(p_alc
, p
, isbad
);
1089 if(p
.p_c
->mc_isfree
){
1090 n_err("n_lofi_free(): double-free avoided at %s, line %d\n"
1091 "\tLast seen: %s, line %" PRIu16
"\n",
1092 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1095 p
.p_c
->mc_isfree
= TRU1
;
1096 memset(vp
, 0377, p
.p_c
->mc_user_size
);
1098 if(p
.p_alc
!= macp
->mac_lofi_top
){
1099 n_err("n_lofi_free(): this is not alloca top at %s, line %d\n"
1100 "\tLast seen: %s, line %" PRIu16
"\n",
1101 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1106 #endif /* HAVE_MEMORY_DEBUG */
1108 a_memory_lofi_free(macp
, --p
.p_alc
);
1114 n_lofi_snap_create(void){ /* TODO avoid temporary alloc */
1118 rv
= n_lofi_alloc(1);
1124 n_lofi_snap_unroll(void *cookie
){ /* TODO optimise */
1125 union a_memory_ptr p
;
1126 struct a_memory_ars_ctx
*macp
;
1131 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1132 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1135 p
.p_alc
= macp
->mac_lofi_top
;
1136 a_memory_lofi_free(macp
, p
.p_vp
);
1138 #ifdef HAVE_MEMORY_DEBUG
1139 a_MEMORY_HOPE_INC(p
.p_ui8p
);
1141 if(p
.p_vp
== cookie
)
1147 #ifdef HAVE_MEMORY_DEBUG
1149 c_memtrace(void *vp
){
1150 /* For a_MEMORY_HOPE_GET() */
1151 char const * const mdbg_file
= "memtrace()";
1152 int const mdbg_line
= -1;
1153 struct a_memory_ars_buffer
*mabp
;
1154 struct a_memory_ars_lofi_chunk
*malcp
;
1155 struct a_memory_ars_lofi
*malp
;
1156 struct a_memory_ars_ctx
*macp
;
1158 union a_memory_ptr p
, xp
;
1164 if((fp
= Ftmp(NULL
, "memtr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1165 n_perr("tmpfile", 0);
1171 "Last-Out-First-In (alloca) storage:\n"
1172 " Buffer cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1173 " Allocations cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1174 " Bytes cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n\n",
1175 a_memory_lofi_bcur
, a_memory_lofi_bmax
, a_memory_lofi_ball
,
1176 a_memory_lofi_acur
, a_memory_lofi_amax
, a_memory_lofi_aall
,
1177 a_memory_lofi_mcur
, a_memory_lofi_mmax
, a_memory_lofi_mall
);
1180 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1181 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1182 for(; macp
!= NULL
; macp
= macp
->mac_outer
){
1183 fprintf(fp
, " Evaluation stack context %p (outer: %p):\n",
1184 (void*)macp
, (void*)macp
->mac_outer
);
1187 for(malp
= macp
->mac_lofi
; malp
!= NULL
;){
1188 fprintf(fp
, " Buffer %p%s, %" PRIuZ
"/%" PRIuZ
" used/free:\n",
1189 (void*)malp
, ((uintptr_t)malp
->mal_last
& 0x1 ? " (huge)" : ""),
1190 PTR2SIZE(malp
->mal_caster
- &malp
->mal_buf
[0]),
1191 PTR2SIZE(malp
->mal_max
- malp
->mal_caster
));
1193 malp
= malp
->mal_last
;
1194 malp
= (struct a_memory_ars_lofi
*)((uintptr_t)malp
& ~1);
1197 for(malcp
= macp
->mac_lofi_top
; malcp
!= NULL
;){
1199 malcp
= (struct a_memory_ars_lofi_chunk
*)
1200 ((uintptr_t)malcp
->malc_last
& ~0x1);
1203 a_MEMORY_HOPE_GET_TRACE(p_alc
, xp
, isbad
);
1204 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1205 (isbad
? "! CANARY ERROR (LOFI): " : ""), xp
.p_vp
,
1206 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1211 "\nAuto-reclaimed storage:\n"
1212 " Buffers cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1213 " Huge allocations cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1214 " Allocations all: %" PRIuZ
", Bytes all: %" PRIuZ
"\n\n",
1215 a_memory_ars_bcur
, a_memory_ars_bmax
, a_memory_ars_ball
,
1216 a_memory_ars_hcur
, a_memory_ars_hmax
, a_memory_ars_hall
,
1217 a_memory_ars_aall
, a_memory_ars_mall
);
1220 for(macp
= n_go_data
->gdc_mempool
; macp
!= NULL
; macp
= macp
->mac_outer
){
1221 fprintf(fp
, " Evaluation stack context %p (outer: %p):\n",
1222 (void*)macp
, (void*)macp
->mac_outer
);
1225 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1226 fprintf(fp
, " Buffer %p, %" PRIuZ
"/%" PRIuZ
" used/free:\n",
1228 PTR2SIZE(mabp
->mab_caster
- &mabp
->mab_buf
[0]),
1229 PTR2SIZE(&mabp
->mab_buf
[sizeof(mabp
->mab_buf
)] - mabp
->mab_caster
));
1232 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1233 ++lines
, p
.p_cp
+= p
.p_c
->mc_size
){
1236 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1237 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1238 (isbad
? "! CANARY ERROR (ARS, top): " : ""), xp
.p_vp
,
1239 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1244 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1245 fprintf(fp
, " Buffer %p, full:\n", (void*)mabp
);
1248 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1249 ++lines
, p
.p_cp
+= p
.p_c
->mc_size
){
1252 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1253 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1254 (isbad
? "! CANARY ERROR (ARS, full): " : ""), xp
.p_vp
,
1255 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1262 "\nHeap memory buffers:\n"
1263 " Allocation cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n"
1264 " Bytes cur/peek/all: %7" PRIuZ
"/%7" PRIuZ
"/%10" PRIuZ
"\n\n",
1265 a_memory_heap_acur
, a_memory_heap_amax
, a_memory_heap_aall
,
1266 a_memory_heap_mcur
, a_memory_heap_mmax
, a_memory_heap_mall
);
1269 for(p
.p_hc
= a_memory_heap_list
; p
.p_hc
!= NULL
;
1270 ++lines
, p
.p_hc
= p
.p_hc
->mhc_next
){
1273 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1274 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1275 (isbad
? "! CANARY ERROR (heap): " : ""), xp
.p_vp
,
1276 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1279 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)){
1280 fprintf(fp
, "Heap buffers lingering for n_free():\n");
1283 for(p
.p_hc
= a_memory_heap_free
; p
.p_hc
!= NULL
;
1284 ++lines
, p
.p_hc
= p
.p_hc
->mhc_next
){
1287 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1288 fprintf(fp
, " %s%p (%u bytes): %s, line %u\n",
1289 (isbad
? "! CANARY ERROR (free): " : ""), xp
.p_vp
,
1290 p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1294 page_or_print(fp
, lines
);
1299 return (vp
!= NULL
);
1303 n__memory_check(char const *mdbg_file
, int mdbg_line
){
1304 union a_memory_ptr p
, xp
;
1305 struct a_memory_ars_buffer
*mabp
;
1306 struct a_memory_ars_lofi_chunk
*malcp
;
1307 struct a_memory_ars_ctx
*macp
;
1308 bool_t anybad
, isbad
;
1313 if((macp
= n_go_data
->gdc_mempool
) == NULL
)
1314 macp
= n_go_data
->gdc_mempool
= n_go_data
->gdc__mempool_buf
;
1318 for(malcp
= macp
->mac_lofi_top
; malcp
!= NULL
;){
1320 malcp
= (struct a_memory_ars_lofi_chunk
*)
1321 ((uintptr_t)malcp
->malc_last
& ~0x1);
1324 a_MEMORY_HOPE_GET_TRACE(p_alc
, xp
, isbad
);
1328 "! CANARY ERROR (LOFI): %p (%u bytes): %s, line %u\n",
1329 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1333 /* Auto-reclaimed */
1335 for(mabp
= macp
->mac_top
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1336 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1337 p
.p_cp
+= p
.p_c
->mc_size
){
1340 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1344 "! CANARY ERROR (ARS, top): %p (%u bytes): %s, line %u\n",
1345 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1350 for(mabp
= macp
->mac_full
; mabp
!= NULL
; mabp
= mabp
->mab_last
){
1351 for(p
.p_cp
= mabp
->mab_buf
; p
.p_cp
< mabp
->mab_caster
;
1352 p
.p_cp
+= p
.p_c
->mc_size
){
1355 a_MEMORY_HOPE_GET_TRACE(p_c
, xp
, isbad
);
1359 "! CANARY ERROR (ARS, full): %p (%u bytes): %s, line %u\n",
1360 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1367 for(p
.p_hc
= a_memory_heap_list
; p
.p_hc
!= NULL
; p
.p_hc
= p
.p_hc
->mhc_next
){
1370 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1374 "! CANARY ERROR (heap): %p (%u bytes): %s, line %u\n",
1375 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1379 if(n_poption
& (n_PO_DEBUG
| n_PO_MEMDEBUG
)){
1380 for(p
.p_hc
= a_memory_heap_free
; p
.p_hc
!= NULL
;
1381 p
.p_hc
= p
.p_hc
->mhc_next
){
1384 a_MEMORY_HOPE_GET_TRACE(p_hc
, xp
, isbad
);
1388 "! CANARY ERROR (free): %p (%u bytes): %s, line %u\n",
1389 xp
.p_vp
, p
.p_c
->mc_user_size
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1394 if(anybad
&& ok_blook(memdebug
))
1395 n_panic("Memory errors encountered");
1399 #endif /* HAVE_MEMORY_DEBUG */