~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/my_malloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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