Annotation of libelwix/inc/elwix/ampool.h, revision 1.5.68.1
1.1 misho 1: /*************************************************************************
2: * (C) 2012 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.5.68.1! misho 6: * $Id: ampool.h,v 1.5 2015/07/01 21:48:39 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.68.1! misho 15: Copyright 2004 - 2022
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: #ifndef __AMPOOL_H
47: #define __AMPOOL_H
48:
49:
50: #include <pthread.h>
51: #include <sys/queue.h>
52:
53:
54: /* Memory pool */
55:
56: #define MEM_BUCKETS 28 /* 32 bits - 4 bits = 28 items in bucket array */
57:
58: struct tagAlloc {
59: char alloc_name[64];
60:
61: unsigned int *alloc_mem;
62:
63: TAILQ_ENTRY(tagAlloc) alloc_node;
64: };
65: typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t;
66:
67: typedef struct _tagMPool {
68: pthread_mutex_t pool_mtx;
69:
70: struct {
71: unsigned long alloc;
72: unsigned long free;
73: unsigned long cache;
74: } pool_calls;
75: struct {
76: unsigned long alloc;
77: unsigned long free;
78: unsigned long cache;
79: } pool_bytes;
80: struct {
81: unsigned long max;
1.4 misho 82: unsigned long real;
1.1 misho 83: unsigned long curr;
84: } pool_quota;
85:
86: /* pool buckets */
87: mpool_bucket_t pool_active[MEM_BUCKETS];
88: mpool_bucket_t pool_inactive[MEM_BUCKETS];
89: } mpool_t;
90: #define mpool_lock(x) (assert((x)), pthread_mutex_lock(&(x)->pool_mtx))
91: #define mpool_unlock(x) (assert((x)), pthread_mutex_unlock(&(x)->pool_mtx))
92:
1.5.68.1! misho 93: typedef int (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int,
! 94: void *, unsigned int);
1.1 misho 95:
96: /* mpool functions */
97:
98: /*
99: * mpool_init() - Init memory pool
100: *
101: * @maxmem = If !=0 set maximum memory quota
102: * return: =NULL error or !=NULL new allocated pool
103: */
104: mpool_t *mpool_init(unsigned long maxmem);
105: /*
106: * mpool_destroy() - Destroy memory pool
107: *
108: * @mp = Memory pool
109: * return: none
110: */
111: void mpool_destroy(mpool_t ** __restrict mp);
112: /*
113: * mpool_purge() - Purge memory block cache and release resources
114: *
115: * @mp = Memory pool
116: * @atmost = Free at most in buckets
117: * return: -1 error or 0 ok
118: */
119: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
120: /*
121: * mpool_malloc() - Memory allocation
122: *
123: * @mp = Memory pool
124: * @size = Size
125: * @memname = Optional memory block name
126: * return: NULL error or !=NULL ok allocated memory
127: */
128: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
129: /*
130: * mpool_free() Free allocated memory with mpool_alloc()
131: *
132: * @mp = Memory pool
133: * @data = Allocated memory data
134: * @purge = if !=0 force release memory block
135: * return: <0 error or 0 ok released memory block
136: */
137: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
138: /*
139: * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
140: *
141: * @mp = Memory pool
142: * @size = Allocated memory data size
143: * @memname = Memory name
144: * @purge = if !=0 force release memory block
145: * return: <0 error or 0 ok released memory block
146: */
147: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
148: /*
149: * mpool_realloc() Reallocate memory block with new size
150: *
151: * @mp = Memory pool
152: * @data = Allocated memory data
153: * @newsize = New size of memory block
154: * @memname = Optional new memory block name
155: * return: NULL error or !=NULL new reallocated memory block
156: */
157: void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data,
158: unsigned int newsize, const char *memname);
159: /*
160: * mpool_strdup() - String duplicate
161: *
162: * @mp = Memory pool
163: * @str = String
164: * @memname = Memory name
165: * return: NULL error or !=NULL new string
166: */
167: char *mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname);
168: /*
169: * mpool_getmembynam() Find allocated memory block by size and memory name
170: *
171: * @mp = Memory pool
172: * @size = Memory size
173: * @memname = Memory name
174: * return: NULL error or not found and !=NULL allocated memory
175: */
1.2 misho 176: struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
1.1 misho 177: /*
178: * mpool_getsizebyaddr() - Get size of allocated memory block by address
179: *
180: * @data = allocated memory from mpool_malloc()
181: * return: usable size of allocated memory block
182: */
1.2 misho 183: unsigned int mpool_getsizebyaddr(void * __restrict data);
1.1 misho 184: /*
185: * mpool_chkaddr() - Check validity of given address
186: *
187: * @data = allocated memory from mpool_malloc()
188: * return: -1 bad address, 1 corrupted address or 0 ok
189: */
1.2 misho 190: int mpool_chkaddr(void * __restrict data);
1.1 misho 191: /*
192: * mpool_setquota() - Change maximum memory quota
193: *
194: * @mp = Memory pool
195: * @maxmem = New max quota size
196: * return: old maximum memory quota size
197: */
1.2 misho 198: unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem);
1.1 misho 199: /*
200: * mpool_getquota() - Get memory quota
201: *
202: * @mp = Memory pool
1.4 misho 203: * @currmem = Return current memory usage
204: * @realmem = Return current real memory usage
1.1 misho 205: * @maxmem = Return max quota size
206: * return: none
207: */
1.2 misho 208: void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem,
1.4 misho 209: unsigned long *realmem, unsigned long *maxmem);
1.1 misho 210: /*
211: * mpool_statistics() - Dump statistics from memory pool buckets
212: *
213: * @mp = Memory pool
214: * @cb = Export statistics to callback
1.5.68.1! misho 215: * return: -1 error or >0 bytes in data buffer
1.1 misho 216: */
1.5.68.1! misho 217: int mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb,
! 218: void *data, unsigned int datlen);
1.1 misho 219:
220:
221: /* Wrappers */
222:
223: /*
224: * mpool_xmalloc() - malloc wrapper
225: *
226: * @size = Size
227: * return: NULL error or !=NULL ok allocated memory
228: */
229: void *mpool_xmalloc(size_t size);
230: /*
231: * mpool_xcalloc() - calloc wrapper
232: *
233: * @num = number of elements
234: * @size = Size of element
235: * return: NULL error or !=NULL ok allocated memory
236: */
237: void *mpool_xcalloc(size_t num, size_t size);
238: /*
239: * mpool_xrealloc() - realloc wrapper
240: *
241: * @data = Allocated memory data
242: * @newsize = New size of memory block
243: * return: NULL error or !=NULL new reallocated memory block
244: */
245: void *mpool_xrealloc(void * __restrict data, size_t newsize);
246: /*
247: * mpool_xfree() - free wrapper
248: *
249: * @data = Allocated memory data
250: * return: none
251: */
252: void mpool_xfree(void * __restrict data);
253: /*
254: * mpool_xstrdup() - strdup wrapper
255: *
256: * @str = string
257: * return: =NULL error or !=NULL new allocated string
258: */
259: char *mpool_xstrdup(const char *str);
1.4 misho 260: /*
1.5.68.1! misho 261: * mpool_xstatistics() - elwix default memory pool statistics wrapper
1.4 misho 262: *
263: * @cb = Export statistics to callback
1.5.68.1! misho 264: * @data = data buffer
! 265: * @datlen = data buffer length
! 266: * return: -1 error or >0 bytes in data buffer
1.4 misho 267: */
1.5.68.1! misho 268: int mpool_xstatistics(mpool_stat_cb cb, void *data, unsigned int datlen);
1.4 misho 269: /*
1.5 misho 270: * mpool_dump() - Dump elwix memory pool statistics
1.4 misho 271: *
1.5 misho 272: * @mp = memory pool, if =NULL dump elwix default memory pool
273: * @fmt = prefix info format string
1.4 misho 274: * @... = argument(s)
275: * return: none
276: */
1.5 misho 277: void mpool_dump(mpool_t * __restrict mp, const char *fmt, ...);
1.5.68.1! misho 278: /*
! 279: * mpool_dump2() - Dump elwix memory pool statistics to string
! 280: *
! 281: * @mp = memory pool, if =NULL dump elwix default memory pool
! 282: * @str = string buffer
! 283: * @strlen = string buffer length
! 284: * return: >0 data in string buffer
! 285: */
! 286: int mpool_dump2(mpool_t * __restrict mp, char *str, int strlen);
1.1 misho 287:
288:
289: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>