Hi Robert,

On Wed, Apr 14, 2021 at 12:18:42PM +0200, Robert Ionescu wrote:
> Hi all,
> 
> I am working on a new patch for haproxy and would need the malloc method
> used to allocate memory for structs like the connection struct in
> connection-t.c
> In case I want to expand the struct with a new variable, do I need to take
> care of the memory for this variable or is there already a dynamic memory
> allocation for the given struct?

I'd respond: "it depends". Since the vast majority of our structs are of
fixed sizes, we use fixed-size pools to allocate them, so this normally
is the way to go. If the fields you need to add are of relatively small
size (a few bytes to a few tens of bytes), let's just add them to the
structure, and possibly try to figure if they're structurally exclusive
with other fields, in which case you could make a union between them.
If you're going to deal with several tens of bytes to a few kB, but of
mostly fixed size (or I'd say bounded size), then I guess it's not always
used so you'd rather create a new pool for this and store a pointer in
the first struct to this entry.

If the size is completely random, then you'd rather store a pointer in
the struct to something allocated using malloc(), but then you'll really
have to be extremely careful about operating systems dealing with a slow
memory allocator (especially in threaded environments) and be prepared
to see your design seriously questioned if you want to submit it, so
it'd better be discussed first to be sure you're on the right track!

Cheers,
Willy

Reply via email to