Blender V2.61 - r43446

btGenericPoolAllocator.h

Go to the documentation of this file.
00001 
00006 /*
00007 Bullet Continuous Collision Detection and Physics Library
00008 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00009 
00010 This software is provided 'as-is', without any express or implied warranty.
00011 In no event will the authors be held liable for any damages arising from the use of this software.
00012 Permission is granted to anyone to use this software for any purpose,
00013 including commercial applications, and to alter it and redistribute it freely,
00014 subject to the following restrictions:
00015 
00016 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00017 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00018 3. This notice may not be removed or altered from any source distribution.
00019 */
00020 
00021 #ifndef BT_GENERIC_POOL_ALLOCATOR_H
00022 #define BT_GENERIC_POOL_ALLOCATOR_H
00023 
00024 #include <limits.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include "LinearMath/btAlignedAllocator.h"
00028 
00029 #define BT_UINT_MAX UINT_MAX
00030 #define BT_DEFAULT_MAX_POOLS 16
00031 
00032 
00034 class btGenericMemoryPool
00035 {
00036 public:
00037     unsigned char * m_pool; //[m_element_size*m_max_element_count];
00038     size_t * m_free_nodes; //[m_max_element_count];//! free nodes
00039     size_t * m_allocated_sizes;//[m_max_element_count];//! Number of elements allocated per node
00040     size_t m_allocated_count;
00041     size_t m_free_nodes_count;
00042 protected:
00043     size_t m_element_size;
00044     size_t m_max_element_count;
00045 
00046     size_t allocate_from_free_nodes(size_t num_elements);
00047     size_t allocate_from_pool(size_t num_elements);
00048 
00049 public:
00050 
00051     void init_pool(size_t element_size, size_t element_count);
00052 
00053     void end_pool();
00054 
00055 
00056     btGenericMemoryPool(size_t element_size, size_t element_count)
00057     {
00058         init_pool(element_size, element_count);
00059     }
00060 
00061     ~btGenericMemoryPool()
00062     {
00063         end_pool();
00064     }
00065 
00066 
00067     inline size_t get_pool_capacity()
00068     {
00069         return m_element_size*m_max_element_count;
00070     }
00071 
00072     inline size_t gem_element_size()
00073     {
00074         return m_element_size;
00075     }
00076 
00077     inline size_t get_max_element_count()
00078     {
00079         return m_max_element_count;
00080     }
00081 
00082     inline size_t get_allocated_count()
00083     {
00084         return m_allocated_count;
00085     }
00086 
00087     inline size_t get_free_positions_count()
00088     {
00089         return m_free_nodes_count;
00090     }
00091 
00092     inline void * get_element_data(size_t element_index)
00093     {
00094         return &m_pool[element_index*m_element_size];
00095     }
00096 
00098 
00101     void * allocate(size_t size_bytes);
00102 
00103     bool freeMemory(void * pointer);
00104 };
00105 
00106 
00107 
00108 
00110 
00113 class btGenericPoolAllocator
00114 {
00115 protected:
00116     size_t m_pool_element_size;
00117     size_t m_pool_element_count;
00118 public:
00119     btGenericMemoryPool * m_pools[BT_DEFAULT_MAX_POOLS];
00120     size_t m_pool_count;
00121 
00122 
00123     inline size_t get_pool_capacity()
00124     {
00125         return m_pool_element_size*m_pool_element_count;
00126     }
00127 
00128 
00129 protected:
00130     // creates a pool
00131     btGenericMemoryPool * push_new_pool();
00132 
00133     void * failback_alloc(size_t size_bytes);
00134 
00135     bool failback_free(void * pointer);
00136 public:
00137 
00138     btGenericPoolAllocator(size_t pool_element_size, size_t pool_element_count)
00139     {
00140         m_pool_count = 0;
00141         m_pool_element_size = pool_element_size;
00142         m_pool_element_count = pool_element_count;
00143     }
00144 
00145     virtual ~btGenericPoolAllocator();
00146 
00148 
00151     void * allocate(size_t size_bytes);
00152 
00153     bool freeMemory(void * pointer);
00154 };
00155 
00156 
00157 
00158 void * btPoolAlloc(size_t size);
00159 void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize);
00160 void btPoolFree(void *ptr);
00161 
00162 
00163 #endif