ab23acca7ffc5525262732dd41e74209bb208373
[apps/madmutt.git] / lib / mem.c
1 /*
2  * This file is part of mutt-ng, see http://www.muttng.org/.
3  * It's licensed under the GNU General Public License,
4  * please see the file GPL in the top level source directory.
5  */
6
7 #include <stdlib.h>
8
9 #include "mem.h"
10 #include "exit.h"
11 #include "intl.h"
12
13 void *_mem_calloc (size_t nmemb, size_t size, int line, const char* fname) {
14   void *p;
15
16   if (!nmemb || !size)
17     return NULL;
18
19   if (((size_t) - 1) / nmemb <= size) {
20     exit_fatal ("mem_calloc", _("Integer overflow -- can't allocate memory!"),
21                 line, fname, 1);
22     return (NULL);
23   }
24
25   if (!(p = calloc (nmemb, size))) {
26     exit_fatal ("mem_calloc", _("Out of memory!"), line, fname, 1);
27     return (NULL);
28   }
29   return p;
30 }
31
32 void _mem_realloc (void *ptr, size_t siz, int line, const char* fname) {
33   void *r;
34   void **p = (void **) ptr;
35
36   if (siz == 0) {
37     if (*p) {
38       free (*p);                /* __MEM_CHECKED__ */
39       *p = NULL;
40     }
41     return;
42   }
43
44   if (*p)
45     r = (void *) realloc (*p, siz);     /* __MEM_CHECKED__ */
46   else {
47     /* realloc(NULL, nbytes) doesn't seem to work under SunOS 4.1.x  --- __MEM_CHECKED__ */
48     r = (void *) malloc (siz);  /* __MEM_CHECKED__ */
49   }
50
51   if (!r)
52     exit_fatal ("mem_realloc", _("Out of memory!"), line, fname, 1);
53
54   *p = r;
55 }