aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-16 18:02:57 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-16 18:02:57 +0200
commit297a44c08675f4b7744081963248e9e3552850e7 (patch)
tree4badae72f48b67dd0299461aecea9961b355b0e6
parent230dc1748aa68cccde6de9aa3cdade7d64aede56 (diff)
downloadffwd-297a44c08675f4b7744081963248e9e3552850e7.tar.gz
ffwd-297a44c08675f4b7744081963248e9e3552850e7.zip
server: extract server_util, sbuf, and base64 leaf utilities
-rw-r--r--Makefile12
-rw-r--r--src/base64.c23
-rw-r--r--src/base64.h10
-rw-r--r--src/sbuf.c61
-rw-r--r--src/sbuf.h24
-rw-r--r--src/server.c153
-rw-r--r--src/server_internal.h7
-rw-r--r--src/server_util.c82
-rw-r--r--src/server_util.h36
9 files changed, 250 insertions, 158 deletions
diff --git a/Makefile b/Makefile
index 9451c47..1612646 100644
--- a/Makefile
+++ b/Makefile
@@ -81,6 +81,11 @@ TOKENIZER_SRCS = src/tokenizer_bpe.c src/tokenizer_wordpiece.c
CORE_SRCS = src/model.c src/forward.c src/late.c src/vec.c src/alloc.c \
src/config.c src/safetensors.c $(KERNEL_SRCS)
+# Server-only objects: the embed-server binary and the server test link these
+# alongside server.o. Not in libembed.a - the CLI and library do not use them.
+SERVER_SRCS = src/server_util.c src/sbuf.c src/base64.c
+SERVER_OBJS = $(SERVER_SRCS:.c=.o)
+
SRCS = $(CORE_SRCS) $(TOKENIZER_SRCS) \
deps/ae/ae.c deps/ae/anet.c deps/ae/monotonic.c
@@ -171,8 +176,8 @@ $(LIB): $(OBJS) $(EXTRA_OBJS)
$(TARGET): $(LIB) src/cli.o
$(CC) $(CFLAGS) -o $@ src/cli.o $(LIB) $(LDFLAGS)
-$(SERVER_TARGET): $(LIB) src/server.o
- $(CC) $(CFLAGS) -o $@ src/server.o $(LIB) $(LDFLAGS) $(CJSON_LDFLAGS)
+$(SERVER_TARGET): $(LIB) src/server.o $(SERVER_OBJS)
+ $(CC) $(CFLAGS) -o $@ src/server.o $(SERVER_OBJS) $(LIB) $(LDFLAGS) $(CJSON_LDFLAGS)
# =============================================================================
# Tests (no model files required)
@@ -199,7 +204,10 @@ TEST_WORKSPACE_SRCS = tests/test_workspace.c $(CORE_SRCS) src/tokenizer_bpe.c
TEST_LATE_SRCS = tests/test_late.c $(CORE_SRCS) src/tokenizer_bpe.c
# test_server.c #includes ../src/server.c, so server is not a separate object
# here; it needs -I. for server.c's "deps/ae/ae.h" plus the ae sources.
+# test_server.c #includes ../src/server.c, so server.c itself is not listed;
+# the carved-out server objects (util/sbuf/base64) are separate TUs and must be.
TEST_SERVER_SRCS = tests/test_server.c $(CORE_SRCS) $(TOKENIZER_SRCS) \
+ $(SERVER_SRCS) \
deps/ae/ae.c deps/ae/anet.c deps/ae/monotonic.c
# The CLI check builds the real CLI (CPU backend) plus a driver that runs it.
TEST_CLI_BIN_SRCS = src/cli.c $(CORE_SRCS) $(TOKENIZER_SRCS)
diff --git a/src/base64.c b/src/base64.c
new file mode 100644
index 0000000..bc64f40
--- /dev/null
+++ b/src/base64.c
@@ -0,0 +1,23 @@
+#include "base64.h"
+#include "server_util.h"
+
+char *base64_encode(const unsigned char *src, size_t n) {
+ static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ size_t out_len = ((n + 2) / 3) * 4;
+ char *out = xmalloc(out_len + 1);
+ size_t o = 0;
+ for (size_t i = 0; i < n; i += 3) {
+ unsigned v = (unsigned)src[i] << 16;
+ int remain = (int)(n - i);
+ if (remain > 1)
+ v |= (unsigned)src[i + 1] << 8;
+ if (remain > 2)
+ v |= (unsigned)src[i + 2];
+ out[o++] = tbl[(v >> 18) & 63];
+ out[o++] = tbl[(v >> 12) & 63];
+ out[o++] = remain > 1 ? tbl[(v >> 6) & 63] : '=';
+ out[o++] = remain > 2 ? tbl[v & 63] : '=';
+ }
+ out[o] = '\0';
+ return out;
+}
diff --git a/src/base64.h b/src/base64.h
new file mode 100644
index 0000000..17e2036
--- /dev/null
+++ b/src/base64.h
@@ -0,0 +1,10 @@
+#ifndef EMBED_BASE64_H
+#define EMBED_BASE64_H
+
+#include <stddef.h>
+
+/* Standard base64 (with '=' padding) of n bytes. Returns a NUL-terminated heap
+ * string the caller frees; die-on-OOM, so it never returns NULL. */
+char *base64_encode(const unsigned char *src, size_t n);
+
+#endif /* EMBED_BASE64_H */
diff --git a/src/sbuf.c b/src/sbuf.c
new file mode 100644
index 0000000..3d77648
--- /dev/null
+++ b/src/sbuf.c
@@ -0,0 +1,61 @@
+#include "sbuf.h"
+#include "server_util.h"
+
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void sbuf_reserve(sbuf *b, size_t add) {
+ if (add > SIZE_MAX - b->len - 1)
+ die_oom();
+ size_t need = b->len + add + 1;
+ if (need <= b->cap)
+ return;
+ size_t cap = b->cap ? b->cap * 2 : 256;
+ while (cap < need) {
+ if (cap > SIZE_MAX / 2)
+ die_oom();
+ cap *= 2;
+ }
+ b->ptr = xrealloc(b->ptr, cap);
+ b->cap = cap;
+}
+
+void sbuf_append(sbuf *b, const void *p, size_t n) {
+ sbuf_reserve(b, n);
+ memcpy(b->ptr + b->len, p, n);
+ b->len += n;
+ b->ptr[b->len] = '\0';
+}
+
+void sbuf_putc(sbuf *b, char c) { sbuf_append(b, &c, 1); }
+void sbuf_puts(sbuf *b, const char *s) { sbuf_append(b, s, strlen(s)); }
+
+void sbuf_clear(sbuf *b) {
+ b->len = 0;
+ if (b->ptr)
+ b->ptr[0] = '\0';
+}
+
+void sbuf_printf(sbuf *b, const char *fmt, ...) {
+ va_list ap, ap2;
+ va_start(ap, fmt);
+ va_copy(ap2, ap);
+ int n = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+ if (n < 0)
+ die_oom();
+ sbuf_reserve(b, (size_t)n);
+ int n2 = vsnprintf(b->ptr + b->len, b->cap - b->len, fmt, ap2);
+ va_end(ap2);
+ if (n2 < 0 || n2 != n)
+ die_oom();
+ b->len += (size_t)n;
+}
+
+void sbuf_free(sbuf *b) {
+ free(b->ptr);
+ memset(b, 0, sizeof(*b));
+}
diff --git a/src/sbuf.h b/src/sbuf.h
new file mode 100644
index 0000000..0c8929e
--- /dev/null
+++ b/src/sbuf.h
@@ -0,0 +1,24 @@
+#ifndef EMBED_SBUF_H
+#define EMBED_SBUF_H
+
+#include <stddef.h>
+
+/* Growable, always-NUL-terminated byte buffer used to build HTTP responses and
+ * JSON bodies. Growth is die-on-OOM (see server_util.h), so the mutators never
+ * fail; callers do not check return values. */
+
+typedef struct {
+ char *ptr;
+ size_t len;
+ size_t cap;
+} sbuf;
+
+void sbuf_reserve(sbuf *b, size_t add);
+void sbuf_append(sbuf *b, const void *p, size_t n);
+void sbuf_putc(sbuf *b, char c);
+void sbuf_puts(sbuf *b, const char *s);
+void sbuf_clear(sbuf *b);
+void sbuf_printf(sbuf *b, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
+void sbuf_free(sbuf *b);
+
+#endif /* EMBED_SBUF_H */
diff --git a/src/server.c b/src/server.c
index 0305689..e3085ec 100644
--- a/src/server.c
+++ b/src/server.c
@@ -5,6 +5,9 @@
#include "build.h"
#include "server.h"
#include "server_internal.h"
+#include "server_util.h"
+#include "sbuf.h"
+#include "base64.h"
#include "embed.h"
#include "safetensors.h"
#include "kernels.h"
@@ -49,134 +52,6 @@
#include <cjson/cJSON.h>
-static void die_oom(void) {
- fprintf(stderr, "embed-server: out of memory\n");
- exit(1);
-}
-
-static void *xmalloc(size_t n) {
- void *p = malloc(n ? n : 1);
- if (!p)
- die_oom();
- return p;
-}
-
-static void *xcalloc(size_t n, size_t sz) {
- void *p = calloc(n ? n : 1, sz ? sz : 1);
- if (!p)
- die_oom();
- return p;
-}
-
-static void *xrealloc(void *p, size_t n) {
- p = realloc(p, n ? n : 1);
- if (!p)
- die_oom();
- return p;
-}
-
-static char *xstrdup(const char *s) {
- size_t n = strlen(s);
- char *p = xmalloc(n + 1);
- memcpy(p, s, n + 1);
- return p;
-}
-
-static char *xstrndup(const char *s, size_t n) {
- char *p = xmalloc(n + 1);
- memcpy(p, s, n);
- p[n] = '\0';
- return p;
-}
-
-static uint64_t mstime(void) {
- struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- return (uint64_t)ts.tv_sec * 1000u + (uint64_t)ts.tv_nsec / 1000000u;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (uint64_t)tv.tv_sec * 1000u + (uint64_t)tv.tv_usec / 1000u;
-}
-
-static uint64_t nstime(void) {
- struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (uint64_t)tv.tv_sec * 1000000000u + (uint64_t)tv.tv_usec * 1000u;
-}
-
-static double ns_to_ms(uint64_t ns) { return (double)ns / 1000000.0; }
-
-static void server_log(const char *fmt, ...) {
- char ts[32];
- time_t now = time(NULL);
- struct tm tmv;
- localtime_r(&now, &tmv);
- strftime(ts, sizeof(ts), "%m%d %H:%M:%S", &tmv);
- fprintf(stderr, "%s ", ts);
- va_list ap;
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fputc('\n', stderr);
-}
-
-static void sbuf_reserve(sbuf *b, size_t add) {
- if (add > SIZE_MAX - b->len - 1)
- die_oom();
- size_t need = b->len + add + 1;
- if (need <= b->cap)
- return;
- size_t cap = b->cap ? b->cap * 2 : 256;
- while (cap < need) {
- if (cap > SIZE_MAX / 2)
- die_oom();
- cap *= 2;
- }
- b->ptr = xrealloc(b->ptr, cap);
- b->cap = cap;
-}
-
-static void sbuf_append(sbuf *b, const void *p, size_t n) {
- sbuf_reserve(b, n);
- memcpy(b->ptr + b->len, p, n);
- b->len += n;
- b->ptr[b->len] = '\0';
-}
-
-static void sbuf_putc(sbuf *b, char c) { sbuf_append(b, &c, 1); }
-static void sbuf_puts(sbuf *b, const char *s) { sbuf_append(b, s, strlen(s)); }
-
-static void sbuf_clear(sbuf *b) {
- b->len = 0;
- if (b->ptr)
- b->ptr[0] = '\0';
-}
-
-static void sbuf_printf(sbuf *b, const char *fmt, ...) {
- va_list ap, ap2;
- va_start(ap, fmt);
- va_copy(ap2, ap);
- int n = vsnprintf(NULL, 0, fmt, ap);
- va_end(ap);
- if (n < 0)
- die_oom();
- sbuf_reserve(b, (size_t)n);
- int n2 = vsnprintf(b->ptr + b->len, b->cap - b->len, fmt, ap2);
- va_end(ap2);
- if (n2 < 0 || n2 != n)
- die_oom();
- b->len += (size_t)n;
-}
-
-static void sbuf_free(sbuf *b) {
- free(b->ptr);
- memset(b, 0, sizeof(*b));
-}
-
-
static const model_info k_models[] = {
{"pplx-embed-v1-0.6b", MODEL_KIND_STANDARD, 1024, 128, 0, EMBED_ATTENTION_BIDIRECTIONAL,
EMBED_POOL_MEAN, 0, EMBED_API_PERPLEXITY, NULL},
@@ -219,7 +94,6 @@ static model_slot model_slot_for_id(const char *id) {
return MODEL_UNKNOWN;
}
-
static int queue_write(client *c);
static void dispatch_request(client *c);
static void read_cb(aeEventLoop *loop, int fd, void *clientData, int mask);
@@ -1115,27 +989,6 @@ dimensions_from_root(cJSON *root, cJSON *detail, int min_dim, int max_dim, const
return dims;
}
-static char *base64_encode(const unsigned char *src, size_t n) {
- static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- size_t out_len = ((n + 2) / 3) * 4;
- char *out = xmalloc(out_len + 1);
- size_t o = 0;
- for (size_t i = 0; i < n; i += 3) {
- unsigned v = (unsigned)src[i] << 16;
- int remain = (int)(n - i);
- if (remain > 1)
- v |= (unsigned)src[i + 1] << 8;
- if (remain > 2)
- v |= (unsigned)src[i + 2];
- out[o++] = tbl[(v >> 18) & 63];
- out[o++] = tbl[(v >> 12) & 63];
- out[o++] = remain > 1 ? tbl[(v >> 6) & 63] : '=';
- out[o++] = remain > 2 ? tbl[v & 63] : '=';
- }
- out[o] = '\0';
- return out;
-}
-
static signed char quantize_int8_tanh(float x) {
int v = (int)lrintf(tanhf(x) * 127.0f);
if (v > 127)
diff --git a/src/server_internal.h b/src/server_internal.h
index c72831e..9aaa5d8 100644
--- a/src/server_internal.h
+++ b/src/server_internal.h
@@ -8,6 +8,7 @@
* the CLI and library do not include it. */
#include "embed.h"
+#include "sbuf.h"
#include "tokenizer_bpe.h"
#include "tokenizer_wordpiece.h"
@@ -67,12 +68,6 @@
#define EMBED_MLX_MEMORY_BUDGET_PERCENT 90
#define EMBED_MLX_RESIDENT_MULTIPLIER 2
-typedef struct {
- char *ptr;
- size_t len;
- size_t cap;
-} sbuf;
-
typedef enum {
MODEL_STD_06,
MODEL_STD_4,
diff --git a/src/server_util.c b/src/server_util.c
new file mode 100644
index 0000000..bc20804
--- /dev/null
+++ b/src/server_util.c
@@ -0,0 +1,82 @@
+#include "server_util.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
+
+void die_oom(void) {
+ fprintf(stderr, "embed-server: out of memory\n");
+ exit(1);
+}
+
+void *xmalloc(size_t n) {
+ void *p = malloc(n ? n : 1);
+ if (!p)
+ die_oom();
+ return p;
+}
+
+void *xcalloc(size_t n, size_t sz) {
+ void *p = calloc(n ? n : 1, sz ? sz : 1);
+ if (!p)
+ die_oom();
+ return p;
+}
+
+void *xrealloc(void *p, size_t n) {
+ p = realloc(p, n ? n : 1);
+ if (!p)
+ die_oom();
+ return p;
+}
+
+char *xstrdup(const char *s) {
+ size_t n = strlen(s);
+ char *p = xmalloc(n + 1);
+ memcpy(p, s, n + 1);
+ return p;
+}
+
+char *xstrndup(const char *s, size_t n) {
+ char *p = xmalloc(n + 1);
+ memcpy(p, s, n);
+ p[n] = '\0';
+ return p;
+}
+
+uint64_t mstime(void) {
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
+ return (uint64_t)ts.tv_sec * 1000u + (uint64_t)ts.tv_nsec / 1000000u;
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (uint64_t)tv.tv_sec * 1000u + (uint64_t)tv.tv_usec / 1000u;
+}
+
+uint64_t nstime(void) {
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
+ return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (uint64_t)tv.tv_sec * 1000000000u + (uint64_t)tv.tv_usec * 1000u;
+}
+
+double ns_to_ms(uint64_t ns) { return (double)ns / 1000000.0; }
+
+void server_log(const char *fmt, ...) {
+ char ts[32];
+ time_t now = time(NULL);
+ struct tm tmv;
+ localtime_r(&now, &tmv);
+ strftime(ts, sizeof(ts), "%m%d %H:%M:%S", &tmv);
+ fprintf(stderr, "%s ", ts);
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
diff --git a/src/server_util.h b/src/server_util.h
new file mode 100644
index 0000000..955903c
--- /dev/null
+++ b/src/server_util.h
@@ -0,0 +1,36 @@
+#ifndef EMBED_SERVER_UTIL_H
+#define EMBED_SERVER_UTIL_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* Die-on-OOM allocation, monotonic clocks, and timestamped logging shared by
+ * the embed-server translation units. The x* allocators abort the process on
+ * failure rather than returning NULL, so callers never null-check them; the
+ * attributes below carry that contract across translation-unit boundaries so
+ * the static analyzer does not flag the (correctly) unchecked uses. */
+
+#if defined(__GNUC__) || defined(__clang__)
+#define SERVER_NORETURN __attribute__((noreturn))
+#define SERVER_RETURNS_NONNULL __attribute__((returns_nonnull))
+#else
+#define SERVER_NORETURN
+#define SERVER_RETURNS_NONNULL
+#endif
+
+SERVER_NORETURN void die_oom(void);
+
+SERVER_RETURNS_NONNULL void *xmalloc(size_t n);
+SERVER_RETURNS_NONNULL void *xcalloc(size_t n, size_t sz);
+SERVER_RETURNS_NONNULL void *xrealloc(void *p, size_t n);
+SERVER_RETURNS_NONNULL char *xstrdup(const char *s);
+SERVER_RETURNS_NONNULL char *xstrndup(const char *s, size_t n);
+
+uint64_t mstime(void);
+uint64_t nstime(void);
+double ns_to_ms(uint64_t ns);
+
+/* printf-style line logger: timestamp prefix, newline appended, to stderr. */
+void server_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
+
+#endif /* EMBED_SERVER_UTIL_H */