~drizzle-trunk/drizzle/development

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
19
#include "mysql_priv.h"
20
21
extern "C" {
22
  void sql_alloc_error_handler(void)
23
  {
24
    sql_print_error(ER(ER_OUT_OF_RESOURCES));
25
  }
26
}
27
28
void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
29
{
30
  init_alloc_root(mem_root, block_size, pre_alloc);
31
  mem_root->error_handler=sql_alloc_error_handler;
32
}
33
34
35
void *sql_alloc(size_t Size)
36
{
37
  MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
38
  return alloc_root(root,Size);
39
}
40
41
42
void *sql_calloc(size_t size)
43
{
44
  void *ptr;
45
  if ((ptr=sql_alloc(size)))
46
    bzero(ptr,size);
47
  return ptr;
48
}
49
50
51
char *sql_strdup(const char *str)
52
{
53
  size_t len= strlen(str)+1;
54
  char *pos;
55
  if ((pos= (char*) sql_alloc(len)))
56
    memcpy(pos,str,len);
57
  return pos;
58
}
59
60
61
char *sql_strmake(const char *str, size_t len)
62
{
63
  char *pos;
64
  if ((pos= (char*) sql_alloc(len+1)))
65
  {
66
    memcpy(pos,str,len);
67
    pos[len]=0;
68
  }
69
  return pos;
70
}
71
72
73
void* sql_memdup(const void *ptr, size_t len)
74
{
75
  void *pos;
76
  if ((pos= sql_alloc(len)))
77
    memcpy(pos,ptr,len);
78
  return pos;
79
}
80
81
void sql_element_free(void *ptr __attribute__((unused)))
82
{} /* purecov: deadcode */
83
84
85
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)
90
{
91
  char *pos;
92
  size_t new_length= to_cs->mbmaxlen*arg_length;
93
  max_res_length--;				// Reserve place for end null
94
95
  set_if_smaller(new_length, max_res_length);
96
  if (!(pos= (char*) sql_alloc(new_length+1)))
97
    return pos;					// Error
98
99
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
100
  {
101
    // Safety if to_cs->mbmaxlen > 0
102
    new_length= min(arg_length, max_res_length);
103
    memcpy(pos, str, new_length);
104
  }
105
  else
106
  {
107
    uint dummy_errors;
108
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
109
				 arg_length, from_cs, &dummy_errors);
110
  }
111
  pos[new_length]= 0;
112
  *result_length= new_length;
113
  return pos;
114
}
115