1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2001, 2003-2004 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
||
17 |
/* Mallocs for used in threads */
|
|
18 |
||
243.1.17
by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.) |
19 |
#include <drizzled/server_includes.h> |
670.2.1
by Monty Taylor
Moved pthread keys |
20 |
#include <drizzled/current_session.h> |
549
by Monty Taylor
Took gettext.h out of header files. |
21 |
#include <drizzled/error.h> |
1
by brian
clean slate |
22 |
|
23 |
extern "C" { |
|
24 |
void sql_alloc_error_handler(void) |
|
25 |
{
|
|
755.2.1
by Mark Atwood
replace sql_print_error etc with errmsg_print |
26 |
errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_OUT_OF_RESOURCES)); |
1
by brian
clean slate |
27 |
}
|
28 |
}
|
|
29 |
||
629.2.7
by Monty Taylor
Fixed a couple of memory buffer size issues. |
30 |
void init_sql_alloc(MEM_ROOT *mem_root, size_t block_size, size_t pre_alloc) |
1
by brian
clean slate |
31 |
{
|
32 |
init_alloc_root(mem_root, block_size, pre_alloc); |
|
975.1.1
by Brian Aker
LCOV cleanup. |
33 |
mem_root->error_handler= sql_alloc_error_handler; |
1
by brian
clean slate |
34 |
}
|
35 |
||
36 |
||
37 |
void *sql_alloc(size_t Size) |
|
38 |
{
|
|
670.2.1
by Monty Taylor
Moved pthread keys |
39 |
MEM_ROOT *root= current_mem_root(); |
1
by brian
clean slate |
40 |
return alloc_root(root,Size); |
41 |
}
|
|
42 |
||
43 |
||
44 |
void *sql_calloc(size_t size) |
|
45 |
{
|
|
46 |
void *ptr; |
|
47 |
if ((ptr=sql_alloc(size))) |
|
212.6.1
by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file. |
48 |
memset(ptr, 0, size); |
1
by brian
clean slate |
49 |
return ptr; |
50 |
}
|
|
51 |
||
52 |
||
53 |
char *sql_strdup(const char *str) |
|
54 |
{
|
|
55 |
size_t len= strlen(str)+1; |
|
56 |
char *pos; |
|
57 |
if ((pos= (char*) sql_alloc(len))) |
|
58 |
memcpy(pos,str,len); |
|
59 |
return pos; |
|
60 |
}
|
|
61 |
||
62 |
||
63 |
char *sql_strmake(const char *str, size_t len) |
|
64 |
{
|
|
65 |
char *pos; |
|
66 |
if ((pos= (char*) sql_alloc(len+1))) |
|
67 |
{
|
|
68 |
memcpy(pos,str,len); |
|
69 |
pos[len]=0; |
|
70 |
}
|
|
71 |
return pos; |
|
72 |
}
|
|
73 |
||
74 |
||
75 |
void* sql_memdup(const void *ptr, size_t len) |
|
76 |
{
|
|
77 |
void *pos; |
|
78 |
if ((pos= sql_alloc(len))) |
|
79 |
memcpy(pos,ptr,len); |
|
80 |
return pos; |
|
81 |
}
|
|
82 |
||
83 |
char *sql_strmake_with_convert(const char *str, size_t arg_length, |
|
575.1.2
by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead. |
84 |
const CHARSET_INFO * const from_cs, |
85 |
size_t max_res_length, |
|
86 |
const CHARSET_INFO * const to_cs, |
|
87 |
size_t *result_length) |
|
1
by brian
clean slate |
88 |
{
|
89 |
char *pos; |
|
90 |
size_t new_length= to_cs->mbmaxlen*arg_length; |
|
91 |
max_res_length--; // Reserve place for end null |
|
92 |
||
93 |
set_if_smaller(new_length, max_res_length); |
|
94 |
if (!(pos= (char*) sql_alloc(new_length+1))) |
|
95 |
return pos; // Error |
|
96 |
||
97 |
if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin)) |
|
98 |
{
|
|
99 |
// Safety if to_cs->mbmaxlen > 0
|
|
398.1.4
by Monty Taylor
Renamed max/min. |
100 |
new_length= cmin(arg_length, max_res_length); |
1
by brian
clean slate |
101 |
memcpy(pos, str, new_length); |
102 |
}
|
|
103 |
else
|
|
104 |
{
|
|
482
by Brian Aker
Remove uint. |
105 |
uint32_t dummy_errors; |
1
by brian
clean slate |
106 |
new_length= copy_and_convert((char*) pos, new_length, to_cs, str, |
107 |
arg_length, from_cs, &dummy_errors); |
|
108 |
}
|
|
109 |
pos[new_length]= 0; |
|
110 |
*result_length= new_length; |
|
111 |
return pos; |
|
112 |
}
|
|
113 |