Revision: 41326
          http://brlcad.svn.sourceforge.net/brlcad/?rev=41326&view=rev
Author:   brlcad
Date:     2010-11-11 02:53:45 +0000 (Thu, 11 Nov 2010)

Log Message:
-----------
libbu memory management is not allowed to return NULL (by design) so having 
bu_realloc() free memory then return NULL for the case where the size is zero 
but the pointer is non-NULL would be inconsistent with that design.  
FORTUNATELY, the standard allows for the return of 'a unique pointer that can 
be successfully passed to free()' so go ahead and return a new uninitialized 
minimum-sized allocation.  this is consistent with c99 and IEEE Std 1003.1-2001.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/malloc.c

Modified: brlcad/trunk/src/libbu/malloc.c
===================================================================
--- brlcad/trunk/src/libbu/malloc.c     2010-11-11 02:33:58 UTC (rev 41325)
+++ brlcad/trunk/src/libbu/malloc.c     2010-11-11 02:53:45 UTC (rev 41326)
@@ -363,23 +363,26 @@
     /* If bu_realloc receives a NULL pointer and zero size then bomb
      * because the behavior of realloc is undefined for these inputs.
      */
-    if (UNLIKELY(!siz && !ptr)) {
+    if (UNLIKELY(ptr == NULL && siz == 0)) {
        bu_bomb("bu_realloc(): invalid input, NULL pointer and zero size\n");
     }
 
     /* If bu_realloc receives a NULL pointer and non-zero size then
-     * allocate the memory.
+     * allocate new memory.
      */
-    if (UNLIKELY(!ptr)) {
+    if (UNLIKELY(ptr == NULL && siz > 0)) {
        return bu_malloc(siz, str);
     }
 
     /* If bu_realloc receives a non-NULL pointer and zero size then
-     * free the memory.
+     * free the memory.  Instead of returning NULL, though, the
+     * standard says we can return a small allocation suitable for
+     * passing to bu_free().  Do that so we can maintain are LIBBU
+     * guarantee of worry-free memory management.
      */
-    if (UNLIKELY(!siz)) {
+    if (UNLIKELY(ptr != NULL && siz == 0)) {
        bu_free(ptr, str);
-       return (genptr_t)NULL;
+       return bu_malloc(MINSIZE, str);
     }
 
     /* If the new allocation size is smaller than the minimum size
@@ -411,8 +414,12 @@
                    "or not allocated with bu_malloc!  Ignored.\n",
                    ptr, str);
            /*
-            * Since we're ignoring this, atleast return the pointer
-            * that was passed in. We should probably return NULL.
+            * Since we're ignoring this, at least return the pointer
+            * that was passed in.  Standard says behavior is
+            * undefined when reallocating memory not allocated with
+            * the matching allocation routines (i.e., bu_malloc() or
+            * bu_calloc() in our situation), so we are fair game to
+            * just return the pointer we were given.
             */
            return ptr;
        }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Centralized Desktop Delivery: Dell and VMware Reference Architecture
Simplifying enterprise desktop deployment and management using
Dell EqualLogic storage and VMware View: A highly scalable, end-to-end
client virtualization framework. Read more!
http://p.sf.net/sfu/dell-eql-dev2dev
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to