Eneboo - Documentación para desarrolladores
|
00001 #ifndef _BUMPALLOC_H_ 00002 #define _BUMPALLOC_H_ 00003 00010 namespace Hoard { 00011 00012 template <int ChunkSize, 00013 class super> 00014 class BumpAlloc : public super { 00015 public: 00016 00017 BumpAlloc (void) 00018 : _bump (NULL), 00019 _remaining (0) 00020 {} 00021 00022 inline void * malloc (size_t sz) { 00023 // If there's not enough space left to fulfill this request, get 00024 // another chunk. 00025 if (_remaining < sz) { 00026 refill(sz); 00027 } 00028 if (_bump) { 00029 char * old = _bump; 00030 _bump += sz; 00031 _remaining -= sz; 00032 return old; 00033 } else { 00034 // We were unable to get memory. 00035 return NULL; 00036 } 00037 } 00038 00040 inline void free (void *) {} 00041 00042 private: 00043 00045 char * _bump; 00046 00048 size_t _remaining; 00049 00050 // Get another chunk. 00051 void refill (size_t sz) { 00052 // Always get at least a ChunkSize worth of memory. 00053 if (sz < ChunkSize) { 00054 sz = ChunkSize; 00055 } 00056 _bump = (char *) super::malloc (sz); 00057 if (_bump) { 00058 _remaining = sz; 00059 } else { 00060 _remaining = 0; 00061 } 00062 } 00063 00064 }; 00065 00066 } 00067 00068 #endif