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("%s", ER(ER_OUT_OF_RESOURCES));
26
THD *thd= current_thd;
29
if (! thd->is_error())
32
This thread is Out Of Memory.
33
An OOM condition is a fatal error.
34
It should not be caught by error handlers in stored procedures.
35
Also, recording that SQL condition in the condition area could
36
cause more memory allocations, which in turn could raise more
37
OOM conditions, causing recursion in the error handling code itself.
38
As a result, my_error() should not be invoked, and the
39
thread diagnostics area is set to an error status directly.
40
Note that Diagnostics_area::set_error_status() is safe,
41
since it does not call any memory allocation routines.
42
The visible result for a client application will be:
43
- a query fails with an ER_OUT_OF_RESOURCES error,
44
returned in the error packet.
45
- SHOW ERROR/SHOW WARNINGS may be empty.
47
thd->main_da.set_error_status(thd,
49
ER(ER_OUT_OF_RESOURCES));
55
void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
57
init_alloc_root(mem_root, block_size, pre_alloc);
58
mem_root->error_handler=sql_alloc_error_handler;
62
void *sql_alloc(size_t Size)
64
MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
65
return alloc_root(root,Size);
69
void *sql_calloc(size_t size)
72
if ((ptr=sql_alloc(size)))
78
char *sql_strdup(const char *str)
80
size_t len= strlen(str)+1;
82
if ((pos= (char*) sql_alloc(len)))
88
char *sql_strmake(const char *str, size_t len)
91
if ((pos= (char*) sql_alloc(len+1)))
100
void* sql_memdup(const void *ptr, size_t len)
103
if ((pos= sql_alloc(len)))
108
void sql_element_free(void *ptr __attribute__((unused)))
109
{} /* purecov: deadcode */
113
char *sql_strmake_with_convert(const char *str, size_t arg_length,
114
CHARSET_INFO *from_cs,
115
size_t max_res_length,
116
CHARSET_INFO *to_cs, size_t *result_length)
119
size_t new_length= to_cs->mbmaxlen*arg_length;
120
max_res_length--; // Reserve place for end null
122
set_if_smaller(new_length, max_res_length);
123
if (!(pos= (char*) sql_alloc(new_length+1)))
126
if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
128
// Safety if to_cs->mbmaxlen > 0
129
new_length= min(arg_length, max_res_length);
130
memcpy(pos, str, new_length);
135
new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
136
arg_length, from_cs, &dummy_errors);
139
*result_length= new_length;