/*
This file is part of GNUnet.
- Copyright (C) 2001, 2002, 2003, 2005, 2006 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2001, 2002, 2003, 2005, 2006 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
* @author Christian Grothoff
*/
#include "platform.h"
-#include "gnunet_util_lib.h"
+#include "gnunet_crypto_lib.h"
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
}
+/**
+ * Allocate memory for a two dimensional array in one block
+ * and set up pointers. Aborts if no more memory is available.
+ * Don't use GNUNET_xnew_array_2d_ directly. Use the
+ * #GNUNET_new_array_2d macro.
+ * The memory of the elements will be zero'ed out.
+ *
+ * @param n size of the first dimension
+ * @param m size of the second dimension
+ * @param elementSize size of a single element in bytes
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return allocated memory, never NULL
+ */
+void **
+GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
+ const char *filename, int linenumber)
+{
+ /* use char pointer internally to avoid void pointer arithmetic warnings */
+ char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */
+ n * m * elementSize, /* element data */
+ filename, linenumber);
+
+ for (size_t i = 0; i < n; i++)
+ ret[i] = (char *)ret + /* base address */
+ n * sizeof (void *) + /* skip 1. dim header */
+ i * m * elementSize; /* skip to 2. dim row header */
+ return (void **)ret;
+}
+
+
+/**
+ * Allocate memory for a three dimensional array in one block
+ * and set up pointers. Aborts if no more memory is available.
+ * Don't use GNUNET_xnew_array_3d_ directly. Use the
+ * #GNUNET_new_array_3d macro.
+ * The memory of the elements will be zero'ed out.
+ *
+ * @param n size of the first dimension
+ * @param m size of the second dimension
+ * @param o size of the third dimension
+ * @param elementSize size of a single element in bytes
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return allocated memory, never NULL
+ */
+void ***
+GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
+ const char *filename, int linenumber)
+{
+ /* use char pointer internally to avoid void pointer arithmetic warnings */
+ char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */
+ n * m * sizeof (void *) + /* 2. dim header */
+ n * m * o * elementSize, /* element data */
+ filename, linenumber);
+
+ for (size_t i = 0; i < n; i++)
+ {
+ /* need to cast to (char *) temporarily for byte level acuracy */
+ ret[i] = (char **)((char *)ret + /* base address */
+ n * sizeof (void **) + /* skip 1. dim header */
+ i * m * sizeof (void *)); /* skip to 2. dim header */
+ for (size_t j = 0; j < m; j++)
+ ret[i][j] = (char *)ret + /* base address */
+ n * sizeof (void **) + /* skip 1. dim header */
+ n * m * sizeof (void *) + /* skip 2. dim header */
+ i * m * o * elementSize + /* skip to 2. dim part */
+ j * o * elementSize; /* skip to 3. dim row data */
+ }
+ return (void ***)ret;
+}
+
+
/**
* Allocate and initialize memory. Checks the return value, aborts if no more
* memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
ret = &((size_t *) ret)[1];
mem_used += size;
#endif
- memcpy (ret, buf, size);
+ GNUNET_memcpy (ret, buf, size);
return ret;
}
/**
* Reallocate memory. Checks the return value, aborts if no more
* memory is available.
+ * The content of the intersection of the new and old size will be unchanged.
*
* @param ptr the pointer to reallocate
* @param n how many bytes of memory to allocate
*/
void *
GNUNET_xrealloc_ (void *ptr,
- size_t n,
- const char *filename,
- int linenumber)
+ size_t n,
+ const char *filename,
+ int linenumber)
{
#ifdef W32_MEM_LIMIT
n += sizeof (size_t);
* want to keep track of allocations.
*
* @param ptr the pointer to free
- * @param filename where in the code was the call to GNUNET_array_grow
- * @param linenumber where in the code was the call to GNUNET_array_grow
+ * @param filename where in the code was the call to GNUNET_free
+ * @param linenumber where in the code was the call to GNUNET_free
*/
void
GNUNET_xfree_ (void *ptr,
for (i=0;i<s/8;i++)
base[i] = baadfood;
- memcpy (&base[s/8], &baadfood, s % 8);
+ GNUNET_memcpy (&base[s/8], &baadfood, s % 8);
}
#endif
#endif
res = GNUNET_xmalloc_ (slen,
filename,
linenumber);
- memcpy (res,
+ GNUNET_memcpy (res,
str,
slen);
return res;
res = GNUNET_xmalloc_ (len + 1,
filename,
linenumber);
- memcpy (res, str, len);
+ GNUNET_memcpy (res, str, len);
/* res[len] = '\0'; 'malloc' zeros out anyway */
return res;
}
GNUNET_xgrow_ (void **old,
size_t elementSize,
unsigned int *oldCount,
- unsigned int newCount,
+ unsigned int newCount,
const char *filename,
int linenumber)
{
GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
size = newCount * elementSize;
- if (size == 0)
+ if (0 == size)
{
tmp = NULL;
}
else
{
tmp = GNUNET_xmalloc_ (size, filename, linenumber);
- memset (tmp, 0, size); /* client code should not rely on this, though... */
- if (*oldCount > newCount)
- *oldCount = newCount; /* shrink is also allowed! */
- memcpy (tmp, *old, elementSize * (*oldCount));
+ if (NULL != *old)
+ {
+ GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
+ }
}
- if (*old != NULL)
+ if (NULL != *old)
{
GNUNET_xfree_ (*old, filename, linenumber);
}
msize = ntohs (msg->size);
GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
ret = GNUNET_malloc (msize);
- memcpy (ret, msg, msize);
+ GNUNET_memcpy (ret, msg, msize);
return ret;
}