CUBRID Engine  latest
memory_private_allocator.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 // memory_private_allocator.cpp - extension to memory_alloc.h private allocation
21 //
22 
24 
25 #if defined (SERVER_MODE)
26 #include "thread_manager.hpp"
27 #endif // SERVER_MODE
28 
29 namespace cubmem
30 {
31  void
32  private_block_allocate (block &b, size_t size)
33  {
34  if (b.ptr == NULL || b.dim == 0)
35  {
36  b.ptr = (char *) db_private_alloc (NULL, (int) size);
37  b.dim = size;
38  }
39  else if (size <= b.dim)
40  {
41  // no change
42  }
43  else
44  {
45  size_t new_size = b.dim;
46  while (new_size < size)
47  {
48  new_size *= 2;
49  }
50  char *new_ptr = (char *) db_private_realloc (NULL, b.ptr, (int) new_size);
51  if (new_ptr != NULL)
52  {
53  b.ptr = new_ptr;
54  b.dim = new_size;
55  }
56  else
57  {
58  assert (false);
59  }
60  }
61  }
62 
63  void
65  {
66  if (b.ptr != NULL)
67  {
69  }
70  b.ptr = NULL;
71  b.dim = 0;
72  }
73 
75 
76  //
77  // private allocation helper functions
78  //
79  HL_HEAPID
81  {
82 #if defined (SERVER_MODE)
83  if (thread_p == NULL)
84  {
85  thread_p = &cubthread::get_entry ();
86  }
87  return thread_p->private_heap_id;
88 #else // not SERVER_MODE
89  (void) thread_p; // not used
90  return 0;
91 #endif // not SERVER_MODE
92  }
93 
94  void *
95  private_heap_allocate (cubthread::entry *thread_p, HL_HEAPID heapid, size_t size)
96  {
97 #if defined (SERVER_MODE)
98  if (heapid != thread_p->private_heap_id)
99  {
100  /* this is not something we should do! */
101  assert (false);
102 
103  HL_HEAPID save_heapid = db_private_set_heapid_to_thread (thread_p, heapid);
104  void *p = db_private_alloc (thread_p, size);
105  (void) db_private_set_heapid_to_thread (thread_p, save_heapid);
106  return p;
107  }
108  else
109 #endif // SERVER_MODE
110  {
111  return db_private_alloc (thread_p, size);
112  }
113  }
114 
115  void
116  private_heap_deallocate (cubthread::entry *thread_p, HL_HEAPID heapid, void *ptr)
117  {
118 #if defined (SERVER_MODE)
119  if (heapid != thread_p->private_heap_id)
120  {
121  /* this is not something we should do! */
122  assert (false);
123 
124  HL_HEAPID save_heapid = db_private_set_heapid_to_thread (thread_p, heapid);
125  db_private_free (thread_p, ptr);
126  (void) db_private_set_heapid_to_thread (thread_p, save_heapid);
127  }
128  else
129 #endif // SERVER_MODE
130  {
131  db_private_free (thread_p, ptr);
132  }
133  }
134 
135  void
137  {
138 #if defined (SERVER_MODE) && !defined (NDEBUG)
139  thread_p->count_private_allocators++;
140 #else
141  (void) thread_p;
142 #endif
143  }
144 
145  void
147  {
148 #if defined (SERVER_MODE) && !defined (NDEBUG)
149  thread_p->count_private_allocators--;
150 #else
151  (void) thread_p;
152 #endif
153  }
154 } // namespace cubmem
void deregister_private_allocator(cubthread::entry *thread_p)
void private_heap_deallocate(cubthread::entry *thread_p, HL_HEAPID heapid, void *ptr)
void * private_heap_allocate(cubthread::entry *thread_p, HL_HEAPID heapid, size_t size)
void private_block_deallocate(block &b)
const block_allocator PRIVATE_BLOCK_ALLOCATOR
void private_block_allocate(block &b, size_t size)
#define assert(x)
#define NULL
Definition: freelistheap.h:34
#define db_private_free(thrd, ptr)
Definition: memory_alloc.h:229
#define db_private_alloc(thrd, size)
Definition: memory_alloc.h:227
HL_HEAPID get_private_heapid(cubthread::entry *&thread_p)
void register_private_allocator(cubthread::entry *thread_p)
HL_HEAPID private_heap_id
entry & get_entry(void)
#define db_private_realloc(thrd, ptr, size)
Definition: memory_alloc.h:231
const char ** p
Definition: dynamic_load.c:945