Some work

This commit is contained in:
2026-01-22 21:08:02 -08:00
parent eca8ae3d3e
commit f67ed56d52
12 changed files with 488 additions and 38 deletions

View File

@ -2,6 +2,7 @@
#define INCLUDED_MEMORY_H
#include <float.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@ -18,6 +19,12 @@
# define ALWAYS_INLINE inline
#endif
#if __has_attribute(format)
# define FORMAT(n, m) __attribute__((format(printf, n, m)))
#else
# define FORMAT(n, m)
#endif
// Byte order stuff
typedef enum {
ENDIAN_LITTLE,
@ -123,4 +130,40 @@ static ALWAYS_INLINE void add_timespecs(const struct timespec *t1,
out->tv_nsec = nsec;
}
typedef struct {
// this is actually size + 1 bytes for the null byte
char *buffer;
size_t size;
size_t nchars;
} StringStream;
static inline void string_stream_init(StringStream *restrict stream) {
stream->buffer = lisp_malloc(1);
stream->size = 0;
stream->buffer[stream->size] = '\0';
stream->nchars = 0;
}
static inline void string_stream_free(StringStream *restrict stream) {
lisp_free(stream->buffer);
}
static inline void string_stream_steal(StringStream *restrict stream,
char **restrict out,
size_t *restrict out_length) {
*out = stream->buffer;
*out_length = stream->nchars;
}
int string_stream_printf(StringStream *restrict stream,
const char *restrict format, ...) FORMAT(2, 3);
int string_stream_vprintf(StringStream *restrict stream,
const char *restrict format, va_list args);
// Get the next line in BUF starting at *START (or &buf[0] if *START is
// NULL). Store the length of the line in LENGTH. BUF is BUF_LENGTH bytes
// long. Return true if we found another line and false otherwise.
bool strgetline(const char *restrict buf, size_t buf_length,
const char **restrict start, size_t *restrict length);
#endif