From 5c905b7de07d6ba2c131ab869ce5b7c3c4f7ab55 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Thu, 12 Aug 2010 15:20:08 +0200 Subject: [PATCH] Use SMARTALLOC+memset instead of overload new/delete that doesn't work in bat The global overload of new/delete in smartalloc.h crashes bat (seems to be incompatible with QT) I added memset(0) in default SMARTALLOC class, and now DEVICE uses it. --- bacula/src/lib/smartall.h | 75 +++++++++++---------------------------- bacula/src/stored/dev.c | 10 +++--- bacula/src/stored/dev.h | 2 +- 3 files changed, 27 insertions(+), 60 deletions(-) diff --git a/bacula/src/lib/smartall.h b/bacula/src/lib/smartall.h index c8cb0a85fe..4a2f90a160 100644 --- a/bacula/src/lib/smartall.h +++ b/bacula/src/lib/smartall.h @@ -103,20 +103,29 @@ extern void *b_malloc(); #define New(type) new(__FILE__, __LINE__) type +/* We do memset(0) because it's not possible to memset a class when + * using subclass with virtual functions + */ + class SMARTALLOC { public: void *operator new(size_t s, const char *fname, int line) { - void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int)); - return p; + size_t size = s > sizeof(int) ? (unsigned int)s : sizeof(int); + void *p = sm_malloc(fname, line, size); + memset(p, 0, size); + return p; } void *operator new[](size_t s, const char *fname, int line) { - void *p = sm_malloc(fname, line, s > sizeof(int) ? (unsigned int)s : sizeof(int)); + size_t size = s > sizeof(int) ? (unsigned int)s : sizeof(int); + void *p = sm_malloc(fname, line, size); + memset(p, 0, size); return p; } + void operator delete(void *ptr) { free(ptr); @@ -130,12 +139,12 @@ 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*/) +void operator delete[](void *ptr, size_t /*i*/, + const char * /*fname*/, int /*line*/) { free(ptr); } - private: void *operator new(size_t s) throw() { (void)s; return 0; } void *operator new[](size_t s) throw() { (void)s; return 0; } @@ -149,10 +158,14 @@ class SMARTALLOC { public: void *operator new(size_t s) { - return malloc(s); + void *p = malloc(s); + memset(p, 0, s); + return p; } void *operator new[](size_t s) { - return malloc(s); + void *p = malloc(s); + memset(p, 0, s); + return p; } void operator delete(void *ptr) { free(ptr); @@ -162,51 +175,5 @@ class SMARTALLOC } }; #endif /* SMARTALLOC */ - -/* We do memset(0) because it's not possible to memset a class when - * using subclass with virtual functions - */ - -/* Now, sm_malloc and sm_free can be smartalloc or normal function */ -inline void *operator new(size_t size, char const * file, int line) -{ - void *pnew = sm_malloc(file,line, size); - memset((char *)pnew, 0, size); - return pnew; -} - -inline void *operator new[](size_t size, char const * file, int line) -{ - void *pnew = sm_malloc(file, line, size); - memset((char *)pnew, 0, size); - return pnew; -} - -inline void *operator new(size_t size) -{ - void *pnew = sm_malloc(__FILE__, __LINE__, size); - memset((char *)pnew, 0, size); - return pnew; -} - -inline void *operator new[](size_t size) -{ - void *pnew = sm_malloc(__FILE__, __LINE__, size); - memset((char *)pnew, 0, size); - return pnew; -} - -//#define new new(__FILE__, __LINE__) - -inline void operator delete(void *buf) -{ - sm_free( __FILE__, __LINE__, buf); -} - -inline void operator delete[] (void *buf) -{ - sm_free(__FILE__, __LINE__, buf); -} - - + #endif /* !SMARTALLOC_H */ diff --git a/bacula/src/stored/dev.c b/bacula/src/stored/dev.c index 1ac76292fc..9fa2e31f52 100644 --- a/bacula/src/stored/dev.c +++ b/bacula/src/stored/dev.c @@ -147,26 +147,26 @@ init_dev(JCR *jcr, DEVRES *device) Jmsg0(jcr, M_FATAL, 0, _("DVD support is now deprecated\n")); return NULL; case B_VTAPE_DEV: - dev = new vtape; + dev = New(vtape); break; #ifdef USE_FTP case B_FTP_DEV: - dev = new ftp_device; + dev = New(ftp_device); break; #endif #ifdef HAVE_WIN32 /* TODO: defined in src/win32/stored/mtops.cpp */ case B_TAPE_DEV: - dev = new win32_tape_device; + dev = New(win32_tape_device); break; case B_FILE_DEV: - dev = new win32_file_device; + dev = New(win32_file_device); break; #else case B_TAPE_DEV: case B_FILE_DEV: case B_FIFO_DEV: - dev = new DEVICE; + dev = New(DEVICE); break; #endif default: diff --git a/bacula/src/stored/dev.h b/bacula/src/stored/dev.h index 8d6abadf1a..55b5998346 100644 --- a/bacula/src/stored/dev.h +++ b/bacula/src/stored/dev.h @@ -206,7 +206,7 @@ class VOLRES; /* forward reference */ * each physical device. Everything here is "global" to * that device and effects all jobs using the device. */ -class DEVICE { +class DEVICE: public SMARTALLOC { protected: int m_fd; /* file descriptor */ private: -- 2.39.5