Lumiera  0.pre.03
»edit your freedom«
safeclib.c
Go to the documentation of this file.
1 /*
2  safe_clib.c - Portable and safe wrapers around some clib functions and some tools
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "lib/error.h"
30 #include "lib/safeclib.h"
31 
32 #include <string.h>
33 #include <stdlib.h>
34 #include <pthread.h>
35 #include <stdint.h>
36 #include <nobug.h>
37 
38 
39 LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
40 
41 
47  {
50 // /** OS filehandles **/
51 // LUMIERA_RESOURCE_FILEHANDLE,
52 // /** CPU time, as in threads and such **/
53 // LUMIERA_RESOURCE_CPU,
54 // /** mmaped regions **/
55 // LUMIERA_RESOURCE_MMAP,
56 // /** disk space for the storage area, context is a pointer to the filename indication the device **/
57 // LUMIERA_RESOURCE_DISKSTORAGE,
58 // /** disk bandwidth for the storage area, context is a pointer to the filename indication the device **/
59 // LUMIERA_RESOURCE_STORAGEBANDWIDTH,
60 // /** disk space for the caching area, context is a pointer to the filename indication the device **/
61 // LUMIERA_RESOURCE_DISKCACHE,
62 // /** disk bandwidth for the caching area, context is a pointer to the filename indication the device **/
63 // LUMIERA_RESOURCE_CACHEBANDWIDTH,
64 
65  LUMIERA_RESOURCE_END /* last entry */
66  };
67 
68 
84  {
99  };
100 
101 
112 static int
113 die_no_mem (enum lumiera_resource which, enum lumiera_resource_try* iteration, void* context)
114 {
115  (void) which; (void) iteration; (void) context;
116  LUMIERA_DIE (NO_MEMORY);
117  return 0; /* not reached */
118 }
119 
120 
121 void*
122 lumiera_malloc (size_t size)
123 {
125  void* o = NULL;
126 
127  if (size)
128  {
129  o = malloc (size);
130  if (!o)
131  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
132  }
133 
134  return o;
135 }
136 
137 
138 void*
139 lumiera_calloc (size_t n, size_t size)
140 {
142  void* o = NULL;
143 
144  size_t gross = n*size;
145 
146  if (n&&size)
147  {
148  o = calloc (n, size);
149  if (!o)
150  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &gross);
151  }
152 
153  return o;
154 }
155 
156 
157 void*
158 lumiera_realloc (void* ptr, size_t size)
159 {
161  void* o = NULL;
162 
163  if (size)
164  {
165  o = realloc (ptr, size);
166  if (!o)
167  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
168  }
169 
170  return o;
171 }
172 
173 
174 char*
175 lumiera_strndup (const char* str, size_t len)
176 {
178  void* o = NULL;
179 
180  if (str && len)
181  o = strndup (str, len);
182  else
183  o = strdup ("");
184 
185  if (!o)
186  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &len);
187 
188  return o;
189 }
190 
191 
192 int
193 lumiera_strncmp (const char* a, const char* b, size_t len)
194 {
195  return a == b ? 0 : strncmp (a?a:"", b?b:"", len);
196 }
197 
198 
199 int
200 lumiera_streq (const char* a, const char* b)
201 {
202  return !lumiera_strncmp (a, b, SIZE_MAX);
203 }
204 
205 
206 
207 
208 /*
209 // Local Variables:
210 // mode: C
211 // c-file-style: "gnu"
212 // indent-tabs-mode: nil
213 // End:
214 */
Portable and safe wrappers around some C-Lib functions.
void * lumiera_calloc(size_t n, size_t size)
Allocate cleared memory for an array.
Definition: safeclib.c:139
lumiera_resource_try
Iteration indicator.
Definition: safeclib.c:83
No op, returned by a handler when it did nothing.
Definition: safeclib.c:86
static int die_no_mem(enum lumiera_resource which, enum lumiera_resource_try *iteration, void *context)
Definition: safeclib.c:113
Lumiera error handling (C interface).
try to free a bigger implementation defined amount of resources
Definition: safeclib.c:92
int lumiera_strncmp(const char *a, const char *b, size_t len)
Compare two C strings.
Definition: safeclib.c:193
try to free one or really few of this resources
Definition: safeclib.c:88
int lumiera_streq(const char *a, const char *b)
check 2 strings for identity.
Definition: safeclib.c:200
void * lumiera_realloc(void *ptr, size_t size)
Change the size of a memory block.
Definition: safeclib.c:158
#define LUMIERA_DIE(err)
Abort unconditionally with a &#39;Fatal Error!&#39; message.
Definition: error.h:63
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:122
try to free a small reasonable implementation defined amount of resources
Definition: safeclib.c:90
When a handler gets unregistered it will be called with this value to give it a chance to clean up th...
Definition: safeclib.c:98
OS filehandles.
Definition: safeclib.c:65
free as much as possible
Definition: safeclib.c:94
char * lumiera_strndup(const char *str, size_t len)
Duplicate a C string.
Definition: safeclib.c:175
lumiera_resource
Resources known to the resource collector.
Definition: safeclib.c:46
memory blocks, context is a pointer to the size_t required
Definition: safeclib.c:49
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:80