1
0
Fork 0

tccrun: resign from "advanced" system calls (memaligh/gettid)

... let's stay compatible
This commit is contained in:
grischka 2024-02-21 10:27:32 +01:00
parent d2f8ceac7a
commit 4e91d38ddc
4 changed files with 5 additions and 49 deletions

View File

@ -242,7 +242,7 @@ static void *(*reallocator)(void*, unsigned long) = default_reallocator;
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *realloc) LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *realloc)
{ {
reallocator = realloc; reallocator = realloc ? realloc : default_reallocator;
} }
/* in case MEM_DEBUG is #defined */ /* in case MEM_DEBUG is #defined */

View File

@ -10,13 +10,11 @@ extern "C" {
#endif #endif
/*****************************/ /*****************************/
/* set custom allocator for all allocations (optional) */ /* set custom allocator for all allocations (optional), NULL for default. */
typedef void *TCCReallocFunc(void *ptr, unsigned long size); typedef void *TCCReallocFunc(void *ptr, unsigned long size);
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc); LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc);
/*****************************/ /*****************************/
typedef struct TCCState TCCState; typedef struct TCCState TCCState;
/* create a new TCC compilation context */ /* create a new TCC compilation context */

1
tcc.h
View File

@ -1005,7 +1005,6 @@ struct TCCState {
void *run_lj, *run_jb; /* sj/lj for tcc_setjmp()/tcc_run() */ void *run_lj, *run_jb; /* sj/lj for tcc_setjmp()/tcc_run() */
TCCBtFunc *bt_func; TCCBtFunc *bt_func;
void *bt_data; void *bt_data;
int run_tid;
#endif #endif
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE

View File

@ -127,15 +127,7 @@ static int rt_mem(TCCState *s1, int size)
ptr_diff = (char*)prw - (char*)ptr; /* = size; */ ptr_diff = (char*)prw - (char*)ptr; /* = size; */
//printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff); //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);
#else #else
# if !CONFIG_RUNMEM_ALIGNED ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */
ptr = tcc_malloc(size += PAGESIZE);
# elif _WIN32
ptr = _aligned_malloc(size, PAGESIZE);
# else
ptr = memalign(PAGESIZE, size);
# endif
if (NULL == ptr)
return tcc_error_noabort("tccrun: could not allocate memory");
#endif #endif
s1->run_ptr = ptr; s1->run_ptr = ptr;
s1->run_size = size; s1->run_size = size;
@ -200,13 +192,7 @@ ST_FUNC void tcc_run_free(TCCState *s1)
# ifdef _WIN64 # ifdef _WIN64
win64_del_function_table(s1->run_function_table); win64_del_function_table(s1->run_function_table);
# endif # endif
# if !CONFIG_RUNMEM_ALIGNED
tcc_free(ptr); tcc_free(ptr);
# elif _WIN32
_aligned_free(ptr);
# else
libc_free(ptr);
# endif
#endif #endif
} }
@ -300,7 +286,7 @@ static void cleanup_sections(TCCState *s1)
/* 0 = .text rwx other rw */ /* 0 = .text rwx other rw */
/* 1 = .text rx .rdata r .data/.bss rw */ /* 1 = .text rx .rdata r .data/.bss rw */
#ifndef CONFIG_RUNMEM_RO #ifndef CONFIG_RUNMEM_RO
# define CONFIG_RUNMEM_RO 1 # define CONFIG_RUNMEM_RO 0
#endif #endif
/* relocate code. Return -1 on error, required size if ptr is NULL, /* relocate code. Return -1 on error, required size if ptr is NULL,
@ -377,7 +363,7 @@ redo:
align = 64; align = 64;
#endif #endif
/* start new page for different permissions */ /* start new page for different permissions */
if (CONFIG_RUNMEM_RO || k < 2) if (CONFIG_RUNMEM_RO || k == 0)
align = PAGESIZE; align = PAGESIZE;
} }
s->sh_addralign = align; s->sh_addralign = align;
@ -556,26 +542,8 @@ static void st_unlink(TCCState *s1)
rt_post_sem(); rt_post_sem();
} }
#ifdef _WIN32
# define GETTID() GetCurrentThreadId()
#elif defined __linux__
# define GETTID() gettid()
#elif 0
# define GETTID() 1234 // threads not supported
#endif
LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *p_jmp_buf, void *func, void *p_longjmp) LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *p_jmp_buf, void *func, void *p_longjmp)
{ {
#ifdef GETTID
int tid = GETTID();
TCCState *s;
rt_wait_sem();
for (s = g_s1; s; s = s->next)
if (s->run_tid == tid)
s->run_tid = -1;
s1->run_tid = (int)tid;
rt_post_sem();
#endif
s1->run_lj = p_longjmp; s1->run_lj = p_longjmp;
s1->run_jb = p_jmp_buf; s1->run_jb = p_jmp_buf;
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE
@ -593,14 +561,6 @@ LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void *data, TCCBtFunc *func)
static TCCState *rt_find_state(rt_frame *f) static TCCState *rt_find_state(rt_frame *f)
{ {
#ifdef GETTID
int tid = GETTID();
TCCState *s;
for (s = g_s1; s; s = s->next)
if (s->run_tid == tid)
break;
return s;
#else
TCCState *s; TCCState *s;
int level; int level;
addr_t pc; addr_t pc;
@ -620,7 +580,6 @@ static TCCState *rt_find_state(rt_frame *f)
} }
} }
return NULL; return NULL;
#endif
} }
static void rt_exit(rt_frame *f, int code) static void rt_exit(rt_frame *f, int code)