From 3b5a4b736257f17756b713f4e8a3974a81f099ca Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Thu, 18 Apr 2002 17:57:20 +0000 Subject: Replaced the explicit header includes with one call to tinyproxy.h. Also, added the utils.h header and changed all the malloc, calloc, free, and strdup to the appropriate safe variety. --- src/hashmap.c | 56 ++++++++++++++++++++------------------------------------ src/vector.c | 27 ++++++++++----------------- 2 files changed, 30 insertions(+), 53 deletions(-) diff --git a/src/hashmap.c b/src/hashmap.c index 74af7a9..4b47d93 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,4 +1,4 @@ -/* $Id: hashmap.c,v 1.3 2002-04-09 20:05:15 rjkaes Exp $ +/* $Id: hashmap.c,v 1.4 2002-04-18 17:57:20 rjkaes Exp $ * * A hashmap implementation. The keys are case-insensitive NULL terminated * strings, and the data is arbitrary lumps of data. Copies of both the @@ -25,27 +25,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifdef HAVE_CONFIG_H -# include -#endif - -#include -#if defined(HAVE_STDINT_H) -# include -#endif -#if defined(HAVE_INTTYPES_H) -# include -#endif - -#if defined(HAVE_CTYPE_H) -# include -#endif -#include -#include -#include +#include "tinyproxy.h" #include "hashmap.h" #include "vector.h" +#include "utils.h" /* * These structures are the storage for the hashmap. Entries are stored in @@ -112,14 +96,14 @@ hashmap_create(unsigned int nbuckets) if (nbuckets == 0) return NULL; - ptr = calloc(1, sizeof(struct hashmap_s)); + ptr = safecalloc(1, sizeof(struct hashmap_s)); if (!ptr) return NULL; ptr->size = nbuckets; - ptr->maps = calloc(nbuckets, sizeof(struct hashentry_s *)); + ptr->maps = safecalloc(nbuckets, sizeof(struct hashentry_s *)); if (!ptr->maps) { - free(ptr); + safefree(ptr); return NULL; } @@ -146,9 +130,9 @@ delete_hashentries(struct hashentry_s* entry) while (ptr) { nextptr = ptr->next; - free(ptr->key); - free(ptr->data); - free(ptr); + safefree(ptr->key); + safefree(ptr->data); + safefree(ptr); ptr = nextptr; } @@ -175,7 +159,7 @@ hashmap_delete(hashmap_t map) delete_hashentries(map->maps[i]); } - free(map); + safefree(map); return 0; } @@ -214,14 +198,14 @@ hashmap_insert(hashmap_t map, const char *key, * First make copies of the key and data in case there is a memory * problem later. */ - key_copy = strdup(key); + key_copy = safestrdup(key); if (!key_copy) return -ENOMEM; if (data) { - data_copy = malloc(len); + data_copy = safemalloc(len); if (!data_copy) { - free(key_copy); + safefree(key_copy); return -ENOMEM; } memcpy(data_copy, data, len); @@ -236,16 +220,16 @@ hashmap_insert(hashmap_t map, const char *key, ptr = ptr->next; } - ptr = calloc(1, sizeof(struct hashentry_s)); + ptr = safecalloc(1, sizeof(struct hashentry_s)); ptr->prev = prevptr; prevptr->next = ptr; } else { - ptr = map->maps[hash] = calloc(1, sizeof(struct hashentry_s)); + ptr = map->maps[hash] = safecalloc(1, sizeof(struct hashentry_s)); } if (!ptr) { - free(key_copy); - free(data_copy); + safefree(key_copy); + safefree(data_copy); return -ENOMEM; } @@ -380,9 +364,9 @@ hashmap_remove(hashmap_t map, const char *key) ptr->prev = NULL; } - free(ptr->key); - free(ptr->data); - free(ptr); + safefree(ptr->key); + safefree(ptr->data); + safefree(ptr); return 1; } diff --git a/src/vector.c b/src/vector.c index 044b085..09844bd 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,4 +1,4 @@ -/* $Id: vector.c,v 1.2 2002-04-09 16:28:13 rjkaes Exp $ +/* $Id: vector.c,v 1.3 2002-04-18 17:57:19 rjkaes Exp $ * * A vector implementation. The vector can be of an arbritrary length, and * the data for each entry is an lump of data (the size is stored in the @@ -21,17 +21,10 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#if defined(HAVE_CONFIG_H) -# include -#endif - -#include - -#include -#include -#include +#include "tinyproxy.h" #include "vector.h" +#include "utils.h" /* * These structures are the storage for the "vector". Entries are @@ -64,7 +57,7 @@ vector_create(void) { vector_t vector; - vector = malloc(sizeof(struct vector_s)); + vector = safemalloc(sizeof(struct vector_s)); if (!vector) return NULL; @@ -91,13 +84,13 @@ vector_delete(vector_t vector) ptr = vector->vector; while (ptr) { next = ptr->next; - free(ptr->data); - free(ptr); + safefree(ptr->data); + safefree(ptr); ptr = next; } - free(vector); + safefree(vector); return 0; } @@ -119,13 +112,13 @@ vector_insert(vector_t vector, void *data, ssize_t len) if (!vector || !data || len <= 0) return -EINVAL; - entry = malloc(sizeof(struct vectorentry_s)); + entry = safemalloc(sizeof(struct vectorentry_s)); if (!entry) return -ENOMEM; - entry->data = malloc(len); + entry->data = safemalloc(len); if (!entry->data) { - free(entry); + safefree(entry); return -ENOMEM; } -- cgit v1.2.3