Reduce code size of mandatory runtime

This change reduces o/tiny/examples/life from 44kb to 24kb in size since
it avoids linking mmap() when unnecessary. This is important, to helping
cosmo not completely lose touch with its roots.
This commit is contained in:
Justine Tunney 2024-07-04 02:50:20 -07:00
parent fdab49b30e
commit 15ea0524b3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
17 changed files with 164 additions and 163 deletions

View file

@ -30,10 +30,8 @@
*/
struct AuxiliaryValue __getauxval(unsigned long at) {
unsigned long *ap;
for (ap = __auxv; ap[0]; ap += 2) {
if (at == ap[0]) {
for (ap = __auxv; ap && ap[0]; ap += 2)
if (at == ap[0])
return (struct AuxiliaryValue){ap[1], true};
}
}
return (struct AuxiliaryValue){0, false};
}

View file

@ -143,7 +143,8 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
break;
if (!TokAtoi(&fdspec, &protocol))
break;
__ensurefds_unlocked(fd);
if (_weaken(__ensurefds_unlocked))
_weaken(__ensurefds_unlocked)(fd);
struct Fd *f = fds->p + fd;
if (f->handle && f->handle != -1 && f->handle != handle) {
CloseHandle(f->handle);

View file

@ -40,9 +40,8 @@ int gettid(void) {
int tid;
if (VERY_LIKELY(__tls_enabled && !__vforked)) {
tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_acquire);
if (VERY_LIKELY(tid > 0)) {
if (VERY_LIKELY(tid > 0))
return tid;
}
}
if (IsXnuSilicon()) {
return enosys(); // can only happen if we can't access thread local storage

View file

@ -31,35 +31,51 @@ __static_yoink("_init_maps");
struct Maps __maps;
void __maps_add(struct Map *map) {
dll_init(&map->elem);
dll_make_first(&__maps.used, &map->elem);
map->next = __maps.maps;
__maps.maps = map;
++__maps.count;
}
static void __maps_adder(struct Map *map, int pagesz) {
__maps.pages += ((map->size + pagesz - 1) & -pagesz) / pagesz;
__maps_add(map);
}
void __maps_stack(void *stackaddr, int pagesz, size_t stacksize, int stackprot,
intptr_t stackhand) {
__maps.stack.addr = stackaddr;
__maps.stack.size = stacksize;
__maps.stack.prot = stackprot;
__maps.stack.h = stackhand;
__maps_adder(&__maps.stack, pagesz);
}
void __maps_init(void) {
int pagesz = getauxval(AT_PAGESZ);
// record _start() stack mapping
if (!IsWindows()) {
struct AddrSize stack;
stack = __get_main_stack();
dll_init(&__maps.stack.elem);
__maps.stack.addr = stack.addr;
__maps.stack.size = stack.size;
__maps.stack.prot = (uintptr_t)ape_stack_prot;
__maps_insert(&__maps.stack);
__maps_stack(stack.addr, pagesz, stack.size, (uintptr_t)ape_stack_prot, 0);
}
// record .text and .data mappings
static struct Map text, data;
dll_init(&text.elem);
text.addr = (char *)__executable_start;
text.size = _etext - __executable_start;
text.prot = PROT_READ | PROT_EXEC;
int pagesz = getauxval(AT_PAGESZ);
uintptr_t ds = ((uintptr_t)_etext + pagesz - 1) & -pagesz;
if (ds < (uintptr_t)_end) {
dll_init(&data.elem);
data.addr = (char *)ds;
data.size = (uintptr_t)_end - ds;
data.prot = PROT_READ | PROT_WRITE;
__maps_insert(&data);
__maps_adder(&data, pagesz);
}
__maps_insert(&text);
__maps_adder(&text, pagesz);
}
privileged void __maps_lock(void) {

View file

@ -43,11 +43,13 @@ void __maps_init(void);
void __maps_lock(void);
void __maps_check(void);
void __maps_unlock(void);
void __maps_add(struct Map *);
struct Map *__maps_alloc(void);
void __maps_free(struct Map *);
void __maps_insert(struct Map *);
int __munmap(char *, size_t, bool);
void *__mmap(char *, size_t, int, int, int, int64_t);
void __maps_stack(void *, int, size_t, int, intptr_t);
struct AddrSize __get_main_stack(void);
COSMOPOLITAN_C_END_

View file

@ -144,10 +144,7 @@ void __maps_insert(struct Map *map) {
dll_make_first(&__maps.used, &last->elem);
__maps_free(map);
} else {
dll_make_first(&__maps.used, &map->elem);
map->next = __maps.maps;
__maps.maps = map;
++__maps.count;
__maps_add(map);
}
__maps_check();
}

View file

@ -18,16 +18,12 @@
*/
#include "libc/thread/posixthread.internal.h"
pthread_spinlock_t _pthread_lock_obj;
void _pthread_init(void) {
(void)pthread_spin_init(&_pthread_lock_obj, 0);
}
pthread_mutex_t _pthread_lock_obj = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
void _pthread_lock(void) {
pthread_spin_lock(&_pthread_lock_obj);
pthread_mutex_lock(&_pthread_lock_obj);
}
void _pthread_unlock(void) {
pthread_spin_unlock(&_pthread_lock_obj);
pthread_mutex_unlock(&_pthread_lock_obj);
}