tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / common_allocation.c
index 91d0a600a941a3eceebf7ecb20dc7fa3f20ed9fb..20333ce560e61ece8b85881b924eb4fb25e38d84 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      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
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     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., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -32,9 +32,9 @@
 #include <malloc/malloc.h>
 #endif
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-common-allocation",__VA_ARGS__)
 
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
 
 #ifndef INT_MAX
 #define INT_MAX 0x7FFFFFFF
@@ -85,6 +85,82 @@ GNUNET_xmalloc_ (size_t size,
 }
 
 
+/**
+ * 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
@@ -145,6 +221,8 @@ GNUNET_xmalloc_unchecked_ (size_t size,
 {
   void *result;
 
+  (void) filename;
+  (void) linenumber;
 #ifdef W32_MEM_LIMIT
   size += sizeof (size_t);
   if (mem_used + size > W32_MEM_LIMIT)
@@ -169,6 +247,7 @@ GNUNET_xmalloc_unchecked_ (size_t size,
 /**
  * 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
@@ -178,10 +257,13 @@ GNUNET_xmalloc_unchecked_ (size_t size,
  */
 void *
 GNUNET_xrealloc_ (void *ptr,
-                 size_t n,
-                 const char *filename,
-                 int linenumber)
+                  size_t n,
+                  const char *filename,
+                  int linenumber)
 {
+  (void) filename;
+  (void) linenumber;
+
 #ifdef W32_MEM_LIMIT
   n += sizeof (size_t);
   ptr = &((size_t *) ptr)[-1];
@@ -190,7 +272,8 @@ GNUNET_xrealloc_ (void *ptr,
   ptr = realloc (ptr, n);
   if ((NULL == ptr) && (n > 0))
   {
-    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
+                 "realloc");
     GNUNET_assert (0);
   }
 #ifdef W32_MEM_LIMIT
@@ -351,7 +434,7 @@ void
 GNUNET_xgrow_ (void **old,
               size_t elementSize,
               unsigned int *oldCount,
-               unsigned int newCount,
+         unsigned int newCount,
               const char *filename,
               int linenumber)
 {
@@ -360,20 +443,20 @@ GNUNET_xgrow_ (void **old,
 
   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);
-    if (*oldCount > newCount)
-      *oldCount = newCount;     /* shrink is also allowed! */
     if (NULL != *old)
-      GNUNET_memcpy (tmp, *old, elementSize * (*oldCount));
+    {
+      GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
+    }
   }
 
-  if (*old != NULL)
+  if (NULL != *old)
   {
     GNUNET_xfree_ (*old, filename, linenumber);
   }
@@ -392,8 +475,8 @@ GNUNET_xgrow_ (void **old,
  */
 int
 GNUNET_asprintf (char **buf,
-                const char *format,
-                ...)
+                 const char *format,
+                 ...)
 {
   int ret;
   va_list args;
@@ -401,6 +484,7 @@ GNUNET_asprintf (char **buf,
   va_start (args, format);
   ret = VSNPRINTF (NULL, 0, format, args);
   va_end (args);
+  GNUNET_assert (ret >= 0);
   *buf = GNUNET_malloc (ret + 1);
   va_start (args, format);
   ret = VSPRINTF (*buf, format, args);
@@ -427,9 +511,13 @@ GNUNET_snprintf (char *buf,
   va_list args;
 
   va_start (args, format);
-  ret = VSNPRINTF (buf, size, format, args);
+  ret = VSNPRINTF (buf,
+                  size,
+                  format,
+                  args);
   va_end (args);
-  GNUNET_assert (ret < size);
+  GNUNET_assert ( (ret >= 0) &&
+                 (((size_t) ret) < size) );
   return ret;
 }
 
@@ -449,7 +537,9 @@ GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
   msize = ntohs (msg->size);
   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
   ret = GNUNET_malloc (msize);
-  GNUNET_memcpy (ret, msg, msize);
+  GNUNET_memcpy (ret,
+                msg,
+                msize);
   return ret;
 }