1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
#include "mysys_priv.h" |
|
17 |
#include "mysys_err.h" |
|
18 |
#include <m_string.h> |
|
19 |
||
20 |
/* My memory allocator */
|
|
21 |
||
22 |
void *my_malloc(size_t size, myf my_flags) |
|
23 |
{
|
|
24 |
void* point; |
|
25 |
DBUG_ENTER("my_malloc"); |
|
26 |
DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags)); |
|
27 |
||
28 |
if (!size) |
|
29 |
size=1; /* Safety */ |
|
30 |
if ((point = (char*)malloc(size)) == NULL) |
|
31 |
{
|
|
32 |
my_errno=errno; |
|
33 |
if (my_flags & MY_FAE) |
|
34 |
error_handler_hook=fatal_error_handler_hook; |
|
35 |
if (my_flags & (MY_FAE+MY_WME)) |
|
36 |
my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG+ME_NOREFRESH),size); |
|
37 |
if (my_flags & MY_FAE) |
|
38 |
exit(1); |
|
39 |
}
|
|
40 |
else if (my_flags & MY_ZEROFILL) |
|
41 |
bzero(point,size); |
|
42 |
DBUG_PRINT("exit",("ptr: 0x%lx", (long) point)); |
|
43 |
DBUG_RETURN((void*) point); |
|
44 |
} /* my_malloc */ |
|
45 |
||
46 |
||
47 |
/* Free memory allocated with my_malloc */
|
|
48 |
/*ARGSUSED*/
|
|
49 |
||
50 |
void my_no_flags_free(void* ptr) |
|
51 |
{
|
|
52 |
DBUG_ENTER("my_free"); |
|
53 |
DBUG_PRINT("my",("ptr: 0x%lx", (long) ptr)); |
|
54 |
if (ptr) |
|
55 |
free(ptr); |
|
56 |
DBUG_VOID_RETURN; |
|
57 |
} /* my_free */ |
|
58 |
||
59 |
||
60 |
/* malloc and copy */
|
|
61 |
||
62 |
void* my_memdup(const void *from, size_t length, myf my_flags) |
|
63 |
{
|
|
64 |
void *ptr; |
|
65 |
if ((ptr= my_malloc(length,my_flags)) != 0) |
|
66 |
memcpy(ptr, from, length); |
|
67 |
return(ptr); |
|
68 |
}
|
|
69 |
||
70 |
||
71 |
char *my_strdup(const char *from, myf my_flags) |
|
72 |
{
|
|
73 |
char *ptr; |
|
74 |
size_t length= strlen(from)+1; |
|
75 |
if ((ptr= (char*) my_malloc(length, my_flags))) |
|
76 |
memcpy((uchar*) ptr, (uchar*) from,(size_t) length); |
|
77 |
return(ptr); |
|
78 |
}
|
|
79 |
||
80 |
||
81 |
char *my_strndup(const char *from, size_t length, myf my_flags) |
|
82 |
{
|
|
83 |
char *ptr; |
|
84 |
if ((ptr= (char*) my_malloc(length+1,my_flags)) != 0) |
|
85 |
{
|
|
86 |
memcpy((uchar*) ptr, (uchar*) from, length); |
|
87 |
ptr[length]=0; |
|
88 |
}
|
|
89 |
return((char*) ptr); |
|
90 |
}
|