onewayalloc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. OWA_PAGE *head = (OWA_PAGE *)owa;
  80. OWA_PAGE *page = head->last;
  81. // update stats
  82. head->stats_mallocs_made++;
  83. head->stats_mallocs_size += size;
  84. // make sure the size is aligned
  85. size = natural_alignment(size);
  86. if(unlikely(page->size - page->offset < size)) {
  87. // we don't have enough space to fit the data
  88. // let's get another page
  89. page = onewayalloc_create_internal(head, (size > page->size)?size:page->size);
  90. }
  91. char *mem = (char *)page;
  92. mem = &mem[page->offset];
  93. page->offset += size;
  94. return (void *)mem;
  95. }
  96. void *onewayalloc_callocz(ONEWAYALLOC *owa, size_t nmemb, size_t size) {
  97. size_t total = nmemb * size;
  98. void *mem = onewayalloc_mallocz(owa, total);
  99. memset(mem, 0, total);
  100. return mem;
  101. }
  102. char *onewayalloc_strdupz(ONEWAYALLOC *owa, const char *s) {
  103. size_t size = strlen(s) + 1;
  104. char *d = onewayalloc_mallocz((OWA_PAGE *)owa, size);
  105. memcpy(d, s, size);
  106. return d;
  107. }
  108. void *onewayalloc_memdupz(ONEWAYALLOC *owa, const void *src, size_t size) {
  109. void *mem = onewayalloc_mallocz((OWA_PAGE *)owa, size);
  110. // memcpy() is way faster than strcpy() since it does not check for '\0'
  111. memcpy(mem, src, size);
  112. return mem;
  113. }
  114. void onewayalloc_freez(ONEWAYALLOC *owa __maybe_unused, const void *ptr __maybe_unused) {
  115. #ifdef NETDATA_INTERNAL_CHECKS
  116. // allow the caller to call us for a mallocz() allocation
  117. // so try to find it in our memory and if it is not there
  118. // log an error
  119. if (unlikely(!ptr))
  120. return;
  121. OWA_PAGE *head = (OWA_PAGE *)owa;
  122. OWA_PAGE *page;
  123. uintptr_t seeking = (uintptr_t)ptr;
  124. for(page = head; page ;page = page->next) {
  125. uintptr_t start = (uintptr_t)page;
  126. uintptr_t end = start + page->size;
  127. if(seeking >= start && seeking <= end) {
  128. // found it - it is ours
  129. // just return to let the caller think we actually did something
  130. return;
  131. }
  132. }
  133. // not found - it is not ours
  134. // let's free it with the system allocator
  135. error("ONEWAYALLOC: request to free address 0x%p that is not allocated by this OWA", ptr);
  136. #endif
  137. return;
  138. }
  139. void *onewayalloc_doublesize(ONEWAYALLOC *owa, const void *src, size_t oldsize) {
  140. size_t newsize = oldsize * 2;
  141. void *dst = onewayalloc_mallocz(owa, newsize);
  142. memcpy(dst, src, oldsize);
  143. onewayalloc_freez(owa, src);
  144. return dst;
  145. }
  146. void onewayalloc_destroy(ONEWAYALLOC *owa) {
  147. if(!owa) return;
  148. OWA_PAGE *head = (OWA_PAGE *)owa;
  149. //info("OWA: %zu allocations of %zu total bytes, in %zu pages of %zu total bytes",
  150. // head->stats_mallocs_made, head->stats_mallocs_size,
  151. // head->stats_pages, head->stats_pages_size);
  152. size_t total_size = 0;
  153. OWA_PAGE *page = head;
  154. while(page) {
  155. total_size += page->size;
  156. OWA_PAGE *p = page;
  157. page = page->next;
  158. // munmap(p, p->size);
  159. freez(p);
  160. }
  161. __atomic_sub_fetch(&onewayalloc_total_memory, total_size, __ATOMIC_RELAXED);
  162. }