Merge "Sync UTC harness" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / common / memory-pool-object-allocator.h
index c110096..554ce9a 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__
-#define __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__
+#ifndef DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
+#define DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 namespace Dali
 {
-
 namespace Internal
 {
-
 /**
  * @brief Helper for allocating/deallocating objects using a memory pool.
  *
@@ -34,16 +32,15 @@ namespace Internal
  * The type may be a class or POD.
  *
  */
-template< typename T >
+template<typename T>
 class MemoryPoolObjectAllocator
 {
 public:
-
   /**
    * @brief Constructor
    */
   MemoryPoolObjectAllocator()
-  : mPool( NULL )
+  : mPool(nullptr)
   {
     ResetMemoryPool();
   }
@@ -63,7 +60,17 @@ public:
    */
   T* Allocate()
   {
-    return new ( mPool->Allocate() ) T();
+    return new(mPool->Allocate()) T();
+  }
+
+  /**
+   * @brief Thread-safe version of Allocate()
+   *
+   * @return Return the allocated object
+   */
+  T* AllocateThreadSafe()
+  {
+    return new(mPool->AllocateThreadSafe()) T();
   }
 
   /**
@@ -80,15 +87,61 @@ public:
   }
 
   /**
+   * @brief Thread-safe version of AllocateRaw()
+   *
+   * @return Return the allocated memory block
+   */
+  void* AllocateRawThreadSafe()
+  {
+    return mPool->AllocateThreadSafe();
+  }
+
+  /**
    * @brief Return the object to the memory pool
+   * Note: This performs a deallocation only, if the object has a destructor and is not
+   * freed from within an overloaded delete operator, Destroy() must be used instead.
    *
    * @param object Pointer to the object to delete
    */
-  void Free( T* object )
+  void Free(T* object)
+  {
+    mPool->Free(object);
+  }
+
+  /**
+   * @brief Thread-safe version of Free()
+   * Note: This performs a deallocation only, if the object has a destructor and is not
+   * freed from within an overloaded delete operator, DestroyThreadSafe() must be used instead.
+   *
+   * @param object Pointer to the object to delete
+   */
+  void FreeThreadSafe(T* object)
+  {
+    mPool->FreeThreadSafe(object);
+  }
+
+  /**
+   * @brief Return the object to the memory pool after destructing it.
+   * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
+   *
+   * @param object Pointer to the object to delete
+   */
+  void Destroy(T* object)
   {
     object->~T();
+    mPool->Free(object);
+  }
 
-    mPool->Free( object );
+  /**
+   * @brief Thread-safe version of Destroy()
+   * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
+   *
+   * @param object Pointer to the object to delete
+   */
+  void DestroyThreadSafe(T* object)
+  {
+    object->~T();
+    mPool->FreeThreadSafe(object);
   }
 
   /**
@@ -96,22 +149,32 @@ public:
    */
   void ResetMemoryPool()
   {
-    if( mPool )
-    {
-      delete mPool;
-    }
+    delete mPool;
 
-    mPool = new FixedSizeMemoryPool( TypeSizeWithAlignment< T >::size );
+    mPool = new FixedSizeMemoryPool(TypeSizeWithAlignment<T>::size);
+  }
+
+  /**
+   * @brief Get the capacity of the memory pool
+   */
+  uint32_t GetCapacity() const
+  {
+    return mPool->GetCapacity();
   }
 
 private:
+  // Undefined
+  MemoryPoolObjectAllocator(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
 
-  FixedSizeMemoryPool* mPool;      ///< Memory pool from which allocations are made
+  // Undefined
+  MemoryPoolObjectAllocator& operator=(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
 
+private:
+  FixedSizeMemoryPool* mPool; ///< Memory pool from which allocations are made
 };
 
 } // namespace Internal
 
 } // namespace Dali
 
-#endif /* __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__ */
+#endif // DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H