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