]> git.deb.at Git - pkg/abook.git/blob - xmalloc.c
- replace abook_malloc, abook_realloc and my_free with new xmalloc routines
[pkg/abook.git] / xmalloc.c
1 /*
2  * $Id$
3  *
4  * Common xmalloc memory allocation routines
5  *
6  * written by Jaakko Heinonen <jheinonen@users.sourceforge.net>
7  */
8
9 /*
10  * Copyright (c) 2005 Jaakko Heinonen
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer
18  *    in this position and unchanged.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 static void
41 xmalloc_default_error_handler(int err)
42 {
43         fprintf(stderr, "Memory allocation failure: %s\n", strerror(err));
44         exit(EXIT_FAILURE);
45 }
46
47 static void (*xmalloc_handle_error)(int err) = xmalloc_default_error_handler;
48
49 void
50 xmalloc_set_error_handler(void (*func)(int err))
51 {
52         if(func)
53                 xmalloc_handle_error = func;
54         else
55                 xmalloc_handle_error = xmalloc_default_error_handler;
56 }
57
58 void *
59 xmalloc(size_t size)
60 {
61         void *p;
62
63         if((p = malloc(size)) == NULL)
64                 (*xmalloc_handle_error)(errno);
65
66         return p;
67 }
68
69 void *
70 xmalloc0(size_t size)
71 {
72         void *p;
73
74         p = xmalloc(size);
75         if(p)
76                 memset(p, 0, size);
77
78         return p;
79 }
80
81 void *
82 xrealloc(void *ptr, size_t size)
83 {
84         if((ptr = realloc(ptr, size)) == NULL)
85                 (*xmalloc_handle_error)(errno);
86
87         return ptr;
88 }
89
90 void
91 xfree(void *ptr)
92 {
93         free(ptr);
94         ptr = NULL;
95 }
96
97 static void *
98 _xmalloc_inc(size_t size, size_t inc, int zero)
99 {
100         size_t total_size = size + inc;
101
102         /*
103          * check if the calculation overflowed
104          */
105         if(total_size < size) {
106                 (*xmalloc_handle_error)(EINVAL);
107                 return NULL;
108         }
109
110         return zero ? xmalloc0(total_size) : xmalloc(total_size);
111 }
112
113 void *
114 xmalloc_inc(size_t size, size_t inc)
115 {
116         return _xmalloc_inc(size, inc, 0);
117 }
118
119 void *
120 xmalloc0_inc(size_t size, size_t inc)
121 {
122         return _xmalloc_inc(size, inc, 1);
123 }
124