segmented_string_pool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include <util/system/align.h>
  3. #include <util/system/yassert.h>
  4. #include <util/system/defaults.h>
  5. #include <util/generic/noncopyable.h>
  6. #include <util/generic/vector.h>
  7. #include <util/generic/strbuf.h>
  8. #include <memory>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. /*
  12. * Non-reallocated storage for the objects of POD type
  13. */
  14. template <class T, class Alloc = std::allocator<T>>
  15. class segmented_pool: TNonCopyable {
  16. protected:
  17. Alloc seg_allocator;
  18. struct seg_inf {
  19. T* data; // allocated chunk
  20. size_t _size; // size of allocated chunk in sizeof(T)-units
  21. size_t freepos; // offset to free chunk's memory in bytes
  22. seg_inf()
  23. : data(nullptr)
  24. , _size(0)
  25. , freepos(0)
  26. {
  27. }
  28. seg_inf(T* d, size_t sz)
  29. : data(d)
  30. , _size(sz)
  31. , freepos(0)
  32. {
  33. }
  34. };
  35. using seg_container = TVector<seg_inf>;
  36. using seg_iterator = typename seg_container::iterator;
  37. using seg_const_iterator = typename seg_container::const_iterator;
  38. const size_t segment_size; // default size of a memory chunk in sizeof(T)-units
  39. size_t last_free; // size of free memory in chunk in sizeof(T)-units
  40. size_t last_ins_size; // size of memory used in chunk by the last append() in bytes
  41. seg_container segs; // array of memory chunks
  42. seg_iterator curseg; // a segment for the current insertion
  43. const char* Name; // for debug memory usage
  44. protected:
  45. void check_capacity(size_t len) {
  46. if (Y_UNLIKELY(!last_free || len > last_free)) {
  47. if (curseg != segs.end() && curseg->freepos > 0) {
  48. ++curseg;
  49. }
  50. last_free = (len > segment_size ? len : segment_size);
  51. if (curseg == segs.end() || curseg->_size < last_free) {
  52. segs.push_back(seg_inf(seg_allocator.allocate(last_free), last_free));
  53. if (Y_UNLIKELY(Name)) {
  54. printf("Pool \"%s\" was increased by %" PRISZT " bytes to %" PRISZT " Mb.\n", Name, last_free * sizeof(T), capacity() / 0x100000);
  55. }
  56. curseg = segs.end() - 1;
  57. }
  58. Y_ASSERT(curseg->freepos == 0);
  59. Y_ASSERT(curseg->_size >= last_free);
  60. }
  61. }
  62. public:
  63. explicit segmented_pool(size_t segsz, const char* name = nullptr)
  64. : segment_size(segsz)
  65. , last_free(0)
  66. , last_ins_size(0)
  67. , Name(name)
  68. {
  69. curseg = segs.begin();
  70. }
  71. ~segmented_pool() {
  72. clear();
  73. }
  74. /* src - array of objects, len - count of elements in array */
  75. T* append(const T* src, size_t len) {
  76. check_capacity(len);
  77. ui8* rv = (ui8*)curseg->data + curseg->freepos;
  78. last_ins_size = sizeof(T) * len;
  79. if (src) {
  80. memcpy(rv, src, last_ins_size);
  81. }
  82. curseg->freepos += last_ins_size, last_free -= len;
  83. return (T*)rv;
  84. }
  85. T* append() {
  86. T* obj = get_raw();
  87. new (obj) T();
  88. return obj;
  89. }
  90. T* get_raw() { // append(0, 1)
  91. check_capacity(1);
  92. ui8* rv = (ui8*)curseg->data + curseg->freepos;
  93. last_ins_size = sizeof(T);
  94. curseg->freepos += last_ins_size, last_free -= 1;
  95. return (T*)rv;
  96. }
  97. size_t get_segment_size() const {
  98. return segment_size;
  99. }
  100. bool contains(const T* ptr) const {
  101. for (seg_const_iterator i = segs.begin(), ie = segs.end(); i != ie; ++i) {
  102. if ((char*)ptr >= (char*)i->data && (char*)ptr < (char*)i->data + i->freepos) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. size_t size() const {
  109. size_t r = 0;
  110. for (seg_const_iterator i = segs.begin(); i != segs.end(); ++i) {
  111. r += i->freepos;
  112. }
  113. return r;
  114. }
  115. size_t capacity() const {
  116. return segs.size() * segment_size * sizeof(T);
  117. }
  118. void restart() {
  119. if (curseg != segs.end()) {
  120. ++curseg;
  121. }
  122. for (seg_iterator i = segs.begin(); i != curseg; ++i) {
  123. i->freepos = 0;
  124. }
  125. curseg = segs.begin();
  126. last_free = 0;
  127. last_ins_size = 0;
  128. }
  129. void clear() {
  130. for (seg_iterator i = segs.begin(); i != segs.end(); ++i) {
  131. seg_allocator.deallocate(i->data, i->_size);
  132. }
  133. segs.clear();
  134. curseg = segs.begin();
  135. last_free = 0;
  136. last_ins_size = 0;
  137. }
  138. void undo_last_append() {
  139. Y_ASSERT(curseg != segs.end()); // do not use before append()
  140. if (last_ins_size) {
  141. Y_ASSERT(last_ins_size <= curseg->freepos);
  142. curseg->freepos -= last_ins_size;
  143. last_free += last_ins_size / sizeof(T);
  144. last_ins_size = 0;
  145. }
  146. }
  147. void alloc_first_seg() {
  148. Y_ASSERT(capacity() == 0);
  149. check_capacity(segment_size);
  150. Y_ASSERT(capacity() == segment_size * sizeof(T));
  151. }
  152. };
  153. class segmented_string_pool: public segmented_pool<char> {
  154. private:
  155. using _Base = segmented_pool<char>;
  156. public:
  157. segmented_string_pool()
  158. : segmented_string_pool(1024 * 1024)
  159. {
  160. }
  161. explicit segmented_string_pool(size_t segsz)
  162. : _Base(segsz)
  163. {
  164. }
  165. char* append(const char* src) {
  166. Y_ASSERT(src);
  167. return _Base::append(src, strlen(src) + 1);
  168. }
  169. char* append(const char* src, size_t len) {
  170. char* rv = _Base::append(nullptr, len + 1);
  171. if (src) {
  172. memcpy(rv, src, len);
  173. }
  174. rv[len] = 0;
  175. return rv;
  176. }
  177. char* Append(const TStringBuf s) {
  178. return append(s.data(), s.size());
  179. }
  180. void align_4() {
  181. size_t t = (curseg->freepos + 3) & ~3;
  182. last_free -= t - curseg->freepos;
  183. curseg->freepos = t;
  184. }
  185. char* Allocate(size_t len) {
  186. return append(nullptr, len);
  187. }
  188. };
  189. template <typename T, typename C>
  190. inline T* pool_push(segmented_pool<C>& pool, const T* v) {
  191. static_assert(sizeof(C) == 1, "only char type supported");
  192. size_t len = SizeOf(v);
  193. C* buf = pool.append(nullptr, AlignUp(len));
  194. memcpy(buf, v, len);
  195. return (T*)buf;
  196. }