]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/mem_pool.c
2af3a7b8b678f26da3bb270c4587ca67290cc9ea
[bacula/bacula] / bacula / src / lib / mem_pool.c
1 /*
2  *  Bacula memory pool routines. 
3  *
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
11  *  increase the size.
12  *
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.
17  *
18  *           Kern E. Sibbald
19  *
20  *   Version $Id$
21  */
22
23 /*
24    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25
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.
30
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.
35
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,
39    MA 02111-1307, USA.
40
41  */
42
43 #include "bacula.h"
44
45 struct s_pool_ctl {
46    size_t size;                       /* default size */
47    size_t max_size;                   /* max allocated */
48    size_t max_used;                   /* max buffers used */
49    size_t in_use;                     /* number in use */
50    struct abufhead *free_buf;         /* pointer to free buffers */
51 };
52
53 static struct s_pool_ctl pool_ctl[] = {
54    {  256,  256, 0, 0, NULL },        /* PM_NOPOOL no pooling */
55    {  256,  256, 0, 0, NULL },        /* PM_FNAME filename buffers */
56    {  512,  512, 0, 0, NULL },        /* PM_MESSAGE message buffer */
57    { 1024, 1024, 0, 0, NULL }         /* PM_EMSG error message buffer */
58 };
59
60 /*  Memory allocation control structures and storage.  */
61 struct abufhead {
62    size_t ablen;                      /* Buffer length in bytes */
63    int32_t pool;                      /* pool */
64    struct abufhead *next;             /* pointer to next free buffer */
65 };
66
67 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
68
69
70 #ifdef SMARTALLOC
71
72 #define HEAD_SIZE BALIGN(sizeof(struct abufhead))
73
74 extern POOLMEM *sm_malloc(char *fname, int lineno, int nbytes);
75
76 POOLMEM *sm_get_pool_memory(char *fname, int lineno, int pool)
77 {
78    struct abufhead *buf;
79
80    sm_check(fname, lineno, True);
81    if (pool > PM_MAX) {
82       Emsg2(M_ABORT, 0, "MemPool index %d larger than max %d\n", pool, PM_MAX);
83    }
84    P(mutex);
85    if (pool_ctl[pool].free_buf) {
86       buf = pool_ctl[pool].free_buf;
87       pool_ctl[pool].free_buf = buf->next;
88       pool_ctl[pool].in_use++;
89       if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
90          pool_ctl[pool].max_used = pool_ctl[pool].in_use;
91       }
92       V(mutex);
93       Dmsg3(150, "sm_get_pool_memory reuse %x to %s:%d\n", buf, fname, lineno);
94       sm_new_owner(fname, lineno, (char *)buf);
95       return (POOLMEM *)((char *)buf+HEAD_SIZE);
96    }
97       
98    if ((buf = (struct abufhead *) sm_malloc(fname, lineno, pool_ctl[pool].size+HEAD_SIZE)) == NULL) {
99       V(mutex);
100       Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", pool_ctl[pool].size);
101    }
102    buf->ablen = pool_ctl[pool].size;
103    buf->pool = pool;
104    pool_ctl[pool].in_use++;
105    if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
106       pool_ctl[pool].max_used = pool_ctl[pool].in_use;
107    }
108    V(mutex);
109    Dmsg3(150, "sm_get_pool_memory give %x to %s:%d\n", buf, fname, lineno);
110    return (POOLMEM *)((char *)buf+HEAD_SIZE);
111 }
112
113 /* Get nonpool memory of size requested */
114 POOLMEM *sm_get_memory(char *fname, int lineno, size_t size)
115 {
116    struct abufhead *buf;
117    int pool = 0;
118
119    sm_check(fname, lineno, True);
120    if ((buf = (struct abufhead *) sm_malloc(fname, lineno, size+HEAD_SIZE)) == NULL) {
121       Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
122    }
123    buf->ablen = size;
124    buf->pool = pool;
125    buf->next = NULL;
126    pool_ctl[pool].in_use++;
127    if (pool_ctl[pool].in_use > pool_ctl[pool].max_used)
128       pool_ctl[pool].max_used = pool_ctl[pool].in_use;
129    return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
130 }
131
132 #else
133
134 POOLMEM *get_pool_memory(int pool)
135 {
136    struct abufhead *buf;
137
138    P(mutex);
139    if (pool_ctl[pool].free_buf) {
140       buf = pool_ctl[pool].free_buf;
141       pool_ctl[pool].free_buf = buf->next;
142       V(mutex);
143       return (POOLMEM *)((char *)buf+HEAD_SIZE);
144    }
145       
146    if ((buf=malloc(pool_ctl[pool].size+HEAD_SIZE)) == NULL) {
147       V(mutex);
148       Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", pool_ctl[pool].size);
149    }
150    buf->ablen = pool_ctl[pool].size;
151    buf->pool = pool;
152    buf->next = NULL;
153    pool_ctl[pool].in_use++;
154    if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
155       pool_ctl[pool].max_used = pool_ctl[pool].in_use;
156    }
157    V(mutex);
158    return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
159 }
160
161 /* Get nonpool memory of size requested */
162 POOLMEM *get_memory(size_t size)
163 {
164    struct abufhead *buf;
165    int pool = 0;
166
167    if ((buf=malloc(size+HEAD_SIZE)) == NULL) {
168       Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
169    }
170    buf->ablen = size;
171    buf->pool = pool;
172    buf->next = NULL;
173    pool_ctl[pool].in_use++;
174    if (pool_ctl[pool].in_use > pool_ctl[pool].max_used) {
175       pool_ctl[pool].max_used = pool_ctl[pool].in_use;
176    }
177    return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
178 }
179 #endif /* SMARTALLOC */
180
181
182
183 /* Free a memory buffer */
184 void free_pool_memory(POOLMEM *obuf)
185 {
186    struct abufhead *buf;
187    int pool;
188
189    sm_check(__FILE__, __LINE__, False);
190    ASSERT(obuf);
191    P(mutex);
192    buf = (struct abufhead *)((char *)obuf - HEAD_SIZE);
193    pool = buf->pool;
194    pool_ctl[pool].in_use--;
195    if (pool == 0) {
196       free((char *)buf);              /* free nonpooled memory */
197    } else {                           /* otherwise link it to the free pool chain */
198       struct abufhead *next;
199       for (next=pool_ctl[pool].free_buf; next; next=next->next) {
200          ASSERT(next != buf);  /* attempt to free twice */
201       }
202       buf->next = pool_ctl[pool].free_buf;
203       pool_ctl[pool].free_buf = buf;
204    }
205    Dmsg2(150, "free_pool_memory %x pool=%d\n", buf, pool);
206    V(mutex);
207 }
208
209
210 /* Return the size of a memory buffer */
211 size_t sizeof_pool_memory(POOLMEM *obuf)
212 {
213    char *cp = (char *)obuf;
214
215    sm_check(__FILE__, __LINE__, False);
216    ASSERT(obuf);
217    cp -= HEAD_SIZE;
218    return ((struct abufhead *)cp)->ablen;
219 }
220
221 /* Realloc pool memory buffer */
222 POOLMEM *realloc_pool_memory(POOLMEM *obuf, size_t size)
223 {
224    char *cp = (char *)obuf;
225    void *buf;
226    int pool;
227
228    sm_check(__FILE__, __LINE__, False);
229    ASSERT(obuf);
230    P(mutex);
231    cp -= HEAD_SIZE;
232    buf = realloc(cp, size+HEAD_SIZE);
233    if (buf == NULL) {
234       V(mutex);
235       Emsg1(M_ABORT, 0, "Out of memory requesting %d bytes\n", size);
236    }
237    ((struct abufhead *)buf)->ablen = size;
238    pool = ((struct abufhead *)buf)->pool;
239    if (size > pool_ctl[pool].max_size) {
240       pool_ctl[pool].max_size = size;
241    }
242    V(mutex);
243    sm_check(__FILE__, __LINE__, False);
244    return (POOLMEM *)(((char *)buf)+HEAD_SIZE);
245 }
246
247 POOLMEM *check_pool_memory_size(POOLMEM *obuf, size_t size)
248 {
249    sm_check(__FILE__, __LINE__, False);
250    ASSERT(obuf);
251    if (size <= sizeof_pool_memory(obuf)) {
252       return obuf;
253    }
254    return realloc_pool_memory(obuf, size);
255 }
256
257 /* Release all pooled memory */
258 void close_memory_pool()
259 {
260    struct abufhead *buf, *next;
261    int i;
262
263    sm_check(__FILE__, __LINE__, False);
264    P(mutex);
265    for (i=1; i<=PM_MAX; i++) {
266       buf = pool_ctl[i].free_buf;
267       while (buf) {
268          next = buf->next;
269          free((char *)buf);
270          buf = next;
271       }
272       pool_ctl[i].free_buf = NULL;
273    }
274    V(mutex);
275 }
276
277 #ifdef DEBUG
278
279 static char *pool_name(int pool)
280 {
281    static char *name[] = {"NoPool", "FNAME ", "MSG   ", "EMSG  "};
282    static char buf[30];
283
284    if (pool >= 0 && pool <= PM_MAX) {
285       return name[pool];
286    }
287    sprintf(buf, "%-6d", pool);
288    return buf;
289 }
290    
291 /* Print staticstics on memory pool usage   
292  */ 
293 void print_memory_pool_stats()
294 {
295    int i;
296
297    Dmsg0(-1, "Pool   Maxsize  Maxused  Inuse\n");
298    for (i=0; i<=PM_MAX; i++)
299       Dmsg4(-1, "%5s  %7d  %7d  %5d\n", pool_name(i), pool_ctl[i].max_size,
300          pool_ctl[i].max_used, pool_ctl[i].in_use);
301
302    Dmsg0(-1, "\n");
303 }
304
305 #else
306 void print_memory_pool_stats() {} 
307 #endif /* DEBUG */