2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
\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
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
13 A copy of the BSD License is distributed with
\r
14 Physics Effects under the filename: physics_effects_license.txt
\r
17 #ifndef _SCE_PFX_HEAP_MANAGER_H
\r
18 #define _SCE_PFX_HEAP_MANAGER_H
\r
20 #include "pfx_common.h"
\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
26 #define SCE_PFX_MIN_ALLOC_SIZE 16
\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
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
35 #define SCE_PFX_ALIGN_MASK_16 0xfffffff0
\r
36 #define SCE_PFX_ALIGN_MASK_128 0xffffff80
\r
39 ///////////////////////////////////////////////////////////////////////////////
\r
43 //J メモリはスタックで管理されています。取得した順と逆に開放する必要があります。
\r
44 //J メモリを一気に開放したい場合はclear()を呼び出してください。
\r
45 //J 最小割り当てサイズはSCE_PFX_MIN_ALLOC_SIZEで定義されます。
\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
53 namespace PhysicsEffects {
\r
55 class PfxHeapManager
\r
59 PfxUInt8 *m_poolStack[SCE_PFX_HEAP_STACK_SIZE];
\r
60 PfxInt32 m_heapBytes;
\r
61 PfxInt32 m_curStack;
\r
65 enum {ALIGN16=16,ALIGN128=128};
\r
67 PfxHeapManager(PfxUInt8 *buf,PfxInt32 bytes)
\r
70 m_heapBytes = bytes;
\r
78 PfxInt32 getAllocated()
\r
80 return (PfxInt32)(m_poolStack[m_curStack]-m_heap);
\r
85 return m_heapBytes-getAllocated();
\r
88 void *allocate(size_t bytes,PfxInt32 alignment = ALIGN16)
\r
90 SCE_PFX_ALWAYS_ASSERT(m_curStack<SCE_PFX_HEAP_STACK_SIZE);
\r
92 bytes = SCE_PFX_MAX(bytes,SCE_PFX_MIN_ALLOC_SIZE);
\r
94 uintptr_t p = (uintptr_t)m_poolStack[m_curStack];
\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
101 p = (p+15) & SCE_PFX_ALIGN_MASK_16;
\r
102 bytes = (bytes+15) & SCE_PFX_ALIGN_MASK_16;
\r
105 SCE_PFX_ALWAYS_ASSERT_MSG(bytes <= (m_heapBytes-(p-(uintptr_t)m_heap)),"Memory overflow");
\r
107 m_poolStack[++m_curStack] = (PfxUInt8 *)(p + bytes);
\r
109 m_rest = getRest();
\r
114 void deallocate(void *p)
\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
128 m_poolStack[0] = m_heap;
\r
135 SCE_PFX_PRINTF("memStack %d/%d\n",m_curStack,SCE_PFX_HEAP_STACK_SIZE);
\r
139 } //namespace PhysicsEffects
\r
141 #endif // _SCE_PFX_HEAP_MANAGER_H
\r