]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / lib / smartall.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21
22         Definitions for the smart memory allocator
23
24 */
25
26 #ifndef SMARTALLOC_H
27 #define SMARTALLOC_H
28
29 extern uint64_t DLL_IMP_EXP sm_max_bytes;
30 extern uint64_t DLL_IMP_EXP sm_bytes;
31 extern uint32_t DLL_IMP_EXP sm_max_buffers;
32 extern uint32_t DLL_IMP_EXP sm_buffers;
33
34 #ifdef  SMARTALLOC
35 #undef  SMARTALLOC
36 #define SMARTALLOC SMARTALLOC
37
38
39 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
40             *sm_calloc(const char *fname, int lineno,
41                 unsigned int nelem, unsigned int elsize),
42             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
43             *actuallymalloc(unsigned int size),
44             *actuallycalloc(unsigned int nelem, unsigned int elsize),
45             *actuallyrealloc(void *ptr, unsigned int size);
46 extern void sm_free(const char *fname, int lineno, void *fp);
47 extern void actuallyfree(void *cp),
48             sm_dump(bool bufdump, bool in_use=false), sm_static(int mode);
49 extern void sm_new_owner(const char *fname, int lineno, char *buf);
50
51 #ifdef SMCHECK
52 #define Dsm_check(lvl) if ((lvl)<=debug_level) sm_check(__FILE__, __LINE__, true)
53 extern void sm_check(const char *fname, int lineno, bool bufdump);
54 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
55 #else
56 #define Dsm_check(lvl)
57 #define sm_check(f, l, fl)
58 #define sm_check_rtn(f, l, fl) 1
59 #endif
60
61
62 /* Redefine standard memory allocator calls to use our routines
63    instead. */
64
65 #define free(x)        sm_free(__FILE__, __LINE__, (x))
66 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
67 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
68 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
69 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
70
71 #else
72
73 /* If SMARTALLOC is disabled, define its special calls to default to
74    the standard routines.  */
75
76 #define actuallyfree(x)      free(x)
77 #define actuallymalloc(x)    malloc(x)
78 #define actuallycalloc(x,y)  calloc(x,y)
79 #define actuallyrealloc(x,y) realloc(x,y)
80 #define sm_dump(x)
81 #define sm_static(x)
82 #define sm_new_owner(a, b, c)
83 #define sm_malloc(f, l, n)     malloc(n)
84 #define sm_free(f, l, n)       free(n)
85 #define sm_check(f, l, fl)
86 #define sm_check_rtn(f, l, fl) 1
87
88 extern void *b_malloc();
89 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))
90
91
92 #endif
93
94 #ifdef SMARTALLOC
95
96 #define New(type) new(__FILE__, __LINE__) type
97
98 /* We do memset(0) because it's not possible to memset a class when
99  * using subclass with virtual functions
100  */
101
102 class SMARTALLOC
103 {
104 public:
105
106 void *operator new(size_t s, const char *fname, int line)
107 {
108    size_t size =  s > sizeof(int) ? (unsigned int)s : sizeof(int);
109    void *p = sm_malloc(fname, line, size);
110    memset(p, 0, size);
111    return p;
112 }
113 void *operator new[](size_t s, const char *fname, int line)
114 {
115    size_t size =  s > sizeof(int) ? (unsigned int)s : sizeof(int);
116    void *p = sm_malloc(fname, line, size);
117    memset(p, 0, size);
118    return p;
119 }
120
121 void  operator delete(void *ptr)
122 {
123    free(ptr);
124 }
125 void  operator delete[](void *ptr, size_t /*i*/)
126 {
127    free(ptr);
128 }
129
130 void  operator delete(void *ptr, const char * /*fname*/, int /*line*/)
131 {
132    free(ptr);
133 }
134 void  operator delete[](void *ptr, size_t /*i*/,
135                         const char * /*fname*/, int /*line*/)
136 {
137    free(ptr);
138 }
139
140 private:
141 void *operator new(size_t s) throw() { (void)s; return 0; }
142 void *operator new[](size_t s) throw() { (void)s; return 0; }
143 };
144
145 #else
146
147 #define New(type) new type
148
149 class SMARTALLOC
150 {
151    public:
152       void *operator new(size_t s) {
153          void *p = malloc(s);
154          memset(p, 0, s);
155          return p;
156       }
157       void *operator new[](size_t s) {
158          void *p = malloc(s);
159          memset(p, 0, s);
160          return p;
161       }
162       void  operator delete(void *ptr) {
163           free(ptr);
164       }
165       void  operator delete[](void *ptr, size_t i) {
166           free(ptr);
167       }
168 };
169 #endif  /* SMARTALLOC */
170
171 #endif  /* !SMARTALLOC_H */