gEDA-dev: PCB MyFree

DJ Delorie dj at delorie.com
Thu Jun 22 14:25:43 EDT 2006


> Hmm.  Details.
> 
> PCB's code base uses a hand-generated (and sometimes wrong)
> __FUNCTION__ as the debugging info; dmalloc uses __FILE__, __LINE__.

__FUNCTION__ is a gcc extension.  Very useful when it's available, so
we synthesize it from the others when it's not.

> These need to at least crash when they fail.  That is
> conventional for the x prefix.

Right, with some useful message about running out of memory.

In our case, we could also try to save the current board to a new /tmp
file, after the message and before we exit.  It might work.

>   free
> Does the code depend on the "set pointer to null" feature?

I don't know.  I use MyFree for consistency.  We can still define
MyFree as a macro, passing *x to xfree and then setting *x=0.

> This function never fails,

Older free()s fail when passed NULL, plus there's the consistency
aspect (malloc/free, new/delete, xmalloc/xfree).

Plus it gives us a hook for other things, like if we always pass
file/line to all malloc/free, we can actually allocate extra memory
and store the file/line in it, to help us track down memory problems.

> is xfree() a believable name for the macro that sets its argument to
> NULL after the free()?

No, xfree() doesn't do that, and IMHO that would surprise people.

At a previous job we had something like MyFree() but when you combine
that with C++ destructors, you end up zeroing out the wrong pointer.
It was both deadly and subtle.

> How to make an invocation of (e.g.) xcalloc call a debugging
> calloc with the proper __FILE__ and __LINE__, then exit on
> failure?  It has to be a macro to get access to those terms.
> That macro has to be a valid expression.  If I could trust
> the gcc ternary operator extension, this could be written
> 
> #define xcalloc(nmemb, size) (calloc(nmemb,size)?:(perror("xcalloc"),exit(1),NULL))

#define xcalloc(n,s) xcalloc_2(n,s,__FILE__,__LINE__)

Let the helper function fail if needed.

> I don't see the "missing second argument to the ternary operator"
> feature documented in my C99 draft, and I _know_ it wasn't in the
> C89 standard, but gcc -Wall -std=c{8,9}9 doesn't complain about it.

It's a gcc extension.  Try -ansi -pedantic.

> #define xcalloc(count, size) xcalloc_function(__FILE__, __LINE__, count, size)

Yeah, like that.


More information about the geda-dev mailing list