mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-01 03:53:33 +00:00
226aaf3547
This commit makes numerous refinements to cosmopolitan memory handling. The default stack size has been reduced from 2mb to 128kb. A new macro is now provided so you can easily reconfigure the stack size to be any value you want. Work around the breaking change by adding to your main: STATIC_STACK_SIZE(0x00200000); // 2mb stack If you're not sure how much stack you need, then you can use: STATIC_YOINK("stack_usage_logging"); After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test suite, nothing in the Cosmopolitan repository (except for Python) needs a stack size greater than 30kb. There are also new macros for detecting the size and address of the stack at runtime, e.g. GetStackAddr(). We also now support sigaltstack() so if you want to see nice looking crash reports whenever a stack overflow happens, you can put this in main(): ShowCrashReports(); Under `make MODE=dbg` and `make MODE=asan` the unit testing framework will now automatically print backtraces of memory allocations when things like memory leaks happen. Bugs are now fixed in ASAN global variable overrun detection. The memtrack and asan runtimes also handle edge cases now. The new tools helped to identify a few memory leaks, which are fixed by this change. This change should fix an issue reported in #288 with ARG_MAX limits. Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
173 lines
6 KiB
C
173 lines
6 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/alg/reverse.internal.h"
|
|
#include "libc/assert.h"
|
|
#include "libc/fmt/conv.h"
|
|
#include "libc/fmt/fmts.h"
|
|
#include "libc/fmt/internal.h"
|
|
#include "libc/limits.h"
|
|
|
|
#define BUFFER_SIZE 144
|
|
|
|
uintmax_t __udivmodti4(uintmax_t, uintmax_t, uintmax_t *);
|
|
|
|
static int __fmt_ntoa_format(int out(const char *, void *, size_t), void *arg,
|
|
char *buf, unsigned len, bool negative,
|
|
unsigned log2base, unsigned prec, unsigned width,
|
|
unsigned char flags) {
|
|
unsigned i;
|
|
/* pad leading zeros */
|
|
if (!(flags & FLAGS_LEFT)) {
|
|
if (width && (flags & FLAGS_ZEROPAD) &&
|
|
(negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
|
width--;
|
|
}
|
|
while ((len < prec) && (len < BUFFER_SIZE)) {
|
|
buf[len++] = '0';
|
|
}
|
|
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < BUFFER_SIZE)) {
|
|
buf[len++] = '0';
|
|
}
|
|
}
|
|
/* handle hash */
|
|
if (flags & FLAGS_HASH) {
|
|
if (!(flags & FLAGS_PRECISION) && len &&
|
|
((len == prec) || (len == width)) && buf[len - 1] == '0') {
|
|
len--;
|
|
if (len && (log2base == 4 || log2base == 1) && buf[len - 1] == '0') {
|
|
len--;
|
|
}
|
|
}
|
|
if (log2base == 4 && len < BUFFER_SIZE) {
|
|
buf[len++] = 'x';
|
|
} else if (log2base == 1 && len < BUFFER_SIZE) {
|
|
buf[len++] = 'b';
|
|
}
|
|
if (len < BUFFER_SIZE) {
|
|
buf[len++] = '0';
|
|
}
|
|
}
|
|
if (len < BUFFER_SIZE) {
|
|
if (negative) {
|
|
buf[len++] = '-';
|
|
} else if (flags & FLAGS_PLUS) {
|
|
buf[len++] = '+'; /* ignore the space if the '+' exists */
|
|
} else if (flags & FLAGS_SPACE) {
|
|
buf[len++] = ' ';
|
|
}
|
|
}
|
|
/* pad spaces up to given width */
|
|
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
|
if (len < width) {
|
|
if (__fmt_pad(out, arg, width - len) == -1) return -1;
|
|
}
|
|
}
|
|
reverse(buf, len);
|
|
if (out(buf, arg, len) == -1) return -1;
|
|
/* append pad spaces up to given width */
|
|
if (flags & FLAGS_LEFT) {
|
|
if (len < width) {
|
|
if (__fmt_pad(out, arg, width - len) == -1) return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int __fmt_ntoa2(int out(const char *, void *, size_t), void *arg,
|
|
uintmax_t value, bool neg, unsigned log2base, unsigned prec,
|
|
unsigned width, unsigned flags, const char *alphabet) {
|
|
uint64_t u64;
|
|
uintmax_t remainder;
|
|
unsigned len, count, digit;
|
|
char buf[BUFFER_SIZE];
|
|
len = 0;
|
|
if (!value) flags &= ~FLAGS_HASH;
|
|
if (value || !(flags & FLAGS_PRECISION)) {
|
|
count = 0;
|
|
do {
|
|
if (!log2base) {
|
|
if (value <= UINT64_MAX) {
|
|
u64 = value;
|
|
digit = u64 % 10;
|
|
value = u64 / 10;
|
|
} else {
|
|
value = __udivmodti4(value, 10, &remainder);
|
|
digit = remainder;
|
|
}
|
|
} else {
|
|
digit = value;
|
|
digit &= (1u << log2base) - 1;
|
|
value >>= log2base;
|
|
}
|
|
if ((flags & FLAGS_GROUPING) && count == 3) {
|
|
buf[len++] = ',';
|
|
count = 1;
|
|
} else {
|
|
count++;
|
|
}
|
|
buf[len++] = alphabet[digit];
|
|
} while (value);
|
|
assert(count <= BUFFER_SIZE);
|
|
}
|
|
return __fmt_ntoa_format(out, arg, buf, len, neg, log2base, prec, width,
|
|
flags);
|
|
}
|
|
|
|
int __fmt_ntoa(int out(const char *, void *, size_t), void *arg, va_list va,
|
|
unsigned char signbit, unsigned long log2base,
|
|
unsigned long prec, unsigned long width, unsigned char flags,
|
|
const char *lang) {
|
|
bool neg;
|
|
uintmax_t value, sign;
|
|
|
|
/* ignore '0' flag when prec is given */
|
|
if (flags & FLAGS_PRECISION) {
|
|
flags &= ~FLAGS_ZEROPAD;
|
|
}
|
|
|
|
/* no plus / space flag for u, x, X, o, b */
|
|
if (!(flags & FLAGS_ISSIGNED)) {
|
|
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
|
}
|
|
|
|
if (signbit > 63) {
|
|
value = va_arg(va, uint128_t);
|
|
} else {
|
|
value = va_arg(va, uint64_t);
|
|
}
|
|
|
|
neg = 0;
|
|
sign = 1;
|
|
sign <<= signbit;
|
|
value &= sign | (sign - 1);
|
|
if (flags & FLAGS_ISSIGNED) {
|
|
if (value != sign) {
|
|
if (value & sign) {
|
|
value = ~value + 1;
|
|
value &= sign | (sign - 1);
|
|
neg = 1;
|
|
}
|
|
value &= sign - 1;
|
|
} else {
|
|
neg = 1;
|
|
}
|
|
}
|
|
|
|
return __fmt_ntoa2(out, arg, value, neg, log2base, prec, width, flags, lang);
|
|
}
|