Skip to content

Commit

Permalink
filter: fix ndb_filter_init_with and make public
Browse files Browse the repository at this point in the history
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")
  • Loading branch information
jb55 committed Jan 13, 2025
1 parent 3d24351 commit 9a3b613
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,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);
Expand Down

0 comments on commit 9a3b613

Please sign in to comment.