]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/mem_pool.h
add function to simplify free and null operation on pointers
[bacula/bacula] / bacula / src / lib / mem_pool.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Memory Pool prototypes
30  *
31  *  Kern Sibbald, MM
32  *
33  *   Version $Id$
34  */
35
36 #ifndef __MEM_POOL_H_
37 #define __MEM_POOL_H_
38
39
40 #ifdef SMARTALLOC
41
42 #define get_pool_memory(pool) sm_get_pool_memory(__FILE__, __LINE__, pool)
43 extern POOLMEM *sm_get_pool_memory(const char *file, int line, int pool);
44
45 #define get_memory(size) sm_get_memory(__FILE__, __LINE__, size)
46 extern POOLMEM *sm_get_memory(const char *fname, int line, int32_t size);
47
48 #define sizeof_pool_memory(buf) sm_sizeof_pool_memory(__FILE__, __LINE__, buf)
49 extern int32_t sm_sizeof_pool_memory(const char *fname, int line, POOLMEM *buf);
50
51 #define realloc_pool_memory(buf,size) sm_realloc_pool_memory(__FILE__, __LINE__, buf, size)
52 extern POOLMEM  *sm_realloc_pool_memory(const char *fname, int line, POOLMEM *buf, int32_t size);
53
54 #define check_pool_memory_size(buf,size) sm_check_pool_memory_size(__FILE__, __LINE__, buf, size)
55 extern POOLMEM  *sm_check_pool_memory_size(const char *fname, int line, POOLMEM *buf, int32_t size);
56
57 #define free_pool_memory(x) sm_free_pool_memory(__FILE__, __LINE__, x)
58 #define free_memory(x) sm_free_pool_memory(__FILE__, __LINE__, x)
59 extern void sm_free_pool_memory(const char *fname, int line, POOLMEM *buf);
60
61 #else
62
63 extern POOLMEM *get_pool_memory(int pool);
64 extern POOLMEM *get_memory(int32_t size);
65 extern int32_t sizeof_pool_memory(POOLMEM *buf);
66 extern POOLMEM  *realloc_pool_memory(POOLMEM *buf, int32_t size);
67 extern POOLMEM  *check_pool_memory_size(POOLMEM *buf, int32_t size);
68 #define free_memory(x) free_pool_memory(x)
69 extern void   free_pool_memory(POOLMEM *buf);
70
71 #endif
72
73 /* Function to simplify free/reset pointers */
74 inline void bfree_and_null(POOLMEM *&a) {
75    if (a) { 
76       free_pool_memory(a);
77       a = NULL;
78    } 
79 }
80
81 #define free_and_null_pool_memory(a) bfree_and_null((a))
82
83 extern void garbage_collect_memory_pool();
84 extern void  close_memory_pool();
85 extern void  print_memory_pool_stats();
86
87
88
89 #define PM_NOPOOL  0                  /* nonpooled memory */
90 #define PM_NAME    1                  /* Bacula name */
91 #define PM_FNAME   2                  /* file name buffer */
92 #define PM_MESSAGE 3                  /* daemon message */
93 #define PM_EMSG    4                  /* error message */
94 #define PM_MAX     PM_EMSG            /* Number of types */
95
96 class POOL_MEM {
97    char *mem;
98 public:
99    POOL_MEM() { mem = get_pool_memory(PM_NAME); *mem = 0; }
100    POOL_MEM(int pool) { mem = get_pool_memory(pool); *mem = 0; }
101    ~POOL_MEM() { free_pool_memory(mem); mem = NULL; }
102    char *c_str() const { return mem; }
103    POOLMEM *&addr() { return mem; }
104    int size() const { return sizeof_pool_memory(mem); }
105    char *check_size(int32_t size) {
106       mem = check_pool_memory_size(mem, size);
107       return mem;
108    }
109    int32_t max_size();
110    void realloc_pm(int32_t size);
111    int strcpy(const char *str);
112    int strcat(const char *str);
113 };
114
115 int pm_strcat(POOLMEM **pm, const char *str);
116 int pm_strcat(POOLMEM *&pm, const char *str);
117 int pm_strcat(POOL_MEM &pm, const char *str);
118 int pm_strcat(POOLMEM *&pm, POOL_MEM &str);
119
120 int pm_strcpy(POOLMEM **pm, const char *str);
121 int pm_strcpy(POOLMEM *&pm, const char *str);
122 int pm_strcpy(POOL_MEM &pm, const char *str);
123 int pm_strcpy(POOLMEM *&pm, POOL_MEM &str);
124
125 int pm_memcpy(POOLMEM **pm, const char *data, int32_t n);
126 int pm_memcpy(POOLMEM *&pm, const char *data, int32_t n);
127 int pm_memcpy(POOL_MEM &pm, const char *data, int32_t n);
128 int pm_memcpy(POOLMEM *&pm, POOL_MEM &data, int32_t n);
129
130 #endif