Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / HeapManager.h
1 /*
2    Copyright (C) 2009 Sony Computer Entertainment Inc.
3    All rights reserved.
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14
15 */
16
17 #ifndef BT_HEAP_MANAGER_H__
18 #define BT_HEAP_MANAGER_H__
19
20 #ifdef __SPU__
21         #define HEAP_STACK_SIZE 32
22 #else
23         #define HEAP_STACK_SIZE 64
24 #endif
25
26 #define MIN_ALLOC_SIZE 16
27
28
29 class HeapManager
30 {
31 private:
32         ATTRIBUTE_ALIGNED16(unsigned char *mHeap);
33         ATTRIBUTE_ALIGNED16(unsigned int mHeapBytes);
34         ATTRIBUTE_ALIGNED16(unsigned char *mPoolStack[HEAP_STACK_SIZE]);
35         ATTRIBUTE_ALIGNED16(unsigned int mCurStack);
36         
37 public:
38         enum {ALIGN16,ALIGN128};
39
40         HeapManager(unsigned char *buf,int bytes)
41         {
42                 mHeap = buf;
43                 mHeapBytes = bytes;
44                 clear();
45         }
46         
47         ~HeapManager()
48         {
49         }
50         
51         int getAllocated()
52         {
53                 return (int)(mPoolStack[mCurStack]-mHeap);
54         }
55         
56         int getRest()
57         {
58                 return mHeapBytes-getAllocated();
59         }
60
61         void *allocate(size_t bytes,int alignment = ALIGN16)
62         {
63                 if(bytes <= 0) bytes = MIN_ALLOC_SIZE;
64                 btAssert(mCurStack < (HEAP_STACK_SIZE-1));
65
66                 
67 #if defined(_WIN64) || defined(__LP64__) || defined(__x86_64__)
68                 unsigned long long p = (unsigned long long )mPoolStack[mCurStack];
69                 if(alignment == ALIGN128) {
70                         p = ((p+127) & 0xffffffffffffff80);
71                         bytes = (bytes+127) & 0xffffffffffffff80;
72                 }
73                 else {
74                         bytes = (bytes+15) & 0xfffffffffffffff0;
75                 }
76
77                 btAssert(bytes <=(mHeapBytes-(p-(unsigned long long )mHeap)) );
78                 
79 #else
80                 unsigned long p = (unsigned long )mPoolStack[mCurStack];
81                 if(alignment == ALIGN128) {
82                         p = ((p+127) & 0xffffff80);
83                         bytes = (bytes+127) & 0xffffff80;
84                 }
85                 else {
86                         bytes = (bytes+15) & 0xfffffff0;
87                 }
88                 btAssert(bytes <=(mHeapBytes-(p-(unsigned long)mHeap)) );
89 #endif
90                 unsigned char * bla = (unsigned char *)(p + bytes);
91                 mPoolStack[++mCurStack] = bla;
92                 return (void*)p;
93         }
94
95         void deallocate(void *p)
96         {
97                 (void) p;
98                 mCurStack--;
99         }
100         
101         void clear()
102         {
103                 mPoolStack[0] = mHeap;
104                 mCurStack = 0;
105         }
106
107 //      void printStack()
108 //      {
109 //              for(unsigned int i=0;i<=mCurStack;i++) {
110 //                      PRINTF("memStack %2d 0x%x\n",i,(uint32_t)mPoolStack[i]);
111 //              }
112 //      }
113
114 };
115
116 #endif //BT_HEAP_MANAGER_H__
117