46 lines
1.5 KiB
C
46 lines
1.5 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_class(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++;
|
|
}
|
|
|
|
const PcktClassDesc * packreg_get_by_class(const PcktRegistry * packReg, uint16_t ownClass, uint16_t containerClass) {
|
|
uint32_t i;
|
|
for (i = 0; i < packReg->entCnt; i++) {
|
|
const PcktClassDesc * cdesc = packReg->ents + i;
|
|
if ((cdesc->class == ownClass) && ((cdesc->containerClass == containerClass) || (containerClass == 0))) {
|
|
return cdesc;
|
|
}
|
|
}
|
|
return NULL;
|
|
} |