memory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright 2015 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Algorithms for distributing the literals and commands of a metablock between
  6. block types and contexts. */
  7. #include "memory.h"
  8. #include <stdlib.h> /* exit, free, malloc */
  9. #include <string.h> /* memcpy */
  10. #include <brotli/types.h>
  11. #include "../common/platform.h"
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. #define MAX_NEW_ALLOCATED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 2)
  16. #define MAX_NEW_FREED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 2)
  17. #define MAX_PERM_ALLOCATED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 1)
  18. #define PERM_ALLOCATED_OFFSET 0
  19. #define NEW_ALLOCATED_OFFSET MAX_PERM_ALLOCATED
  20. #define NEW_FREED_OFFSET (MAX_PERM_ALLOCATED + MAX_NEW_ALLOCATED)
  21. void BrotliInitMemoryManager(
  22. MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func,
  23. void* opaque) {
  24. if (!alloc_func) {
  25. m->alloc_func = BrotliDefaultAllocFunc;
  26. m->free_func = BrotliDefaultFreeFunc;
  27. m->opaque = 0;
  28. } else {
  29. m->alloc_func = alloc_func;
  30. m->free_func = free_func;
  31. m->opaque = opaque;
  32. }
  33. #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  34. m->is_oom = BROTLI_FALSE;
  35. m->perm_allocated = 0;
  36. m->new_allocated = 0;
  37. m->new_freed = 0;
  38. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  39. }
  40. #if defined(BROTLI_ENCODER_EXIT_ON_OOM)
  41. void* BrotliAllocate(MemoryManager* m, size_t n) {
  42. void* result = m->alloc_func(m->opaque, n);
  43. if (!result) exit(EXIT_FAILURE);
  44. return result;
  45. }
  46. void BrotliFree(MemoryManager* m, void* p) {
  47. m->free_func(m->opaque, p);
  48. }
  49. void BrotliWipeOutMemoryManager(MemoryManager* m) {
  50. BROTLI_UNUSED(m);
  51. }
  52. #else /* BROTLI_ENCODER_EXIT_ON_OOM */
  53. static void SortPointers(void** items, const size_t n) {
  54. /* Shell sort. */
  55. /* TODO(eustas): fine-tune for "many slots" case */
  56. static const size_t gaps[] = {23, 10, 4, 1};
  57. int g = 0;
  58. for (; g < 4; ++g) {
  59. size_t gap = gaps[g];
  60. size_t i;
  61. for (i = gap; i < n; ++i) {
  62. size_t j = i;
  63. void* tmp = items[i];
  64. for (; j >= gap && tmp < items[j - gap]; j -= gap) {
  65. items[j] = items[j - gap];
  66. }
  67. items[j] = tmp;
  68. }
  69. }
  70. }
  71. static size_t Annihilate(void** a, size_t a_len, void** b, size_t b_len) {
  72. size_t a_read_index = 0;
  73. size_t b_read_index = 0;
  74. size_t a_write_index = 0;
  75. size_t b_write_index = 0;
  76. size_t annihilated = 0;
  77. while (a_read_index < a_len && b_read_index < b_len) {
  78. if (a[a_read_index] == b[b_read_index]) {
  79. a_read_index++;
  80. b_read_index++;
  81. annihilated++;
  82. } else if (a[a_read_index] < b[b_read_index]) {
  83. a[a_write_index++] = a[a_read_index++];
  84. } else {
  85. b[b_write_index++] = b[b_read_index++];
  86. }
  87. }
  88. while (a_read_index < a_len) a[a_write_index++] = a[a_read_index++];
  89. while (b_read_index < b_len) b[b_write_index++] = b[b_read_index++];
  90. return annihilated;
  91. }
  92. static void CollectGarbagePointers(MemoryManager* m) {
  93. size_t annihilated;
  94. SortPointers(m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated);
  95. SortPointers(m->pointers + NEW_FREED_OFFSET, m->new_freed);
  96. annihilated = Annihilate(
  97. m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated,
  98. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  99. m->new_allocated -= annihilated;
  100. m->new_freed -= annihilated;
  101. if (m->new_freed != 0) {
  102. annihilated = Annihilate(
  103. m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated,
  104. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  105. m->perm_allocated -= annihilated;
  106. m->new_freed -= annihilated;
  107. BROTLI_DCHECK(m->new_freed == 0);
  108. }
  109. if (m->new_allocated != 0) {
  110. BROTLI_DCHECK(m->perm_allocated + m->new_allocated <= MAX_PERM_ALLOCATED);
  111. memcpy(m->pointers + PERM_ALLOCATED_OFFSET + m->perm_allocated,
  112. m->pointers + NEW_ALLOCATED_OFFSET,
  113. sizeof(void*) * m->new_allocated);
  114. m->perm_allocated += m->new_allocated;
  115. m->new_allocated = 0;
  116. SortPointers(m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated);
  117. }
  118. }
  119. void* BrotliAllocate(MemoryManager* m, size_t n) {
  120. void* result = m->alloc_func(m->opaque, n);
  121. if (!result) {
  122. m->is_oom = BROTLI_TRUE;
  123. return NULL;
  124. }
  125. if (m->new_allocated == MAX_NEW_ALLOCATED) CollectGarbagePointers(m);
  126. m->pointers[NEW_ALLOCATED_OFFSET + (m->new_allocated++)] = result;
  127. return result;
  128. }
  129. void BrotliFree(MemoryManager* m, void* p) {
  130. if (!p) return;
  131. m->free_func(m->opaque, p);
  132. if (m->new_freed == MAX_NEW_FREED) CollectGarbagePointers(m);
  133. m->pointers[NEW_FREED_OFFSET + (m->new_freed++)] = p;
  134. }
  135. void BrotliWipeOutMemoryManager(MemoryManager* m) {
  136. size_t i;
  137. CollectGarbagePointers(m);
  138. /* Now all unfreed pointers are in perm-allocated list. */
  139. for (i = 0; i < m->perm_allocated; ++i) {
  140. m->free_func(m->opaque, m->pointers[PERM_ALLOCATED_OFFSET + i]);
  141. }
  142. m->perm_allocated = 0;
  143. }
  144. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  145. void* BrotliBootstrapAlloc(size_t size,
  146. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
  147. if (!alloc_func && !free_func) {
  148. return malloc(size);
  149. } else if (alloc_func && free_func) {
  150. return alloc_func(opaque, size);
  151. }
  152. return NULL;
  153. }
  154. void BrotliBootstrapFree(void* address, MemoryManager* m) {
  155. if (!address) {
  156. /* Should not happen! */
  157. return;
  158. } else {
  159. /* Copy values, as those would be freed. */
  160. brotli_free_func free_func = m->free_func;
  161. void* opaque = m->opaque;
  162. free_func(opaque, address);
  163. }
  164. }
  165. #if defined(__cplusplus) || defined(c_plusplus)
  166. } /* extern "C" */
  167. #endif