[Spread-users] [PATCH] Clarity of Mem_alloc() in memory.c

Daniel Rall dlr at finemaltcoding.com
Tue Sep 17 15:10:16 EDT 2002


For best readability of memory allocations, the individual elements
used in the size calculation should match up to the location in memory
where each data structure sits.

For instance, when allocating a memory header which sits at the front
of a buffer, the calculation to determine the size of the header
should come before the length of the buffer, because that is the way
the data structures are actually positioned in memory.

block =  malloc(sizeof(mem_header) + buffer_length)

                |     header       |   buffer    |    ....


Index: memory.c
===================================================================
RCS file: /storage/cvsroot/spread/daemon/memory.c,v
retrieving revision 1.4
diff -u -u -r1.4 memory.c
--- memory.c	29 Aug 2002 15:43:01 -0000	1.4
+++ memory.c	17 Sep 2002 19:01:33 -0000
@@ -532,7 +532,7 @@
         }
 
         
-        head_ptr = (mem_header *) calloc(1, length + sizeof(mem_header));
+        head_ptr = (mem_header *) calloc(1, sizeof(mem_header) + length);
         if (head_ptr == NULL) 
         {
                 Alarm(MEMORY, "mem_alloc: Failure to calloc a block. Returning NULL block\n");

-- 

Daniel Rall <dlr at finemaltcoding.com>




More information about the Spread-users mailing list