]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mem/PoolingAllocator.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / mem / PoolingAllocator.h
CommitLineData
c3b51d64 1/*
b8ae064d 2+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
c3b51d64
EB
3+ *
4+ * Squid software is distributed under GPLv2+ license and includes
5+ * contributions from numerous individuals and organizations.
6+ * Please see the COPYING and CONTRIBUTORS files for details.
7+ */
8
9#ifndef SQUID_MEM_POOLINGALLOCATOR_H
10#define SQUID_MEM_POOLINGALLOCATOR_H
11
12#include "mem/forward.h"
13
14/// STL Allocator that uses Squid memory pools for memory management
15template <class Value>
16class PoolingAllocator
17{
18public:
19 /* STL Allocator API */
20 using value_type = Value;
21 PoolingAllocator() noexcept {}
22 template <class Other> PoolingAllocator(const PoolingAllocator<Other> &) noexcept {}
23 value_type *allocate(std::size_t n) { return static_cast<value_type*>(memAllocRigid(n*sizeof(value_type))); }
24 void deallocate(value_type *vp, std::size_t n) noexcept { memFreeRigid(vp, n*sizeof(value_type)); }
bedfa6a4 25
bedfa6a4
AR
26 template <class OtherValue>
27 struct rebind {
70ac5b29 28 typedef PoolingAllocator<OtherValue> other;
bedfa6a4
AR
29 };
30
72247610 31 template<class U, class ... Args> void construct(U *p, Args && ... args) { new((void *)p) U(std::forward<Args>(args)...); }
bedfa6a4 32 template<typename OtherValue> void destroy(OtherValue *p) { p->~OtherValue(); }
c3b51d64
EB
33};
34
35template <class L, class R>
36inline bool
37operator ==(const PoolingAllocator<L>&, const PoolingAllocator<R>&) noexcept
38{
39 return true;
40}
41
42template <class L, class R>
43inline bool
44operator !=(const PoolingAllocator<L> &l, const PoolingAllocator<R> &r) noexcept
45{
46 return !(l == r);
47}
48
49#endif /* SQUID_MEM_POOLINGALLOCATOR_H */
50