Skip to content

Commit 318072c

Browse files
committed
add comments of allocator design
test=develop
1 parent 2d92b6b commit 318072c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

paddle/fluid/memory/allocation/allocator.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,49 @@ class Allocator;
4646
// NOTE: this is the base class of Allocation. Each allocator can use its own
4747
// allocation object.
4848
// 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+
*/
4985
class Allocation {
5086
public:
5187
Allocation(void* ptr, size_t size, platform::Place place)
5288
: 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.
5392
decorated_allocators_.reserve(8);
5493
}
5594

0 commit comments

Comments
 (0)