2 Copyright (C) 2009 Sony Computer Entertainment Inc.
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:
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.
17 #ifndef BT_HEAP_MANAGER_H__
18 #define BT_HEAP_MANAGER_H__
21 #define HEAP_STACK_SIZE 32
23 #define HEAP_STACK_SIZE 64
26 #define MIN_ALLOC_SIZE 16
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);
38 enum {ALIGN16,ALIGN128};
40 HeapManager(unsigned char *buf,int bytes)
53 return (int)(mPoolStack[mCurStack]-mHeap);
58 return mHeapBytes-getAllocated();
61 void *allocate(size_t bytes,int alignment = ALIGN16)
63 if(bytes <= 0) bytes = MIN_ALLOC_SIZE;
64 btAssert(mCurStack < (HEAP_STACK_SIZE-1));
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;
74 bytes = (bytes+15) & 0xfffffffffffffff0;
77 btAssert(bytes <=(mHeapBytes-(p-(unsigned long long )mHeap)) );
80 unsigned long p = (unsigned long )mPoolStack[mCurStack];
81 if(alignment == ALIGN128) {
82 p = ((p+127) & 0xffffff80);
83 bytes = (bytes+127) & 0xffffff80;
86 bytes = (bytes+15) & 0xfffffff0;
88 btAssert(bytes <=(mHeapBytes-(p-(unsigned long)mHeap)) );
90 unsigned char * bla = (unsigned char *)(p + bytes);
91 mPoolStack[++mCurStack] = bla;
95 void deallocate(void *p)
103 mPoolStack[0] = mHeap;
109 // for(unsigned int i=0;i<=mCurStack;i++) {
110 // PRINTF("memStack %2d 0x%x\n",i,(uint32_t)mPoolStack[i]);
116 #endif //BT_HEAP_MANAGER_H__