]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
Add new/delete operators with memset(0) on smartalloc
[bacula/bacula] / bacula / src / lib / smartall.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2010 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 three of the GNU Affero 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 Affero 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
30         Definitions for the smart memory allocator
31
32 */
33
34 #ifndef SMARTALLOC_H
35 #define SMARTALLOC_H
36
37 extern uint64_t DLL_IMP_EXP sm_max_bytes;
38 extern uint64_t DLL_IMP_EXP sm_bytes;
39 extern uint32_t DLL_IMP_EXP sm_max_buffers;
40 extern uint32_t DLL_IMP_EXP sm_buffers;
41
42 #ifdef  SMARTALLOC
43 #undef  SMARTALLOC
44 #define SMARTALLOC SMARTALLOC
45
46
47 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
48             *sm_calloc(const char *fname, int lineno,
49                 unsigned int nelem, unsigned int elsize),
50             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
51             *actuallymalloc(unsigned int size),
52             *actuallycalloc(unsigned int nelem, unsigned int elsize),
53             *actuallyrealloc(void *ptr, unsigned int size);
54 extern void sm_free(const char *fname, int lineno, void *fp);
55 extern void actuallyfree(void *cp),
56             sm_dump(bool bufdump, bool in_use=false), sm_static(int mode);
57 extern void sm_new_owner(const char *fname, int lineno, char *buf);
58
59 #ifdef SMCHECK
60 #define Dsm_check(lvl) if ((lvl)<=debug_level) sm_check(__FILE__, __LINE__, true)
61 extern void sm_check(const char *fname, int lineno, bool bufdump);
62 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
63 #else
64 #define Dsm_check(lvl)
65 #define sm_check(f, l, fl)
66 #define sm_check_rtn(f, l, fl) 1
67 #endif
68
69
70 /* Redefine standard memory allocator calls to use our routines
71    instead. */
72
73 #define free(x)        sm_free(__FILE__, __LINE__, (x))
74 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
75 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
76 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
77 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
78
79 #else
80
81 /* If SMARTALLOC is disabled, define its special calls to default to
82    the standard routines.  */
83
84 #define actuallyfree(x)      free(x)
85 #define actuallymalloc(x)    malloc(x)
86 #define actuallycalloc(x,y)  calloc(x,y)
87 #define actuallyrealloc(x,y) realloc(x,y)
88 #define sm_dump(x)
89 #define sm_static(x)
90 #define sm_new_owner(a, b, c)
91 #define sm_malloc(f, l, n)     malloc(n)
92 #define sm_free(f,l n)         free(n)
93 #define sm_check(f, l, fl)
94 #define sm_check_rtn(f, l, fl) 1
95
96 extern void *b_malloc();
97 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))
98
99
100 #endif
101
102 #ifdef SMARTALLOC
103
104 #define New(type) new(__FILE__, __LINE__) type
105
106 class SMARTALLOC
107 {
108 public:
109
110 void *operator new(size_t s, const char *fname, int line)
111 {
112   void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
113   return p;
114 }
115 void *operator new[](size_t s, const char *fname, int line)
116 {
117    void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
118    return p;
119 }
120 void  operator delete(void *ptr)
121 {
122    free(ptr);
123 }
124 void  operator delete[](void *ptr, size_t /*i*/)
125 {
126    free(ptr);
127 }
128
129 void  operator delete(void *ptr, const char * /*fname*/, int /*line*/)
130 {
131    free(ptr);
132 }
133 void  operator delete[](void *ptr, size_t /*i*/, const char * /*fname*/, int /*line*/)
134 {
135    free(ptr);
136 }
137
138
139 private:
140 void *operator new(size_t s) throw() { (void)s; return 0; }
141 void *operator new[](size_t s) throw() { (void)s; return 0; }
142 };
143
144 #else
145
146 #define New(type) new type
147
148 class SMARTALLOC
149 {
150    public:
151       void *operator new(size_t s) {
152           return malloc(s);
153       }
154       void *operator new[](size_t s) {
155           return malloc(s);
156       }
157       void  operator delete(void *ptr) {
158           free(ptr);
159       }
160       void  operator delete[](void *ptr, size_t i) {
161           free(ptr);
162       }
163 };
164 #endif  /* SMARTALLOC */
165
166 /* We do memset(0) because it's not possible to memset a class when
167  * using subclass with virtual functions
168  */
169
170 /* Now, sm_malloc and sm_free can be smartalloc or normal function */
171 inline void *operator new(size_t size, char const * file, int line)
172 {
173    void *pnew = sm_malloc(file,line, size);
174    memset((char *)pnew, 0, size);
175    return pnew;
176 }
177
178 inline void *operator new[](size_t size, char const * file, int line)
179 {
180    void *pnew = sm_malloc(file, line, size);
181    memset((char *)pnew, 0, size);
182    return pnew;
183 }
184
185 inline void *operator new(size_t size)
186 {
187    void *pnew = sm_malloc(__FILE__, __LINE__, size);
188    memset((char *)pnew, 0, size);
189    return pnew;
190 }
191
192 inline void *operator new[](size_t size)
193 {
194    void *pnew = sm_malloc(__FILE__, __LINE__, size);
195    memset((char *)pnew, 0, size);
196    return pnew;
197 }
198
199 //#define new   new(__FILE__, __LINE__)
200
201 inline void operator delete(void *buf)
202 {
203    sm_free( __FILE__, __LINE__, buf);
204 }
205
206 inline void operator delete[] (void *buf)
207 {
208   sm_free(__FILE__, __LINE__, buf);
209 }
210
211
212 #endif  /* !SMARTALLOC_H */