Annotation of libelwix/src/mem.c, revision 1.11
1.1 misho 1: /*************************************************************************
2: * (C) 2012 AITNET ltd - Sofia/Bulgaria - <misho@elwix.org>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.11 ! misho 6: * $Id: mem.c,v 1.10.8.2 2024/10/27 10:08:02 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.10 misho 15: Copyright 2004 - 2024
1.1 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: mpool_t *elwix_mpool;
50:
51:
52: /*
53: * mpool_init() - Init memory pool
54: *
55: * @maxmem = If !=0 set maximum memory quota
56: * return: =NULL error or !=NULL new allocated pool
57: */
58: mpool_t *
59: mpool_init(u_long maxmem)
60: {
61: mpool_t *mp;
62: register int i;
63:
64: mp = malloc(sizeof(mpool_t));
65: if (!mp) {
66: LOGERR;
67: return NULL;
68: } else
69: memset(mp, 0, sizeof(mpool_t));
70:
71: pthread_mutex_init(&mp->pool_mtx, NULL);
72:
73: mp->pool_quota.max = maxmem;
74:
75: mpool_lock(mp);
76: for (i = 0; i < MEM_BUCKETS; i++) {
77: TAILQ_INIT(&mp->pool_active[i]);
78: TAILQ_INIT(&mp->pool_inactive[i]);
79: }
80: mpool_unlock(mp);
81:
82: return mp;
83: }
84:
85: /*
86: * mpool_destroy() - Destroy memory pool
87: *
88: * @mp = Memory pool
89: * return: none
90: */
91: void
92: mpool_destroy(mpool_t ** __restrict mp)
93: {
1.11 ! misho 94: struct tagAlloc *m, *n;
1.1 misho 95: register int i;
96:
1.10 misho 97: if (!mp || !*mp)
1.1 misho 98: return;
99:
100: mpool_lock(*mp);
101:
102: for (i = 0; i < MEM_BUCKETS; i++) {
1.11 ! misho 103: for (m = TAILQ_FIRST(&(*mp)->pool_active[i]); m; m = n) {
! 104: n = TAILQ_NEXT(m, alloc_node);
1.1 misho 105: if (m->alloc_mem)
106: free(m->alloc_mem);
107: free(m);
108: }
1.11 ! misho 109: for (m = TAILQ_FIRST(&(*mp)->pool_inactive[i]); m; m = n) {
! 110: n = TAILQ_NEXT(m, alloc_node);
1.1 misho 111: if (m->alloc_mem)
112: free(m->alloc_mem);
113: free(m);
114: }
115: }
116:
117: mpool_unlock(*mp);
118: pthread_mutex_destroy(&(*mp)->pool_mtx);
119:
120: free(*mp);
121: *mp = NULL;
122: }
123:
124: /* ----------------------------------------------------------- */
125:
126: static inline long
127: BucketIndex(u_int size)
128: {
129: register long b;
130:
131: if (!size--)
132: return 0; /* min bucket position in array */
133:
134: for (b = MEM_MIN_BUCKET; b < MEM_MAX_BUCKET; b++)
135: if (!(size >> b))
136: break;
137:
138: return b - MEM_MIN_BUCKET; /* convert to bucket array index */
139: }
140:
141: static inline struct tagAlloc *
142: pullInactive(mpool_t * __restrict mp, int idx)
143: {
144: struct tagAlloc *m = NULL;
145:
146: /* must be locked pool before use this function */
147: if ((m = TAILQ_FIRST(&mp->pool_inactive[idx]))) {
148: TAILQ_REMOVE(&mp->pool_inactive[idx], m, alloc_node);
149: /* statistics */
150: mp->pool_calls.cache--;
151: mp->pool_bytes.cache -= mem_size(m);
152:
153: /* clear name */
154: *m->alloc_name = 0;
155: }
156:
157: return m;
158: }
159:
160: /*
161: * mpool_malloc() - Memory allocation
162: *
163: * @mp = Memory pool
164: * @size = Size
165: * @memname = Optional memory block name
166: * return: NULL error or !=NULL ok allocated memory
167: */
168: void *
169: mpool_malloc(mpool_t * __restrict mp, u_int size, const char *memname)
170: {
171: struct tagAlloc *m;
172: int idx;
173: u_int align;
174:
175: if (!mp) {
176: elwix_SetErr(EINVAL, "Pool not specified");
177: return NULL;
178: }
179: if (size > MEM_ALLOC_MAX) {
180: elwix_SetErr(ENOMEM, "Memory size is too large");
181: return NULL;
182: } else
183: size = (size + 3) & ~3; /* must align to 4 because needed room for sentinels */
184:
185: idx = BucketIndex(size);
186:
187: mpool_lock(mp);
188:
189: /* get memory from cache if exists */
190: if (!(m = pullInactive(mp, idx))) {
191: /* quota */
192: if (mp->pool_quota.max &&
193: (mp->pool_quota.curr + size) > mp->pool_quota.max) {
194: elwix_SetErr(ENOMEM, "Max.allocate memory quota has been reached");
195: mpool_unlock(mp);
196: return NULL;
197: }
198:
199: m = malloc(sizeof(struct tagAlloc));
200: if (!m) {
201: LOGERR;
202: mpool_unlock(mp);
203: return NULL;
204: } else
205: memset(m, 0, sizeof(struct tagAlloc));
206: }
207:
208: if (memname)
209: strlcpy(m->alloc_name, memname, sizeof m->alloc_name);
210:
211: if (!m->alloc_mem) {
212: align = 1 << (idx + MEM_MIN_BUCKET);
213: m->alloc_mem = malloc(align + 12); /* +12 sentinel bytes */
214: if (!m->alloc_mem) {
215: LOGERR;
216: free(m);
217: mpool_unlock(mp);
218: return NULL;
219: } else { /* quota */
220: mp->pool_quota.curr += size;
1.5 misho 221: mp->pool_quota.real += 1 << (idx + MEM_MIN_BUCKET);
1.10 misho 222: #ifdef MPOOL_MEM_ZERO
1.1 misho 223: memset(m->alloc_mem, 0, align + 12);
1.10 misho 224: #endif
1.1 misho 225: }
226: }
227:
228: m->alloc_mem[0] = size / sizeof(u_int);
229: m->alloc_mem[1] = MEM_MAGIC_START;
230: m->alloc_mem[2 + size / sizeof(u_int)] = MEM_MAGIC_STOP;
231: TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node);
232: /* statistics */
233: mp->pool_calls.alloc++;
234: mp->pool_bytes.alloc += size;
235:
236: mpool_unlock(mp);
237: return mem_data(m, void*);
238: }
239:
240: /*
1.10 misho 241: * mpool_calloc() - Multiple memory block allocation
242: *
243: * @mp = Memory pool
244: * @nmemb = Number of memory blocks
245: * @size = Size
246: * @memname = Optional memory block name
247: * return: NULL error or !=NULL ok allocated memory
248: */
249: void *
250: mpool_calloc(mpool_t * __restrict mp, u_int nmemb, u_int size, const char *memname)
251: {
252: void *m;
253: u_int total = nmemb * size;
254:
255: m = mpool_malloc(mp, total, memname);
256: #ifndef MPOOL_MEM_ZERO
257: if (m)
258: memset(m, 0, total);
259: #endif
260: return m;
261: }
262:
263: /*
1.1 misho 264: * mpool_realloc() Reallocate memory block with new size
265: *
266: * @mp = Memory pool
267: * @data = Allocated memory data
268: * @newsize = New size of memory block
269: * @memname = Optional new memory block name
270: * return: NULL error or !=NULL new reallocated memory block
271: */
272: void *
273: mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname)
274: {
275: int idx, oidx;
276: void *p;
1.6 misho 277: u_int osize;
1.1 misho 278:
279: /* if !data execute mpool_malloc() */
280: if (!data)
281: return mpool_malloc(mp, newsize, memname);
282:
283: if (!mp) {
284: elwix_SetErr(EINVAL, "Pool not specified");
285: return NULL;
286: }
287: /* check address range & sentinel */
288: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
289: elwix_SetErr(EFAULT, "Corrupted memory address");
290: return NULL;
291: }
292: /* prepare new size */
293: if (newsize > MEM_ALLOC_MAX) {
294: elwix_SetErr(ENOMEM, "Memory size is too large");
295: return NULL;
296: }
297:
298: mpool_lock(mp);
299:
1.6 misho 300: osize = ((u_int*)data)[-2] * sizeof(u_int);
301: oidx = BucketIndex(osize);
302: newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */
303: idx = BucketIndex(newsize);
304:
1.1 misho 305: /* quota */
306: if (mp->pool_quota.max &&
307: (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) {
308: elwix_SetErr(ENOMEM, "Max.allocate memory quota has been reached");
309: mpool_unlock(mp);
310: return NULL;
311: }
312:
1.6 misho 313: if (oidx != idx) {
1.1 misho 314: mpool_unlock(mp);
1.6 misho 315: p = mpool_malloc(mp, newsize, memname);
316: if (!p)
317: return NULL;
1.1 misho 318:
1.7 misho 319: memcpy(p, data, MIN(osize, newsize));
1.6 misho 320: mpool_free(mp, data, 0);
321: } else {
322: p = data;
1.1 misho 323:
1.6 misho 324: ((u_int*) p)[-2] = newsize / sizeof(u_int);
325: ((u_int*) p)[newsize / sizeof(u_int)] = MEM_MAGIC_STOP;
1.1 misho 326:
1.6 misho 327: mp->pool_bytes.alloc += (u_long) newsize - osize;
328: mp->pool_quota.curr += (u_long) newsize - osize;
1.1 misho 329:
1.6 misho 330: mpool_unlock(mp);
1.1 misho 331: }
332:
1.6 misho 333: return p;
1.1 misho 334: }
335:
336: /*
337: * mpool_purge() - Purge memory block cache and release resources
338: *
339: * @mp = Memory pool
340: * @atmost = Free at most in buckets
341: * return: -1 error or 0 ok
342: */
343: int
344: mpool_purge(mpool_t * __restrict mp, u_int atmost)
345: {
346: register int i, cx;
347: struct tagAlloc *m, *tmp;
348:
349: if (!mp) {
350: elwix_SetErr(EINVAL, "Pool not specified");
351: return -1;
352: }
353:
354: mpool_lock(mp);
355:
356: for (i = cx = 0; i < MEM_BUCKETS; cx = 0, i++) {
357: TAILQ_FOREACH_SAFE(m, &mp->pool_inactive[i], alloc_node, tmp) {
358: /* barrier for purge */
359: if (cx < atmost) {
360: cx++;
361: continue;
362: }
363:
364: TAILQ_REMOVE(&mp->pool_inactive[i], m, alloc_node);
365: /* statistics */
366: mp->pool_calls.cache--;
367: mp->pool_bytes.cache -= mem_size(m);
368:
369: mp->pool_calls.free++;
370: mp->pool_bytes.free += mem_size(m);
371: /* quota */
372: mp->pool_quota.curr -= mem_size(m);
1.5 misho 373: mp->pool_quota.real -= 1 << (i + MEM_MIN_BUCKET);
1.1 misho 374:
375: if (m->alloc_mem)
376: free(m->alloc_mem);
377: free(m);
378: }
379: }
380:
381: mpool_unlock(mp);
382: return 0;
383: }
384:
385: /*
386: * mpool_free() Free allocated memory with mpool_alloc()
387: *
388: * @mp = Memory pool
389: * @data = Allocated memory data
390: * @purge = if !=0 force release memory block
391: * return: <0 error or 0 ok released memory block
392: */
393: int
394: mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge)
395: {
396: int idx;
397: struct tagAlloc *m, *tmp;
398:
1.4 misho 399: if (!data)
400: return 0;
1.1 misho 401: if (!mp) {
402: elwix_SetErr(EINVAL, "Pool not specified");
403: return -1;
404: }
405: /* check address range & sentinel */
406: assert(!MEM_BADADDR(data) && !MEM_CORRUPT(data));
407: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
408: elwix_SetErr(EFAULT, "Corrupted memory address");
409: return -2;
410: } else
411: idx = BucketIndex(((u_int*)data)[-2] * sizeof(u_int));
412:
413: mpool_lock(mp);
414: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
415: if (mem_data(m, void*) == data) {
416: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
417: /* statistics */
418: mp->pool_calls.alloc--;
419: mp->pool_bytes.alloc -= mem_size(m);
420:
421: if (!purge) {
422: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
423: /* statistics */
424: mp->pool_calls.cache++;
425: mp->pool_bytes.cache += mem_size(m);
426: } else {
427: /* statistics */
428: mp->pool_calls.free++;
429: mp->pool_bytes.free += mem_size(m);
430: /* quota */
431: mp->pool_quota.curr -= mem_size(m);
1.5 misho 432: mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
1.1 misho 433:
434: if (m->alloc_mem)
435: free(m->alloc_mem);
436: free(m);
437: }
438: break;
439: }
440: mpool_unlock(mp);
441:
442: return 0;
443: }
444:
445: /*
446: * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
447: *
448: * @mp = Memory pool
449: * @size = Allocated memory data size
450: * @memname = Memory name
451: * @purge = if !=0 force release memory block
452: * return: <0 error or 0 ok released memory block
453: */
454: int
455: mpool_free2(mpool_t * __restrict mp, u_int size, const char *memname, int purge)
456: {
457: int idx;
458: struct tagAlloc *m, *tmp;
459:
460: if (!mp || !memname) {
461: elwix_SetErr(EINVAL, "Pool or memory name is not specified");
462: return -1;
463: } else
464: idx = BucketIndex(size);
465:
466: mpool_lock(mp);
467: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
468: if (!strcmp(m->alloc_name, memname)) {
469: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
470: /* statistics */
471: mp->pool_calls.alloc--;
472: mp->pool_bytes.alloc -= mem_size(m);
473:
474: if (!purge) {
475: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
476: /* statistics */
477: mp->pool_calls.cache++;
478: mp->pool_bytes.cache += mem_size(m);
479: } else {
480: /* statistics */
481: mp->pool_calls.free++;
482: mp->pool_bytes.free += mem_size(m);
483: /* quota */
484: mp->pool_quota.curr -= mem_size(m);
1.5 misho 485: mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
1.1 misho 486:
487: if (m->alloc_mem)
488: free(m->alloc_mem);
489: free(m);
490: }
491: break;
492: }
493: mpool_unlock(mp);
494:
495: return 0;
496: }
497:
498: /*
499: * mpool_strdup() - String duplicate
500: *
501: * @mp = Memory pool
502: * @str = String
503: * @memname = Memory name
504: * return: NULL error or !=NULL new string
505: */
506: char *
507: mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname)
508: {
509: char *s = NULL;
510: u_int len;
511:
512: if (!mp) {
513: elwix_SetErr(EINVAL, "Pool not specified");
514: return NULL;
515: }
516: if (!str) {
517: elwix_SetErr(EINVAL, "String is NULL");
518: return NULL;
519: } else
520: len = strlen(str) + 1;
521:
522: s = mpool_malloc(mp, len, memname);
523: if (!s)
524: return NULL;
525: else
526: memcpy(s, str, len);
527:
528: return s;
529: }
530:
531: /*
532: * mpool_getmembynam() Find allocated memory block by size and memory name
533: *
534: * @mp = Memory pool
535: * @size = Memory size
536: * @memname = Memory name
537: * return: NULL error or not found and !=NULL allocated memory
538: */
1.2 misho 539: struct tagAlloc *
1.1 misho 540: mpool_getmembynam(mpool_t * __restrict mp, u_int size, const char *memname)
541: {
542: int idx;
543: struct tagAlloc *m = NULL;
544:
545: if (!mp || !memname)
546: return NULL;
547:
548: idx = BucketIndex(size);
549: TAILQ_FOREACH(m, &mp->pool_active[idx], alloc_node)
550: if (!strcmp(m->alloc_name, memname))
551: break;
552:
553: return mem_data(m, void*);
554: }
555:
556: /*
557: * mpool_getsizebyaddr() - Get size of allocated memory block by address
558: *
559: * @addr = allocated memory from mpool_malloc()
560: * return: usable size of allocated memory block
561: */
1.2 misho 562: u_int
1.1 misho 563: mpool_getsizebyaddr(void * __restrict data)
564: {
565: if (mpool_chkaddr(data))
566: return 0;
567:
568: return (((u_int*) data)[-2] * sizeof(u_int));
569: }
570:
571: /*
572: * mpool_chkaddr() - Check validity of given address
573: *
574: * @data = allocated memory from mpool_malloc()
575: * return: -1 bad address, 1 corrupted address or 0 ok
576: */
1.2 misho 577: int
1.1 misho 578: mpool_chkaddr(void * __restrict data)
579: {
580: /* check address range */
581: if (MEM_BADADDR(data))
582: return -1;
583: /* check sentinel */
584: if (MEM_CORRUPT(data))
585: return 1;
586: /* data address is ok! */
587: return 0;
588: }
589:
590: /*
591: * mpool_setquota() - Change maximum memory quota
592: *
593: * @mp = Memory pool
594: * @maxmem = New max quota size
595: * return: old maximum memory quota size
596: */
1.2 misho 597: u_long
1.1 misho 598: mpool_setquota(mpool_t * __restrict mp, u_long maxmem)
599: {
600: u_long ret;
601:
602: if (!mp)
603: return 0;
604:
605: ret = mp->pool_quota.max;
606: mp->pool_quota.max = maxmem;
607:
608: /* if new max quota is less then current allocated memory,
609: * try to purge memory cache blocks
610: */
611: if (mp->pool_quota.max < mp->pool_quota.curr)
612: mpool_purge(mp, 0);
613:
614: return ret;
615: }
616:
617: /*
618: * mpool_getquota() - Get memory quota
619: *
620: * @mp = Memory pool
1.5 misho 621: * @currmem = Return current memory usage
622: * @realmem = Return current real memory usage
1.1 misho 623: * @maxmem = Return max quota size
624: * return: none
625: */
1.2 misho 626: void
1.5 misho 627: mpool_getquota(mpool_t * __restrict mp, u_long *currmem, u_long *realmem, u_long *maxmem)
1.1 misho 628: {
629: if (!mp)
630: return;
631:
632: if (maxmem)
633: *maxmem = mp->pool_quota.max;
1.5 misho 634: if (realmem)
635: *realmem = mp->pool_quota.real;
1.1 misho 636: if (currmem)
637: *currmem = mp->pool_quota.curr;
638: }
639:
640: /* ----------------------------------------------------------- */
641:
642: /*
643: * mpool_statistics() - Dump statistics from memory pool buckets
644: *
645: * @mp = Memory pool
646: * @cb = Export statistics to callback
1.9 misho 647: * return: -1 error or >0 bytes in data buffer
1.1 misho 648: */
1.9 misho 649: int
650: mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb, void *data, u_int datlen)
1.1 misho 651: {
652: struct tagAlloc *m;
653: register int i, act, inact;
1.9 misho 654: int len = 0;
1.1 misho 655:
656: if (!mp || !cb)
1.9 misho 657: return -1;
1.1 misho 658:
659: for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) {
660: TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node)
661: act++;
662: TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node)
663: inact++;
664:
1.9 misho 665: len += cb(1 << (i + MEM_MIN_BUCKET), act, inact, data, datlen);
1.1 misho 666: }
1.9 misho 667:
668: return len;
1.1 misho 669: }
670:
671: /* ----------------------------------------------------------- */
672:
673: /*
674: * mpool_xmalloc() - malloc wrapper
675: *
676: * @size = Size
677: * return: NULL error or !=NULL ok allocated memory
678: */
679: void *
680: mpool_xmalloc(size_t size)
681: {
682: return mpool_malloc(elwix_mpool, size, elwix_Prog);
683: }
684:
685: /*
686: * mpool_xcalloc() - calloc wrapper
687: *
688: * @num = number of elements
689: * @size = Size of element
690: * return: NULL error or !=NULL ok allocated memory
691: */
692: void *
693: mpool_xcalloc(size_t num, size_t size)
694: {
1.10 misho 695: return mpool_calloc(elwix_mpool, num, size, elwix_Prog);
1.1 misho 696: }
697:
698: /*
699: * mpool_xrealloc() - realloc wrapper
700: *
701: * @data = Allocated memory data
702: * @newsize = New size of memory block
703: * return: NULL error or !=NULL new reallocated memory block
704: */
705: void *
706: mpool_xrealloc(void * __restrict data, size_t newsize)
707: {
708: return mpool_realloc(elwix_mpool, data, newsize, elwix_Prog);
709: }
710:
711: /*
712: * mpool_xfree() - free wrapper
713: *
714: * @data = Allocated memory data
715: * return: none
716: */
717: void
718: mpool_xfree(void * __restrict data)
719: {
720: mpool_free(elwix_mpool, data, 0);
721: }
722:
723: /*
724: * mpool_xstrdup() - strdup wrapper
725: *
726: * @str = string
727: * return: =NULL error or !=NULL new allocated string
728: */
729: char *
730: mpool_xstrdup(const char *str)
731: {
732: return mpool_strdup(elwix_mpool, str, elwix_Prog);
733: }
1.5 misho 734:
735: /*
1.9 misho 736: * mpool_xstatistics() - elwix default memory pool statistics wrapper
1.5 misho 737: *
738: * @cb = Export statistics to callback
1.9 misho 739: * @data = data buffer
740: * @datlen = data buffer length
741: * return: >0 data in string buffer
1.5 misho 742: */
1.9 misho 743: int
744: mpool_xstatistics(mpool_stat_cb cb, void *data, u_int datlen)
1.5 misho 745: {
1.9 misho 746: return mpool_statistics(elwix_mpool, cb, data, datlen);
1.5 misho 747: }
748:
1.9 misho 749: static int
750: xdump_show(u_int size, u_int act, u_int inact, void *data, u_int datlen)
1.5 misho 751: {
752: if (!act && !inact)
1.9 misho 753: return 0; /* skip empty bucket */
1.5 misho 754:
755: if (size < 1024)
756: printf("\t\t* BUCKET %uB size, %u active, %u inactive\n",
757: size, act, inact);
758: else if (size < 1048576)
759: printf("\t\t* BUCKET %uKB size, %u active, %u inactive\n",
760: size / 1024, act, inact);
761: else
762: printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n",
763: size / 1048576, act, inact);
1.9 misho 764:
765: return 0;
766: }
767:
768: static int
769: xdump_show2(u_int size, u_int act, u_int inact, void *data, u_int datlen)
770: {
771: char szStr[STRSIZ], *str = data;
772: int len = 0;
773:
774: if (!data || !datlen)
775: return 0; /* skip missing data buffer */
776: if (!act && !inact)
777: return 0; /* skip empty bucket */
778:
779: if (size < 1024)
780: len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uB size, %u active, %u inactive\n",
781: size, act, inact);
782: else if (size < 1048576)
783: len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uKB size, %u active, %u inactive\n",
784: size / 1024, act, inact);
785: else
786: len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uMB size, %u active, %u inactive\n",
787: size / 1048576, act, inact);
788:
789: strlcat(str, szStr, datlen);
790: return len;
1.5 misho 791: }
792:
793: /*
1.6 misho 794: * mpool_dump() - Dump elwix memory pool statistics
1.5 misho 795: *
1.6 misho 796: * @mp = memory pool, if =NULL dump elwix default memory pool
797: * @fmt = prefix info format string
1.5 misho 798: * @... = argument(s)
799: * return: none
800: */
801: void
1.6 misho 802: mpool_dump(mpool_t * __restrict mp, const char *fmt, ...)
1.5 misho 803: {
804: va_list lst;
1.6 misho 805: mpool_t *p = mp ? mp : elwix_mpool;
1.5 misho 806:
807: if (fmt) {
808: va_start(lst, fmt);
809: vprintf(fmt, lst);
810: va_end(lst);
811: } else
812: printf("\n%s(%d)\n", __func__, __LINE__);
813:
814: printf("------------------------------------------------------------\n");
1.9 misho 815: printf( " ELWIX memory pool %p ::\n"
1.5 misho 816: "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"
817: "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"
818: "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"
1.9 misho 819: "\t- buckets :\n", mp,
1.6 misho 820: p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max,
821: p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache,
822: p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
1.5 misho 823:
1.9 misho 824: mpool_statistics(p, xdump_show, NULL, 0);
1.5 misho 825: printf("------------------------------------------------------------\n");
826: }
827:
1.9 misho 828: /*
829: * mpool_dump2() - Dump elwix memory pool statistics to string
830: *
831: * @mp = memory pool, if =NULL dump elwix default memory pool
832: * @str = string buffer
833: * @strlen = string buffer length
834: * return: >0 data in string buffer
835: */
836: int
837: mpool_dump2(mpool_t * __restrict mp, char *str, int strlen)
838: {
839: int len;
840: mpool_t *p = mp ? mp : elwix_mpool;
841:
842: len = snprintf(str, strlen,
843: "------------------------------------------------------------\n");
844: len += snprintf(str + len, strlen - len, " ELWIX memory pool %p ::\n"
845: "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"
846: "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"
847: "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"
848: "\t- buckets :\n", mp,
849: p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max,
850: p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache,
851: p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
852: len += mpool_statistics(p, xdump_show2, str + len, strlen - len);
853: len += snprintf(str + len, strlen - len,
854: "------------------------------------------------------------\n");
855:
856: return len;
857: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>