#include "array.h"
-static char UNUSED id[] = "$Id: array.c,v 1.1 2005/07/07 18:38:14 fw Exp $";
+static char UNUSED id[] = "$Id: array.c,v 1.5 2005/07/28 16:12:50 fw Exp $";
#include <assert.h>
#define ALIGN_4096U(x) ((x | 0xfffU) +1)
-static int
+static bool
safemult_uint(unsigned int a, unsigned int b, unsigned int *res)
{
unsigned int tmp;
if (!a || !b) {
*res = 0;
- return 1;
+ return true;
}
tmp = a * b;
if (tmp / b != a)
- return 0;
+ return false;
*res = tmp;
- return 1;
+ return true;
}
+void
+array_init(array *a)
+{
+ assert(a);
+ a->mem = NULL;
+ a->allocated = 0;
+ a->used = 0;
+}
+
+
/* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
void *
array_alloc(array * a, unsigned int size, unsigned int pos)
unsigned int aligned = 0;
char *tmp;
- assert(size);
+ assert(size > 0);
if (pos_plus1 < pos)
return NULL;
}
}
#ifdef DEBUG
- Log(LOG_DEBUG, "Rounded %u to %u byte.", alloc, aligned);
+ Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned);
#endif
assert(aligned >= alloc);
alloc = aligned;
#ifdef DEBUG
- Log(LOG_DEBUG, "array_alloc: changing size from %u to %u byte",
+ Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
a->allocated, aligned);
#endif
unsigned int
array_length(const array * const a, unsigned int membersize)
{
- assert(a);
- assert(membersize);
+ assert(a != NULL);
+ assert(membersize > 0);
if (array_UNUSABLE(a))
return 0;
bool
array_copyb(array * dest, const char *src, unsigned int len)
{
- assert(dest);
- assert(src);
+ assert(dest != NULL);
+ assert(src != NULL );
if (!len || !src)
return true;
memcpy(dest->mem, src, len);
#ifdef DEBUG
Log(LOG_DEBUG,
- "array_copyb: copied %u bytes to array (%u total bytes allocated)",
+ "array_copyb(): copied %u bytes to array (%u bytes allocated).",
len, dest->allocated);
#endif
return true;
unsigned int used;
char *ptr;
- assert(dest);
- assert(src);
+ assert(dest != NULL);
+ assert(src != NULL);
if (!len)
return true;
ptr = dest->mem;
- assert(ptr);
+ assert(ptr != NULL);
#ifdef DEBUG
Log(LOG_DEBUG,
- "array_catb: appending %u bytes to array (now %u total bytes in array)",
+ "array_catb(): appending %u bytes to array (now %u bytes in array).",
len, tmp);
#endif
memcpy(ptr + used, src, len);
}
+/* append trailing NUL byte to array, but do not count it. */
+bool
+array_cat0_temporary(array * a)
+{
+ unsigned int len = array_bytes(a);
+ if (!array_catb(a, "", 1))
+ return false;
+
+ array_truncate(a, 1, len);
+ return true;
+}
+
/* add contents of array src to array dest. */
bool
array_cat(array * dest, const array * const src)
{
unsigned int totalsize;
- assert(membersize);
- assert(a);
+ assert(membersize > 0);
+ assert(a != NULL);
if (array_UNUSABLE(a))
return NULL;
void
array_free(array * a)
{
- assert(a);
+ assert(a != NULL);
#ifdef DEBUG
Log(LOG_DEBUG,
- "array_free: %u bytes free'd (%u bytes still used at time of free())",
+ "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
a->allocated, a->used);
#endif
free(a->mem);
void *
array_start(const array * const a)
{
- assert(a);
+ assert(a != NULL);
return a->mem;
}
void
array_trunc(array * a)
{
- assert(a);
+ assert(a != NULL);
a->used = 0;
}
array_truncate(array * a, unsigned int membersize, unsigned int len)
{
unsigned int newlen;
- assert(a);
+ assert(a != NULL);
if (!safemult_uint(membersize, len, &newlen))
return;
{
unsigned int bytepos;
- assert(a);
- assert(membersize);
+ assert(a != NULL);
+ assert(membersize > 0);
if (!pos)
return;
#ifdef DEBUG
Log(LOG_DEBUG,
- "array_moveleft: %u used bytes in array, move to beginning, starting at pos %u",
+ "array_moveleft(): %u bytes used in array, starting at position %u.",
a->used, bytepos);
#endif
if (a->used <= bytepos) {