Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / common_allocation.c
index c31ad625347067e3337141d925a48fe4a7d604ce..95a19f00f227ecf503711c4712c4414bee439fa0 100644 (file)
@@ -1,6 +1,6 @@
 /*
      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
@@ -14,8 +14,8 @@
 
      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.
 */
 
 /**
@@ -24,7 +24,7 @@
  * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_util_lib.h"
+#include "gnunet_crypto_lib.h"
 #if HAVE_MALLOC_H
 #include <malloc.h>
 #endif
@@ -53,25 +53,32 @@ static LONG mem_used = 0;
  * memory is available.
  *
  * @param size how many bytes of memory to allocate, do NOT use
- *  this function (or GNUNET_malloc) to allocate more than several MB
+ *  this function (or GNUNET_malloc()) to allocate more than several MB
  *  of memory, if you are possibly needing a very large chunk use
- *  GNUNET_xmalloc_unchecked_ instead.
- * @param filename where in the code was the call to GNUNET_malloc
- * @param linenumber where in the code was the call to GNUNET_malloc
+ *  #GNUNET_xmalloc_unchecked_() instead.
+ * @param filename where in the code was the call to GNUNET_malloc()
+ * @param linenumber where in the code was the call to GNUNET_malloc()
  * @return pointer to size bytes of memory
  */
 void *
-GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
+GNUNET_xmalloc_ (size_t size,
+                const char *filename,
+                int linenumber)
 {
   void *ret;
 
   /* As a security precaution, we generally do not allow very large
-   * allocations using the default 'GNUNET_malloc' macro */
-  GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
-  ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
-  if (ret == NULL)
+   * allocations using the default 'GNUNET_malloc()' macro */
+  GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED,
+                   filename,
+                   linenumber);
+  ret = GNUNET_xmalloc_unchecked_ (size,
+                                  filename,
+                                  linenumber);
+  if (NULL == ret)
   {
-    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
+                 "malloc");
     GNUNET_assert (0);
   }
   return ret;
@@ -80,8 +87,8 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
 
 /**
  * Allocate and initialize memory. Checks the return value, aborts if no more
- * memory is available.  Don't use GNUNET_xmemdup_ directly. Use the
- * GNUNET_memdup macro.
+ * memory is available.  Don't use #GNUNET_xmemdup_() directly. Use the
+ * GNUNET_memdup() macro.
  *
  * @param buf buffer to initialize from (must contain size bytes)
  * @param size number of bytes to allocate
@@ -90,7 +97,9 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
  * @return allocated memory, never NULL
  */
 void *
-GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
+GNUNET_xmemdup_ (const void *buf,
+                size_t size,
+                const char *filename,
                  int linenumber)
 {
   void *ret;
@@ -120,18 +129,19 @@ GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
 }
 
 
-
 /**
- * Wrapper around malloc. Allocates size bytes of memory.
+ * Wrapper around malloc(). Allocates size bytes of memory.
  * The memory will be zero'ed out.
  *
  * @param size the number of bytes to allocate
- * @param filename where in the code was the call to GNUNET_malloc_large
- * @param linenumber where in the code was the call to GNUNET_malloc_large
+ * @param filename where in the code was the call to GNUNET_malloc_unchecked()
+ * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
  * @return pointer to size bytes of memory, NULL if we do not have enough memory
  */
 void *
-GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
+GNUNET_xmalloc_unchecked_ (size_t size,
+                          const char *filename,
+                          int linenumber)
 {
   void *result;
 
@@ -142,7 +152,7 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
 #endif
 
   result = malloc (size);
-  if (result == NULL)
+  if (NULL == result)
     return NULL;
   memset (result, 0, size);
 
@@ -162,12 +172,15 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
  *
  * @param ptr the pointer to reallocate
  * @param n how many bytes of memory to allocate
- * @param filename where in the code was the call to GNUNET_realloc
- * @param linenumber where in the code was the call to GNUNET_realloc
+ * @param filename where in the code was the call to GNUNET_realloc()
+ * @param linenumber where in the code was the call to GNUNET_realloc()
  * @return pointer to size bytes of memory
  */
 void *
-GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
+GNUNET_xrealloc_ (void *ptr,
+                 size_t n,
+                 const char *filename,
+                 int linenumber)
 {
 #ifdef W32_MEM_LIMIT
   n += sizeof (size_t);
@@ -211,13 +224,17 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
  * 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, const char *filename, int linenumber)
+GNUNET_xfree_ (void *ptr,
+              const char *filename,
+              int linenumber)
 {
-  GNUNET_assert_at (ptr != NULL, filename, linenumber);
+  GNUNET_assert_at (NULL != ptr,
+                   filename,
+                   linenumber);
 #ifdef W32_MEM_LIMIT
   ptr = &((size_t *) ptr)[-1];
   mem_used -= *((size_t *) ptr);
@@ -239,22 +256,33 @@ GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
   free (ptr);
 }
 
+
 /**
  * Dup a string (same semantics as strdup).
  *
  * @param str the string to dup
- * @param filename where in the code was the call to GNUNET_strdup
- * @param linenumber where in the code was the call to GNUNET_strdup
- * @return strdup(str)
+ * @param filename where in the code was the call to GNUNET_strdup()
+ * @param linenumber where in the code was the call to GNUNET_strdup()
+ * @return `strdup(@a str)`
  */
 char *
-GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
+GNUNET_xstrdup_ (const char *str,
+                const char *filename,
+                int linenumber)
 {
   char *res;
-
-  GNUNET_assert_at (str != NULL, filename, linenumber);
-  res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
-  memcpy (res, str, strlen (str) + 1);
+  size_t slen;
+
+  GNUNET_assert_at (str != NULL,
+                   filename,
+                   linenumber);
+  slen = strlen (str) + 1;
+  res = GNUNET_xmalloc_ (slen,
+                        filename,
+                        linenumber);
+  memcpy (res,
+         str,
+         slen);
   return res;
 }
 
@@ -279,21 +307,28 @@ strnlen (const char *s,
  *
  * @param str the string to dup
  * @param len the length of the string to dup
- * @param filename where in the code was the call to GNUNET_strndup
- * @param linenumber where in the code was the call to GNUNET_strndup
- * @return strndup(str,len)
+ * @param filename where in the code was the call to GNUNET_strndup()
+ * @param linenumber where in the code was the call to GNUNET_strndup()
+ * @return `strndup(@a str,@a len)`
  */
 char *
-GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
+GNUNET_xstrndup_ (const char *str,
+                 size_t len,
+                 const char *filename,
                   int linenumber)
 {
   char *res;
 
   if (0 == len)
     return GNUNET_strdup ("");
-  GNUNET_assert_at (str != NULL, filename, linenumber);
-  len = strnlen (str, len);
-  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
+  GNUNET_assert_at (NULL != str,
+                   filename,
+                   linenumber);
+  len = strnlen (str,
+                len);
+  res = GNUNET_xmalloc_ (len + 1,
+                        filename,
+                        linenumber);
   memcpy (res, str, len);
   /* res[len] = '\0'; 'malloc' zeros out anyway */
   return res;
@@ -309,12 +344,16 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
  * @param elementSize the size of the elements of the array
  * @param oldCount address of the number of elements in the *old array
  * @param newCount number of elements in the new array, may be 0
- * @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_array_grow()
+ * @param linenumber where in the code was the call to GNUNET_array_grow()
  */
 void
-GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
-               unsigned int newCount, const char *filename, int linenumber)
+GNUNET_xgrow_ (void **old,
+              size_t elementSize,
+              unsigned int *oldCount,
+               unsigned int newCount,
+              const char *filename,
+              int linenumber)
 {
   void *tmp;
   size_t size;
@@ -331,7 +370,8 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
     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)
+      memcpy (tmp, *old, elementSize * (*oldCount));
   }
 
   if (*old != NULL)
@@ -344,15 +384,17 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
 
 
 /**
- * Like asprintf, just portable.
+ * Like asprintf(), just portable.
  *
  * @param buf set to a buffer of sufficient size (allocated, caller must free)
- * @param format format string (see printf, fprintf, etc.)
+ * @param format format string (see printf(), fprintf(), etc.)
  * @param ... data for format string
- * @return number of bytes in "*buf" excluding 0-termination
+ * @return number of bytes in `*@a buf`, excluding 0-termination
  */
 int
-GNUNET_asprintf (char **buf, const char *format, ...)
+GNUNET_asprintf (char **buf,
+                const char *format,
+                ...)
 {
   int ret;
   va_list args;
@@ -369,7 +411,7 @@ GNUNET_asprintf (char **buf, const char *format, ...)
 
 
 /**
- * Like snprintf, just aborts if the buffer is of insufficient size.
+ * Like snprintf(), just aborts if the buffer is of insufficient size.
  *
  * @param buf pointer to buffer that is written to
  * @param size number of bytes in buf
@@ -378,7 +420,9 @@ GNUNET_asprintf (char **buf, const char *format, ...)
  * @return number of bytes written to buf or negative value on error
  */
 int
-GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
+GNUNET_snprintf (char *buf,
+                size_t size,
+                const char *format, ...)
 {
   int ret;
   va_list args;