@@ -48,19 +48,17 @@ typedef struct YR_NOTEBOOK_PAGE YR_NOTEBOOK_PAGE;
4848// all the buffers allocated via yr_notebook_alloc().
4949struct YR_NOTEBOOK
5050{
51- // Size of pages in the notebook. Most pages are this size, but some
52- // of them can be 2x, 3x, or in general Nx this size. This happens when
53- // yr_notebook_alloc is called with a size that is larger than page_size,
54- // which means that the notebook needs to allocate a page that is larger
55- // than the rest for accomodating the requested buffer.
56- size_t page_size ;
51+ // The mininum size of each page in the notebook.
52+ size_t min_page_size ;
5753 // Pointer to the first page in the book, this is also the most recently
5854 // created page, the one that is being filled.
5955 YR_NOTEBOOK_PAGE * page_list_head ;
6056};
6157
6258struct YR_NOTEBOOK_PAGE
6359{
60+ // Size of this page.
61+ size_t size ;
6462 // Amount of bytes in the page that are actually used.
6563 size_t used ;
6664 // Pointer to next page.
@@ -81,23 +79,24 @@ struct YR_NOTEBOOK_PAGE
8179// ERROR_SUCCESS
8280// ERROR_INSUFFICIENT_MEMORY
8381//
84- int yr_notebook_create (size_t page_size , YR_NOTEBOOK * * notebook )
82+ int yr_notebook_create (size_t min_page_size , YR_NOTEBOOK * * notebook )
8583{
8684 YR_NOTEBOOK * new_notebook = yr_malloc (sizeof (YR_NOTEBOOK ));
8785
8886 if (new_notebook == NULL )
8987 return ERROR_INSUFFICIENT_MEMORY ;
9088
9189 new_notebook -> page_list_head = yr_malloc (
92- sizeof (YR_NOTEBOOK_PAGE ) + page_size );
90+ sizeof (YR_NOTEBOOK_PAGE ) + min_page_size );
9391
9492 if (new_notebook -> page_list_head == NULL )
9593 {
9694 yr_free (new_notebook );
9795 return ERROR_INSUFFICIENT_MEMORY ;
9896 }
9997
100- new_notebook -> page_size = page_size ;
98+ new_notebook -> min_page_size = min_page_size ;
99+ new_notebook -> page_list_head -> size = min_page_size ;
101100 new_notebook -> page_list_head -> used = 0 ;
102101 new_notebook -> page_list_head -> next = NULL ;
103102
@@ -151,20 +150,26 @@ void* yr_notebook_alloc(YR_NOTEBOOK* notebook, size_t size)
151150 // deferrencing pointers to types larger than a byte.
152151 size = (size + 7 ) & ~0x7 ;
153152
153+ YR_NOTEBOOK_PAGE * current_page = notebook -> page_list_head ;
154+
154155 // If the requested size doesn't fit in current page's free space, allocate
155156 // a new page.
156- if (notebook -> page_size - notebook -> page_list_head -> used < size )
157+ if (current_page -> size - current_page -> used < size )
157158 {
159+ size_t min_size = notebook -> min_page_size ;
160+
158161 // The new page must be able to fit the requested buffer, so find the
159- // multiple of notebook->page_size that is larger than size.
160- size_t page_size = (size / notebook -> page_size + 1 ) * notebook -> page_size ;
162+ // multiple of notebook->min_page_size that is larger or equal than than
163+ // size.
164+ size_t page_size = (size / min_size ) * min_size + min_size ;
161165
162166 YR_NOTEBOOK_PAGE * new_page = yr_malloc (
163167 sizeof (YR_NOTEBOOK_PAGE ) + page_size );
164168
165169 if (new_page == NULL )
166170 return NULL ;
167171
172+ new_page -> size = page_size ;
168173 new_page -> used = 0 ;
169174 new_page -> next = notebook -> page_list_head ;
170175 notebook -> page_list_head = new_page ;
0 commit comments