@@ -48,19 +48,17 @@ typedef struct YR_NOTEBOOK_PAGE YR_NOTEBOOK_PAGE;
48
48
// all the buffers allocated via yr_notebook_alloc().
49
49
struct YR_NOTEBOOK
50
50
{
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 ;
57
53
// Pointer to the first page in the book, this is also the most recently
58
54
// created page, the one that is being filled.
59
55
YR_NOTEBOOK_PAGE * page_list_head ;
60
56
};
61
57
62
58
struct YR_NOTEBOOK_PAGE
63
59
{
60
+ // Size of this page.
61
+ size_t size ;
64
62
// Amount of bytes in the page that are actually used.
65
63
size_t used ;
66
64
// Pointer to next page.
@@ -81,23 +79,24 @@ struct YR_NOTEBOOK_PAGE
81
79
// ERROR_SUCCESS
82
80
// ERROR_INSUFFICIENT_MEMORY
83
81
//
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 )
85
83
{
86
84
YR_NOTEBOOK * new_notebook = yr_malloc (sizeof (YR_NOTEBOOK ));
87
85
88
86
if (new_notebook == NULL )
89
87
return ERROR_INSUFFICIENT_MEMORY ;
90
88
91
89
new_notebook -> page_list_head = yr_malloc (
92
- sizeof (YR_NOTEBOOK_PAGE ) + page_size );
90
+ sizeof (YR_NOTEBOOK_PAGE ) + min_page_size );
93
91
94
92
if (new_notebook -> page_list_head == NULL )
95
93
{
96
94
yr_free (new_notebook );
97
95
return ERROR_INSUFFICIENT_MEMORY ;
98
96
}
99
97
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 ;
101
100
new_notebook -> page_list_head -> used = 0 ;
102
101
new_notebook -> page_list_head -> next = NULL ;
103
102
@@ -151,20 +150,26 @@ void* yr_notebook_alloc(YR_NOTEBOOK* notebook, size_t size)
151
150
// deferrencing pointers to types larger than a byte.
152
151
size = (size + 7 ) & ~0x7 ;
153
152
153
+ YR_NOTEBOOK_PAGE * current_page = notebook -> page_list_head ;
154
+
154
155
// If the requested size doesn't fit in current page's free space, allocate
155
156
// a new page.
156
- if (notebook -> page_size - notebook -> page_list_head -> used < size )
157
+ if (current_page -> size - current_page -> used < size )
157
158
{
159
+ size_t min_size = notebook -> min_page_size ;
160
+
158
161
// 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 ;
161
165
162
166
YR_NOTEBOOK_PAGE * new_page = yr_malloc (
163
167
sizeof (YR_NOTEBOOK_PAGE ) + page_size );
164
168
165
169
if (new_page == NULL )
166
170
return NULL ;
167
171
172
+ new_page -> size = page_size ;
168
173
new_page -> used = 0 ;
169
174
new_page -> next = notebook -> page_list_head ;
170
175
notebook -> page_list_head = new_page ;
0 commit comments