commit cursetree

This commit is contained in:
Emile Clark-Boman 2025-09-27 20:46:05 +10:00
parent 7f4f9941de
commit 9b0e752b8a
8 changed files with 95 additions and 37 deletions

View file

@ -4,23 +4,23 @@
#include "_ct_shared.h"
#include "surface.h"
#define ALLOC_ROW(w) ((char *)calloc(w, sizeof(char)))
#define ALLOC_COLS(w) ((glyph *)calloc(w, sizeof(glyph)))
/* Allocates a new 2D surface buffer with ROWS=h, COLS=w.
* Returns NULL on failure and sets errno, otherwise
* a pointer to the new buffer is returned.
*/
static inline __attribute__((malloc, warn_unused_result)) char **
static inline __attribute__((malloc, warn_unused_result)) glyph **
sfbuf(const pos w, const pos h) {
char **buf;
glyph **buf;
pos i;
buf = (char **)malloc(sizeof(char *) * h);
buf = (glyph **)malloc(sizeof(glyph *) * h);
if (buf == NULL)
return buf;
for (i = 0; i < w; i++) {
buf[i] = ALLOC_ROW(w);
buf[i] = ALLOC_COLS(w);
if (buf[i] == NULL)
goto fail;
}
@ -43,7 +43,7 @@ fail:
static inline int sfsetw(const pos w1, surface *const s) {
void *ptr;
pos i, w0 = s->w;
char **row;
glyph **row;
errno = 0; // sanity check
@ -54,11 +54,11 @@ static inline int sfsetw(const pos w1, surface *const s) {
}
for (i = 0, row = s->buf; i < s->h; i++, row++) {
ptr = (void *)reallocarray(*row, w1, sizeof(char));
ptr = (void *)reallocarray(*row, w1, sizeof(glyph));
if (ptr == NULL) {
return errno;
}
*row = (char *)ptr;
*row = (glyph *)ptr;
}
s->w = w1;
@ -73,7 +73,7 @@ static inline int sfsetw(const pos w1, surface *const s) {
static inline int sfseth(const pos h1, surface *const s) {
void *ptr;
pos i, h0 = s->h;
char **row;
glyph **row;
errno = 0; // sanity check
@ -91,18 +91,18 @@ static inline int sfseth(const pos h1, surface *const s) {
}
// use ptr to avoid losing unfreed s->buf
ptr = (void *)reallocarray(s->buf, h1, sizeof(char));
ptr = (void *)reallocarray(s->buf, h1, sizeof(glyph));
if (ptr == NULL)
return errno;
// ENOMEM X_X
s->buf = (char **)ptr;
s->buf = (glyph **)ptr;
s->h = h1;
// height increased (alloc bottom rows)
if (h1 > h0) {
for (i = h0, row = s->buf; i < h1; i++, row++) {
*row = ALLOC_ROW(s->w);
*row = ALLOC_COLS(s->w);
if (*row == NULL)
return errno;
}
@ -135,7 +135,7 @@ int sfresize(const pos w1, const pos h1, surface *const s) {
__attribute__((malloc, warn_unused_result)) surface *sfalloc(const pos x, const pos y, const pos w, const pos h) {
surface *s;
char **buf = sfbuf(w, h);
glyph **buf = sfbuf(w, h);
if (buf == NULL)
return NULL;
@ -155,7 +155,7 @@ __attribute__((malloc, warn_unused_result)) surface *sfalloc(const pos x, const
void sffree(surface *const s) {
pos i;
char **row;
glyph **row;
for (i=0, row=s->buf; i < s->h; i++, row++) {
free(*row);
}