a_head_addrspec_check(): in quoted-string, only quoted-pairize " and \
[s-mailx.git] / memory.c
blobc1986b2cf5be99d7db0cf674a4c945661f8cecb0
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.
20 #undef n_FILE
21 #define n_FILE memory
23 #ifndef HAVE_AMALGAMATION
24 # include "nail.h"
25 #endif
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) \
76 do{\
77 ui64_t __h__ = (uintptr_t)(P);\
78 __h__ *= ((ui64_t)0x9E37FFFFu << 32) | 0xFFFC0000u;\
79 __h__ >>= 56;\
80 (S) = (ui8_t)__h__;\
81 }while(0)
83 # define a_MEMORY_HOPE_UPPER(S,P) \
84 do{\
85 ui32_t __i__;\
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){\
90 (S) = (ui8_t)__x__;\
91 break;\
93 if(__i__ == 0)\
94 (S) = 0xAAu;\
95 }while(0)
97 # define a_MEMORY_HOPE_SET(T,C) \
98 do{\
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]);\
121 }while(0)
123 # define a_MEMORY_HOPE_GET_TRACE(T,C,BAD) \
124 do{\
125 a_MEMORY_HOPE_INC((C).p_cp);\
126 a_MEMORY_HOPE_GET(T, C, BAD);\
127 a_MEMORY_HOPE_INC((C).p_cp);\
128 }while(0)
130 # define a_MEMORY_HOPE_GET(T,C,BAD) \
131 do{\
132 union a_memory_ptr __xp;\
133 struct a_memory_chunk *__xc;\
134 ui32_t __i;\
135 ui8_t __m;\
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);\
140 (BAD) = FAL0;\
141 __i = 0;\
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;\
158 if(__i != 0){\
159 (BAD) = TRU1;\
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;\
166 __i = 0;\
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;\
183 if(__i != 0){\
184 (BAD) = TRU1;\
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);\
190 if(BAD)\
191 n_alert(" ..canary last seen: %s, line %u",\
192 __xc->mc_file, __xc->mc_line);\
193 }while(0)
194 #endif /* HAVE_MEMORY_DEBUG */
196 #ifdef HAVE_MEMORY_DEBUG
197 struct a_memory_chunk{
198 char const *mc_file;
199 ui32_t mc_line;
200 ui8_t mc_isfree;
201 ui8_t mc__dummy[3];
202 ui32_t mc_user_size;
203 ui32_t mc_size;
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;
218 #endif
219 struct a_memory_ars_lofi_chunk *malc_last; /* Bit 1 set: it's a heap alloc */
222 union a_memory_ptr{
223 void *p_vp;
224 char *p_cp;
225 ui8_t *p_ui8p;
226 #ifdef HAVE_MEMORY_DEBUG
227 struct a_memory_chunk *p_c;
228 struct a_memory_heap_chunk *p_hc;
229 #endif
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(). Only used for the global _ctx */
248 char *mab_relax; /* If !NULL, used by _relax_unroll() instead of .mab_bot */
249 char *mab_caster; /* Point of casting memory, NULL if full */
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_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");
258 #endif
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;
270 char *mal_caster;
271 char *mal_max;
272 char mal_buf[n_VFIELD_SIZE(a_MEMORY_ARS_ROUNDUP(1))];
275 /* */
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;
288 #endif
290 /* */
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);
296 SINLINE void
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;
300 NYD2_ENTER;
302 p.p_vp = vp;
303 #ifdef HAVE_MEMORY_DEBUG
304 --a_memory_lofi_acur;
305 a_memory_lofi_mcur -= p.p_c->mc_user_size;
306 #endif
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);
314 n_free(malp);
315 #ifdef HAVE_MEMORY_DEBUG
316 --a_memory_lofi_bcur;
317 #endif
318 }else{
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;
326 n_free(malp);
327 #ifdef HAVE_MEMORY_DEBUG
328 --a_memory_lofi_bcur;
329 #endif
331 }else
332 malp->mal_caster = p.p_cp;
334 NYD2_LEAVE;
337 static void
338 a_memory_ars_reset(struct a_memory_ars_ctx *macp){
339 union{
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;
344 } m, m2;
345 NYD2_ENTER;
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;
358 x = m.abp;
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){
363 if(m2.abp == NULL)
364 macp->mac_top = m.abp;
365 else
366 m2.abp->mab_last = m.abp;
367 n_free(x);
368 #ifdef HAVE_MEMORY_DEBUG
369 --a_memory_ars_bcur;
370 #endif
371 }else{
372 m2.abp = x;
373 x->mab_caster = x->mab_bot;
374 x->mab_relax = NULL;
375 #ifdef HAVE_MEMORY_DEBUG
376 memset(x->mab_caster, 0377,
377 PTR2SIZE(&x->mab_buf[sizeof(x->mab_buf)] - x->mab_caster));
378 #endif
382 while((m.ahp = macp->mac_huge) != NULL){
383 macp->mac_huge = m.ahp->mah_last;
384 n_free(m.ahp);
385 #ifdef HAVE_MEMORY_DEBUG
386 --a_memory_ars_hcur;
387 #endif
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!");
394 #endif
395 while((m.alcp = macp->mac_lofi_top) != NULL)
396 a_memory_lofi_free(macp, m.alcp);
397 NYD2_LEAVE;
400 FL void
401 n_memory_reset(void){
402 #ifdef HAVE_MEMORY_DEBUG
403 union a_memory_ptr p;
404 size_t c, s;
405 #endif
406 struct a_memory_ars_ctx *macp;
407 NYD_ENTER;
409 n_memory_check();
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){
416 macp->mac_recur = 1;
417 n_memory_autorec_relax_gut();
419 a_memory_ars_reset(macp);
422 /* Now we are ready to deal with heap */
423 #ifdef HAVE_MEMORY_DEBUG
424 c = s = 0;
426 for(p.p_hc = a_memory_heap_free; p.p_hc != NULL;){
427 void *vp;
429 vp = p.p_hc;
430 ++c;
431 s += p.p_c->mc_size;
432 p.p_hc = p.p_hc->mhc_next;
433 (free)(vp);
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);
439 #endif
440 NYD_LEAVE;
443 FL void
444 n_memory_pool_fixate(void){
445 struct a_memory_ars_buffer *mabp;
446 struct a_memory_ars_ctx *macp;
447 NYD_ENTER;
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;
455 NYD_LEAVE;
458 FL void
459 n_memory_pool_push(void *vp){
460 struct a_memory_ars_ctx *macp;
461 NYD_ENTER;
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;
469 NYD_LEAVE;
472 FL void
473 n_memory_pool_pop(void *vp){
474 struct a_memory_ars_buffer *mabp;
475 struct a_memory_ars_ctx *macp;
476 NYD_ENTER;
478 n_memory_check();
480 if((macp = vp) == NULL){
481 macp = n_go_data->gdc_mempool;
482 assert(macp != NULL);
483 }else{
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;
499 while(mabp != NULL){
500 vp = mabp;
501 mabp = mabp->mab_last;
502 n_free(vp);
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;
511 #endif
512 n_free(vp);
514 NYD_LEAVE;
517 #ifndef HAVE_MEMORY_DEBUG
518 FL void *
519 n_alloc(size_t s){
520 void *rv;
521 NYD2_ENTER;
523 if(s == 0)
524 s = 1;
525 if((rv = malloc(s)) == NULL)
526 n_panic(_("no memory"));
527 NYD2_LEAVE;
528 return rv;
531 FL void *
532 n_realloc(void *vp, size_t s){
533 void *rv;
534 NYD2_ENTER;
536 if(vp == NULL)
537 rv = n_alloc(s);
538 else{
539 if(s == 0)
540 s = 1;
541 if((rv = realloc(vp, s)) == NULL)
542 n_panic(_("no memory"));
544 NYD2_LEAVE;
545 return rv;
548 FL void *
549 n_calloc(size_t nmemb, size_t size){
550 void *rv;
551 NYD2_ENTER;
553 if(size == 0)
554 size = 1;
555 if((rv = calloc(nmemb, size)) == NULL)
556 n_panic(_("no memory"));
557 NYD2_LEAVE;
558 return rv;
561 FL void
562 (n_free)(void *vp){
563 NYD2_ENTER;
564 (free)(vp);
565 NYD2_LEAVE;
568 #else /* !HAVE_MEMORY_DEBUG */
569 FL void *
570 (n_alloc)(size_t s n_MEMORY_DEBUG_ARGS){
571 union a_memory_ptr p;
572 ui32_t user_s;
573 NYD2_ENTER;
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)
579 s = 1;
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);
604 NYD2_LEAVE;
605 return p.p_vp;
608 FL void *
609 (n_realloc)(void *vp, size_t s n_MEMORY_DEBUG_ARGS){
610 union a_memory_ptr p;
611 ui32_t user_s;
612 bool_t isbad;
613 NYD2_ENTER;
615 if((p.p_vp = vp) == NULL){
616 jforce:
617 p.p_vp = (n_alloc)(s, mdbg_file, mdbg_line);
618 goto jleave;
621 a_MEMORY_HOPE_GET(p_hc, p, isbad);
622 --p.p_hc;
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);
628 goto jforce;
631 if(p.p_hc == a_memory_heap_list)
632 a_memory_heap_list = p.p_hc->mhc_next;
633 else
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)
645 s = 1;
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);
669 jleave:
670 NYD2_LEAVE;
671 return p.p_vp;
674 FL void *
675 (n_calloc)(size_t nmemb, size_t size n_MEMORY_DEBUG_ARGS){
676 union a_memory_ptr p;
677 ui32_t user_s;
678 NYD2_ENTER;
680 if(nmemb == 0)
681 nmemb = 1;
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)
686 size = 1;
687 if((UI32_MAX - sizeof(struct a_memory_heap_chunk) - a_MEMORY_HOPE_SIZE) /
688 nmemb < size)
689 n_panic("n_calloc(): allocation count too large: %s, line %d",
690 mdbg_file, mdbg_line);
692 size *= nmemb;
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);
718 NYD2_LEAVE;
719 return p.p_vp;
722 FL void
723 (n_free)(void *vp n_MEMORY_DEBUG_ARGS){
724 union a_memory_ptr p;
725 bool_t isbad;
726 NYD2_ENTER;
728 if((p.p_vp = vp) == NULL){
729 n_err("n_free(NULL) from %s, line %d\n", mdbg_file, mdbg_line);
730 goto jleave;
733 a_MEMORY_HOPE_GET(p_hc, p, isbad);
734 --p.p_hc;
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);
740 goto jleave;
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;
746 }else
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;
761 }else
762 (free)(p.p_vp);
763 jleave:
764 NYD2_LEAVE;
766 #endif /* HAVE_MEMORY_DEBUG */
768 FL void *
769 (n_autorec_alloc_from_pool)(void *vp, size_t size n_MEMORY_DEBUG_ARGS){
770 #ifdef HAVE_MEMORY_DEBUG
771 ui32_t user_s;
772 #endif
773 union a_memory_ptr p;
774 union{
775 struct a_memory_ars_buffer *abp;
776 struct a_memory_ars_huge *ahp;
777 } m, m2;
778 struct a_memory_ars_ctx *macp;
779 NYD2_ENTER;
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;
786 #endif
787 if(size == 0)
788 ++size;
789 #ifdef HAVE_MEMORY_DEBUG
790 size += sizeof(struct a_memory_chunk) + a_MEMORY_HOPE_SIZE;
791 #endif
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);
799 #endif
800 goto jhuge;
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])){
813 if(m2.abp == NULL)
814 macp->mac_top = m.abp->mab_last;
815 else
816 m2.abp->mab_last = m.abp->mab_last;
817 m.abp->mab_last = macp->mac_full;
818 macp->mac_full = m.abp;
820 goto jleave;
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
833 ++a_memory_ars_ball;
834 ++a_memory_ars_bcur;
835 a_memory_ars_bmax = n_MAX(a_memory_ars_bmax, a_memory_ars_bcur);
836 #endif
838 jleave:
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;
844 ++p.p_c;
845 a_MEMORY_HOPE_SET(p_c, p);
847 ++a_memory_ars_aall;
848 a_memory_ars_mall += user_s;
849 #endif
850 NYD2_LEAVE;
851 return p.p_vp;
853 jhuge:
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
859 ++a_memory_ars_hall;
860 ++a_memory_ars_hcur;
861 a_memory_ars_hmax = n_MAX(a_memory_ars_hmax, a_memory_ars_hcur);
862 #endif
863 goto jleave;
866 FL void *
867 (n_autorec_calloc_from_pool)(void *vp, size_t nmemb, size_t size
868 n_MEMORY_DEBUG_ARGS){
869 void *rv;
870 NYD2_ENTER;
872 size *= nmemb; /* XXX overflow, but only used for struct inits */
873 rv = (n_autorec_alloc_from_pool)(vp, size n_MEMORY_DEBUG_ARGSCALL);
874 memset(rv, 0, size);
875 NYD2_LEAVE;
876 return rv;
879 FL void
880 n_memory_autorec_relax_create(void){
881 struct a_memory_ars_ctx *macp;
882 NYD2_ENTER;
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;
895 #ifdef HAVE_DEVEL
896 else
897 n_err("n_memory_autorec_relax_create(): recursion >0\n");
898 #endif
899 NYD2_LEAVE;
902 FL void
903 n_memory_autorec_relax_gut(void){
904 struct a_memory_ars_ctx *macp;
905 NYD2_ENTER;
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;
915 macp->mac_recur = 1;
916 n_memory_autorec_relax_unroll();
917 macp->mac_recur = 0;
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;
924 #ifdef HAVE_DEVEL
925 else
926 n_err("n_memory_autorec_relax_unroll(): recursion >0\n");
927 #endif
928 NYD2_LEAVE;
931 FL void
932 n_memory_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;
938 NYD2_ENTER;
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);
944 n_memory_check();
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){
951 y = mabp->mab_last;
953 if(mabp->mab_relax == NULL ||
954 mabp->mab_relax < &mabp->mab_buf[sizeof(mabp->mab_buf) - 42]){
955 if(x == NULL)
956 macp->mac_full = y;
957 else
958 x->mab_last = y;
959 mabp->mab_last = macp->mac_top;
960 macp->mac_top = mabp;
961 }else
962 x = 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));
971 #endif
974 NYD2_LEAVE;
977 FL void *
978 (n_lofi_alloc)(size_t size n_MEMORY_DEBUG_ARGS){
979 #ifdef HAVE_MEMORY_DEBUG
980 ui32_t user_s;
981 #endif
982 union a_memory_ptr p;
983 struct a_memory_ars_lofi *malp;
984 bool_t isheap;
985 struct a_memory_ars_ctx *macp;
986 NYD2_ENTER;
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;
993 #endif
994 if(size == 0)
995 ++size;
996 size += sizeof(struct a_memory_ars_lofi_chunk);
997 #ifdef HAVE_MEMORY_DEBUG
998 size += a_MEMORY_HOPE_SIZE;
999 #endif
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);
1007 #endif
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];
1011 goto jleave;
1014 /* Need a new buffer */
1015 /* C99 */{
1016 size_t i;
1018 i = n_VSTRUCT_SIZEOF(struct a_memory_ars_lofi, mal_buf) + size;
1019 i = n_MAX(i, n_MEMORY_AUTOREC_SIZE);
1020 malp = n_alloc(i);
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);
1032 #endif
1035 jleave:
1036 p.p_alc->malc_last = macp->mac_lofi_top;
1037 macp->mac_lofi_top = p.p_alc;
1038 if(isheap)
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
1043 ++p.p_alc;
1044 #else
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;
1050 ++p.p_alc;
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);
1059 #endif
1060 NYD2_LEAVE;
1061 return p.p_vp;
1064 FL void
1065 (n_lofi_free)(void *vp n_MEMORY_DEBUG_ARGS){
1066 #ifdef HAVE_MEMORY_DEBUG
1067 bool_t isbad;
1068 #endif
1069 union a_memory_ptr p;
1070 struct a_memory_ars_ctx *macp;
1071 NYD2_ENTER;
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);
1079 #endif
1080 goto jleave;
1083 #ifdef HAVE_MEMORY_DEBUG
1084 a_MEMORY_HOPE_GET(p_alc, p, isbad);
1085 --p.p_alc;
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);
1091 goto jleave;
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);
1100 goto jleave;
1103 ++p.p_alc;
1104 #endif /* HAVE_MEMORY_DEBUG */
1106 a_memory_lofi_free(macp, --p.p_alc);
1107 jleave:
1108 NYD2_LEAVE;
1111 FL void *
1112 n_lofi_snap_create(void){ /* TODO avoid temporary alloc */
1113 void *rv;
1114 NYD2_ENTER;
1116 rv = n_lofi_alloc(1);
1117 NYD2_LEAVE;
1118 return rv;
1121 FL void
1122 n_lofi_snap_unroll(void *cookie){ /* TODO optimise */
1123 union a_memory_ptr p;
1124 struct a_memory_ars_ctx *macp;
1125 NYD2_ENTER;
1127 n_memory_check();
1129 if((macp = n_go_data->gdc_mempool) == NULL)
1130 macp = n_go_data->gdc_mempool = n_go_data->gdc__mempool_buf;
1132 for(;;){
1133 p.p_alc = macp->mac_lofi_top;
1134 a_memory_lofi_free(macp, p.p_vp);
1135 ++p.p_alc;
1136 #ifdef HAVE_DEBUG
1137 a_MEMORY_HOPE_INC(p.p_ui8p);
1138 #endif
1139 if(p.p_vp == cookie)
1140 break;
1142 NYD2_LEAVE;
1145 #ifdef HAVE_MEMORY_DEBUG
1146 FL int
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;
1155 bool_t isbad;
1156 union a_memory_ptr p, xp;
1157 size_t lines;
1158 FILE *fp;
1159 NYD2_ENTER;
1161 vp = (void*)0x1;
1162 if((fp = Ftmp(NULL, "memtr", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1163 n_perr("tmpfile", 0);
1164 goto jleave;
1166 lines = 0;
1168 fprintf(fp,
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);
1176 lines += 7;
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);
1183 ++lines;
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));
1190 ++lines;
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;){
1196 p.p_alc = malcp;
1197 malcp = (struct a_memory_ars_lofi_chunk*)
1198 ((uintptr_t)malcp->malc_last & ~0x1);
1199 xp = p;
1200 ++xp.p_alc;
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);
1208 fprintf(fp,
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);
1216 lines += 7;
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);
1221 ++lines;
1223 for(mabp = macp->mac_top; mabp != NULL; mabp = mabp->mab_last){
1224 fprintf(fp, " Buffer %p, %" PRIuZ "/%" PRIuZ " used/free:\n",
1225 (void*)mabp,
1226 PTR2SIZE(mabp->mab_caster - &mabp->mab_buf[0]),
1227 PTR2SIZE(&mabp->mab_buf[sizeof(mabp->mab_buf)] - mabp->mab_caster));
1228 ++lines;
1230 for(p.p_cp = mabp->mab_buf; p.p_cp < mabp->mab_caster;
1231 ++lines, p.p_cp += p.p_c->mc_size){
1232 xp = p;
1233 ++xp.p_c;
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);
1239 ++lines;
1242 for(mabp = macp->mac_full; mabp != NULL; mabp = mabp->mab_last){
1243 fprintf(fp, " Buffer %p, full:\n", (void*)mabp);
1244 ++lines;
1246 for(p.p_cp = mabp->mab_buf; p.p_cp < mabp->mab_caster;
1247 ++lines, p.p_cp += p.p_c->mc_size){
1248 xp = p;
1249 ++xp.p_c;
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);
1255 ++lines;
1259 fprintf(fp,
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);
1265 lines += 6;
1267 for(p.p_hc = a_memory_heap_list; p.p_hc != NULL;
1268 ++lines, p.p_hc = p.p_hc->mhc_next){
1269 xp = p;
1270 ++xp.p_hc;
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");
1279 ++lines;
1281 for(p.p_hc = a_memory_heap_free; p.p_hc != NULL;
1282 ++lines, p.p_hc = p.p_hc->mhc_next){
1283 xp = p;
1284 ++xp.p_hc;
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);
1293 Fclose(fp);
1294 vp = NULL;
1295 jleave:
1296 NYD2_LEAVE;
1297 return (vp != NULL);
1300 FL bool_t
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;
1307 NYD2_ENTER;
1309 anybad = FAL0;
1311 if((macp = n_go_data->gdc_mempool) == NULL)
1312 macp = n_go_data->gdc_mempool = n_go_data->gdc__mempool_buf;
1314 /* Alloca */
1316 for(malcp = macp->mac_lofi_top; malcp != NULL;){
1317 p.p_alc = malcp;
1318 malcp = (struct a_memory_ars_lofi_chunk*)
1319 ((uintptr_t)malcp->malc_last & ~0x1);
1320 xp = p;
1321 ++xp.p_alc;
1322 a_MEMORY_HOPE_GET_TRACE(p_alc, xp, isbad);
1323 if(isbad){
1324 anybad = TRU1;
1325 n_err(
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){
1336 xp = p;
1337 ++xp.p_c;
1338 a_MEMORY_HOPE_GET_TRACE(p_c, xp, isbad);
1339 if(isbad){
1340 anybad = TRU1;
1341 n_err(
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){
1351 xp = p;
1352 ++xp.p_c;
1353 a_MEMORY_HOPE_GET_TRACE(p_c, xp, isbad);
1354 if(isbad){
1355 anybad = TRU1;
1356 n_err(
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);
1363 /* Heap*/
1365 for(p.p_hc = a_memory_heap_list; p.p_hc != NULL; p.p_hc = p.p_hc->mhc_next){
1366 xp = p;
1367 ++xp.p_hc;
1368 a_MEMORY_HOPE_GET_TRACE(p_hc, xp, isbad);
1369 if(isbad){
1370 anybad = TRU1;
1371 n_err(
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){
1380 xp = p;
1381 ++xp.p_hc;
1382 a_MEMORY_HOPE_GET_TRACE(p_hc, xp, isbad);
1383 if(isbad){
1384 anybad = TRU1;
1385 n_err(
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");
1394 NYD2_LEAVE;
1395 return anybad;
1397 #endif /* HAVE_MEMORY_DEBUG */
1399 /* s-it-mode */