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)
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
20 #include "lib/error.h"
21 #include "lib/safeclib.h"
22 
23 #include <string.h>
24 #include <stdlib.h>
25 #include <pthread.h>
26 #include <stdint.h>
27 #include <nobug.h>
28 
29 
30 LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
31 
32 
38  {
41 // /** OS filehandles **/
42 // LUMIERA_RESOURCE_FILEHANDLE,
43 // /** CPU time, as in threads and such **/
44 // LUMIERA_RESOURCE_CPU,
45 // /** mmaped regions **/
46 // LUMIERA_RESOURCE_MMAP,
47 // /** disk space for the storage area, context is a pointer to the filename indication the device **/
48 // LUMIERA_RESOURCE_DISKSTORAGE,
49 // /** disk bandwidth for the storage area, context is a pointer to the filename indication the device **/
50 // LUMIERA_RESOURCE_STORAGEBANDWIDTH,
51 // /** disk space for the caching area, context is a pointer to the filename indication the device **/
52 // LUMIERA_RESOURCE_DISKCACHE,
53 // /** disk bandwidth for the caching area, context is a pointer to the filename indication the device **/
54 // LUMIERA_RESOURCE_CACHEBANDWIDTH,
55 
56  LUMIERA_RESOURCE_END /* last entry */
57  };
58 
59 
75  {
90  };
91 
92 
103 static int
104 die_no_mem (enum lumiera_resource which, enum lumiera_resource_try* iteration, void* context)
105 {
106  (void) which; (void) iteration; (void) context;
107  LUMIERA_DIE (NO_MEMORY);
108  return 0; /* not reached */
109 }
110 
111 
112 void*
113 lumiera_malloc (size_t size)
114 {
116  void* o = NULL;
117 
118  if (size)
119  {
120  o = malloc (size);
121  if (!o)
122  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
123  }
124 
125  return o;
126 }
127 
128 
129 void*
130 lumiera_calloc (size_t n, size_t size)
131 {
133  void* o = NULL;
134 
135  size_t gross = n*size;
136 
137  if (n&&size)
138  {
139  o = calloc (n, size);
140  if (!o)
141  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &gross);
142  }
143 
144  return o;
145 }
146 
147 
148 void*
149 lumiera_realloc (void* ptr, size_t size)
150 {
152  void* o = NULL;
153 
154  if (size)
155  {
156  o = realloc (ptr, size);
157  if (!o)
158  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
159  }
160 
161  return o;
162 }
163 
164 
165 char*
166 lumiera_strndup (const char* str, size_t len)
167 {
169  void* o = NULL;
170 
171  if (str && len)
172  o = strndup (str, len);
173  else
174  o = strdup ("");
175 
176  if (!o)
177  die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &len);
178 
179  return o;
180 }
181 
182 
183 int
184 lumiera_strncmp (const char* a, const char* b, size_t len)
185 {
186  return a == b ? 0 : strncmp (a?a:"", b?b:"", len);
187 }
188 
189 
190 int
191 lumiera_streq (const char* a, const char* b)
192 {
193  return !lumiera_strncmp (a, b, SIZE_MAX);
194 }
195 
196 
197 
198 
199 /*
200 // Local Variables:
201 // mode: C
202 // c-file-style: "gnu"
203 // indent-tabs-mode: nil
204 // End:
205 */
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:130
lumiera_resource_try
Iteration indicator.
Definition: safeclib.c:74
No op, returned by a handler when it did nothing.
Definition: safeclib.c:77
static int die_no_mem(enum lumiera_resource which, enum lumiera_resource_try *iteration, void *context)
Definition: safeclib.c:104
Lumiera error handling (C interface).
try to free a bigger implementation defined amount of resources
Definition: safeclib.c:83
int lumiera_strncmp(const char *a, const char *b, size_t len)
Compare two C strings.
Definition: safeclib.c:184
try to free one or really few of this resources
Definition: safeclib.c:79
int lumiera_streq(const char *a, const char *b)
check 2 strings for identity.
Definition: safeclib.c:191
void * lumiera_realloc(void *ptr, size_t size)
Change the size of a memory block.
Definition: safeclib.c:149
#define LUMIERA_DIE(err)
Abort unconditionally with a &#39;Fatal Error!&#39; message.
Definition: error.h:54
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:113
try to free a small reasonable implementation defined amount of resources
Definition: safeclib.c:81
When a handler gets unregistered it will be called with this value to give it a chance to clean up th...
Definition: safeclib.c:89
OS filehandles.
Definition: safeclib.c:56
free as much as possible
Definition: safeclib.c:85
char * lumiera_strndup(const char *str, size_t len)
Duplicate a C string.
Definition: safeclib.c:166
lumiera_resource
Resources known to the resource collector.
Definition: safeclib.c:37
memory blocks, context is a pointer to the size_t required
Definition: safeclib.c:40
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:71