2 * Bacula memory pool routines.
4 * The idea behind these routines is that there will be
5 * pools of memory that are pre-allocated for quick
6 * access. The pools will have a fixed memory size on allocation
7 * but if need be, the size can be increased. This is
8 * particularly useful for filename
9 * buffers where 256 bytes should be sufficient in 99.99%
10 * of the cases, but when it isn't we want to be able to
13 * A major advantage of the pool memory aside from the speed
14 * is that the buffer carrys around its size, so to ensure that
15 * there is enough memory, simply call the check_pool_memory_size()
16 * with the desired size and it will adjust only if necessary.
24 Copyright (C) 2000-2004 Kern Sibbald and John Walker
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation; either version 2 of
29 the License, or (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 General Public License for more details.
36 You should have received a copy of the GNU General Public
37 License along with this program; if not, write to the Free
38 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
46 int32_t size; /* default size */
47 int32_t max_size; /* max allocated */
48 int32_t max_used; /* max buffers used */
49 int32_t in_use; /* number in use */
50 struct abufhead *free_buf; /* pointer to free buffers */
53 /* Bacula Name length plus extra */
54 #define NLEN (MAX_NAME_LENGTH+2)
56 /* #define STRESS_TEST_POOL */
57 #ifndef STRESS_TEST_POOL
59 * Define default Pool buffer sizes
61 static struct s_pool_ctl pool_ctl[] = {
62 { 256, 256, 0, 0, NULL }, /* PM_NOPOOL no pooling */
63 { NLEN, NLEN,0, 0, NULL }, /* PM_NAME Bacula name */
64 { 256, 256, 0, 0, NULL }, /* PM_FNAME filename buffers */
65 { 512, 512, 0, 0, NULL }, /* PM_MESSAGE message buffer */
66 { 1024, 1024, 0, 0, NULL } /* PM_EMSG error message buffer */
70 /* This is used ONLY when stress testing the code */
71 static struct s_pool_ctl pool_ctl[] = {
72 { 20, 20, 0, 0, NULL }, /* PM_NOPOOL no pooling */
73 { NLEN, NLEN,0, 0, NULL }, /* PM_NAME Bacula name */
74 { 20, 20, 0, 0, NULL }, /* PM_FNAME filename buffers */
75 { 20, 20, 0, 0, NULL }, /* PM_MESSAGE message buffer */
76 { 20, 20, 0, 0, NULL } /* PM_EMSG error message buffer */
81 /* Memory allocation control structures and storage. */
83 int32_t ablen; /* Buffer length in bytes */
84 int32_t pool; /* pool */
85 struct abufhead *next; /* pointer to next free buffer */
88 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
93 #define HEAD_SIZE BALIGN(sizeof(struct abufhead))
95 POOLMEM *sm_get_pool_memory(const char *fname, int lineno, int pool)
100 Emsg2(M_ABORT, 0, "MemPool index %d larger than max %d\n", pool, PM_MAX);
103 if (pool_ctl[pool].free_buf) {
104 buf = pool_ctl[pool].free_buf;
105 pool_ctl[pool].free_buf = buf->next;
106 pool_ctl[pool].in_use++;
107 if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
108 pool_ctl[pool].max_used = pool_ctl[pool].in_use;
111 Dmsg3(300, "sm_get_pool_memory reuse %x to %s:%d\n", buf, fname, lineno);
112 sm_new_owner(fname, lineno, (char *)buf);
113 return (POOLMEM *)((char *)buf+HEAD_SIZE);
116 if ((buf = (struct abufhead *)sm_malloc(fname, lineno, pool_ctl[pool].size+HEAD_SIZE)) == NULL) {
118 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", pool_ctl[pool].size);
120 buf->ablen = pool_ctl[pool].size;
122 pool_ctl[pool].in_use++;
123 if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
124 pool_ctl[pool].max_used = pool_ctl[pool].in_use;
127 Dmsg3(300, "sm_get_pool_memory give %x to %s:%d\n", buf, fname, lineno);
128 return (POOLMEM *)((char *)buf+HEAD_SIZE);
131 /* Get nonpool memory of size requested */
132 POOLMEM *sm_get_memory(const char *fname, int lineno, int32_t size)
134 struct abufhead *buf;
137 if ((buf = (struct abufhead *)sm_malloc(fname, lineno, size+HEAD_SIZE)) == NULL) {
138 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
143 pool_ctl[pool].in_use++;
144 if (pool_ctl[pool].in_use > pool_ctl[pool].max_used)
145 pool_ctl[pool].max_used = pool_ctl[pool].in_use;
146 return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
150 /* Return the size of a memory buffer */
151 int32_t sm_sizeof_pool_memory(const char *fname, int lineno, POOLMEM *obuf)
153 char *cp = (char *)obuf;
157 return ((struct abufhead *)cp)->ablen;
160 /* Realloc pool memory buffer */
161 POOLMEM *sm_realloc_pool_memory(const char *fname, int lineno, POOLMEM *obuf, int32_t size)
163 char *cp = (char *)obuf;
170 buf = sm_realloc(fname, lineno, cp, size+HEAD_SIZE);
173 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
175 ((struct abufhead *)buf)->ablen = size;
176 pool = ((struct abufhead *)buf)->pool;
177 if (size > pool_ctl[pool].max_size) {
178 pool_ctl[pool].max_size = size;
181 return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
184 POOLMEM *sm_check_pool_memory_size(const char *fname, int lineno, POOLMEM *obuf, int32_t size)
187 if (size <= sizeof_pool_memory(obuf)) {
190 return realloc_pool_memory(obuf, size);
193 /* Free a memory buffer */
194 void sm_free_pool_memory(const char *fname, int lineno, POOLMEM *obuf)
196 struct abufhead *buf;
201 buf = (struct abufhead *)((char *)obuf - HEAD_SIZE);
203 pool_ctl[pool].in_use--;
205 free((char *)buf); /* free nonpooled memory */
206 } else { /* otherwise link it to the free pool chain */
208 struct abufhead *next;
209 /* Don't let him free the same buffer twice */
210 for (next=pool_ctl[pool].free_buf; next; next=next->next) {
212 Dmsg4(300, "bad free_pool_memory %x pool=%d from %s:%d\n", buf, pool, fname, lineno);
213 V(mutex); /* unblock the pool */
214 ASSERT(next != buf); /* attempt to free twice */
218 buf->next = pool_ctl[pool].free_buf;
219 pool_ctl[pool].free_buf = buf;
221 Dmsg4(300, "free_pool_memory %x pool=%d from %s:%d\n", buf, pool, fname, lineno);
228 /* =================================================================== */
230 POOLMEM *get_pool_memory(int pool)
232 struct abufhead *buf;
235 if (pool_ctl[pool].free_buf) {
236 buf = pool_ctl[pool].free_buf;
237 pool_ctl[pool].free_buf = buf->next;
239 return (POOLMEM *)((char *)buf+HEAD_SIZE);
242 if ((buf=malloc(pool_ctl[pool].size+HEAD_SIZE)) == NULL) {
244 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", pool_ctl[pool].size);
246 buf->ablen = pool_ctl[pool].size;
249 pool_ctl[pool].in_use++;
250 if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
251 pool_ctl[pool].max_used = pool_ctl[pool].in_use;
254 return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
257 /* Get nonpool memory of size requested */
258 POOLMEM *get_memory(int32_t size)
260 struct abufhead *buf;
263 if ((buf=malloc(size+HEAD_SIZE)) == NULL) {
264 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
269 pool_ctl[pool].in_use++;
270 if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
271 pool_ctl[pool].max_used = pool_ctl[pool].in_use;
273 return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
277 /* Return the size of a memory buffer */
278 int32_t sizeof_pool_memory(POOLMEM *obuf)
280 char *cp = (char *)obuf;
284 return ((struct abufhead *)cp)->ablen;
287 /* Realloc pool memory buffer */
288 POOLMEM *realloc_pool_memory(POOLMEM *obuf, int32_t size)
290 char *cp = (char *)obuf;
297 buf = realloc(cp, size+HEAD_SIZE);
300 Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
302 ((struct abufhead *)buf)->ablen = size;
303 pool = ((struct abufhead *)buf)->pool;
304 if (size > pool_ctl[pool].max_size) {
305 pool_ctl[pool].max_size = size;
308 return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
311 POOLMEM *check_pool_memory_size(POOLMEM *obuf, int32_t size)
314 if (size <= sizeof_pool_memory(obuf)) {
317 return realloc_pool_memory(obuf, size);
320 /* Free a memory buffer */
321 void free_pool_memory(POOLMEM *obuf)
323 struct abufhead *buf;
328 buf = (struct abufhead *)((char *)obuf - HEAD_SIZE);
330 pool_ctl[pool].in_use--;
332 free((char *)buf); /* free nonpooled memory */
333 } else { /* otherwise link it to the free pool chain */
335 struct abufhead *next;
336 /* Don't let him free the same buffer twice */
337 for (next=pool_ctl[pool].free_buf; next; next=next->next) {
340 ASSERT(next != buf); /* attempt to free twice */
344 buf->next = pool_ctl[pool].free_buf;
345 pool_ctl[pool].free_buf = buf;
347 Dmsg2(300, "free_pool_memory %x pool=%d\n", buf, pool);
351 #endif /* SMARTALLOC */
358 /* Release all pooled memory */
359 void close_memory_pool()
361 struct abufhead *buf, *next;
363 sm_check(__FILE__, __LINE__, false);
365 for (int i=1; i<=PM_MAX; i++) {
366 buf = pool_ctl[i].free_buf;
372 pool_ctl[i].free_buf = NULL;
379 static const char *pool_name(int pool)
381 static const char *name[] = {"NoPool", "NAME ", "FNAME ", "MSG ", "EMSG "};
384 if (pool >= 0 && pool <= PM_MAX) {
387 sprintf(buf, "%-6d", pool);
391 /* Print staticstics on memory pool usage
393 void print_memory_pool_stats()
395 Dmsg0(-1, "Pool Maxsize Maxused Inuse\n");
396 for (int i=0; i<=PM_MAX; i++)
397 Dmsg4(-1, "%5s %7d %7d %5d\n", pool_name(i), pool_ctl[i].max_size,
398 pool_ctl[i].max_used, pool_ctl[i].in_use);
404 void print_memory_pool_stats() {}
408 * Concatenate a string (str) onto a pool memory buffer pm
409 * Returns: length of concatenated string
411 int pm_strcat(POOLMEM **pm, const char *str)
413 int pmlen = strlen(*pm);
414 int len = strlen(str) + 1;
416 *pm = check_pool_memory_size(*pm, pmlen + len);
417 memcpy(*pm+pmlen, str, len);
418 return pmlen + len - 1;
421 int pm_strcat(POOLMEM *&pm, const char *str)
423 int pmlen = strlen(pm);
424 int len = strlen(str) + 1;
426 pm = check_pool_memory_size(pm, pmlen + len);
427 memcpy(pm+pmlen, str, len);
428 return pmlen + len - 1;
432 int pm_strcat(POOLMEM *&pm, POOL_MEM &str)
434 int pmlen = strlen(pm);
435 int len = strlen(str.c_str()) + 1;
437 pm = check_pool_memory_size(pm, pmlen + len);
438 memcpy(pm+pmlen, str.c_str(), len);
439 return pmlen + len - 1;
442 int pm_strcat(POOL_MEM &pm, const char *str)
444 int pmlen = strlen(pm.c_str());
445 int len = strlen(str) + 1;
447 pm.check_size(pmlen + len);
448 memcpy(pm.c_str()+pmlen, str, len);
449 return pmlen + len - 1;
454 * Copy a string (str) into a pool memory buffer pm
455 * Returns: length of string copied
457 int pm_strcpy(POOLMEM **pm, const char *str)
459 int len = strlen(str) + 1;
461 *pm = check_pool_memory_size(*pm, len);
462 memcpy(*pm, str, len);
466 int pm_strcpy(POOLMEM *&pm, const char *str)
468 int len = strlen(str) + 1;
470 pm = check_pool_memory_size(pm, len);
471 memcpy(pm, str, len);
475 int pm_strcpy(POOLMEM *&pm, POOL_MEM &str)
477 int len = strlen(str.c_str()) + 1;
479 pm = check_pool_memory_size(pm, len);
480 memcpy(pm, str.c_str(), len);
485 int pm_strcpy(POOL_MEM &pm, const char *str)
487 int len = strlen(str) + 1;
489 memcpy(pm.c_str(), str, len);
493 int POOL_MEM::strcat(const char *str)
495 int pmlen = strlen(mem);
496 int len = strlen(str) + 1;
498 check_size(pmlen + len);
499 memcpy(mem+pmlen, str, len);
500 return pmlen + len - 1;
504 int POOL_MEM::strcpy(const char *str)
506 int len = strlen(str) + 1;
508 memcpy(mem, str, len);