Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
30LUMIERA_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
91
92
103static int
104die_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
112void*
113lumiera_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
129void*
130lumiera_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
148void*
149lumiera_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
165char*
166lumiera_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
183int
184lumiera_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
190int
191lumiera_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*/
Lumiera error handling (C interface).
#define LUMIERA_DIE(err)
Abort unconditionally with a 'Fatal Error!' message.
Definition error.h:54
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition error.h:71
return NULL
Definition llist.h:586
void * lumiera_calloc(size_t n, size_t size)
Allocate cleared memory for an array.
Definition safeclib.c:130
int lumiera_streq(const char *a, const char *b)
check 2 strings for identity.
Definition safeclib.c:191
int lumiera_strncmp(const char *a, const char *b, size_t len)
Compare two C strings.
Definition safeclib.c:184
char * lumiera_strndup(const char *str, size_t len)
Duplicate a C string.
Definition safeclib.c:166
void * lumiera_realloc(void *ptr, size_t size)
Change the size of a memory block.
Definition safeclib.c:149
lumiera_resource_try
Iteration indicator.
Definition safeclib.c:75
@ LUMIERA_RESOURCE_SOME
try to free a small reasonable implementation defined amount of resources
Definition safeclib.c:81
@ LUMIERA_RESOURCE_ONE
try to free one or really few of this resources
Definition safeclib.c:79
@ LUMIERA_RESOURCE_MANY
try to free a bigger implementation defined amount of resources
Definition safeclib.c:83
@ LUMIERA_RESOURCE_NONE
No op, returned by a handler when it did nothing.
Definition safeclib.c:77
@ LUMIERA_RESOURCE_ALL
free as much as possible
Definition safeclib.c:85
@ LUMIERA_RESOURCE_UNREGISTER
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
@ LUMIERA_RESOURCE_PANIC
die!
Definition safeclib.c:87
static int die_no_mem(enum lumiera_resource which, enum lumiera_resource_try *iteration, void *context)
Definition safeclib.c:104
lumiera_resource
Resources known to the resource collector.
Definition safeclib.c:38
@ LUMIERA_RESOURCE_MEMORY
memory blocks, context is a pointer to the size_t required
Definition safeclib.c:40
@ LUMIERA_RESOURCE_END
Definition safeclib.c:56
void * lumiera_malloc(size_t size)
Allocate memory.
Definition safeclib.c:113
Portable and safe wrappers around some C-Lib functions.