dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
small_buffer_allocator.cpp
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#include <dispenso/detail/small_buffer_allocator_impl.h>
10
11#include <new>
12
13namespace dispenso {
14namespace detail {
15
16template <size_t kChunkSize>
17SmallBufferGlobals& getSmallBufferGlobals() {
18 // controlled leak here
19 static SmallBufferGlobals* globals = new SmallBufferGlobals();
20 return *globals;
21}
22
23char* allocSmallBufferImpl(size_t ordinal) {
24 switch (ordinal) {
25 case 0:
26 return detail::SmallBufferAllocator<4>::alloc();
27 case 1:
28 return detail::SmallBufferAllocator<8>::alloc();
29 case 2:
30 return detail::SmallBufferAllocator<16>::alloc();
31 case 3:
32 return detail::SmallBufferAllocator<32>::alloc();
33 case 4:
34 return detail::SmallBufferAllocator<64>::alloc();
35 case 5:
36 return detail::SmallBufferAllocator<128>::alloc();
37 case 6:
38 return detail::SmallBufferAllocator<256>::alloc();
39 default:
40 assert(false && "Invalid small buffer ordinal requested");
41 return nullptr;
42 }
43}
44
45void deallocSmallBufferImpl(size_t ordinal, void* buf) {
46 switch (ordinal) {
47 case 0:
48 detail::SmallBufferAllocator<4>::dealloc(reinterpret_cast<char*>(buf));
49 break;
50 case 1:
51 detail::SmallBufferAllocator<8>::dealloc(reinterpret_cast<char*>(buf));
52 break;
53 case 2:
54 detail::SmallBufferAllocator<16>::dealloc(reinterpret_cast<char*>(buf));
55 break;
56 case 3:
57 detail::SmallBufferAllocator<32>::dealloc(reinterpret_cast<char*>(buf));
58 break;
59 case 4:
60 detail::SmallBufferAllocator<64>::dealloc(reinterpret_cast<char*>(buf));
61 break;
62 case 5:
63 detail::SmallBufferAllocator<128>::dealloc(reinterpret_cast<char*>(buf));
64 break;
65 case 6:
66 detail::SmallBufferAllocator<256>::dealloc(reinterpret_cast<char*>(buf));
67 break;
68 default:
69 assert(false && "Invalid small buffer ordinal requested");
70 }
71}
72
73size_t approxBytesAllocatedSmallBufferImpl(size_t ordinal) {
74 switch (ordinal) {
75 case 0:
76 return detail::SmallBufferAllocator<4>::bytesAllocated();
77 case 1:
78 return detail::SmallBufferAllocator<8>::bytesAllocated();
79 case 2:
80 return detail::SmallBufferAllocator<16>::bytesAllocated();
81 case 3:
82 return detail::SmallBufferAllocator<32>::bytesAllocated();
83 case 4:
84 return detail::SmallBufferAllocator<64>::bytesAllocated();
85 case 5:
86 return detail::SmallBufferAllocator<128>::bytesAllocated();
87 case 6:
88 return detail::SmallBufferAllocator<256>::bytesAllocated();
89 default:
90 assert(false && "Invalid small buffer ordinal requested");
91 return 0;
92 }
93}
94
95template <size_t kChunkSize>
96SmallBufferAllocator<kChunkSize>::PerThreadQueuingData::~PerThreadQueuingData() {
97 enqueue_bulk(buffers_, count_);
98
99 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
100 ptoken().~ProducerToken();
101 ctoken().~ConsumerToken();
102 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_END();
103}
104
105template class SmallBufferAllocator<4>;
106template class SmallBufferAllocator<8>;
107template class SmallBufferAllocator<16>;
108template class SmallBufferAllocator<32>;
109template class SmallBufferAllocator<64>;
110template class SmallBufferAllocator<128>;
111template class SmallBufferAllocator<256>;
112
113} // namespace detail
114} // namespace dispenso