onewayalloc.c 5.4 KB

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