44 lines
846 B
C
44 lines
846 B
C
|
#include <stdlib.h>
|
||
|
#include <merror.h>
|
||
|
|
||
|
#include "tab.h"
|
||
|
|
||
|
#define REFTAB_INIT_LEN 8
|
||
|
|
||
|
int reftab_init(struct reference_table *reftab)
|
||
|
{
|
||
|
reftab->size = REFTAB_INIT_LEN;
|
||
|
reftab->len = 0;
|
||
|
reftab->references = malloc(sizeof(struct reference)
|
||
|
* REFTAB_INIT_LEN);
|
||
|
|
||
|
if (reftab->references == NULL) {
|
||
|
PERROR("cannot alloc");
|
||
|
return M_ERROR;
|
||
|
}
|
||
|
|
||
|
return M_SUCCESS;
|
||
|
}
|
||
|
|
||
|
void reftab_free(struct reference_table *reftab)
|
||
|
{
|
||
|
free(reftab->references);
|
||
|
}
|
||
|
|
||
|
int reftab_push(struct reference_table *reftab, struct reference *ref)
|
||
|
{
|
||
|
if (reftab->len >= reftab->size) {
|
||
|
reftab->size *= 2;
|
||
|
reftab->references = realloc(reftab->references,
|
||
|
sizeof(struct reference) * reftab->size);
|
||
|
|
||
|
if (reftab->references == NULL) {
|
||
|
PERROR("cannot realloc");
|
||
|
return M_ERROR;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
reftab->references[reftab->len++] = *ref;
|
||
|
return M_SUCCESS;
|
||
|
}
|