Browse Source

Refactor library code.

primary
CismonX 4 months ago
parent
commit
5d13a15994
Signed by: CismonX GPG Key ID: 3094873E29A482FB
  1. 41
      src/ctlseqs.c

41
src/ctlseqs.c

@ -26,7 +26,6 @@
#include "ctlseqs.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -417,18 +416,15 @@ ctlseqs_reader_match(struct ctlseqs_reader *reader, struct ctlseqs_matcher const
struct ctlseqs_matcher *
ctlseqs_matcher_init()
{
struct ctlseqs_trie_node *pool = malloc(sizeof(struct ctlseqs_trie_node) * CTLSEQS_TRIE_NODE_POOL_INIT_SIZE);
struct ctlseqs_matcher *matcher = malloc(sizeof(struct ctlseqs_matcher));
if (ctlseqs_unlikely(pool == NULL || matcher == NULL)) {
free(pool);
free(matcher);
return NULL;
struct ctlseqs_matcher *mem = malloc(sizeof(struct ctlseqs_matcher) +
sizeof(struct ctlseqs_trie_node) * CTLSEQS_TRIE_NODE_POOL_INIT_SIZE);
if (ctlseqs_likely(mem != NULL)) {
*mem = (struct ctlseqs_matcher) {
.node_pools = { (struct ctlseqs_trie_node *)(mem + 1) },
.pool_size = CTLSEQS_TRIE_NODE_POOL_INIT_SIZE
};
}
*matcher = (struct ctlseqs_matcher) {
.node_pools = { pool },
.pool_size = CTLSEQS_TRIE_NODE_POOL_INIT_SIZE
};
return matcher;
return mem;
}
int
@ -456,7 +452,7 @@ ctlseqs_matcher_config(struct ctlseqs_matcher *matcher, struct ctlseqs_matcher_o
continue;
}
if (ctlseqs_unlikely(++node_idx >= matcher->pool_size)) {
if (ctlseqs_unlikely(++matcher->pool_idx >= CTLSEQS_TRIE_NODE_POOL_MAX_NUM)) {
if (ctlseqs_unlikely(matcher->pool_idx >= CTLSEQS_TRIE_NODE_POOL_MAX_NUM - 1)) {
return CTLSEQS_NOMEM;
}
node_pool = malloc(sizeof(struct ctlseqs_trie_node) * matcher->pool_size * 2);
@ -464,7 +460,7 @@ ctlseqs_matcher_config(struct ctlseqs_matcher *matcher, struct ctlseqs_matcher_o
return CTLSEQS_NOMEM;
}
node_idx = 0;
matcher->node_pools[matcher->pool_idx] = node_pool;
matcher->node_pools[++matcher->pool_idx] = node_pool;
matcher->pool_size *= 2;
}
old_node->children[ch] = node = node_pool + node_idx;
@ -513,8 +509,8 @@ ctlseqs_match(struct ctlseqs_matcher const *matcher, char const *str, size_t str
ctlseqs_cold void
ctlseqs_matcher_free(struct ctlseqs_matcher *matcher)
{
if (matcher != NULL) {
for (size_t idx = 0; idx <= matcher->pool_idx; ++idx) {
if (ctlseqs_likely(matcher != NULL)) {
for (size_t idx = 1; idx <= matcher->pool_idx; ++idx) {
free(matcher->node_pools[idx]);
}
free(matcher);
@ -534,13 +530,12 @@ ctlseqs_reader_init()
int
ctlseqs_reader_config(struct ctlseqs_reader *reader, struct ctlseqs_reader_options const *options)
{
size_t readlen = options->maxlen;
size_t const readlen = options->maxlen;
if (reader->readlen != readlen) {
char *rbuf;
if (readlen < reader->buf_end) {
return CTLSEQS_ERROR;
}
rbuf = realloc(reader->rbuf, readlen);
char *rbuf = realloc(reader->rbuf, readlen);
if (rbuf == NULL) {
return CTLSEQS_NOMEM;
}
@ -550,9 +545,9 @@ ctlseqs_reader_config(struct ctlseqs_reader *reader, struct ctlseqs_reader_optio
reader->buf_end = reader->readlen;
}
}
reader->result = options->result;
reader->pollfd.fd = options->fd;
reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL;
reader->result = options->result;
reader->pollfd.fd = options->fd;
reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL;
reader->save_matched = options->flags & CTLSEQS_READER_SAVE_MATCHED_SEQS;
return CTLSEQS_OK;
}
@ -599,7 +594,7 @@ ctlseqs_purge(struct ctlseqs_reader *reader, size_t nbytes)
ctlseqs_cold void
ctlseqs_reader_free(struct ctlseqs_reader *reader)
{
if (reader != NULL) {
if (ctlseqs_likely(reader != NULL)) {
free(reader->rbuf);
free(reader);
}

Loading…
Cancel
Save