1361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper/* Convenience functions for allocation.
21ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaard   Copyright (C) 2006, 2015 Red Hat, Inc.
3de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   This file is part of elfutils.
4b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
5de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   This file is free software; you can redistribute it and/or modify
6de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   it under the terms of either
7b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
8de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard     * the GNU Lesser General Public License as published by the Free
9de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       Software Foundation; either version 3 of the License, or (at
10de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       your option) any later version
11de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
12de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   or
13de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
14de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard     * the GNU General Public License as published by the Free
15de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       Software Foundation; either version 2 of the License, or (at
16de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       your option) any later version
17de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
18de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   or both in parallel, as here.
19de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
20de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   elfutils is distributed in the hope that it will be useful, but
21361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   WITHOUT ANY WARRANTY; without even the implied warranty of
22361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   General Public License for more details.
24361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper
25de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   You should have received copies of the GNU General Public License and
26de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   the GNU Lesser General Public License along with this program.  If
27de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   not, see <http://www.gnu.org/licenses/>.  */
28b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
29b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifdef HAVE_CONFIG_H
30b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# include <config.h>
31b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif
32b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
33b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <error.h>
34b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <libintl.h>
35b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stddef.h>
36b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stdlib.h>
37b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <sys/types.h>
38b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include "system.h"
39b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
40b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifndef _
41b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# define _(str) gettext (str)
42b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif
43b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
44b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
45b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Allocate N bytes of memory dynamically, with error checking.  */
46b08d5a8fb42f4586d756068065186b5af7e48daUlrich Dreppervoid *
471ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaardxmalloc (size_t n)
48b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
49b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  void *p;
50b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
51b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  p = malloc (n);
52b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (p == NULL)
53b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    error (EXIT_FAILURE, 0, _("memory exhausted"));
54b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return p;
55b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
56b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
57b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
58b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Allocate memory for N elements of S bytes, with error checking.  */
59b08d5a8fb42f4586d756068065186b5af7e48daUlrich Dreppervoid *
601ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaardxcalloc (size_t n, size_t s)
61b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
62b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  void *p;
63b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
64b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  p = calloc (n, s);
65b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (p == NULL)
66b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    error (EXIT_FAILURE, 0, _("memory exhausted"));
67b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return p;
68b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
69b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
70b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
71b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Change the size of an allocated block of memory P to N bytes,
72b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   with error checking.  */
73b08d5a8fb42f4586d756068065186b5af7e48daUlrich Dreppervoid *
741ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaardxrealloc (void *p, size_t n)
75b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
76b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  p = realloc (p, n);
77b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (p == NULL)
78b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    error (EXIT_FAILURE, 0, _("memory exhausted"));
79b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return p;
80b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
81