]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
Use Pmsg in smartall.c rather than printf for tracing Windows
[bacula/bacula] / bacula / src / lib / smartall.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29
30         Definitions for the smart memory allocator
31
32 */
33
34 extern uint64_t DLL_IMP_EXP sm_max_bytes;
35 extern uint64_t DLL_IMP_EXP sm_bytes;
36 extern uint32_t DLL_IMP_EXP sm_max_buffers;
37 extern uint32_t DLL_IMP_EXP sm_buffers;
38
39 #ifdef  SMARTALLOC
40 #undef  SMARTALLOC
41 #define SMARTALLOC SMARTALLOC
42
43
44 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
45             *sm_calloc(const char *fname, int lineno,
46                 unsigned int nelem, unsigned int elsize),
47             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
48             *actuallymalloc(unsigned int size),
49             *actuallycalloc(unsigned int nelem, unsigned int elsize),
50             *actuallyrealloc(void *ptr, unsigned int size);
51 extern void sm_free(const char *fname, int lineno, void *fp);
52 extern void actuallyfree(void *cp),
53             sm_dump(bool bufdump, bool in_use=false), sm_static(int mode);
54 extern void sm_new_owner(const char *fname, int lineno, char *buf);
55
56 #ifdef SMCHECK
57 #define Dsm_check(lvl) if ((lvl)<=debug_level) sm_check(__FILE__, __LINE__, true)
58 extern void sm_check(const char *fname, int lineno, bool bufdump);
59 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
60 #else
61 #define Dsm_check(lvl)
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
101 #define New(type) new(__FILE__, __LINE__) type
102
103 class SMARTALLOC
104 {
105 public:
106
107 void *operator new(size_t s, const char *fname, int line)
108 {
109   void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
110   return p;
111 }
112 void *operator new[](size_t s, const char *fname, int line)
113 {
114    void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
115    return p;
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*/, const char * /*fname*/, int /*line*/)
131 {
132    free(ptr);
133 }
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
142 #else
143
144 #define New(type) new type
145
146 class SMARTALLOC
147 {
148    public:
149       void *operator new(size_t s) {
150           return malloc(s);
151       }
152       void *operator new[](size_t s) {
153           return malloc(s);
154       }
155       void  operator delete(void *ptr) {
156           free(ptr);
157       }
158       void  operator delete[](void *ptr, size_t i) {
159           free(ptr);
160       }
161 };
162 #endif