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