memory.h
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// FST memory utilities.
#ifndef FST_MEMORY_H_
#define FST_MEMORY_H_
#include <list>
#include <memory>
#include <utility>
#include <vector>
#include <fst/types.h>
#include <fst/log.h>
#include <fstream>
namespace fst {
// Default block allocation size.
constexpr int kAllocSize = 64;
// Minimum number of allocations per block.
constexpr int kAllocFit = 4;
// Base class for MemoryArena that allows (e.g.) MemoryArenaCollection to
// easily manipulate collections of variously sized arenas.
class MemoryArenaBase {
public:
virtual ~MemoryArenaBase() {}
virtual size_t Size() const = 0;
};
namespace internal {
// Allocates 'size' unintialized memory chunks of size object_size from
// underlying blocks of (at least) size 'block_size * object_size'.
// All blocks are freed when this class is deleted. Result of allocate() will
// be aligned to object_size.
template <size_t object_size>
class MemoryArenaImpl : public MemoryArenaBase {
public:
enum { kObjectSize = object_size };
explicit MemoryArenaImpl(size_t block_size = kAllocSize)
: block_size_(block_size * kObjectSize), block_pos_(0) {
blocks_.emplace_front(new char[block_size_]);
}
void *Allocate(size_t size) {
const auto byte_size = size * kObjectSize;
if (byte_size * kAllocFit > block_size_) {
// Large block; adds new large block.
auto *ptr = new char[byte_size];
blocks_.emplace_back(ptr);
return ptr;
}
if (block_pos_ + byte_size > block_size_) {
// Doesn't fit; adds new standard block.
auto *ptr = new char[block_size_];
block_pos_ = 0;
blocks_.emplace_front(ptr);
}
// Fits; uses current block.
auto *ptr = blocks_.front().get() + block_pos_;
block_pos_ += byte_size;
return ptr;
}
size_t Size() const override { return kObjectSize; }
private:
const size_t block_size_; // Default block size in bytes.
size_t block_pos_; // Current position in block in bytes.
std::list<std::unique_ptr<char[]>> blocks_; // List of allocated blocks.
};
} // namespace internal
template <typename T>
using MemoryArena = internal::MemoryArenaImpl<sizeof(T)>;
// Base class for MemoryPool that allows (e.g.) MemoryPoolCollection to easily
// manipulate collections of variously sized pools.
class MemoryPoolBase {
public:
virtual ~MemoryPoolBase() {}
virtual size_t Size() const = 0;
};
namespace internal {
// Allocates and frees initially uninitialized memory chunks of size
// object_size. Keeps an internal list of freed chunks that are reused (as is)
// on the next allocation if available. Chunks are constructed in blocks of size
// 'pool_size'.
template <size_t object_size>
class MemoryPoolImpl : public MemoryPoolBase {
public:
enum { kObjectSize = object_size };
struct Link {
char buf[kObjectSize];
Link *next;
};
explicit MemoryPoolImpl(size_t pool_size)
: mem_arena_(pool_size), free_list_(nullptr) {}
void *Allocate() {
if (free_list_ == nullptr) {
auto *link = static_cast<Link *>(mem_arena_.Allocate(1));
link->next = nullptr;
return link;
} else {
auto *link = free_list_;
free_list_ = link->next;
return link;
}
}
void Free(void *ptr) {
if (ptr) {
auto *link = static_cast<Link *>(ptr);
link->next = free_list_;
free_list_ = link;
}
}
size_t Size() const override { return kObjectSize; }
private:
MemoryArena<Link> mem_arena_;
Link *free_list_;
MemoryPoolImpl(const MemoryPoolImpl &) = delete;
MemoryPoolImpl &operator=(const MemoryPoolImpl &) = delete;
};
} // namespace internal
// Allocates and frees initially uninitialized memory chunks of size sizeof(T).
// All memory is freed when the class is deleted. The result of Allocate() will
// be suitably memory-aligned. Combined with placement operator new and destroy
// functions for the T class, this can be used to improve allocation efficiency.
// See nlp/fst/lib/visit.h (global new) and nlp/fst/lib/dfs-visit.h (class new)
// for examples.
template <typename T>
class MemoryPool : public internal::MemoryPoolImpl<sizeof(T)> {
public:
// 'pool_size' specifies the size of the initial pool and how it is extended.
MemoryPool(size_t pool_size = kAllocSize)
: internal::MemoryPoolImpl<sizeof(T)>(pool_size) {}
};
// Stores a collection of memory arenas.
class MemoryArenaCollection {
public:
// 'block_size' specifies the block size of the arenas.
explicit MemoryArenaCollection(size_t block_size = kAllocSize)
: block_size_(block_size), ref_count_(1) {}
template <typename T>
MemoryArena<T> *Arena() {
if (sizeof(T) >= arenas_.size()) arenas_.resize(sizeof(T) + 1);
MemoryArenaBase *arena = arenas_[sizeof(T)].get();
if (arena == nullptr) {
arena = new MemoryArena<T>(block_size_);
arenas_[sizeof(T)].reset(arena);
}
return static_cast<MemoryArena<T> *>(arena);
}
size_t BlockSize() const { return block_size_; }
size_t RefCount() const { return ref_count_; }
size_t IncrRefCount() { return ++ref_count_; }
size_t DecrRefCount() { return --ref_count_; }
private:
size_t block_size_;
size_t ref_count_;
std::vector<std::unique_ptr<MemoryArenaBase>> arenas_;
};
// Stores a collection of memory pools
class MemoryPoolCollection {
public:
// 'pool_size' specifies the size of initial pool and how it is extended.
explicit MemoryPoolCollection(size_t pool_size = kAllocSize)
: pool_size_(pool_size), ref_count_(1) {}
template <typename T>
MemoryPool<T> *Pool() {
if (sizeof(T) >= pools_.size()) pools_.resize(sizeof(T) + 1);
MemoryPoolBase *pool = pools_[sizeof(T)].get();
if (pool == nullptr) {
pool = new MemoryPool<T>(pool_size_);
pools_[sizeof(T)].reset(pool);
}
return static_cast<MemoryPool<T> *>(pool);
}
size_t PoolSize() const { return pool_size_; }
size_t RefCount() const { return ref_count_; }
size_t IncrRefCount() { return ++ref_count_; }
size_t DecrRefCount() { return --ref_count_; }
private:
size_t pool_size_;
size_t ref_count_;
std::vector<std::unique_ptr<MemoryPoolBase>> pools_;
};
// STL allocator using memory arenas. Memory is allocated from underlying
// blocks of size 'block_size * sizeof(T)'. Memory is freed only when all
// objects using this allocator are destroyed and there is otherwise no reuse
// (unlike PoolAllocator).
//
// This allocator has object-local state so it should not be used with splicing
// or swapping operations between objects created with different allocators nor
// should it be used if copies must be thread-safe. The result of allocate()
// will be suitably memory-aligned.
template <typename T>
class BlockAllocator {
public:
using Allocator = std::allocator<T>;
using size_type = typename Allocator::size_type;
using difference_type = typename Allocator::difference_type;
using pointer = typename Allocator::pointer;
using const_pointer = typename Allocator::const_pointer;
using reference = typename Allocator::reference;
using const_reference = typename Allocator::const_reference;
using value_type = typename Allocator::value_type;
template <typename U>
struct rebind {
using other = BlockAllocator<U>;
};
explicit BlockAllocator(size_t block_size = kAllocSize)
: arenas_(new MemoryArenaCollection(block_size)) {}
BlockAllocator(const BlockAllocator<T> &arena_alloc)
: arenas_(arena_alloc.Arenas()) {
Arenas()->IncrRefCount();
}
template <typename U>
explicit BlockAllocator(const BlockAllocator<U> &arena_alloc)
: arenas_(arena_alloc.Arenas()) {
Arenas()->IncrRefCount();
}
~BlockAllocator() {
if (Arenas()->DecrRefCount() == 0) delete Arenas();
}
pointer address(reference ref) const { return Allocator().address(ref); }
const_pointer address(const_reference ref) const {
return Allocator().address(ref);
}
size_type max_size() const { return Allocator().max_size(); }
template <class U, class... Args>
void construct(U *p, Args &&... args) {
Allocator().construct(p, std::forward<Args>(args)...);
}
void destroy(pointer p) { Allocator().destroy(p); }
pointer allocate(size_type n, const void *hint = nullptr) {
if (n * kAllocFit <= kAllocSize) {
return static_cast<pointer>(Arena()->Allocate(n));
} else {
return Allocator().allocate(n, hint);
}
}
void deallocate(pointer p, size_type n) {
if (n * kAllocFit > kAllocSize) Allocator().deallocate(p, n);
}
MemoryArenaCollection *Arenas() const { return arenas_; }
private:
MemoryArena<T> *Arena() { return arenas_->Arena<T>(); }
MemoryArenaCollection *arenas_;
BlockAllocator<T> operator=(const BlockAllocator<T> &);
};
template <typename T, typename U>
bool operator==(const BlockAllocator<T> &alloc1,
const BlockAllocator<U> &alloc2) {
return false;
}
template <typename T, typename U>
bool operator!=(const BlockAllocator<T> &alloc1,
const BlockAllocator<U> &alloc2) {
return true;
}
// STL allocator using memory pools. Memory is allocated from underlying
// blocks of size 'block_size * sizeof(T)'. Keeps an internal list of freed
// chunks thare are reused on the next allocation.
//
// This allocator has object-local state so it should not be used with splicing
// or swapping operations between objects created with different allocators nor
// should it be used if copies must be thread-safe. The result of allocate()
// will be suitably memory-aligned.
template <typename T>
class PoolAllocator {
public:
using Allocator = std::allocator<T>;
using size_type = typename Allocator::size_type;
using difference_type = typename Allocator::difference_type;
using pointer = typename Allocator::pointer;
using const_pointer = typename Allocator::const_pointer;
using reference = typename Allocator::reference;
using const_reference = typename Allocator::const_reference;
using value_type = typename Allocator::value_type;
template <typename U>
struct rebind {
using other = PoolAllocator<U>;
};
explicit PoolAllocator(size_t pool_size = kAllocSize)
: pools_(new MemoryPoolCollection(pool_size)) {}
PoolAllocator(const PoolAllocator<T> &pool_alloc)
: pools_(pool_alloc.Pools()) {
Pools()->IncrRefCount();
}
template <typename U>
explicit PoolAllocator(const PoolAllocator<U> &pool_alloc)
: pools_(pool_alloc.Pools()) {
Pools()->IncrRefCount();
}
~PoolAllocator() {
if (Pools()->DecrRefCount() == 0) delete Pools();
}
pointer address(reference ref) const { return Allocator().address(ref); }
const_pointer address(const_reference ref) const {
return Allocator().address(ref);
}
size_type max_size() const { return Allocator().max_size(); }
template <class U, class... Args>
void construct(U *p, Args &&... args) {
Allocator().construct(p, std::forward<Args>(args)...);
}
void destroy(pointer p) { Allocator().destroy(p); }
pointer allocate(size_type n, const void *hint = nullptr) {
if (n == 1) {
return static_cast<pointer>(Pool<1>()->Allocate());
} else if (n == 2) {
return static_cast<pointer>(Pool<2>()->Allocate());
} else if (n <= 4) {
return static_cast<pointer>(Pool<4>()->Allocate());
} else if (n <= 8) {
return static_cast<pointer>(Pool<8>()->Allocate());
} else if (n <= 16) {
return static_cast<pointer>(Pool<16>()->Allocate());
} else if (n <= 32) {
return static_cast<pointer>(Pool<32>()->Allocate());
} else if (n <= 64) {
return static_cast<pointer>(Pool<64>()->Allocate());
} else {
return Allocator().allocate(n, hint);
}
}
void deallocate(pointer p, size_type n) {
if (n == 1) {
Pool<1>()->Free(p);
} else if (n == 2) {
Pool<2>()->Free(p);
} else if (n <= 4) {
Pool<4>()->Free(p);
} else if (n <= 8) {
Pool<8>()->Free(p);
} else if (n <= 16) {
Pool<16>()->Free(p);
} else if (n <= 32) {
Pool<32>()->Free(p);
} else if (n <= 64) {
Pool<64>()->Free(p);
} else {
Allocator().deallocate(p, n);
}
}
MemoryPoolCollection *Pools() const { return pools_; }
private:
template <int n>
struct TN {
T buf[n];
};
template <int n>
MemoryPool<TN<n>> *Pool() {
return pools_->Pool<TN<n>>();
}
MemoryPoolCollection *pools_;
PoolAllocator<T> operator=(const PoolAllocator<T> &);
};
template <typename T, typename U>
bool operator==(const PoolAllocator<T> &alloc1,
const PoolAllocator<U> &alloc2) {
return false;
}
template <typename T, typename U>
bool operator!=(const PoolAllocator<T> &alloc1,
const PoolAllocator<U> &alloc2) {
return true;
}
} // namespace fst
#endif // FST_MEMORY_H_