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
10GNU Libltdl is free software; you can redistribute it and/or
11modify it under the terms of the GNU Lesser General Public
12License as published by the Free Software Foundation; either
13version 2 of the License, or (at your option) any later version.
14
15As a special exception to the GNU Lesser General Public License,
16if you distribute this file as part of a program or library that
17is built using GNU Libtool, you may include this file under the
18same distribution terms that you use for the rest of that program.
19
20GNU Libltdl is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23GNU Lesser General Public License for more details.
24
25You should have received a copy of the GNU Lesser General Public
26License along with GNU Libltdl; see the file COPYING.LIB. If not, a
27copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
28or obtained by writing to the Free Software Foundation, Inc.,
2951 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
38static void alloc_die_default (void);
39
40void (*lt__alloc_die) (void) = alloc_die_default;
41
42/* Unless overridden, exit on memory failure. */
43static void
44alloc_die_default (void)
45{
46 fprintf (stderr, "Out of memory.\n");
47 exit (EXIT_FAILURE);
48}
49
50void *
51lt__malloc (size_t n)
52{
53 void *mem;
54
55 if (! (mem = malloc (n)))
56 (*lt__alloc_die) ();
57
58 return mem;
59}
60
61void *
62lt__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
72void *
73lt__realloc (void *mem, size_t n)
74{
75 if (! (mem = realloc (mem, n)))
76 (*lt__alloc_die) ();
77
78 return mem;
79}
80
81void *
82lt__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
92char *
93lt__strdup (const char *string)
94{
95 return (char *) lt__memdup (string, strlen (string) +1);
96}
97