1 |
michael |
945 |
/* lt__alloc.c -- internal memory management interface |
2 |
|
|
|
3 |
|
|
Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc. |
4 |
|
|
Written by Gary V. Vaughan, 2004 |
5 |
|
|
|
6 |
|
|
NOTE: The canonical source of this file is maintained with the |
7 |
|
|
GNU Libtool package. Report bugs to bug-libtool@gnu.org. |
8 |
|
|
|
9 |
|
|
GNU Libltdl is free software; you can redistribute it and/or |
10 |
|
|
modify it under the terms of the GNU Lesser General Public |
11 |
|
|
License as published by the Free Software Foundation; either |
12 |
|
|
version 2 of the License, or (at your option) any later version. |
13 |
|
|
|
14 |
|
|
As a special exception to the GNU Lesser General Public License, |
15 |
|
|
if you distribute this file as part of a program or library that |
16 |
|
|
is built using GNU Libtool, you may include this file under the |
17 |
|
|
same distribution terms that you use for the rest of that program. |
18 |
|
|
|
19 |
|
|
GNU Libltdl is distributed in the hope that it will be useful, |
20 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 |
|
|
GNU Lesser General Public License for more details. |
23 |
|
|
|
24 |
|
|
You should have received a copy of the GNU Lesser General Public |
25 |
|
|
License along with GNU Libltdl; see the file COPYING.LIB. If not, a |
26 |
|
|
copy can be downloaded from http://www.gnu.org/licenses/lgpl.html, |
27 |
|
|
or obtained by writing to the Free Software Foundation, Inc., |
28 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
29 |
|
|
*/ |
30 |
|
|
|
31 |
|
|
#include "lt__private.h" |
32 |
|
|
|
33 |
|
|
#include <stdio.h> |
34 |
|
|
|
35 |
|
|
#include "lt__alloc.h" |
36 |
|
|
|
37 |
|
|
static void alloc_die_default (void); |
38 |
|
|
|
39 |
|
|
void (*lt__alloc_die) (void) = alloc_die_default; |
40 |
|
|
|
41 |
|
|
/* Unless overridden, exit on memory failure. */ |
42 |
|
|
static void |
43 |
|
|
alloc_die_default (void) |
44 |
|
|
{ |
45 |
|
|
fprintf (stderr, "Out of memory.\n"); |
46 |
|
|
exit (EXIT_FAILURE); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
void * |
50 |
|
|
lt__malloc (size_t n) |
51 |
|
|
{ |
52 |
|
|
void *mem; |
53 |
|
|
|
54 |
|
|
if (! (mem = malloc (n))) |
55 |
|
|
(*lt__alloc_die) (); |
56 |
|
|
|
57 |
|
|
return mem; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
void * |
61 |
|
|
lt__zalloc (size_t n) |
62 |
|
|
{ |
63 |
|
|
void *mem; |
64 |
|
|
|
65 |
|
|
if ((mem = lt__malloc (n))) |
66 |
|
|
memset (mem, 0, n); |
67 |
|
|
|
68 |
|
|
return mem; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
void * |
72 |
|
|
lt__realloc (void *mem, size_t n) |
73 |
|
|
{ |
74 |
|
|
if (! (mem = realloc (mem, n))) |
75 |
|
|
(*lt__alloc_die) (); |
76 |
|
|
|
77 |
|
|
return mem; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void * |
81 |
|
|
lt__memdup (void const *mem, size_t n) |
82 |
|
|
{ |
83 |
|
|
void *newmem; |
84 |
|
|
|
85 |
|
|
if ((newmem = lt__malloc (n))) |
86 |
|
|
return memcpy (newmem, mem, n); |
87 |
|
|
|
88 |
|
|
return 0; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
char * |
92 |
|
|
lt__strdup (const char *string) |
93 |
|
|
{ |
94 |
|
|
return (char *) lt__memdup (string, strlen (string) +1); |
95 |
|
|
} |