MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
thr_malloc.cc
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 
17 /* Mallocs for used in threads */
18 
19 #include "sql_priv.h"
20 #include "unireg.h"
21 #include "thr_malloc.h"
22 #include "sql_class.h"
23 
24 #include <algorithm>
25 
26 using std::min;
27 using std::max;
28 
29 extern "C" void sql_alloc_error_handler(void);
30 
31 void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
32 {
33  init_alloc_root(mem_root, block_size, pre_alloc);
34  mem_root->error_handler=sql_alloc_error_handler;
35 }
36 
37 
38 void *sql_alloc(size_t Size)
39 {
40  MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
41  return alloc_root(root,Size);
42 }
43 
44 
45 void *sql_calloc(size_t size)
46 {
47  void *ptr;
48  if ((ptr=sql_alloc(size)))
49  memset(ptr, 0, size);
50  return ptr;
51 }
52 
53 
54 char *sql_strdup(const char *str)
55 {
56  size_t len= strlen(str)+1;
57  char *pos;
58  if ((pos= (char*) sql_alloc(len)))
59  memcpy(pos,str,len);
60  return pos;
61 }
62 
63 
64 char *sql_strmake(const char *str, size_t len)
65 {
66  char *pos;
67  if ((pos= (char*) sql_alloc(len+1)))
68  {
69  memcpy(pos,str,len);
70  pos[len]=0;
71  }
72  return pos;
73 }
74 
75 
76 void* sql_memdup(const void *ptr, size_t len)
77 {
78  void *pos;
79  if ((pos= sql_alloc(len)))
80  memcpy(pos,ptr,len);
81  return pos;
82 }
83 
84 
85 char *sql_strmake_with_convert(const char *str, size_t arg_length,
86  const CHARSET_INFO *from_cs,
87  size_t max_res_length,
88  const CHARSET_INFO *to_cs, size_t *result_length)
89 {
90  char *pos;
91  size_t new_length= to_cs->mbmaxlen*arg_length;
92  max_res_length--; // Reserve place for end null
93 
94  set_if_smaller(new_length, max_res_length);
95  if (!(pos= (char*) sql_alloc(new_length+1)))
96  return pos; // Error
97 
98  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
99  {
100  // Safety if to_cs->mbmaxlen > 0
101  new_length= min(arg_length, max_res_length);
102  memcpy(pos, str, new_length);
103  }
104  else
105  {
106  uint dummy_errors;
107  new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
108  arg_length, from_cs, &dummy_errors);
109  }
110  pos[new_length]= 0;
111  *result_length= new_length;
112  return pos;
113 }
114