HeapManager.h

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2009 Sony Computer Entertainment Inc.
00003    All rights reserved.
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 
00015 */
00016 
00017 #ifndef BT_HEAP_MANAGER_H__
00018 #define BT_HEAP_MANAGER_H__
00019 
00020 #ifdef __SPU__
00021         #define HEAP_STACK_SIZE 32
00022 #else
00023         #define HEAP_STACK_SIZE 64
00024 #endif
00025 
00026 #define MIN_ALLOC_SIZE 16
00027 
00028 
00029 class HeapManager
00030 {
00031 private:
00032         ATTRIBUTE_ALIGNED16(unsigned char *mHeap);
00033         ATTRIBUTE_ALIGNED16(unsigned int mHeapBytes);
00034         ATTRIBUTE_ALIGNED16(unsigned char *mPoolStack[HEAP_STACK_SIZE]);
00035         ATTRIBUTE_ALIGNED16(unsigned int mCurStack);
00036         
00037 public:
00038         enum {ALIGN16,ALIGN128};
00039 
00040         HeapManager(unsigned char *buf,int bytes)
00041         {
00042                 mHeap = buf;
00043                 mHeapBytes = bytes;
00044                 clear();
00045         }
00046         
00047         ~HeapManager()
00048         {
00049         }
00050         
00051         int getAllocated()
00052         {
00053                 return (int)(mPoolStack[mCurStack]-mHeap);
00054         }
00055         
00056         int getRest()
00057         {
00058                 return mHeapBytes-getAllocated();
00059         }
00060 
00061         void *allocate(size_t bytes,int alignment = ALIGN16)
00062         {
00063                 if(bytes <= 0) bytes = MIN_ALLOC_SIZE;
00064                 btAssert(mCurStack < (HEAP_STACK_SIZE-1));
00065 
00066                 
00067 #if defined(_WIN64) || defined(__LP64__) || defined(__x86_64__)
00068                 unsigned long long p = (unsigned long long )mPoolStack[mCurStack];
00069                 if(alignment == ALIGN128) {
00070                         p = ((p+127) & 0xffffffffffffff80);
00071                         bytes = (bytes+127) & 0xffffffffffffff80;
00072                 }
00073                 else {
00074                         bytes = (bytes+15) & 0xfffffffffffffff0;
00075                 }
00076 
00077                 btAssert(bytes <=(mHeapBytes-(p-(unsigned long long )mHeap)) );
00078                 
00079 #else
00080                 unsigned long p = (unsigned long )mPoolStack[mCurStack];
00081                 if(alignment == ALIGN128) {
00082                         p = ((p+127) & 0xffffff80);
00083                         bytes = (bytes+127) & 0xffffff80;
00084                 }
00085                 else {
00086                         bytes = (bytes+15) & 0xfffffff0;
00087                 }
00088                 btAssert(bytes <=(mHeapBytes-(p-(unsigned long)mHeap)) );
00089 #endif
00090                 unsigned char * bla = (unsigned char *)(p + bytes);
00091                 mPoolStack[++mCurStack] = bla;
00092                 return (void*)p;
00093         }
00094 
00095         void deallocate(void *p)
00096         {
00097                 (void) p;
00098                 mCurStack--;
00099         }
00100         
00101         void clear()
00102         {
00103                 mPoolStack[0] = mHeap;
00104                 mCurStack = 0;
00105         }
00106 
00107 //      void printStack()
00108 //      {
00109 //              for(unsigned int i=0;i<=mCurStack;i++) {
00110 //                      PRINTF("memStack %2d 0x%x\n",i,(uint32_t)mPoolStack[i]);
00111 //              }
00112 //      }
00113 
00114 };
00115 
00116 #endif //BT_HEAP_MANAGER_H__
00117