~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/thr_malloc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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("%s", ER(ER_OUT_OF_RESOURCES));
 
25
 
 
26
    THD *thd= current_thd;
 
27
    if (thd)
 
28
    {
 
29
      if (! thd->is_error())
 
30
      {
 
31
        /*
 
32
          This thread is Out Of Memory.
 
33
          An OOM condition is a fatal error.
 
34
          It should not be caught by error handlers in stored procedures.
 
35
          Also, recording that SQL condition in the condition area could
 
36
          cause more memory allocations, which in turn could raise more
 
37
          OOM conditions, causing recursion in the error handling code itself.
 
38
          As a result, my_error() should not be invoked, and the
 
39
          thread diagnostics area is set to an error status directly.
 
40
          Note that Diagnostics_area::set_error_status() is safe,
 
41
          since it does not call any memory allocation routines.
 
42
          The visible result for a client application will be:
 
43
          - a query fails with an ER_OUT_OF_RESOURCES error,
 
44
          returned in the error packet.
 
45
          - SHOW ERROR/SHOW WARNINGS may be empty.
 
46
        */
 
47
        thd->main_da.set_error_status(thd,
 
48
                                      ER_OUT_OF_RESOURCES,
 
49
                                      ER(ER_OUT_OF_RESOURCES));
 
50
      }
 
51
    }
 
52
  }
 
53
}
 
54
 
 
55
void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
 
56
{
 
57
  init_alloc_root(mem_root, block_size, pre_alloc);
 
58
  mem_root->error_handler=sql_alloc_error_handler;
 
59
}
 
60
 
 
61
 
 
62
void *sql_alloc(size_t Size)
 
63
{
 
64
  MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
 
65
  return alloc_root(root,Size);
 
66
}
 
67
 
 
68
 
 
69
void *sql_calloc(size_t size)
 
70
{
 
71
  void *ptr;
 
72
  if ((ptr=sql_alloc(size)))
 
73
    bzero(ptr,size);
 
74
  return ptr;
 
75
}
 
76
 
 
77
 
 
78
char *sql_strdup(const char *str)
 
79
{
 
80
  size_t len= strlen(str)+1;
 
81
  char *pos;
 
82
  if ((pos= (char*) sql_alloc(len)))
 
83
    memcpy(pos,str,len);
 
84
  return pos;
 
85
}
 
86
 
 
87
 
 
88
char *sql_strmake(const char *str, size_t len)
 
89
{
 
90
  char *pos;
 
91
  if ((pos= (char*) sql_alloc(len+1)))
 
92
  {
 
93
    memcpy(pos,str,len);
 
94
    pos[len]=0;
 
95
  }
 
96
  return pos;
 
97
}
 
98
 
 
99
 
 
100
void* sql_memdup(const void *ptr, size_t len)
 
101
{
 
102
  void *pos;
 
103
  if ((pos= sql_alloc(len)))
 
104
    memcpy(pos,ptr,len);
 
105
  return pos;
 
106
}
 
107
 
 
108
void sql_element_free(void *ptr __attribute__((unused)))
 
109
{} /* purecov: deadcode */
 
110
 
 
111
 
 
112
 
 
113
char *sql_strmake_with_convert(const char *str, size_t arg_length,
 
114
                               CHARSET_INFO *from_cs,
 
115
                               size_t max_res_length,
 
116
                               CHARSET_INFO *to_cs, size_t *result_length)
 
117
{
 
118
  char *pos;
 
119
  size_t new_length= to_cs->mbmaxlen*arg_length;
 
120
  max_res_length--;                             // Reserve place for end null
 
121
 
 
122
  set_if_smaller(new_length, max_res_length);
 
123
  if (!(pos= (char*) sql_alloc(new_length+1)))
 
124
    return pos;                                 // Error
 
125
 
 
126
  if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
 
127
  {
 
128
    // Safety if to_cs->mbmaxlen > 0
 
129
    new_length= min(arg_length, max_res_length);
 
130
    memcpy(pos, str, new_length);
 
131
  }
 
132
  else
 
133
  {
 
134
    uint dummy_errors;
 
135
    new_length= copy_and_convert((char*) pos, new_length, to_cs, str,
 
136
                                 arg_length, from_cs, &dummy_errors);
 
137
  }
 
138
  pos[new_length]= 0;
 
139
  *result_length= new_length;
 
140
  return pos;
 
141
}
 
142