00001 /* Copyright (C) 2000 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ 00018 #undef SAFEMALLOC 00019 #endif 00020 00021 #include "mysys_priv.h" 00022 #include "mysys_err.h" 00023 #include <m_string.h> 00024 00025 /* My memory allocator */ 00026 00027 gptr my_malloc(unsigned int size, myf my_flags) 00028 { 00029 gptr point; 00030 DBUG_ENTER("my_malloc"); 00031 DBUG_PRINT("my",("size: %u my_flags: %d",size, my_flags)); 00032 00033 if (!size) 00034 size=1; /* Safety */ 00035 if ((point = (char*)malloc(size)) == NULL) 00036 { 00037 my_errno=errno; 00038 if (my_flags & MY_FAE) 00039 error_handler_hook=fatal_error_handler_hook; 00040 if (my_flags & (MY_FAE+MY_WME)) 00041 my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size); 00042 if (my_flags & MY_FAE) 00043 exit(1); 00044 } 00045 else if (my_flags & MY_ZEROFILL) 00046 bzero(point,size); 00047 DBUG_PRINT("exit",("ptr: 0x%lx",point)); 00048 DBUG_RETURN(point); 00049 } /* my_malloc */ 00050 00051 00052 /* Free memory allocated with my_malloc */ 00053 /*ARGSUSED*/ 00054 00055 void my_no_flags_free(gptr ptr) 00056 { 00057 DBUG_ENTER("my_free"); 00058 DBUG_PRINT("my",("ptr: 0x%lx",ptr)); 00059 if (ptr) 00060 free(ptr); 00061 DBUG_VOID_RETURN; 00062 } /* my_free */ 00063 00064 00065 /* malloc and copy */ 00066 00067 gptr my_memdup(const byte *from, uint length, myf my_flags) 00068 { 00069 gptr ptr; 00070 if ((ptr=my_malloc(length,my_flags)) != 0) 00071 memcpy((byte*) ptr, (byte*) from,(size_t) length); 00072 return(ptr); 00073 } 00074 00075 00076 char *my_strdup(const char *from, myf my_flags) 00077 { 00078 gptr ptr; 00079 uint length=(uint) strlen(from)+1; 00080 if ((ptr=my_malloc(length,my_flags)) != 0) 00081 memcpy((byte*) ptr, (byte*) from,(size_t) length); 00082 return((my_string) ptr); 00083 } 00084 00085 00086 char *my_strndup(const char *from, uint length, myf my_flags) 00087 { 00088 gptr ptr; 00089 if ((ptr=my_malloc(length+1,my_flags)) != 0) 00090 { 00091 memcpy((byte*) ptr, (byte*) from,(size_t) length); 00092 ((char*) ptr)[length]=0; 00093 } 00094 return((char*) ptr); 00095 }
1.4.7

