mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_malloc.c
blob9deffeeae6946bf8141dffdd6f52fa020eb94b61
1 /*
2 Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */
19 #undef SAFEMALLOC
20 #endif
22 #include "mysys_priv.h"
23 #include "mysys_err.h"
24 #include <m_string.h>
26 /* My memory allocator */
28 void *my_malloc(size_t size, myf my_flags)
30 void* point;
31 DBUG_ENTER("my_malloc");
32 DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags));
34 if (!size)
35 size=1; /* Safety */
37 point= (char *) malloc(size);
38 DBUG_EXECUTE_IF("simulate_out_of_memory",
40 free(point);
41 point= NULL;
42 });
44 if (point == NULL)
46 my_errno=errno;
47 if (my_flags & MY_FAE)
48 error_handler_hook=fatal_error_handler_hook;
49 if (my_flags & (MY_FAE+MY_WME))
50 my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG+ME_NOREFRESH),size);
51 DBUG_EXECUTE_IF("simulate_out_of_memory",
52 DBUG_SET("-d,simulate_out_of_memory"););
53 if (my_flags & MY_FAE)
54 exit(1);
56 else if (my_flags & MY_ZEROFILL)
57 bzero(point,size);
58 DBUG_PRINT("exit",("ptr: 0x%lx", (long) point));
59 DBUG_RETURN((void*) point);
60 } /* my_malloc */
63 /* Free memory allocated with my_malloc */
64 /*ARGSUSED*/
66 void my_no_flags_free(void* ptr)
68 DBUG_ENTER("my_free");
69 DBUG_PRINT("my",("ptr: 0x%lx", (long) ptr));
70 if (ptr)
71 free(ptr);
72 DBUG_VOID_RETURN;
73 } /* my_free */
76 /* malloc and copy */
78 void* my_memdup(const void *from, size_t length, myf my_flags)
80 void *ptr;
81 if ((ptr= my_malloc(length,my_flags)) != 0)
82 memcpy(ptr, from, length);
83 return(ptr);
87 char *my_strdup(const char *from, myf my_flags)
89 char *ptr;
90 size_t length= strlen(from)+1;
91 if ((ptr= (char*) my_malloc(length, my_flags)))
92 memcpy((uchar*) ptr, (uchar*) from,(size_t) length);
93 return(ptr);
97 char *my_strndup(const char *from, size_t length, myf my_flags)
99 char *ptr;
100 if ((ptr= (char*) my_malloc(length+1,my_flags)) != 0)
102 memcpy((uchar*) ptr, (uchar*) from, length);
103 ptr[length]=0;
105 return((char*) ptr);