Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAOZ3c1pEKEKbLJqxuDFtPBQkwDHNi8_yrxOTeQF3vRvoS++SAA@mail.gmail.com>
Date: Sat, 1 Jul 2023 08:43:23 +0100
From: zxuiji <gb2985@...il.com>
To: musl@...ts.openwall.com
Subject: C89 Compatible techniques for emulating certain C++ behaviours

1. template<A,B,...>
x.h:
#define A
#define B
#include "y.h"
y.h:
/* Template, expects A, B, ... */
...
#undef A
#undef B
2. Inheritance
/* x.h */
#define T void
typedef struct
{
#include "struct_buffer_members.h"
...
};
struct_buffer_members.h
size_t pad; /* Node size, stored to avoid duplicate functions */
size_t cap; /* Total size of the buffer */
uint len;
uint pos;
T *top;
#undef T
3. __INCLUDE_LEVEL__ can be used to create a preprocessor for loop
x.h:
#define END 10
#define INC y.h
#include <for.h>
for.h:
#ifndef BEGAN
#define BEGAN __INCLUDE_LEVEL__
#endif
/* Can't concate this but it's still better than nothing */
#define I (BEGAN - __INCLUDE_LEVEL)
#if I < END
#include INC
#undef I
#include __FILE__
#endif
#undef BEGAN

>From here it's more techniques that can be used that you may not be aware of:
1. Thread syncronisation:
linked lists, lock must ALWAYS be held by a thread, initialiser gets
it 1st. Lock holder is responsible for passing onto another thread,
threads just randomly write to a volatile member their ID or the
pointer to the link they allocated and expect to be linked, they
simply check the "next" link is not equal to itself after each yield
they make.

2. setlongjmp etc can be used to emulate threads where none are
available, caviets exist however, can possibly create a pre-existing
stack copy prior to main() to get
default state of globals when __thread_local etc are unavailable

3. Linked lists can have the benefits of arrays
typedef struct _link { int i; struct _link *prev_offset, *next_offset; }
typedef struct
{
    link *head_offset, *head_removed_offset, *root;
    struct_buffer _links, _data
} link_list_struct;
...
/* Examples of offset usage */
link->next = next - link;
link->prev = link - prev;
link += link->next;

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.