various utils

This commit is contained in:
pantonshire committed 2025-12-20 13:10:23 +00:00
1 parent 3851b60310
commit 45284910cc
8 files changed
+179 -2

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "eventloop.h"
#include "mem.h"
struct eventloop {
int kqueue_fd;
};
struct eventloop *eventloop_new(void) {
MEM_SCOPED(struct eventloop, el);
if (!(el = malloc(sizeof(struct eventloop)))) {
errno = ENOMEM;
return NULL;
}
return MEM_RELEASE(struct eventloop, &el);
}
void eventloop_free(struct eventloop *el) {
if (!el)
return;
close(el->kqueue_fd);
free(el);
}
+18
View File
@@ -0,0 +1,18 @@
#include <stdlib.h>
#include <string.h>
#include "str.h"
str_buf_t sb_from_sv(str_view_t s) {
char *buf;
if (sv_is_null(s) || sv_is_empty(s))
return SB_NULL;
if (!(buf = malloc(s.len + 1)))
return SB_NULL;
memcpy(buf, s.ptr, s.len);
buf[s.len] = 0;
return (str_buf_t) { buf, s.len };
}