Add blocks API

isolation_bkp/dynres
Dominik Madarász 2021-01-11 15:27:32 +01:00
parent f0713d7fc1
commit a6761bf271
2 changed files with 58 additions and 0 deletions

View File

@ -1,3 +1,14 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
#define BLOCKS_ERROR_NONE +0x0000
#define BLOCKS_ERROR_OUTOFMEM -0x0001
#define BLOCKS_ERROR_NOTFOUND -0x0002
#define BLOCKS_ERROR_INVALID -0x0003
int32_t blocks_init(void);
int32_t blocks_destroy(void);
// persisting buffer
char *blocks_get_name(uint32_t id);
uint32_t blocks_get_flags(uint32_t id);

View File

@ -1,3 +1,50 @@
#include "blocks.h" #include "blocks.h"
#include "zpl.h"
// todo: csv parsing + utils // todo: csv parsing + utils
#define BLOCK_NAMELEN 80
typedef struct {
uint32_t id;
char name[BLOCK_NAMELEN];
uint32_t flags;
} block;
static block *blocks = NULL;
static uint32_t blocks_count = 0;
int blocks_comparer(void const *a, void const *b) {
block *ba = (block*)a;
uint32_t bb = *(uint32_t*)b;
return ba->id < bb ? -1 : ba->id > bb;
}
static block *blocks_find(uint32_t id) {
ZPL_ASSERT_NOT_NULL(blocks);
int32_t index = zpl_binary_search((void*)blocks, blocks_count, sizeof(block), (void*)&id, blocks_comparer);
ZPL_ASSERT_MSG(index != -1, "block could not be found");
return &blocks[index];
}
int32_t blocks_init(void) {
// todo read csv by lines, linecount-1 == blocks_count
// preallocate and assign values
return BLOCKS_ERROR_NONE;
}
int32_t blocks_destroy(void) {
ZPL_ASSERT_NOT_NULL(blocks);
zpl_mfree(blocks);
return BLOCKS_ERROR_NONE;
}
char *blocks_get_name(uint32_t id) {
ZPL_ASSERT_NOT_NULL(blocks);
return blocks_find(id)->name;
}
uint32_t blocks_get_flags(uint32_t id) {
ZPL_ASSERT_NOT_NULL(blocks);
return blocks_find(id)->flags;
}