dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
pool_allocator.h
Go to the documentation of this file.
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
15#pragma once
16
17#include <atomic>
18#include <functional>
19#include <vector>
20
21#include <dispenso/platform.h>
22
23namespace dispenso {
24
28template <bool kThreadSafe>
30 public:
39 DISPENSO_DLL_ACCESS PoolAllocatorT(
40 size_t chunkSize,
41 size_t allocSize,
42 std::function<void*(size_t)> allocFunc,
43 std::function<void(void*)> deallocFunc);
44
50 DISPENSO_DLL_ACCESS char* alloc();
51
57 DISPENSO_DLL_ACCESS void dealloc(char* ptr);
58
64 DISPENSO_DLL_ACCESS void clear();
65
70 size_t totalChunkCapacity() const {
71 return (backingAllocs2_.size() + backingAllocs_.size()) * chunksPerAlloc_;
72 }
76 DISPENSO_DLL_ACCESS ~PoolAllocatorT();
77
78 private:
79 const size_t chunkSize_;
80 const size_t allocSize_;
81 const size_t chunksPerAlloc_;
82
83 std::function<void*(size_t)> allocFunc_;
84 std::function<void(void*)> deallocFunc_;
85
86 // Use of a spin lock was found to be faster than std::mutex in benchmarks.
87 alignas(kCacheLineSize) std::atomic<uint32_t> backingAllocLock_{0};
88 std::vector<char*> backingAllocs_;
89 std::vector<char*> backingAllocs2_;
90
91 std::vector<char*> chunks_;
92};
93
94using PoolAllocator = PoolAllocatorT<true>;
95using NoLockPoolAllocator = PoolAllocatorT<false>;
96
97} // namespace dispenso
DISPENSO_DLL_ACCESS char * alloc()
DISPENSO_DLL_ACCESS void clear()
DISPENSO_DLL_ACCESS ~PoolAllocatorT()
size_t totalChunkCapacity() const
DISPENSO_DLL_ACCESS void dealloc(char *ptr)
detail::OpResult< T > OpResult
Definition pipeline.h:29