dispenso 1.4.1
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
16#pragma once
17
18#include <atomic>
19#include <functional>
20#include <vector>
21
22#include <dispenso/platform.h>
23
24namespace dispenso {
25
29template <bool kThreadSafe>
31 public:
40 DISPENSO_DLL_ACCESS PoolAllocatorT(
41 size_t chunkSize,
42 size_t allocSize,
43 std::function<void*(size_t)> allocFunc,
44 std::function<void(void*)> deallocFunc);
45
51 DISPENSO_DLL_ACCESS char* alloc();
52
58 DISPENSO_DLL_ACCESS void dealloc(char* ptr);
59
65 DISPENSO_DLL_ACCESS void clear();
66
71 size_t totalChunkCapacity() const {
72 return (backingAllocs2_.size() + backingAllocs_.size()) * chunksPerAlloc_;
73 }
77 DISPENSO_DLL_ACCESS ~PoolAllocatorT();
78
79 private:
80 const size_t chunkSize_;
81 const size_t allocSize_;
82 const size_t chunksPerAlloc_;
83
84 std::function<void*(size_t)> allocFunc_;
85 std::function<void(void*)> deallocFunc_;
86
87 // Use of a spin lock was found to be faster than std::mutex in benchmarks.
88 alignas(kCacheLineSize) std::atomic<uint32_t> backingAllocLock_{0};
89 std::vector<char*> backingAllocs_;
90 std::vector<char*> backingAllocs2_;
91
92 std::vector<char*> chunks_;
93};
94
95using PoolAllocator = PoolAllocatorT<true>;
96using NoLockPoolAllocator = PoolAllocatorT<false>;
97
98} // namespace dispenso
DISPENSO_DLL_ACCESS char * alloc()
DISPENSO_DLL_ACCESS void clear()
DISPENSO_DLL_ACCESS ~PoolAllocatorT()
size_t totalChunkCapacity() const
DISPENSO_DLL_ACCESS PoolAllocatorT(size_t chunkSize, size_t allocSize, std::function< void *(size_t)> allocFunc, std::function< void(void *)> deallocFunc)
DISPENSO_DLL_ACCESS void dealloc(char *ptr)
constexpr size_t kCacheLineSize
A constant that defines a safe number of bytes+alignment to avoid false sharing.
Definition platform.h:61