Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #126326
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Aligned Issue when malloc() |
| Date | 2018-02-06 11:52 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <874lmumj2n.fsf@bsb.me.uk> (permalink) |
| References | <7d04ef8a-4410-4855-a69a-34c350465879@googlegroups.com> |
hwding <m@amastigote.com> writes:
> 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?
The question has been answered, so I'll just suggest out that this could
be written a bit more clearly using a flexible array member (or the
"struck hack" for older C. Start with
struct allocation {
size_t size;
union {
size_t s;
void *p;
double d;
} data[];
};
and the allocation code becomes
struct allocation *p = malloc(sizeof *p + size);
p->size = size;
return p->data;
The code to convert the data back to the struct pointer can be made more
general than the rather basic INPTR macro
static inline struct allocation *inptr(void *p)
{
return (void *)((char *)p - offsetof(struct allocation, data));
}
because this function allows simple access to the size data via
inptr(p)->size;
but a macro
#define INPTR(ptr) ((char *)(ptr) - offsetof(struct allocation, data))
will do if this extra convenience is not required.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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