Annotation of libaitio/inc/ampool.h, revision 1.1.2.3
1.1.2.1 misho 1: #ifndef __AMPOOL_H
2: #define __AMPOOL_H
3:
4:
5: /* Memory pool */
6:
7: #define MEM_BUCKETS 28 /* 32 bits - 4 bits = 28 items in bucket array */
8:
9: struct tagAlloc {
1.1.2.2 misho 10: char alloc_name[64];
1.1.2.1 misho 11:
12: unsigned int *alloc_mem;
13:
14: TAILQ_ENTRY(tagAlloc) alloc_node;
15: };
16: typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t;
17:
18: typedef struct _tagMPool {
19: pthread_mutex_t pool_mtx;
20:
21: struct {
22: unsigned long alloc;
23: unsigned long free;
24: unsigned long cache;
25: } pool_calls;
26: struct {
27: unsigned long alloc;
28: unsigned long free;
29: unsigned long cache;
30: } pool_bytes;
31: struct {
32: unsigned long max;
33: unsigned long curr;
34: } pool_quota;
35:
36: /* pool buckets */
37: mpool_bucket_t pool_active[MEM_BUCKETS];
38: mpool_bucket_t pool_inactive[MEM_BUCKETS];
39: } mpool_t;
40: #define mpool_lock(x) (assert((x)), pthread_mutex_lock(&(x)->pool_mtx))
41: #define mpool_unlock(x) (assert((x)), pthread_mutex_unlock(&(x)->pool_mtx))
42:
43: typedef void (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int);
44:
45: /* mpool functions */
46:
47: /*
48: * mpool_init() - Init memory pool
49: *
50: * @maxmem = If !=0 set maximum memory quota
51: * return: =NULL error or !=NULL new allocated pool
52: */
53: mpool_t *mpool_init(unsigned long maxmem);
54: /*
55: * mpool_destroy() - Destroy memory pool
56: *
57: * @mp = Memory pool
58: * return: none
59: */
60: void mpool_destroy(mpool_t ** __restrict mp);
61: /*
62: * mpool_purge() - Purge memory block cache and release resources
63: *
64: * @mp = Memory pool
65: * @atmost = Free at most in buckets
66: * return: -1 error or 0 ok
67: */
68: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
69: /*
70: * mpool_malloc() - Memory allocation
71: *
72: * @mp = Memory pool
73: * @size = Size
74: * @memname = Optional memory block name
75: * return: NULL error or !=NULL ok allocated memory
76: */
77: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
78: /*
79: * mpool_free() Free allocated memory with mpool_alloc()
80: *
81: * @mp = Memory pool
82: * @data = Allocated memory data
83: * @purge = if !=0 force release memory block
84: * return: <0 error or 0 ok released memory block
85: */
86: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
87: /*
88: * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
89: *
90: * @mp = Memory pool
91: * @size = Allocated memory data size
92: * @memname = Memory name
93: * @purge = if !=0 force release memory block
94: * return: <0 error or 0 ok released memory block
95: */
96: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
97: /*
98: * mpool_realloc() Reallocate memory block with new size
99: *
100: * @mp = Memory pool
101: * @data = Allocated memory data
102: * @newsize = New size of memory block
103: * @memname = Optional new memory block name
104: * return: NULL error or !=NULL new reallocated memory block
105: */
106: void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data,
107: unsigned int newsize, const char *memname);
108: /*
1.1.2.3 ! misho 109: * mpool_strdup() - String duplicate
! 110: *
! 111: * @mp = Memory pool
! 112: * @str = String
! 113: * @memname = Memory name
! 114: * return: NULL error or !=NULL new string
! 115: */
! 116: char *mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname);
! 117: /*
1.1.2.1 misho 118: * mpool_getmembynam() Find allocated memory block by size and memory name
119: *
120: * @mp = Memory pool
121: * @size = Memory size
122: * @memname = Memory name
123: * return: NULL error or not found and !=NULL allocated memory
124: */
125: inline struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
126: /*
127: * mpool_getsizebyaddr() - Get size of allocated memory block by address
128: *
129: * @data = allocated memory from mpool_malloc()
130: * return: usable size of allocated memory block
131: */
132: inline unsigned int mpool_getsizebyaddr(void * __restrict data);
133: /*
134: * mpool_chkaddr() - Check validity of given address
135: *
136: * @data = allocated memory from mpool_malloc()
137: * return: -1 bad address, 1 corrupted address or 0 ok
138: */
139: inline int mpool_chkaddr(void * __restrict data);
140: /*
141: * mpool_setquota() - Change maximum memory quota
142: *
143: * @mp = Memory pool
144: * @maxmem = New max quota size
145: * return: old maximum memory quota size
146: */
147: inline unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem);
148: /*
149: * mpool_getquota() - Get memory quota
150: *
151: * @mp = Memory pool
152: * @currmem = Return current memory
153: * @maxmem = Return max quota size
154: * return: none
155: */
156: inline void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem,
157: unsigned long *maxmem);
158: /*
159: * mpool_statistics() - Dump statistics from memory pool buckets
160: *
161: * @mp = Memory pool
162: * @cb = Export statistics to callback
163: * return: none
164: */
165: void mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb);
166:
167:
1.1.2.3 ! misho 168: /* Wrappers */
! 169:
! 170: /*
! 171: * mpool_xmalloc() - malloc wrapper
! 172: *
! 173: * @size = Size
! 174: * return: NULL error or !=NULL ok allocated memory
! 175: */
! 176: void *mpool_xmalloc(size_t size);
! 177: /*
! 178: * mpool_xcalloc() - calloc wrapper
! 179: *
! 180: * @num = number of elements
! 181: * @size = Size of element
! 182: * return: NULL error or !=NULL ok allocated memory
! 183: */
! 184: void *mpool_xcalloc(size_t num, size_t size);
! 185: /*
! 186: * mpool_xrealloc() - realloc wrapper
! 187: *
! 188: * @data = Allocated memory data
! 189: * @newsize = New size of memory block
! 190: * return: NULL error or !=NULL new reallocated memory block
! 191: */
! 192: void *mpool_xrealloc(void * __restrict data, size_t newsize);
! 193: /*
! 194: * mpool_xfree() - free wrapper
! 195: *
! 196: * @data = Allocated memory data
! 197: * return: none
! 198: */
! 199: void mpool_xfree(void * __restrict data);
! 200: /*
! 201: * mpool_xstrdup() - strdup wrapper
! 202: *
! 203: * @str = string
! 204: * return: =NULL error or !=NULL new allocated string
! 205: */
! 206: char *mpool_xstrdup(const char *str);
! 207:
! 208:
1.1.2.1 misho 209: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>