~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>
549 by Monty Taylor
Took gettext.h out of header files.
20
#include <drizzled/error.h>
1 by brian
clean slate
21
22
extern "C" {
23
  void sql_alloc_error_handler(void)
24
  {
512.1.8 by Stewart Smith
thr_malloc.cc: In function void sql_alloc_error_handler():
25
    sql_print_error("%s",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
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
82
void sql_element_free(void *)
1 by brian
clean slate
83
{} /* purecov: deadcode */
84
85
86
87
char *sql_strmake_with_convert(const char *str, size_t arg_length,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
88
                               const CHARSET_INFO * const from_cs,
89
                               size_t max_res_length,
90
                               const CHARSET_INFO * const to_cs,
91
                               size_t *result_length)
1 by brian
clean slate
92
{
93
  char *pos;
94
  size_t new_length= to_cs->mbmaxlen*arg_length;
95
  max_res_length--;				// Reserve place for end null
96
97
  set_if_smaller(new_length, max_res_length);
98
  if (!(pos= (char*) sql_alloc(new_length+1)))
99
    return pos;					// Error
100
101
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
102
  {
103
    // Safety if to_cs->mbmaxlen > 0
398.1.4 by Monty Taylor
Renamed max/min.
104
    new_length= cmin(arg_length, max_res_length);
1 by brian
clean slate
105
    memcpy(pos, str, new_length);
106
  }
107
  else
108
  {
482 by Brian Aker
Remove uint.
109
    uint32_t dummy_errors;
1 by brian
clean slate
110
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
111
				 arg_length, from_cs, &dummy_errors);
112
  }
113
  pos[new_length]= 0;
114
  *result_length= new_length;
115
  return pos;
116
}
117