Joshua
open source statistical hierarchical phrase-based machine translation system
|
00001 // Very simple pool. It can only allocate memory. And all of the memory it 00002 // allocates must be freed at the same time. 00003 00004 #ifndef UTIL_POOL_H 00005 #define UTIL_POOL_H 00006 00007 #include <vector> 00008 #include <stdint.h> 00009 00010 namespace util { 00011 00012 class Pool { 00013 public: 00014 Pool(); 00015 00016 ~Pool(); 00017 00018 void *Allocate(std::size_t size) { 00019 void *ret = current_; 00020 current_ += size; 00021 if (current_ < current_end_) { 00022 return ret; 00023 } else { 00024 return More(size); 00025 } 00026 } 00027 00028 void FreeAll(); 00029 00030 private: 00031 void *More(std::size_t size); 00032 00033 std::vector<void *> free_list_; 00034 00035 uint8_t *current_, *current_end_; 00036 00037 // no copying 00038 Pool(const Pool &); 00039 Pool &operator=(const Pool &); 00040 }; 00041 00042 } // namespace util 00043 00044 #endif // UTIL_POOL_H