#undef

2025-10-13

right i need to follow #define #undef and #pragma again

2025-09-10

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

perf report revealing ton of CPU time spent in get_memnode() traversing a linked list.get_memnode function code using linear list search, which is O(n).pagealign_free() function calling get_memnode() for other than HAVE_POSIX_MEMALIGN code paths.cvs process pegged to 100% CPU time.
2025-09-07

I'd track a stack of control-flow preprocessor lines tracking whether to keep or discard the lines between them, inserting #line where needed. This is how I'd handle #if, #elif, #else, #endif, #ifdef, #ifndef, #elifdef, #elifndef.

Some of these take identifiers whose presence it should check in the macros table, others would interpret infix expressions via a couple stacks & The Shunting Yard Algorithm. Or they simply end a control-flow block.

#undef removes an entry from the macros table.

3/4

2025-08-18

Weekly GCC update:

Optimization improvements:
* Copy prop for aggregates improvements; now into args
* Recongize integer zero as zeroing for memset into aggregate copy or memcpy
* Improve VN support over aggregates copies
* Recongize saturation multiple more
* Allow for more mergeable constants to be placed in the mergeable section

C++ improvements/changes:
* Implement C++20 (Defect report) P1766R1: Mitigating minor modules maladies
* Fix parsing of non-comma variadic methods with default args
* Warn on #undef/#define cpp.predefined macros
* Implement C++26 P1306R5 - Expansion statements
* Implement P2036R3 - Change scope of lambda trailing-return-type
* Finish up P2115R0 implementation (unnamed unscoped enums)

Target changes:
* epiphany and rl78 are marked as obsolete targets
* LoongArch: 128bit atomics support
* LoongArch: _BitInt support
* x86: Add target("80387") function attribute
* RISCV: Add MIPS prefetch extensions
* aarch64: Fix CMPBR extension

Uli Kusterer (Not a kitteh)uliwitness@chaos.social
2025-08-02

@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"?

2025-07-17

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

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.

ity [unit X-69] - VIOLENT FUCKity@estradiol.city
2025-06-10
/*
* The sys_call_table[] is no longer used for system calls, but
* kernel/trace/trace_syscalls.c still wants to know the system
* call address.
*/
#define __SYSCALL(nr, sym) __x64_##sym,
const sys_call_ptr_t sys_call_table[] = {
#include <asm/syscalls_64.h>
};
#undef __SYSCALL

#define __SYSCALL(nr, sym) case nr: return __x64_##sym(regs);
long x64_sys_call(const struct pt_regs *regs, unsigned int nr)
{
switch (nr) {
#include <asm/syscalls_64.h>
default: return __x64_sys_ni_syscall(regs);
}
}

:neobot_glare_sob:

lizzy :bi_heart: :cuwu:lizzy@social.vlhl.dev
2025-05-17

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
}
Jevin Swevaljevinskie
2025-04-11

How to politely use assert in a header (gcc/clang/msvc/what else?):

push_macro("NDEBUG")
NDEBUG
<assert.h>
// your asserts should properly assert
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.

note to self unreal tournamentmothcompute@vixen.zone
2025-03-03
sjolsen (MP3 & JPEG compatible!)sjolsen@tech.lgbt
2025-02-26

@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;
}
2025-02-05

@ipg

#undef HAS_GAMES
2025-01-07

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.

Jari Komppa 🇫🇮 (has moved)sol_hsa@peoplemaking.games
2024-12-12

@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.

2024-10-04

@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)

DJ月経少年野崎くんA.K.A杜仲茶またの名を立花いづみtochu_cha@mstdn.jp
2024-09-17

gosh> (print "a")
a
#undef

なんでシンボルかと思っても文字列だぞ。気を付けろ!(俺がな)

IB Teguh TMteguhteja
2024-09-17

Discover how conditional compilation directives can enhance your C programming skills. Learn to use , , , , , , and to create more flexible and efficient code. Unlock the power of the C preprocessor!

teguhteja.id/conditional-compi

Client Info

Server: https://mastodon.social
Version: 2025.07
Repository: https://github.com/cyevgeniy/lmst