CUBRID Engine  latest
quick_fit.c
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  * quick_fit.c: Implementation of the workspace heap
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <assert.h>
28 
29 #include "customheaps.h"
30 #include "memory_alloc.h"
31 #include "quick_fit.h"
32 #include "memory_alloc.h"
33 
34 static HL_HEAPID ws_Heap_id = 0;
35 
36 /*
37  * db_create_workspace_heap () - create a lea heap
38  * return: memory heap identifier
39  * req_size(in): a paramter to get chunk size
40  * recs_per_chunk(in): a parameter to get chunk size
41  */
42 HL_HEAPID
44 {
45  if (ws_Heap_id == 0)
46  {
48  }
49  return ws_Heap_id;
50 }
51 
52 /*
53  * db_destroy_workspace_heap () - destroy a lea heap
54  * return:
55  * heap_id(in): memory heap identifier to destroy
56  */
57 void
59 {
60  if (ws_Heap_id != 0)
61  {
63  ws_Heap_id = 0;
64  }
65 }
66 
67 /*
68  * db_ws_alloc () - call allocation function for the lea heap
69  * return: allocated memory pointer
70  * size(in): size to allocate
71  */
72 void *
73 db_ws_alloc (size_t size)
74 {
75 #if defined(SA_MODE)
76  void *ptr = NULL;
77 
78  if (ws_Heap_id == 0)
79  {
80  /* not initialized yet */
82  }
83 
84  if (ws_Heap_id && size > 0)
85  {
86  PRIVATE_MALLOC_HEADER *h;
87  size_t req_sz;
88 
89  req_sz = private_request_size (size);
90  h = (PRIVATE_MALLOC_HEADER *) hl_lea_alloc (ws_Heap_id, req_sz);
91 
92  if (h != NULL)
93  {
94  h->magic = PRIVATE_MALLOC_HEADER_MAGIC;
95  h->alloc_type = PRIVATE_ALLOC_TYPE_WS;
96  ptr = private_hl2user_ptr (h);
97  }
98  }
99  return ptr;
100 #else
101  void *ptr = NULL;
102 
103  if (ws_Heap_id == 0)
104  {
105  /* not initialized yet */
107  }
108 
109  if (ws_Heap_id && (size > 0))
110  {
111  ptr = hl_lea_alloc (ws_Heap_id, size);
112  }
113  return ptr;
114 #endif
115 }
116 
117 /*
118  * db_ws_realloc () - call re-allocation function for the lea heap
119  * return: allocated memory pointer
120  * size(in): size to allocate
121  */
122 void *
123 db_ws_realloc (void *ptr, size_t size)
124 {
125 #if defined(SA_MODE)
126  if (ptr == NULL)
127  {
128  return db_ws_alloc (size);
129  }
130 
131  if (ws_Heap_id == 0)
132  {
133  /* not initialized yet */
135  }
136 
137  if (ws_Heap_id && size > 0)
138  {
139  PRIVATE_MALLOC_HEADER *h;
140 
141  h = private_user2hl_ptr (ptr);
142  if (h->magic != PRIVATE_MALLOC_HEADER_MAGIC)
143  {
144  return NULL;
145  }
146 
147  if (h->alloc_type == PRIVATE_ALLOC_TYPE_WS)
148  {
149  PRIVATE_MALLOC_HEADER *new_h;
150  size_t req_sz;
151 
152  req_sz = private_request_size (size);
153  new_h = (PRIVATE_MALLOC_HEADER *) hl_lea_realloc (ws_Heap_id, h, req_sz);
154  if (new_h == NULL)
155  {
156  return NULL;
157  }
158  return private_hl2user_ptr (new_h);
159  }
160  else if (h->alloc_type == PRIVATE_ALLOC_TYPE_LEA)
161  {
162  return db_private_realloc (NULL, ptr, size);
163  }
164  else
165  {
166  return NULL;
167  }
168  }
169  else
170  {
171  return NULL;
172  }
173 #else
174  if (ws_Heap_id == 0)
175  {
176  /* not initialized yet */
178  }
179 
180  if (ws_Heap_id && (size > 0))
181  {
182  ptr = hl_lea_realloc (ws_Heap_id, ptr, size);
183  }
184  return ptr;
185 #endif
186 }
187 
188 /*
189  * db_ws_free () - call free function for the lea heap
190  * return:
191  * ptr(in): memory pointer to free
192  */
193 void
194 db_ws_free (void *ptr)
195 {
196 #if defined(SA_MODE)
197  assert (ws_Heap_id != 0);
198 
199  if (ws_Heap_id && ptr)
200  {
201  PRIVATE_MALLOC_HEADER *h;
202 
203  h = private_user2hl_ptr (ptr);
204  if (h->magic != PRIVATE_MALLOC_HEADER_MAGIC)
205  {
206  /* assertion point */
207  return;
208  }
209 
210  if (h->alloc_type == PRIVATE_ALLOC_TYPE_WS)
211  {
212  hl_lea_free (ws_Heap_id, h);
213  }
214  else if (h->alloc_type == PRIVATE_ALLOC_TYPE_LEA)
215  {
216  db_private_free (NULL, ptr); /* not 'h' */
217  }
218  else
219  {
220  return;
221  }
222  }
223 #else
224  assert (ws_Heap_id != 0);
225 
226  if (ws_Heap_id && ptr)
227  {
228  hl_lea_free (ws_Heap_id, ptr);
229  }
230 #endif
231 }
void * hl_lea_realloc(UINTPTR heap_id, void *ptr, size_t sz)
Definition: lea_heap.c:361
UINTPTR hl_register_lea_heap(void)
Definition: lea_heap.c:213
void db_destroy_workspace_heap(void)
Definition: quick_fit.c:58
void * db_ws_realloc(void *ptr, size_t size)
Definition: quick_fit.c:123
HL_HEAPID db_create_workspace_heap(void)
Definition: quick_fit.c:43
void db_ws_free(void *ptr)
Definition: quick_fit.c:194
static HL_HEAPID ws_Heap_id
Definition: quick_fit.c:34
#define assert(x)
#define NULL
Definition: freelistheap.h:34
#define db_private_free(thrd, ptr)
Definition: memory_alloc.h:229
void hl_unregister_lea_heap(UINTPTR heap_id)
Definition: lea_heap.c:299
void * db_ws_alloc(size_t size)
Definition: quick_fit.c:73
void * hl_lea_alloc(UINTPTR heap_id, size_t sz)
Definition: lea_heap.c:326
#define db_private_realloc(thrd, ptr, size)
Definition: memory_alloc.h:231
void hl_lea_free(UINTPTR heap_id, void *ptr)
Definition: lea_heap.c:395