From 9bb2610e5f17057336dcdcbf5106cba01066e87b Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 24 May 2026 01:53:25 -0400 Subject: [PATCH] mvm: Add initial mvm groundwork Signed-off-by: Ian Moffett --- mvm/.gitignore | 1 + mvm/Makefile | 25 +++++++++++++ mvm/core/balloon_subr.c | 75 +++++++++++++++++++++++++++++++++++++++ mvm/core/mvm.c | 38 ++++++++++++++++++++ mvm/include/mvm/balloon.h | 53 +++++++++++++++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 mvm/.gitignore create mode 100644 mvm/Makefile create mode 100644 mvm/core/balloon_subr.c create mode 100644 mvm/core/mvm.c create mode 100644 mvm/include/mvm/balloon.h diff --git a/mvm/.gitignore b/mvm/.gitignore new file mode 100644 index 0000000..27b60e2 --- /dev/null +++ b/mvm/.gitignore @@ -0,0 +1 @@ +mvm diff --git a/mvm/Makefile b/mvm/Makefile new file mode 100644 index 0000000..3203859 --- /dev/null +++ b/mvm/Makefile @@ -0,0 +1,25 @@ +# +# Copyright (c) 2026 Mirocom Laboratories and MSP engineers. +# All Rights Reserved. +# + +CFILES = $(shell find core/ -name "*.c") +OFILES = $(CFILES:.c=.o) +DFILES = $(CFILES:.c=.d) +CC = gcc + +CFLAGS = \ + -Wall \ + -pedantic \ + -MMD \ + -Iinclude + +.PHONY: all +all: mvm + +.PHONY: mvm +mvm: $(OFILES) + $(CC) $^ -o $@ + +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ diff --git a/mvm/core/balloon_subr.c b/mvm/core/balloon_subr.c new file mode 100644 index 0000000..68ab7b8 --- /dev/null +++ b/mvm/core/balloon_subr.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2026 Mirocom Laboratories and MSP engineers. + * All Rights Reserved. + */ + +#include +#include +#include +#include +#include +#include +#include "mvm/balloon.h" + +int +balloon_init(struct mvm_balloon *bp, size_t cap) +{ + if (bp == NULL || cap == 0) { + errno = EINVAL; + return -1; + } + + assert(cap > BALLOON_INIT_SIZE && "cap must be bigger than minimum"); + bp->cap = cap; + bp->size = BALLOON_INIT_SIZE; + bp->data = calloc(bp->size, sizeof(char)); + + if (bp->data == NULL) { + errno = ENOMEM; + return -1; + } + + return 0; +} + +int +balloon_memcpy(struct mvm_balloon *dest, off_t off, void *source, size_t count) +{ + size_t overflow, newsize; + size_t delta; + void *p; + + if (dest == NULL || source == NULL) { + errno = EINVAL; + return -1; + } + + if (count == 0) { + errno = EINVAL; + return -1; + } + + /* Compute how far off this will push us */ + overflow = off + count; + + /* If we are overflowing size and have room to expand, do it */ + if (overflow >= dest->size && dest->size < dest->cap) { + delta = overflow - dest->size; + newsize = dest->size + delta; + + /* Attempt to resize the pool */ + if ((p = realloc(dest->data, newsize)) == NULL) { + errno = ENOMEM; + return -1; + } + + dest->size = newsize; + dest->data = p; + } else if (overflow >= dest->cap) { + errno = ENOSPC; + return -1; + } + + memcpy((char *)dest->data + off, source, count); + return 0; +} diff --git a/mvm/core/mvm.c b/mvm/core/mvm.c new file mode 100644 index 0000000..10d5c49 --- /dev/null +++ b/mvm/core/mvm.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2026 Mirocom Laboratories and MSP engineers. + * All Rights Reserved. + */ + +#include +#include +#include +#include "mvm/balloon.h" + +static void +help(void) +{ + printf("usage: ./mvm [flags]\n"); + printf("[-h] Display this help menu\n"); +} + +int +main(int argc, char **argv) +{ + int opt; + + if (argc < 2) { + printf("fatal: too few arguments\n"); + help(); + return -1; + } + + while ((opt = getopt(argc, argv, "h")) != -1) { + switch (opt) { + case 'h': + help(); + return -1; + } + } + + return 0; +} diff --git a/mvm/include/mvm/balloon.h b/mvm/include/mvm/balloon.h new file mode 100644 index 0000000..26a2609 --- /dev/null +++ b/mvm/include/mvm/balloon.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2026 Mirocom Laboratories and MSP engineers. + * All Rights Reserved. + */ + +#ifndef MVM_BALLOON_H +#define MVM_BALLOON_H 1 + +#include +#include +#include + +/* Initialize balloon size */ +#define BALLOON_INIT_SIZE 8 + +/* + * A balloon is a region of memory that is capped at a specific + * maximum size and starts small before gradually growing as more + * data is added. + * + * @data: Allocated data pool + * @cap: Maximum capacity + * @size: Current size + */ +struct mvm_balloon { + void *data; + size_t cap; + size_t size; +}; + +/* + * Create a new balloon + * + * @bp: Balloon to create + * @cap: Maximum capacity of balloon + * + * Returns zero on success + */ +int balloon_init(struct mvm_balloon *bp, size_t cap); + +/* + * Copy a buffer into a balloon + * + * @dest: Destination balloon + * @off: Offset to write to + * @source: Source buffer + * @count: Number of bytes to copy + * + * Returns zero on success + */ +int balloon_memcpy(struct mvm_balloon *dest, off_t off, void *source, size_t count); + +#endif /* !MVM_BALLOON_H */