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

Jonathan Stanton jonathan at cnds.jhu.edu
Tue Sep 17 19:25:24 EDT 2002


Sure, makes sense to me. I'll do it.

Jonathan


On Tue, Sep 17, 2002 at 12:10:16PM -0700, Daniel Rall wrote:
> 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>
> 

-- 
-------------------------------------------------------
Jonathan R. Stanton         jonathan at cs.jhu.edu
Dept. of Computer Science   
Johns Hopkins University    
-------------------------------------------------------




More information about the Spread-users mailing list