Re: [PATCH 2/2] strbuf: allow to use preallocated memory
- From
Mike Hommey <mh@glandium.org>
- Date
- May 30, 2016, 21:56 UTC
- Message-ID
- <20160530215652.GA6456@glandium.org>
- In-Reply-To
- <20160530103642.7213-3-william.duclot@ensimag.grenoble-inp.fr>
Show 6 quoted lines
> struct strbuf {
> + unsigned int flags;
> size_t alloc;
> size_t len;
> char *buf;
> };Depending whether the size of strbuf matters, it /might/ be worth considering some packing here. malloc() usually returns buffers that can contain more data than what is requested. Which means allocation sizes could be rounded and that wouldn't change the amount of allocated memory. On glibc malloc_usable_size(malloc(1)) apparently returns 24. On jemalloc, it's 4 or 8. It's in the same ballbark with many allocators.
So, it would be possible to round alloc such that it's always a multiple of, say, 4, and stick flags in the low, unused bits.
Whether it's worth doing is another question.
Mike