~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
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
19
#include <drizzled/server_includes.h>
202.3.6 by Monty Taylor
First pass at gettexizing the error messages.
20
#include <drizzled/drizzled_error_messages.h>
1 by brian
clean slate
21
22
extern "C" {
23
  void sql_alloc_error_handler(void)
24
  {
261.3.7 by Monty Taylor
Made the ER() macro always gettext the return value.
25
    sql_print_error(ER(ER_OUT_OF_RESOURCES));
1 by brian
clean slate
26
  }
27
}
28
482 by Brian Aker
Remove uint.
29
void init_sql_alloc(MEM_ROOT *mem_root, uint32_t block_size, uint32_t pre_alloc)
1 by brian
clean slate
30
{
31
  init_alloc_root(mem_root, block_size, pre_alloc);
32
  mem_root->error_handler=sql_alloc_error_handler;
33
}
34
35
36
void *sql_alloc(size_t Size)
37
{
4 by Brian Aker
Remove my_pthread_getspecific_ptr()
38
  MEM_ROOT *root= *(MEM_ROOT **)pthread_getspecific(THR_MALLOC);
1 by brian
clean slate
39
  return alloc_root(root,Size);
40
}
41
42
43
void *sql_calloc(size_t size)
44
{
45
  void *ptr;
46
  if ((ptr=sql_alloc(size)))
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
47
    memset(ptr, 0, size);
1 by brian
clean slate
48
  return ptr;
49
}
50
51
52
char *sql_strdup(const char *str)
53
{
54
  size_t len= strlen(str)+1;
55
  char *pos;
56
  if ((pos= (char*) sql_alloc(len)))
57
    memcpy(pos,str,len);
58
  return pos;
59
}
60
61
62
char *sql_strmake(const char *str, size_t len)
63
{
64
  char *pos;
65
  if ((pos= (char*) sql_alloc(len+1)))
66
  {
67
    memcpy(pos,str,len);
68
    pos[len]=0;
69
  }
70
  return pos;
71
}
72
73
74
void* sql_memdup(const void *ptr, size_t len)
75
{
76
  void *pos;
77
  if ((pos= sql_alloc(len)))
78
    memcpy(pos,ptr,len);
79
  return pos;
80
}
81
82
void sql_element_free(void *ptr __attribute__((unused)))
83
{} /* purecov: deadcode */
84
85
86
87
char *sql_strmake_with_convert(const char *str, size_t arg_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
88
			       const CHARSET_INFO * const from_cs,
1 by brian
clean slate
89
			       size_t max_res_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
90
			       const CHARSET_INFO * const to_cs, size_t *result_length)
1 by brian
clean slate
91
{
92
  char *pos;
93
  size_t new_length= to_cs->mbmaxlen*arg_length;
94
  max_res_length--;				// Reserve place for end null
95
96
  set_if_smaller(new_length, max_res_length);
97
  if (!(pos= (char*) sql_alloc(new_length+1)))
98
    return pos;					// Error
99
100
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
101
  {
102
    // Safety if to_cs->mbmaxlen > 0
398.1.4 by Monty Taylor
Renamed max/min.
103
    new_length= cmin(arg_length, max_res_length);
1 by brian
clean slate
104
    memcpy(pos, str, new_length);
105
  }
106
  else
107
  {
482 by Brian Aker
Remove uint.
108
    uint32_t dummy_errors;
1 by brian
clean slate
109
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
110
				 arg_length, from_cs, &dummy_errors);
111
  }
112
  pos[new_length]= 0;
113
  *result_length= new_length;
114
  return pos;
115
}
116