Annotation of libaitsess/src/mem.c, revision 1.1.2.9
1.1.2.2 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.1.2.9 ! misho 6: * $Id: mem.c,v 1.1.2.8 2012/02/28 12:01:12 misho Exp $
1.1.2.2 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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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: */
1.1.2.1 misho 46: #include "global.h"
47:
48:
49: /*
50: * mpool_init() - Init memory pool
51: *
1.1.2.6 misho 52: * @maxmem = If !=0 set maximum memory quota
1.1.2.1 misho 53: * return: =NULL error or !=NULL new allocated pool
54: */
55: mpool_t *
1.1.2.6 misho 56: mpool_init(u_long maxmem)
1.1.2.1 misho 57: {
58: mpool_t *mp;
1.1.2.3 misho 59: register int i;
1.1.2.1 misho 60:
61: mp = malloc(sizeof(mpool_t));
62: if (!mp) {
63: LOGERR;
64: return NULL;
65: } else
66: memset(mp, 0, sizeof(mpool_t));
67:
68: pthread_mutex_init(&mp->pool_mtx, NULL);
1.1.2.3 misho 69:
1.1.2.6 misho 70: mp->pool_quota.max = maxmem;
71:
1.1.2.3 misho 72: mpool_lock(mp);
73: for (i = 0; i < MEM_BUCKETS; i++) {
74: TAILQ_INIT(&mp->pool_active[i]);
75: TAILQ_INIT(&mp->pool_inactive[i]);
76: }
77: mpool_unlock(mp);
78:
1.1.2.1 misho 79: return mp;
80: }
81:
82: /*
83: * mpool_destroy() - Destroy memory pool
84: *
85: * @mp = Memory pool
86: * return: none
87: */
88: void
89: mpool_destroy(mpool_t ** __restrict mp)
90: {
91: struct tagAlloc *m;
92: register int i;
93:
94: if (!mp && !*mp)
95: return;
96:
97: mpool_lock(*mp);
98:
99: for (i = 0; i < MEM_BUCKETS; i++) {
100: while ((m = TAILQ_FIRST(&(*mp)->pool_active[i]))) {
101: TAILQ_REMOVE(&(*mp)->pool_active[i], m, alloc_node);
102: if (m->alloc_mem)
103: free(m->alloc_mem);
104: free(m);
105: }
106: while ((m = TAILQ_FIRST(&(*mp)->pool_inactive[i]))) {
107: TAILQ_REMOVE(&(*mp)->pool_inactive[i], m, alloc_node);
108: if (m->alloc_mem)
109: free(m->alloc_mem);
110: free(m);
111: }
112: }
113:
114: mpool_unlock(*mp);
115: pthread_mutex_destroy(&(*mp)->pool_mtx);
116:
117: free(*mp);
118: *mp = NULL;
119: }
120:
121: /* ----------------------------------------------------------- */
122:
123: static inline long
124: BucketIndex(u_int size)
125: {
126: register long b;
127:
128: if (!size--)
129: return 0; /* min bucket position in array */
130:
131: for (b = MEM_MIN_BUCKET; b < MEM_MAX_BUCKET; b++)
132: if (!(size >> b))
133: break;
134:
135: return b - MEM_MIN_BUCKET; /* convert to bucket array index */
136: }
137:
138: static inline struct tagAlloc *
139: pullInactive(mpool_t * __restrict mp, int idx)
140: {
141: struct tagAlloc *m = NULL;
142:
143: /* must be locked pool before use this function */
144: if ((m = TAILQ_FIRST(&mp->pool_inactive[idx]))) {
145: TAILQ_REMOVE(&mp->pool_inactive[idx], m, alloc_node);
1.1.2.3 misho 146: /* statistics */
147: mp->pool_calls.cache--;
148: mp->pool_bytes.cache -= mem_size(m);
1.1.2.1 misho 149:
150: /* clear name */
151: *m->alloc_name = 0;
152: /* clear flags */
153: m->alloc_flags ^= m->alloc_flags;
154: }
155:
156: return m;
157: }
158:
159: /*
160: * mpool_malloc() - Memory allocation
161: *
162: * @mp = Memory pool
163: * @size = Size
164: * @memname = Optional memory block name
165: * return: NULL error or !=NULL ok allocated memory
166: */
167: void *
168: mpool_malloc(mpool_t * __restrict mp, u_int size, const char *memname)
169: {
170: struct tagAlloc *m;
1.1.2.8 misho 171: int idx;
172: u_int align;
1.1.2.1 misho 173:
174: if (!mp) {
175: sess_SetErr(EINVAL, "Pool not specified");
176: return NULL;
177: }
178: if (size > MEM_ALLOC_MAX) {
179: sess_SetErr(ENOMEM, "Memory size is too large");
180: return NULL;
181: } else
182: size = (size + 3) & ~3; /* must align to 4 because needed room for sentinels */
183:
184: idx = BucketIndex(size);
185:
186: mpool_lock(mp);
187:
188: /* get memory from cache if exists */
189: if (!(m = pullInactive(mp, idx))) {
190: /* quota */
191: if (mp->pool_quota.max &&
192: (mp->pool_quota.curr + size) > mp->pool_quota.max) {
193: sess_SetErr(ENOMEM, "Max.allocate memory quota has been reached");
194: mpool_unlock(mp);
195: return NULL;
196: }
197:
198: m = malloc(sizeof(struct tagAlloc));
199: if (!m) {
200: LOGERR;
201: mpool_unlock(mp);
202: return NULL;
203: } else
204: memset(m, 0, sizeof(struct tagAlloc));
205: }
206:
207: if (memname)
208: strlcpy(m->alloc_name, memname, sizeof m->alloc_name);
209:
210: if (!m->alloc_mem) {
211: align = 1 << (idx + MEM_MIN_BUCKET);
212: m->alloc_mem = malloc(align + 12); /* +12 sentinel bytes */
213: if (!m->alloc_mem) {
214: LOGERR;
215: free(m);
216: mpool_unlock(mp);
217: return NULL;
218: } else /* quota */
219: mp->pool_quota.curr += size;
220: }
221:
222: m->alloc_mem[0] = size / sizeof(u_int);
223: m->alloc_mem[1] = MEM_MAGIC_START;
224: m->alloc_mem[2 + size / sizeof(u_int)] = MEM_MAGIC_STOP;
225: TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node);
226: /* statistics */
227: mp->pool_calls.alloc++;
228: mp->pool_bytes.alloc += size;
229:
230: mpool_unlock(mp);
231: return mem_data(m, void*);
232: }
233:
234: /*
1.1.2.7 misho 235: * mpool_realloc() Reallocate memory block with new size
236: *
237: * @mp = Memory pool
238: * @data = Allocated memory data
239: * @newsize = New size of memory block
240: * @memname = Optional new memory block name
241: * return: NULL error or !=NULL new reallocated memory block
242: */
243: void *
244: mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname)
245: {
246: struct tagAlloc *m, *tmp;
1.1.2.8 misho 247: int idx, oidx;
1.1.2.7 misho 248: void *p;
1.1.2.8 misho 249: u_int align, osize;
1.1.2.7 misho 250:
251: /* if !data execute mpool_malloc() */
252: if (!data)
253: return mpool_malloc(mp, newsize, memname);
254:
255: if (!mp) {
256: sess_SetErr(EINVAL, "Pool not specified");
257: return NULL;
258: }
259: /* check address range & sentinel */
260: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
261: sess_SetErr(EFAULT, "Corrupted memory address");
262: return NULL;
263: } else {
264: osize = ((u_int*)data)[-2] * sizeof(u_int);
265: oidx = BucketIndex(osize);
266: }
267: /* prepare new size */
268: if (newsize > MEM_ALLOC_MAX) {
269: sess_SetErr(ENOMEM, "Memory size is too large");
270: return NULL;
271: } else {
272: newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */
273: idx = BucketIndex(newsize);
274: }
275:
276: mpool_lock(mp);
277:
278: /* quota */
279: if (mp->pool_quota.max &&
280: (mp->pool_quota.curr + (newsize - osize)) > mp->pool_quota.max) {
281: sess_SetErr(ENOMEM, "Max.allocate memory quota has been reached");
282: mpool_unlock(mp);
283: return NULL;
284: }
285:
286: /* find old memory block */
1.1.2.8 misho 287: TAILQ_FOREACH_SAFE(m, &mp->pool_active[oidx], alloc_node, tmp) {
1.1.2.7 misho 288: if (mem_data(m, void*) == data && mem_size(m) == osize) {
1.1.2.8 misho 289: /* case in different buckets */
290: if (oidx != idx) {
291: TAILQ_REMOVE(&mp->pool_active[oidx], m, alloc_node);
292: /* statistics */
293: mp->pool_calls.alloc--;
294: }
1.1.2.7 misho 295: mp->pool_bytes.alloc -= osize;
1.1.2.8 misho 296: break;
1.1.2.7 misho 297: }
1.1.2.8 misho 298: }
1.1.2.7 misho 299: /* memory block not found! */
300: if (!m) {
301: mpool_unlock(mp);
302: sess_SetErr(EFAULT, "Memory block not found");
303: return NULL;
304: }
305:
306: /* try to reallocate memory block to new bucket */
1.1.2.8 misho 307: if (oidx != idx) {
308: align = 1 << (idx + MEM_MIN_BUCKET);
309: p = realloc(m->alloc_mem, align + 12);
310: if (!p) {
311: LOGERR;
1.1.2.7 misho 312:
1.1.2.8 misho 313: /* restore to old bucket pulled memory block for reallocation */
314: TAILQ_INSERT_HEAD(&mp->pool_active[oidx], m, alloc_node);
315: /* statistics */
316: mp->pool_calls.alloc++;
317: mp->pool_bytes.alloc += osize;
1.1.2.7 misho 318:
1.1.2.8 misho 319: mpool_unlock(mp);
320: return NULL;
1.1.2.9 ! misho 321: } else
! 322: m->alloc_mem = (u_int*) p;
1.1.2.8 misho 323: }
324: /* quota */
325: mp->pool_quota.curr += (newsize - osize);
1.1.2.7 misho 326:
327: m->alloc_mem[0] = newsize / sizeof(u_int);
328: m->alloc_mem[1] = MEM_MAGIC_START;
329: m->alloc_mem[2 + newsize / sizeof(u_int)] = MEM_MAGIC_STOP;
1.1.2.8 misho 330:
331: if (oidx != idx) {
332: TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node);
333: /* statistics */
334: mp->pool_calls.alloc++;
335: }
1.1.2.7 misho 336: mp->pool_bytes.alloc += newsize;
337:
338: if (memname)
339: strlcpy(m->alloc_name, memname, sizeof m->alloc_name);
340:
341: mpool_unlock(mp);
342: return mem_data(m, void*);
343: }
344:
345: /*
1.1.2.3 misho 346: * mpool_purge() - Purge memory block cache and release resources
347: *
348: * @mp = Memory pool
349: * @atmost = Free at most in buckets
350: * return: -1 error or 0 ok
351: */
352: int
1.1.2.4 misho 353: mpool_purge(mpool_t * __restrict mp, u_int atmost)
1.1.2.3 misho 354: {
1.1.2.4 misho 355: register int i, cx;
1.1.2.3 misho 356: struct tagAlloc *m, *tmp;
357:
358: if (!mp) {
359: sess_SetErr(EINVAL, "Pool not specified");
360: return -1;
361: }
362:
363: mpool_lock(mp);
364:
1.1.2.4 misho 365: for (i = cx = 0; i < MEM_BUCKETS; cx = 0, i++) {
1.1.2.3 misho 366: TAILQ_FOREACH_SAFE(m, &mp->pool_inactive[i], alloc_node, tmp) {
1.1.2.4 misho 367: /* barrier for purge */
368: if (cx < atmost) {
369: cx++;
1.1.2.3 misho 370: continue;
1.1.2.4 misho 371: }
1.1.2.3 misho 372:
373: TAILQ_REMOVE(&mp->pool_inactive[i], m, alloc_node);
374: /* statistics */
375: mp->pool_calls.cache--;
376: mp->pool_bytes.cache -= mem_size(m);
377:
378: mp->pool_calls.free++;
379: mp->pool_bytes.free += mem_size(m);
380: /* quota */
381: mp->pool_quota.curr -= mem_size(m);
382:
383: if (m->alloc_mem)
384: free(m->alloc_mem);
385: free(m);
386: }
387: }
388:
389: mpool_unlock(mp);
390: return 0;
391: }
392:
393: /*
1.1.2.1 misho 394: * mpool_free() Free allocated memory with mpool_alloc()
395: *
396: * @mp = Memory pool
397: * @data = Allocated memory data
398: * @purge = if !=0 force release memory block
399: * return: <0 error or 0 ok released memory block
400: */
401: int
402: mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge)
403: {
404: int idx;
1.1.2.7 misho 405: struct tagAlloc *m, *tmp;
1.1.2.1 misho 406:
407: if (!mp) {
408: sess_SetErr(EINVAL, "Pool not specified");
409: return -1;
410: }
411: /* check address range & sentinel */
412: if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
413: sess_SetErr(EFAULT, "Corrupted memory address");
414: return -2;
415: } else
1.1.2.5 misho 416: idx = BucketIndex(((u_int*)data)[-2] * sizeof(u_int));
1.1.2.1 misho 417:
418: mpool_lock(mp);
1.1.2.7 misho 419: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
1.1.2.1 misho 420: if (mem_data(m, void*) == data) {
421: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
1.1.2.3 misho 422: /* statistics */
423: mp->pool_calls.alloc--;
424: mp->pool_bytes.alloc -= mem_size(m);
1.1.2.1 misho 425:
426: if (!purge) {
427: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
428: /* statistics */
429: mp->pool_calls.cache++;
430: mp->pool_bytes.cache += mem_size(m);
431: } else {
432: /* statistics */
433: mp->pool_calls.free++;
434: mp->pool_bytes.free += mem_size(m);
1.1.2.3 misho 435: /* quota */
436: mp->pool_quota.curr -= mem_size(m);
437:
438: if (m->alloc_mem)
439: free(m->alloc_mem);
440: free(m);
1.1.2.1 misho 441: }
442: break;
443: }
444: mpool_unlock(mp);
445:
446: return 0;
447: }
448:
449: /*
450: * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
451: *
452: * @mp = Memory pool
453: * @size = Allocated memory data size
454: * @memname = Memory name
455: * @purge = if !=0 force release memory block
456: * return: <0 error or 0 ok released memory block
457: */
458: int
459: mpool_free2(mpool_t * __restrict mp, u_int size, const char *memname, int purge)
460: {
461: int idx;
1.1.2.7 misho 462: struct tagAlloc *m, *tmp;
1.1.2.1 misho 463:
464: if (!mp || !memname) {
465: sess_SetErr(EINVAL, "Pool or memory name is not specified");
466: return -1;
467: } else
468: idx = BucketIndex(size);
469:
470: mpool_lock(mp);
1.1.2.7 misho 471: TAILQ_FOREACH_SAFE(m, &mp->pool_active[idx], alloc_node, tmp)
1.1.2.1 misho 472: if (!strcmp(m->alloc_name, memname)) {
473: TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node);
1.1.2.3 misho 474: /* statistics */
475: mp->pool_calls.alloc--;
476: mp->pool_bytes.alloc -= mem_size(m);
1.1.2.1 misho 477:
478: if (!purge) {
479: TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node);
480: /* statistics */
481: mp->pool_calls.cache++;
482: mp->pool_bytes.cache += mem_size(m);
483: } else {
484: /* statistics */
485: mp->pool_calls.free++;
486: mp->pool_bytes.free += mem_size(m);
1.1.2.3 misho 487: /* quota */
488: mp->pool_quota.curr -= mem_size(m);
489:
490: if (m->alloc_mem)
491: free(m->alloc_mem);
492: free(m);
1.1.2.1 misho 493: }
494: break;
495: }
496: mpool_unlock(mp);
497:
498: return 0;
499: }
500:
501: /*
502: * mpool_getmembynam() Find allocated memory block by size and memory name
503: *
504: * @mp = Memory pool
505: * @size = Memory size
506: * @memname = Memory name
507: * return: NULL error or not found and !=NULL allocated memory
508: */
509: inline struct tagAlloc *
510: mpool_getmembynam(mpool_t * __restrict mp, u_int size, const char *memname)
511: {
512: int idx;
513: struct tagAlloc *m = NULL;
514:
515: if (!mp || !memname)
516: return NULL;
517:
518: idx = BucketIndex(size);
519: TAILQ_FOREACH(m, &mp->pool_active[idx], alloc_node)
520: if (!strcmp(m->alloc_name, memname))
521: break;
522:
523: return mem_data(m, void*);
524: }
1.1.2.3 misho 525:
526: /*
527: * mpool_getsizebyaddr() - Get size of allocated memory block by address
528: *
529: * @addr = allocated memory from mpool_malloc()
530: * return: usable size of allocated memory block
531: */
532: inline u_int
533: mpool_getsizebyaddr(void * __restrict data)
534: {
535: if (mpool_chkaddr(data))
536: return 0;
537:
538: return (((u_int*) data)[-2] * sizeof(u_int));
539: }
540:
541: /*
542: * mpool_chkaddr() - Check validity of given address
543: *
544: * @data = allocated memory from mpool_malloc()
545: * return: -1 bad address, 1 corrupted address or 0 ok
546: */
547: inline int
548: mpool_chkaddr(void * __restrict data)
549: {
550: /* check address range */
551: if (MEM_BADADDR(data))
552: return -1;
553: /* check sentinel */
554: if (MEM_CORRUPT(data))
555: return 1;
556: /* data address is ok! */
557: return 0;
558: }
1.1.2.6 misho 559:
560: /*
561: * mpool_setquota() - Change maximum memory quota
562: *
563: * @mp = Memory pool
564: * @maxmem = New max quota size
565: * return: old maximum memory quota size
566: */
567: inline u_long
568: mpool_setquota(mpool_t * __restrict mp, u_long maxmem)
569: {
570: u_long ret;
571:
572: if (!mp)
573: return 0;
574:
575: ret = mp->pool_quota.max;
576: mp->pool_quota.max = maxmem;
577:
578: /* if new max quota is less then current allocated memory,
579: * try to purge memory cache blocks
580: */
581: if (mp->pool_quota.max < mp->pool_quota.curr)
582: mpool_purge(mp, 0);
583:
584: return ret;
585: }
586:
1.1.2.7 misho 587: /*
588: * mpool_getquota() - Get memory quota
589: *
590: * @mp = Memory pool
591: * @currmem = Return current memory
592: * @maxmem = Return max quota size
593: * return: none
594: */
595: inline void
596: mpool_getquota(mpool_t * __restrict mp, u_long *currmem, u_long *maxmem)
597: {
598: if (!mp)
599: return;
600:
601: if (maxmem)
602: *maxmem = mp->pool_quota.max;
603: if (currmem)
604: *currmem = mp->pool_quota.curr;
605: }
606:
1.1.2.6 misho 607: /* ----------------------------------------------------------- */
608:
609: /*
610: * mpool_statistics() - Dump statistics from memory pool buckets
611: *
612: * @mp = Memory pool
613: * @cb = Export statistics to callback
614: * return: none
615: */
616: void
617: mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb)
618: {
619: struct tagAlloc *m;
620: register int i, act, inact;
621:
622: if (!mp || !cb)
623: return;
624:
625: for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) {
626: TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node)
627: act++;
628: TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node)
629: inact++;
630:
631: cb(1 << (i + MEM_MIN_BUCKET), act, inact);
632: }
633: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>