MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_malloc.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "mysys_priv.h"
17 #include "mysys_err.h"
18 #include <m_string.h>
19 
28 void *my_malloc(size_t size, myf my_flags)
29 {
30  void* point;
31  DBUG_ENTER("my_malloc");
32  DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags));
33 
34  /* Safety */
35  if (!size)
36  size=1;
37 
38  point= malloc(size);
39  DBUG_EXECUTE_IF("simulate_out_of_memory",
40  {
41  free(point);
42  point= NULL;
43  });
44  DBUG_EXECUTE_IF("simulate_persistent_out_of_memory",
45  {
46  free(point);
47  point= NULL;
48  });
49 
50  if (point == NULL)
51  {
52  my_errno=errno;
53  if (my_flags & MY_FAE)
54  error_handler_hook=fatal_error_handler_hook;
55  if (my_flags & (MY_FAE+MY_WME))
56  my_error(EE_OUTOFMEMORY, MYF(ME_BELL + ME_WAITTANG +
57  ME_NOREFRESH + ME_FATALERROR),size);
58  DBUG_EXECUTE_IF("simulate_out_of_memory",
59  DBUG_SET("-d,simulate_out_of_memory"););
60  if (my_flags & MY_FAE)
61  exit(1);
62  }
63  else if (my_flags & MY_ZEROFILL)
64  memset(point, 0, size);
65  DBUG_PRINT("exit",("ptr: %p", point));
66  DBUG_RETURN(point);
67 }
68 
69 
80 void *my_realloc(void *oldpoint, size_t size, myf my_flags)
81 {
82  void *point;
83  DBUG_ENTER("my_realloc");
84  DBUG_PRINT("my",("ptr: %p size: %lu my_flags: %d", oldpoint,
85  (ulong) size, my_flags));
86 
87  DBUG_ASSERT(size > 0);
88  DBUG_EXECUTE_IF("simulate_out_of_memory",
89  point= NULL;
90  goto end;);
91  if (!oldpoint && (my_flags & MY_ALLOW_ZERO_PTR))
92  DBUG_RETURN(my_malloc(size, my_flags));
93 #ifdef USE_HALLOC
94  point= malloc(size);
95 #else
96  point= realloc(oldpoint, size);
97 #endif
98 #ifndef DBUG_OFF
99 end:
100 #endif
101  if (point == NULL)
102  {
103  if (my_flags & MY_FREE_ON_ERROR)
104  my_free(oldpoint);
105  if (my_flags & MY_HOLD_ON_ERROR)
106  DBUG_RETURN(oldpoint);
107  my_errno=errno;
108  if (my_flags & (MY_FAE+MY_WME))
109  my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ ME_WAITTANG + ME_FATALERROR),
110  size);
111  DBUG_EXECUTE_IF("simulate_out_of_memory",
112  DBUG_SET("-d,simulate_out_of_memory"););
113  }
114 #ifdef USE_HALLOC
115  else
116  {
117  memcpy(point,oldpoint,size);
118  free(oldpoint);
119  }
120 #endif
121  DBUG_PRINT("exit",("ptr: %p", point));
122  DBUG_RETURN(point);
123 }
124 
125 
133 void my_free(void *ptr)
134 {
135  DBUG_ENTER("my_free");
136  DBUG_PRINT("my",("ptr: %p", ptr));
137  free(ptr);
138  DBUG_VOID_RETURN;
139 }
140 
141 
142 void *my_memdup(const void *from, size_t length, myf my_flags)
143 {
144  void *ptr;
145  if ((ptr= my_malloc(length,my_flags)) != 0)
146  memcpy(ptr, from, length);
147  return ptr;
148 }
149 
150 
151 char *my_strdup(const char *from, myf my_flags)
152 {
153  char *ptr;
154  size_t length= strlen(from)+1;
155  if ((ptr= (char*) my_malloc(length, my_flags)))
156  memcpy(ptr, from, length);
157  return ptr;
158 }
159 
160 
161 char *my_strndup(const char *from, size_t length, myf my_flags)
162 {
163  char *ptr;
164  if ((ptr= (char*) my_malloc(length+1, my_flags)))
165  {
166  memcpy(ptr, from, length);
167  ptr[length]= 0;
168  }
169  return ptr;
170 }
171