| 56 | struct PagedOps |
| 57 | { |
| 58 | static void Resize(uint32_t newSize, T**& pages, PageLayout& layout, TAllocator& allocator) |
| 59 | { |
| 60 | if (newSize) { |
| 61 | auto bucketSize = (int32_t)layout.bucket_size(); |
| 62 | auto newNumBuckets = (uint32_t)((-bucketSize & (bucketSize + newSize - 1)) >> layout.BitsPerPage); |
| 63 | if (newNumBuckets != layout.NumPages) { |
| 64 | for (auto i = newNumBuckets; i < layout.NumPages; i++) { |
| 65 | allocator.free(pages[i]); |
| 66 | } |
| 67 | |
| 68 | auto newAllocBuckets = (newNumBuckets + 15u) & 0xFFF0u; |
| 69 | auto oldAllocBuckets = (layout.NumPages + 15u) & 0xFFF0u; |
| 70 | if (oldAllocBuckets < newAllocBuckets) { |
| 71 | auto newBuckets = allocator.allocArray<T*>(newAllocBuckets); |
| 72 | if (pages) { |
| 73 | memcpy(newBuckets, pages, sizeof(T*) * std::min(newNumBuckets, (uint32_t)layout.NumPages)); |
| 74 | allocator.free(pages); |
| 75 | } |
| 76 | pages = newBuckets; |
| 77 | } |
| 78 | |
| 79 | for (auto i = layout.NumPages; i < newNumBuckets; i++) { |
| 80 | pages[i] = static_cast<T*>(allocator.alloc(sizeof(T) * bucketSize)); |
| 81 | } |
| 82 | |
| 83 | layout.NumPages = newNumBuckets; |
| 84 | } |
| 85 | } else { |
| 86 | if (layout.NumPages) { |
| 87 | for (auto i = 0; i < layout.NumPages; i++) { |
| 88 | allocator.free(pages[i]); |
| 89 | } |
| 90 | |
| 91 | if (pages != nullptr) { |
| 92 | allocator.free(pages); |
| 93 | } |
| 94 | |
| 95 | layout.NumPages = 0; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | template <class T, class TAllocator = DefaultPagedAllocator> |
no test coverage detected