memory.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "../common/platform.h"
  11. #include <brotli/types.h>
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. #define MAX_PERM_ALLOCATED 128
  16. #define MAX_NEW_ALLOCATED 64
  17. #define MAX_NEW_FREED 64
  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. static const size_t gaps[] = {23, 10, 4, 1};
  56. int g = 0;
  57. for (; g < 4; ++g) {
  58. size_t gap = gaps[g];
  59. size_t i;
  60. for (i = gap; i < n; ++i) {
  61. size_t j = i;
  62. void* tmp = items[i];
  63. for (; j >= gap && tmp < items[j - gap]; j -= gap) {
  64. items[j] = items[j - gap];
  65. }
  66. items[j] = tmp;
  67. }
  68. }
  69. }
  70. static size_t Annihilate(void** a, size_t a_len, void** b, size_t b_len) {
  71. size_t a_read_index = 0;
  72. size_t b_read_index = 0;
  73. size_t a_write_index = 0;
  74. size_t b_write_index = 0;
  75. size_t annihilated = 0;
  76. while (a_read_index < a_len && b_read_index < b_len) {
  77. if (a[a_read_index] == b[b_read_index]) {
  78. a_read_index++;
  79. b_read_index++;
  80. annihilated++;
  81. } else if (a[a_read_index] < b[b_read_index]) {
  82. a[a_write_index++] = a[a_read_index++];
  83. } else {
  84. b[b_write_index++] = b[b_read_index++];
  85. }
  86. }
  87. while (a_read_index < a_len) a[a_write_index++] = a[a_read_index++];
  88. while (b_read_index < b_len) b[b_write_index++] = b[b_read_index++];
  89. return annihilated;
  90. }
  91. static void CollectGarbagePointers(MemoryManager* m) {
  92. size_t annihilated;
  93. SortPointers(m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated);
  94. SortPointers(m->pointers + NEW_FREED_OFFSET, m->new_freed);
  95. annihilated = Annihilate(
  96. m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated,
  97. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  98. m->new_allocated -= annihilated;
  99. m->new_freed -= annihilated;
  100. if (m->new_freed != 0) {
  101. annihilated = Annihilate(
  102. m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated,
  103. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  104. m->perm_allocated -= annihilated;
  105. m->new_freed -= annihilated;
  106. BROTLI_DCHECK(m->new_freed == 0);
  107. }
  108. if (m->new_allocated != 0) {
  109. BROTLI_DCHECK(m->perm_allocated + m->new_allocated <= MAX_PERM_ALLOCATED);
  110. memcpy(m->pointers + PERM_ALLOCATED_OFFSET + m->perm_allocated,
  111. m->pointers + NEW_ALLOCATED_OFFSET,
  112. sizeof(void*) * m->new_allocated);
  113. m->perm_allocated += m->new_allocated;
  114. m->new_allocated = 0;
  115. SortPointers(m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated);
  116. }
  117. }
  118. void* BrotliAllocate(MemoryManager* m, size_t n) {
  119. void* result = m->alloc_func(m->opaque, n);
  120. if (!result) {
  121. m->is_oom = BROTLI_TRUE;
  122. return NULL;
  123. }
  124. if (m->new_allocated == MAX_NEW_ALLOCATED) CollectGarbagePointers(m);
  125. m->pointers[NEW_ALLOCATED_OFFSET + (m->new_allocated++)] = result;
  126. return result;
  127. }
  128. void BrotliFree(MemoryManager* m, void* p) {
  129. if (!p) return;
  130. m->free_func(m->opaque, p);
  131. if (m->new_freed == MAX_NEW_FREED) CollectGarbagePointers(m);
  132. m->pointers[NEW_FREED_OFFSET + (m->new_freed++)] = p;
  133. }
  134. void BrotliWipeOutMemoryManager(MemoryManager* m) {
  135. size_t i;
  136. CollectGarbagePointers(m);
  137. /* Now all unfreed pointers are in perm-allocated list. */
  138. for (i = 0; i < m->perm_allocated; ++i) {
  139. m->free_func(m->opaque, m->pointers[PERM_ALLOCATED_OFFSET + i]);
  140. }
  141. m->perm_allocated = 0;
  142. }
  143. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  144. #if defined(__cplusplus) || defined(c_plusplus)
  145. } /* extern "C" */
  146. #endif