CUBRID Engine  latest
mem_block.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * mem_block.cpp - Memory Block Functionality
21  */
22 
23 #include "mem_block.hpp"
24 
25 #include <functional>
26 #include <cstring>
27 
28 namespace cubmem
29 {
30  void
31  standard_alloc (block &b, size_t size)
32  {
33  if (b.ptr == NULL || b.dim == 0)
34  {
35  b.ptr = new char[size];
36  b.dim = size;
37  }
38  else if (size <= b.dim)
39  {
40  // do not reduce
41  }
42  else
43  {
44  char *new_ptr = new char[size];
45  std::memcpy (new_ptr, b.ptr, b.dim);
46 
47  delete[] b.ptr;
48 
49  b.ptr = new_ptr;
50  b.dim = size;
51  }
52  }
53 
54  void
56  {
57  delete [] b.ptr;
58  b.ptr = NULL;
59  b.dim = 0;
60  }
61 
63 
64  void
66  {
67  if (b.ptr == NULL || b.dim == 0)
68  {
69  b.ptr = new char[size];
70  b.dim = size;
71  }
72  else if (size <= b.dim)
73  {
74  // do not reduce
75  }
76  else
77  {
78  size_t new_size;
79 
80  for (new_size = b.dim; new_size < size; new_size *= 2)
81  ;
82 
83  char *new_ptr = new char[new_size];
84  std::memcpy (new_ptr, b.ptr, b.dim);
85 
86  delete[] b.ptr;
87 
88  b.ptr = new_ptr;
89  b.dim = new_size;
90  }
91  }
92 
94  {
95  std::bind (exponential_standard_alloc, std::placeholders::_1, std::placeholders::_2),
96  std::bind (standard_dealloc, std::placeholders::_1)
97  };
98 
99  void
100  cstyle_alloc (block &b, size_t size)
101  {
102  if (b.ptr == NULL)
103  {
104  b.ptr = (char *) malloc (size);
105  b.dim = size;
106  assert (b.ptr != NULL);
107  }
108  else
109  {
110  b.ptr = (char *) realloc (b.ptr, size);
111  b.dim = size;
112  assert (b.ptr != NULL);
113  }
114  }
115 
116  void
118  {
119  if (b.ptr != NULL)
120  {
121  free (b.ptr);
122  }
123  b.ptr = NULL;
124  b.dim = 0;
125  }
126 
128 
129  //
130  // block_allocator
131  //
132  block_allocator::block_allocator (const alloc_func &alloc_f, const dealloc_func &dealloc_f)
133  : m_alloc_f (alloc_f)
134  , m_dealloc_f (dealloc_f)
135  {
136  }
137 
140  {
141  m_alloc_f = other.m_alloc_f;
142  m_dealloc_f = other.m_dealloc_f;
143  return *this;
144  }
145 
146  //
147  // single_block_allocator
148  //
149 
151  : m_base_allocator (base_alloc)
152  , m_block {}
153  , m_allocator { std::bind (&single_block_allocator::allocate, this, std::placeholders::_1, std::placeholders::_2),
154  std::bind (&single_block_allocator::deallocate, this, std::placeholders::_1) }
155  {
156  }
157 
159  {
161  }
162 
163  void
165  {
166  // argument should be uninitialized or should be same as m_block; giving a different block may be a logical error
167  assert (b.ptr == NULL || (b.ptr == m_block.ptr && b.dim == m_block.dim));
168 
170  b.ptr = m_block.ptr;
171  b.dim = m_block.dim;
172  }
173 
174  void
176  {
177  // local block remains
178  b.ptr = NULL;
179  b.dim = 0;
180  }
181 
182  const block_allocator &
184  {
185  return m_allocator;
186  }
187 
188  const block &
190  {
191  return m_block;
192  }
193 
194  char *
196  {
197  return m_block.ptr;
198  }
199 
200  size_t
202  {
203  return m_block.dim;
204  }
205 
206  void
208  {
210  }
211 
212 } // namespace cubmem
const block_allocator CSTYLE_BLOCK_ALLOCATOR
Definition: mem_block.cpp:127
void standard_alloc(block &b, size_t size)
Definition: mem_block.cpp:31
std::function< void(block &b)> dealloc_func
Definition: mem_block.hpp:103
const block_allocator STANDARD_BLOCK_ALLOCATOR
Definition: mem_block.cpp:62
const block_allocator & m_base_allocator
Definition: mem_block.hpp:143
void exponential_standard_alloc(block &b, size_t size)
Definition: mem_block.cpp:65
#define assert(x)
block_allocator & operator=(const block_allocator &other)
Definition: mem_block.cpp:139
std::function< void(block &b, size_t size)> alloc_func
Definition: mem_block.hpp:102
void cstyle_dealloc(block &b)
Definition: mem_block.cpp:117
void standard_dealloc(block &b)
Definition: mem_block.cpp:55
#define NULL
Definition: freelistheap.h:34
dealloc_func m_dealloc_f
Definition: mem_block.hpp:106
single_block_allocator(const block_allocator &base_alloc)
Definition: mem_block.cpp:150
void allocate(block &b, size_t size)
Definition: mem_block.cpp:164
const block_allocator EXPONENTIAL_STANDARD_BLOCK_ALLOCATOR
Definition: mem_block.cpp:94
const block_allocator & get_block_allocator() const
Definition: mem_block.cpp:183
const block & get_block() const
Definition: mem_block.cpp:189
void cstyle_alloc(block &b, size_t size)
Definition: mem_block.cpp:100