Skip to content

Instantly share code, notes, and snippets.

@encukou
Last active February 9, 2024 10:44
Show Gist options
  • Save encukou/41e72922c441ed25688b7ff3d166ac81 to your computer and use it in GitHub Desktop.
Save encukou/41e72922c441ed25688b7ff3d166ac81 to your computer and use it in GitHub Desktop.

A function may be shadowed by a static inline function or macro with the same behavior. Typically, this allows better performance for C/C++.

Shadowing example

To provide a static inline equivalent to an exported function, write something like:

.h Header:

    static inline returntype
    _Py_Foo_impl(ARGS)
    {
        ...
    }

    PyAPI_FUNC(returntype) Py_Foo (ARGS);

    #define Py_Foo _Py_Foo_impl

.c Code:

    // at the end (after all calls to Py_Foo):
    #undef Py_Foo

    returntype
    Py_Foo(ARGS)
    {
        return _Py_Foo_impl(ARGS);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment