Bullet Collision Detection & Physics Library
HeapManager.h
Go to the documentation of this file.
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 
30 {
31 private:
32  ATTRIBUTE_ALIGNED16(unsigned char *mHeap);
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 
48  {
49  }
50 
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;
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 
unsigned int mCurStack
Definition: HeapManager.h:35
void clear()
Definition: HeapManager.h:101
#define btAssert(x)
Definition: btScalar.h:101
int getRest()
Definition: HeapManager.h:56
unsigned char * mHeap
Definition: HeapManager.h:32
#define MIN_ALLOC_SIZE
Definition: HeapManager.h:26
unsigned int mHeapBytes
Definition: HeapManager.h:33
void * allocate(size_t bytes, int alignment=ALIGN16)
Definition: HeapManager.h:61
int getAllocated()
Definition: HeapManager.h:51
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:59
void deallocate(void *p)
Definition: HeapManager.h:95
#define HEAP_STACK_SIZE
Definition: HeapManager.h:23
HeapManager(unsigned char *buf, int bytes)
Definition: HeapManager.h:40
unsigned char * mPoolStack[64]
Definition: HeapManager.h:34