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