1 /* Copyright (c) 2000, 2013, 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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 Without limiting anything contained in the foregoing, this file,
15 which is part of C Driver for MySQL (Connector/C), is also subject to the
16 Universal FOSS Exception, version 1.0, a copy of which can be found at
17 http://oss.oracle.com/licenses/universal-foss-exception.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
27
28 #include "mysys_priv.h"
29 #include "mysys_err.h"
30 #include <m_string.h>
31
32 /**
33 Allocate a sized block of memory.
34
35 @param size The size of the memory block in bytes.
36 @param flags Failure action modifiers (bitmasks).
37
38 @return A pointer to the allocated memory block, or NULL on failure.
39 */
my_malloc(size_t size,myf my_flags)40 void *my_malloc(size_t size, myf my_flags)
41 {
42 void* point;
43 DBUG_ENTER("my_malloc");
44 DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags));
45
46 /* Safety */
47 if (!size)
48 size=1;
49
50 point= malloc(size);
51 DBUG_EXECUTE_IF("simulate_out_of_memory",
52 {
53 free(point);
54 point= NULL;
55 });
56 DBUG_EXECUTE_IF("simulate_persistent_out_of_memory",
57 {
58 free(point);
59 point= NULL;
60 });
61
62 if (point == NULL)
63 {
64 my_errno=errno;
65 if (my_flags & MY_FAE)
66 error_handler_hook=fatal_error_handler_hook;
67 if (my_flags & (MY_FAE+MY_WME))
68 my_error(EE_OUTOFMEMORY, MYF(ME_BELL + ME_WAITTANG +
69 ME_NOREFRESH + ME_FATALERROR),size);
70 DBUG_EXECUTE_IF("simulate_out_of_memory",
71 DBUG_SET("-d,simulate_out_of_memory"););
72 if (my_flags & MY_FAE)
73 exit(1);
74 }
75 else if (my_flags & MY_ZEROFILL)
76 memset(point, 0, size);
77 DBUG_PRINT("exit",("ptr: %p", point));
78 DBUG_RETURN(point);
79 }
80
81
82 /**
83 @brief wrapper around realloc()
84
85 @param oldpoint pointer to currently allocated area
86 @param size new size requested, must be >0
87 @param my_flags flags
88
89 @note if size==0 realloc() may return NULL; my_realloc() treats this as an
90 error which is not the intention of realloc()
91 */
my_realloc(void * oldpoint,size_t size,myf my_flags)92 void *my_realloc(void *oldpoint, size_t size, myf my_flags)
93 {
94 void *point;
95 DBUG_ENTER("my_realloc");
96 DBUG_PRINT("my",("ptr: %p size: %lu my_flags: %d", oldpoint,
97 (ulong) size, my_flags));
98
99 DBUG_ASSERT(size > 0);
100 /* These flags are mutually exclusive. */
101 DBUG_ASSERT(!((my_flags & MY_FREE_ON_ERROR) &&
102 (my_flags & MY_HOLD_ON_ERROR)));
103 DBUG_EXECUTE_IF("simulate_out_of_memory",
104 point= NULL;
105 goto end;);
106 if (!oldpoint && (my_flags & MY_ALLOW_ZERO_PTR))
107 DBUG_RETURN(my_malloc(size, my_flags));
108 #ifdef USE_HALLOC
109 point= malloc(size);
110 #else
111 point= realloc(oldpoint, size);
112 #endif
113 #ifndef DBUG_OFF
114 end:
115 #endif
116 if (point == NULL)
117 {
118 if (my_flags & MY_HOLD_ON_ERROR)
119 DBUG_RETURN(oldpoint);
120 if (my_flags & MY_FREE_ON_ERROR)
121 my_free(oldpoint);
122 my_errno=errno;
123 if (my_flags & (MY_FAE+MY_WME))
124 my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ ME_WAITTANG + ME_FATALERROR),
125 size);
126 DBUG_EXECUTE_IF("simulate_out_of_memory",
127 DBUG_SET("-d,simulate_out_of_memory"););
128 }
129 #ifdef USE_HALLOC
130 else
131 {
132 memcpy(point,oldpoint,size);
133 free(oldpoint);
134 }
135 #endif
136 DBUG_PRINT("exit",("ptr: %p", point));
137 DBUG_RETURN(point);
138 }
139
140
141 /**
142 Free memory allocated with my_malloc.
143
144 @remark Relies on free being able to handle a NULL argument.
145
146 @param ptr Pointer to the memory allocated by my_malloc.
147 */
my_free(void * ptr)148 void my_free(void *ptr)
149 {
150 DBUG_ENTER("my_free");
151 DBUG_PRINT("my",("ptr: %p", ptr));
152 free(ptr);
153 DBUG_VOID_RETURN;
154 }
155
156
my_memdup(const void * from,size_t length,myf my_flags)157 void *my_memdup(const void *from, size_t length, myf my_flags)
158 {
159 void *ptr;
160 if ((ptr= my_malloc(length,my_flags)) != 0)
161 memcpy(ptr, from, length);
162 return ptr;
163 }
164
165
my_strdup(const char * from,myf my_flags)166 char *my_strdup(const char *from, myf my_flags)
167 {
168 char *ptr;
169 size_t length= strlen(from)+1;
170 if ((ptr= (char*) my_malloc(length, my_flags)))
171 memcpy(ptr, from, length);
172 return ptr;
173 }
174
175
my_strndup(const char * from,size_t length,myf my_flags)176 char *my_strndup(const char *from, size_t length, myf my_flags)
177 {
178 char *ptr;
179 if ((ptr= (char*) my_malloc(length+1, my_flags)))
180 {
181 memcpy(ptr, from, length);
182 ptr[length]= 0;
183 }
184 return ptr;
185 }
186
187