mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] C89 Compatible techniques for emulating certain C++ behaviours
@ 2023-07-01  7:43 zxuiji
  0 siblings, 0 replies; only message in thread
From: zxuiji @ 2023-07-01  7:43 UTC (permalink / raw)
  To: musl

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;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-01  7:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-01  7:43 [musl] C89 Compatible techniques for emulating certain C++ behaviours zxuiji

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).