1
/* Copyright (C) 2000-2001, 2003-2004 MySQL AB
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.
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.
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 */
17
/* Mallocs for used in threads */
19
#include "mysql_priv.h"
22
void sql_alloc_error_handler(void)
24
sql_print_error(ER(ER_OUT_OF_RESOURCES));
28
void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
30
init_alloc_root(mem_root, block_size, pre_alloc);
31
mem_root->error_handler=sql_alloc_error_handler;
35
void *sql_alloc(size_t Size)
37
MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
38
return alloc_root(root,Size);
42
void *sql_calloc(size_t size)
45
if ((ptr=sql_alloc(size)))
51
char *sql_strdup(const char *str)
53
size_t len= strlen(str)+1;
55
if ((pos= (char*) sql_alloc(len)))
61
char *sql_strmake(const char *str, size_t len)
64
if ((pos= (char*) sql_alloc(len+1)))
73
void* sql_memdup(const void *ptr, size_t len)
76
if ((pos= sql_alloc(len)))
81
void sql_element_free(void *ptr __attribute__((unused)))
82
{} /* purecov: deadcode */
86
char *sql_strmake_with_convert(const char *str, size_t arg_length,
87
CHARSET_INFO *from_cs,
88
size_t max_res_length,
89
CHARSET_INFO *to_cs, size_t *result_length)
92
size_t new_length= to_cs->mbmaxlen*arg_length;
93
max_res_length--; // Reserve place for end null
95
set_if_smaller(new_length, max_res_length);
96
if (!(pos= (char*) sql_alloc(new_length+1)))
99
if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
101
// Safety if to_cs->mbmaxlen > 0
102
new_length= min(arg_length, max_res_length);
103
memcpy(pos, str, new_length);
108
new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
109
arg_length, from_cs, &dummy_errors);
112
*result_length= new_length;