summaryrefslogtreecommitdiff
path: root/gprofng/src/dbe_memmgr.c
blob: 097314e924a4304a2836e75ea27b9abaa9e1f9e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (C) 2021-2023 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "config.h"
#include <dlfcn.h>
#include "util.h"

#define CHECK_OUT_OF_MEM(ptr, size) if (ptr == NULL) err_out_of_memory(size)

/* Report Out of Memory error and exit */
static void
err_out_of_memory (unsigned nbytes)
{
  char *nm = get_prog_name (1);
  if (nm)
    fprintf (stderr, GTXT ("%s: Error: Memory capacity exceeded.\n"), nm);
  else
    fprintf (stderr, GTXT ("Error: Memory capacity exceeded.\n"));
  fprintf (stderr, GTXT ("  Requested %u bytes.\n"), nbytes);
  exit (16);
}

#define CALL_REAL(x) (__real_##x)
#define NULL_PTR(x) ( __real_##x == NULL )

static void *(*__real_malloc)(size_t) = NULL;
static void (*__real_free)(void *) = NULL;
static void *(*__real_realloc)(void *, size_t) = NULL;
static void *(*__real_calloc)(size_t, size_t) = NULL;
static char *(*__real_strdup)(const char*) = NULL;
static volatile int in_init = 0;

static int
init_heap_intf ()
{
  in_init = 1;
  __real_malloc = (void*(*)(size_t))dlsym (RTLD_NEXT, "malloc");
  __real_free = (void(*)(void *))dlsym (RTLD_NEXT, "free");
  __real_realloc = (void*(*)(void *, size_t))dlsym (RTLD_NEXT, "realloc");
  __real_calloc = (void*(*)(size_t, size_t))dlsym (RTLD_NEXT, "calloc");
  __real_strdup = (char*(*)(const char*))dlsym (RTLD_NEXT, "strdup");
  in_init = 0;
  return 0;
}

/* --------------------------------------------------------------------------- */
/* libc's memory management functions substitutions */

/* Allocate memory and make sure we got some */
void *
malloc (size_t size)
{
  if (NULL_PTR (malloc))
    init_heap_intf ();
  void *ptr = CALL_REAL (malloc)(size);
  CHECK_OUT_OF_MEM (ptr, size);
  return ptr;
}


/* Implement a workaround for a libdl recursion problem */
void *
calloc (size_t nelem, size_t size)
{
  if (NULL_PTR (calloc))
    {
      /* If a program is linked with libpthread then the following
       * calling sequence occurs:
       * init_heap_intf -> dlsym -> calloc -> malloc -> init_heap_intf
       * We break some performance improvement in libdl by returning
       * NULL but preserve functionality.
       */
      if (in_init)
	return NULL;
      init_heap_intf ();
    }
  return CALL_REAL (calloc)(nelem, size);
}

/* Free the storage associated with data */
void
free (void *ptr)
{
  if (ptr == NULL)
    return;
  if (NULL_PTR (free))
    init_heap_intf ();
  CALL_REAL (free)(ptr);
  return;
}

/* Reallocate buffer */
void *
realloc (void *ptr, size_t size)
{
  if (NULL_PTR (realloc))
    init_heap_intf ();
  ptr = CALL_REAL (realloc)(ptr, size);
  CHECK_OUT_OF_MEM (ptr, size);
  return ptr;
}