From 3c8a4c458bc66cfe54e83c00f2d4460a52e04535 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 25 Jun 2012 10:35:24 +0000 Subject: more guardedalloc use in C++, also make compositorMutex a static var, was allocated and never freed. --- CMakeLists.txt | 1 + intern/ghost/GHOST_ISystem.h | 9 ++----- intern/ghost/GHOST_ISystemPaths.h | 5 ++++ intern/ghost/GHOST_ITimerTask.h | 9 ++----- intern/ghost/GHOST_Types.h | 6 ++++- intern/ghost/intern/GHOST_CallbackEventConsumer.h | 4 +++ intern/ghost/intern/GHOST_DisplayManager.h | 5 ++++ intern/ghost/intern/GHOST_EventManager.h | 5 ++++ intern/ghost/intern/GHOST_SystemPaths.h | 3 +-- intern/ghost/intern/GHOST_TimerManager.h | 5 ++++ intern/guardedalloc/cpp/mallocn.cpp | 30 +++++++++++++++++----- .../compositor/intern/COM_ChunkOrderHotspot.h | 9 +++++++ .../compositor/intern/COM_ExecutionSystem.cpp | 4 +++ .../blender/compositor/intern/COM_NodeOperation.h | 4 +++ source/blender/compositor/intern/COM_Socket.h | 4 +++ .../blender/compositor/intern/COM_SocketReader.h | 4 +++ .../blender/compositor/intern/COM_compositor.cpp | 15 ++++++----- source/gameengine/Expressions/PyObjectPlus.h | 2 +- 18 files changed, 92 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3614007ef9..0fb635d45d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1723,6 +1723,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PLATFORM_CFLAGS} ${CXX_WARNINGS}") # better not set includes here but this debugging option is off by default. if(WITH_CXX_GUARDEDALLOC) include_directories(${CMAKE_SOURCE_DIR}/intern/guardedalloc) + add_definitions(-DWITH_CXX_GUARDEDALLOC) endif() if(WITH_ASSERT_ABORT) diff --git a/intern/ghost/GHOST_ISystem.h b/intern/ghost/GHOST_ISystem.h index bd4f3aed5aa..0bc09077f39 100644 --- a/intern/ghost/GHOST_ISystem.h +++ b/intern/ghost/GHOST_ISystem.h @@ -415,14 +415,9 @@ protected: /** The one and only system */ static GHOST_ISystem *m_system; + #ifdef WITH_CXX_GUARDEDALLOC -public: - void *operator new(size_t num_bytes) { - return MEM_mallocN(num_bytes, "GHOST:GHOST_ISystem"); - } - void operator delete(void *mem) { - MEM_freeN(mem); - } + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_ISystem") #endif }; diff --git a/intern/ghost/GHOST_ISystemPaths.h b/intern/ghost/GHOST_ISystemPaths.h index ee8bd9d1eda..1ba4ceaaaba 100644 --- a/intern/ghost/GHOST_ISystemPaths.h +++ b/intern/ghost/GHOST_ISystemPaths.h @@ -98,6 +98,11 @@ public: private: /** The one and only system paths*/ static GHOST_ISystemPaths *m_systemPaths; + + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_ISystemPaths") +#endif }; #endif diff --git a/intern/ghost/GHOST_ITimerTask.h b/intern/ghost/GHOST_ITimerTask.h index bb4a81be5f8..08c4890939f 100644 --- a/intern/ghost/GHOST_ITimerTask.h +++ b/intern/ghost/GHOST_ITimerTask.h @@ -84,14 +84,9 @@ public: */ virtual void setUserData(const GHOST_TUserDataPtr userData) = 0; + #ifdef WITH_CXX_GUARDEDALLOC -public: - void *operator new(size_t num_bytes) { - return MEM_mallocN(num_bytes, "GHOST:GHOST_ITimerTask"); - } - void operator delete(void *mem) { - MEM_freeN(mem); - } + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_ITimerTask") #endif }; diff --git a/intern/ghost/GHOST_Types.h b/intern/ghost/GHOST_Types.h index 8454f338645..4921acde670 100644 --- a/intern/ghost/GHOST_Types.h +++ b/intern/ghost/GHOST_Types.h @@ -37,7 +37,11 @@ #include "MEM_guardedalloc.h" #endif -#define GHOST_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name +#if defined(WITH_CXX_GUARDEDALLOC) && defined(__cplusplus) +# define GHOST_DECLARE_HANDLE(name) typedef struct name##__ { int unused; MEM_CXX_CLASS_ALLOC_FUNCS(#name) } *name +#else +# define GHOST_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name +#endif typedef char GHOST_TInt8; typedef unsigned char GHOST_TUns8; diff --git a/intern/ghost/intern/GHOST_CallbackEventConsumer.h b/intern/ghost/intern/GHOST_CallbackEventConsumer.h index 61aef742a48..e13a56c38f4 100644 --- a/intern/ghost/intern/GHOST_CallbackEventConsumer.h +++ b/intern/ghost/intern/GHOST_CallbackEventConsumer.h @@ -73,6 +73,10 @@ protected: GHOST_EventCallbackProcPtr m_eventCallback; /** The data passed back though the call-back routine. */ GHOST_TUserDataPtr m_userData; + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_CallbackEventConsumer") +#endif }; #endif // __GHOST_CALLBACKEVENTCONSUMER_H__ diff --git a/intern/ghost/intern/GHOST_DisplayManager.h b/intern/ghost/intern/GHOST_DisplayManager.h index 3e867054d01..7893f0936b3 100644 --- a/intern/ghost/intern/GHOST_DisplayManager.h +++ b/intern/ghost/intern/GHOST_DisplayManager.h @@ -133,6 +133,11 @@ protected: bool m_settingsInitialized; /** The list with display settings for the main display. */ std::vector m_settings; + + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_DisplayManager") +#endif }; diff --git a/intern/ghost/intern/GHOST_EventManager.h b/intern/ghost/intern/GHOST_EventManager.h index f941dac9fb4..eec00789800 100644 --- a/intern/ghost/intern/GHOST_EventManager.h +++ b/intern/ghost/intern/GHOST_EventManager.h @@ -168,6 +168,11 @@ protected: /** The list with event consumers. */ TConsumerVector m_consumers; + + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_EventManager") +#endif }; #endif // __GHOST_EVENTMANAGER_H__ diff --git a/intern/ghost/intern/GHOST_SystemPaths.h b/intern/ghost/intern/GHOST_SystemPaths.h index 75acbf885e3..f53d4f4a7b7 100644 --- a/intern/ghost/intern/GHOST_SystemPaths.h +++ b/intern/ghost/intern/GHOST_SystemPaths.h @@ -57,7 +57,7 @@ public: */ virtual const GHOST_TUns8 *getSystemDir(int version, const char *versionstr) const = 0; - /** + /** * Determine the base dir in which user configuration is stored, including versioning. * If needed, it will create the base directory. * @return Unsigned char string pointing to user dir (eg ~/.blender/). @@ -74,7 +74,6 @@ public: * Add the file to the operating system most recently used files */ virtual void addToSystemRecentFiles(const char *filename) const = 0; - }; #endif diff --git a/intern/ghost/intern/GHOST_TimerManager.h b/intern/ghost/intern/GHOST_TimerManager.h index 0b189cf3aa9..88d27088c1d 100644 --- a/intern/ghost/intern/GHOST_TimerManager.h +++ b/intern/ghost/intern/GHOST_TimerManager.h @@ -119,6 +119,11 @@ protected: typedef std::vector TTimerVector; /** The list with event consumers. */ TTimerVector m_timers; + + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_TimerManager") +#endif }; #endif // __GHOST_TIMERMANAGER_H__ diff --git a/intern/guardedalloc/cpp/mallocn.cpp b/intern/guardedalloc/cpp/mallocn.cpp index 130fcb6960b..21f5e323cf2 100644 --- a/intern/guardedalloc/cpp/mallocn.cpp +++ b/intern/guardedalloc/cpp/mallocn.cpp @@ -28,20 +28,36 @@ #include #include "../MEM_guardedalloc.h" -void* operator new (size_t size) +/* not default but can be used when needing to set a string */ +void *operator new(size_t size, const char *str) { - return MEM_mallocN(size, "C++/anonymous"); + return MEM_mallocN(size, str); } - -/* not default but can be used when needing to set a string */ -void* operator new (size_t size, const char *str) +void *operator new[](size_t size, const char *str) { return MEM_mallocN(size, str); } -void operator delete (void *p) + +void *operator new(size_t size) +{ + return MEM_mallocN(size, "C++/anonymous"); +} +void *operator new[](size_t size) +{ + return MEM_mallocN(size, "C++/anonymous[]"); +} + + +void operator delete(void *p) +{ + /* delete NULL is valid in c++ */ + if (p) + MEM_freeN(p); +} +void operator delete[](void *p) { /* delete NULL is valid in c++ */ - if(p) + if (p) MEM_freeN(p); } diff --git a/source/blender/compositor/intern/COM_ChunkOrderHotspot.h b/source/blender/compositor/intern/COM_ChunkOrderHotspot.h index ceb5934a7df..68140745f8b 100644 --- a/source/blender/compositor/intern/COM_ChunkOrderHotspot.h +++ b/source/blender/compositor/intern/COM_ChunkOrderHotspot.h @@ -23,6 +23,10 @@ #ifndef _COM_ChunkOrderHotSpot_h_ #define _COM_ChunkOrderHotSpot_h_ +#ifdef WITH_CXX_GUARDEDALLOC +#include "MEM_guardedalloc.h" +#endif + class ChunkOrderHotspot { private: int x; @@ -32,6 +36,11 @@ private: public: ChunkOrderHotspot(int x, int y, float addition); double determineDistance(int x, int y); + + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("COM:ChunkOrderHotspot") +#endif }; #endif diff --git a/source/blender/compositor/intern/COM_ExecutionSystem.cpp b/source/blender/compositor/intern/COM_ExecutionSystem.cpp index 7e09486fd0b..5001cabbd9f 100644 --- a/source/blender/compositor/intern/COM_ExecutionSystem.cpp +++ b/source/blender/compositor/intern/COM_ExecutionSystem.cpp @@ -41,6 +41,10 @@ #include "BKE_global.h" +#ifdef WITH_CXX_GUARDEDALLOC +#include "MEM_guardedalloc.h" +#endif + ExecutionSystem::ExecutionSystem(RenderData *rd, bNodeTree *editingtree, bool rendering) { context.setbNodeTree(editingtree); diff --git a/source/blender/compositor/intern/COM_NodeOperation.h b/source/blender/compositor/intern/COM_NodeOperation.h index f96b994685a..bf2dbd8c9c0 100644 --- a/source/blender/compositor/intern/COM_NodeOperation.h +++ b/source/blender/compositor/intern/COM_NodeOperation.h @@ -270,6 +270,10 @@ protected: * @brief set if this NodeOperation can be scheduled on a OpenCLDevice */ void setOpenCL(bool openCL) { this->openCL = openCL; } + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("COM:NodeOperation") +#endif }; #endif diff --git a/source/blender/compositor/intern/COM_Socket.h b/source/blender/compositor/intern/COM_Socket.h index 4f82a490e14..9feef1bae93 100644 --- a/source/blender/compositor/intern/COM_Socket.h +++ b/source/blender/compositor/intern/COM_Socket.h @@ -29,6 +29,10 @@ #include "DNA_node_types.h" #include "COM_defines.h" +#ifdef WITH_CXX_GUARDEDALLOC +#include "MEM_guardedalloc.h" +#endif + using namespace std; class SocketConnection; class NodeBase; diff --git a/source/blender/compositor/intern/COM_SocketReader.h b/source/blender/compositor/intern/COM_SocketReader.h index f4f58e6e007..df8899e8eea 100644 --- a/source/blender/compositor/intern/COM_SocketReader.h +++ b/source/blender/compositor/intern/COM_SocketReader.h @@ -25,6 +25,10 @@ #include "BLI_rect.h" #include "COM_defines.h" +#ifdef WITH_CXX_GUARDEDALLOC +#include "MEM_guardedalloc.h" +#endif + typedef enum PixelSampler { COM_PS_NEAREST, COM_PS_BILINEAR, diff --git a/source/blender/compositor/intern/COM_compositor.cpp b/source/blender/compositor/intern/COM_compositor.cpp index bec9ff95eed..ccd3b2e6458 100644 --- a/source/blender/compositor/intern/COM_compositor.cpp +++ b/source/blender/compositor/intern/COM_compositor.cpp @@ -33,20 +33,21 @@ extern "C" { #include "COM_WorkScheduler.h" #include "OCL_opencl.h" -static ThreadMutex *compositorMutex; +static ThreadMutex compositorMutex = {{0}}; +static char is_compositorMutex_init = FALSE; void COM_execute(RenderData *rd, bNodeTree *editingtree, int rendering) { - if (compositorMutex == NULL) { /// TODO: move to blender startup phase - compositorMutex = new ThreadMutex(); - BLI_mutex_init(compositorMutex); + if (is_compositorMutex_init == FALSE) { /// TODO: move to blender startup phase + BLI_mutex_init(&compositorMutex); OCL_init(); WorkScheduler::initialize(); ///TODO: call workscheduler.deinitialize somewhere + is_compositorMutex_init = TRUE; } - BLI_mutex_lock(compositorMutex); + BLI_mutex_lock(&compositorMutex); if (editingtree->test_break(editingtree->tbh)) { // during editing multiple calls to this method can be triggered. // make sure one the last one will be doing the work. - BLI_mutex_unlock(compositorMutex); + BLI_mutex_unlock(&compositorMutex); return; } @@ -60,5 +61,5 @@ void COM_execute(RenderData *rd, bNodeTree *editingtree, int rendering) system->execute(); delete system; - BLI_mutex_unlock(compositorMutex); + BLI_mutex_unlock(&compositorMutex); } diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h index af8acaa993f..2fee6aaab92 100644 --- a/source/gameengine/Expressions/PyObjectPlus.h +++ b/source/gameengine/Expressions/PyObjectPlus.h @@ -166,7 +166,7 @@ public: \ void *operator new(size_t num_bytes) { \ return MEM_mallocN(num_bytes, Type.tp_name); \ } \ - void operator delete( void *mem ) { \ + void operator delete(void *mem) { \ MEM_freeN(mem); \ } \ -- cgit v1.2.3