Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #126323

Re: Aligned Issue when malloc()

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Aligned Issue when malloc()
Date 2018-02-06 22:41 +1300
Message-ID <fdtbipFc9eU4@mid.individual.net> (permalink)
References <7d04ef8a-4410-4855-a69a-34c350465879@googlegroups.com>

Show all headers | View raw


On 02/06/2018 10:17 PM, hwding wrote:
> In the libevent manual, memory management chapter(http://www.wangafu.net/%7Enickm/libevent-book/Ref1_libsetup.html), when we use our own allocating functions,
> before certain size of memory segment is allocated,
> the custom allocater always ask for an extra space to record
> the information(the size) of this piece of memory.
> 
> void *chunk = malloc(sz + ALIGNMENT);
> if (!chunk) return chunk;
> total_allocated += sz;
> *(size_t*)chunk = sz; // size is written here
> 
> Then we use the macro function to return the real starting address
> of that memory segment.
> 
> return OUTPTR(chunk);
> 
> The definitions:
> 
> /* This union's purpose is to be as big as the largest of all the
>   * types it contains. */
> union alignment {
>      size_t sz;
>      void *ptr;
>      double dbl;
> };
> /* We need to make sure that everything we return is on the right
>     alignment to hold anything, including a double. */
> #define ALIGNMENT sizeof(union alignment)
> 
> /* We need to do this cast-to-char* trick on our pointers to adjust
>     them; doing arithmetic on a void* is not standard. */
> #define OUTPTR(ptr) (((char*)ptr)+ALIGNMENT)
> #define INPTR(ptr) (((char*)ptr)-ALIGNMENT)
> 
> Since only the size_t sz is used in my_malloc, my_realloc and my_free, why
> the void * and double type are claimed in the union alignment?

To ensure the returned pointer remains suitably aligned for any built-in 
type (assuming double has the strictest alignment requirement in this case).

-- 
Ian.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Aligned Issue when malloc() hwding <m@amastigote.com> - 2018-02-06 01:17 -0800
  Re: Aligned Issue when malloc() Ian Collins <ian-news@hotmail.com> - 2018-02-06 22:41 +1300
  Re: Aligned Issue when malloc() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 11:52 +0000
    Re: Aligned Issue when malloc() hwding <m@amastigote.com> - 2018-02-06 04:35 -0800
      Re: Aligned Issue when malloc() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 13:48 +0000
        Re: Aligned Issue when malloc() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-02-06 12:55 -0800
      Re: Aligned Issue when malloc() Richard Damon <Richard@Damon-Family.org> - 2018-02-06 08:52 -0500
        Re: Aligned Issue when malloc() "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-06 09:59 -0500
          Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-06 07:20 -0800
            Re: Aligned Issue when malloc() Robert Wessel <robertwessel2@yahoo.com> - 2018-02-06 11:50 -0600
            Re: Aligned Issue when malloc() Keith Thompson <kst-u@mib.org> - 2018-02-06 11:34 -0800
            Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-09 16:20 +0200
              Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-15 08:56 -0800
                Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-15 19:35 +0200
                Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-15 11:27 -0800
                Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-16 02:48 +0200
                Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-16 08:49 -0800
      Re: Aligned Issue when malloc() "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-06 09:41 -0500

csiph-web