forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
139 lines (127 loc) · 3.11 KB
/
memory.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file
* Memory management wrappers
*
* @authors
* Copyright (C) 2017 Richard Russon <[email protected]>
*
* @copyright
* 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 2 of the License, 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, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_memory Memory management wrappers
*
* "Safe" memory management routines.
*
* @note If any of the allocators fail, the user is notified and the program is
* stopped immediately.
*/
#include "config.h"
#include <stdlib.h>
#include "memory.h"
#include "exit.h"
#include "logging2.h"
#include "message.h"
/**
* mutt_mem_calloc - Allocate zeroed memory on the heap
* @param nmemb Number of blocks
* @param size Size of blocks
* @retval ptr Memory on the heap
*
* @note On error, this function will never return NULL.
* It will print an error and exit the program.
*
* The caller should call mutt_mem_free() to release the memory
*/
void *mutt_mem_calloc(size_t nmemb, size_t size)
{
if ((nmemb == 0) || (size == 0))
return NULL;
void *p = calloc(nmemb, size);
if (!p)
{
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}
return p;
}
/**
* mutt_mem_free - Release memory allocated on the heap
* @param ptr Memory to release
*/
void mutt_mem_free(void *ptr)
{
if (!ptr)
return;
void **p = (void **) ptr;
if (*p)
{
free(*p);
*p = NULL;
}
}
/**
* mutt_mem_malloc - Allocate memory on the heap
* @param size Size of block to allocate
* @retval ptr Memory on the heap
*
* @note On error, this function will never return NULL.
* It will print an error and exit the program.
*
* The caller should call mutt_mem_free() to release the memory
*/
void *mutt_mem_malloc(size_t size)
{
if (size == 0)
return NULL;
void *p = malloc(size);
if (!p)
{
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}
return p;
}
/**
* mutt_mem_realloc - Resize a block of memory on the heap
* @param ptr Memory block to resize
* @param size New size
*
* @note On error, this function will never return NULL.
* It will print an error and exit the program.
*
* If the new size is zero, the block will be freed.
*/
void mutt_mem_realloc(void *ptr, size_t size)
{
if (!ptr)
return;
void **p = (void **) ptr;
if (size == 0)
{
if (*p)
{
free(*p);
*p = NULL;
}
return;
}
void *r = realloc(*p, size);
if (!r)
{
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}
*p = r;
}