Skip to content

Commit

Permalink
use complex initializer in dosio.c
Browse files Browse the repository at this point in the history
Sometimes the constant initializer doesn't suit for swappable context.
Add support for complex initializers and use in dosio.c.

Should fix crash with pcmos build.
See dosemu2/dosemu2#2291
  • Loading branch information
stsp committed Oct 16, 2024
1 parent f5abdf6 commit ceb0c5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
7 changes: 6 additions & 1 deletion include/libc/djctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ void djregister_ctx_hooks(void (*)(int), void (*)(void), void (*)(void),

#define SW_CTX_MAX 100

#define DJ64_DEFINE_SWAPPABLE_CONTEXT2(t, c, init, pre, post) \
#define DJ64_DEFINE_SWAPPABLE_CONTEXT3(t, c, init, pre, post, cinit) \
static struct t t##_contexts[SW_CTX_MAX]; \
static void t##_init(int idx) \
{ \
struct t *x = &t##_contexts[idx]; \
assert(c != x); \
*x = init; \
cinit(x); \
} \
static void t##_deinit(void) \
{ \
Expand All @@ -56,6 +57,10 @@ static void static_##t##_init(void) \
djregister_ctx_hooks(t##_init, t##_deinit, t##_save, t##_restore); \
}

#define DJ64_DEFINE_SWAPPABLE_CONTEXT2(t, c, init, pre, post) \
static void _dummy_cinit(struct t *dummy) {} \
DJ64_DEFINE_SWAPPABLE_CONTEXT3(t, c, init, pre, post, _dummy_cinit)

#define DJ64_DEFINE_SWAPPABLE_CONTEXT(t, c) \
static const struct t ctx_##t##_init = {}; \
DJ64_DEFINE_SWAPPABLE_CONTEXT2(t, c, ctx_##t##_init,,)
Expand Down
25 changes: 14 additions & 11 deletions src/libc/dos/io/dosio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
#include <libc/bss.h>
#include <libc/djctx.h>

static const char init_fh[] = {
O_TEXT,
O_TEXT,
O_TEXT,
O_BINARY,
O_BINARY
};
#define init_fh { \
O_TEXT, \
O_TEXT, \
O_TEXT, \
O_BINARY, \
O_BINARY, \
}

struct fh_state {
char init_file_handle_modes[20];
Expand All @@ -32,6 +32,7 @@ char *__file_handle_modes;

static const struct fh_state fhinit =
{
.init_file_handle_modes = init_fh,
.dosio_bss_count = -1,
.count = 20,
};
Expand All @@ -46,8 +47,12 @@ static void fhs_post(void)
#define _RS(x) x = fhs->x
_RS(__file_handle_modes);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(fh_state, fhs, fhinit,
fhs_pre(), fhs_post());
static void fhs_init(struct fh_state *st)
{
st->__file_handle_modes = st->init_file_handle_modes;
}
DJ64_DEFINE_SWAPPABLE_CONTEXT3(fh_state, fhs, fhinit,
fhs_pre(), fhs_post(), fhs_init);

#define init_file_handle_modes fhs->init_file_handle_modes
#define dosio_bss_count fhs->dosio_bss_count
Expand All @@ -62,8 +67,6 @@ __file_handle_set(int fd, int mode)
{
dosio_bss_count = __bss_count;
count = 20;
memcpy(init_file_handle_modes, init_fh, sizeof(init_fh));
__file_handle_modes = init_file_handle_modes;
}

/* Check for bogus values */
Expand Down

0 comments on commit ceb0c5d

Please sign in to comment.