@cryptotaxi247 / netdata-1 / commits / 7c986c53f

make onewayalloc fallback to malloc (#19646)

Costa Tsaousis committed Feb 14, 2025 at 20:38 UTC 7c986c53f0b314baf10acd7bc1c6fc2f72033cbb
1 file changed +12 -3
src/libnetdata/onewayalloc/onewayalloc.c
+12 -3
@@ -7,6 +7,7 @@ typedef struct owa_page {
7 size_t stats_mallocs_size;
8 size_t size; // the total size of the page
9 size_t offset; // the first free byte of the page
10 + bool mmap;
11 struct owa_page *next; // the next page on the list
12 struct owa_page *last; // the last page on the list - we currently allocate on this
13 } OWA_PAGE;
@@ -54,7 +55,12 @@ static OWA_PAGE *onewayalloc_create_internal(OWA_PAGE *head, size_t size_hint) {
55
56 // Use netdata_mmap instead of mallocz
57 OWA_PAGE *page = (OWA_PAGE *)nd_mmap_advanced(NULL, size, MAP_ANONYMOUS | MAP_PRIVATE, 0, false, false, NULL);
57 - if(unlikely(!page)) fatal("Cannot allocate onewayalloc buffer of size %zu", size);
58 + if(unlikely(!page)) {
59 + page = mallocz(size);
60 + page->mmap = false;
61 + }
62 + else
63 + page->mmap = true;
64
65 __atomic_add_fetch(&onewayalloc_total_memory, size, __ATOMIC_RELAXED);
66
@@ -196,8 +202,11 @@ void onewayalloc_destroy(ONEWAYALLOC *owa) {
202 page = page->next;
203
204 // Use netdata_munmap instead of freez
199 - nd_munmap(p, p->size);
205 + if(p->mmap)
206 + nd_munmap(p, p->size);
207 + else
208 + freez(p);
209 }
210
211 __atomic_sub_fetch(&onewayalloc_total_memory, total_size, __ATOMIC_RELAXED);
203 -}
\ No newline at end of file
212 +}