diff options
author | mdempsky <mdempsky@chromium.org> | 2015-09-22 10:37:07 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-22 10:37:08 -0700 |
commit | 78041fab416f5e92019f632dc64507c8ede43eab (patch) | |
tree | 89107566da8874721515a897dbbab4e05dd31118 /src/core | |
parent | 11edad9098c1abc041e65d28b87d1080c490bc58 (diff) |
Small cleanups to SkSmallAllocator
- Add missing #include <new>.
- Simplify createT functions with C++11 variadic templates.
- Change destroyT helper function into a private static function.
Review URL: https://codereview.chromium.org/1359853002
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkSmallAllocator.h | 62 |
1 files changed, 11 insertions, 51 deletions
diff --git a/src/core/SkSmallAllocator.h b/src/core/SkSmallAllocator.h index c7977d36a5..79b1d299bc 100644 --- a/src/core/SkSmallAllocator.h +++ b/src/core/SkSmallAllocator.h @@ -11,11 +11,7 @@ #include "SkTDArray.h" #include "SkTypes.h" -// Used by SkSmallAllocator to call the destructor for objects it has -// allocated. -template<typename T> void destroyT(void* ptr) { - static_cast<T*>(ptr)->~T(); -} +#include <new> /* * Template class for allocating small objects without additional heap memory @@ -52,58 +48,16 @@ public: /* * Create a new object of type T. Its lifetime will be handled by this * SkSmallAllocator. - * Each version behaves the same but takes a different number of - * arguments. * Note: If kMaxObjects have been created by this SkSmallAllocator, nullptr * will be returned. */ - template<typename T> - T* createT() { - void* buf = this->reserveT<T>(); - if (nullptr == buf) { - return nullptr; - } - new (buf) T; - return static_cast<T*>(buf); - } - - template<typename T, typename A1> T* createT(const A1& a1) { - void* buf = this->reserveT<T>(); - if (nullptr == buf) { - return nullptr; - } - new (buf) T(a1); - return static_cast<T*>(buf); - } - - template<typename T, typename A1, typename A2> - T* createT(const A1& a1, const A2& a2) { - void* buf = this->reserveT<T>(); - if (nullptr == buf) { - return nullptr; - } - new (buf) T(a1, a2); - return static_cast<T*>(buf); - } - - template<typename T, typename A1, typename A2, typename A3> - T* createT(const A1& a1, const A2& a2, const A3& a3) { - void* buf = this->reserveT<T>(); - if (nullptr == buf) { - return nullptr; - } - new (buf) T(a1, a2, a3); - return static_cast<T*>(buf); - } - - template<typename T, typename A1, typename A2, typename A3, typename A4> - T* createT(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { + template<typename T, typename... Args> + T* createT(const Args&... args) { void* buf = this->reserveT<T>(); if (nullptr == buf) { return nullptr; } - new (buf) T(a1, a2, a3, a4); - return static_cast<T*>(buf); + return new (buf) T(args...); } /* @@ -138,7 +92,7 @@ public: rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); fStorageUsed += storageRequired; } - rec->fKillProc = destroyT<T>; + rec->fKillProc = DestroyT<T>; fNumObjects++; return rec->fObj; } @@ -165,6 +119,12 @@ private: void (*fKillProc)(void*); }; + // Used to call the destructor for allocated objects. + template<typename T> + static void DestroyT(void* ptr) { + static_cast<T*>(ptr)->~T(); + } + // Number of bytes used so far. size_t fStorageUsed; // Pad the storage size to be 4-byte aligned. |