Skip to content

Commit 9a3b613

Browse files
committed
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: f7aac32 ("filter: introduce ndb_filter_init_with")
1 parent 3d24351 commit 9a3b613

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/nostrdb.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,16 @@ ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index)
685685
return els->elements[index];
686686
}
687687

688-
static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
688+
int ndb_filter_init_with(struct ndb_filter *filter, int pages)
689689
{
690690
struct cursor cur;
691-
int page_size, elem_pages, data_pages, buf_size;
691+
int page_size, elem_size, data_size, buf_size;
692692

693693
page_size = 4096; // assuming this, not a big deal if we're wrong
694-
elem_pages = pages / 4;
695-
data_pages = pages - elem_pages;
694+
696695
buf_size = page_size * pages;
696+
elem_size = buf_size / 4;
697+
data_size = buf_size - elem_size;
697698

698699
unsigned char *buf = malloc(buf_size);
699700
if (!buf)
@@ -702,8 +703,8 @@ static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
702703
// init memory arena for the cursor
703704
make_cursor(buf, buf + buf_size, &cur);
704705

705-
cursor_slice(&cur, &filter->elem_buf, page_size * elem_pages);
706-
cursor_slice(&cur, &filter->data_buf, page_size * data_pages);
706+
cursor_slice(&cur, &filter->elem_buf, elem_size);
707+
cursor_slice(&cur, &filter->data_buf, data_size);
707708

708709
// make sure we are fully allocated
709710
assert(cur.p == cur.end);

src/nostrdb.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,12 @@ int ndb_builder_push_tag_str(struct ndb_builder *builder, const char *str, int l
501501

502502
// FILTERS
503503
int ndb_filter_init(struct ndb_filter *);
504+
505+
/// Allocate a filter with a fixed sized buffer (where pages is number of 4096-byte sized blocks)
506+
/// You can set pages to 1 if you know you are constructing small filters
507+
// TODO: replace this with passed-in buffers
508+
int ndb_filter_init_with(struct ndb_filter *filter, int pages);
509+
504510
int ndb_filter_add_id_element(struct ndb_filter *, const unsigned char *id);
505511
int ndb_filter_add_int_element(struct ndb_filter *, uint64_t integer);
506512
int ndb_filter_add_str_element(struct ndb_filter *, const char *str);

0 commit comments

Comments
 (0)