nostrdb: filter: fix ndb_filter_init_with and make public
This fixes an allocation issue with ndb_filter_init_with for small
page sizes. instead of allocating the buffer around pages, we allocate
based on total buffer size.
Fixes: f7aac3215575 ("filter: introduce ndb_filter_init_with")
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
ec798bdeb2
commit
d3496af5cc
@@ -685,15 +685,16 @@ ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index)
|
||||
return els->elements[index];
|
||||
}
|
||||
|
||||
static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
|
||||
int ndb_filter_init_with(struct ndb_filter *filter, int pages)
|
||||
{
|
||||
struct cursor cur;
|
||||
int page_size, elem_pages, data_pages, buf_size;
|
||||
int page_size, elem_size, data_size, buf_size;
|
||||
|
||||
page_size = 4096; // assuming this, not a big deal if we're wrong
|
||||
elem_pages = pages / 4;
|
||||
data_pages = pages - elem_pages;
|
||||
|
||||
buf_size = page_size * pages;
|
||||
elem_size = buf_size / 4;
|
||||
data_size = buf_size - elem_size;
|
||||
|
||||
unsigned char *buf = malloc(buf_size);
|
||||
if (!buf)
|
||||
@@ -702,8 +703,8 @@ static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
|
||||
// init memory arena for the cursor
|
||||
make_cursor(buf, buf + buf_size, &cur);
|
||||
|
||||
cursor_slice(&cur, &filter->elem_buf, page_size * elem_pages);
|
||||
cursor_slice(&cur, &filter->data_buf, page_size * data_pages);
|
||||
cursor_slice(&cur, &filter->elem_buf, elem_size);
|
||||
cursor_slice(&cur, &filter->data_buf, data_size);
|
||||
|
||||
// make sure we are fully allocated
|
||||
assert(cur.p == cur.end);
|
||||
|
||||
@@ -503,6 +503,12 @@ int ndb_builder_push_tag_str(struct ndb_builder *builder, const char *str, int l
|
||||
|
||||
// FILTERS
|
||||
int ndb_filter_init(struct ndb_filter *);
|
||||
|
||||
/// Allocate a filter with a fixed sized buffer (where pages is number of 4096-byte sized blocks)
|
||||
/// You can set pages to 1 if you know you are constructing small filters
|
||||
// TODO: replace this with passed-in buffers
|
||||
int ndb_filter_init_with(struct ndb_filter *filter, int pages);
|
||||
|
||||
int ndb_filter_add_id_element(struct ndb_filter *, const unsigned char *id);
|
||||
int ndb_filter_add_int_element(struct ndb_filter *, uint64_t integer);
|
||||
int ndb_filter_add_str_element(struct ndb_filter *, const char *str);
|
||||
|
||||
Reference in New Issue
Block a user