EtherLib/packet_registry.c

39 lines
1.2 KiB
C

#include "packet_registry.h"
#include "global_state.h"
#include "dynmem.h"
#include "utils.h"
#include <stddef.h>
#include <memory.h>
#define PCKTREG_INITIAL_RESERVED_CNT (6)
PcktRegistry * packreg_new() {
PcktRegistry * packReg = (PcktRegistry *)dynmem_alloc(sizeof(PcktRegistry));
ASSERT_NULL(packReg);
packReg->reservedCnt = PCKTREG_INITIAL_RESERVED_CNT;
packReg->ents = (PcktClassDesc *) dynmem_alloc(sizeof(PcktClassDesc) * packReg->reservedCnt);
ASSERT_NULL(packReg->ents);
packReg->entCnt = 0;
return packReg;
}
void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc) {
// if buffer is full and reallocation and resize is needed
if (packReg->reservedCnt == packReg->entCnt) {
PcktClassDesc * newEnts = (PcktClassDesc *) dynmem_alloc(packReg->reservedCnt * 2);
ASSERT_NULL(newEnts);
memcpy(newEnts, packReg->ents, packReg->reservedCnt * sizeof(PcktClassDesc));
PcktClassDesc * oldEnts = packReg->ents;
packReg->ents = newEnts;
dynmem_free(oldEnts);
}
// append new item
packReg->ents[packReg->entCnt] = *classDesc;
packReg->entCnt++;
}
void packreg_get_by_class(uint16_t class) {
}