Replace COSMO define with _COSMO_SOURCE

This change might cause ABI breakages for /opt/cosmos. It's needed to
help us better conform to header declaration practices.
This commit is contained in:
Justine Tunney 2023-08-13 20:31:27 -07:00
parent a033b65a33
commit c776a32f75
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
238 changed files with 858 additions and 1069 deletions

View file

@ -481,11 +481,11 @@ static void ReadFlags(struct As *a, int argc, char *argv[]) {
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-o")) {
a->outpath = StrDup(a, argv[++i]);
} else if (_startswith(argv[i], "-o")) {
} else if (startswith(argv[i], "-o")) {
a->outpath = StrDup(a, argv[i] + 2);
} else if (!strcmp(argv[i], "-I")) {
SaveString(&a->incpaths, strdup(argv[++i]));
} else if (_startswith(argv[i], "-I")) {
} else if (startswith(argv[i], "-I")) {
SaveString(&a->incpaths, strdup(argv[i] + 2));
} else if (!strcmp(argv[i], "-Z")) {
a->inhibiterr = true;
@ -1669,13 +1669,13 @@ static int GrabSection(struct As *a, int name, int flags, int type, int group,
static void OnSection(struct As *a, struct Slice s) {
int name, flags, type, group = -1, comdat = -1;
name = SliceDup(a, GetSlice(a));
if (_startswith(a->strings.p[name], ".text")) {
if (startswith(a->strings.p[name], ".text")) {
flags = SHF_ALLOC | SHF_EXECINSTR;
type = SHT_PROGBITS;
} else if (_startswith(a->strings.p[name], ".data")) {
} else if (startswith(a->strings.p[name], ".data")) {
flags = SHF_ALLOC | SHF_WRITE;
type = SHT_PROGBITS;
} else if (_startswith(a->strings.p[name], ".bss")) {
} else if (startswith(a->strings.p[name], ".bss")) {
flags = SHF_ALLOC | SHF_WRITE;
type = SHT_NOBITS;
} else {
@ -2609,8 +2609,8 @@ static bool HasXmmOnLine(struct As *a) {
int i;
for (i = 0; !IsPunct(a, a->i + i, ';'); ++i) {
if (IsSlice(a, a->i + i) && a->slices.p[a->things.p[a->i + i].i].n >= 4 &&
(_startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") ||
_startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) {
(startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") ||
startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) {
return true;
}
}

View file

@ -370,7 +370,7 @@ static Token *ParseAsmClobbers(Asm *a, Token *tok) {
a->flagclob = true;
} else if ((i = GetIndexOfRegisterName(s)) != -1) {
a->regclob |= 1 << i;
} else if (_startswith(s, "xmm") && isdigit(s[3]) &&
} else if (startswith(s, "xmm") && isdigit(s[3]) &&
(!s[4] || isdigit(s[4]))) {
i = s[3] - '0';
if (s[4]) {
@ -381,7 +381,7 @@ static Token *ParseAsmClobbers(Asm *a, Token *tok) {
a->xmmclob |= 1 << i;
} else if (!strcmp(s, "st")) {
a->x87clob |= 1;
} else if (_startswith(s, "st(") && isdigit(s[3]) && s[4] == ')') {
} else if (startswith(s, "st(") && isdigit(s[3]) && s[4] == ')') {
i = s[3] - '0';
i &= 7;
a->x87clob |= 1 << i;

View file

@ -218,7 +218,7 @@ static void parse_args(int argc, char **argv) {
atexit(PrintMemoryUsage);
} else if (!strcmp(argv[i], "-o")) {
opt_o = argv[++i];
} else if (_startswith(argv[i], "-o")) {
} else if (startswith(argv[i], "-o")) {
opt_o = argv[i] + 2;
} else if (!strcmp(argv[i], "-S")) {
opt_S = true;
@ -242,19 +242,19 @@ static void parse_args(int argc, char **argv) {
opt_P = true;
} else if (!strcmp(argv[i], "-I")) {
strarray_push(&include_paths, argv[++i]);
} else if (_startswith(argv[i], "-I")) {
} else if (startswith(argv[i], "-I")) {
strarray_push(&include_paths, argv[i] + 2);
} else if (!strcmp(argv[i], "-iquote")) {
strarray_push(&include_paths, argv[++i]);
} else if (_startswith(argv[i], "-iquote")) {
} else if (startswith(argv[i], "-iquote")) {
strarray_push(&include_paths, argv[i] + strlen("-iquote"));
} else if (!strcmp(argv[i], "-isystem")) {
strarray_push(&include_paths, argv[++i]);
} else if (_startswith(argv[i], "-isystem")) {
} else if (startswith(argv[i], "-isystem")) {
strarray_push(&include_paths, argv[i] + strlen("-isystem"));
} else if (!strcmp(argv[i], "-D")) {
define(argv[++i]);
} else if (_startswith(argv[i], "-D")) {
} else if (startswith(argv[i], "-D")) {
define(argv[i] + 2);
} else if (!strcmp(argv[i], "-U")) {
undef_macro(argv[++i]);
@ -266,9 +266,9 @@ static void parse_args(int argc, char **argv) {
opt_x = parse_opt_x(argv[++i]);
} else if (!strncmp(argv[i], "-x", 2)) {
opt_x = parse_opt_x(argv[i] + 2);
} else if (_startswith(argv[i], "-Wa")) {
} else if (startswith(argv[i], "-Wa")) {
strarray_push_comma(&as_extra_args, argv[i] + 3);
} else if (_startswith(argv[i], "-Wl")) {
} else if (startswith(argv[i], "-Wl")) {
strarray_push_comma(&ld_extra_args, argv[i] + 3);
} else if (!strcmp(argv[i], "-Xassembler")) {
strarray_push(&as_extra_args, argv[++i]);
@ -336,7 +336,7 @@ static void parse_args(int argc, char **argv) {
} else if (!strcmp(argv[i], "-L")) {
strarray_push(&ld_extra_args, "-L");
strarray_push(&ld_extra_args, argv[++i]);
} else if (_startswith(argv[i], "-L")) {
} else if (startswith(argv[i], "-L")) {
strarray_push(&ld_extra_args, "-L");
strarray_push(&ld_extra_args, argv[i] + 2);
} else {
@ -563,11 +563,11 @@ static Token *append_tokens(Token *tok1, Token *tok2) {
static FileType get_file_type(const char *filename) {
if (opt_x != FILE_NONE) return opt_x;
if (_endswith(filename, ".a")) return FILE_AR;
if (_endswith(filename, ".o")) return FILE_OBJ;
if (_endswith(filename, ".c")) return FILE_C;
if (_endswith(filename, ".s")) return FILE_ASM;
if (_endswith(filename, ".S")) return FILE_ASM_CPP;
if (endswith(filename, ".a")) return FILE_AR;
if (endswith(filename, ".o")) return FILE_OBJ;
if (endswith(filename, ".c")) return FILE_C;
if (endswith(filename, ".s")) return FILE_ASM;
if (endswith(filename, ".S")) return FILE_ASM_CPP;
error("<command line>: unknown file extension: %s", filename);
}

View file

@ -985,7 +985,7 @@ static bool gen_builtin_funcall(Node *node, const char *name) {
char regprefix;
gen_expr(node->args);
emitlin("\tor\t$-1,%edi");
regprefix = _endswith(name, "l") ? 'r' : 'e';
regprefix = endswith(name, "l") ? 'r' : 'e';
println("\tbsf\t%%%cax,%%%cax", regprefix, regprefix);
emitlin("\tcmovz\t%edi,%eax");
emitlin("\tinc\t%eax");
@ -1413,7 +1413,7 @@ void gen_expr(Node *node) {
case ND_FUNCALL: {
const char *funcname = NULL;
if (node->lhs->kind == ND_VAR) {
if (_startswith(nameof(node->lhs->var), "__builtin_")) {
if (startswith(nameof(node->lhs->var), "__builtin_")) {
funcname = nameof(node->lhs->var) + 10;
if (gen_builtin_funcall(node, funcname)) {
return;

View file

@ -285,23 +285,22 @@ static void LoadPublicDefinitions(struct DoxWriter *dox, Obj *prog) {
if (*obj->name == '_') continue;
if (strchr(obj->name, '$')) continue;
if (obj->visibility && !strcmp(obj->visibility, "hidden")) continue;
if (_startswith(obj->name, "nsync_") && _endswith(obj->name, "_"))
continue;
if (startswith(obj->name, "nsync_") && endswith(obj->name, "_")) continue;
if (!obj->is_definition && (!obj->is_function || !obj->params ||
!obj->params->name || !*obj->params->name)) {
continue;
}
}
if (_startswith(obj->name, "__gdtoa_")) continue;
if (_startswith(obj->name, "sys_")) continue;
if (_startswith(obj->name, "ioctl_")) continue;
if (_startswith(obj->name, "nsync_mu_semaphore_")) continue;
if (_startswith(obj->name, "Describe")) continue;
if (_startswith(obj->name, "__sig")) continue;
if (_startswith(obj->name, "__zipos")) continue;
if (startswith(obj->name, "__gdtoa_")) continue;
if (startswith(obj->name, "sys_")) continue;
if (startswith(obj->name, "ioctl_")) continue;
if (startswith(obj->name, "nsync_mu_semaphore_")) continue;
if (startswith(obj->name, "Describe")) continue;
if (startswith(obj->name, "__sig")) continue;
if (startswith(obj->name, "__zipos")) continue;
if (obj->is_static) continue;
if (obj->is_string_literal) continue;
if (obj->section && _startswith(obj->section, ".init_array")) continue;
if (obj->section && startswith(obj->section, ".init_array")) continue;
APPEND(dox->objects);
dox->objects.p[dox->objects.n - 1] = obj;
}

View file

@ -777,7 +777,7 @@ document.addEventListener('DOMContentLoaded', function () {\n\
prefix = xasprintf("%s ", o->params.p[j].name);
for (k = 0; k < o->javadown->tags.n; ++k) {
if (!strcmp(o->javadown->tags.p[k].tag, "param") &&
_startswith(o->javadown->tags.p[k].text, prefix)) {
startswith(o->javadown->tags.p[k].text, prefix)) {
fprintf(f, "<dd>");
PrintText(f, o->javadown->tags.p[k].text + strlen(prefix));
fprintf(f, "\n");
@ -911,7 +911,7 @@ document.addEventListener('DOMContentLoaded', function () {\n\
prefix = xasprintf("%s ", m->params.p[j].name);
for (k = 0; k < m->javadown->tags.n; ++k) {
if (!strcmp(m->javadown->tags.p[k].tag, "param") &&
_startswith(m->javadown->tags.p[k].text, prefix)) {
startswith(m->javadown->tags.p[k].text, prefix)) {
fprintf(f, "<dd>");
PrintText(f, m->javadown->tags.p[k].text + strlen(prefix));
fprintf(f, "\n");

View file

@ -3,8 +3,8 @@
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
__static_yoink("sys_mmap"); /* asan needs it */
__static_yoink("TrackMemoryInterval"); /* asan needs it */
__static_yoink("sys_mmap"); /* asan needs it */
__static_yoink("__track_memory"); /* asan needs it */
#define ASSERT(x, y) Assert2(x, y, #y, __FILE__, __LINE__)
#define ASSERT128(x, y) Assert128(x, y, #y, __FILE__, __LINE__)

View file

@ -653,7 +653,7 @@ static uint32_t read_universal_char(char *p, int len) {
uint32_t c = 0;
for (int i = 0; i < len; i++) {
if (!isxdigit(p[i])) return 0;
c = (c << 4) | hextoint(p[i]);
c = (c << 4) | kHexToInt[p[i] & 255];
}
return c;
}

View file

@ -1297,29 +1297,6 @@ void* dlmemalign(size_t alignment, size_t bytes) {
return internal_memalign(gm, alignment, bytes);
}
int dlposix_memalign(void** pp, size_t alignment, size_t bytes) {
void* mem = 0;
if (alignment == MALLOC_ALIGNMENT)
mem = dlmalloc(bytes);
else {
size_t d = alignment / sizeof(void*);
size_t r = alignment % sizeof(void*);
if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0)
return EINVAL;
else if (bytes <= MAX_REQUEST - alignment) {
if (alignment < MIN_CHUNK_SIZE)
alignment = MIN_CHUNK_SIZE;
mem = internal_memalign(gm, alignment, bytes);
}
}
if (!mem) {
return ENOMEM;
} else {
*pp = mem;
return 0;
}
}
#if USE_LOCKS
void dlmalloc_atfork(void) {
bzero(&gm->mutex, sizeof(gm->mutex));

View file

@ -1,5 +1,30 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_DLMALLOC_DLMALLOC_H_
#define COSMOPOLITAN_THIRD_PARTY_DLMALLOC_DLMALLOC_H_
#define dlbulk_free __dlbulk_free
#define dlcalloc __dlcalloc
#define dlfree __dlfree
#define dlindependent_calloc __dlindependent_calloc
#define dlindependent_comalloc __dlindependent_comalloc
#define dlmallinfo __dlmallinfo
#define dlmalloc __dlmalloc
#define dlmalloc_abort __dlmalloc_abort
#define dlmalloc_atfork __dlmalloc_atfork
#define dlmalloc_footprint __dlmalloc_footprint
#define dlmalloc_footprint_limit __dlmalloc_footprint_limit
#define dlmalloc_inspect_all __dlmalloc_inspect_all
#define dlmalloc_max_footprint __dlmalloc_max_footprint
#define dlmalloc_set_footprint_limit __dlmalloc_set_footprint_limit
#define dlmalloc_stats __dlmalloc_stats
#define dlmalloc_trim __dlmalloc_trim
#define dlmalloc_usable_size __dlmalloc_usable_size
#define dlmallopt __dlmallopt
#define dlmallopt __dlmallopt
#define dlmemalign __dlmemalign
#define dlrealloc __dlrealloc
#define dlrealloc_in_place __dlrealloc_in_place
#define dlrealloc_in_place __dlrealloc_in_place
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -88,16 +113,6 @@ void* dlrealloc_in_place(void*, size_t);
*/
void* dlmemalign(size_t, size_t);
/*
int posix_memalign(void** pp, size_t alignment, size_t n);
Allocates a chunk of n bytes, aligned in accord with the alignment
argument. Differs from memalign only in that it (1) assigns the
allocated memory to *pp rather than returning it, (2) fails and
returns EINVAL if the alignment is not a power of two (3) fails and
returns ENOMEM if memory cannot be allocated.
*/
int dlposix_memalign(void**, size_t, size_t);
/*
mallopt(int parameter_number, int parameter_value)
Sets tunable parameters The format is to provide a

View file

@ -21,6 +21,7 @@
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/dlmalloc/dlmalloc.h"
#define MESSAGE "dlmalloc_abort()\n"

View file

@ -2140,7 +2140,7 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
bool quantize =
tensor.ne.size() == 2 &&
tensor.type != quantized_type &&
_endswith(tensor.name.c_str(), "weight") &&
endswith(tensor.name.c_str(), "weight") &&
tensor.name != "output.weight";
enum ggml_type new_type;

View file

@ -1224,7 +1224,7 @@ StartOver:
} else {
flipit = hasflip && (i == flip[0] || i == flip[1]);
if (flipit) abAppendw(&ab, READ32LE("\e[1m"));
abAppendw(&ab, _tpenc(rune.c));
abAppendw(&ab, tpenc(rune.c));
if (flipit) abAppendw(&ab, READ64LE("\e[22m\0\0"));
}
t = wcwidth(rune.c);
@ -1473,7 +1473,7 @@ static void linenoiseEditXlatWord(struct linenoiseState *l,
r = GetUtf8(l->buf + j, l->len - j);
if (iswseparator(r.c)) break;
if ((c = xlat(r.c)) != r.c) {
abAppendw(&ab, _tpenc(c));
abAppendw(&ab, tpenc(c));
} else { /* avoid canonicalization */
abAppend(&ab, l->buf + j, r.n);
}
@ -1727,7 +1727,7 @@ static void linenoiseEditBarf(struct linenoiseState *l) {
/* now move the text */
r = GetUtf8(l->buf + end, l->len - end);
memmove(l->buf + pos + r.n, l->buf + pos, end - pos);
w = _tpenc(r.c);
w = tpenc(r.c);
for (i = 0; i < r.n; ++i) {
l->buf[pos + i] = w;
w >>= 8;
@ -2248,7 +2248,7 @@ ssize_t linenoiseEdit(struct linenoiseState *l, const char *prompt, char **obuf,
uint64_t w;
struct rune rune;
rune = GetUtf8(seq, rc);
w = _tpenc(xlatCallback(rune.c));
w = tpenc(xlatCallback(rune.c));
rc = 0;
do {
seq[rc++] = w;
@ -2393,7 +2393,7 @@ char *linenoiseGetHistoryPath(const char *prog) {
if (*a) {
abAppends(&path, a);
abAppends(&path, b);
if (!_endswith(path.b, "/") && !_endswith(path.b, "\\")) {
if (!endswith(path.b, "/") && !endswith(path.b, "\\")) {
abAppendw(&path, '/');
}
}

View file

@ -149,7 +149,7 @@ void lua_readline_completions (const char *p, linenoiseCompletions *c) {
while (lua_next(L, -2)) {
if (lua_type(L, -2) == LUA_TSTRING) {
name = lua_tolstring(L, -2, &n);
if (_startswithi(name, a) && (s = malloc(a - p + n + 1))) {
if (startswithi(name, a) && (s = malloc(a - p + n + 1))) {
memcpy(s, p, a - p);
memcpy(s + (a - p), name, n + 1);
lua_readline_addcompletion(c, s);
@ -162,7 +162,7 @@ void lua_readline_completions (const char *p, linenoiseCompletions *c) {
lua_pop(L, 1); // pop table
for (i = 0; i < ARRAYLEN(kKeywordHints); ++i) {
if (_startswithi(kKeywordHints[i], p)) {
if (startswithi(kKeywordHints[i], p)) {
if ((s = strdup(kKeywordHints[i]))) {
lua_readline_addcompletion(c, s);
}

View file

@ -2667,7 +2667,7 @@ static PyObject *SFLObject_is_package(SourcelessFileLoader *self,
self->name, name);
return NULL;
}
if (_startswith(basename(self->path), "__init__")) {
if (startswith(basename(self->path), "__init__")) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;

View file

@ -346,7 +346,7 @@ GetModName(bool *ispkg)
{
char *mod;
mod = Dotify(xstripexts(StripComponents(pyfile, strip_components)));
if ((*ispkg = _endswith(mod, ".__init__"))) {
if ((*ispkg = endswith(mod, ".__init__"))) {
mod[strlen(mod) - strlen(".__init__")] = 0;
}
return mod;
@ -377,7 +377,7 @@ GetParent2(void)
{
char *p, *mod;
mod = Dotify(xstripexts(StripComponents(pyfile, strip_components)));
if (_endswith(mod, ".__init__")) mod[strlen(mod) - strlen(".__init__")] = 0;
if (endswith(mod, ".__init__")) mod[strlen(mod) - strlen(".__init__")] = 0;
if ((p = strrchr(mod, '.'))) *p = 0;
return mod;
}

View file

@ -96,7 +96,7 @@ CompleteModule(const char *s, const char *p, linenoiseCompletions *c)
PyObject *m, *f, *g, *i, *v, *n;
plen = strlen(p);
for (it = PyImport_Inittab; it->name; ++it) {
if (_startswithi(it->name, p)) {
if (startswithi(it->name, p)) {
AddCompletion(c, xasprintf("%s%s", s, it->name + plen));
}
}
@ -170,7 +170,7 @@ Complete(const char *p, linenoiseCompletions *c)
{
PyObject *o, *t, *i;
const char *q, *s, *b;
if (_startswith(p, "import ")) {
if (startswith(p, "import ")) {
for (q = p + 7; *q; ++q) {
if (!isalnum(*q) && *q != '_') {
return;

View file

@ -636,7 +636,7 @@ int main(int argc, char **argv)
"\n"
);
} else {
#ifndef COSMO
#ifndef _COSMO_SOURCE
fprintf(fo, "#include <inttypes.h>\n"
"\n"
);

View file

@ -2050,7 +2050,7 @@ static void call_handler(JSContext *ctx, JSValueConst func)
JS_FreeValue(ctx, ret);
}
#if defined(COSMO)
#if defined(_COSMO_SOURCE)
#define DWORD uint32_t
#define HANDLE int64_t
#define _get_osfhandle(fd) g_fds.p[fd].handle

View file

@ -2147,7 +2147,7 @@ static void gptneox_model_quantize_internal(const std::string & fname_inp, const
bool quantize =
tensor.ne.size() == 2 &&
tensor.type != quantized_type &&
_endswith(tensor.name.c_str(), "weight") &&
endswith(tensor.name.c_str(), "weight") &&
tensor.name != "output.weight";
enum ggml_type new_type;

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/safemacros.internal.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/str/str.h"
#include "third_party/regex/regex.h"
@ -40,6 +40,18 @@ static const char kRegexErrors[] =
"Out of memory\0"
"Repetition not preceded by valid expression\0";
static const char *IndexDoubleNulString(const char *s, unsigned i) {
size_t n;
while (i--) {
if ((n = strlen(s))) {
s += n + 1;
} else {
return NULL;
}
}
return s;
}
/**
* Converts regular expression error code to string.
*

View file

@ -21,8 +21,6 @@
/**
* Xed error code names.
*
* puts(IndexDoubleNulString(kXedErrorNames, xedd->op.error));
*
* @see XedError
*/
const char kXedErrorNames[] = "\