00001 #ifndef __MEM_POOL__ 00002 #define __MEM_POOL__ 00003 #include <stdint.h> 00004 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1)) 00005 00011 class mem_pool 00012 { 00013 public: 00014 mem_pool() : mem_(0), maxsize_(0), curpos_(0) {} 00015 00016 virtual ~mem_pool() {} 00017 00025 virtual bool init(unsigned long maxsize) = 0; 00026 00034 void * alloc(unsigned long size) 00035 { 00036 if(size+curpos_ > maxsize_) 00037 return 0; 00038 curpos_ = ALIGN(curpos_, 64); 00039 void * ret = (void *) (mem_ + curpos_); 00040 curpos_ += size; 00041 curpos_ = ALIGN(curpos_, 64); 00042 return ret; 00043 } 00044 00049 __inline__ void reset() 00050 { 00051 curpos_ = 0; 00052 } 00053 00060 __inline__ unsigned long get_curpos() { return curpos_; } 00061 00067 __inline__ void* get_mem() { return mem_; } 00068 00069 protected: 00070 uint8_t * mem_; 00071 unsigned long maxsize_; 00072 unsigned long curpos_; 00073 }; 00074 00075 #undef ALIGN 00076 00077 #endif