X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Farray.c;h=1e56b719cd4fabc74de4326959790d68805383ca;hp=bc405351e0d5fd825cba4e89f6412b06131a13ba;hb=3243d9ee441e9cd4338965bac7c2ed3b49a3c2dd;hpb=4a2eea2939bc9131f240491355aa40976ca7a38d diff --git a/src/ngircd/array.c b/src/ngircd/array.c index bc405351..1e56b719 100644 --- a/src/ngircd/array.c +++ b/src/ngircd/array.c @@ -12,7 +12,7 @@ #include "array.h" -static char UNUSED id[] = "$Id: array.c,v 1.9 2006/05/07 10:52:47 fw Exp $"; +static char UNUSED id[] = "$Id: array.c,v 1.15 2007/11/18 15:05:35 alex Exp $"; #include @@ -21,19 +21,22 @@ static char UNUSED id[] = "$Id: array.c,v 1.9 2006/05/07 10:52:47 fw Exp $"; #include "log.h" +/* Enable more Debug messages in alloc / append / memmove code. */ +/* #define DEBUG_ARRAY */ + + + #define array_UNUSABLE(x) ( !(x)->mem || (0 == (x)->allocated) ) -#define ALIGN_32U(x) (((x) | 0x1fU) +1) -#define ALIGN_1024U(x) (((x) | 0x3ffU) +1) -#define ALIGN_4096U(x) (((x) | 0xfffU) +1) +#define ALIGN_32U(x) (((x)+(unsigned)31 ) & ~((unsigned)31)) +#define ALIGN_1024U(x) (((x)+(unsigned)1023) & ~((unsigned)1023)) +#define ALIGN_4096U(x) (((x)+(unsigned)4095) & ~((unsigned)4095)) static bool safemult_sizet(size_t a, size_t b, size_t *res) { - size_t tmp; - - tmp = a * b; + size_t tmp = a * b; if (b && (tmp / b != a)) return false; @@ -51,7 +54,7 @@ array_init(array *a) a->allocated = 0; a->used = 0; } - + /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */ void * @@ -63,10 +66,7 @@ array_alloc(array * a, size_t size, size_t pos) assert(size > 0); - if (pos_plus1 < pos) - return NULL; - - if (!safemult_sizet(size, pos_plus1, &alloc)) + if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc)) return NULL; if (a->allocated < alloc) { @@ -79,7 +79,7 @@ array_alloc(array * a, size_t size, size_t pos) aligned = ALIGN_4096U(alloc); } } -#ifdef DEBUG +#ifdef DEBUG_ARRAY Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned); #endif @@ -89,7 +89,7 @@ array_alloc(array * a, size_t size, size_t pos) return NULL; alloc = aligned; -#ifdef DEBUG +#ifdef DEBUG_ARRAY Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.", a->allocated, aligned); #endif @@ -190,7 +190,7 @@ array_catb(array * dest, const char *src, size_t len) assert(ptr != NULL); -#ifdef DEBUG +#ifdef DEBUG_ARRAY Log(LOG_DEBUG, "array_catb(): appending %u bytes to array (now %u bytes in array).", len, tmp); @@ -247,20 +247,22 @@ void * array_get(array * a, size_t membersize, size_t pos) { size_t totalsize; + size_t posplus1 = pos + 1; assert(membersize > 0); assert(a != NULL); - if (array_UNUSABLE(a)) + if (!posplus1 || array_UNUSABLE(a)) return NULL; - if (!safemult_sizet(pos, membersize, &totalsize)) + if (!safemult_sizet(posplus1, membersize, &totalsize)) return NULL; if (a->allocated < totalsize) return NULL; - return a->mem + pos * membersize; + totalsize = pos * membersize; + return a->mem + totalsize; } @@ -268,7 +270,7 @@ void array_free(array * a) { assert(a != NULL); -#ifdef DEBUG +#ifdef DEBUG_ARRAY Log(LOG_DEBUG, "array_free(): %u bytes free'd (%u bytes still used at time of free()).", a->allocated, a->used); @@ -279,17 +281,15 @@ array_free(array * a) a->used = 0; } - void -array_free_wipe(array * a) +array_free_wipe(array *a) { - if (!array_UNUSABLE(a)) - memset(a->mem, 0, a->allocated); - + size_t bytes = a->allocated; + if (bytes) + memset(a->mem, 0, bytes); array_free(a); } - void * array_start(const array * const a) { @@ -328,9 +328,6 @@ array_moveleft(array * a, size_t membersize, size_t pos) assert(a != NULL); assert(membersize > 0); - if (!pos) - return; - if (!safemult_sizet(membersize, pos, &bytepos)) { a->used = 0; return; @@ -339,7 +336,7 @@ array_moveleft(array * a, size_t membersize, size_t pos) if (!bytepos) return; /* nothing to do */ -#ifdef DEBUG +#ifdef DEBUG_ARRAY Log(LOG_DEBUG, "array_moveleft(): %u bytes used in array, starting at position %u.", a->used, bytepos);