]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/smartall.h
Fix header file includes.
[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-2005 Kern Sibbald
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
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
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 
20    the file LICENSE for additional details.
21
22  */
23
24 extern uint64_t DLL_IMP_EXP sm_max_bytes;
25 extern uint64_t DLL_IMP_EXP sm_bytes;
26 extern uint32_t DLL_IMP_EXP sm_max_buffers;
27 extern uint32_t DLL_IMP_EXP sm_buffers;
28
29 #ifdef  SMARTALLOC
30 #undef  SMARTALLOC
31 #define SMARTALLOC SMARTALLOC
32
33
34 extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
35             *sm_calloc(const char *fname, int lineno,
36                 unsigned int nelem, unsigned int elsize),
37             *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
38             *actuallymalloc(unsigned int size),
39             *actuallycalloc(unsigned int nelem, unsigned int elsize),
40             *actuallyrealloc(void *ptr, unsigned int size);
41 extern void sm_free(const char *fname, int lineno, void *fp);
42 extern void actuallyfree(void *cp),
43             sm_dump(bool bufdump), sm_static(int mode);
44 extern void sm_new_owner(const char *fname, int lineno, char *buf);
45
46 #ifdef SMCHECK
47 extern void sm_check(const char *fname, int lineno, bool bufdump);
48 extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
49 #else
50 #define sm_check(f, l, fl)
51 #define sm_check_rtn(f, l, fl) 1
52 #endif
53
54
55 /* Redefine standard memory allocator calls to use our routines
56    instead. */
57
58 #define free(x)        sm_free(__FILE__, __LINE__, (x))
59 #define cfree(x)       sm_free(__FILE__, __LINE__, (x))
60 #define malloc(x)      sm_malloc(__FILE__, __LINE__, (x))
61 #define calloc(n,e)    sm_calloc(__FILE__, __LINE__, (n), (e))
62 #define realloc(p,x)   sm_realloc(__FILE__, __LINE__, (p), (x))
63
64 #else
65
66 /* If SMARTALLOC is disabled, define its special calls to default to
67    the standard routines.  */
68
69 #define actuallyfree(x)      free(x)
70 #define actuallymalloc(x)    malloc(x)
71 #define actuallycalloc(x,y)  calloc(x,y)
72 #define actuallyrealloc(x,y) realloc(x,y)
73 #define sm_dump(x)
74 #define sm_static(x)
75 #define sm_new_owner(a, b, c)
76 #define sm_malloc(f, l, n) malloc(n)
77
78 #define sm_check(f, l, fl)
79 #define sm_check_rtn(f, l, fl) 1
80
81 extern void *b_malloc();
82 #define malloc(x) b_malloc(__FILE__, __LINE__, (x))
83
84
85 #endif
86
87 #ifdef SMARTALLOC
88 // #ifdef xxx
89
90 #define New(type) new(__FILE__, __LINE__) type
91
92 class SMARTALLOC
93 {
94 public:
95
96 void *operator new(size_t s, const char *fname, int line)
97 {
98   void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
99   return p;
100 }
101 void *operator new[](size_t s, const char *fname, int line)
102 {
103    void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int));
104    return p;
105 }
106 void  operator delete(void *ptr)
107 {
108    free(ptr);
109 }
110 void  operator delete[](void *ptr, size_t i)
111 {
112    free(ptr);
113 }
114
115 void  operator delete(void *ptr, const char *fname, int line)
116 {
117    free(ptr);
118 }
119 void  operator delete[](void *ptr, size_t i, const char *fname, int line)
120 {
121    free(ptr);
122 }
123
124
125 private:
126 void *operator new(size_t s) throw() { return 0; }
127 void *operator new[](size_t s) throw() { return 0; }
128 };
129
130
131 #else
132
133 #define New(type) new type
134
135 class SMARTALLOC
136 {
137    public:
138       void *operator new(size_t s)
139       {
140           return malloc(s);
141       }
142       void *operator new[](size_t s)
143       {
144           return malloc(s);
145       }
146       void  operator delete(void *ptr)
147       {
148           free(ptr);
149       }
150       void  operator delete[](void *ptr, size_t i)
151       {
152           free(ptr);
153       }
154 };
155 #endif