Bring back gc() function

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.
This commit is contained in:
Justine Tunney 2024-01-08 10:07:35 -08:00
parent 6cb0354e19
commit a4b455185b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
280 changed files with 1362 additions and 1407 deletions

View file

@ -95,7 +95,7 @@ static int LuaFetch(lua_State *L) {
return LuaNilError(L, "invalid header name: %s", key);
val = lua_tolstring(L, -1, &vallen);
if (!(hdr = _gc(EncodeHttpHeaderValue(val, vallen, 0))))
if (!(hdr = gc(EncodeHttpHeaderValue(val, vallen, 0))))
return LuaNilError(L, "invalid header %s value encoding", key);
// Content-Length will be overwritten; skip it to avoid duplicates;
@ -133,14 +133,14 @@ static int LuaFetch(lua_State *L) {
if (bodylen > 0 || !(methodidx == kHttpGet || methodidx == kHttpHead ||
methodidx == kHttpTrace || methodidx == kHttpDelete ||
methodidx == kHttpConnect)) {
conlenhdr = _gc(xasprintf("Content-Length: %zu\r\n", bodylen));
conlenhdr = gc(xasprintf("Content-Length: %zu\r\n", bodylen));
}
/*
* Parse URL.
*/
_gc(ParseUrl(urlarg, urlarglen, &url, true));
_gc(url.params.p);
gc(ParseUrl(urlarg, urlarglen, &url, true));
gc(url.params.p);
usingssl = false;
if (url.scheme.n) {
#ifndef UNSECURE
@ -160,9 +160,9 @@ static int LuaFetch(lua_State *L) {
#endif
if (url.host.n) {
host = _gc(strndup(url.host.p, url.host.n));
host = gc(strndup(url.host.p, url.host.n));
if (url.port.n) {
port = _gc(strndup(url.port.p, url.port.n));
port = gc(strndup(url.port.p, url.port.n));
#ifndef UNSECURE
} else if (usingssl) {
port = "443";
@ -179,7 +179,7 @@ static int LuaFetch(lua_State *L) {
if (!IsAcceptableHost(host, -1)) {
return LuaNilError(L, "invalid host");
}
if (!hosthdr) hosthdr = _gc(xasprintf("%s:%s", host, port));
if (!hosthdr) hosthdr = gc(xasprintf("%s:%s", host, port));
// check if hosthdr is in keepalive table
if (keepalive && lua_istable(L, 2)) {
@ -207,7 +207,7 @@ static int LuaFetch(lua_State *L) {
url.host.p = 0, url.host.n = 0;
url.port.p = 0, url.port.n = 0;
if (!url.path.n || url.path.p[0] != '/') {
void *p = _gc(xmalloc(1 + url.path.n));
void *p = gc(xmalloc(1 + url.path.n));
mempcpy(mempcpy(p, "/", 1), url.path.p, url.path.n);
url.path.p = p;
++url.path.n;
@ -224,14 +224,14 @@ static int LuaFetch(lua_State *L) {
"User-Agent: %s\r\n"
"%s%s"
"\r\n",
method, _gc(EncodeUrl(&url, 0)), hosthdr,
method, gc(EncodeUrl(&url, 0)), hosthdr,
(keepalive == kaNONE || keepalive == kaCLOSE)
? "close"
: (connhdr ? connhdr : "keep-alive"),
agenthdr, conlenhdr, headers ? headers : "");
appendd(&request, body, bodylen);
requestlen = appendz(request).i;
_gc(request);
gc(request);
if (keepalive == kaNONE || keepalive == kaOPEN) {
/*
@ -274,7 +274,7 @@ static int LuaFetch(lua_State *L) {
if (!evadedragnetsurveillance) {
mbedtls_ssl_set_hostname(&sslcli, host);
}
bio = _gc(malloc(sizeof(struct TlsBio)));
bio = gc(malloc(sizeof(struct TlsBio)));
bio->fd = sock;
bio->a = 0;
bio->b = 0;
@ -524,7 +524,7 @@ VerifyFailed:
LockInc(&shared->c.sslverifyfailed);
close(sock);
return LuaNilTlsError(
L, _gc(DescribeSslVerifyFailure(sslcli.session_negotiate->verify_result)),
L, gc(DescribeSslVerifyFailure(sslcli.session_negotiate->verify_result)),
ret);
#endif
#undef ssl