diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 9050d4896a..1b75f04531 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -614,15 +614,9 @@ cocos_source_files(MODULE ccunzip cocos_source_files( cocos/base/memory/AllocatedObj.cpp cocos/base/memory/AllocatedObj.h - cocos/base/memory/JeAlloc.cpp - cocos/base/memory/JeAlloc.h cocos/base/memory/MemDef.h cocos/base/memory/Memory.cpp cocos/base/memory/Memory.h - cocos/base/memory/MemTracker.cpp - cocos/base/memory/MemTracker.h - cocos/base/memory/NedPooling.cpp - cocos/base/memory/NedPooling.h cocos/base/memory/StdAlloc.h cocos/base/memory/StlAlloc.h cocos/base/memory/MemoryHook.cpp diff --git a/native/cocos/audio/apple/AudioCache.mm b/native/cocos/audio/apple/AudioCache.mm index c33eab4f46..d2fca79502 100644 --- a/native/cocos/audio/apple/AudioCache.mm +++ b/native/cocos/audio/apple/AudioCache.mm @@ -33,6 +33,7 @@ #include #include "application/ApplicationManager.h" #include "base/Scheduler.h" +#include "base/memory/Memory.h" #include "audio/apple/AudioDecoder.h" diff --git a/native/cocos/base/CachedArray.h b/native/cocos/base/CachedArray.h index b9cc65f924..67deb51189 100644 --- a/native/cocos/base/CachedArray.h +++ b/native/cocos/base/CachedArray.h @@ -29,6 +29,7 @@ #include #include "base/Object.h" #include "base/TypeDef.h" +#include "base/memory/Memory.h" namespace cc { diff --git a/native/cocos/base/Macros.h b/native/cocos/base/Macros.h index 87e214f366..74b5d1adf8 100644 --- a/native/cocos/base/Macros.h +++ b/native/cocos/base/Macros.h @@ -133,44 +133,6 @@ It should work same as apples CFSwapInt32LittleToHost(..) // Generic macros -#define CC_SAFE_DELETE(p) \ - do { \ - delete (p); \ - (p) = nullptr; \ - } while (0) -#define CC_SAFE_DELETE_ARRAY(p) \ - do { \ - if (p) { \ - delete[](p); \ - (p) = nullptr; \ - } \ - } while (0) -#define CC_SAFE_FREE(p) \ - do { \ - if (p) { \ - free(p); \ - (p) = nullptr; \ - } \ - } while (0) -#define CC_SAFE_RELEASE(p) \ - do { \ - if (p) { \ - (p)->release(); \ - } \ - } while (0) -#define CC_SAFE_RELEASE_NULL(p) \ - do { \ - if (p) { \ - (p)->release(); \ - (p) = nullptr; \ - } \ - } while (0) -#define CC_SAFE_ADD_REF(p) \ - do { \ - if (p) { \ - (p)->addRef(); \ - } \ - } while (0) #define CC_BREAK_IF(cond) \ if (cond) break @@ -246,15 +208,6 @@ It should work same as apples CFSwapInt32LittleToHost(..) #define CC_MODE_DEBUG 1 #define CC_MODE_RELEASE 2 -// Memory Allocator -#define CC_MEMORY_ALLOCATOR_STD 0 -#define CC_MEMORY_ALLOCATOR_NEDPOOLING 1 -#define CC_MEMORY_ALLOCATOR_JEMALLOC 2 - -// STL Memory Allocator -#define CC_STL_MEMORY_ALLOCATOR_STANDARD 1 -#define CC_STL_MEMORY_ALLOCATOR_CUSTOM 2 - // Compiler type and version recognition #if defined(_MSC_VER) #define CC_COMPILER CC_COMPILER_MSVC @@ -350,33 +303,13 @@ It should work same as apples CFSwapInt32LittleToHost(..) #define CC_DISALLOW_ASSIGN(TypeName) \ TypeName &operator=(const TypeName &) = delete; \ - TypeName &operator=(TypeName &&) = delete + TypeName &operator=(TypeName &&) = delete; #define CC_DISALLOW_COPY_MOVE_ASSIGN(TypeName) \ TypeName(const TypeName &) = delete; \ TypeName(TypeName &&) = delete; \ CC_DISALLOW_ASSIGN(TypeName) -#define CC_ENABLE_COPY_SEMANTICS(cls) \ - cls(const cls &) = default; \ - cls &operator=(const cls &) = default; - -#define CC_DISABLE_COPY_SEMANTICS(cls) \ - cls(const cls &) = delete; \ - cls &operator=(const cls &) = delete; - -#define CC_ENABLE_MOVE_SEMANTICS(cls) \ - cls(cls &&) noexcept = default; \ - cls &operator=(cls &&) noexcept = default; - -#define CC_DISABLE_MOVE_SEMANTICS(cls) \ - cls(cls &&) noexcept = delete; \ - cls &operator=(cls &&) noexcept = delete; - -#define CC_DISABLE_COPY_AND_MOVE_SEMANTICS(cls) \ - CC_DISABLE_COPY_SEMANTICS(cls) \ - CC_DISABLE_MOVE_SEMANTICS(cls) - #if (CC_COMPILER == CC_COMPILER_MSVC) #define CC_ALIGN(N) __declspec(align(N)) #define CC_CACHE_ALIGN __declspec(align(CC_CACHELINE_SIZE)) @@ -515,45 +448,6 @@ It should work same as apples CFSwapInt32LittleToHost(..) #define CC_MODE CC_MODE_RELEASE #endif -// Engine Memory Management -// #if (CC_PLATFORM == CC_PLATFORM_WINDOWS) -// #if (CC_MODE == CC_MODE_DEBUG) -// #define CC_MEMORY_TRACKER -// #endif -// #elif (CC_PLATFORM == CC_PLATFORM_ANDROID) -// //# define CC_MEMORY_TRACKER -// #else -// #endif - -// use simd -//#define CC_USE_SIMD - -// Memory Allocator -// #if (CC_PLATFORM == CC_PLATFORM_WINDOWS) -// #define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_STD -// #undef CC_MEMORY_TRACKER -// //#if defined(CC_MEMORY_TRACKER) -// //# define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_JEMALLOC -// //#else -// //# define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_STD -// //#endif -// #elif (CC_PLATFORM == CC_PLATFORM_ANDROID) -// #if defined(CC_MEMORY_TRACKER) -// #define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_JEMALLOC -// #else -// #define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_STD -// #endif -// #else -// #define CC_MEMORY_ALLOCATOR CC_MEMORY_ALLOCATOR_STD -// #endif - -// // STL memory allocator -// #if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_STD) -// #define CC_STL_MEMORY_ALLOCATOR CC_STL_MEMORY_ALLOCATOR_STANDARD -// #else -// #define CC_STL_MEMORY_ALLOCATOR CC_STL_MEMORY_ALLOCATOR_CUSTOM -// #endif - #define CC_TOSTR(s) #s #if defined(__GNUC__) && __GNUC__ >= 4 diff --git a/native/cocos/base/Object.h b/native/cocos/base/Object.h index a6dcdf2153..0589e4fc52 100755 --- a/native/cocos/base/Object.h +++ b/native/cocos/base/Object.h @@ -25,11 +25,11 @@ #pragma once -#include "memory/Memory.h" +#include "base/memory/AllocatedObj.h" namespace cc { // Now define all the base classes for each allocation -using Object = AllocatedObject; +using Object = AllocatedObject; } // namespace cc diff --git a/native/cocos/base/Value.cpp b/native/cocos/base/Value.cpp index 59e2fb79ed..6b84f794a8 100644 --- a/native/cocos/base/Value.cpp +++ b/native/cocos/base/Value.cpp @@ -35,6 +35,7 @@ #include #include "base/Utils.h" +#include "base/memory/Memory.h" namespace cc { diff --git a/native/cocos/base/Vector.h b/native/cocos/base/Vector.h index 1632dec733..443ad91d1a 100644 --- a/native/cocos/base/Vector.h +++ b/native/cocos/base/Vector.h @@ -35,7 +35,7 @@ #include #include "base/Log.h" -#include "base/Macros.h" +#include "base/memory/Memory.h" #include "base/Random.h" #include "base/RefCounted.h" #include "base/TypeDef.h" diff --git a/native/cocos/base/ZipUtils.cpp b/native/cocos/base/ZipUtils.cpp index b852ffc3fc..9877252647 100644 --- a/native/cocos/base/ZipUtils.cpp +++ b/native/cocos/base/ZipUtils.cpp @@ -48,6 +48,7 @@ #include "base/Data.h" #include "base/Locked.h" #include "platform/FileUtils.h" +#include "base/memory/Memory.h" // minizip 1.2.0 is same with other platforms #ifndef unzGoToFirstFile64 diff --git a/native/cocos/base/memory/AllocatedObj.cpp b/native/cocos/base/memory/AllocatedObj.cpp index db046160b5..392fdc6791 100755 --- a/native/cocos/base/memory/AllocatedObj.cpp +++ b/native/cocos/base/memory/AllocatedObj.cpp @@ -29,12 +29,4 @@ THE SOFTWARE. namespace cc { -/* - The problem is that operator new/delete are *implicitly* static. We have to - instantiate them for each combination exactly once throughout all the compilation - units that are linked together, and this appears to be the only way to do it. - */ - -template class AllocatedObject; - } // namespace cc diff --git a/native/cocos/base/memory/AllocatedObj.h b/native/cocos/base/memory/AllocatedObj.h index dce0b0c53a..ac0346e2ce 100755 --- a/native/cocos/base/memory/AllocatedObj.h +++ b/native/cocos/base/memory/AllocatedObj.h @@ -30,75 +30,12 @@ THE SOFTWARE. #include "../Macros.h" #include "base/RefCounted.h" -// Anything that has done a #define new will screw operator new definitions up -// so undefine -#ifdef new - #undef new -#endif -#ifdef delete - #undef delete -#endif - namespace cc { -/** Superclass for all objects that wish to use custom memory allocators - when their new / delete operators are called. - Requires a template parameter identifying the memory allocator policy - to use (e.g. see StdAllocPolicy). - */ -template class CC_DLL AllocatedObject : public RefCounted { public: explicit AllocatedObject() = default; ~AllocatedObject() override = default; - - // operator new, with debug line info - void *operator new(size_t sz, const char *file, int line, const char *func) { - return Alloc::AllocateBytes(sz, file, line, func); - } - - /// array operator new, with debug line info - void *operator new[](size_t sz, const char *file, int line, const char *func) { - return Alloc::AllocateBytes(sz, file, line, func); - } - - /// placement operator new - void *operator new(size_t sz, void *ptr) { - (void)sz; - return ptr; - } - -#if 1 - void *operator new(size_t sz) { - return Alloc::AllocateBytes(sz); - } - - void *operator new[](size_t sz) { - return Alloc::AllocateBytes(sz); - } -#endif - - void operator delete(void *ptr) { - Alloc::DeallocateBytes(ptr); - } - - // Corresponding operator for placement delete (second param same as the first) - void operator delete(void *ptr, void * /*unused*/) { - Alloc::DeallocateBytes(ptr); - } - - // only called if there is an exception in corresponding 'new' - void operator delete(void *ptr, const char * /*unused*/, int /*unused*/, const char * /*unused*/) { - Alloc::DeallocateBytes(ptr); - } - - void operator delete[](void *ptr) { - Alloc::DeallocateBytes(ptr); - } - - void operator delete[](void *ptr, const char * /*unused*/, int /*unused*/, const char * /*unused*/) { - Alloc::DeallocateBytes(ptr); - } }; } // namespace cc diff --git a/native/cocos/base/memory/JeAlloc.cpp b/native/cocos/base/memory/JeAlloc.cpp deleted file mode 100755 index 21de9b4e59..0000000000 --- a/native/cocos/base/memory/JeAlloc.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/**************************************************************************** -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ - -Copyright (c) 2000-2014 Torus Knot Software Ltd - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "base/CoreStd.h" -#include "JeAlloc.h" -#include "MemTracker.h" - -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_JEMALLOC) - - #include "jemalloc/jemalloc.h" -extern const char *je_malloc_conf = "narenas:4"; - -namespace cc { - - #ifdef CC_MEMORY_TRACKER - #define MEM_CHECKTAG_SIZE 4 - #define MEM_CHECKTAG 0x20170719 - -inline static void CheckOverflowAlloc(void *ptr) { - size_t size = je_malloc_usable_size(ptr); - char *p = (char *)ptr + size - MEM_CHECKTAG_SIZE; - uint tag = MEM_CHECKTAG; - memcpy(p, &tag, MEM_CHECKTAG_SIZE); -} - -inline static void CheckOverflowFree(void *ptr) { - size_t size = je_malloc_usable_size(ptr); - char *p = (char *)ptr + size - MEM_CHECKTAG_SIZE; - uint tag; - memcpy(&tag, p, MEM_CHECKTAG_SIZE); - CCASSERT(tag == MEM_CHECKTAG); -} - - #else - #define MEM_CHECKTAG_SIZE 0 - #endif - -CC_DECL_MALLOC void *JeAllocImpl::AllocBytes(size_t count, const char *file, int line, const char *func) { - void *ptr = je_malloc(count + MEM_CHECKTAG_SIZE); - #ifdef CC_MEMORY_TRACKER - CheckOverflowAlloc(ptr); - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #else - // avoid unused params warning - (void)file; - (void)line; - (void)func; - #endif - return ptr; -} - -CC_DECL_MALLOC void *JeAllocImpl::ReallocBytes(void *ptr, size_t count, const char *file, int line, const char *func) { - #ifdef CC_MEMORY_TRACKER - if (ptr) { - if (count == 0) { - CheckOverflowFree(ptr); - MemTracker::Instance()->RecordFree(ptr); - je_free(ptr); - return nullptr; - } else { - size_t oldsz = je_malloc_usable_size(ptr) - MEM_CHECKTAG_SIZE; - if (oldsz > count) oldsz = count; - - void *nptr = je_malloc(count + MEM_CHECKTAG_SIZE); - memcpy(nptr, ptr, oldsz); - CheckOverflowAlloc(nptr); - MemTracker::Instance()->RecordAlloc(nptr, count, file, line, func); - - CheckOverflowFree(ptr); - MemTracker::Instance()->RecordFree(ptr); - je_free(ptr); - return nptr; - } - } else { - if (count) { - ptr = je_malloc(count + MEM_CHECKTAG_SIZE); - CheckOverflowAlloc(ptr); - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - return ptr; - } else { - return nullptr; - } - } - #else - // avoid unused params warning - (void)file; - (void)line; - (void)func; - return je_realloc(ptr, count); - #endif -} - -CC_DECL_MALLOC void *JeAllocImpl::AllocBytesAligned(size_t align, size_t count, const char *file, int line, const char *func) { - // default to platform SIMD alignment if none specified - void *ptr = je_aligned_alloc(align, count + MEM_CHECKTAG_SIZE); - #ifdef COCOS_MEMORY_TRACKER - CheckOverflowAlloc(ptr); - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #else - // avoid unused params warning - (void)file; - (void)line; - (void)func; - #endif - return ptr; -} - -void JeAllocImpl::DeallocBytes(void *ptr) { - // deal with null - if (!ptr) - return; - #ifdef CC_MEMORY_TRACKER - CheckOverflowFree(ptr); - MemTracker::Instance()->RecordFree(ptr); - #endif - je_free(ptr); -} - -struct JeDumpData { - char *buf; - int size; -}; - -static void JeStatsPrint(void *param, const char *msg) { - JeDumpData *jd = (JeDumpData *)param; - if (jd->size == 0) return; - - int len = (int)strlen(msg); - if (len > jd->size) len = jd->size; - memcpy(jd->buf, msg, len); - jd->buf[len] = 0; - jd->buf += len; - jd->size -= len; -} - -void JeAllocImpl::DumpStats(char *buf, int bufsize) { - JeDumpData jd; - jd.buf = buf; - jd.size = bufsize - 1; - je_malloc_stats_print(JeStatsPrint, &jd, "ma"); -} - -void JeAllocImpl::TrimAlloc() { - int narenas = 0; - size_t sz = sizeof(narenas); - je_mallctl("arenas.narenas", (void *)&narenas, &sz, nullptr, 0); - char buf[24]; - sprintf(buf, "arena.%d.purge", narenas); - printf("jemalloc trim: %s\n", buf); - je_mallctl(buf, NULL, NULL, NULL, 0); -} - -} // namespace cc - -#endif // end - #ifdef CC_MEMORY_ALLOCATOR_JEMALLOC diff --git a/native/cocos/base/memory/JeAlloc.h b/native/cocos/base/memory/JeAlloc.h deleted file mode 100755 index 2ee071e139..0000000000 --- a/native/cocos/base/memory/JeAlloc.h +++ /dev/null @@ -1,86 +0,0 @@ -/**************************************************************************** -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ - -Copyright (c) 2000-2014 Torus Knot Software Ltd - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#ifndef CC_JE_ALLOC_H_ -#define CC_JE_ALLOC_H_ - -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_JEMALLOC) - -namespace cc { - -//Non-templated utility class just to hide jemalloc. -class CC_CORE_API JeAllocImpl { -public: - static CC_DECL_MALLOC void *AllocBytes(size_t count, const char *file, int line, const char *func); - static CC_DECL_MALLOC void *ReallocBytes(void *ptr, size_t count, const char *file, int line, const char *func); - static CC_DECL_MALLOC void *AllocBytesAligned(size_t align, size_t count, const char *file, int line, const char *func); - static void DeallocBytes(void *ptr); - static void DumpStats(char *buf, int bufsize); - static void TrimAlloc(); -}; - -/** An allocation policy for use with AllocatedObject and - STLAllocator. This is the class that actually does the allocation - and deallocation of physical memory, and is what you will want to - provide a custom version of if you wish to change how memory is allocated. - */ -class CC_CORE_API JeAllocPolicy { -public: - static inline CC_DECL_MALLOC void *AllocateBytes(size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return JeAllocImpl::AllocBytes(count, file, line, func); - } - - static inline CC_DECL_MALLOC void *ReallocateBytes(void *ptr, size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return JeAllocImpl::ReallocBytes(ptr, count, file, line, func); - } - - static inline void DeallocateBytes(void *ptr) { - JeAllocImpl::DeallocBytes(ptr); - } - - static inline CC_DECL_MALLOC void *AllocateBytesAligned(size_t alignment, size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return JeAllocImpl::AllocBytesAligned(alignment, count, file, line, func); - } - - static inline void DeallocateBytesAligned(void *ptr) { - JeAllocImpl::DeallocBytes(ptr); - } - - // Get the maximum size of a single allocation - static inline size_t GetMaxAllocationSize() { - return (std::numeric_limits::max)(); - } - -private: - // No instantiation - JeAllocPolicy() {} -}; - -} // namespace cc - -#endif // end - #ifdef COCOS_MEMORY_ALLOCATOR_JEMALLOC - -#endif // CC_JE_ALLOC_H_ diff --git a/native/cocos/base/memory/MemDef.h b/native/cocos/base/memory/MemDef.h index 1d218b3e24..d558e5f5fc 100755 --- a/native/cocos/base/memory/MemDef.h +++ b/native/cocos/base/memory/MemDef.h @@ -24,221 +24,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#ifndef CC_MEM_DEF_H_ -#define CC_MEM_DEF_H_ +#pragma once -#include "AllocatedObj.h" -#include "JeAlloc.h" -#include "NedPooling.h" #include "StdAlloc.h" -namespace cc { +// aligned allocation +/// Allocate a block of raw memory aligned to user defined boundaries. +#define _CC_MALLOC_ALIGN(bytes, align) ::cc::StdAllocPolicy::AllocateBytesAligned(align, bytes) +/// Free the memory allocated with either _CC_MALLOC_ALIGN or _CC_ALLOC_T_ALIGN. +#define _CC_FREE_ALIGN(ptr) ::cc::StdAllocPolicy::DeallocateBytesAligned((void *)ptr) -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_STD) +/// Allocate space for one primitive type, external type or non-virtual type aligned to user defined boundaries +#define _CC_NEW_ALIGN(T, align) new (::cc::StdAllocPolicy::AllocateBytesAligned(align, sizeof(T))) T +#define _CC_NEW_ALIGN_ARGS(T, align, ...) new (::cc::StdAllocPolicy::AllocateBytesAligned(align, sizeof(T))) T(__VA_ARGS__) -// configure default allocators based on the options above -// notice how we're not using the memory categories here but still roughing them out -// in your allocators you might choose to create different policies per category - -// configurable category, for general malloc -// notice how we ignore the category here, you could specialize -class CategorisedAllocPolicy : public StdAllocPolicy {}; - -#elif (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_NEDPOOLING) - -// configure default allocators based on the options above -// notice how we're not using the memory categories here but still roughing them out -// in your allocators you might choose to create different policies per category - -// configurable category, for general malloc -// notice how we ignore the category here, you could specialize -class CategorisedAllocPolicy : public NedPoolingAllocPolicy {}; - -#elif (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_JEMALLOC) - -class CategorisedAllocPolicy : public JeAllocPolicy {}; - -#endif - -// Useful shortcuts -typedef CategorisedAllocPolicy GAP; -typedef CategorisedAllocPolicy STLAP; - -// Containers (by-value only) -// Will be of the form: -// typedef STLAllocator TAlloc; -// for use in vector::type - -////////////////////////////////////////////////////////////////////////// - -/** Utility function for constructing an array of objects with placement new, - without using new[] (which allocates an undocumented amount of extra memory - and so isn't appropriate for custom allocators). -*/ -template -T *ConstructN(T *basePtr, size_t count) { - for (size_t i = 0; i < count; ++i) { - new ((void *)(basePtr + i)) T(); +/// Free the memory allocated with _CC_NEW_ALIGN. +#define _CC_DELETE_ALIGN(ptr, T, align) \ + if (ptr) { \ + (ptr)->~T(); \ + ::cc::StdAllocPolicy::DeallocateBytesAligned((void *)ptr); \ + } +/// Free the memory allocated with _CC_NEW_ARRAY_ALIGN. +#define _CC_DELETE_ARRAY_ALIGN(ptr, T, count, align) \ + if (ptr) { \ + for (size_t _b = 0; _b < (size_t)count; ++_b) { \ + (ptr)[_b].~T(); \ + } \ + ::cc::StdAllocPolicy::DeallocateBytesAligned((void *)ptr); \ } - return basePtr; -} - -} // namespace cc - -#ifdef CC_MEMORY_TRACKER - - /// Allocate a block of raw memory. - #define _CC_MALLOC(bytes) ::cc::CategorisedAllocPolicy::AllocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__) - /// Reallocate a block of raw memory. - #define _CC_REALLOC(ptr, bytes) ::cc::CategorisedAllocPolicy::ReallocateBytes(ptr, bytes, __FILE__, __LINE__, __FUNCTION__) - /// Allocate a block of memory for a primitive type. - #define _CC_ALLOC_T(T, count) static_cast(::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T) * (count), __FILE__, __LINE__, __FUNCTION__)) - /// Free the memory allocated with _CC_MALLOC or _CC_ALLOC_T. - #define _CC_FREE(ptr) ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr) - - /// Allocate space for one primitive type, external type or non-virtual type with constructor parameters - #define _CC_NEW_T(T) new (::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T - /// Allocate a block of memory for 'count' primitive types - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T(T, count) ::cc::ConstructN(static_cast(::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T) * (count), __FILE__, __LINE__, __FUNCTION__)), count) - /// Free the memory allocated with _CC_NEW_T. - #define _CC_DELETE_T(ptr, T) \ - if (ptr) { \ - (ptr)->~T(); \ - ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr); \ - } - /// Free the memory allocated with _CC_NEW_ARRAY_T. - #define _CC_DELETE_ARRAY_T(ptr, T, count) \ - if (ptr) { \ - for (size_t b = 0; b < (size_t)count; ++b) { \ - (ptr)[b].~T(); \ - } \ - ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr); \ - } - - // aligned allocation - /// Allocate a block of raw memory aligned to user defined boundaries. - #define _CC_MALLOC_ALIGN(bytes, align) ::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, bytes, __FILE__, __LINE__, __FUNCTION__) - /// Allocate a block of memory for a primitive type aligned to user defined boundaries. - #define _CC_ALLOC_T_ALIGN(T, count, align) static_cast(::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T) * (count), __FILE__, __LINE__, __FUNCTION__)) - /// Free the memory allocated with either _CC_MALLOC_ALIGN or _CC_ALLOC_T_ALIGN. - #define _CC_FREE_ALIGN(ptr, align) ::cc::CategorisedAllocPolicy::DeallocateBytesAligned(ptr) - /// Allocate a block of raw memory aligned to SIMD boundaries. - #define _CC_MALLOC_SIMD(bytes) _CC_MALLOC_ALIGN(bytes, CC_SIMD_ALIGNMENT) - /// Allocate a block of memory for a primitive type aligned to SIMD boundaries. - #define _CC_ALLOC_T_SIMD(T, count) _CC_ALLOC_T_ALIGN(T, count, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with either _CC_MALLOC_SIMD or _CC_ALLOC_T_SIMD. - #define _CC_FREE_SIMD(ptr) _CC_FREE_ALIGN(ptr, CC_SIMD_ALIGNMENT) - - /// Allocate space for one primitive type, external type or non-virtual type aligned to user defined boundaries - #define _CC_NEW_T_ALIGN(T, align) new (::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T - /// Allocate a block of memory for 'count' primitive types aligned to user defined boundaries - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T_ALIGN(T, count, align) ::cc::ConstructN(static_cast(::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T) * (count), __FILE__, __LINE__, __FUNCTION__)), count) - /// Free the memory allocated with _CC_NEW_T_ALIGN. - #define _CC_DELETE_T_ALIGN(ptr, T, align) \ - if (ptr) { \ - (ptr)->~T(); \ - ::cc::CategorisedAllocPolicy::DeallocateBytesAligned(ptr); \ - } - /// Free the memory allocated with _CC_NEW_ARRAY_T_ALIGN. - #define _CC_DELETE_ARRAY_T_ALIGN(ptr, T, count, align) \ - if (ptr) { \ - for (size_t _b = 0; _b < (size_t)count; ++_b) { \ - (ptr)[_b].~T(); \ - } \ - ::cc::CategorisedAllocPolicy::DeallocateBytesAligned(ptr); \ - } - /// Allocate space for one primitive type, external type or non-virtual type aligned to SIMD boundaries - #define _CC_NEW_T_SIMD(T) _CC_NEW_T_ALIGN(T, CC_SIMD_ALIGNMENT) - /// Allocate a block of memory for 'count' primitive types aligned to SIMD boundaries - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T_SIMD(T, count) _CC_NEW_ARRAY_T_ALIGN(T, count, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with _CC_NEW_T_SIMD. - #define _CC_DELETE_T_SIMD(ptr, T) _CC_DELETE_T_ALIGN(ptr, T, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with _CC_NEW_ARRAY_T_SIMD. - #define _CC_DELETE_ARRAY_T_SIMD(ptr, T, count) _CC_DELETE_ARRAY_T_ALIGN(ptr, T, CC_SIMD_ALIGNMENT) - - // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy) - // Also hooks up the file/line/function params - // Can only be used with classes that derive from AllocatedObject since customized new/delete needed - #define _CC_NEW new (__FILE__, __LINE__, __FUNCTION__) - #define _CC_DELETE delete - -#else - - /// Allocate a block of raw memory. - #define _CC_MALLOC(bytes) ::cc::CategorisedAllocPolicy::AllocateBytes(bytes) - /// Reallocate a block of raw memory. - #define _CC_REALLOC(ptr, bytes) ::cc::CategorisedAllocPolicy::ReallocateBytes(ptr, bytes) - /// Allocate a block of memory for a primitive type. - #define _CC_ALLOC_T(T, count) static_cast(::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T) * (count))) - /// Free the memory allocated with _CC_MALLOC or _CC_ALLOC_T. Category is required to be restated to ensure the matching policy is used - #define _CC_FREE(ptr) ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr) - - /// Allocate space for one primitive type, external type or non-virtual type with constructor parameters - #define _CC_NEW_T(T) new (::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T))) T - /// Allocate a block of memory for 'count' primitive types - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T(T, count) ::cc::ConstructN(static_cast(::cc::CategorisedAllocPolicy::AllocateBytes(sizeof(T) * (count))), count) - /// Free the memory allocated with _CC_NEW_T. - #define _CC_DELETE_T(ptr, T) \ - if (ptr) { \ - (ptr)->~T(); \ - ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr); \ - } - /// Free the memory allocated with _CC_NEW_ARRAY_T. - #define _CC_DELETE_ARRAY_T(ptr, T, count) \ - if (ptr) { \ - for (size_t b = 0; b < (size_t)count; ++b) { \ - (ptr)[b].~T(); \ - } \ - ::cc::CategorisedAllocPolicy::DeallocateBytes((void *)ptr); \ - } - - // aligned allocation - /// Allocate a block of raw memory aligned to user defined boundaries. - #define _CC_MALLOC_ALIGN(bytes, align) ::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, bytes) - /// Allocate a block of memory for a primitive type aligned to user defined boundaries. - #define _CC_ALLOC_T_ALIGN(T, count, align) static_cast(::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T) * (count))) - /// Free the memory allocated with either _CC_MALLOC_ALIGN or _CC_ALLOC_T_ALIGN. - #define _CC_FREE_ALIGN(ptr) ::cc::CategorisedAllocPolicy::DeallocateBytesAligned((void *)ptr) - /// Allocate a block of raw memory aligned to SIMD boundaries. - #define _CC_MALLOC_SIMD(bytes) _CC_MALLOC_ALIGN(bytes, CC_SIMD_ALIGNMENT) - /// Allocate a block of memory for a primitive type aligned to SIMD boundaries. - #define _CC_ALLOC_T_SIMD(T, count) _CC_ALLOC_T_ALIGN(T, count, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with either _CC_MALLOC_SIMD or _CC_ALLOC_T_SIMD. - #define _CC_FREE_SIMD(ptr) _CC_FREE_ALIGN(ptr, CC_SIMD_ALIGNMENT) - - /// Allocate space for one primitive type, external type or non-virtual type aligned to user defined boundaries - #define _CC_NEW_T_ALIGN(T, align) new (::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T))) T - #define _CC_NEW_T_ALIGN_ARGS(T, align, ...) new (::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T))) T(__VA_ARGS__) - - /// Allocate a block of memory for 'count' primitive types aligned to user defined boundaries - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T_ALIGN(T, count, align) ::cc::ConstructN(static_cast(::cc::CategorisedAllocPolicy::AllocateBytesAligned(align, sizeof(T) * (count))), count) - /// Free the memory allocated with _CC_NEW_T_ALIGN. - #define _CC_DELETE_T_ALIGN(ptr, T, align) \ - if (ptr) { \ - (ptr)->~T(); \ - ::cc::CategorisedAllocPolicy::DeallocateBytesAligned((void *)ptr); \ - } - /// Free the memory allocated with _CC_NEW_ARRAY_T_ALIGN. - #define _CC_DELETE_ARRAY_T_ALIGN(ptr, T, count, align) \ - if (ptr) { \ - for (size_t _b = 0; _b < (size_t)count; ++_b) { \ - (ptr)[_b].~T(); \ - } \ - ::cc::CategorisedAllocPolicy::DeallocateBytesAligned((void *)ptr); \ - } - /// Allocate space for one primitive type, external type or non-virtual type aligned to SIMD boundaries - #define _CC_NEW_T_SIMD(T) _CC_NEW_T_ALIGN(T, CC_SIMD_ALIGNMENT) - /// Allocate a block of memory for 'count' primitive types aligned to SIMD boundaries - do not use for classes that inherit from AllocatedObject - #define _CC_NEW_ARRAY_T_SIMD(T, count) _CC_NEW_ARRAY_T_ALIGN(T, count, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with _CC_NEW_T_SIMD. - #define _CC_DELETE_T_SIMD(ptr, T) _CC_DELETE_T_ALIGN(ptr, T, CC_SIMD_ALIGNMENT) - /// Free the memory allocated with _CC_NEW_ARRAY_T_SIMD. - #define _CC_DELETE_ARRAY_T_SIMD(ptr, T, count) _CC_DELETE_ARRAY_T_ALIGN(ptr, T, count, CC_SIMD_ALIGNMENT) - - // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy) - #define _CC_NEW new - #define _CC_DELETE delete - -#endif - -#endif // CC_MEM_DEF_H_ diff --git a/native/cocos/base/memory/MemTracker.cpp b/native/cocos/base/memory/MemTracker.cpp deleted file mode 100755 index 032ec2b626..0000000000 --- a/native/cocos/base/memory/MemTracker.cpp +++ /dev/null @@ -1,286 +0,0 @@ -/**************************************************************************** - This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ -Copyright (c) 2000-2014 Torus Knot Software Ltd -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "base/CoreStd.h" -#include "MemTracker.h" -#include "../StringUtil.h" - -#if (CC_PLATFORM == CC_PLATFORM_ANDROID) - #include -#endif - -#ifdef CC_MEMORY_TRACKER - -extern "C" { - #include -} - - #if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #if !defined(NOMINMAX) && defined(_MSC_VER) - #define NOMINMAX // required to stop windows.h messing up std::min - #endif - #include - - #define CC_OUTPUT_STR(str) ::OutputDebugStringA(str) - #else - #define CC_OUTPUT_STR(str) fputs(str, stdout) - #endif - - #define LEAK_FILENAME "MemoryLeaks.log" - -namespace cc { - -struct AllocHashNode { - tommy_hashdyn_node node; - void *key; - unsigned int bytes; - const char *filename; - const char *function; - unsigned int line; -}; - -struct AllocListParam { - char *buf; - char *last; -}; - -struct AllocDumpParam { - std::map allocMap; -}; - -inline static void FreeHashNode(AllocHashNode *n) { - free(n); -} - -static int HashNodeCompare(const void *arg, const void *obj) { - return ((AllocHashNode *)obj)->key != (void *)arg; -} - -static void HashNodeListAndFree(void *arg, void *obj) { - AllocHashNode *node = (AllocHashNode *)obj; - AllocListParam *param = (AllocListParam *)arg; - int len = StringUtil::printf(param->buf, param->last, "%s(%u): {%u bytes} functions: %s\n", (node->filename ? node->filename : "(unknown source)"), - node->line, node->bytes, (node->function ? node->function : "")); - param->buf += len; - FreeHashNode(node); -} - -static void HashNodeListAndDump(void *arg, void *obj) { - AllocHashNode *node = (AllocHashNode *)obj; - AllocDumpParam *param = (AllocDumpParam *)arg; - const char *key = node->filename ? node->filename : "null"; - std::map::iterator ptr = param->allocMap.find(key); - if (ptr == param->allocMap.end()) { - param->allocMap[key] = node->bytes; - } else { - ptr->second += node->bytes; - } - //char tmpbuf[1024]; - //sprintf(tmpbuf, "%s|%s|%u\n", node->filename, node->function, node->bytes); - //fputs(tmpbuf, param); -} - -///////////////////////////////////////////////////////// - -MemTracker::MemTracker() -: total_memory_allocated_(0) { - allocations_ = malloc(sizeof(tommy_hashdyn)); - tommy_hashdyn_init((tommy_hashdyn *)allocations_); -} - -MemTracker::~MemTracker() { - ReportLeaks(); - tommy_hashdyn_done((tommy_hashdyn *)allocations_); - free(allocations_); -} - -void MemTracker::RecordAlloc(void *ptr, size_t sz, const char *file, size_t ln, const char *func) { - tommy_hash_t hash = tommy_inthash_u32((tommy_uint32_t) reinterpret_cast(ptr)); - - mutex_.lock(); - - AllocHashNode *node = (AllocHashNode *)tommy_hashdyn_search((tommy_hashdyn *)allocations_, HashNodeCompare, ptr, hash); - if (node) { - CCASSERT(0, "Double allocation with same address - this probably means you have a mismatched allocation / deallocation style."); - } else { - node = (AllocHashNode *)malloc(sizeof(AllocHashNode)); - node->key = ptr; - node->bytes = (unsigned int)sz; - node->filename = file; - node->function = func; - node->line = (unsigned int)ln; - tommy_hashdyn_insert((tommy_hashdyn *)allocations_, &node->node, node, hash); - total_memory_allocated_ += sz; - } - - mutex_.unlock(); -} - -void MemTracker::RecordReAlloc(void *oldptr, void *ptr, size_t sz, const char *file, size_t ln, const char *func) { - if (oldptr != ptr) { - if (oldptr) RecordFree(oldptr); - RecordAlloc(ptr, sz, file, ln, func); - return; - } - - if (sz == 0) { - RecordFree(oldptr); - return; - } - - tommy_hash_t hash = tommy_inthash_u32((tommy_uint32_t) reinterpret_cast(ptr)); - - mutex_.lock(); - - AllocHashNode *node = (AllocHashNode *)tommy_hashdyn_search((tommy_hashdyn *)allocations_, HashNodeCompare, ptr, hash); - if (node) { - int oldsize = (int)node->bytes; - node->bytes = (unsigned int)sz; - node->filename = file; - node->function = func; - node->line = (unsigned int)ln; - total_memory_allocated_ += ((int)sz - oldsize); - } else { - CCASSERT(0, "Reallocation address not found."); - } - - mutex_.unlock(); -} - -void MemTracker::RecordFree(void *ptr) { - // deal cleanly with null pointers - if (!ptr) - return; - - tommy_hash_t hash = tommy_inthash_u32((tommy_uint32_t) reinterpret_cast(ptr)); - - mutex_.lock(); - - AllocHashNode *node = (AllocHashNode *)tommy_hashdyn_remove((tommy_hashdyn *)allocations_, HashNodeCompare, ptr, hash); - if (node) { - total_memory_allocated_ -= node->bytes; - FreeHashNode(node); - } else { - CCASSERT(0, "Unable to locate allocation unit - this probably means you have a mismatched allocation / deallocation style."); - } - - mutex_.unlock(); -} - -int MemTracker::GetAllocSize(void *ptr) { - int sz = -1; - tommy_hash_t hash = tommy_inthash_u32((tommy_uint32_t) reinterpret_cast(ptr)); - - mutex_.lock(); - AllocHashNode *node = (AllocHashNode *)tommy_hashdyn_search((tommy_hashdyn *)allocations_, HashNodeCompare, ptr, hash); - if (node) { - sz = (int)node->bytes; - } - mutex_.unlock(); - return sz; -} - -void MemTracker::ReportLeaks() { - char *buffer; - - unsigned int count = (unsigned int)tommy_hashdyn_count((tommy_hashdyn *)allocations_); - if (count == 0) { - buffer = (char *)malloc(256); - strcpy(buffer, "MemoryTracker: No memory leaks.\n"); - } else { - int len = 768 * 1024; - buffer = (char *)malloc(len); - char *last = buffer + len - 3; - char *buf = buffer; - buf += sprintf(buffer, "MemoryTracker: Detected memory leaks !!!\n" - "MemoryTracker: (%u) Allocation(s) with total %u bytes.\n" - "MemoryTracker: Dumping allocations ->\n", - count, (unsigned int)total_memory_allocated_); - - AllocListParam param; - param.buf = buf; - param.last = last; - tommy_hashdyn_foreach_arg((tommy_hashdyn *)allocations_, HashNodeListAndFree, ¶m); - } - - FILE *f = fopen(LEAK_FILENAME, "w"); - if (f) { - fputs(buffer, f); - fclose(f); - } - CC_OUTPUT_STR(buffer); - free(buffer); -} - -struct AllocDumpNode { - std::string name; - unsigned int bytes; -}; - -bool operator<(const AllocDumpNode &a, const AllocDumpNode &b) { - return a.bytes < b.bytes; -} - -void MemTracker::DumpMemoryAllocation() { - AllocDumpParam param; - tommy_hashdyn_foreach_arg((tommy_hashdyn *)allocations_, HashNodeListAndDump, ¶m); - std::priority_queue q; - for (std::map::iterator ptr = param.allocMap.begin(); ptr != param.allocMap.end(); ++ptr) { - AllocDumpNode node; - node.name = ptr->first; - node.bytes = ptr->second; - q.push(std::move(node)); - } - - unsigned int total = 0; - char tmpbuf[1024]; - sprintf(tmpbuf, "total memory: %u\n", (unsigned int)total_memory_allocated_); - std::string str = tmpbuf; - while (!q.empty()) { - const AllocDumpNode &n = q.top(); - sprintf(tmpbuf, "%s:%u\n", n.name.c_str(), n.bytes); - str += tmpbuf; - total += n.bytes; - q.pop(); - } - sprintf(tmpbuf, "calc total memory: %u\n", total); - str += tmpbuf; - - #if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - FILE *f = fopen("MemDump.txt", "w"); - if (f) { - fputs(str.c_str(), f); - fclose(f); - } - #elif (CC_PLATFORM == CC_PLATFORM_ANDROID) - __android_log_write(ANDROID_LOG_WARN, "Tao", str.c_str()); - #else - fputs(str.c_str(), stdout); - #endif -} - -} // namespace cc -#endif diff --git a/native/cocos/base/memory/MemTracker.h b/native/cocos/base/memory/MemTracker.h deleted file mode 100755 index 159409a5fc..0000000000 --- a/native/cocos/base/memory/MemTracker.h +++ /dev/null @@ -1,91 +0,0 @@ -/**************************************************************************** -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ - -Copyright (c) 2000-2014 Torus Knot Software Ltd - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#ifndef CC_CORE_MEM_TRACKER_H_ -#define CC_CORE_MEM_TRACKER_H_ - -#ifdef CC_MEMORY_TRACKER - -namespace cc { - -/** This class tracks the allocations and deallocations made, and -is able to report memory statistics and leaks. -@note -This class is only available in debug builds. -*/ -class CC_CORE_API MemTracker { -public: - ~MemTracker(); - -private: - // protected ctor - MemTracker(); - -public: - // Get the total amount of memory allocated currently. - inline size_t total_memory_allocated() const { return total_memory_allocated_; } - - void DumpMemoryAllocation(); - - /** Record an allocation that has been made. Only to be called by - the memory management subsystem. - @param ptr The pointer to the memory - @param sz The size of the memory in bytes - @param pool The memory pool this allocation is occurring from - @param file The file in which the allocation is being made - @param ln The line on which the allocation is being made - @param func The function in which the allocation is being made - */ - void RecordAlloc(void *ptr, size_t sz, - const char *file = nullptr, size_t ln = 0, const char *func = nullptr); - // Record the deallocation of memory. - void RecordFree(void *ptr); - void RecordReAlloc(void *oldptr, void *ptr, size_t sz, - const char *file = nullptr, size_t ln = 0, const char *func = nullptr); - - int GetAllocSize(void *ptr); - - // Static utility method to get the memory tracker instance - inline static MemTracker *Instance() { - static MemTracker tracker; - return &tracker; - } - -private: - void ReportLeaks(); - -private: - std::mutex mutex_; - size_t total_memory_allocated_; - //tommy_hashdyn m_allocations; - void *allocations_; -}; - -} // namespace cc - -#endif - -#endif // CC_CORE_MEM_TRACKER_H_ diff --git a/native/cocos/base/memory/Memory.cpp b/native/cocos/base/memory/Memory.cpp index a0120155ed..d6e140fd95 100755 --- a/native/cocos/base/memory/Memory.cpp +++ b/native/cocos/base/memory/Memory.cpp @@ -23,18 +23,6 @@ THE SOFTWARE. ****************************************************************************/ -#include "base/CoreStd.h" - -#include "Memory.h" -#include "StlAlloc.h" - namespace cc { -CC_DLL SA stl_char_allocator; - -#if 0 -CC_DLL void* BareNewErroneouslyCalled(size_t sz) { - return malloc(sz); -} -#endif } // namespace cc diff --git a/native/cocos/base/memory/Memory.h b/native/cocos/base/memory/Memory.h index 615248c99d..d0948bfe9d 100644 --- a/native/cocos/base/memory/Memory.h +++ b/native/cocos/base/memory/Memory.h @@ -25,9 +25,7 @@ #pragma once -#include "AllocatedObj.h" #include "MemDef.h" -#include "MemTracker.h" #include "StlAlloc.h" #if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) #include @@ -41,20 +39,14 @@ #undef CC_SAFE_DELETE_ARRAY #undef CC_SAFE_FREE -#define CC_THROW_IF_OOM(ptr) \ - if (!ptr) { \ - throw std::runtime_error("out of memory"); \ - } - -#define CC_RETURN_IF_OOM(ptr, ret) \ - if (!ptr) { \ - return ret; \ - } - -#define CC_NEW(T) _CC_NEW T -#define CC_NEW_ARRAY(T, count) _CC_NEW T[count] -#define CC_DELETE(ptr) _CC_DELETE(ptr) -#define CC_DELETE_ARRAY(ptr) _CC_DELETE[](ptr) +#define CC_NEW(T) new T +#define CC_NEW_ALIGN(T, align) _CC_NEW_ALIGN(T, align) +#define CC_NEW_ALIGN_ARGS(T, align, ...) _CC_NEW_ALIGN_ARGS(T, align, ...) +#define CC_NEW_ARRAY(T, count) new T[count] +#define CC_DELETE(ptr) delete (ptr) +#define CC_DELETE_ARRAY(ptr) delete[](ptr) +#define CC_DELETE_ALIGN(ptr, T, align) _CC_DELETE_ALIGN(ptr, T, align) +#define CC_DELETE_ARRAY_ALIGN(ptr, T, count, align) _CC_DELETE_ARRAY_ALIGN(ptr, T, count, align) #define CC_SAFE_DELETE(ptr) \ if (ptr) { \ CC_DELETE(ptr); \ @@ -65,59 +57,17 @@ CC_DELETE_ARRAY(ptr); \ (ptr) = nullptr; \ } -#define CC_UNSAFE_DELETE(ptr) \ - if (ptr) { \ - CC_DELETE(ptr); \ - } -#define CC_UNSAFE_DELETE_ARRAY(ptr) \ - if (ptr) { \ - CC_DELETE_ARRAY(ptr); \ - } -#define CC_NEW_T(T) _CC_NEW_T(T) -#define CC_NEW_ARRAY_T(T, count) _CC_NEW_ARRAY_T(T, count) -#define CC_DELETE_T(ptr, T) _CC_DELETE_T(ptr, T) -#define CC_DELETE_ARRAY_T(ptr, T, count) _CC_DELETE_ARRAY_T(ptr, T, count) -#define CC_SAFE_DELETE_T(ptr, T) \ - if (ptr) { \ - CC_DELETE_T(ptr, T); \ - (ptr) = nullptr; \ - } -#define CC_SAFE_DELETE_ARRAY_T(ptr, T, count) \ - if (ptr) { \ - CC_DELETE_ARRAY_T(ptr, T, count); \ - (ptr) = nullptr; \ - } -#define CC_UNSAFE_DELETE_T(ptr, T) \ - if (ptr) { \ - CC_DELETE_T(ptr, T); \ - } -#define CC_UNSAFE_DELETE_ARRAY_T(ptr, T, count) \ - if (ptr) { \ - CC_DELETE_ARRAY_T(ptr, T, count); \ - } - -#define CC_MALLOC(bytes) _CC_MALLOC(bytes) -#define CC_REALLOC(ptr, bytes) _CC_REALLOC(ptr, bytes) -#define CC_ALLOC(T, count) _CC_ALLOC_T(T, count) -#define CC_FREE(ptr) _CC_FREE(ptr) +#define CC_MALLOC(bytes) malloc(bytes) +#define CC_MALLOC_ALIGN(bytes, align) _CC_MALLOC_ALIGN(align, bytes) +#define CC_REALLOC(ptr, bytes) realloc(ptr, bytes) +#define CC_FREE(ptr) free((void *)ptr) +#define CC_FREE_ALIGN(ptr) _CC_FREE_ALIGN(ptr) #define CC_SAFE_FREE(ptr) \ if (ptr) { \ CC_FREE(ptr); \ (ptr) = nullptr; \ } -#define CC_UNSAFE_FREE(ptr) \ - if (ptr) { \ - CC_FREE(ptr); \ - } - -#define CC_MALLOC_SIMD(bytes) _CC_MALLOC_SIMD(bytes) -#define CC_FREE_SIMD(ptr) _CC_FREE_SIMD(ptr) - -#define CC_DESTROY(ptr) \ - { \ - (ptr)->destroy(); \ - } #define CC_SAFE_DESTROY(ptr) \ if (ptr) { \ @@ -137,23 +87,21 @@ (ptr) = nullptr; \ } -#define CC_SAFE_RELEASE_REF(ptr) \ - if (ptr) { \ - (ptr)->ReleaseRef(); \ - ptr = nullptr; \ - } -#define CC_UNSAFE_RELEASE_REF(ptr) \ - if (ptr) { \ - (ptr)->ReleaseRef(); \ +#define CC_SAFE_RELEASE(p) \ + if (p) { \ + (p)->release(); \ } -#if 0 - #if (CC_PLATFORM == CC_PLATFORM_WINDOWS) && (CC_MODE == CC_MODE_DEBUG) -CC_CORE_API void *BareNewErroneouslyCalled(size_t sz); -inline void *operator new(size_t sz) { return BareNewErroneouslyCalled(sz); } -inline void operator delete(void *ptr) throw() { free(ptr); } - #endif -#endif +#define CC_SAFE_RELEASE_NULL(p) \ + if (p) { \ + (p)->release(); \ + (p) = nullptr; \ + } + +#define CC_SAFE_ADD_REF(p) \ + if (p) { \ + (p)->addRef(); \ + } #if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_11_0) #define ALIGNAS(x) diff --git a/native/cocos/base/memory/NedPooling.cpp b/native/cocos/base/memory/NedPooling.cpp deleted file mode 100755 index 6e447eba6a..0000000000 --- a/native/cocos/base/memory/NedPooling.cpp +++ /dev/null @@ -1,228 +0,0 @@ -/**************************************************************************** -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ - -Copyright (c) 2000-2014 Torus Knot Software Ltd - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "base/CoreStd.h" -#include "NedPooling.h" -#include "MemTracker.h" - -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_NEDPOOLING) - - // include Ned implementation - #if (CC_COMPILER == CC_COMPILER_MSVC) - #pragma warning(disable : 4005 4189 4267 4565 4702 4706) - #endif - - #define USE_LOCKS 1 - #define INSECURE 1 - #include - -namespace cc { - -namespace nedPoolingIntern { - -const size_t ned_poolCount = 14; // Needs to be greater than 4 - #if (CC_CPU_ARCH == CC_CPU_ARCH_32) -void *ned_poolFootprint = reinterpret_cast(0xBB1AA45A); - #else -void *ned_poolFootprint = reinterpret_cast((ui64)0xBB1AA45A); - #endif -nedalloc::nedpool *ned_pools[ned_poolCount + 1] = {0}; -nedalloc::nedpool *ned_poolsAligned[ned_poolCount + 1] = {0}; - -size_t PoolIDFromSize(size_t a_reqSize) { - // Requests size 16 or smaller are allocated at a 4 byte granularity. - // Requests size 17 or larger are allocated at a 16 byte granularity. - // With a ned_poolCount of 14, requests size 177 or larger go in the default pool. - - // spreadsheet style =IF(B35<=16; FLOOR((B35-1)/4;1); MIN(FLOOR((B35-1)/16; 1) + 3; 14)) - - size_t poolID = 0; - - if (a_reqSize > 0) { - if (a_reqSize <= 16) { - poolID = (a_reqSize - 1) >> 2; - } else { - poolID = std::min(((a_reqSize - 1) >> 4) + 3, ned_poolCount); - } - } - - return poolID; -} - -CC_DECL_MALLOC void *InternalAlloc(size_t a_reqSize) { - size_t poolID = PoolIDFromSize(a_reqSize); - nedalloc::nedpool *pool(0); // A pool pointer of 0 means the default pool. - - if (poolID < ned_poolCount) { - if (ned_pools[poolID] == 0) { - // Init pool if first use - - ned_pools[poolID] = nedalloc::nedcreatepool(0, 8); - nedalloc::nedpsetvalue(ned_pools[poolID], ned_poolFootprint); // All pools are stamped with a footprint - } - - pool = ned_pools[poolID]; - } - - return nedalloc::nedpmalloc(pool, a_reqSize); -} - -CC_DECL_MALLOC void *InternalRealloc(void *ptr, size_t a_reqSize) { - size_t poolID = PoolIDFromSize(a_reqSize); - nedalloc::nedpool *pool(0); // A pool pointer of 0 means the default pool. - - if (poolID < ned_poolCount) { - if (ned_pools[poolID] == 0) { - // Init pool if first use - - ned_pools[poolID] = nedalloc::nedcreatepool(0, 8); - nedalloc::nedpsetvalue(ned_pools[poolID], ned_poolFootprint); // All pools are stamped with a footprint - } - - pool = ned_pools[poolID]; - } - - return nedalloc::nedprealloc(pool, ptr, a_reqSize); -} - -CC_DECL_MALLOC void *InternalAllocAligned(size_t a_align, size_t a_reqSize) { - size_t poolID = PoolIDFromSize(a_reqSize); - nedalloc::nedpool *pool(0); // A pool pointer of 0 means the default pool. - - if (poolID < ned_poolCount) { - if (ned_poolsAligned[poolID] == 0) { - // Init pool if first use - - ned_poolsAligned[poolID] = nedalloc::nedcreatepool(0, 8); - nedalloc::nedpsetvalue(ned_poolsAligned[poolID], ned_poolFootprint); // All pools are stamped with a footprint - } - - pool = ned_poolsAligned[poolID]; - } - - return nedalloc::nedpmemalign(pool, a_align, a_reqSize); -} - -void InternalFree(void *a_mem) { - if (a_mem) { - nedalloc::nedpool *pool(0); - - // nedalloc lets us get the pool pointer from the memory pointer - void *footprint = nedalloc::nedgetvalue(&pool, a_mem); - - // Check footprint - if (footprint == ned_poolFootprint) { - // If we allocated the pool, deallocate from this pool... - nedalloc::nedpfree(pool, a_mem); - } else { - // ...otherwise let nedalloc handle it. - nedalloc::nedfree(a_mem); - } - } -} - -} // namespace nedPoolingIntern - -////////////////////////////////////////////////////////////////////////// - -CC_DECL_MALLOC void *NedPoolingImpl::AllocBytes(size_t count, const char *file, int line, const char *func) { - void *ptr = nedPoolingIntern::InternalAlloc(count); - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #else - // avoid unused params warning - (void)file; - (void)line; - (void)func; - #endif - return ptr; -} - -CC_DECL_MALLOC void *NedPoolingImpl::ReallocBytes(void *ptr, size_t count, const char *file, int line, const char *func) { - #ifdef CC_MEMORY_TRACKER - if (ptr) { - if (count == 0) { - MemTracker::Instance()->RecordFree(ptr); - nedPoolingIntern::InternalFree(ptr); - return nullptr; - } else { - int oldsz = MemTracker::Instance()->GetAllocSize(ptr); - if (oldsz < 0) { - CCASSERT(false, "ned realloc: get old size of ptr fail!"); - return realloc(ptr, count); - } else { - if (oldsz > (int)count) oldsz = (int)count; - } - - void *nptr = nedPoolingIntern::InternalAlloc(count); - memcpy(nptr, ptr, oldsz); - MemTracker::Instance()->RecordAlloc(nptr, count, file, line, func); - MemTracker::Instance()->RecordFree(ptr); - nedPoolingIntern::InternalFree(ptr); - return nptr; - } - } else { - if (count) { - ptr = nedPoolingIntern::InternalAlloc(count); - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - return ptr; - } else { - return nullptr; - } - } - #else - return nedPoolingIntern::InternalRealloc(ptr, count); - #endif -} - -CC_DECL_MALLOC void *NedPoolingImpl::AllocBytesAligned(size_t align, size_t count, const char *file, int line, const char *func) { - // default to platform SIMD alignment if none specified - void *ptr = nedPoolingIntern::InternalAllocAligned(align, count); - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #else - // avoid unused params warning - (void)file; - (void)line; - (void)func; - #endif - return ptr; -} - -void NedPoolingImpl::DeallocBytes(void *ptr) { - // deal with null - if (!ptr) { - return; - } - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordFree(ptr); - #endif - nedPoolingIntern::InternalFree(ptr); -} - -} // namespace cc - -#endif // end - #ifdef CC_MEMORY_ALLOCATOR_NEDPOOLING diff --git a/native/cocos/base/memory/NedPooling.h b/native/cocos/base/memory/NedPooling.h deleted file mode 100755 index d794e8d349..0000000000 --- a/native/cocos/base/memory/NedPooling.h +++ /dev/null @@ -1,84 +0,0 @@ -/**************************************************************************** -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ - -Copyright (c) 2000-2014 Torus Knot Software Ltd - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#ifndef CC_CORE_NED_POOLING_H_ -#define CC_CORE_NED_POOLING_H_ - -#include "base/Macros.h" - -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_NEDPOOLING) - -namespace cc { - -//Non-templated utility class just to hide nedmalloc. -class CC_DLL NedPoolingImpl { -public: - static CC_DECL_MALLOC void *AllocBytes(size_t count, const char *file, int line, const char *func); - static CC_DECL_MALLOC void *ReallocBytes(void *ptr, size_t count, const char *file, int line, const char *func); - static CC_DECL_MALLOC void *AllocBytesAligned(size_t align, size_t count, const char *file, int line, const char *func); - static void DeallocBytes(void *ptr); -}; - -/** An allocation policy for use with AllocatedObject and - STLAllocator. This is the class that actually does the allocation - and deallocation of physical memory, and is what you will want to - provide a custom version of if you wish to change how memory is allocated. - @par - This allocation policy uses nedmalloc (http://nedprod.com/programs/portable/nedmalloc/index.html). - */ -class CC_DLL NedPoolingAllocPolicy { -public: - static inline CC_DECL_MALLOC void *AllocateBytes(size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return NedPoolingImpl::AllocBytes(count, file, line, func); - } - static inline CC_DECL_MALLOC void *ReallocateBytes(void *ptr, size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return NedPoolingImpl::ReallocBytes(ptr, count, file, line, func); - } - static inline void DeallocateBytes(void *ptr) { - NedPoolingImpl::DeallocBytes(ptr); - } - static inline CC_DECL_MALLOC void *AllocateBytesAligned(size_t alignment, size_t count, const char *file = nullptr, int line = 0, const char *func = nullptr) { - return NedPoolingImpl::AllocBytesAligned(alignment, count, file, line, func); - } - static inline void DeallocateBytesAligned(void *ptr) { - NedPoolingImpl::DeallocBytes(ptr); - } - - // Get the maximum size of a single allocation - static inline size_t getMaxAllocationSize() { - return (std::numeric_limits::max)(); - } - -private: - // No instantiation - NedPoolingAllocPolicy() {} -}; - -} // namespace cc - -#endif // end - #ifdef CC_MEMORY_ALLOCATOR_NEDPOOLING - -#endif // CC_CORE_NED_POOLING_H_ diff --git a/native/cocos/base/memory/StdAlloc.h b/native/cocos/base/memory/StdAlloc.h index 514a88c81c..b1837195bb 100755 --- a/native/cocos/base/memory/StdAlloc.h +++ b/native/cocos/base/memory/StdAlloc.h @@ -24,14 +24,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#ifndef CC_CORE_STD_ALLOC_H_ -#define CC_CORE_STD_ALLOC_H_ +#pragma once -#if (CC_MEMORY_ALLOCATOR == CC_MEMORY_ALLOCATOR_STD) - - #include "base/Macros.h" - #include - #include +#include "base/Macros.h" +#include +#include namespace cc { @@ -44,119 +41,37 @@ namespace cc { */ class CC_DLL StdAllocPolicy { public: - static inline CC_DECL_MALLOC void *AllocateBytes(size_t count, - #ifdef CC_MEMORY_TRACKER - const char *file = nullptr, int line = 0, const char *func = nullptr - #else - const char * = nullptr, int = 0, const char * = nullptr - #endif - ) { - void *ptr = malloc(count); - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #endif - return ptr; - } - - static inline CC_DECL_MALLOC void *ReallocateBytes(void *ptr, size_t count, - #ifdef CC_MEMORY_TRACKER - const char *file = nullptr, int line = 0, const char *func = nullptr - #else - const char * = nullptr, int = 0, const char * = nullptr - #endif - ) { - #ifdef CC_MEMORY_TRACKER - if (ptr) { - if (count == 0) { - MemTracker::Instance()->RecordFree(ptr); - free(ptr); - return NULL; - } else { - int oldsz = MemTracker::Instance()->GetAllocSize(ptr); - if (oldsz < 0) { - printf("realloc: get old size of ptr fail!\n"); - assert(0); - return realloc(ptr, count); - } else { - if (oldsz > (int)count) oldsz = (int)count; - } - - void *nptr = malloc(count); - memcpy(nptr, ptr, oldsz); - MemTracker::Instance()->RecordAlloc(nptr, count, file, line, func); - MemTracker::Instance()->RecordFree(ptr); - free(ptr); - return nptr; - } - } else { - if (count) { - ptr = malloc(count); - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - return ptr; - } else { - return nullptr; - } - } - #else - return realloc(ptr, count); - #endif - } - - static inline void DeallocateBytes(void *ptr) { - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordFree(ptr); - #endif - free(ptr); - } - static inline CC_DECL_MALLOC void *AllocateBytesAligned(size_t alignment, size_t count, - #ifdef CC_MEMORY_TRACKER - const char *file = NULL, int line = 0, const char *func = NULL - #else - const char * = NULL, int = 0, const char * = NULL - #endif - ) { - #ifdef _MSC_VER + const char * = NULL, int = 0, const char * = NULL) { +#ifdef _MSC_VER void *ptr = _aligned_malloc(count, alignment); - #elif defined(__ANDROID__) +#elif defined(__ANDROID__) //void* ptr = memalign(alignment, count); - unsigned char *p = (unsigned char *)malloc(count + alignment); - size_t offset = alignment - (size_t(p) & (alignment - 1)); - unsigned char *ptr = p + offset; - ptr[-1] = (unsigned char)offset; - #else - CCASSERT(alignment % sizeof(void*) == 0, "alignment is not multiple of sizeof(void*)"); + unsigned char *p = (unsigned char *)malloc(count + alignment); + size_t offset = alignment - (size_t(p) & (alignment - 1)); + unsigned char *ptr = p + offset; + ptr[-1] = (unsigned char)offset; +#else + CCASSERT(alignment % sizeof(void *) == 0, "alignment is not multiple of sizeof(void*)"); void *ptr = NULL; posix_memalign(&ptr, alignment, count); - #endif - - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordAlloc(ptr, count, file, line, func); - #endif +#endif return ptr; } static inline void DeallocateBytesAligned(void *ptr) { - #ifdef CC_MEMORY_TRACKER - MemTracker::Instance()->RecordFree(ptr); - #endif - #ifdef _MSC_VER +#ifdef _MSC_VER _aligned_free(ptr); - #elif defined(__ANDROID__) +#elif defined(__ANDROID__) if (ptr) { unsigned char *mem = (unsigned char *)ptr; - mem = mem - mem[-1]; + mem = mem - mem[-1]; free(mem); } - #else +#else free(ptr); - #endif - } - - // Get the maximum size of a single allocation - static inline size_t getMaxAllocationSize() { - return (std::numeric_limits::max)(); +#endif } private: @@ -165,7 +80,3 @@ private: }; } // namespace cc - -#endif - -#endif // CC_CORE_STD_ALLOC_H_ diff --git a/native/cocos/base/memory/StlAlloc.h b/native/cocos/base/memory/StlAlloc.h index 05c64b8268..394cf35ab8 100755 --- a/native/cocos/base/memory/StlAlloc.h +++ b/native/cocos/base/memory/StlAlloc.h @@ -24,10 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#ifndef CC_CORE_STL_ALLOC_H_ -#define CC_CORE_STL_ALLOC_H_ +#pragma once -#include "MemDef.h" #include #include #include @@ -39,249 +37,6 @@ THE SOFTWARE. namespace cc { -// Base STL allocator class. -template -struct STLAllocatorBase { - // base class for generic allocators - typedef T value_type; -}; - -// Base STL allocator class. (const T version). -template -struct STLAllocatorBase { - // base class for generic allocators for const T - typedef T value_type; -}; - -template -class SA : public STLAllocatorBase { -public: - /// define our types, as per ISO C++ - typedef STLAllocatorBase Base; - typedef typename Base::value_type value_type; - typedef value_type *pointer; - typedef const value_type *const_pointer; - typedef value_type &reference; - typedef const value_type &const_reference; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - - /// the standard rebind mechanism - template - struct rebind { - typedef SA other; - }; - - /// ctor - inline explicit SA() {} - - /// dtor - virtual ~SA() {} - - /// copy ctor - done component wise - inline SA(SA const &) {} - - /// cast - template - inline SA(SA const &) {} - - /// cast - template - inline SA(SA const &) {} - - // TODO: fix C++17 warning - //inline pointer allocate(size_type count, typename std::allocator::const_pointer ptr = 0) { - // (void)ptr; - - // // convert request to bytes - // register size_type sz = count * sizeof(T); - // return static_cast(AllocPolicy::AllocateBytes(sz)); - //} - - inline void deallocate(pointer ptr, size_type) { - AllocPolicy::DeallocateBytes(ptr); - } - - pointer address(reference x) const { - return &x; - } - - const_pointer address(const_reference x) const { - return &x; - } - - size_type max_size() const throw() { - return size_t(-1) / sizeof(T); - } - -#if __cplusplus >= 201103L - template - void construct(Up *p, Args &&... args) { - ::new ((void *)p) Up(std::forward(args)...); - } - - template - void destroy(Up *p) { - p->~Up(); - } -#else - void construct(pointer p, const T &val) { - // call placement new - ::new ((void *)p) T(val); - } - - void destroy(pointer p) { - // do we have to protect against non-classes here? - // some articles suggest yes, some no - p->~T(); - } -#endif -}; - -/// determine equality, can memory from another allocator -/// be released by this allocator, (ISO C++) -template -inline bool operator==(SA const &, SA const &) { - // same alloc policy (P), memory can be freed - return true; -} - -/// determine equality, can memory from another allocator -/// be released by this allocator, (ISO C++) -template -inline bool operator==(SA const &, OtherAllocator const &) { - return false; -} -/// determine equality, can memory from another allocator -/// be released by this allocator, (ISO C++) -template -inline bool operator!=(SA const &, SA const &) { - // same alloc policy (P), memory can be freed - return false; -} - -/// determine equality, can memory from another allocator -/// be released by this allocator, (ISO C++) -template -inline bool operator!=(SA const &, OtherAllocator const &) { - return true; -} - -//////////////////////////////////////////////////////////////////// -extern CC_DLL SA stl_char_allocator; - -#define StdStringT(T) std::basic_string, std::allocator> -#define CustomMemoryStringT(T) std::basic_string, SA> - -template -bool operator<(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) < 0; -} -template -bool operator<(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) < 0; -} -template -bool operator<=(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) <= 0; -} -template -bool operator<=(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) <= 0; -} -template -bool operator>(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) > 0; -} -template -bool operator>(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) > 0; -} -template -bool operator>=(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) >= 0; -} -template -bool operator>=(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) >= 0; -} - -template -bool operator==(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) == 0; -} -template -bool operator==(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) == 0; -} - -template -bool operator!=(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) != 0; -} -template -bool operator!=(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return l.compare(0, l.length(), o.c_str(), o.length()) != 0; -} - -template -CustomMemoryStringT(T) operator+=(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return CustomMemoryStringT(T)(l) += o.c_str(); -} -template -CustomMemoryStringT(T) operator+=(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return CustomMemoryStringT(T)(l.c_str()) += o.c_str(); -} - -template -CustomMemoryStringT(T) operator+(const CustomMemoryStringT(T) & l, const StdStringT(T) & o) { - return CustomMemoryStringT(T)(l) += o.c_str(); -} - -template -CustomMemoryStringT(T) operator+(const StdStringT(T) & l, const CustomMemoryStringT(T) & o) { - return CustomMemoryStringT(T)(l.c_str()) += o.c_str(); -} - -template -CustomMemoryStringT(T) operator+(const T *l, const CustomMemoryStringT(T) & o) { - return CustomMemoryStringT(T)(l) += o; -} - -#undef StdStringT -#undef CustomMemoryStringT - -#if (CC_STL_MEMORY_ALLOCATOR == CC_STL_MEMORY_ALLOCATOR_CUSTOM) -template > -using vector = std::vector; - -template > -using list = std::list; - -template > -using queue = std::queue; - -template ::type, typename P = std::less> -using priority_queue = std::priority_queue; - -template > -using deque = std::deque; - -template , typename A = SA> -using set = std::set; - -template , typename A = SA, STLAP>> -using map = std::map; - -template , typename A = SA, STLAP>> -using multimap = std::multimap; - -template , typename P = std::equal_to, typename A = SA, STLAP>> -using unordered_map = std::unordered_map; - -typedef std::basic_string, SA> String; -typedef std::basic_string, SA> WString; -#else template using vector = std::vector; @@ -321,12 +76,8 @@ using unordered_map = std::unordered_map; template , typename P = std::equal_to> using unordered_multimap = std::unordered_multimap; -typedef std::string String; -typedef std::wstring WString; -#endif - -typedef vector StringArray; +using String = std::string; +using WString = std::wstring; +using StringArray = vector; } // namespace cc - -#endif // CC_CORE_STL_ALLOC_H_ diff --git a/native/cocos/base/threading/ThreadSafeLinearAllocator.cpp b/native/cocos/base/threading/ThreadSafeLinearAllocator.cpp index 9bf33b4afe..d8df47d523 100644 --- a/native/cocos/base/threading/ThreadSafeLinearAllocator.cpp +++ b/native/cocos/base/threading/ThreadSafeLinearAllocator.cpp @@ -32,18 +32,18 @@ namespace cc { ThreadSafeLinearAllocator::ThreadSafeLinearAllocator(size_t size, size_t alignment) noexcept : _capacity(size), _alignment(alignment) { if (alignment == 1) { - _buffer = _CC_MALLOC(size); + _buffer = CC_MALLOC(size); } else { - _buffer = _CC_MALLOC_ALIGN(size, alignment); + _buffer = CC_MALLOC_ALIGN(size, alignment); } CCASSERT(_buffer, "Out of memory"); } ThreadSafeLinearAllocator::~ThreadSafeLinearAllocator() { if (_alignment == 1) { - _CC_FREE(_buffer); + CC_FREE(_buffer); } else { - _CC_FREE_ALIGN(_buffer); + CC_FREE_ALIGN(_buffer); } } diff --git a/native/cocos/editor-support/SharedBufferManager.cpp b/native/cocos/editor-support/SharedBufferManager.cpp index 816684307f..ebf594e1b7 100644 --- a/native/cocos/editor-support/SharedBufferManager.cpp +++ b/native/cocos/editor-support/SharedBufferManager.cpp @@ -24,7 +24,7 @@ ****************************************************************************/ #include "SharedBufferManager.h" -#include "base/Macros.h" +#include "base/memory/Memory.h" MIDDLEWARE_BEGIN diff --git a/native/cocos/editor-support/dragonbones-creator-support/ArmatureCache.cpp b/native/cocos/editor-support/dragonbones-creator-support/ArmatureCache.cpp index 8504af0063..6e426adcb9 100644 --- a/native/cocos/editor-support/dragonbones-creator-support/ArmatureCache.cpp +++ b/native/cocos/editor-support/dragonbones-creator-support/ArmatureCache.cpp @@ -24,6 +24,7 @@ #include "ArmatureCache.h" #include "CCFactory.h" #include "base/TypeDef.h" +#include "base/memory/Memory.h" USING_NS_MW; // NOLINT(google-build-using-namespace) diff --git a/native/cocos/editor-support/middleware-adapter.cpp b/native/cocos/editor-support/middleware-adapter.cpp index d8b74329b6..fd6fee9d84 100644 --- a/native/cocos/editor-support/middleware-adapter.cpp +++ b/native/cocos/editor-support/middleware-adapter.cpp @@ -25,7 +25,7 @@ #include "middleware-adapter.h" #include "base/DeferredReleasePool.h" -#include "base/Macros.h" +#include "base/memory/Memory.h" MIDDLEWARE_BEGIN diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp index 502d5d813b..cd2883a8dd 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp @@ -29,6 +29,7 @@ #include "SkeletonCache.h" #include "spine-creator-support/AttachmentVertices.h" +#include "base/memory/Memory.h" USING_NS_MW; // NOLINT(google-build-using-namespace) using namespace cc; // NOLINT(google-build-using-namespace) diff --git a/native/cocos/engine/Engine.h b/native/cocos/engine/Engine.h index f886c6043e..04c3a08e54 100644 --- a/native/cocos/engine/Engine.h +++ b/native/cocos/engine/Engine.h @@ -125,7 +125,7 @@ private: bool _needRestart{false}; std::map _eventCallbacks; - CC_DISABLE_COPY_AND_MOVE_SEMANTICS(Engine); + CC_DISALLOW_COPY_MOVE_ASSIGN(Engine); }; } // namespace cc \ No newline at end of file diff --git a/native/cocos/network/HttpClient-apple.mm b/native/cocos/network/HttpClient-apple.mm index 5d8e56b38c..1e75d4cea0 100644 --- a/native/cocos/network/HttpClient-apple.mm +++ b/native/cocos/network/HttpClient-apple.mm @@ -36,6 +36,7 @@ #include "platform/FileUtils.h" #include "application/ApplicationManager.h" #include "base/Scheduler.h" +#include "base/memory/Memory.h" namespace cc { diff --git a/native/cocos/network/HttpClient.cpp b/native/cocos/network/HttpClient.cpp index b61a80bf4c..ec45d526cf 100644 --- a/native/cocos/network/HttpClient.cpp +++ b/native/cocos/network/HttpClient.cpp @@ -32,6 +32,7 @@ #include #include "application/ApplicationManager.h" #include "base/Log.h" +#include "base/memory/Memory.h" #include "platform/FileUtils.h" #include "platform/StdC.h" diff --git a/native/cocos/network/SocketIO.cpp b/native/cocos/network/SocketIO.cpp index d42a087d29..a88657972b 100644 --- a/native/cocos/network/SocketIO.cpp +++ b/native/cocos/network/SocketIO.cpp @@ -31,6 +31,7 @@ #include #include #include "application/ApplicationManager.h" +#include "base/memory/Memory.h" #include "base/Log.h" #include "base/UTF8.h" #include "network/HttpClient.h" @@ -392,8 +393,8 @@ void SIOClientImpl::handshakeResponse(HttpClient * /*sender*/, HttpResponse *res CC_LOG_INFO("%s completed", response->getHttpRequest()->getTag()); } - int32_t statusCode = static_cast(response->getResponseCode()); - char statusString[64] = {}; + auto statusCode = static_cast(response->getResponseCode()); + char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); CC_LOG_INFO("response code: %ld", statusCode); diff --git a/native/cocos/network/SocketIO.h b/native/cocos/network/SocketIO.h index 25f6a23217..5a4df0eb1b 100644 --- a/native/cocos/network/SocketIO.h +++ b/native/cocos/network/SocketIO.h @@ -152,8 +152,7 @@ private: friend class SIOClientImpl; - CC_DISABLE_COPY_SEMANTICS(SocketIO) - CC_DISABLE_MOVE_SEMANTICS(SocketIO) + CC_DISALLOW_COPY_MOVE_ASSIGN(SocketIO) }; //c++11 style callbacks entities will be created using CC_CALLBACK (which uses std::bind) diff --git a/native/cocos/network/WebSocket-libwebsockets.cpp b/native/cocos/network/WebSocket-libwebsockets.cpp index a91fbebe83..24a98d99a9 100644 --- a/native/cocos/network/WebSocket-libwebsockets.cpp +++ b/native/cocos/network/WebSocket-libwebsockets.cpp @@ -32,6 +32,7 @@ // clang-format off #include "base/Macros.h" +#include "base/memory/Memory.h" #include "uv.h" // clang-format on diff --git a/native/cocos/platform/BasePlatform.h b/native/cocos/platform/BasePlatform.h index b94161b3c3..241e795619 100644 --- a/native/cocos/platform/BasePlatform.h +++ b/native/cocos/platform/BasePlatform.h @@ -156,7 +156,7 @@ public: private: std::vector _osInterfaces; - CC_DISABLE_COPY_AND_MOVE_SEMANTICS(BasePlatform); + CC_DISALLOW_COPY_MOVE_ASSIGN(BasePlatform); }; } // namespace cc diff --git a/native/cocos/platform/FileUtils.cpp b/native/cocos/platform/FileUtils.cpp index 1baced35a1..4246830c11 100644 --- a/native/cocos/platform/FileUtils.cpp +++ b/native/cocos/platform/FileUtils.cpp @@ -28,13 +28,6 @@ #include #include -#include "base/Data.h" -#include "base/Log.h" -#include "platform/SAXParser.h" - -#include "tinydir/tinydir.h" -#include "tinyxml2/tinyxml2.h" - #include #include #include @@ -47,6 +40,14 @@ #include #include +#include "base/memory/Memory.h" +#include "base/Data.h" +#include "base/Log.h" +#include "platform/SAXParser.h" + +#include "tinydir/tinydir.h" +#include "tinyxml2/tinyxml2.h" + namespace cc { // Implement DictMaker diff --git a/native/cocos/platform/interfaces/OSInterface.h b/native/cocos/platform/interfaces/OSInterface.h index e7d8098f13..0d6fdb8fdc 100644 --- a/native/cocos/platform/interfaces/OSInterface.h +++ b/native/cocos/platform/interfaces/OSInterface.h @@ -69,7 +69,7 @@ protected: IEventDispatch *_evDispatchInterface{nullptr}; private: - CC_DISABLE_COPY_AND_MOVE_SEMANTICS(OSInterface); + CC_DISALLOW_COPY_MOVE_ASSIGN(OSInterface); }; } // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/Device.h b/native/cocos/platform/interfaces/modules/Device.h index 673c2910bc..52492590f0 100644 --- a/native/cocos/platform/interfaces/modules/Device.h +++ b/native/cocos/platform/interfaces/modules/Device.h @@ -121,7 +121,7 @@ public: private: Device(); - CC_DISABLE_COPY_AND_MOVE_SEMANTICS(Device) + CC_DISALLOW_COPY_MOVE_ASSIGN(Device) }; // end group diff --git a/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp b/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp index a5ae6dabe5..7e866dc04a 100644 --- a/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp +++ b/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "platform/win32/modules/CanvasRenderingContext2DDelegate.h" +#include "base/memory/Memory.h" namespace { void fillRectWithColor(uint8_t *buf, uint32_t totalWidth, uint32_t totalHeight, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t r, uint8_t g, uint8_t b, uint8_t a) { diff --git a/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp b/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp index d946225d06..42d294031e 100644 --- a/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp +++ b/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp @@ -87,14 +87,14 @@ void CommandBufferAgent::initMessageQueue() { device->_cmdBuffRefs.insert(this); // TODO(PatriceJiang): replace with: _messageQueue = CC_NEW(MessageQueue); - _messageQueue = _CC_NEW_T_ALIGN(MessageQueue, alignof(MessageQueue)); + _messageQueue = CC_NEW_ALIGN(MessageQueue, alignof(MessageQueue)); if (device->_multithreaded) _messageQueue->setImmediateMode(false); } void CommandBufferAgent::destroyMessageQueue() { DeviceAgent::getInstance()->getMessageQueue()->kickAndWait(); // TODO(PatriceJiang): replace with: CC_SAFE_DELETE(_messageQueue); - _CC_DELETE_T_ALIGN(_messageQueue, MessageQueue, alignof(MessageQueue)); + CC_DELETE_ALIGN(_messageQueue, MessageQueue, alignof(MessageQueue)); _messageQueue = nullptr; DeviceAgent::getInstance()->_cmdBuffRefs.erase(this); diff --git a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp index 120a767f5c..65f735433f 100644 --- a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp +++ b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp @@ -79,7 +79,7 @@ bool DeviceAgent::doInit(const DeviceInfo &info) { // NOTE: C++17 is required when enable alignment // TODO(PatriceJiang): replace with: _mainMessageQueue = CC_NEW(MessageQueue); - _mainMessageQueue = _CC_NEW_T_ALIGN(MessageQueue, alignof(MessageQueue)); //NOLINT + _mainMessageQueue = CC_NEW_ALIGN(MessageQueue, alignof(MessageQueue)); //NOLINT static_cast(_cmdBuff)->_queue = _queue; static_cast(_cmdBuff)->initAgent(); @@ -118,7 +118,7 @@ void DeviceAgent::doDestroy() { // NOTE: C++17 required when enable alignment // TODO(PatriceJiang): replace with: CC_SAFE_DELETE(_mainMessageQueue); - _CC_DELETE_T_ALIGN(_mainMessageQueue, MessageQueue, alignof(MessageQueue)); // NOLINT + CC_DELETE_ALIGN(_mainMessageQueue, MessageQueue, alignof(MessageQueue)); // NOLINT _mainMessageQueue = nullptr; } @@ -288,7 +288,7 @@ void doBufferTextureCopy(const uint8_t *const *buffers, Texture *texture, const } //TODO(PatriceJiang): in C++17 replace with:*allocator = CC_NEW(ThreadSafeLinearAllocator(totalSize)); - auto *allocator = _CC_NEW_T_ALIGN(ThreadSafeLinearAllocator(totalSize), alignof(ThreadSafeLinearAllocator)); + auto *allocator = CC_NEW_ALIGN(ThreadSafeLinearAllocator(totalSize), alignof(ThreadSafeLinearAllocator)); auto *actorRegions = allocator->allocate(count); memcpy(actorRegions, regions, count * sizeof(BufferTextureCopy)); @@ -316,7 +316,7 @@ void doBufferTextureCopy(const uint8_t *const *buffers, Texture *texture, const { actor->copyBuffersToTexture(buffers, dst, regions, count); // TODO(PatriceJiang): C++17 replace with: CC_DELETE(allocator); - if (allocator) _CC_DELETE_T_ALIGN(allocator, ThreadSafeLinearAllocator, alignof(ThreadSafeLinearAllocator)); + if (allocator) CC_DELETE_ALIGN(allocator, ThreadSafeLinearAllocator, alignof(ThreadSafeLinearAllocator)); }); } diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp index 0669cb5012..47ba83bdca 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -194,7 +194,7 @@ bool DeferredPipeline::destroy() { destroyQuadInputAssembler(); for (auto &it : _renderPasses) { - CC_DESTROY(it.second); + it.second->destroy(); } _renderPasses.clear(); diff --git a/native/cocos/ui/webview/WebView-inl.h b/native/cocos/ui/webview/WebView-inl.h index 01dae948df..a4e9fe42df 100644 --- a/native/cocos/ui/webview/WebView-inl.h +++ b/native/cocos/ui/webview/WebView-inl.h @@ -26,6 +26,7 @@ #include "WebView.h" #include "platform/FileUtils.h" +#include "base/memory/Memory.h" #if CC_PLATFORM == CC_PLATFORM_MAC_IOS #include "WebViewImpl-ios.h" diff --git a/native/extensions/assets-manager/AssetsManagerEx.cpp b/native/extensions/assets-manager/AssetsManagerEx.cpp index c291dfb088..ce1e78f17b 100644 --- a/native/extensions/assets-manager/AssetsManagerEx.cpp +++ b/native/extensions/assets-manager/AssetsManagerEx.cpp @@ -32,6 +32,7 @@ #include "base/DeferredReleasePool.h" #include "base/Log.h" #include "base/UTF8.h" +#include "base/memory/Memory.h" #ifdef MINIZIP_FROM_SYSTEM #include diff --git a/native/tools/simulator/libsimulator/lib/runtime/ConfigParser.cpp b/native/tools/simulator/libsimulator/lib/runtime/ConfigParser.cpp index 44042970ec..d0520b6117 100644 --- a/native/tools/simulator/libsimulator/lib/runtime/ConfigParser.cpp +++ b/native/tools/simulator/libsimulator/lib/runtime/ConfigParser.cpp @@ -30,6 +30,7 @@ #include "json/document.h" #include "json/stringbuffer.h" #include "json/writer.h" +#include "cocos/base/memory/Memory.h" // ConfigParser ConfigParser *ConfigParser::s_sharedConfigParserInstance = NULL; diff --git a/native/tools/simulator/libsimulator/lib/runtime/FileServer.cpp b/native/tools/simulator/libsimulator/lib/runtime/FileServer.cpp index 41ff274b6a..6829ff34db 100644 --- a/native/tools/simulator/libsimulator/lib/runtime/FileServer.cpp +++ b/native/tools/simulator/libsimulator/lib/runtime/FileServer.cpp @@ -28,6 +28,7 @@ THE SOFTWARE. #include "ConfigParser.h" #include "Runtime.h" #include "cocos/base/Log.h" +#include "cocos/base/memory/Memory.h" #include "cocos/platform/FileUtils.h" #include "zlib.h" diff --git a/native/tools/simulator/libsimulator/lib/runtime/Runtime.cpp b/native/tools/simulator/libsimulator/lib/runtime/Runtime.cpp index b83ad8e156..981acd5b75 100644 --- a/native/tools/simulator/libsimulator/lib/runtime/Runtime.cpp +++ b/native/tools/simulator/libsimulator/lib/runtime/Runtime.cpp @@ -31,6 +31,7 @@ THE SOFTWARE. #include "cocos/base/Log.h" #include "cocos/platform/FileUtils.h" +#include "cocos/base/memory/Memory.h" #if ((CC_PLATFORM == CC_PLATFORM_WINDOWS) || (CC_PLATFORM == CC_PLATFORM_MAC_OSX)) #include "DeviceEx.h"