1 /*
2    Copyright (c) 2000, 2013, Oracle and/or its affiliates
3    Copyright (c) 2009, 2014, SkySQL Ab
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
17 
18 #include "mysys_priv.h"
19 #include "mysys_err.h"
20 #include <m_string.h>
21 
22 struct my_memory_header
23 {
24   PSI_thread *m_owner;
25   size_t m_size;
26   PSI_memory_key m_key;
27 };
28 typedef struct my_memory_header my_memory_header;
29 #define HEADER_SIZE 24
30 
31 #define USER_TO_HEADER(P) ((my_memory_header*)((char *)(P) - HEADER_SIZE))
32 #define HEADER_TO_USER(P) ((char*)(P) + HEADER_SIZE)
33 
34 /**
35   Inform application that memory usage has changed
36 
37   @param size	Size of memory segment allocated or freed
38   @param flag   1 if thread specific (allocated by MY_THREAD_SPECIFIC),
39                 0 if system specific.
40 
41   The type os size is long long, to be able to handle negative numbers to
42   decrement the memory usage
43 
44   @return 0 - ok
45           1 - failure, abort the allocation
46 */
dummy(long long size,my_bool is_thread_specific)47 static void dummy(long long size __attribute__((unused)),
48                   my_bool is_thread_specific __attribute__((unused)))
49 {}
50 
51 static MALLOC_SIZE_CB update_malloc_size= dummy;
52 
set_malloc_size_cb(MALLOC_SIZE_CB func)53 void set_malloc_size_cb(MALLOC_SIZE_CB func)
54 {
55   update_malloc_size= func ? func : dummy;
56 }
57 
58 
59 /**
60   Allocate a sized block of memory.
61 
62   @param size   The size of the memory block in bytes.
63   @param flags  Failure action modifiers (bitmasks).
64 
65   @return A pointer to the allocated memory block, or NULL on failure.
66 */
my_malloc(PSI_memory_key key,size_t size,myf my_flags)67 void *my_malloc(PSI_memory_key key, size_t size, myf my_flags)
68 {
69   my_memory_header *mh;
70   void *point;
71   DBUG_ENTER("my_malloc");
72   DBUG_PRINT("my",("size: %zu flags: %lu", size, my_flags));
73   compile_time_assert(sizeof(my_memory_header) <= HEADER_SIZE);
74 
75   if (!(my_flags & (MY_WME | MY_FAE)))
76     my_flags|= my_global_flags;
77 
78   /* Safety */
79   if (!size)
80     size=1;
81   if (size > SIZE_T_MAX - 1024L*1024L*16L)           /* Wrong call */
82     DBUG_RETURN(0);
83 
84   /* We have to align size as we store MY_THREAD_SPECIFIC flag in the LSB */
85   size= ALIGN_SIZE(size);
86 
87   if (DBUG_EVALUATE_IF("simulate_out_of_memory", 1, 0))
88     mh= NULL;
89   else
90     mh= (my_memory_header*) sf_malloc(size + HEADER_SIZE, my_flags);
91 
92   if (mh == NULL)
93   {
94     my_errno=errno;
95     if (my_flags & MY_FAE)
96       error_handler_hook=fatal_error_handler_hook;
97     if (my_flags & (MY_FAE+MY_WME))
98       my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_ERROR_LOG+ME_FATAL),size);
99     if (my_flags & MY_FAE)
100       abort();
101     point= NULL;
102   }
103   else
104   {
105     int flag= MY_TEST(my_flags & MY_THREAD_SPECIFIC);
106     mh->m_size= size | flag;
107     mh->m_key= PSI_CALL_memory_alloc(key, size, & mh->m_owner);
108     update_malloc_size(size + HEADER_SIZE, flag);
109     point= HEADER_TO_USER(mh);
110     if (my_flags & MY_ZEROFILL)
111       bzero(point, size);
112     else
113       TRASH_ALLOC(point, size);
114   }
115   DBUG_PRINT("exit",("ptr: %p", point));
116   DBUG_RETURN(point);
117 }
118 
119 
120 /**
121    @brief wrapper around realloc()
122 
123    @param  old_point        pointer to currently allocated area
124    @param  size            new size requested, must be >0
125    @param  my_flags        flags
126 
127    @note if size==0 realloc() may return NULL; my_realloc() treats this as an
128    error which is not the intention of realloc()
129 */
my_realloc(PSI_memory_key key,void * old_point,size_t size,myf my_flags)130 void *my_realloc(PSI_memory_key key, void *old_point, size_t size, myf my_flags)
131 {
132   my_memory_header *old_mh, *mh;
133   void *point;
134   size_t old_size;
135   my_bool old_flags;
136   DBUG_ENTER("my_realloc");
137   DBUG_PRINT("my",("ptr: %p size: %zu flags: %lu", old_point, size, my_flags));
138 
139   DBUG_ASSERT(size > 0);
140   if (!old_point && (my_flags & MY_ALLOW_ZERO_PTR))
141     DBUG_RETURN(my_malloc(key, size, my_flags));
142 
143   old_mh= USER_TO_HEADER(old_point);
144   old_size= old_mh->m_size & ~1;
145   old_flags= old_mh->m_size & 1;
146 
147   DBUG_ASSERT(old_mh->m_key == key || old_mh->m_key == PSI_NOT_INSTRUMENTED);
148   DBUG_ASSERT(old_flags == MY_TEST(my_flags & MY_THREAD_SPECIFIC));
149 
150   size= ALIGN_SIZE(size);
151   mh= sf_realloc(old_mh, size + HEADER_SIZE, my_flags);
152 
153   if (mh == NULL)
154   {
155     if (size < old_size)
156       DBUG_RETURN(old_point);
157     my_errno=errno;
158     if (my_flags & MY_FREE_ON_ERROR)
159     {
160       /* my_free will take care of size accounting */
161       my_free(old_point);
162       old_point= 0;
163     }
164     if (my_flags & (MY_FAE+MY_WME))
165       my_error(EE_OUTOFMEMORY, MYF(ME_BELL + ME_FATAL), size);
166     point= NULL;
167   }
168   else
169   {
170     mh->m_size= size | old_flags;
171     mh->m_key= PSI_CALL_memory_realloc(key, old_size, size, & mh->m_owner);
172     update_malloc_size((longlong)size - (longlong)old_size, old_flags);
173     point= HEADER_TO_USER(mh);
174   }
175 
176   DBUG_PRINT("exit",("ptr: %p", point));
177   DBUG_RETURN(point);
178 }
179 
180 
181 /**
182   Free memory allocated with my_malloc.
183 
184   @param ptr Pointer to the memory allocated by my_malloc.
185 */
my_free(void * ptr)186 void my_free(void *ptr)
187 {
188   my_memory_header *mh;
189   size_t old_size;
190   my_bool old_flags;
191   DBUG_ENTER("my_free");
192   DBUG_PRINT("my",("ptr: %p", ptr));
193 
194   if (ptr == NULL)
195     DBUG_VOID_RETURN;
196 
197   mh= USER_TO_HEADER(ptr);
198   old_size= mh->m_size & ~1;
199   old_flags= mh->m_size & 1;
200   PSI_CALL_memory_free(mh->m_key, old_size, mh->m_owner);
201 
202   update_malloc_size(- (longlong) old_size - HEADER_SIZE, old_flags);
203 
204 #ifndef SAFEMALLOC
205   /*
206     Trash memory if not safemalloc. We don't have to do this if safemalloc
207     is used as safemalloc will also do trashing
208   */
209   TRASH_FREE(ptr, old_size);
210 #endif
211   sf_free(mh);
212   DBUG_VOID_RETURN;
213 }
214 
215 
my_memdup(PSI_memory_key key,const void * from,size_t length,myf my_flags)216 void *my_memdup(PSI_memory_key key, const void *from, size_t length, myf my_flags)
217 {
218   void *ptr;
219   DBUG_ENTER("my_memdup");
220 
221   if ((ptr= my_malloc(key, length,my_flags)) != 0)
222     memcpy(ptr, from, length);
223   DBUG_RETURN(ptr);
224 }
225 
226 
my_strdup(PSI_memory_key key,const char * from,myf my_flags)227 char *my_strdup(PSI_memory_key key, const char *from, myf my_flags)
228 {
229   char *ptr;
230   size_t length= strlen(from)+1;
231   DBUG_ENTER("my_strdup");
232 
233   if ((ptr= (char*) my_malloc(key, length, my_flags)))
234     memcpy(ptr, from, length);
235   DBUG_RETURN(ptr);
236 }
237 
238 
my_strndup(PSI_memory_key key,const char * from,size_t length,myf my_flags)239 char *my_strndup(PSI_memory_key key, const char *from, size_t length, myf my_flags)
240 {
241   char *ptr;
242   DBUG_ENTER("my_strndup");
243 
244   if ((ptr= (char*) my_malloc(key, length+1, my_flags)))
245   {
246     memcpy(ptr, from, length);
247     ptr[length]= 0;
248   }
249   DBUG_RETURN(ptr);
250 }
251 
252