safeclib.c
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 #include "lib/error.h" 22 #include "lib/safeclib.h" 23 24 #include "backend/resourcecollector.h" 25 26 #include <string.h> 27 #include <stdlib.h> 28 #include <pthread.h> 29 #include <stdint.h> 30 #include <nobug.h> 31 32 33 ERROR_DEFINE (NO_MEMORY, "Out of Memory!"); 34 35 36 37 /* placeholder function until the resourcecollector gets hooked in */ 38 static int 39 die_no_mem (enum resource which, enum resource_try* iteration, void* context) 40 { 41 (void) which; (void) iteration; (void) context; 42 DIE (NO_MEMORY); 43 return 0; /* not reached */ 44 } 45 46 static resourcecollector_run_fn safeclib_resourcecollector_run_hook = die_no_mem; 47 48 void 49 safeclib_set_resourcecollector (void* hook) 50 { 51 if (hook) 52 safeclib_resourcecollector_run_hook = (resourcecollector_run_fn)hook; 53 else 54 safeclib_resourcecollector_run_hook = die_no_mem; 55 } 56 57 58 void* 59 malloc (size_t size) 60 { 61 enum resource_try iteration = RESOURCE_ONE; 62 void* o = NULL; 63 64 if (size) 65 do 66 o = malloc (size); 67 while (!o && safeclib_resourcecollector_run_hook (RESOURCE_MEMORY, &iteration, &size)); 68 69 return o; 70 } 71 72 73 void* 74 calloc (size_t n, size_t size) 75 { 76 enum resource_try iteration = RESOURCE_ONE; 77 void* o = NULL; 78 79 size_t gross = n*size; 80 81 if (n&&size) 82 do 83 o = calloc (n, size); 84 while (!o && safeclib_resourcecollector_run_hook (RESOURCE_MEMORY, &iteration, &gross)); 85 86 return o; 87 } 88 89 90 void* 91 realloc (void* ptr, size_t size) 92 { 93 enum resource_try iteration = RESOURCE_ONE; 94 void* o = NULL; 95 96 if (size) 97 do 98 o = realloc (ptr, size); 99 while (!o && safeclib_resourcecollector_run_hook (RESOURCE_MEMORY, &iteration, &size)); 100 101 return o; 102 } 103 104 105 char* 106 strndup (const char* str, size_t len) 107 { 108 enum resource_try iteration = RESOURCE_ONE; 109 void* o = NULL; 110 111 do 112 if (str && len) 113 o = strndup (str, len); 114 else 115 o = strdup (""); 116 while (!o && safeclib_resourcecollector_run_hook (RESOURCE_MEMORY, &iteration, &len)); 117 118 return o; 119 } 120 121 122 int 123 strncmp (const char* a, const char* b, size_t len) 124 { 125 return a == b ? 0 : strncmp (a?a:"", b?b:"", len); 126 } 127 128 129 int 130 streq (const char* a, const char* b) 131 { 132 return !strncmp (a, b, SIZE_MAX); 133 } 134 135 136 137 138 /* 139 // Local Variables: 140 // mode: C 141 // c-file-style: "gnu" 142 // indent-tabs-mode: nil 143 // End: 144 */