BVB Source Codes

CRYENGINE Show 3DEngineMemory.h Source code

Return Download CRYENGINE: download 3DEngineMemory.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   3DEngineMemory.h
  5. //  Version:     v1.00
  6. //  Created:     23/04/2010 by Chris Raine.
  7. //  Compilers:   Visual Studio.NET
  8. // -------------------------------------------------------------------------
  9. //  History:
  10. ////////////////////////////////////////////////////////////////////////////
  11.  
  12. #ifndef Engine3DMemory_H
  13. #define Engine3DMemory_H
  14.  
  15. // The type of pool responsible for temporary allocations within the 3dengine
  16. //
  17. // Note: The header is included here for reasons of devirtualization. If
  18. // included directly from the precompiled header in 3dEngine, the gamedll will
  19. // fail to compile!
  20. #include <CryMemory/CryPool/PoolAlloc.h>
  21. #include <CrySystem/InplaceFactory.h>
  22. #include <CryCore/StlUtils.h>
  23.  
  24. using NCryPoolAlloc::CFirstFit;        // speed of allocations are crucial, so simply use the first fitting free allocation
  25. using NCryPoolAlloc::CInPlace;         //
  26. using NCryPoolAlloc::CMemoryDynamic;   // the pool itself will be dynamically allocated
  27. using NCryPoolAlloc::CListItemInPlace; // use inplace items
  28.  
  29. // Tempororary Pool Holder
  30. class CTemporaryPool
  31. {
  32. private:
  33.         // Access granted for 3dEngine to create, destroy and maintain the temporary
  34.         // pool for the 3d engine
  35.         friend class C3DEngine;
  36.  
  37.         // The static pool instance - one pool to rule them all (temp allocations at least)
  38.         static CTemporaryPool* s_Instance;
  39.  
  40.         // The type of the backing temporary pool
  41.         typedef CFirstFit<CInPlace<CMemoryDynamic>, CListItemInPlace> TTemporaryPool;
  42.         TTemporaryPool Pool;
  43.  
  44.         // A non-recursive critical section guards the pool against concurrent access
  45.         typedef CryCriticalSectionNonRecursive TTemporaryPoolLock;
  46.         TTemporaryPoolLock Lock;
  47.  
  48.         // Initialize the pool manager.
  49.         //
  50.         // Allocates the backing storage and initializes the temporary pool
  51.         // itself. The backing storage is aligned to 16 bytes to reduce the amount of
  52.         // cachelines crossed by the temporary pool
  53.         static bool Initialize(size_t poolSize)
  54.         {
  55.                 // Create the object instance
  56.                 s_Instance = new CTemporaryPool();
  57.                 if (!s_Instance)
  58.                 {
  59.                         CryFatalError("CTemporaryPool::Init(): could not create an instance of CTemporaryPool");
  60.                         return false;
  61.                 }
  62.  
  63.                 // Allocate the backing storage
  64.                 uint8* tempPool = reinterpret_cast<uint8*>(CryModuleMemalign(poolSize, 16));
  65.                 if (!tempPool)
  66.                 {
  67.                         CryFatalError("CTemporaryPool::Init(): could not allocate %" PRISIZE_T " bytes for temportary pool", poolSize);
  68.                         return false;
  69.                 }
  70.  
  71.                 // Initialize the actual pool
  72.                 s_Instance->Pool.InitMem(poolSize, tempPool);
  73.                 return true;
  74.         }
  75.  
  76.         // Shutdown the temporary pool manager.
  77.         //
  78.         // Frees the temporary pool
  79.         static bool Shutdown()
  80.         {
  81.                 if (s_Instance == NULL)
  82.                 {
  83.                         CryFatalError("CTemporaryPool::Shutdown(): no temporary pool instance present");
  84.                         return false;
  85.                 }
  86.  
  87.                 bool error = false;
  88.                 CTemporaryPool& instance = *s_Instance;
  89.                 if (instance.Pool.Data())
  90.                         CryModuleMemalignFree(instance.Pool.Data());
  91.                 else
  92.                         error = true;
  93.  
  94.                 delete s_Instance;
  95.                 s_Instance = NULL;
  96.                 return !error;
  97.         }
  98.  
  99.         // Templated construct helper member function using an inplace factory
  100.         //
  101.         // Called from the templated New<T, Expr> function below. Returns a typed
  102.         // pointer to the inplace constructed object.
  103.         template<typename T, typename InPlaceFactory>
  104.         T* Construct(const InPlaceFactory& factory, void* storage)
  105.         {
  106.                 return reinterpret_cast<T*>(factory.template apply<T>(storage));
  107.         }
  108.  
  109.         // Templated destruct helper member function.
  110.         //
  111.         // Calls the object's destructor and returns a void pointer to the storage
  112.         template<typename T>
  113.         void* Destruct(T* obj)
  114.         {
  115.                 obj->~T();
  116.                 return reinterpret_cast<void*>(obj);
  117.         }
  118.  
  119.         // Empty private constructor/destructors to prevent clients from creating and
  120.         // destroying instances of CTemporaryPool (there should only be one instance
  121.         // in the 3DEngine).
  122.         CTemporaryPool() {};
  123.         ~CTemporaryPool() {};
  124.  
  125. public:
  126.  
  127.         // Allocate a block of memory with the given size and alignment
  128.         void* Allocate(size_t size, size_t align)
  129.         {
  130.                 AUTO_LOCK_T(CryCriticalSectionNonRecursive, Lock);
  131.                 void* pData = Pool.Allocate<void*>(size, align);
  132.                 if (pData == NULL)
  133.                 {
  134.                         CryFatalError("**** could not allocate %" PRISIZE_T " bytes from temporary pool", size);
  135.                 }
  136.                 return Pool.Resolve<void*>(pData);
  137.         };
  138.  
  139.         // Allocates memory and constructs object of type 'T'
  140.         //
  141.         // Note: This method is respects the alignment of 'T' via C99 alignof()
  142.         template<typename T, typename Expr>
  143.         T* New(const Expr& expr)
  144.         {
  145.                 AUTO_LOCK_T(CryCriticalSectionNonRecursive, Lock);
  146.                 void* pObjStorage = Pool.Allocate<void*>(sizeof(T), alignof(T));
  147.                 if (pObjStorage == NULL)
  148.                 {
  149.                         CryFatalError("**** could not allocate %d bytes from temporary pool",
  150.                                       (int)sizeof(T));
  151.                 }
  152.                 return Construct<T>(expr, pObjStorage);
  153.         };
  154.  
  155.         // Allocates memory and constructs object of type 'T'
  156.         //
  157.         // Note: This method is respects the alignment of 'T' via C99 alignof()
  158.         template<typename T>
  159.         T* New()
  160.         {
  161.                 AUTO_LOCK_T(CryCriticalSectionNonRecursive, Lock);
  162.                 void* pObjStorage = Pool.Allocate<void*>(sizeof(T), alignof(T));
  163.                 if (pObjStorage == NULL)
  164.                 {
  165.                         CryFatalError("**** could not allocate %d bytes from temporary pool",
  166.                                       (int)sizeof(T));
  167.                 }
  168.                 return Construct<T>(InplaceFactory(), pObjStorage);
  169.         };
  170.  
  171.         // Frees a block of memory from the temporary pool
  172.         //
  173.         void Free(void* ptr)
  174.         {
  175.                 AUTO_LOCK_T(CryCriticalSectionNonRecursive, Lock);
  176.                 Pool.Free(ptr);
  177.         }
  178.  
  179.         // Destroys an object of type 'T' and frees the underlying block of memory
  180.         template<typename T>
  181.         void Delete(T* ptr)
  182.         {
  183.                 AUTO_LOCK_T(CryCriticalSectionNonRecursive, Lock);
  184.                 Pool.Free(Destruct<T>(ptr));
  185.         }
  186.  
  187.         // Static function to retrieve the static instance of CTemporaryPool
  188.         static CTemporaryPool* Get() { return s_Instance; };
  189.  
  190.         void                   GetMemoryUsage(ICrySizer* pSizer) const
  191.         {
  192.                 pSizer->AddObject(Pool.Data(), Pool.MemSize());
  193.         }
  194. };
  195.  
  196. // A stl compliant scratch allocator that uses the given temporary pool.
  197. template<class Type>
  198. class scratch_allocator : public stl::SAllocatorConstruct
  199. {
  200. public:
  201.         typedef Type              value_type;
  202.         typedef value_type*       pointer;
  203.         typedef const value_type* const_pointer;
  204.         typedef value_type&       reference;
  205.         typedef const value_type& const_reference;
  206.         typedef size_t            size_type;
  207.         typedef ptrdiff_t         difference_type;
  208.  
  209.         template<class value_type1> struct rebind
  210.         {
  211.                 typedef scratch_allocator<value_type1> other;
  212.         };
  213.  
  214.         scratch_allocator() {}
  215.         template<class value_type1> scratch_allocator(const scratch_allocator<value_type1>&) {}
  216.         scratch_allocator(const scratch_allocator<value_type>&) {}
  217.         ~scratch_allocator() {}
  218.  
  219.         pointer       address(reference x) const       { return &x; }
  220.         const_pointer address(const_reference x) const { return &x; }
  221.  
  222.         // Note: size can be zero - return value will be null in that case
  223.         value_type* allocate(size_type n, const void* = 0)
  224.         {
  225.                 if (n != 0)
  226.                 {
  227.                         size_type buf_size = n * sizeof(value_type);
  228.  
  229.                         void* ret = CTemporaryPool::Get()->Allocate(
  230.                           buf_size,
  231.                           alignof(value_type));
  232.  
  233.                         return reinterpret_cast<value_type*>(ret);
  234.                 }
  235.                 return 0;
  236.         }
  237.  
  238.         // Note: size can be zero.
  239.         void deallocate(pointer p, size_type n)
  240.         {
  241.                 if (p != NULL)
  242.                 {
  243.                         CTemporaryPool::Get()->Free(p);
  244.                 }
  245.         }
  246.  
  247.         size_type max_size() const           { return size_t(-1) / sizeof(value_type); }
  248.  
  249.         void      destroy(pointer p)         { p->~value_type(); }
  250.  
  251.         void      cleanup()                  {}
  252.  
  253.         size_t    get_heap_size()            { return 0; }
  254.  
  255.         size_t    get_wasted_in_allocation() { return 0; }
  256.  
  257.         size_t    get_wasted_in_blocks()     { return 0; }
  258. };
  259.  
  260. // A scratch vector type to use the stl vector
  261. template<typename Type>
  262. class scratch_vector : public std::vector<Type, scratch_allocator<Type>>
  263. {
  264. };
  265.  
  266. namespace util
  267. {
  268. extern void* pool_allocate(size_t nSize);
  269. extern void  pool_free(void* ptr);
  270. }
  271.  
  272. #endif
  273.  
download3DEngineMemory.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top