mvm: Add initial mvm groundwork
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
mvm
|
||||||
@@ -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 $@
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Mirocom Laboratories and MSP engineers.
|
||||||
|
* All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Mirocom Laboratories and MSP engineers.
|
||||||
|
* All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -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 <sys/types.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* 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 */
|
||||||
Reference in New Issue
Block a user