mailing list of musl libc
 help / color / mirror / code / Atom feed
From: zxuiji <gb2985@gmail.com>
To: musl@lists.openwall.com
Subject: [musl] C89 Compatible techniques for emulating certain C++ behaviours
Date: Sat, 1 Jul 2023 08:43:23 +0100	[thread overview]
Message-ID: <CAOZ3c1pEKEKbLJqxuDFtPBQkwDHNi8_yrxOTeQF3vRvoS++SAA@mail.gmail.com> (raw)

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;

                 reply	other threads:[~2023-07-01  7:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAOZ3c1pEKEKbLJqxuDFtPBQkwDHNi8_yrxOTeQF3vRvoS++SAA@mail.gmail.com \
    --to=gb2985@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).