xref: /trafficserver/src/tscore/Arena.cc (revision 4cfd5a73)
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 #include "tscore/ink_platform.h"
25 #include "tscore/ink_memory.h"
26 #include "tscore/Allocator.h"
27 #include "tscore/Arena.h"
28 #include <cassert>
29 #include <cstring>
30 
31 #define DEFAULT_ALLOC_SIZE 1024
32 #define DEFAULT_BLOCK_SIZE (DEFAULT_ALLOC_SIZE - (sizeof(ArenaBlock) - 8))
33 
34 static Allocator defaultSizeArenaBlock("ArenaBlock", DEFAULT_ALLOC_SIZE);
35 
36 /*-------------------------------------------------------------------------
37   -------------------------------------------------------------------------*/
38 
39 static inline ArenaBlock *
blk_alloc(int size)40 blk_alloc(int size)
41 {
42   ArenaBlock *blk;
43 
44   if (size == DEFAULT_BLOCK_SIZE) {
45     blk = static_cast<ArenaBlock *>(defaultSizeArenaBlock.alloc_void());
46   } else {
47     blk = static_cast<ArenaBlock *>(ats_malloc(size + sizeof(ArenaBlock) - 8));
48   }
49 
50   blk->next          = nullptr;
51   blk->m_heap_end    = &blk->data[size];
52   blk->m_water_level = &blk->data[0];
53 
54   return blk;
55 }
56 
57 /*-------------------------------------------------------------------------
58   -------------------------------------------------------------------------*/
59 
60 static inline void
blk_free(ArenaBlock * blk)61 blk_free(ArenaBlock *blk)
62 {
63   int size;
64 
65   size = blk->m_heap_end - &blk->data[0];
66   if (size == DEFAULT_BLOCK_SIZE) {
67     defaultSizeArenaBlock.free_void(blk);
68   } else {
69     ats_free(blk);
70   }
71 }
72 
73 /*-------------------------------------------------------------------------
74   -------------------------------------------------------------------------*/
75 
76 static void *
block_alloc(ArenaBlock * block,size_t size,size_t alignment)77 block_alloc(ArenaBlock *block, size_t size, size_t alignment)
78 {
79   char *mem;
80 
81   mem = block->m_water_level;
82   if (((size_t)mem) & (alignment - 1)) {
83     mem += (alignment - ((size_t)mem)) & (alignment - 1);
84   }
85 
86   if ((block->m_heap_end >= mem) && (((size_t)block->m_heap_end - (size_t)mem) >= size)) {
87     block->m_water_level = mem + size;
88     return mem;
89   }
90 
91   return nullptr;
92 }
93 
94 void *
alloc(size_t size,size_t alignment)95 Arena::alloc(size_t size, size_t alignment)
96 {
97   ArenaBlock *b;
98   unsigned int block_size;
99   void *mem;
100 
101   ink_assert((alignment & (alignment - 1)) == 0);
102 
103   b = m_blocks;
104   while (b) {
105     mem = block_alloc(b, size, alignment);
106     if (mem) {
107       return mem;
108     }
109     b = b->next;
110   }
111 
112   block_size = static_cast<unsigned int>(size * 1.5);
113   if (block_size < DEFAULT_BLOCK_SIZE) {
114     block_size = DEFAULT_BLOCK_SIZE;
115   }
116 
117   b        = blk_alloc(block_size);
118   b->next  = m_blocks;
119   m_blocks = b;
120 
121   mem = block_alloc(b, size, alignment);
122   return mem;
123 }
124 
125 /*-------------------------------------------------------------------------
126   -------------------------------------------------------------------------*/
127 
128 void
free(void * mem,size_t size)129 Arena::free(void *mem, size_t size)
130 {
131   if (m_blocks) {
132     ArenaBlock *b;
133 
134     b = m_blocks;
135     while (b->next) {
136       if (b->m_water_level == (static_cast<char *>(mem) + size)) {
137         b->m_water_level = static_cast<char *>(mem);
138         return;
139       }
140       b = b->next;
141     }
142   }
143 }
144 
145 /*-------------------------------------------------------------------------
146   -------------------------------------------------------------------------*/
147 
148 void
reset()149 Arena::reset()
150 {
151   ArenaBlock *b;
152 
153   while (m_blocks) {
154     b = m_blocks->next;
155     blk_free(m_blocks);
156     m_blocks = b;
157   }
158   ink_assert(m_blocks == nullptr);
159 }
160