Coverage Report

Created: 2023-06-07 07:16

/src/gdbm/tools/mem.c
Line
Count
Source (jump to first uncovered line)
1
/* This file is part of GDBM, the GNU data base manager.
2
   Copyright (C) 2011-2023 Free Software Foundation, Inc.
3
4
   GDBM is free software; you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 3, or (at your option)
7
   any later version.
8
9
   GDBM is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
16
17
# include "autoconf.h"
18
# include "gdbm.h"
19
# include "gdbmapp.h"
20
# include "gdbmdefs.h"
21
22
void
23
ealloc_die (void)
24
0
{
25
0
  error ("%s", strerror (ENOMEM));
26
0
  exit (EXIT_FATAL);
27
0
}
28
29
void *
30
emalloc (size_t size)
31
103k
{
32
103k
  void *p = malloc (size);
33
103k
  if (!p)
34
0
    ealloc_die ();
35
103k
  return p;
36
103k
}
37
38
void *
39
erealloc (void *ptr, size_t size)
40
72.3k
{
41
72.3k
  void *newptr = realloc (ptr, size);
42
72.3k
  if (!newptr)
43
0
    ealloc_die ();
44
72.3k
  return newptr;
45
72.3k
}
46
47
void *
48
ecalloc (size_t nmemb, size_t size)
49
37.1k
{
50
37.1k
  void *p = calloc (nmemb, size);
51
37.1k
  if (!p)
52
0
    ealloc_die ();
53
37.1k
  return p;
54
37.1k
}
55
56
void *
57
ezalloc (size_t size)
58
0
{
59
0
  return ecalloc (1, size);
60
0
}
61
62
char *
63
estrdup (const char *str)
64
83.9k
{
65
83.9k
  char *p;
66
67
83.9k
  if (!str)
68
0
    return NULL;
69
83.9k
  p = emalloc (strlen (str) + 1);
70
83.9k
  strcpy (p, str);
71
83.9k
  return p;
72
83.9k
}
73
74
void *
75
e2nrealloc (void *p, size_t *pn, size_t s)
76
58.8k
{
77
58.8k
  size_t n = *pn;
78
58.8k
  char *newp;
79
  
80
58.8k
  if (!p)
81
58.8k
    {
82
58.8k
      if (!n)
83
58.8k
  {
84
    /* The approximate size to use for initial small
85
       allocation requests, when the invoking code
86
       specifies an old size of zero.  64 bytes is
87
       the largest "small" request for the
88
       GNU C library malloc.  */
89
58.8k
    enum { DEFAULT_MXFAST = 64 };
90
    
91
58.8k
    n = DEFAULT_MXFAST / s;
92
58.8k
    n += !n;
93
58.8k
  }
94
58.8k
    }
95
0
  else if ((size_t) -1 / 3 * 2 / s <= n)
96
0
    {
97
0
      ealloc_die ();
98
0
    }
99
0
  else
100
0
    n += (n + 1) / 2;
101
102
58.8k
  newp = erealloc (p, n * s);
103
58.8k
  *pn = n;
104
58.8k
  return newp;
105
58.8k
}
106
107