Skip to content

Commit

Permalink
Bring back gc() function
Browse files Browse the repository at this point in the history
Renaming gc() to _gc() was a mistake since the better thing to do is put
it behind the _COSMO_SOURCE macro. We need this change because I haven't
wanted to use my amazing garbage collector ever since we renamed it. You
now need to define _COSMO_SOURCE yourself when using amalgamation header
and cosmocc users need to pass the -mcosmo flag to get the gc() function

Some other issues relating to cancelation have been fixed along the way.
We're also now putting cosmocc in a folder named `.cosmocc` so it can be
more safely excluded by grep --exclude-dir=.cosmocc --exclude-dir=o etc.
  • Loading branch information
jart committed Jan 8, 2024
1 parent 6cb0354 commit a4b4551
Show file tree
Hide file tree
Showing 280 changed files with 1,362 additions and 1,407 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- conf -*-

/o
/cosmocc
/.prompt.jtlp
/.cosmocc

# TODO: Find some way to have Python write to o/
__pycache__
Expand Down
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export MODE
export SOURCE_DATE_EPOCH
export TMPDIR

COSMOCC = cosmocc/3.2
COSMOCC = .cosmocc/3.2
TOOLCHAIN = $(COSMOCC)/bin/$(ARCH)-linux-cosmo-
DOWNLOAD := $(shell build/download-cosmocc.sh $(COSMOCC) 3.2 28b48682595f0f46b45ab381118cdffdabc8fcfa29aa54e301fe6ffe35269f5e)

Expand Down Expand Up @@ -543,9 +543,6 @@ o/cosmopolitan.h: o/cosmopolitan.h.txt \
$(wildcard libc/integral/*) \
$(foreach x,$(COSMOPOLITAN_H_PKGS),$($(x)_HDRS)) \
$(foreach x,$(COSMOPOLITAN_H_PKGS),$($(x)_INCS))
@$(ECHO) '#ifndef __STRICT_ANSI__' >$@
@$(ECHO) '#define _COSMO_SOURCE' >>$@
@$(ECHO) '#endif' >>$@
@$(COMPILE) -AROLLUP -T$@ build/bootstrap/rollup.com @$< >>$@

o/cosmopolitan.html: private .UNSANDBOXED = 1
Expand Down
2 changes: 0 additions & 2 deletions build/definitions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ DEFAULT_CFLAGS = \
-std=gnu2x

DEFAULT_CXXFLAGS = \
-fno-rtti \
-fno-exceptions \
-fuse-cxa-atexit \
-Wno-int-in-bool-context \
-Wno-narrowing \
Expand Down
16 changes: 8 additions & 8 deletions build/objdump
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ if [ -n "$OBJDUMP" ]; then
fi

find_objdump() {
if [ -x cosmocc/3.2/bin/$1-linux-cosmo-objdump ]; then
OBJDUMP=cosmocc/3.2/bin/$1-linux-cosmo-objdump
elif [ -x cosmocc/3.2/bin/$1-linux-musl-objdump ]; then
OBJDUMP=cosmocc/3.2/bin/$1-linux-musl-objdump
elif [ -x "$COSMO/cosmocc/3.2/bin/$1-linux-cosmo-objdump" ]; then
OBJDUMP="$COSMO/cosmocc/3.2/bin/$1-linux-cosmo-objdump"
elif [ -x "$COSMO/cosmocc/3.2/bin/$1-linux-musl-objdump" ]; then
OBJDUMP="$COSMO/cosmocc/3.2/bin/$1-linux-musl-objdump"
if [ -x .cosmocc/3.2/bin/$1-linux-cosmo-objdump ]; then
OBJDUMP=.cosmocc/3.2/bin/$1-linux-cosmo-objdump
elif [ -x .cosmocc/3.2/bin/$1-linux-musl-objdump ]; then
OBJDUMP=.cosmocc/3.2/bin/$1-linux-musl-objdump
elif [ -x "$COSMO/.cosmocc/3.2/bin/$1-linux-cosmo-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.2/bin/$1-linux-cosmo-objdump"
elif [ -x "$COSMO/.cosmocc/3.2/bin/$1-linux-musl-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.2/bin/$1-linux-musl-objdump"
else
echo "error: toolchain not found (try running 'cosmocc --update' or 'make' in the cosmo monorepo)" >&2
exit 1
Expand Down
2 changes: 1 addition & 1 deletion dsp/scale/gyarados.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
Expand Down
2 changes: 1 addition & 1 deletion dsp/tty/sendtitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "dsp/tty/tty.h"
#include "libc/mem/arraylist2.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/x/x.h"

Expand Down
2 changes: 1 addition & 1 deletion dsp/tty/ttyraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "libc/calls/ucontext.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
Expand Down
4 changes: 2 additions & 2 deletions examples/greenbean.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ int main(int argc, char *argv[]) {
// print all the ips that 0.0.0.0 would bind
// Cosmo's GetHostIps() API is much easier than ioctl(SIOCGIFCONF)
uint32_t *hostips;
for (hostips = _gc(GetHostIps()), i = 0; hostips[i]; ++i) {
for (hostips = gc(GetHostIps()), i = 0; hostips[i]; ++i) {
kprintf("listening on http://%hhu.%hhu.%hhu.%hhu:%hu\n", hostips[i] >> 24,
hostips[i] >> 16, hostips[i] >> 8, hostips[i], PORT);
}
Expand Down Expand Up @@ -339,7 +339,7 @@ int main(int argc, char *argv[]) {
unassert(!pthread_attr_setguardsize(&attr, pagesz));
unassert(!pthread_attr_setsigmask_np(&attr, &block));
unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0));
pthread_t *th = _gc(calloc(threads, sizeof(pthread_t)));
pthread_t *th = gc(calloc(threads, sizeof(pthread_t)));
for (i = 0; i < threads; ++i) {
int rc;
++a_workers;
Expand Down
2 changes: 1 addition & 1 deletion examples/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void List(const char *path) {
if (strcmp(path, ".") == 0) {
vpath = "";
} else if (!endswith(path, "/")) {
vpath = _gc(xasprintf("%s/", path));
vpath = gc(xasprintf("%s/", path));
} else {
vpath = path;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ void PrintFileMetadata(const char *pathname, struct stat *st) {
DescribeFileType(st->st_mode), "owner id", st->st_uid, "group id",
st->st_gid, "flags", st->st_flags, "gen", st->st_gen,
"device id (if special)", st->st_rdev, "block size", st->st_blksize,
"access time", _gc(xiso8601(&st->st_atim)), "modified time",
_gc(xiso8601(&st->st_mtim)), "c[omplicated]time",
_gc(xiso8601(&st->st_ctim)), "birthtime",
_gc(xiso8601(&st->st_birthtim)));
"access time", gc(xiso8601(&st->st_atim)), "modified time",
gc(xiso8601(&st->st_mtim)), "c[omplicated]time",
gc(xiso8601(&st->st_ctim)), "birthtime",
gc(xiso8601(&st->st_birthtim)));
}

int main(int argc, char *argv[]) {
Expand Down
2 changes: 1 addition & 1 deletion examples/unbourne.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/mem/alloca.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/paths.h"
#include "libc/runtime/runtime.h"
Expand Down
2 changes: 2 additions & 0 deletions libc/calls/getrandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ static ssize_t GetDevUrandom(char *p, size_t n) {
int fd;
ssize_t rc;
BLOCK_SIGNALS;
BLOCK_CANCELATION;
fd = sys_openat(AT_FDCWD, "/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
if (fd != -1) {
rc = sys_read(fd, p, n);
} else {
rc = -1;
}
ALLOW_CANCELATION;
ALLOW_SIGNALS;
return rc;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/tmpfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int _mkstemp(char *, int);
* On newer Linux only (c. 2013) it's possible to turn the anonymous
* returned file back into a real file, by doing this:
*
* linkat(AT_FDCWD, _gc(xasprintf("/proc/self/fd/%d", fd)),
* linkat(AT_FDCWD, gc(xasprintf("/proc/self/fd/%d", fd)),
* AT_FDCWD, "real.txt", AT_SYMLINK_FOLLOW)
*
* On the New Technology, temporary files created by this function
Expand Down
3 changes: 3 additions & 0 deletions libc/dlopen/dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/struct/stat.h"
Expand Down Expand Up @@ -798,6 +799,7 @@ static void *dlopen_silicon(const char *path, int mode) {
void *cosmo_dlopen(const char *path, int mode) {
void *res;
BLOCK_SIGNALS;
BLOCK_CANCELATION;
if (IsWindows()) {
res = dlopen_nt(path, mode);
} else if (IsXnuSilicon()) {
Expand All @@ -814,6 +816,7 @@ void *cosmo_dlopen(const char *path, int mode) {
} else {
res = 0;
}
ALLOW_CANCELATION;
ALLOW_SIGNALS;
STRACE("dlopen(%#s, %d) → %p% m", path, mode, res);
return res;
Expand Down
13 changes: 13 additions & 0 deletions libc/intrin/stracef.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dce.h"
#include "libc/intrin/describebacktrace.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
Expand All @@ -27,3 +29,14 @@ dontinstrument void __stracef(const char *fmt, ...) {
kvprintf(fmt, v);
va_end(v);
}

#if IsModeDbg()
void report_cancellation_point(void) {
kprintf("error: cancelable raw system call not annotated in wrapper\n"
"choice #1 use BLOCK_CANCELATION / ALLOW_CANCELATION\n"
"choice #2 use BEGIN_CANCELATION_POINT / END_CANCELATION_POINT\n"
"backtrace: %s\n",
DescribeBacktrace(__builtin_frame_address(0)));
__builtin_trap();
}
#endif
40 changes: 20 additions & 20 deletions libc/log/libfatal.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include "libc/macros.internal.h"
COSMOPOLITAN_C_START_

__funline unsigned long __strlen(const char *s) {
forceinline unsigned long __strlen(const char *s) {
unsigned long n = 0;
while (*s++) ++n;
return n;
}

__funline int __strcmp(const char *l, const char *r) {
forceinline int __strcmp(const char *l, const char *r) {
size_t i = 0;
while (l[i] == r[i] && r[i]) ++i;
return (l[i] & 255) - (r[i] & 255);
}

__funline char *__stpcpy(char *d, const char *s) {
forceinline char *__stpcpy(char *d, const char *s) {
size_t i;
for (i = 0;; ++i) {
if (!(d[i] = s[i])) {
Expand All @@ -24,7 +24,7 @@ __funline char *__stpcpy(char *d, const char *s) {
}
}

__funline void *__repstosb(void *di, char al, size_t cx) {
forceinline void *__repstosb(void *di, char al, size_t cx) {
#if defined(__x86__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm("rep stosb"
: "=D"(di), "=c"(cx), "=m"(*(char(*)[cx])di)
Expand All @@ -40,7 +40,7 @@ __funline void *__repstosb(void *di, char al, size_t cx) {
#endif
}

__funline void *__repmovsb(void *di, const void *si, size_t cx) {
forceinline void *__repmovsb(void *di, const void *si, size_t cx) {
#if defined(__x86__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm("rep movsb"
: "=D"(di), "=S"(si), "=c"(cx), "=m"(*(char(*)[cx])di)
Expand All @@ -57,7 +57,7 @@ __funline void *__repmovsb(void *di, const void *si, size_t cx) {
#endif
}

__funline void *__mempcpy(void *d, const void *s, size_t n) {
forceinline void *__mempcpy(void *d, const void *s, size_t n) {
size_t i;
for (i = 0; i < n; ++i) {
((char *)d)[i] = ((const char *)s)[i];
Expand All @@ -66,7 +66,7 @@ __funline void *__mempcpy(void *d, const void *s, size_t n) {
return (char *)d + n;
}

__funline char *__uintcpy(char p[hasatleast 21], uint64_t x) {
forceinline char *__uintcpy(char p[hasatleast 21], uint64_t x) {
char t;
size_t i, a, b;
i = 0;
Expand All @@ -85,22 +85,22 @@ __funline char *__uintcpy(char p[hasatleast 21], uint64_t x) {
return p + i;
}

__funline char *__intcpy(char p[hasatleast 21], int64_t x) {
forceinline char *__intcpy(char p[hasatleast 21], int64_t x) {
if (x < 0) *p++ = '-', x = -(uint64_t)x;
return __uintcpy(p, x);
}

__funline char *__fixcpy(char p[hasatleast 17], uint64_t x, uint8_t k) {
forceinline char *__fixcpy(char p[hasatleast 17], uint64_t x, uint8_t k) {
while (k > 0) *p++ = "0123456789abcdef"[(x >> (k -= 4)) & 15];
*p = '\0';
return p;
}

__funline char *__hexcpy(char p[hasatleast 17], uint64_t x) {
forceinline char *__hexcpy(char p[hasatleast 17], uint64_t x) {
return __fixcpy(p, x, ROUNDUP(x ? (__builtin_clzll(x) ^ 63) + 1 : 1, 4));
}

__funline const void *__memchr(const void *s, unsigned char c, size_t n) {
forceinline const void *__memchr(const void *s, unsigned char c, size_t n) {
size_t i;
for (i = 0; i < n; ++i) {
if (((const unsigned char *)s)[i] == c) {
Expand All @@ -110,7 +110,7 @@ __funline const void *__memchr(const void *s, unsigned char c, size_t n) {
return 0;
}

__funline char *__strstr(const char *haystack, const char *needle) {
forceinline char *__strstr(const char *haystack, const char *needle) {
size_t i;
for (;;) {
for (i = 0;; ++i) {
Expand All @@ -123,8 +123,8 @@ __funline char *__strstr(const char *haystack, const char *needle) {
return 0;
}

__funline char16_t *__strstr16(const char16_t *haystack,
const char16_t *needle) {
forceinline char16_t *__strstr16(const char16_t *haystack,
const char16_t *needle) {
size_t i;
for (;;) {
for (i = 0;; ++i) {
Expand All @@ -137,21 +137,21 @@ __funline char16_t *__strstr16(const char16_t *haystack,
return 0;
}

__funline const char *__strchr(const char *s, unsigned char c) {
forceinline const char *__strchr(const char *s, unsigned char c) {
for (;; ++s) {
if ((*s & 255) == c) return s;
if (!*s) return 0;
}
}

__funline unsigned long __atoul(const char *p) {
forceinline unsigned long __atoul(const char *p) {
int c;
unsigned long x = 0;
while ('0' <= (c = *p++) && c <= '9') x *= 10, x += c - '0';
return x;
}

__funline long __atol(const char *p) {
forceinline long __atol(const char *p) {
int s = *p;
unsigned long x;
if (s == '-' || s == '+') ++p;
Expand All @@ -160,7 +160,7 @@ __funline long __atol(const char *p) {
return x;
}

__funline void *__memset(void *a, int c, unsigned long n) {
forceinline void *__memset(void *a, int c, unsigned long n) {
char *d = a;
unsigned long i;
for (i = 0; i < n; ++i) {
Expand All @@ -170,7 +170,7 @@ __funline void *__memset(void *a, int c, unsigned long n) {
return d;
}

__funline void *__memcpy(void *a, const void *b, unsigned long n) {
forceinline void *__memcpy(void *a, const void *b, unsigned long n) {
char *d = a;
unsigned long i;
const char *s = b;
Expand All @@ -181,7 +181,7 @@ __funline void *__memcpy(void *a, const void *b, unsigned long n) {
return d;
}

__funline void *__memmove(void *a, const void *b, unsigned long n) {
forceinline void *__memmove(void *a, const void *b, unsigned long n) {
char *d = a;
unsigned long i;
const char *s = b;
Expand Down
2 changes: 2 additions & 0 deletions libc/log/vflogf.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
flockfile(f);
strace_enabled(-1);
BLOCK_SIGNALS;
BLOCK_CANCELATION;

// We display TIMESTAMP.MICROS normally. However, when we log multiple
// times in the same second, we display TIMESTAMP+DELTAMICROS instead.
Expand Down Expand Up @@ -136,6 +137,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
_Exit(22);
}

ALLOW_CANCELATION;
ALLOW_SIGNALS;
strace_enabled(+1);
funlockfile(f);
Expand Down
Loading

0 comments on commit a4b4551

Please sign in to comment.