mvm: Add initial mvm groundwork
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user