]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
3f202974b6c4f97b8bcb7cee1086f86cb549fe1e
[bacula/bacula] / bacula / src / lib / smartall.h
1 /*
2
3         Definitions for the smart memory allocator
4   
5      Version $Id$
6
7 */
8
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 extern uint64_t sm_max_bytes;
30 extern uint64_t sm_bytes;
31 extern uint32_t sm_max_buffers;
32 extern uint32_t 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), sm_static(int mode);
49 extern void sm_new_owner(const char *fname, int lineno, char *buf);
50
51 #ifdef SMCHECK
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 sm_check(f, l, fl)
56 #define sm_check_rtn(f, l, fl) 1
57 #endif
58
59
60 /* Redefine standard memory allocator calls to use our routines
61    instead. */
62
63 #define free(x)        sm_free(__FILE__, __LINE__, (x))
64 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
65 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
66 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
67 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
68
69 #else
70
71 /* If SMARTALLOC is disabled, define its special calls to default to
72    the standard routines.  */
73
74 #define actuallyfree(x)      free(x)
75 #define actuallymalloc(x)    malloc(x)
76 #define actuallycalloc(x,y)  calloc(x,y)
77 #define actuallyrealloc(x,y) realloc(x,y)
78 #define sm_dump(x)
79 #define sm_static(x)
80 #define sm_new_owner(a, b, c)
81 #define sm_malloc(f, l, n) malloc(n)
82
83 #define sm_check(f, l, fl)
84 #define sm_check_rtn(f, l, fl) 1
85
86 extern void *b_malloc();
87 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))                  
88
89
90 #endif
91
92 #ifdef SMARTALLOC
93 // #ifdef xxx
94
95 #define New(type) new(__FILE__, __LINE__) type
96
97 class SMARTALLOC
98 {
99 public:
100
101 void *operator new(size_t s, const char *fname, int line)
102 {
103   void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
104   return p;
105 }
106 void *operator new[](size_t s, const char *fname, int line)
107 {
108    void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
109    return p;
110 }
111 void  operator delete(void *ptr)
112 {
113    free(ptr);
114 }
115 void  operator delete[](void *ptr, size_t i)
116 {
117    free(ptr);
118 }
119
120 void  operator delete(void *ptr, const char *fname, int line)
121 {
122    free(ptr);
123 }
124 void  operator delete[](void *ptr, size_t i, const char *fname, int line) 
125 {
126    free(ptr);
127 }
128
129
130 private:
131 void *operator new(size_t s) throw() { return 0; }
132 void *operator new[](size_t s) throw() { return 0; }
133 };
134  
135
136 #else
137
138 #define New(type) new type
139
140 class SMARTALLOC
141 {
142    public:
143       void *operator new(size_t s)
144       {
145           return malloc(s);
146       }
147       void *operator new[](size_t s)
148       {
149           return malloc(s);
150       }
151       void  operator delete(void *ptr)
152       {
153           free(ptr);
154       }
155       void  operator delete[](void *ptr, size_t i)
156       {
157           free(ptr);
158       }
159 };
160 #endif