Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / include / physics_effects / base_level / base / pfx_heap_manager.h
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \r
5 Physics Effects is open software; you can redistribute it and/or\r
6 modify it under the terms of the BSD License.\r
7 \r
8 Physics Effects is distributed in the hope that it will be useful,\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
11 See the BSD License for more details.\r
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \r
17 #ifndef _SCE_PFX_HEAP_MANAGER_H\r
18 #define _SCE_PFX_HEAP_MANAGER_H\r
19 \r
20 #include "pfx_common.h"\r
21 \r
22 //J プールされたメモリを管理するスタックのサイズ\r
23 //E Size of a stack which used to manage pool memory\r
24 #define SCE_PFX_HEAP_STACK_SIZE 64\r
25 \r
26 #define SCE_PFX_MIN_ALLOC_SIZE 16\r
27 \r
28 #define SCE_PFX_ALLOC_BYTES_ALIGN16(bytes) SCE_PFX_MAX(16,SCE_PFX_BYTES_ALIGN16(bytes))\r
29 #define SCE_PFX_ALLOC_BYTES_ALIGN128(bytes) SCE_PFX_MAX(128,SCE_PFX_BYTES_ALIGN128(bytes))\r
30 \r
31 #if defined (_WIN64) || defined (__LP64__)\r
32         #define SCE_PFX_ALIGN_MASK_16   0xfffffffffffffff0\r
33         #define SCE_PFX_ALIGN_MASK_128  0xffffffffffffff80\r
34 #else\r
35         #define SCE_PFX_ALIGN_MASK_16   0xfffffff0\r
36         #define SCE_PFX_ALIGN_MASK_128  0xffffff80\r
37 #endif\r
38 \r
39 ///////////////////////////////////////////////////////////////////////////////\r
40 // PfxHeapManager\r
41 \r
42 //J <補足>\r
43 //J メモリはスタックで管理されています。取得した順と逆に開放する必要があります。\r
44 //J メモリを一気に開放したい場合はclear()を呼び出してください。\r
45 //J 最小割り当てサイズはSCE_PFX_MIN_ALLOC_SIZEで定義されます。\r
46 \r
47 //E <Notes>\r
48 //E Memory is managed as a stack, so deallocate() needs to be called in reverse order.\r
49 //E Use clear() to deallocate all allocated memory at once.\r
50 //E SCE_PFX_MIN_ALLOC_SIZE defines the smallest amount of buffer.\r
51 \r
52 namespace sce {\r
53 namespace PhysicsEffects {\r
54 \r
55 class PfxHeapManager\r
56 {\r
57 private:\r
58         PfxUInt8 *m_heap;\r
59         PfxUInt8 *m_poolStack[SCE_PFX_HEAP_STACK_SIZE];\r
60         PfxInt32 m_heapBytes;\r
61         PfxInt32 m_curStack;\r
62         PfxInt32 m_rest;\r
63         \r
64 public:\r
65         enum {ALIGN16=16,ALIGN128=128};\r
66 \r
67         PfxHeapManager(PfxUInt8 *buf,PfxInt32 bytes)\r
68         {\r
69                 m_heap = buf;\r
70                 m_heapBytes = bytes;\r
71                 clear();\r
72         }\r
73         \r
74         ~PfxHeapManager()\r
75         {\r
76         }\r
77         \r
78         PfxInt32 getAllocated()\r
79         {\r
80                 return (PfxInt32)(m_poolStack[m_curStack]-m_heap);\r
81         }\r
82         \r
83         PfxInt32 getRest()\r
84         {\r
85                 return m_heapBytes-getAllocated();\r
86         }\r
87 \r
88         void *allocate(size_t bytes,PfxInt32 alignment = ALIGN16)\r
89         {\r
90         SCE_PFX_ALWAYS_ASSERT(m_curStack<SCE_PFX_HEAP_STACK_SIZE);\r
91 \r
92         bytes = SCE_PFX_MAX(bytes,SCE_PFX_MIN_ALLOC_SIZE);\r
93 \r
94         uintptr_t p = (uintptr_t)m_poolStack[m_curStack];\r
95 \r
96         if(alignment == ALIGN128) {\r
97                 p = (p+127) & SCE_PFX_ALIGN_MASK_128;\r
98                 bytes = (bytes+127) & SCE_PFX_ALIGN_MASK_128;\r
99         }\r
100         else {\r
101                 p = (p+15) & SCE_PFX_ALIGN_MASK_16;\r
102                 bytes = (bytes+15) & SCE_PFX_ALIGN_MASK_16;\r
103         }\r
104 \r
105         SCE_PFX_ALWAYS_ASSERT_MSG(bytes <= (m_heapBytes-(p-(uintptr_t)m_heap)),"Memory overflow");\r
106 \r
107         m_poolStack[++m_curStack] = (PfxUInt8 *)(p + bytes);\r
108 \r
109         m_rest = getRest();\r
110 \r
111         return (void*)p;\r
112         }\r
113 \r
114         void deallocate(void *p)\r
115         {\r
116 #if 0\r
117                 m_curStack--;\r
118                 PfxInt32 addr = (PfxInt32)m_poolStack[m_curStack];\r
119                 SCE_PFX_ALWAYS_ASSERT_MSG(addr == (PfxInt32)p || ((addr+127) & 0xffffff80) == (PfxInt32)p,"Stack overflow");\r
120 #else\r
121                 (void) p;\r
122                 m_curStack--;\r
123 #endif\r
124         }\r
125         \r
126         void clear()\r
127         {\r
128                 m_poolStack[0] = m_heap;\r
129                 m_curStack = 0;\r
130                 m_rest = 0;\r
131         }\r
132 \r
133         void printStack()\r
134         {\r
135                 SCE_PFX_PRINTF("memStack %d/%d\n",m_curStack,SCE_PFX_HEAP_STACK_SIZE);\r
136         }\r
137 };\r
138 \r
139 } //namespace PhysicsEffects\r
140 } //namespace sce\r
141 #endif // _SCE_PFX_HEAP_MANAGER_H\r