gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r12140 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r12140 - gnunet/src/util
Date: Fri, 2 Jul 2010 23:05:43 +0200

Author: grothoff
Date: 2010-07-02 23:05:43 +0200 (Fri, 02 Jul 2010)
New Revision: 12140

Modified:
   gnunet/src/util/common_allocation.c
   gnunet/src/util/connection.c
   gnunet/src/util/server_mst.c
Log:
fixing calculations and code for buffer realloc

Modified: gnunet/src/util/common_allocation.c
===================================================================
--- gnunet/src/util/common_allocation.c 2010-07-02 20:26:31 UTC (rev 12139)
+++ gnunet/src/util/common_allocation.c 2010-07-02 21:05:43 UTC (rev 12140)
@@ -103,6 +103,7 @@
   return result;
 }
 
+
 /**
  * Reallocate memory. Checks the return value, aborts if no more
  * memory is available.
@@ -139,6 +140,7 @@
   return ptr;
 }
 
+
 /**
  * Free memory. Merely a wrapper for the case that we
  * want to keep track of allocations.

Modified: gnunet/src/util/connection.c
===================================================================
--- gnunet/src/util/connection.c        2010-07-02 20:26:31 UTC (rev 12139)
+++ gnunet/src/util/connection.c        2010-07-02 21:05:43 UTC (rev 12140)
@@ -1586,27 +1586,14 @@
                                          GNUNET_CONNECTION_TransmitReadyNotify
                                          notify, void *notify_cls)
 {
-  size_t temp_size;
-#if !REALLOC
-  char *temp_buf;
-#endif
   if (sock->nth.notify_ready != NULL)
     return NULL;
   GNUNET_assert (notify != NULL);
-  if ((sock->write_buffer_size < size) && (size < 
GNUNET_SERVER_MAX_MESSAGE_SIZE))
+  GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  if (sock->write_buffer_size < size)
     {
-      temp_size = sock->write_buffer_size + size + 1;
-      if (temp_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-        temp_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
-#if REALLOC
-      sock->write_buffer = GNUNET_realloc(sock->write_buffer, temp_size);
-#else
-      temp_buf = GNUNET_malloc(temp_size);
-      memcpy(temp_buf, sock->write_buffer, sock->write_buffer_size);
-      GNUNET_free(sock->write_buffer);
-      sock->write_buffer = temp_buf;
-#endif
-      sock->write_buffer_size = temp_size;
+      sock->write_buffer = GNUNET_realloc(sock->write_buffer, size);
+      sock->write_buffer_size = size;
     }
   GNUNET_assert (sock->write_buffer_size >= size);
   GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);

Modified: gnunet/src/util/server_mst.c
===================================================================
--- gnunet/src/util/server_mst.c        2010-07-02 20:26:31 UTC (rev 12139)
+++ gnunet/src/util/server_mst.c        2010-07-02 21:05:43 UTC (rev 12140)
@@ -129,14 +129,9 @@
   size_t delta;
   uint16_t want;
   char *ibuf;
-
-#if !REALLOC
-  char *temp_buf;
-#endif
   int need_align;
   unsigned long offset;
   int ret;
-  size_t newsize;
 
 #if DEBUG_SERVER_MST
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -144,22 +139,6 @@
              (unsigned int) size,
              (unsigned int) (mst->pos - mst->off));
 #endif
-  if ((size > mst->curr_buf) && (size < GNUNET_SERVER_MAX_MESSAGE_SIZE)) /* 
Received bigger message than we can currently handle! */
-    {
-      newsize = mst->curr_buf + size + 1; /* How much space do we need? */
-      if (newsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-        newsize = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1; /* Check it's not bigger 
than GNUNET_SERVER_MAX_MESSAGE_SIZE */
-#if REALLOC
-      mst->hdr = GNUNET_realloc(mst->hdr, newsize);
-#else
-      temp_buf = GNUNET_malloc(newsize);
-      memcpy(temp_buf, mst->hdr, mst->curr_buf);
-      GNUNET_free(mst->hdr);
-      mst->hdr = temp_buf;
-#endif
-      mst->curr_buf = newsize;
-    }
-
   ret = GNUNET_OK;
   ibuf = (char*)mst->hdr;
   while (mst->pos > 0)
@@ -211,6 +190,13 @@
                   mst->pos);
          mst->off = 0;
        }
+      if (want > mst->curr_buf)
+       {
+         mst->hdr = GNUNET_realloc(mst->hdr, want);
+         ibuf = (char*)mst->hdr;         
+         mst->curr_buf = want;
+       }
+      hdr = (const struct GNUNET_MessageHeader*) &ibuf[mst->off];
       if (mst->pos - mst->off < want)
        {
          delta = GNUNET_MIN (want - (mst->pos - mst->off),
@@ -290,6 +276,12 @@
  copy:
   if ( (size > 0) && (! purge) )
     {
+      if (size + mst->pos > mst->curr_buf)
+       {
+         mst->hdr = GNUNET_realloc(mst->hdr, size + mst->pos);
+         ibuf = (char*)mst->hdr;         
+         mst->curr_buf = size + mst->pos;
+       }
       GNUNET_assert (mst->pos + size <= mst->curr_buf);
       memcpy (&ibuf[mst->pos], buf, size);
       mst->pos += size;




reply via email to

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