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 "tscore/ink_platform.h"
34 
35 class EThread;
36 
37 extern int thread_freelist_high_watermark;
38 extern int thread_freelist_low_watermark;
39 extern int cmd_disable_pfreelist;
40 
41 struct ProxyAllocator {
42   int allocated  = 0;
43   void *freelist = nullptr;
44 
ProxyAllocatorProxyAllocator45   ProxyAllocator() {}
46 };
47 
48 template <class C>
49 inline C *
thread_alloc(ClassAllocator<C> & a,ProxyAllocator & l)50 thread_alloc(ClassAllocator<C> &a, ProxyAllocator &l)
51 {
52   if (!cmd_disable_pfreelist && l.freelist) {
53     C *v       = (C *)l.freelist;
54     l.freelist = *(C **)l.freelist;
55     --(l.allocated);
56     *(void **)v = *(void **)&a.proto.typeObject;
57     return v;
58   }
59   return a.alloc();
60 }
61 
62 template <class C>
63 inline C *
thread_alloc_init(ClassAllocator<C> & a,ProxyAllocator & l)64 thread_alloc_init(ClassAllocator<C> &a, ProxyAllocator &l)
65 {
66   if (!cmd_disable_pfreelist && l.freelist) {
67     C *v       = (C *)l.freelist;
68     l.freelist = *(C **)l.freelist;
69     --(l.allocated);
70     memcpy((void *)v, (void *)&a.proto.typeObject, sizeof(C));
71     return v;
72   }
73   return a.alloc();
74 }
75 
76 template <class C>
77 inline void
thread_free(ClassAllocator<C> & a,C * p)78 thread_free(ClassAllocator<C> &a, C *p)
79 {
80   a.free(p);
81 }
82 
83 static inline void
thread_free(Allocator & a,void * p)84 thread_free(Allocator &a, void *p)
85 {
86   a.free_void(p);
87 }
88 
89 template <class C>
90 inline void
thread_freeup(ClassAllocator<C> & a,ProxyAllocator & l)91 thread_freeup(ClassAllocator<C> &a, ProxyAllocator &l)
92 {
93   C *head      = (C *)l.freelist;
94   C *tail      = (C *)l.freelist;
95   size_t count = 0;
96   while (l.freelist && l.allocated > thread_freelist_low_watermark) {
97     tail       = (C *)l.freelist;
98     l.freelist = *(C **)l.freelist;
99     --(l.allocated);
100     ++count;
101   }
102 
103   if (unlikely(count == 1)) {
104     a.free(tail);
105   } else if (count > 0) {
106     a.free_bulk(head, tail, count);
107   }
108 
109   ink_assert(l.allocated >= thread_freelist_low_watermark);
110 }
111 
112 void *thread_alloc(Allocator &a, ProxyAllocator &l);
113 void thread_freeup(Allocator &a, ProxyAllocator &l);
114 
115 #define THREAD_ALLOC(_a, _t) thread_alloc(::_a, _t->_a)
116 #define THREAD_ALLOC_INIT(_a, _t) thread_alloc_init(::_a, _t->_a)
117 #define THREAD_FREE(_p, _a, _t)                              \
118   if (!cmd_disable_pfreelist) {                              \
119     do {                                                     \
120       *(char **)_p    = (char *)_t->_a.freelist;             \
121       _t->_a.freelist = _p;                                  \
122       _t->_a.allocated++;                                    \
123       if (_t->_a.allocated > thread_freelist_high_watermark) \
124         thread_freeup(::_a, _t->_a);                         \
125     } while (0);                                             \
126   } else {                                                   \
127     thread_free(::_a, _p);                                   \
128   }
129