forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_malloc.cpp
More file actions
165 lines (155 loc) · 4.95 KB
/
parse_malloc.cpp
File metadata and controls
165 lines (155 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_PARSER
#include "sql/parser/parse_malloc.h"
#include <string.h>
#include <lib/alloc/alloc_assist.h>
#include "lib/charset/ob_ctype.h"
#include "sql/parser/parse_define.h"
#include "sql/parser/parser_proxy_func.h"
// get memory from the thread obStringBuf, and not release untill thread quits
void* parse_malloc(const size_t nbyte, void* malloc_pool)
{
void* ptr = NULL;
size_t headlen = sizeof(int64_t);
if (OB_ISNULL(malloc_pool)) {
} else if (OB_UNLIKELY(nbyte <= 0)) {
} else {
if (OB_UNLIKELY(NULL == (ptr = parser_alloc_buffer(malloc_pool, headlen + nbyte)))) {
} else {
*(static_cast<int64_t*>(ptr)) = nbyte;
ptr = static_cast<char*>(ptr) + headlen;
MEMSET(ptr, 0, nbyte);
}
}
return ptr;
}
/* ptr must point to a memory allocated by parse_malloc.cpp */
void* parse_realloc(void* ptr, size_t nbyte, void* malloc_pool)
{
void* new_ptr = NULL;
// need not to check nbyte
if (OB_ISNULL(malloc_pool)) {
} else {
if (OB_UNLIKELY(NULL == ptr)) {
new_ptr = parser_alloc_buffer(malloc_pool, nbyte);
} else {
size_t headlen = sizeof(int64_t);
if (OB_UNLIKELY(NULL == (new_ptr = parser_alloc_buffer(malloc_pool, headlen + nbyte)))) {
} else {
int64_t obyte = *(reinterpret_cast<int64_t*>(static_cast<char*>(ptr) - headlen));
*(static_cast<int64_t*>(new_ptr)) = nbyte;
new_ptr = static_cast<char*>(new_ptr) + headlen;
MEMMOVE(new_ptr, ptr, static_cast<int64_t>(nbyte) > obyte ? obyte : nbyte);
parser_free_buffer(malloc_pool, static_cast<char*>(ptr) - headlen);
}
}
}
return new_ptr;
}
char* parse_strndup(const char* str, size_t nbyte, void* malloc_pool)
{
char* new_str = NULL;
// need not to check nbyte
if (OB_ISNULL(str)) {
} else {
if (OB_LIKELY(NULL != (new_str = static_cast<char*>(parse_malloc(nbyte + 1, malloc_pool))))) {
MEMMOVE(new_str, str, nbyte);
new_str[nbyte] = '\0';
} else {
}
}
return new_str;
}
char* parse_strdup(const char* str, void* malloc_pool, int64_t* out_len)
{
char* out_str = NULL;
if (OB_ISNULL(str)) {
} else if (OB_ISNULL(out_len)) {
} else {
size_t dup_len = STRLEN(str);
out_str = parse_strndup(str, dup_len, malloc_pool);
*out_len = dup_len;
}
return out_str;
}
char* parse_str_convert_utf8(
const struct ObCharsetInfo* src_cs, const char* str, void* malloc_pool, int64_t* out_len, int* extra_errno)
{
char* out_str = NULL;
if (OB_ISNULL(str) || OB_ISNULL(out_len) || OB_ISNULL(extra_errno)) {
} else {
uint errors;
size_t str_len = STRLEN(str);
size_t dst_len = str_len * 4;
if (OB_ISNULL(out_str = static_cast<char*>(parse_malloc(dst_len + 1, malloc_pool)))) {
} else {
*out_len = ob_convert(out_str, dst_len, &ob_charset_utf8mb4_general_ci, str, str_len, src_cs, &errors);
out_str[*out_len] = '\0';
if (0 != errors) {
*extra_errno = OB_PARSER_ERR_ILLEGAL_NAME;
}
}
}
return out_str;
}
char* strndup_with_prefix(const char* prefix, const char* str, size_t nbyte, void* malloc_pool)
{
char* new_str = NULL;
size_t prefix_len = strlen(prefix);
// need not to check nbyte
if (OB_ISNULL(str)) {
} else {
if (OB_LIKELY(NULL != (new_str = static_cast<char*>(parse_malloc(nbyte + prefix_len, malloc_pool))))) {
MEMMOVE(new_str, prefix, prefix_len);
MEMMOVE(new_str + prefix_len, str, nbyte);
} else {
// do nothing
}
}
return new_str;
}
char* strndup_with_prefix_and_postfix(
const char* prefix, const char* postfix, const char* str, size_t nbyte, void* malloc_pool)
{
char* new_str = NULL;
size_t prefix_len = strlen(prefix);
size_t postfix_len = strlen(postfix);
if (OB_ISNULL(str)) {
} else {
if (OB_LIKELY(
NULL != (new_str = static_cast<char*>(parse_malloc(nbyte + prefix_len + postfix_len, malloc_pool))))) {
MEMMOVE(new_str, prefix, prefix_len);
MEMMOVE(new_str + prefix_len, str, nbyte);
MEMMOVE(new_str + prefix_len + nbyte, postfix, postfix_len);
} else {
}
}
return new_str;
}
void parse_free(void* ptr)
{
UNUSED(ptr);
/* do nothing, we don't really free the memory */
}
char* cp_str_value(const char* src, const size_t nbyte, void* malloc_pool)
{
char* new_str = NULL;
if (OB_ISNULL(src)) {
} else {
if (OB_LIKELY(NULL != (new_str = static_cast<char*>(parse_malloc(nbyte, malloc_pool))))) {
MEMCPY(new_str, src, nbyte);
} else {
}
}
return new_str;
}