Clean up more code

The *NSYNC linked list API is good enough that it deserves to be part of
the C libray, so this change writes an improved version of it which uses
that offsetof() trick from the Linux Kernel. We vendor all of the *NSYNC
tests in third_party which helped confirm the needed refactoring is safe

This change also deletes more old code that didn't pan out. My goal here
is to work towards a vision where the Cosmopolitan core libraries become
less experimental and more focused on curation. This better reflects the
current level of quality we've managed to achieve.
This commit is contained in:
Justine Tunney 2023-07-06 06:57:28 -07:00
parent 88612a2cd7
commit 0a24b4fc3c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
268 changed files with 632 additions and 8688 deletions

View file

@ -26,7 +26,6 @@
#include "libc/mem/mem.h"
#include "libc/nexgen32e/kompressor.h"
#include "libc/nexgen32e/lz4.h"
#include "libc/runtime/ezmap.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"

View file

@ -36,7 +36,6 @@
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/ezmap.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/stdio/append.h"

View file

@ -25,5 +25,5 @@ void boxblur(struct Graphic *g) {
{+1.0, +1.0, +1.0},
{+1.0, +1.0, +1.0},
};
convolve(g->yn, g->xn, g->b.p, 3, kBoxBlurKernel, 9.0, 0);
convolve(g->yn, g->xn, g->b, 3, kBoxBlurKernel, 9.0, 0);
}

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_TOOL_VIZ_LIB_CONVOLVE_H_
#define COSMOPOLITAN_TOOL_VIZ_LIB_CONVOLVE_H_
#include "dsp/tty/quant.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "tool/viz/lib/graphic.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
@ -24,7 +25,7 @@ forceinline void convolve(unsigned yn, unsigned xn, ttyrgb_m128 img[yn][xn],
}
bzero(&g, sizeof(g));
resizegraphic(&g, yn, xn);
tmp = g.b.p;
tmp = g.b;
for (y = 0; y < yn - KW; ++y) {
for (x = 0; x < xn - KW; ++x) {
bzero(&p, sizeof(p));
@ -37,7 +38,7 @@ forceinline void convolve(unsigned yn, unsigned xn, ttyrgb_m128 img[yn][xn],
}
}
memcpy(img, tmp, yn * xn * sizeof(img[0][0]));
bfree(&g.b);
free(g.b);
}
COSMOPOLITAN_C_END_

View file

@ -26,5 +26,5 @@ void emboss(struct Graphic *g) {
{-1.0, +1.0, +1.0},
{+0.0, +1.0, +2.0},
};
convolve(g->yn, g->xn, g->b.p, 3, kEmbossKernel, 1, 0);
convolve(g->yn, g->xn, g->b, 3, kEmbossKernel, 1, 0);
}

View file

@ -22,7 +22,7 @@
void getxtermcodes(struct TtyRgb *p, const struct Graphic *g) {
unsigned y, x;
unsigned char(*img)[3][g->yn][g->xn] = g->b.p;
unsigned char(*img)[3][g->yn][g->xn] = g->b;
for (y = 0; y < g->yn; ++y) {
for (x = 0; x < g->xn; ++x) {
*p++ = rgb2tty((*img)[0][y][x], (*img)[1][y][x], (*img)[2][y][x]);

View file

@ -1,13 +1,12 @@
#ifndef COSMOPOLITAN_TOOL_VIZ_LIB_GRAPHIC_H_
#define COSMOPOLITAN_TOOL_VIZ_LIB_GRAPHIC_H_
#include "dsp/tty/quant.h"
#include "libc/runtime/buffer.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct Graphic {
union {
struct GuardedBuffer b;
void *b;
char *bytes;
float (*lum)[2][8];
float (*rgba)[2][2];

View file

@ -18,19 +18,14 @@
*/
#include "dsp/tty/quant.h"
#include "libc/assert.h"
#include "libc/runtime/buffer.internal.h"
#include "libc/mem/mem.h"
#include "tool/viz/lib/graphic.h"
/**
* Allocates graphic.
*
* @param g should be zero initialized before first call
* @note bfree(g->b) needs to be called later
*/
// TODO(jart): DELETE
struct Graphic *resizegraphic(struct Graphic *g, size_t yn, size_t xn) {
/* assert(xn % 2 == 0); */ /* todo: ughhh this whole thing is wrong */
yn &= ~1;
balloc(&g->b, 64, yn * xn * sizeof(ttyrgb_m128) + /* wut */ PAGESIZE);
g->b = pvalloc(yn * xn * sizeof(ttyrgb_m128));
g->yn = yn;
g->xn = xn;
return g;

View file

@ -69,7 +69,7 @@ void sobel(struct Graphic* g) {
FLIP(BROADCAST(-2), BROADCAST(+0), BROADCAST(+2)),
FLIP(BROADCAST(-1), BROADCAST(+0), BROADCAST(+1)));
if (g->yn >= 3 && g->xn >= 3) {
ConvolveGradient(g->yn, g->xn, g->b.p, 3, &kSobelEmbossKernelY,
ConvolveGradient(g->yn, g->xn, g->b, 3, &kSobelEmbossKernelY,
&kSobelEmbossKernelX);
}
}

View file

@ -66,8 +66,7 @@ o/$(MODE)/tool/viz/lib/dither.o \
o/$(MODE)/tool/viz/lib/emboss.o \
o/$(MODE)/tool/viz/lib/getxtermcodes.o \
o/$(MODE)/tool/viz/lib/lingamma.o \
o/$(MODE)/tool/viz/lib/perlin3.o \
o/$(MODE)/tool/viz/lib/resizegraphic.o: private \
o/$(MODE)/tool/viz/lib/perlin3.o: private \
CFLAGS += \
-DSTACK_FRAME_UNLIMITED \
$(MATHEMATICAL)

View file

@ -56,7 +56,6 @@
#include "libc/nexgen32e/x86feature.h"
#include "libc/nt/console.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/buffer.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/pollfd.h"
@ -173,7 +172,7 @@ mode.\n\
#define BALLOC(B, A, N, NAME) \
({ \
INFOF("balloc/%s %,zu bytes", NAME, N); \
balloc(B, A, N); \
*(B) = pvalloc(N); \
})
#define TIMEIT(OUT_NANOS, FORM) \
@ -207,7 +206,7 @@ struct NamedVector {
struct VtFrame {
size_t i, n;
union {
struct GuardedBuffer b;
void *b;
char *bytes;
};
};
@ -266,6 +265,7 @@ static bool emboss_, sobel_;
static volatile int playpid_;
static struct winsize wsize_;
static float hue_, sat_, lit_;
static void *xtcodes_, *audio_;
static struct FrameBuffer fb0_;
static unsigned chans_, srate_;
static volatile bool ignoresigs_;
@ -277,7 +277,6 @@ static openspeaker_f tryspeakerfns_[4];
static int primaries_, lighting_, swing_;
static uint64_t t1, t2, t3, t4, t5, t6, t8;
static const char *sox_, *ffplay_, *patharg_;
static struct GuardedBuffer xtcodes_, audio_;
static struct VtFrame vtframe_[2], *f1_, *f2_;
static struct Graphic graphic_[2], *g1_, *g2_;
static bool yes_, stats_, dither_, ttymode_, istango_;
@ -614,7 +613,7 @@ static char *StartRender(char *vt) {
static void EndRender(char *vt) {
vt += sprintf(vt, "\e[0m");
f2_->n = (intptr_t)vt - (intptr_t)f2_->b.p;
f2_->n = (intptr_t)vt - (intptr_t)f2_->b;
f2_->i = 0;
}
@ -692,7 +691,7 @@ static void RenderIt(void) {
struct TtyRgb bg, fg;
yn = g2_->yn;
xn = g2_->xn;
vt = f2_->b.p;
vt = f2_->b;
p = StartRender(vt);
if (TTYQUANT()->alg == kTtyQuantTrue) {
bg = (struct TtyRgb){0, 0, 0, 0};
@ -708,7 +707,7 @@ static void RenderIt(void) {
fg = (struct TtyRgb){0xff, 0xff, 0xff, 231};
p = stpcpy(p, "\e[48;5;16;38;5;231m");
}
p = ttyraster(p, xtcodes_.p, yn, xn, bg, fg);
p = ttyraster(p, xtcodes_, yn, xn, bg, fg);
if (ttymode_ && stats_) {
bpc = bpf = p - vt;
bpc /= wsize_.ws_row * wsize_.ws_col;
@ -759,7 +758,7 @@ static void RasterIt(void) {
once = true;
}
WriteToFrameBuffer(fb0_.vscreen.yres_virtual, fb0_.vscreen.xres_virtual, buf,
g2_->yn, g2_->xn, g2_->b.p, fb0_.vscreen.yres,
g2_->yn, g2_->xn, g2_->b, fb0_.vscreen.yres,
fb0_.vscreen.xres);
memcpy(fb0_.map, buf, fb0_.size);
}
@ -776,7 +775,7 @@ static void TranscodeVideo(plm_frame_t *pf) {
if (pf1_) pary_ = 1.;
if (pf2_) pary_ = (266 / 64.) * (900 / 1600.);
pary_ *= plm_get_pixel_aspect_ratio(plm_);
YCbCr2RgbScale(g2_->yn, g2_->xn, g2_->b.p, pf->y.height, pf->y.width,
YCbCr2RgbScale(g2_->yn, g2_->xn, g2_->b, pf->y.height, pf->y.width,
(void *)pf->y.data, pf->cr.height, pf->cr.width,
(void *)pf->cb.data, (void *)pf->cr.data, pf->y.height,
pf->y.width, pf->cr.height, pf->cr.width, pf->height,
@ -791,7 +790,7 @@ static void TranscodeVideo(plm_frame_t *pf) {
boxblur(g2_);
break;
case kBlurGaussian:
gaussian(g2_->yn, g2_->xn, g2_->b.p);
gaussian(g2_->yn, g2_->xn, g2_->b);
break;
default:
break;
@ -800,16 +799,16 @@ static void TranscodeVideo(plm_frame_t *pf) {
if (emboss_) emboss(g2_);
switch (sharp_) {
case kSharpSharp:
sharpen(3, g2_->yn, g2_->xn, g2_->b.p, g2_->yn, g2_->xn);
sharpen(3, g2_->yn, g2_->xn, g2_->b, g2_->yn, g2_->xn);
break;
case kSharpUnsharp:
unsharp(3, g2_->yn, g2_->xn, g2_->b.p, g2_->yn, g2_->xn);
unsharp(3, g2_->yn, g2_->xn, g2_->b, g2_->yn, g2_->xn);
break;
default:
break;
}
if (dither_ && TTYQUANT()->alg != kTtyQuantTrue) {
dither(g2_->yn, g2_->xn, g2_->b.p, g2_->yn, g2_->xn);
dither(g2_->yn, g2_->xn, g2_->b, g2_->yn, g2_->xn);
}
});
@ -817,7 +816,7 @@ static void TranscodeVideo(plm_frame_t *pf) {
t3 = 0;
TIMEIT(t4, RasterIt());
} else {
TIMEIT(t3, getxtermcodes(xtcodes_.p, g2_));
TIMEIT(t3, getxtermcodes(xtcodes_, g2_));
TIMEIT(t4, RenderIt());
}
@ -1401,19 +1400,16 @@ static void OnExit(void) {
ttyidentclear(&ti_);
close(infd_), infd_ = -1;
close(outfd_), outfd_ = -1;
bfree(&graphic_[0].b);
bfree(&graphic_[1].b);
bfree(&vtframe_[0].b);
bfree(&vtframe_[1].b);
bfree(&xtcodes_);
bfree(&audio_);
free(graphic_[0].b);
free(graphic_[1].b);
free(vtframe_[0].b);
free(vtframe_[1].b);
free(xtcodes_);
free(audio_);
CloseSpeaker();
}
static void MakeLatencyLittleLessBad(void) {
#ifdef __x86_64__
_peekall();
#endif
LOGIFNEG1(sys_mlockall(MCL_CURRENT));
LOGIFNEG1(nice(-5));
}