segmented_string_pool.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. last_free = (len > segment_size ? len : segment_size);
  50. if (curseg == segs.end() || curseg->_size < last_free) {
  51. segs.push_back(seg_inf(seg_allocator.allocate(last_free), last_free));
  52. if (Y_UNLIKELY(Name))
  53. printf("Pool \"%s\" was increased by %" PRISZT " bytes to %" PRISZT " Mb.\n", Name, last_free * sizeof(T), capacity() / 0x100000);
  54. curseg = segs.end() - 1;
  55. }
  56. Y_ASSERT(curseg->freepos == 0);
  57. Y_ASSERT(curseg->_size >= last_free);
  58. }
  59. }
  60. public:
  61. explicit segmented_pool(size_t segsz, const char* name = nullptr)
  62. : segment_size(segsz)
  63. , last_free(0)
  64. , last_ins_size(0)
  65. , Name(name)
  66. {
  67. curseg = segs.begin();
  68. }
  69. ~segmented_pool() {
  70. clear();
  71. }
  72. /* src - array of objects, len - count of elements in array */
  73. T* append(const T* src, size_t len) {
  74. check_capacity(len);
  75. ui8* rv = (ui8*)curseg->data + curseg->freepos;
  76. last_ins_size = sizeof(T) * len;
  77. if (src)
  78. memcpy(rv, src, last_ins_size);
  79. curseg->freepos += last_ins_size, last_free -= len;
  80. return (T*)rv;
  81. }
  82. T* append() {
  83. T* obj = get_raw();
  84. new (obj) T();
  85. return obj;
  86. }
  87. T* get_raw() { // append(0, 1)
  88. check_capacity(1);
  89. ui8* rv = (ui8*)curseg->data + curseg->freepos;
  90. last_ins_size = sizeof(T);
  91. curseg->freepos += last_ins_size, last_free -= 1;
  92. return (T*)rv;
  93. }
  94. size_t get_segment_size() const {
  95. return segment_size;
  96. }
  97. bool contains(const T* ptr) const {
  98. for (seg_const_iterator i = segs.begin(), ie = segs.end(); i != ie; ++i)
  99. if ((char*)ptr >= (char*)i->data && (char*)ptr < (char*)i->data + i->freepos)
  100. return true;
  101. return false;
  102. }
  103. size_t size() const {
  104. size_t r = 0;
  105. for (seg_const_iterator i = segs.begin(); i != segs.end(); ++i)
  106. r += i->freepos;
  107. return r;
  108. }
  109. size_t capacity() const {
  110. return segs.size() * segment_size * sizeof(T);
  111. }
  112. void restart() {
  113. if (curseg != segs.end())
  114. ++curseg;
  115. for (seg_iterator i = segs.begin(); i != curseg; ++i)
  116. i->freepos = 0;
  117. curseg = segs.begin();
  118. last_free = 0;
  119. last_ins_size = 0;
  120. }
  121. void clear() {
  122. for (seg_iterator i = segs.begin(); i != segs.end(); ++i)
  123. seg_allocator.deallocate(i->data, i->_size);
  124. segs.clear();
  125. curseg = segs.begin();
  126. last_free = 0;
  127. last_ins_size = 0;
  128. }
  129. void undo_last_append() {
  130. Y_ASSERT(curseg != segs.end()); // do not use before append()
  131. if (last_ins_size) {
  132. Y_ASSERT(last_ins_size <= curseg->freepos);
  133. curseg->freepos -= last_ins_size;
  134. last_free += last_ins_size / sizeof(T);
  135. last_ins_size = 0;
  136. }
  137. }
  138. void alloc_first_seg() {
  139. Y_ASSERT(capacity() == 0);
  140. check_capacity(segment_size);
  141. Y_ASSERT(capacity() == segment_size * sizeof(T));
  142. }
  143. };
  144. class segmented_string_pool: public segmented_pool<char> {
  145. private:
  146. using _Base = segmented_pool<char>;
  147. public:
  148. segmented_string_pool()
  149. : segmented_string_pool(1024 * 1024)
  150. {
  151. }
  152. explicit segmented_string_pool(size_t segsz)
  153. : _Base(segsz)
  154. {
  155. }
  156. char* append(const char* src) {
  157. Y_ASSERT(src);
  158. return _Base::append(src, strlen(src) + 1);
  159. }
  160. char* append(const char* src, size_t len) {
  161. char* rv = _Base::append(nullptr, len + 1);
  162. if (src)
  163. memcpy(rv, src, len);
  164. rv[len] = 0;
  165. return rv;
  166. }
  167. char* Append(const TStringBuf s) {
  168. return append(s.data(), s.size());
  169. }
  170. void align_4() {
  171. size_t t = (curseg->freepos + 3) & ~3;
  172. last_free -= t - curseg->freepos;
  173. curseg->freepos = t;
  174. }
  175. char* Allocate(size_t len) {
  176. return append(nullptr, len);
  177. }
  178. };
  179. template <typename T, typename C>
  180. inline T* pool_push(segmented_pool<C>& pool, const T* v) {
  181. static_assert(sizeof(C) == 1, "only char type supported");
  182. size_t len = SizeOf(v);
  183. C* buf = pool.append(nullptr, AlignUp(len));
  184. memcpy(buf, v, len);
  185. return (T*)buf;
  186. }