diff options
author | Brecht Van Lommel <brecht@blender.org> | 2022-08-26 19:09:44 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2022-08-29 20:45:58 +0300 |
commit | ff27457240950e51753ff56181841a14d1bf6b11 (patch) | |
tree | 209d6e763a0fdb80fab7ec0033840c25ccf61b26 /intern | |
parent | 658ff994c578aae5260c4b7933d95c6cb14f2652 (diff) |
Allocator: add MEM_cnew_array
This is a more C++ friendly version MEM_calloc_arrayN, like MEM_cnew is for
MEM_callocN. For cases where data structures are still C and Vector or Array
don't work.
Diffstat (limited to 'intern')
-rw-r--r-- | intern/guardedalloc/MEM_guardedalloc.h | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h index 64987548a11..a8c2d9abcc8 100644 --- a/intern/guardedalloc/MEM_guardedalloc.h +++ b/intern/guardedalloc/MEM_guardedalloc.h @@ -276,6 +276,21 @@ inline T *MEM_new(const char *allocation_name, Args &&...args) } /** + * Destructs and deallocates an object previously allocated with any `MEM_*` function. + * Passing in null does nothing. + */ +template<typename T> inline void MEM_delete(const T *ptr) +{ + if (ptr == nullptr) { + /* Support #ptr being null, because C++ `delete` supports that as well. */ + return; + } + /* C++ allows destruction of const objects, so the pointer is allowed to be const. */ + ptr->~T(); + MEM_freeN(const_cast<T *>(ptr)); +} + +/** * Allocates zero-initialized memory for an object of type #T. The constructor of #T is not called, * therefor this should only used with trivial types (like all C types). * It's valid to call #MEM_freeN on a pointer returned by this, because a destructor call is not @@ -288,6 +303,15 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name) } /** + * Same as MEM_cnew but for arrays, better alternative to #MEM_calloc_arrayN. + */ +template<typename T> inline T *MEM_cnew_array(const size_t length, const char *allocation_name) +{ + static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used."); + return static_cast<T *>(MEM_calloc_arrayN(length, sizeof(T), allocation_name)); +} + +/** * Allocate memory for an object of type #T and copy construct an object from `other`. * Only applicable for a trivial types. * @@ -301,23 +325,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot { static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used."); T *new_object = static_cast<T *>(MEM_mallocN(sizeof(T), allocation_name)); - memcpy(new_object, &other, sizeof(T)); - return new_object; -} - -/** - * Destructs and deallocates an object previously allocated with any `MEM_*` function. - * Passing in null does nothing. - */ -template<typename T> inline void MEM_delete(const T *ptr) -{ - if (ptr == nullptr) { - /* Support #ptr being null, because C++ `delete` supports that as well. */ - return; + if (new_object) { + memcpy(new_object, &other, sizeof(T)); } - /* C++ allows destruction of const objects, so the pointer is allowed to be const. */ - ptr->~T(); - MEM_freeN(const_cast<T *>(ptr)); + return new_object; } /* Allocation functions (for C++ only). */ |