[prev in list] [next in list] [prev in thread] [next in thread]
List: spread-users
Subject: Re: [Spread-users] [PATCH] Clarity of Mem_alloc() in memory.c
From: Jonathan Stanton <jonathan () cnds ! jhu ! edu>
Date: 2002-09-17 23:25:24
[Download RAW message or body]
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@finemaltcoding.com>
>
--
-------------------------------------------------------
Jonathan R. Stanton jonathan@cs.jhu.edu
Dept. of Computer Science
Johns Hopkins University
-------------------------------------------------------
_______________________________________________
Spread-users mailing list
Spread-users@lists.spread.org
http://lists.spread.org/mailman/listinfo/spread-users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic