attribute nonnull/expect_nonnull for return value?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Given a function declaration

  void* f( void* a, void* b ) __attribute__(( nonnull( 1, 2 ) ));

GCC now knows that the arguments are nonnull, but how to I tell the compiler that the returned pointer is nonnull? Or if null is allowed but only the exception to the rule, I would like to tell the compiler that is should *expect* the pointer to be nonnull, but not disallow null. Actually, the second case is more important to me, but I think both should be possible.

Rationale:

Writing a library, the function's definition goes into a separate compile unit and I would like to allow the user's code to be optimized automatically. Currently, in cases where the optimization matters, the user is required to do the work manually:

  T* p = (T*)f( a, b );
  if( __builtin_expect( !p, false ) ) {
    // ...don't optimize for this case...
  }
  return p;

This is not a good solution, the library/API should take care of it. In fact, I think that this applies to an awful lot of functions (think system calls) and could really help to optimize user programs. If this is currently not possible, would it be possible that it will be added in future versions of GCC? Seeing __attribute__(( malloc )) exists, the infrastructure seems to be there, only that malloc does/guarantees way too much...

Regards, Daniel

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail


[Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

Add to Google