• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

DS_BytePool.cpp

Go to the documentation of this file.
00001 #include "DS_BytePool.h"
00002 #include "RakAssert.h"
00003 #ifndef __APPLE__
00004 // Use stdlib and not malloc for compatibility
00005 #include <stdlib.h>
00006 #endif
00007 
00008 using namespace DataStructures;
00009 
00010 BytePool::BytePool()
00011 {
00012         pool128.SetPageSize(8192*4);
00013         pool512.SetPageSize(8192*4);
00014         pool2048.SetPageSize(8192*4);
00015         pool8192.SetPageSize(8192*4);
00016 }
00017 BytePool::~BytePool()
00018 {
00019 }
00020 void BytePool::SetPageSize(int size)
00021 {
00022         pool128.SetPageSize(size);
00023         pool512.SetPageSize(size);
00024         pool2048.SetPageSize(size);
00025         pool8192.SetPageSize(size);
00026 }
00027 unsigned char *BytePool::Allocate(int bytesWanted, const char *file, unsigned int line)
00028 {
00029 #ifdef _DISABLE_BYTE_POOL
00030         return rakMalloc_Ex(bytesWanted, __FILE__, __LINE__);
00031 #endif
00032         unsigned char *out;
00033         if (bytesWanted <= 127)
00034         {
00035                 #ifdef _THREADSAFE_BYTE_POOL
00036                 mutex128.Lock();
00037                 #endif
00038                 out = (unsigned char*) pool128.Allocate(file, line);
00039                 #ifdef _THREADSAFE_BYTE_POOL
00040                 mutex128.Unlock();
00041                 #endif
00042                 out[0]=0;
00043                 return ((unsigned char*) out)+1;
00044         }
00045         if (bytesWanted <= 511)
00046         {
00047                 #ifdef _THREADSAFE_BYTE_POOL
00048                 mutex512.Lock();
00049                 #endif
00050                 out = (unsigned char*) pool512.Allocate(file, line);
00051                 #ifdef _THREADSAFE_BYTE_POOL
00052                 mutex512.Unlock();
00053                 #endif
00054                 out[0]=1;
00055                 return ((unsigned char*) out)+1;
00056         }
00057         if (bytesWanted <= 2047)
00058         {
00059                 #ifdef _THREADSAFE_BYTE_POOL
00060                 mutex2048.Lock();
00061                 #endif
00062                 out = (unsigned char*) pool2048.Allocate(file, line);
00063                 #ifdef _THREADSAFE_BYTE_POOL
00064                 mutex2048.Unlock();
00065                 #endif
00066                 out[0]=2;
00067                 return ((unsigned char*) out)+1;
00068         }
00069         if (bytesWanted <= 8191)
00070         {
00071                 #ifdef _THREADSAFE_BYTE_POOL
00072                 mutex8192.Lock();
00073                 #endif
00074                 out = (unsigned char*) pool8192.Allocate(file, line);
00075                 #ifdef _THREADSAFE_BYTE_POOL
00076                 mutex8192.Unlock();
00077                 #endif
00078                 out[0]=3;
00079                 return ((unsigned char*) out)+1;
00080         }
00081 
00082         out = (unsigned char*) rakMalloc_Ex(bytesWanted+1, __FILE__, __LINE__);
00083         out[0]=(unsigned char)255;
00084         return out+1;
00085 }
00086 void BytePool::Release(unsigned char *data, const char *file, unsigned int line)
00087 {
00088 #ifdef _DISABLE_BYTE_POOL
00089         _rakFree_Ex(data, __FILE__, __LINE__ );
00090 #endif
00091         unsigned char *realData = data-1;
00092         switch (realData[0])
00093         {
00094         case 0:
00095                 #ifdef _THREADSAFE_BYTE_POOL
00096                 mutex128.Lock();
00097                 #endif
00098                 pool128.Release((unsigned char(*)[128]) realData, file, line );
00099                 #ifdef _THREADSAFE_BYTE_POOL
00100                 mutex128.Unlock();
00101                 #endif
00102                 break;
00103         case 1:
00104                 #ifdef _THREADSAFE_BYTE_POOL
00105                 mutex512.Lock();
00106                 #endif
00107                 pool512.Release((unsigned char(*)[512]) realData, file, line );
00108                 #ifdef _THREADSAFE_BYTE_POOL
00109                 mutex512.Unlock();
00110                 #endif
00111                 break;
00112         case 2:
00113                 #ifdef _THREADSAFE_BYTE_POOL
00114                 mutex2048.Lock();
00115                 #endif
00116                 pool2048.Release((unsigned char(*)[2048]) realData, file, line );
00117                 #ifdef _THREADSAFE_BYTE_POOL
00118                 mutex2048.Unlock();
00119                 #endif
00120                 break;
00121         case 3:
00122                 #ifdef _THREADSAFE_BYTE_POOL
00123                 mutex8192.Lock();
00124                 #endif
00125                 pool8192.Release((unsigned char(*)[8192]) realData, file, line );
00126                 #ifdef _THREADSAFE_BYTE_POOL
00127                 mutex8192.Unlock();
00128                 #endif
00129                 break;
00130         case 255:
00131                 rakFree_Ex(realData, file, line );
00132                 break;
00133         default:
00134                 RakAssert(0);
00135                 break;
00136         }
00137 }
00138 void BytePool::Clear(const char *file, unsigned int line)
00139 {
00140         (void) file;
00141         (void) line;
00142 
00143 #ifdef _THREADSAFE_BYTE_POOL
00144         pool128.Clear(file, line);
00145         pool512.Clear(file, line);
00146         pool2048.Clear(file, line);
00147         pool8192.Clear(file, line);
00148 #endif
00149 }

Generated on Thu Sep 30 2010 01:27:22 for RakNet by  doxygen 1.7.1