diff options
Diffstat (limited to '')
-rw-r--r-- | libnetdata/onewayalloc/onewayalloc.c | 35 | ||||
-rw-r--r-- | libnetdata/onewayalloc/onewayalloc.h | 2 |
2 files changed, 27 insertions, 10 deletions
diff --git a/libnetdata/onewayalloc/onewayalloc.c b/libnetdata/onewayalloc/onewayalloc.c index a048aebf6..59c3b6859 100644 --- a/libnetdata/onewayalloc/onewayalloc.c +++ b/libnetdata/onewayalloc/onewayalloc.c @@ -1,7 +1,7 @@ #include "onewayalloc.h" -static size_t OWA_NATURAL_PAGE_SIZE = 0; -static size_t OWA_NATURAL_ALIGNMENT = sizeof(int*); +// https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html +#define OWA_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2) typedef struct owa_page { size_t stats_pages; @@ -28,15 +28,22 @@ static inline size_t natural_alignment(size_t size) { // any number of times, for any amount of memory. static OWA_PAGE *onewayalloc_create_internal(OWA_PAGE *head, size_t size_hint) { - if(unlikely(!OWA_NATURAL_PAGE_SIZE)) - OWA_NATURAL_PAGE_SIZE = sysconf(_SC_PAGE_SIZE); + static size_t OWA_NATURAL_PAGE_SIZE = 0; + + if(unlikely(!OWA_NATURAL_PAGE_SIZE)) { + long int page_size = sysconf(_SC_PAGE_SIZE); + if (unlikely(page_size == -1)) + OWA_NATURAL_PAGE_SIZE = 4096; + else + OWA_NATURAL_PAGE_SIZE = page_size; + } // our default page size size_t size = OWA_NATURAL_PAGE_SIZE; // make sure the new page will fit both the requested size // and the OWA_PAGE structure at its beginning - size_hint += sizeof(OWA_PAGE); + size_hint += natural_alignment(sizeof(OWA_PAGE)); // prefer the user size if it is bigger than our size if(size_hint > size) size = size_hint; @@ -75,11 +82,11 @@ static OWA_PAGE *onewayalloc_create_internal(OWA_PAGE *head, size_t size_hint) { head->stats_pages++; head->stats_pages_size += size; - return (ONEWAYALLOC *)page; + return page; } ONEWAYALLOC *onewayalloc_create(size_t size_hint) { - return onewayalloc_create_internal(NULL, size_hint); + return (ONEWAYALLOC *)onewayalloc_create_internal(NULL, size_hint); } void *onewayalloc_mallocz(ONEWAYALLOC *owa, size_t size) { @@ -138,11 +145,11 @@ void onewayalloc_freez(ONEWAYALLOC *owa __maybe_unused, const void *ptr __maybe_ OWA_PAGE *head = (OWA_PAGE *)owa; OWA_PAGE *page; - size_t seeking = (size_t)ptr; + uintptr_t seeking = (uintptr_t)ptr; for(page = head; page ;page = page->next) { - size_t start = (size_t)page; - size_t end = start + page->size; + uintptr_t start = (uintptr_t)page; + uintptr_t end = start + page->size; if(seeking >= start && seeking <= end) { // found it - it is ours @@ -159,6 +166,14 @@ void onewayalloc_freez(ONEWAYALLOC *owa __maybe_unused, const void *ptr __maybe_ return; } +void *onewayalloc_doublesize(ONEWAYALLOC *owa, const void *src, size_t oldsize) { + size_t newsize = oldsize * 2; + void *dst = onewayalloc_mallocz(owa, newsize); + memcpy(dst, src, oldsize); + onewayalloc_freez(owa, src); + return dst; +} + void onewayalloc_destroy(ONEWAYALLOC *owa) { if(!owa) return; diff --git a/libnetdata/onewayalloc/onewayalloc.h b/libnetdata/onewayalloc/onewayalloc.h index ed8f12f39..9eb908bfb 100644 --- a/libnetdata/onewayalloc/onewayalloc.h +++ b/libnetdata/onewayalloc/onewayalloc.h @@ -14,4 +14,6 @@ extern char *onewayalloc_strdupz(ONEWAYALLOC *owa, const char *s); extern void *onewayalloc_memdupz(ONEWAYALLOC *owa, const void *src, size_t size); extern void onewayalloc_freez(ONEWAYALLOC *owa, const void *ptr); +extern void *onewayalloc_doublesize(ONEWAYALLOC *owa, const void *src, size_t oldsize); + #endif // ONEWAYALLOC_H |