CUBRID Engine  latest
fixed_alloc.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  * fixed_alloc.c - implementation of fixed-size record allocator
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 
31 #include "memory_alloc.h"
32 #include "customheaps.h"
33 #if defined (SERVER_MODE)
34 #include "connection_defs.h"
35 #include "connection_error.h"
36 #endif /* SERVER_MODE */
37 
38 typedef struct rec_link REC_LINK;
39 struct rec_link
40 {
42 };
43 
44 /*
45  * compute_rec_size () - compute the actual memory size for requested
46  * memory size
47  * return:
48  * nominal_size(in):
49  */
50 static int
51 compute_rec_size (int nominal_size)
52 {
53  return db_align_to (MAX ((int) sizeof (REC_LINK), nominal_size),
54  MAX (db_alignment (nominal_size), db_alignment ((int) sizeof (REC_LINK *))));
55 }
56 
57 /*
58  * db_create_fixed_heap () - create a fixed heap
59  * return: memory heap identifier
60  * req_size(in): a paramter to get chunk size
61  * recs_per_chunk(in): a parameter to get chunk size
62  */
63 HL_HEAPID
64 db_create_fixed_heap (int req_size, int recs_per_chunk)
65 {
66  int chunk_size, rec_size;
67 
68  rec_size = compute_rec_size (req_size);
69  chunk_size = rec_size * recs_per_chunk;
70 
71  return (HL_HEAPID) hl_register_fixed_heap (chunk_size);
72 }
73 
74 /*
75  * db_destroy_fixed_heap () - destroy a fixed heap
76  * return:
77  * heap_id(in): memory heap identifier to destroy
78  */
79 void
80 db_destroy_fixed_heap (HL_HEAPID heap_id)
81 {
82  hl_unregister_fixed_heap (heap_id);
83 }
84 
85 /*
86  * db_fixed_alloc () - call allocation function for the fixed heap
87  * return: allocated memory pointer
88  * heap_id(in): memory heap identifier
89  * size(in): size to allocate
90  */
91 void *
92 db_fixed_alloc (HL_HEAPID heap_id, size_t size)
93 {
94  void *ptr = NULL;
95  if (heap_id && (size > 0))
96  {
97  ptr = hl_fixed_alloc (heap_id, size);
98  }
99  return ptr;
100 }
101 
102 /*
103  * db_fixed_free () - call free function for the fixed heap
104  * return:
105  * heap_id(in): memory heap identifier
106  * ptr(in): memory pointer to free
107  */
108 void
109 db_fixed_free (HL_HEAPID heap_id, void *ptr)
110 {
111  if (heap_id && ptr)
112  {
113  hl_fixed_free (heap_id, ptr);
114  }
115 }
void hl_fixed_free(UINTPTR heap_id, void *ptr)
Definition: customheaps.cpp:77
void * hl_fixed_alloc(UINTPTR heap_id, size_t sz)
Definition: customheaps.cpp:66
void * db_fixed_alloc(HL_HEAPID heap_id, size_t size)
Definition: fixed_alloc.c:92
void hl_unregister_fixed_heap(UINTPTR heap_id)
Definition: customheaps.cpp:55
void db_destroy_fixed_heap(HL_HEAPID heap_id)
Definition: fixed_alloc.c:80
static int compute_rec_size(int nominal_size)
Definition: fixed_alloc.c:51
#define NULL
Definition: freelistheap.h:34
int db_alignment(int n)
Definition: memory_alloc.c:192
UINTPTR hl_register_fixed_heap(int chunk_size)
Definition: customheaps.cpp:43
HL_HEAPID db_create_fixed_heap(int req_size, int recs_per_chunk)
Definition: fixed_alloc.c:64
void db_fixed_free(HL_HEAPID heap_id, void *ptr)
Definition: fixed_alloc.c:109
int db_align_to(int n, int alignment)
Definition: memory_alloc.c:224