1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   ProxyAllocator.h
27 
28 
29 
30 *****************************************************************************/
31 #pragma once
32 
33 #include <new>
34 #include <utility>
35 
36 #include "tscore/ink_platform.h"
37 
38 class EThread;
39 
40 extern int thread_freelist_high_watermark;
41 extern int thread_freelist_low_watermark;
42 extern int cmd_disable_pfreelist;
43 
44 struct ProxyAllocator {
45   int allocated  = 0;
46   void *freelist = nullptr;
47 
ProxyAllocatorProxyAllocator48   ProxyAllocator() {}
49 };
50 
51 template <class CAlloc, typename... Args>
52 typename CAlloc::Value_type *
thread_alloc(CAlloc & a,ProxyAllocator & l,Args &&...args)53 thread_alloc(CAlloc &a, ProxyAllocator &l, Args &&... args)
54 {
55   if (!cmd_disable_pfreelist && l.freelist) {
56     void *v    = l.freelist;
57     l.freelist = *reinterpret_cast<void **>(l.freelist);
58     --(l.allocated);
59     ::new (v) typename CAlloc::Value_type(std::forward<Args>(args)...);
60     return static_cast<typename CAlloc::Value_type *>(v);
61   }
62   return a.alloc(std::forward<Args>(args)...);
63 }
64 
65 class Allocator;
66 
67 void *thread_alloc(Allocator &a, ProxyAllocator &l);
68 void thread_freeup(Allocator &a, ProxyAllocator &l);
69 
70 #if 1
71 
72 // Potentially empty variable arguments -- non-standard GCC way
73 //
74 #define THREAD_ALLOC(_a, _t, ...) thread_alloc(::_a, _t->_a, ##__VA_ARGS__)
75 #define THREAD_ALLOC_INIT(_a, _t, ...) thread_alloc(::_a, _t->_a, ##__VA_ARGS__)
76 
77 #else
78 
79 // Potentially empty variable arguments -- Standard C++20 way
80 //
81 #define THREAD_ALLOC(_a, _t, ...) thread_alloc(::_a, _t->_a __VA_OPT__(, ) __VA_ARGS__)
82 #define THREAD_ALLOC_INIT(_a, _t, ...) thread_alloc(::_a, _t->_a __VA_OPT__(, ) __VA_ARGS__)
83 
84 #endif
85 
86 #define THREAD_FREE(_p, _a, _t)                              \
87   do {                                                       \
88     ::_a.destroy_if_enabled(_p);                             \
89     if (!cmd_disable_pfreelist) {                            \
90       *(char **)_p    = (char *)_t->_a.freelist;             \
91       _t->_a.freelist = _p;                                  \
92       _t->_a.allocated++;                                    \
93       if (_t->_a.allocated > thread_freelist_high_watermark) \
94         thread_freeup(::_a.raw(), _t->_a);                   \
95     } else {                                                 \
96       ::_a.raw().free_void(_p);                              \
97     }                                                        \
98   } while (0)
99