]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
734b9100908c1d3f8505b92af4d4576506431f18
[bacula/bacula] / bacula / src / lib / smartall.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
30         Definitions for the smart memory allocator
31
32      Version $Id$
33
34 */
35
36 extern uint64_t DLL_IMP_EXP sm_max_bytes;
37 extern uint64_t DLL_IMP_EXP sm_bytes;
38 extern uint32_t DLL_IMP_EXP sm_max_buffers;
39 extern uint32_t DLL_IMP_EXP sm_buffers;
40
41 #ifdef  SMARTALLOC
42 #undef  SMARTALLOC
43 #define SMARTALLOC SMARTALLOC
44
45
46 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
47             *sm_calloc(const char *fname, int lineno,
48                 unsigned int nelem, unsigned int elsize),
49             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
50             *actuallymalloc(unsigned int size),
51             *actuallycalloc(unsigned int nelem, unsigned int elsize),
52             *actuallyrealloc(void *ptr, unsigned int size);
53 extern void sm_free(const char *fname, int lineno, void *fp);
54 extern void actuallyfree(void *cp),
55             sm_dump(bool bufdump, bool in_use=false), sm_static(int mode);
56 extern void sm_new_owner(const char *fname, int lineno, char *buf);
57
58 #ifdef SMCHECK
59 #define Dsm_check(lvl) if ((lvl)<=debug_level) sm_check(__FILE__, __LINE__, true)
60 extern void sm_check(const char *fname, int lineno, bool bufdump);
61 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
62 #else
63 #define Dsm_check(lvl)
64 #define sm_check(f, l, fl)
65 #define sm_check_rtn(f, l, fl) 1
66 #endif
67
68
69 /* Redefine standard memory allocator calls to use our routines
70    instead. */
71
72 #define free(x)        sm_free(__FILE__, __LINE__, (x))
73 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
74 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
75 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
76 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
77
78 #else
79
80 /* If SMARTALLOC is disabled, define its special calls to default to
81    the standard routines.  */
82
83 #define actuallyfree(x)      free(x)
84 #define actuallymalloc(x)    malloc(x)
85 #define actuallycalloc(x,y)  calloc(x,y)
86 #define actuallyrealloc(x,y) realloc(x,y)
87 #define sm_dump(x)
88 #define sm_static(x)
89 #define sm_new_owner(a, b, c)
90 #define sm_malloc(f, l, n) malloc(n)
91
92 #define sm_check(f, l, fl)
93 #define sm_check_rtn(f, l, fl) 1
94
95 extern void *b_malloc();
96 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))
97
98
99 #endif
100
101 #ifdef SMARTALLOC
102
103 #define New(type) new(__FILE__, __LINE__) type
104
105 class SMARTALLOC
106 {
107 public:
108
109 void *operator new(size_t s, const char *fname, int line)
110 {
111   void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
112   return p;
113 }
114 void *operator new[](size_t s, const char *fname, int line)
115 {
116    void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
117    return p;
118 }
119 void  operator delete(void *ptr)
120 {
121    free(ptr);
122 }
123 void  operator delete[](void *ptr, size_t /*i*/)
124 {
125    free(ptr);
126 }
127
128 void  operator delete(void *ptr, const char * /*fname*/, int /*line*/)
129 {
130    free(ptr);
131 }
132 void  operator delete[](void *ptr, size_t /*i*/, const char * /*fname*/, int /*line*/)
133 {
134    free(ptr);
135 }
136
137
138 private:
139 void *operator new(size_t s) throw() { (void)s; return 0; }
140 void *operator new[](size_t s) throw() { (void)s; return 0; }
141 };
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       {
153           return malloc(s);
154       }
155       void *operator new[](size_t s)
156       {
157           return malloc(s);
158       }
159       void  operator delete(void *ptr)
160       {
161           free(ptr);
162       }
163       void  operator delete[](void *ptr, size_t i)
164       {
165           free(ptr);
166       }
167 };
168 #endif