joyces

seattle cat

  • she/they hir/it | plural

domestic longhair. remembers the outside world but only sometimes desires to return to it.



eniko
@eniko

I know I was supposed to talk about memory allocators but I'm feeling self indulgent so instead I'm going to talk about the C strings API that I created for my fantasy console that runs on a Motorola 68000 CPU with Mega68k as a working title.

Because, you see, C strings suck. In fact C strings don't even exist. They're just like, a gentleman's agreement to think of things which aren't strings as strings. And that sucks. They're also unsafe af (any time you hear about a buffer over or underrun security issue it's basically always C strings that are the culprit) but that's not even my concern. They just feel bad to work with. C strings have bad vibes.

Part of that is that C has no automatic memory management. Strings can't refer to anything else so they're actually perfect candidates for reference counting, but C doesn't have anything like C++'s shared_ptr<T>... or does it? GCC and Clang actually have the cleanup attribute. Used like so:

void destroy_int(int* value) {
    printf("destroying int %d\n", *value);
}

void foo(void) {
    printf("before scope\n");
    {
        int __attribute__ ((cleanup(destroy_int))) i = 12;
        printf("i = %d\n", i);
    }
    printf("after scope\n");
}

// output:
// before scope
// i = 12
// destroying int 12
// after scope

This means we can "clean up" a named variable once it goes out of scope, which brings us 80% of the way to automatic memory management. And that, my good reader, is where the fuckery starts in earnest.