]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
kes Add dynamic dll entry point for SHGetFolderPath to Win32 code.
[bacula/bacula] / bacula / src / lib / smartall.h
1 /*
2
3         Definitions for the smart memory allocator
4
5      Version $Id$
6
7 */
8 /*
9    Bacula® - The Network Backup Solution
10
11    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
12
13    The main author of Bacula is Kern Sibbald, with contributions from
14    many others, a complete list can be found in the file AUTHORS.
15    This program is Free Software; you can redistribute it and/or
16    modify it under the terms of version two of the GNU General Public
17    License as published by the Free Software Foundation plus additions
18    that are listed in the file LICENSE.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28    02110-1301, USA.
29
30    Bacula® is a registered trademark ofJohn Walker.
31    The licensor of Bacula is the Free Software Foundation Europe
32    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
33    Switzerland, email:ftf@fsfeurope.org.
34 */
35
36 extern uint64_t DLL_IMP_EXP sm_max_bytes;
37 extern uint64_t DLL_IMP_EXP sm_bytes;
38 extern uint32_t DLL_IMP_EXP sm_max_buffers;
39 extern uint32_t DLL_IMP_EXP sm_buffers;
40
41 #ifdef  SMARTALLOC
42 #undef  SMARTALLOC
43 #define SMARTALLOC SMARTALLOC
44
45
46 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
47             *sm_calloc(const char *fname, int lineno,
48                 unsigned int nelem, unsigned int elsize),
49             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
50             *actuallymalloc(unsigned int size),
51             *actuallycalloc(unsigned int nelem, unsigned int elsize),
52             *actuallyrealloc(void *ptr, unsigned int size);
53 extern void sm_free(const char *fname, int lineno, void *fp);
54 extern void actuallyfree(void *cp),
55             sm_dump(bool bufdump), sm_static(int mode);
56 extern void sm_new_owner(const char *fname, int lineno, char *buf);
57
58 #ifdef SMCHECK
59 extern void sm_check(const char *fname, int lineno, bool bufdump);
60 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
61 #else
62 #define sm_check(f, l, fl)
63 #define sm_check_rtn(f, l, fl) 1
64 #endif
65
66
67 /* Redefine standard memory allocator calls to use our routines
68    instead. */
69
70 #define free(x)        sm_free(__FILE__, __LINE__, (x))
71 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
72 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
73 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
74 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
75
76 #else
77
78 /* If SMARTALLOC is disabled, define its special calls to default to
79    the standard routines.  */
80
81 #define actuallyfree(x)      free(x)
82 #define actuallymalloc(x)    malloc(x)
83 #define actuallycalloc(x,y)  calloc(x,y)
84 #define actuallyrealloc(x,y) realloc(x,y)
85 #define sm_dump(x)
86 #define sm_static(x)
87 #define sm_new_owner(a, b, c)
88 #define sm_malloc(f, l, n) malloc(n)
89
90 #define sm_check(f, l, fl)
91 #define sm_check_rtn(f, l, fl) 1
92
93 extern void *b_malloc();
94 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))
95
96
97 #endif
98
99 #ifdef SMARTALLOC
100 // #ifdef xxx
101
102 #define New(type) new(__FILE__, __LINE__) type
103
104 class SMARTALLOC
105 {
106 public:
107
108 void *operator new(size_t s, const char *fname, int line)
109 {
110   void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
111   return p;
112 }
113 void *operator new[](size_t s, const char *fname, int line)
114 {
115    void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
116    return p;
117 }
118 void  operator delete(void *ptr)
119 {
120    free(ptr);
121 }
122 void  operator delete[](void *ptr, size_t i)
123 {
124    free(ptr);
125 }
126
127 void  operator delete(void *ptr, const char *fname, int line)
128 {
129    free(ptr);
130 }
131 void  operator delete[](void *ptr, size_t i, const char *fname, int line)
132 {
133    free(ptr);
134 }
135
136
137 private:
138 void *operator new(size_t s) throw() { return 0; }
139 void *operator new[](size_t s) throw() { return 0; }
140 };
141
142
143 #else
144
145 #define New(type) new type
146
147 class SMARTALLOC
148 {
149    public:
150       void *operator new(size_t s)
151       {
152           return malloc(s);
153       }
154       void *operator new[](size_t s)
155       {
156           return malloc(s);
157       }
158       void  operator delete(void *ptr)
159       {
160           free(ptr);
161       }
162       void  operator delete[](void *ptr, size_t i)
163       {
164           free(ptr);
165       }
166 };
167 #endif