gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r13791 - in gnunet/src: include util


From: gnunet
Subject: [GNUnet-SVN] r13791 - in gnunet/src: include util
Date: Wed, 24 Nov 2010 15:23:32 +0100

Author: grothoff
Date: 2010-11-24 15:23:32 +0100 (Wed, 24 Nov 2010)
New Revision: 13791

Modified:
   gnunet/src/include/gnunet_common.h
   gnunet/src/util/common_allocation.c
Log:
memdup

Modified: gnunet/src/include/gnunet_common.h
===================================================================
--- gnunet/src/include/gnunet_common.h  2010-11-24 13:42:41 UTC (rev 13790)
+++ gnunet/src/include/gnunet_common.h  2010-11-24 14:23:32 UTC (rev 13791)
@@ -354,6 +354,15 @@
 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
 
 /**
+ * Allocate and initialize a block of memory.
+ * 
+ * @param buf data to initalize the block with
+ * @param size the number of bytes in buf (and size of the allocation)
+ * @return pointer to size bytes of memory, never NULL (!)
+ */
+#define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
+
+/**
  * Wrapper around malloc. Allocates size bytes of memory.
  * The memory will be zero'ed out.
  *
@@ -478,7 +487,22 @@
 void *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.
+ *
+ * @param buf buffer to initialize from (must contain size bytes)
+ * @param size number of bytes to allocate
+ * @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_xmemdup_ (const void *buf, size_t size, const char *filename, int 
linenumber);
+
+
+/**
  * Allocate memory.  This function does not check if the allocation
  * request is within reasonable bounds, allowing allocations larger
  * than 40 MB.  If you don't expect the possibility of very large

Modified: gnunet/src/util/common_allocation.c
===================================================================
--- gnunet/src/util/common_allocation.c 2010-11-24 13:42:41 UTC (rev 13790)
+++ gnunet/src/util/common_allocation.c 2010-11-24 14:23:32 UTC (rev 13791)
@@ -69,6 +69,46 @@
 
 
 /**
+ * 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.
+ *
+ * @param buf buffer to initialize from (must contain size bytes)
+ * @param size number of bytes to allocate
+ * @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_xmemdup_ (const void *buf, size_t size, const char *filename, int 
linenumber)
+{
+  void *ret;
+  /* As a security precaution, we generally do not allow very large
+     allocations here */
+  GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
+#ifdef W32_MEM_LIMIT
+  size += sizeof (size_t);
+  if (mem_used + size > W32_MEM_LIMIT)
+    return NULL;
+#endif
+  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
+  ret = malloc (size);
+  if (ret == NULL)
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
+      abort ();
+    }
+#ifdef W32_MEM_LIMIT
+  *((size_t *) ret) = size;
+  ret = &((size_t *) ret)[1];
+  mem_used += size;
+#endif
+  memcpy (ret, buf, size);
+  return ret;
+}
+
+
+
+/**
  * Wrapper around malloc. Allocates size bytes of memory.
  * The memory will be zero'ed out.
  *




reply via email to

[Prev in Thread] Current Thread [Next in Thread]