@@ -46,10 +46,49 @@ class Allocator;
46
46
// NOTE: this is the base class of Allocation. Each allocator can use its own
47
47
// allocation object.
48
48
// NOTE: the `Allocation::ptr()` could be nullptr, if the allocation size is 0
49
+
50
+ /* *
51
+ * Allocation is returned by Allocator::Allocate() method.
52
+ *
53
+ * An allocator may be decorated by another allocator. For example, we can
54
+ * decorate
55
+ * a RetryAllocator to any allocator to perform allocation retry when first
56
+ * allocation request fails.
57
+ *
58
+ * Explanations of Allocator design is as follows:
59
+ *
60
+ * Suppose we have an allocator which is decorated by several allocators:
61
+ *
62
+ * A(1) <- A(2) <- A(3) <- ... <- A(n)
63
+ *
64
+ * , and the public allocator is A(1).
65
+ *
66
+ * The allocation process would be:
67
+ *
68
+ * A(n).Allocate() -> ... -> A(2).Allocate() -> A(1).Allocate()
69
+ *
70
+ * , and the free process would be:
71
+ *
72
+ * A(1).Free() -> A(2).Free() -> ... -> A(n).Free()
73
+ *
74
+ * Therefore, we should record the allocator chain when allocating, so
75
+ * that we can free the allocation in the reverse order of allocator chain.
76
+ * The field `decorated_allocators_` is used to record this chain.
77
+ *
78
+ * Another example is that we want to add additional fields in Allocation,
79
+ * e.g., something what is done in AlignedAllocator, etc.
80
+ * In this case, we should declare a derived class of Allocation, which
81
+ * contains an underlying Allocation allocated by the underlying allocator.
82
+ * Therefore, `decorated_allocators_` of the new Allocation object would
83
+ * be a new chain, differing from the underlying Allocation object.
84
+ */
49
85
class Allocation {
50
86
public:
51
87
Allocation (void * ptr, size_t size, platform::Place place)
52
88
: ptr_(ptr), size_(size), place_(place) {
89
+ // NOTE(zjl): Since decorated_allocators_ is usually a small vector
90
+ // We reserve a small buffer to it to prevent frequent heap allocation
91
+ // Not quite sure whether we need something like gtl vector.
53
92
decorated_allocators_.reserve (8 );
54
93
}
55
94
0 commit comments