core: tokbuf: Dynamically expand token buffer
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -48,4 +48,5 @@ state_close(struct cescal_state *state)
|
|||||||
close(state->in_fd);
|
close(state->in_fd);
|
||||||
state->in_fd = -1;
|
state->in_fd = -1;
|
||||||
ptrbox_destroy(&state->ptrbox);
|
ptrbox_destroy(&state->ptrbox);
|
||||||
|
tokbuf_destroy(&state->tokbuf);
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-3
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -18,20 +19,33 @@ tokbuf_init(struct tokbuf *tokbuf)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(tokbuf->buf, 0, sizeof(tokbuf->buf));
|
|
||||||
tokbuf->head = 0;
|
tokbuf->head = 0;
|
||||||
|
tokbuf->cap = TOKBUF_CAP;
|
||||||
|
tokbuf->buf = malloc(sizeof(struct token) * TOKBUF_CAP);
|
||||||
|
if (tokbuf->buf == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
|
tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
|
||||||
{
|
{
|
||||||
|
void *p;
|
||||||
|
|
||||||
if (tokbuf == NULL || tok == NULL) {
|
if (tokbuf == NULL || tok == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((tokbuf->head++) >= TOKBUF_CAP - 1) {
|
if ((tokbuf->head++) >= tokbuf->cap - 1) {
|
||||||
tokbuf->head = 0;
|
tokbuf->cap += 8;
|
||||||
|
p = realloc(tokbuf->buf, sizeof(struct token) * tokbuf->cap);
|
||||||
|
if (tokbuf->buf == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokbuf->buf = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
tokbuf->buf[tokbuf->head] = *tok;
|
tokbuf->buf[tokbuf->head] = *tok;
|
||||||
@@ -60,3 +74,16 @@ tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res)
|
|||||||
*res = tokbuf->buf[off];
|
*res = tokbuf->buf[off];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
tokbuf_destroy(struct tokbuf *tokbuf)
|
||||||
|
{
|
||||||
|
if (tokbuf == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tokbuf->buf != NULL) {
|
||||||
|
free(tokbuf->buf);
|
||||||
|
tokbuf->buf = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,7 +13,8 @@
|
|||||||
#define TOKBUF_CAP 4
|
#define TOKBUF_CAP 4
|
||||||
|
|
||||||
struct tokbuf {
|
struct tokbuf {
|
||||||
struct token buf[TOKBUF_CAP];
|
struct token *buf;
|
||||||
|
size_t cap;
|
||||||
uint8_t head;
|
uint8_t head;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -47,4 +48,11 @@ int tokbuf_push(struct tokbuf *tokbuf, struct token *tok);
|
|||||||
*/
|
*/
|
||||||
int tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res);
|
int tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Destroy a token buffer
|
||||||
|
*
|
||||||
|
* @tokbuf: Token buffer to destroy
|
||||||
|
*/
|
||||||
|
void tokbuf_destroy(struct tokbuf *tokbuf);
|
||||||
|
|
||||||
#endif /* !CESCAL_TOKBUF_H */
|
#endif /* !CESCAL_TOKBUF_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user