xserver: Cleaning up memory allocation functions and macros

Kristian Høgsberg krh at bitplanet.net
Mon Apr 30 13:13:05 PDT 2007


On 4/28/07, Magnus Vigerlöf <Magnus.Vigerlof at home.se> wrote:
...
> So, what memory allocation functions should be used from now on?
>
> * Use standard libc-functions
>   - For every call to alloc/calloc/realloc/... there *must* be a check if it
> succeded to allocate the memory.
>   - free() can take NULL as argument, so it's not needed to test for NULL
> before calling it.

Sounds good.

> * xstrdup() will be defined as a macro which can handle being passed NULL.
> strdup doesn't.

I would just do this as a function.

> * xfreeZ() will be defined as a macro that set the pointer to NULL after
> freeing the memory with free(). Use *only* if the pointer must be set to NULL
> for some reason.

I really want to avoid this.  I'm personally very adverse to code that
sets pointers to NULL "for good measure" or "just in case", only to
free the struct containing these pointers a couple of lines down.
Aside from being unnecessary bloat, more seriously, it obfuscates the
code.  Some times there is a good reason for setting a pointer to
NULL, for example, to prevent another code path from freeing that
pointer again.  If you set pointers to NULL for no good reason, it
obscures the cases where there is a point to doing it.

So, I don't what a function that encourages superfluous NULLing of
pointers and when a pointer needs to be set to zero after being freed,
I *want* to see the assignment, instead of a cryptic 'Z' prefix to
free.

> What have I missed?

We have a couple of allocating snprintf-style functions, did you want
to do anything about those?  If we're streamlining our allocation
functions, we should consider them too.  I think they're called
Xprintf or something, I always wanted a name that better reflected
their allocating nature - like, xstrdup_printf or something.

But other than that, it looks great, thanks for going through this.
Kristian


More information about the xorg mailing list