~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
670.2.1 by Monty Taylor
Moved pthread keys
23
1 by brian
clean slate
24
extern "C" {
25
  void sql_alloc_error_handler(void)
26
  {
512.1.8 by Stewart Smith
thr_malloc.cc: In function void sql_alloc_error_handler():
27
    sql_print_error("%s",ER(ER_OUT_OF_RESOURCES));
1 by brian
clean slate
28
  }
29
}
30
629.2.7 by Monty Taylor
Fixed a couple of memory buffer size issues.
31
void init_sql_alloc(MEM_ROOT *mem_root, size_t block_size, size_t pre_alloc)
1 by brian
clean slate
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
{
670.2.1 by Monty Taylor
Moved pthread keys
40
  MEM_ROOT *root= current_mem_root();
1 by brian
clean slate
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)))
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
49
    memset(ptr, 0, size);
1 by brian
clean slate
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
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
84
void sql_element_free(void *)
1 by brian
clean slate
85
{} /* purecov: deadcode */
86
87
88
89
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.
90
                               const CHARSET_INFO * const from_cs,
91
                               size_t max_res_length,
92
                               const CHARSET_INFO * const to_cs,
93
                               size_t *result_length)
1 by brian
clean slate
94
{
95
  char *pos;
96
  size_t new_length= to_cs->mbmaxlen*arg_length;
97
  max_res_length--;				// Reserve place for end null
98
99
  set_if_smaller(new_length, max_res_length);
100
  if (!(pos= (char*) sql_alloc(new_length+1)))
101
    return pos;					// Error
102
103
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
104
  {
105
    // Safety if to_cs->mbmaxlen > 0
398.1.4 by Monty Taylor
Renamed max/min.
106
    new_length= cmin(arg_length, max_res_length);
1 by brian
clean slate
107
    memcpy(pos, str, new_length);
108
  }
109
  else
110
  {
482 by Brian Aker
Remove uint.
111
    uint32_t dummy_errors;
1 by brian
clean slate
112
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
113
				 arg_length, from_cs, &dummy_errors);
114
  }
115
  pos[new_length]= 0;
116
  *result_length= new_length;
117
  return pos;
118
}
119