onewayalloc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "onewayalloc.h"
  2. // https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
  3. #define OWA_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  4. typedef struct owa_page {
  5. size_t stats_pages;
  6. size_t stats_pages_size;
  7. size_t stats_mallocs_made;
  8. size_t stats_mallocs_size;
  9. size_t size; // the total size of the page
  10. size_t offset; // the first free byte of the page
  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;
  14. static size_t onewayalloc_total_memory = 0;
  15. size_t onewayalloc_allocated_memory(void) {
  16. return __atomic_load_n(&onewayalloc_total_memory, __ATOMIC_RELAXED);
  17. }
  18. // allocations need to be aligned to CPU register width
  19. // https://en.wikipedia.org/wiki/Data_structure_alignment
  20. static inline size_t natural_alignment(size_t size) {
  21. if(unlikely(size % OWA_NATURAL_ALIGNMENT))
  22. size = size + OWA_NATURAL_ALIGNMENT - (size % OWA_NATURAL_ALIGNMENT);
  23. return size;
  24. }
  25. // Create an OWA
  26. // Once it is created, the called may call the onewayalloc_mallocz()
  27. // any number of times, for any amount of memory.
  28. static OWA_PAGE *onewayalloc_create_internal(OWA_PAGE *head, size_t size_hint) {
  29. static size_t OWA_NATURAL_PAGE_SIZE = 0;
  30. if(unlikely(!OWA_NATURAL_PAGE_SIZE)) {
  31. long int page_size = sysconf(_SC_PAGE_SIZE);
  32. if (unlikely(page_size == -1))
  33. OWA_NATURAL_PAGE_SIZE = 4096;
  34. else
  35. OWA_NATURAL_PAGE_SIZE = page_size;
  36. }
  37. // our default page size
  38. size_t size = OWA_NATURAL_PAGE_SIZE;
  39. // make sure the new page will fit both the requested size
  40. // and the OWA_PAGE structure at its beginning
  41. size_hint += natural_alignment(sizeof(OWA_PAGE));
  42. // prefer the user size if it is bigger than our size
  43. if(size_hint > size) size = size_hint;
  44. // try to allocate half of the total we have allocated already
  45. if(likely(head)) {
  46. size_t optimal_size = head->stats_pages_size / 2;
  47. if(optimal_size > size) size = optimal_size;
  48. }
  49. // Make sure our allocations are always a multiple of the hardware page size
  50. if(size % OWA_NATURAL_PAGE_SIZE) size = size + OWA_NATURAL_PAGE_SIZE - (size % OWA_NATURAL_PAGE_SIZE);
  51. // OWA_PAGE *page = (OWA_PAGE *)netdata_mmap(NULL, size, MAP_ANONYMOUS|MAP_PRIVATE, 0);
  52. // if(unlikely(!page)) fatal("Cannot allocate onewayalloc buffer of size %zu", size);
  53. OWA_PAGE *page = (OWA_PAGE *)mallocz(size);
  54. __atomic_add_fetch(&onewayalloc_total_memory, size, __ATOMIC_RELAXED);
  55. page->size = size;
  56. page->offset = natural_alignment(sizeof(OWA_PAGE));
  57. page->next = page->last = NULL;
  58. if(unlikely(!head)) {
  59. // this is the first time we are called
  60. head = page;
  61. head->stats_pages = 0;
  62. head->stats_pages_size = 0;
  63. head->stats_mallocs_made = 0;
  64. head->stats_mallocs_size = 0;
  65. }
  66. else {
  67. // link this page into our existing linked list
  68. head->last->next = page;
  69. }
  70. head->last = page;
  71. head->stats_pages++;
  72. head->stats_pages_size += size;
  73. return page;
  74. }
  75. ONEWAYALLOC *onewayalloc_create(size_t size_hint) {
  76. return (ONEWAYALLOC *)onewayalloc_create_internal(NULL, size_hint);
  77. }
  78. void *onewayalloc_mallocz(ONEWAYALLOC *owa, size_t size) {
  79. #ifdef FSANITIZE_ADDRESS
  80. return mallocz(size);
  81. #endif
  82. OWA_PAGE *head = (OWA_PAGE *)owa;
  83. OWA_PAGE *page = head->last;
  84. // update stats
  85. head->stats_mallocs_made++;
  86. head->stats_mallocs_size += size;
  87. // make sure the size is aligned
  88. size = natural_alignment(size);
  89. if(unlikely(page->size - page->offset < size)) {
  90. // we don't have enough space to fit the data
  91. // let's get another page
  92. page = onewayalloc_create_internal(head, (size > page->size)?size:page->size);
  93. }
  94. char *mem = (char *)page;
  95. mem = &mem[page->offset];
  96. page->offset += size;
  97. return (void *)mem;
  98. }
  99. void *onewayalloc_callocz(ONEWAYALLOC *owa, size_t nmemb, size_t size) {
  100. size_t total = nmemb * size;
  101. void *mem = onewayalloc_mallocz(owa, total);
  102. memset(mem, 0, total);
  103. return mem;
  104. }
  105. char *onewayalloc_strdupz(ONEWAYALLOC *owa, const char *s) {
  106. size_t size = strlen(s) + 1;
  107. char *d = onewayalloc_mallocz((OWA_PAGE *)owa, size);
  108. memcpy(d, s, size);
  109. return d;
  110. }
  111. void *onewayalloc_memdupz(ONEWAYALLOC *owa, const void *src, size_t size) {
  112. void *mem = onewayalloc_mallocz((OWA_PAGE *)owa, size);
  113. // memcpy() is way faster than strcpy() since it does not check for '\0'
  114. memcpy(mem, src, size);
  115. return mem;
  116. }
  117. void onewayalloc_freez(ONEWAYALLOC *owa __maybe_unused, const void *ptr __maybe_unused) {
  118. #ifdef FSANITIZE_ADDRESS
  119. freez((void *)ptr);
  120. return;
  121. #endif
  122. #ifdef NETDATA_INTERNAL_CHECKS
  123. // allow the caller to call us for a mallocz() allocation
  124. // so try to find it in our memory and if it is not there
  125. // log an error
  126. if (unlikely(!ptr))
  127. return;
  128. OWA_PAGE *head = (OWA_PAGE *)owa;
  129. OWA_PAGE *page;
  130. uintptr_t seeking = (uintptr_t)ptr;
  131. for(page = head; page ;page = page->next) {
  132. uintptr_t start = (uintptr_t)page;
  133. uintptr_t end = start + page->size;
  134. if(seeking >= start && seeking <= end) {
  135. // found it - it is ours
  136. // just return to let the caller think we actually did something
  137. return;
  138. }
  139. }
  140. // not found - it is not ours
  141. // let's free it with the system allocator
  142. netdata_log_error("ONEWAYALLOC: request to free address 0x%p that is not allocated by this OWA", ptr);
  143. #endif
  144. }
  145. void *onewayalloc_doublesize(ONEWAYALLOC *owa, const void *src, size_t oldsize) {
  146. size_t newsize = oldsize * 2;
  147. void *dst = onewayalloc_mallocz(owa, newsize);
  148. memcpy(dst, src, oldsize);
  149. onewayalloc_freez(owa, src);
  150. return dst;
  151. }
  152. void onewayalloc_destroy(ONEWAYALLOC *owa) {
  153. if(!owa) return;
  154. OWA_PAGE *head = (OWA_PAGE *)owa;
  155. //netdata_log_info("OWA: %zu allocations of %zu total bytes, in %zu pages of %zu total bytes",
  156. // head->stats_mallocs_made, head->stats_mallocs_size,
  157. // head->stats_pages, head->stats_pages_size);
  158. size_t total_size = 0;
  159. OWA_PAGE *page = head;
  160. while(page) {
  161. total_size += page->size;
  162. OWA_PAGE *p = page;
  163. page = page->next;
  164. // munmap(p, p->size);
  165. freez(p);
  166. }
  167. __atomic_sub_fetch(&onewayalloc_total_memory, total_size, __ATOMIC_RELAXED);
  168. }