1 /** @file
2 
3     Implementation of the proxy allocators.
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 #include "I_EventSystem.h"
24 
25 int thread_freelist_high_watermark = 512;
26 int thread_freelist_low_watermark  = 32;
27 extern int cmd_disable_pfreelist;
28 
29 void *
30 thread_alloc(Allocator &a, ProxyAllocator &l)
31 {
32   if (!cmd_disable_pfreelist && l.freelist) {
33     void *v    = (void *)l.freelist;
34     l.freelist = *(void **)l.freelist;
35     --(l.allocated);
36     return v;
37   }
38   return a.alloc_void();
39 }
40 
41 void
42 thread_freeup(Allocator &a, ProxyAllocator &l)
43 {
44   void *head   = (void *)l.freelist;
45   void *tail   = (void *)l.freelist;
46   size_t count = 0;
47   while (l.freelist && l.allocated > thread_freelist_low_watermark) {
48     tail       = l.freelist;
49     l.freelist = *(void **)l.freelist;
50     --(l.allocated);
51     ++count;
52   }
53 
54   if (unlikely(count == 1)) {
55     a.free_void(tail);
56   } else if (count > 0) {
57     a.free_void_bulk(head, tail, count);
58   }
59 
60   ink_assert(l.allocated >= thread_freelist_low_watermark);
61 }
62