~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>
670.2.1 by Monty Taylor
Moved pthread keys
20
#include <drizzled/current_session.h>
549 by Monty Taylor
Took gettext.h out of header files.
21
#include <drizzled/error.h>
1 by brian
clean slate
22
23
extern "C" {
24
  void sql_alloc_error_handler(void)
25
  {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
26
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_OUT_OF_RESOURCES));
1 by brian
clean slate
27
  }
28
}
29
629.2.7 by Monty Taylor
Fixed a couple of memory buffer size issues.
30
void init_sql_alloc(MEM_ROOT *mem_root, size_t block_size, size_t pre_alloc)
1 by brian
clean slate
31
{
32
  init_alloc_root(mem_root, block_size, pre_alloc);
33
  mem_root->error_handler=sql_alloc_error_handler;
34
}
35
36
37
void *sql_alloc(size_t Size)
38
{
670.2.1 by Monty Taylor
Moved pthread keys
39
  MEM_ROOT *root= current_mem_root();
1 by brian
clean slate
40
  return alloc_root(root,Size);
41
}
42
43
44
void *sql_calloc(size_t size)
45
{
46
  void *ptr;
47
  if ((ptr=sql_alloc(size)))
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
48
    memset(ptr, 0, size);
1 by brian
clean slate
49
  return ptr;
50
}
51
52
53
char *sql_strdup(const char *str)
54
{
55
  size_t len= strlen(str)+1;
56
  char *pos;
57
  if ((pos= (char*) sql_alloc(len)))
58
    memcpy(pos,str,len);
59
  return pos;
60
}
61
62
63
char *sql_strmake(const char *str, size_t len)
64
{
65
  char *pos;
66
  if ((pos= (char*) sql_alloc(len+1)))
67
  {
68
    memcpy(pos,str,len);
69
    pos[len]=0;
70
  }
71
  return pos;
72
}
73
74
75
void* sql_memdup(const void *ptr, size_t len)
76
{
77
  void *pos;
78
  if ((pos= sql_alloc(len)))
79
    memcpy(pos,ptr,len);
80
  return pos;
81
}
82
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
83
void sql_element_free(void *)
1 by brian
clean slate
84
{} /* purecov: deadcode */
85
86
87
88
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.
89
                               const CHARSET_INFO * const from_cs,
90
                               size_t max_res_length,
91
                               const CHARSET_INFO * const to_cs,
92
                               size_t *result_length)
1 by brian
clean slate
93
{
94
  char *pos;
95
  size_t new_length= to_cs->mbmaxlen*arg_length;
96
  max_res_length--;				// Reserve place for end null
97
98
  set_if_smaller(new_length, max_res_length);
99
  if (!(pos= (char*) sql_alloc(new_length+1)))
100
    return pos;					// Error
101
102
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
103
  {
104
    // Safety if to_cs->mbmaxlen > 0
398.1.4 by Monty Taylor
Renamed max/min.
105
    new_length= cmin(arg_length, max_res_length);
1 by brian
clean slate
106
    memcpy(pos, str, new_length);
107
  }
108
  else
109
  {
482 by Brian Aker
Remove uint.
110
    uint32_t dummy_errors;
1 by brian
clean slate
111
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
112
				 arg_length, from_cs, &dummy_errors);
113
  }
114
  pos[new_length]= 0;
115
  *result_length= new_length;
116
  return pos;
117
}
118