[U-Boot] "_ENTRY" is both a struct and a typedef?

Måns Rullgård mans at mansr.com
Fri Sep 9 13:15:29 CEST 2016


<Peter.Chubb at data61.csiro.au> writes:

>>>>>> "Robert" == Robert P J Day <rpjday at crashcourse.ca> writes:
>
> Robert>   from lib/hashtable.c:
>
> Robert>   typedef struct _ENTRY { int used; ENTRY entry; } _ENTRY;
>
> Robert> ok, that's just kind of creepy ... defining a typedef over top
> Robert> of a struct of the same name. does anyone else find that
> Robert> strange?
>
> It's standard practice in some C styles.  Personally I'd delete the
> struct tag, as the typedef should be used everywhere instead.

I'd rather drop the typedef.  The struct/typedef is only referenced by
name in two places.

First in include/search.h:

/* Opaque type for internal use.  */
struct _ENTRY;
[...]
/* Data type for reentrant functions.  */
struct hsearch_data {
	struct _ENTRY *table;

It is standard practice to use "struct foo" in such constructs.

The second use is further down in lib/hashtable.c:

	htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY));

This line is terrible for two reasons:
1. Type-casting the return value of calloc is always wrong.
2. It is safer to use sizeof(*htab->table) instead of an explicit type.

If someone *really* wants to "fix" this, the proper thing is to drop the
typedef and rewrite the calloc line to not explicitly mention the type.

Then again, this is code borrowed from uclibc/glibc, so perhaps it's
best to simply leave it alone.

-- 
Måns Rullgård


More information about the U-Boot mailing list