alloc.c
2.45 KB
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
/* Error-proof memory allocation functions.
Copyright (C) 2008, 2010-2012, 2014-2017 Free Software Foundation,
Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/nls.h>
#include <mailutils/alloc.h>
void (*mu_alloc_die_hook) (void) = NULL;
void
mu_alloc_die ()
{
if (mu_alloc_die_hook)
mu_alloc_die_hook ();
mu_error (_("Not enough memory"));
abort ();
}
void *
mu_alloc (size_t size)
{
void *p = malloc (size);
if (!p)
mu_alloc_die ();
return p;
}
void *
mu_calloc (size_t nmemb, size_t size)
{
void *p = calloc (nmemb, size);
if (!p)
mu_alloc_die ();
return p;
}
void *
mu_zalloc (size_t size)
{
void *p = mu_alloc (size);
memset (p, 0, size);
return p;
}
void *
mu_realloc (void *p, size_t size)
{
void *newp = realloc (p, size);
if (!newp)
mu_alloc_die ();
return newp;
}
char *
mu_strdup (const char *s)
{
char *news = strdup (s);
if (!news)
mu_alloc_die ();
return news;
}
/* Copied from gnulib */
void *
mu_2nrealloc (void *p, size_t *pn, size_t s)
{
size_t n = *pn;
if (!p)
{
if (!n)
{
/* The approximate size to use for initial small allocation
requests, when the invoking code specifies an old size of
zero. 64 bytes is the largest "small" request for the
GNU C library malloc. */
enum { DEFAULT_MXFAST = 64 };
n = DEFAULT_MXFAST / s;
n += !n;
}
}
else
{
/* Set N = ceil (1.5 * N) so that progress is made if N == 1.
Check for overflow, so that N * S stays in size_t range.
The check is slightly conservative, but an exact check isn't
worth the trouble. */
if ((size_t) -1 / 3 * 2 / s <= n)
mu_alloc_die ();
n += (n + 1) / 2;
}
*pn = n;
return mu_realloc (p, n * s);
}