]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
!!! I didn't run the regression tests.!!!
[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
36
37 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
38             *sm_calloc(const char *fname, int lineno,
39                 unsigned int nelem, unsigned int elsize),
40             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
41             *actuallymalloc(unsigned int size),
42             *actuallycalloc(unsigned int nelem, unsigned int elsize),
43             *actuallyrealloc(void *ptr, unsigned int size);
44 extern void sm_free(const char *fname, int lineno, void *fp);
45 extern void actuallyfree(void *cp),
46             sm_dump(bool bufdump), sm_static(int mode);
47 extern void sm_new_owner(const char *fname, int lineno, char *buf);
48
49 #ifdef SMCHECK
50 extern void sm_check(const char *fname, int lineno, bool bufdump);
51 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
52 #else
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
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 /* BUG 2 brace is missing */
93
94 #undef SMARTALLOC 
95 #define SMARTALLOC SMARTALLOC
96
97 class SMARTALLOC
98 {
99 private:
100 public:
101
102 void *operator new(size_t s, const char *fname, int line)
103 {
104   void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
105   return p;
106 }
107 void *operator new[](size_t s, const char *fname, int line)
108 {
109    void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
110    return p;
111 }
112 void  operator delete(void *ptr)
113 {
114    free(ptr);
115 }
116 void  operator delete[](void *ptr, size_t i)
117 {
118    free(ptr);
119 }
120
121 private:
122 void *operator new(size_t s) throw() { return 0; }
123 void *operator new[](size_t s) throw() { return 0; }
124 };
125  
126
127 #else
128 #define New(type) new type
129
130 class SMARTALLOC
131 {
132    public:
133       void *operator new(size_t s)
134       {
135           return malloc(s);
136       }
137       void *operator new[](size_t s)
138       {
139           return malloc(s);
140       }
141       void  operator delete(void *ptr)
142       {
143           free(ptr);
144       }
145       void  operator delete[](void *ptr, size_t i)
146       {
147           free(ptr);
148       }
149 };
150 #endif