home.social

#undef — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #undef, aggregated by home.social.

  1. sys/arch/m68k/include: asm.h

    christos: Make the _C_LABEL definition match exactly <sys/cdefs_elf.h> because
    atomic_cas.S now includes <sys/ras.h> and <sys/ras.h> includes <sys/cdefs.h>
    because it wants things like __CONCAT(). I think it is better to do it this
    way rather than #undef here or in atomic_cas.S because then if the definition
    changes it will break again.

    cvsweb.netbsd.org/bsdweb.cgi/s

  2. Oh for heck's sake.

    #undef Status
    #undef Success

    This at the top of the file fixes the problem. I'm in #define hell and I don't know why. :(

  3. As it happens, we still use CVS in our operating system project (there are reasons for doing this, but migration to git would indeed make sense).

    While working on our project, we occasionally have to do a full checkout of the whole codebase, which is several gigabytes. Over time, this operation has gotten very, very, very slow - I mean "2+ hours to perform a checkout" slow.

    This was getting quite ridiculous. Even though it's CVS, it shouldn't crawl like this. A quick build of CVS with debug symbols and sampling the "cvs server" process with Linux perf showed something peculiar: The code was spending the majority of the time inside one function.

    So what is this get_memnode() function? Turns out this is a support function from Gnulib that enables page-aligned memory allocations. (NOTE: I have no clue why CVS thinks doing page-aligned allocations is beneficial here - but here we are.)

    The code in question has support for three different backend allocators:
    1. mmap
    2. posix_memalign
    3. malloc

    Sounds nice, except that both 1 and 3 use a linked list to track the allocations. The get_memnode() function is called when deallocating memory to find out the original pointer to pass to the backend deallocation function: The node search code appears as:

    for (c = *p_next; c != NULL; p_next = &c->next, c = c->next)
    if (c->aligned_ptr == aligned_ptr)
    break;

    The get_memnode() function is called from pagealign_free():

    #if HAVE_MMAP
    if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
    error (EXIT_FAILURE, errno, "Failed to unmap memory");
    #elif HAVE_POSIX_MEMALIGN
    free (aligned_ptr);
    #else
    free (get_memnode (aligned_ptr));
    #endif

    This is an O(n) operation. CVS must be allocating a huge number of small allocations, which will result in it spending most of the CPU time in get_memnode() trying to find the node to remove from the list.

    Why should we care? This is "just CVS" after all. Well, Gnulib is used in a lot of projects, not just CVS. While pagealign_alloc() is likely not the most used functionality, it can still end up hurting performance in many places.

    The obvious easy fix is to prefer the posix_memalign method over the other options (I quickly made this happen for my personal CVS build by adding tactical #undef HAVE_MMAP). Even better, the list code should be replaced with something more sensible. In fact, there is no need to store the original pointer in a list; a better solution is to allocate enough memory and store the pointer before the calculated aligned pointer. This way, the original pointer can be fetched from the negative offset of the pointer passed to pagealign_free(). This way, it will be O(1).

    I tried to report this to the Gnulib project, but I have trouble reaching gnu.org services currently. I'll be sure to do that once things recover.

    #opensource #development #bugstories

  4. @eniko You mean as a shorthand for #undef and #define in a row ? Or as a "I've undefed this, now restore the previous version"?

  5. So now I have a bunch of this in my code

    #undef EXCEPTION_HANDLER
    #define EXCEPTION_HANDLER myhandler

    Then a block of code followed by

    #undef EXCEPTION_HANDLER
    #define EXCEPTION_HANDLER default_exception_handler

    Is it jank? Yes, extremely. Does it do what I need it to do? Also yes. Do I feel embarrassed about this in any way? Not at all

  6. Ughhh, autotools....

    Not naming the project here.. Why would you assume that malloc and realloc are both broken just because you're cross compiling? Are you just being lazy? Even worse, don't '#undef malloc' and replace it with your own *broken* version.

    Better yet, just don't even use autotools at all.

  7. silly little thing i made to debug that heisenbug

    collecting all the things that i want to log and printing them after the fact, this avoids the overhead of printing immediately that seeming skewed the timing enough to make the bug disappear

    there is one logger for every relevant thread, because synchronizing the logging would have also skewed the result obviously

    struct microlog_entry
    {
        LONGLONG perf;
        const char *text;
    };
    
    #define MICROLOGGER_CAP 100
    
    struct micrologger
    {
        size_t pos;
        size_t read_pos;
        struct microlog_entry entries[MICROLOGGER_CAP];
    };
    
    #define microlog(l, text) if (l.pos < MICROLOGGER_CAP) { \
            LARGE_INTEGER perf; \
            QueryPerformanceCounter(&perf); \
            l.entries[l.pos++] = (struct microlog_entry) { perf.QuadPart, text }; \
        }
    
    static struct micrologger l_grabber_events;
    static struct micrologger l_grabber_samples;
    static struct micrologger l_main;
    
    void microlog_flush()
    {
    #define NUM_MICROLOGGERS 3
        struct micrologger *loggers[NUM_MICROLOGGERS] = { &l_grabber_events, &l_grabber_samples, &l_main };
    
        for (;;) {
            struct micrologger *least = NULL;
    
            for (size_t i = 0; i < NUM_MICROLOGGERS; i++) {
                if (loggers[i]->read_pos == loggers[i]->pos)
                    continue;
                if (!least || loggers[i]->entries[loggers[i]->read_pos].perf < least->entries[least->read_pos].perf)
                    least = loggers[i];
            }
    
            if (!least)
                break;
    
            printf("%s\n", least->entries[least->read_pos].text);
            least->read_pos++;
        }
    #undef NUM_MICROLOGGERS
    }
    
  8. How to politely use assert in a header (gcc/clang/msvc/what else?):

    #pragma push_macro("NDEBUG")
    #undef NDEBUG
    #include <assert.h>
    // your asserts should properly assert
    #pragma pop_macro("NDEBUG")
    // nobody has 2 know

    Your vendor’s assert.h should be able to be included multiple times with NDEBUG defined or not.

  9. @kirakira did you know that you can define structured data in the c preprocessor using church encoding?

    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>

    #define USERS(_user) \
    _user("sjolsen", "tech.lgbt"), \
    _user("kirakira", "furry.engineer"), \
    _user("Gargron", "mastodon.social")

    #define handles_user(_name, _instance) "@" _name "@" _instance

    static const char *const handles[] = {
    USERS(handles_user)
    };

    #undef handles_user

    #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))

    int main() {
    for (size_t i = 0; i < ARRAY_SIZE(handles); ++i) {
    puts(handles[i]);
    }

    return EXIT_SUCCESS;
    }
  10. The Fourth Hard Problem in Computer Science:

    Whether to use

    #define FOO 0

    or

    /* #undef FOO */

    or

    #undef FOO

    or even

    /* #define FOO */

    for things that aren't present/enabled/turned on in a config.h. FOO can start with HAVE_ or not.

  11. @pythno I think I remember having to do a kludge to work around that at some point.

    I think the min/max are macros in windows.h, so #undef should do the trick.

  12. @icculus I love SDL and have been using it for more than a decade, but the way they treat `main` always bothered me (`#define main SDL_main` is already obnoxious, and the actual main being implemented in a standalone shared lib is extra spicy). I always just did `#undef main` and took control from SDL on this.

    Now they're making this thing even more integral and this makes me sad :(

    (yes you can at least disable it with a special macro, but this being the default really bothers me)

  13. Discover how conditional compilation directives can enhance your C programming skills. Learn to use #ifdef, #ifndef, #undef, #if, #else, #elif, and #endif to create more flexible and efficient code. Unlock the power of the C preprocessor!

    teguhteja.id/conditional-compi

  14. what was in the air in 1997 bro
    /* define the boolean values in a general and
    * portable way.
    */
    #undef TRUE
    #define TRUE (0 || !(0))
    #undef FALSE
    #define FALSE (!(TRUE))
    /* typedef enum { false = FALSE, true = TRUE } bool; */

    /* some other names for true and false */
    #define YES TRUE
    #define NO FALSE

    #define GOOD TRUE
    #define WRONG FALSE

    #undef OK
    #define OK TRUE
    #define BAD FALSE

    #define SOME TRUE
    #define NONE FALSE

  15. @fluor I've seen people do complex repeated-include metaprogramming with the C preprocessor for sure (it helps to know about #undef, #stringify and ##) but also leaning on the C preprocessor this way can get you in some very hard to debug spots very fast.

  16. Recursive inclusion is possible in C, and more surprisingly it even makes sense in some cases. In fact it provides an easy way to do code unrolling of small code snippets:

    #if !defined(DO_I) || (DO_I < DO_BOUND)# include "do-incr.c"DO_BODY# include __FILE__#endif#undef DO_I

    Here this uses two macros that have to be defined beforehand, DO_BOUND and DO_BODY, and one macro DO_I for a hexadecimal loop counter that is maintained by the include file "do-incr.c". If we hold the above recursive code in some file "doit.c" (plus some extras, see below) we can use this as simply as in

    #define DO_BODY [DO_I] = DO_I,#define DO_BOUND 0x17unsigned array[] = {# include "doit.c"};#undef DO_BODY#undef DO_BOUND

    do generate the intialization of an array,

    unsigned array[] = {[0x0000] = 0x0000,[0x0001] = 0x0001,...[0x0016] = 0x0016,[0x0017] = 0x0017,};

    or with an additional macro DO_NAME (DO_I without a hex prefix) as

    #define ENAME_(X) enum_ ## X#define ENAME(X) ENAME_(X)#define DO_BODY ENAME(DO_NAME) = DO_I,#define DO_BOUND 197enum {# include "doit.c"};#undef DO_BODY#undef DO_BOUND

    for the declaration of an enumeration type

    enum fun {enum_0000 = 0x0000,enum_0001 = 0x0001,...enum_00C4 = 0x00C4,enum_00C5 = 0x00C5,};

    Here the given limit of 197 is in fact a limit that is imposed by gcc for which I was testing this. They restrict the depth of inclusion to 200, unless you raise that limit with the command line option -fmax-include-depth.

    Note also, that the recursive line #include __FILE__ uses a macro to specify the file name because #include lines are indeed evaluated by the preprocessor if necessary.

    Recursion is only reasonable if we have a stop condition, so to handle this the include file "do-incr.c" is crucial. Since in standard C we have no way to evaluate the line of a #define while processing it, we have to ensure that we can change the value of DO_I. That file looks something like

    #ifndef DO_I# undef  DO_I0# define DO_I0 0# undef  DO_I1# define DO_I1 0# undef  DO_I2# define DO_I2 0# undef  DO_I3# define DO_I3 0# define DO_I DO_HEX(DO_NAME)#elif DO_HEX(DO_I0) == 0# undef DO_I0# define DO_I0 1#elif DO_HEX(DO_I0) == 1# undef DO_I0# define DO_I0 2...#elif DO_HEX(DO_I0) == 0xF# undef DO_I0# define DO_I0 0# include "do-incr1.c"#endif

    As you may have guessed, this uses macros DO_I0, DO_I1, DO_I2, and DO_I3 as hexdigits of a 16 bit number, and includes another file "do-incr1.c" if the digit DO_I0 overflows.

    I hope that if you are interested in this technique, you will be able to join the dots in the above code. To be complete, here are the macros that join the digits to make a hexnumber (DO_HEX) and to make a name component (DO_NAME):

    #ifndef DO_HEX# define DO_HEX_(X) 0x ## X# define DO_HEX(X) DO_HEX_(X)# define DO_NAME_(A, B, C, D) A ## B ## C ## D# define DO_NAME_IIII(A, B, C, D) DO_NAME_(D, C, B, A)# define DO_NAME DO_NAME_IIII(DO_I0, DO_I1, DO_I2, DO_I3)#endif

    https://gustedt.wordpress.com/2024/05/30/include-__file__/

    #define #elif #endif #undef

  17. the stub includes the winsocks2.h Windows header which, through a long chain of includes, stumbles upon the guiddef.h header which defines a GUID struct that conflicts with a GUID type defined within our project

    the solution? straight up just rename Windows' type!

    #define GUID _GUID
    #include <winsock2.h>
    #undef GUID
  18. @mcc X is Like This, unfortunately
    the solution is generally a pile of #undef lines

  19. Note to everyone replying to this thread to suggest I use #undef: I did it thirty-five minutes ago.

  20. @amdg2 aha, right, good point about conditional includes! And the same defer mechanism would be just as useful for undefining macros. So, I imagine something like this:

    // in stdlib.h

    #define __cplusplus
    #include <algorithm>

    #defer
    #undef __cplusplus
    #uninclude <algorithm>
    #enddefer

    static inline void qsort(whatever) {
    // Efficient implementation using std::sort
    }