Annotation of libelwix/src/mem.c, revision 1.6
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.6 ! misho 6: * $Id: mem.c,v 1.5.2.2 2015/07/01 21:47:19 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.5 misho 15: Copyright 2004 - 2015
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: {
94: struct tagAlloc *m;
95: register int i;
96:
97: if (!mp && !*mp)
98: return;
99:
100: mpool_lock(*mp);
101:
102: for (i = 0; i < MEM_BUCKETS; i++) {
103: while ((m = TAILQ_FIRST(&(*mp)->pool_active[i]))) {
104: TAILQ_REMOVE(&(*mp)->pool_active[i], m, alloc_node);
105: if (m->alloc_mem)
106: free(m->alloc_mem);
107: free(m);
108: }
109: while ((m = TAILQ_FIRST(&(*mp)->pool_inactive[i]))) {
110: TAILQ_REMOVE(&(*mp)->pool_inactive[i], m, alloc_node);
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.1 misho 222: memset(m->alloc_mem, 0, align + 12);
223: }
224: }
225:
226: m->alloc_mem[0] = size / sizeof(u_int);
227: m->alloc_mem[1] = MEM_MAGIC_START;
228: m->alloc_mem[2 + size / sizeof(u_int)] = MEM_MAGIC_STOP;
229: TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node);
230: /* statistics */
231: mp->pool_calls.alloc++;
232: mp->pool_bytes.alloc += size;
233:
234: mpool_unlock(mp);
235: return mem_data(m, void*);
236: }
237:
238: /*
239: * mpool_realloc() Reallocate memory block with new size
240: *
241: * @mp = Memory pool
242: * @data = Allocated memory data
243: * @newsize = New size of memory block
244: * @memname = Optional new memory block name
245: * return: NULL error or !=NULL new reallocated memory block
246: */
247: void *
248: mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname)
249: {
250: int idx, oidx;
251: void *p;
1.6 ! misho 252: u_int osize;
1.1 misho 253:
254: /* if !data execute mpool_malloc() */
255: if (!data)
256: return mpool_malloc(mp, newsize, memname);
257:
258: if (!mp) {
259: elwix_SetErr(EINVAL, "Pool not specified");
260: return NULL;
261: }
262: /* check address range & sentinel */
263: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
264: elwix_SetErr(EFAULT, "Corrupted memory address");
265: return NULL;
266: }
267: /* prepare new size */
268: if (newsize > MEM_ALLOC_MAX) {
269: elwix_SetErr(ENOMEM, "Memory size is too large");
270: return NULL;
271: }
272:
273: mpool_lock(mp);
274:
1.6 ! misho 275: osize = ((u_int*)data)[-2] * sizeof(u_int);
! 276: oidx = BucketIndex(osize);
! 277: newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */
! 278: idx = BucketIndex(newsize);
! 279:
1.1 misho 280: /* quota */
281: if (mp->pool_quota.max &&
282: (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) {
283: elwix_SetErr(ENOMEM, "Max.allocate memory quota has been reached");
284: mpool_unlock(mp);
285: return NULL;
286: }
287:
1.6 ! misho 288: if (oidx != idx) {
1.1 misho 289: mpool_unlock(mp);
1.6 ! misho 290: p = mpool_malloc(mp, newsize, memname);
! 291: if (!p)
! 292: return NULL;
1.1 misho 293:
1.6 ! misho 294: memcpy(p, data, osize);
! 295: mpool_free(mp, data, 0);
! 296: } else {
! 297: p = data;
1.1 misho 298:
1.6 ! misho 299: ((u_int*) p)[-2] = newsize / sizeof(u_int);
! 300: ((u_int*) p)[newsize / sizeof(u_int)] = MEM_MAGIC_STOP;
1.1 misho 301:
1.6 ! misho 302: mp->pool_bytes.alloc += (u_long) newsize - osize;
! 303: mp->pool_quota.curr += (u_long) newsize - osize;
1.1 misho 304:
1.6 ! misho 305: mpool_unlock(mp);
1.1 misho 306: }
307:
1.6 ! misho 308: return p;
1.1 misho 309: }
310:
311: /*
312: * mpool_purge() - Purge memory block cache and release resources
313: *
314: * @mp = Memory pool
315: * @atmost = Free at most in buckets
316: * return: -1 error or 0 ok
317: */
318: int
319: mpool_purge(mpool_t * __restrict mp, u_int atmost)
320: {
321: register int i, cx;
322: struct tagAlloc *m, *tmp;
323:
324: if (!mp) {
325: elwix_SetErr(EINVAL, "Pool not specified");
326: return -1;
327: }
328:
329: mpool_lock(mp);
330:
331: for (i = cx = 0; i < MEM_BUCKETS; cx = 0, i++) {
332: TAILQ_FOREACH_SAFE(m, &mp->pool_inactive[i], alloc_node, tmp) {
333: /* barrier for purge */
334: if (cx < atmost) {
335: cx++;
336: continue;
337: }
338:
339: TAILQ_REMOVE(&mp->pool_inactive[i], m, alloc_node);
340: /* statistics */
341: mp->pool_calls.cache--;
342: mp->pool_bytes.cache -= mem_size(m);
343:
344: mp->pool_calls.free++;
345: mp->pool_bytes.free += mem_size(m);
346: /* quota */
347: mp->pool_quota.curr -= mem_size(m);
1.5 misho 348: mp->pool_quota.real -= 1 << (i + MEM_MIN_BUCKET);
1.1 misho 349:
350: if (m->alloc_mem)
351: free(m->alloc_mem);
352: free(m);
353: }
354: }
355:
356: mpool_unlock(mp);
357: return 0;
358: }
359:
360: /*
361: * mpool_free() Free allocated memory with mpool_alloc()
362: *
363: * @mp = Memory pool
364: * @data = Allocated memory data
365: * @purge = if !=0 force release memory block
366: * return: <0 error or 0 ok released memory block
367: */
368: int
369: mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge)
370: {
371: int idx;
372: struct tagAlloc *m, *tmp;
373:
1.4 misho 374: if (!data)
375: return 0;
1.1 misho 376: if (!mp) {
377: elwix_SetErr(EINVAL, "Pool not specified");
378: return -1;
379: }
380: /* check address range & sentinel */
381: assert(!MEM_BADADDR(data) && !MEM_CORRUPT(data));
382: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
383: elwix_SetErr(EFAULT, "Corrupted memory address");
384: return -2;
385: } else
386: idx = BucketIndex(((u_int*)data)[-2] * sizeof(u_int));
387:
388: mpool_lock(mp);
389: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
390: if (mem_data(m, void*) == data) {
391: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
392: /* statistics */
393: mp->pool_calls.alloc--;
394: mp->pool_bytes.alloc -= mem_size(m);
395:
396: if (!purge) {
397: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
398: /* statistics */
399: mp->pool_calls.cache++;
400: mp->pool_bytes.cache += mem_size(m);
401: } else {
402: /* statistics */
403: mp->pool_calls.free++;
404: mp->pool_bytes.free += mem_size(m);
405: /* quota */
406: mp->pool_quota.curr -= mem_size(m);
1.5 misho 407: mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
1.1 misho 408:
409: if (m->alloc_mem)
410: free(m->alloc_mem);
411: free(m);
412: }
413: break;
414: }
415: mpool_unlock(mp);
416:
417: return 0;
418: }
419:
420: /*
421: * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
422: *
423: * @mp = Memory pool
424: * @size = Allocated memory data size
425: * @memname = Memory name
426: * @purge = if !=0 force release memory block
427: * return: <0 error or 0 ok released memory block
428: */
429: int
430: mpool_free2(mpool_t * __restrict mp, u_int size, const char *memname, int purge)
431: {
432: int idx;
433: struct tagAlloc *m, *tmp;
434:
435: if (!mp || !memname) {
436: elwix_SetErr(EINVAL, "Pool or memory name is not specified");
437: return -1;
438: } else
439: idx = BucketIndex(size);
440:
441: mpool_lock(mp);
442: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
443: if (!strcmp(m->alloc_name, memname)) {
444: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
445: /* statistics */
446: mp->pool_calls.alloc--;
447: mp->pool_bytes.alloc -= mem_size(m);
448:
449: if (!purge) {
450: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
451: /* statistics */
452: mp->pool_calls.cache++;
453: mp->pool_bytes.cache += mem_size(m);
454: } else {
455: /* statistics */
456: mp->pool_calls.free++;
457: mp->pool_bytes.free += mem_size(m);
458: /* quota */
459: mp->pool_quota.curr -= mem_size(m);
1.5 misho 460: mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
1.1 misho 461:
462: if (m->alloc_mem)
463: free(m->alloc_mem);
464: free(m);
465: }
466: break;
467: }
468: mpool_unlock(mp);
469:
470: return 0;
471: }
472:
473: /*
474: * mpool_strdup() - String duplicate
475: *
476: * @mp = Memory pool
477: * @str = String
478: * @memname = Memory name
479: * return: NULL error or !=NULL new string
480: */
481: char *
482: mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname)
483: {
484: char *s = NULL;
485: u_int len;
486:
487: if (!mp) {
488: elwix_SetErr(EINVAL, "Pool not specified");
489: return NULL;
490: }
491: if (!str) {
492: elwix_SetErr(EINVAL, "String is NULL");
493: return NULL;
494: } else
495: len = strlen(str) + 1;
496:
497: s = mpool_malloc(mp, len, memname);
498: if (!s)
499: return NULL;
500: else
501: memcpy(s, str, len);
502:
503: return s;
504: }
505:
506: /*
507: * mpool_getmembynam() Find allocated memory block by size and memory name
508: *
509: * @mp = Memory pool
510: * @size = Memory size
511: * @memname = Memory name
512: * return: NULL error or not found and !=NULL allocated memory
513: */
1.2 misho 514: struct tagAlloc *
1.1 misho 515: mpool_getmembynam(mpool_t * __restrict mp, u_int size, const char *memname)
516: {
517: int idx;
518: struct tagAlloc *m = NULL;
519:
520: if (!mp || !memname)
521: return NULL;
522:
523: idx = BucketIndex(size);
524: TAILQ_FOREACH(m, &mp->pool_active[idx], alloc_node)
525: if (!strcmp(m->alloc_name, memname))
526: break;
527:
528: return mem_data(m, void*);
529: }
530:
531: /*
532: * mpool_getsizebyaddr() - Get size of allocated memory block by address
533: *
534: * @addr = allocated memory from mpool_malloc()
535: * return: usable size of allocated memory block
536: */
1.2 misho 537: u_int
1.1 misho 538: mpool_getsizebyaddr(void * __restrict data)
539: {
540: if (mpool_chkaddr(data))
541: return 0;
542:
543: return (((u_int*) data)[-2] * sizeof(u_int));
544: }
545:
546: /*
547: * mpool_chkaddr() - Check validity of given address
548: *
549: * @data = allocated memory from mpool_malloc()
550: * return: -1 bad address, 1 corrupted address or 0 ok
551: */
1.2 misho 552: int
1.1 misho 553: mpool_chkaddr(void * __restrict data)
554: {
555: /* check address range */
556: if (MEM_BADADDR(data))
557: return -1;
558: /* check sentinel */
559: if (MEM_CORRUPT(data))
560: return 1;
561: /* data address is ok! */
562: return 0;
563: }
564:
565: /*
566: * mpool_setquota() - Change maximum memory quota
567: *
568: * @mp = Memory pool
569: * @maxmem = New max quota size
570: * return: old maximum memory quota size
571: */
1.2 misho 572: u_long
1.1 misho 573: mpool_setquota(mpool_t * __restrict mp, u_long maxmem)
574: {
575: u_long ret;
576:
577: if (!mp)
578: return 0;
579:
580: ret = mp->pool_quota.max;
581: mp->pool_quota.max = maxmem;
582:
583: /* if new max quota is less then current allocated memory,
584: * try to purge memory cache blocks
585: */
586: if (mp->pool_quota.max < mp->pool_quota.curr)
587: mpool_purge(mp, 0);
588:
589: return ret;
590: }
591:
592: /*
593: * mpool_getquota() - Get memory quota
594: *
595: * @mp = Memory pool
1.5 misho 596: * @currmem = Return current memory usage
597: * @realmem = Return current real memory usage
1.1 misho 598: * @maxmem = Return max quota size
599: * return: none
600: */
1.2 misho 601: void
1.5 misho 602: mpool_getquota(mpool_t * __restrict mp, u_long *currmem, u_long *realmem, u_long *maxmem)
1.1 misho 603: {
604: if (!mp)
605: return;
606:
607: if (maxmem)
608: *maxmem = mp->pool_quota.max;
1.5 misho 609: if (realmem)
610: *realmem = mp->pool_quota.real;
1.1 misho 611: if (currmem)
612: *currmem = mp->pool_quota.curr;
613: }
614:
615: /* ----------------------------------------------------------- */
616:
617: /*
618: * mpool_statistics() - Dump statistics from memory pool buckets
619: *
620: * @mp = Memory pool
621: * @cb = Export statistics to callback
622: * return: none
623: */
624: void
625: mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb)
626: {
627: struct tagAlloc *m;
628: register int i, act, inact;
629:
630: if (!mp || !cb)
631: return;
632:
633: for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) {
634: TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node)
635: act++;
636: TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node)
637: inact++;
638:
639: cb(1 << (i + MEM_MIN_BUCKET), act, inact);
640: }
641: }
642:
643: /* ----------------------------------------------------------- */
644:
645: /*
646: * mpool_xmalloc() - malloc wrapper
647: *
648: * @size = Size
649: * return: NULL error or !=NULL ok allocated memory
650: */
651: void *
652: mpool_xmalloc(size_t size)
653: {
654: return mpool_malloc(elwix_mpool, size, elwix_Prog);
655: }
656:
657: /*
658: * mpool_xcalloc() - calloc wrapper
659: *
660: * @num = number of elements
661: * @size = Size of element
662: * return: NULL error or !=NULL ok allocated memory
663: */
664: void *
665: mpool_xcalloc(size_t num, size_t size)
666: {
667: return mpool_malloc(elwix_mpool, num * size, elwix_Prog);
668: }
669:
670: /*
671: * mpool_xrealloc() - realloc wrapper
672: *
673: * @data = Allocated memory data
674: * @newsize = New size of memory block
675: * return: NULL error or !=NULL new reallocated memory block
676: */
677: void *
678: mpool_xrealloc(void * __restrict data, size_t newsize)
679: {
680: return mpool_realloc(elwix_mpool, data, newsize, elwix_Prog);
681: }
682:
683: /*
684: * mpool_xfree() - free wrapper
685: *
686: * @data = Allocated memory data
687: * return: none
688: */
689: void
690: mpool_xfree(void * __restrict data)
691: {
692: mpool_free(elwix_mpool, data, 0);
693: }
694:
695: /*
696: * mpool_xstrdup() - strdup wrapper
697: *
698: * @str = string
699: * return: =NULL error or !=NULL new allocated string
700: */
701: char *
702: mpool_xstrdup(const char *str)
703: {
704: return mpool_strdup(elwix_mpool, str, elwix_Prog);
705: }
1.5 misho 706:
707: /*
708: * mpool_xstatistics() - elwix memory pool statistics wrapper
709: *
710: * @cb = Export statistics to callback
711: * return: none
712: */
713: void
714: mpool_xstatistics(mpool_stat_cb cb)
715: {
716: mpool_statistics(elwix_mpool, cb);
717: }
718:
719: static void
720: xdump_show(u_int size, u_int act, u_int inact)
721: {
722: if (!act && !inact)
723: return; /* skip empty bucket */
724:
725: if (size < 1024)
726: printf("\t\t* BUCKET %uB size, %u active, %u inactive\n",
727: size, act, inact);
728: else if (size < 1048576)
729: printf("\t\t* BUCKET %uKB size, %u active, %u inactive\n",
730: size / 1024, act, inact);
731: else
732: printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n",
733: size / 1048576, act, inact);
734: }
735:
736: /*
1.6 ! misho 737: * mpool_dump() - Dump elwix memory pool statistics
1.5 misho 738: *
1.6 ! misho 739: * @mp = memory pool, if =NULL dump elwix default memory pool
! 740: * @fmt = prefix info format string
1.5 misho 741: * @... = argument(s)
742: * return: none
743: */
744: void
1.6 ! misho 745: mpool_dump(mpool_t * __restrict mp, const char *fmt, ...)
1.5 misho 746: {
747: va_list lst;
1.6 ! misho 748: mpool_t *p = mp ? mp : elwix_mpool;
1.5 misho 749:
750: if (fmt) {
751: va_start(lst, fmt);
752: vprintf(fmt, lst);
753: va_end(lst);
754: } else
755: printf("\n%s(%d)\n", __func__, __LINE__);
756:
757: printf("------------------------------------------------------------\n");
758: printf( " ELWIX memory pool ::\n"
759: "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"
760: "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"
761: "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"
762: "\t- buckets :\n",
1.6 ! misho 763: p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max,
! 764: p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache,
! 765: p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
1.5 misho 766:
1.6 ! misho 767: mpool_statistics(p, xdump_show);
1.5 misho 768: printf("------------------------------------------------------------\n");
769: }
770:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>