/*
- Definitions for the smart memory allocator
+ Definitions for the smart memory allocator
+
+ Version $Id$
*/
/*
- Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+ Copyright (C) 2000-2004 Kern Sibbald and John Walker
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
*/
+extern uint64_t sm_max_bytes;
+extern uint64_t sm_bytes;
+extern uint32_t sm_max_buffers;
+extern uint32_t sm_buffers;
-#ifdef SMARTALLOC
+#ifdef SMARTALLOC
+#undef SMARTALLOC
+#define SMARTALLOC SMARTALLOC
-typedef enum {False = 0, True = 1} Boolean;
-extern void *sm_malloc(char *fname, int lineno, unsigned int nbytes),
- *sm_calloc(char *fname, int lineno,
- unsigned int nelem, unsigned int elsize),
- *sm_realloc(char *fname, int lineno, void *ptr, unsigned int size),
- *actuallymalloc(unsigned int size),
- *actuallycalloc(unsigned int nelem, unsigned int elsize),
- *actuallyrealloc(void *ptr, unsigned int size);
-extern void sm_free(char *fname, int lineno, void *fp);
+extern void *sm_malloc(const char *fname, int lineno, unsigned int nbytes),
+ *sm_calloc(const char *fname, int lineno,
+ unsigned int nelem, unsigned int elsize),
+ *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size),
+ *actuallymalloc(unsigned int size),
+ *actuallycalloc(unsigned int nelem, unsigned int elsize),
+ *actuallyrealloc(void *ptr, unsigned int size);
+extern void sm_free(const char *fname, int lineno, void *fp);
extern void actuallyfree(void *cp),
- sm_dump(Boolean bufdump), sm_static(int mode);
-extern void sm_new_owner(char *fname, int lineno, char *buf);
+ sm_dump(bool bufdump), sm_static(int mode);
+extern void sm_new_owner(const char *fname, int lineno, char *buf);
#ifdef SMCHECK
-extern void sm_check(char *fname, int lineno, Boolean bufdump);
-extern int sm_check_rtn(char *fname, int lineno, Boolean bufdump);
+extern void sm_check(const char *fname, int lineno, bool bufdump);
+extern int sm_check_rtn(const char *fname, int lineno, bool bufdump);
#else
#define sm_check(f, l, fl)
#define sm_check_rtn(f, l, fl) 1
#define sm_check_rtn(f, l, fl) 1
extern void *b_malloc();
-#define malloc(x) b_malloc(__FILE__, __LINE__, (x))
+#define malloc(x) b_malloc(__FILE__, __LINE__, (x))
+
+
+#endif
+#ifdef SMARTALLOC
+// #ifdef xxx
+
+#define New(type) new(__FILE__, __LINE__) type
+
+class SMARTALLOC
+{
+public:
+
+void *operator new(size_t s, const char *fname, int line)
+{
+ void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
+ return p;
+}
+void *operator new[](size_t s, const char *fname, int line)
+{
+ void *p = sm_malloc(fname, line, s > sizeof(int) ? s : sizeof(int));
+ return p;
+}
+void operator delete(void *ptr)
+{
+ free(ptr);
+}
+void operator delete[](void *ptr, size_t i)
+{
+ free(ptr);
+}
+
+void operator delete(void *ptr, const char *fname, int line)
+{
+ free(ptr);
+}
+void operator delete[](void *ptr, size_t i, const char *fname, int line)
+{
+ free(ptr);
+}
+
+
+private:
+void *operator new(size_t s) throw() { return 0; }
+void *operator new[](size_t s) throw() { return 0; }
+};
+
+
+#else
+#define New(type) new type
+
+class SMARTALLOC
+{
+ public:
+ void *operator new(size_t s)
+ {
+ return malloc(s);
+ }
+ void *operator new[](size_t s)
+ {
+ return malloc(s);
+ }
+ void operator delete(void *ptr)
+ {
+ free(ptr);
+ }
+ void operator delete[](void *ptr, size_t i)
+ {
+ free(ptr);
+ }
+};
#endif