memory.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <assert.h>
  9. #include <stdlib.h> /* exit, free, malloc */
  10. #include <string.h> /* memcpy */
  11. #include "../common/types.h"
  12. #include "./port.h"
  13. #if defined(__cplusplus) || defined(c_plusplus)
  14. extern "C" {
  15. #endif
  16. #define MAX_PERM_ALLOCATED 128
  17. #define MAX_NEW_ALLOCATED 64
  18. #define MAX_NEW_FREED 64
  19. #define PERM_ALLOCATED_OFFSET 0
  20. #define NEW_ALLOCATED_OFFSET MAX_PERM_ALLOCATED
  21. #define NEW_FREED_OFFSET (MAX_PERM_ALLOCATED + MAX_NEW_ALLOCATED)
  22. static void* DefaultAllocFunc(void* opaque, size_t size) {
  23. BROTLI_UNUSED(opaque);
  24. return malloc(size);
  25. }
  26. static void DefaultFreeFunc(void* opaque, void* address) {
  27. BROTLI_UNUSED(opaque);
  28. free(address);
  29. }
  30. void BrotliInitMemoryManager(
  31. MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func,
  32. void* opaque) {
  33. if (!alloc_func) {
  34. m->alloc_func = DefaultAllocFunc;
  35. m->free_func = DefaultFreeFunc;
  36. m->opaque = 0;
  37. } else {
  38. m->alloc_func = alloc_func;
  39. m->free_func = free_func;
  40. m->opaque = opaque;
  41. }
  42. #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  43. m->is_oom = 0;
  44. m->perm_allocated = 0;
  45. m->new_allocated = 0;
  46. m->new_freed = 0;
  47. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  48. }
  49. #if defined(BROTLI_ENCODER_EXIT_ON_OOM)
  50. void* BrotliAllocate(MemoryManager* m, size_t n) {
  51. void* result = m->alloc_func(m->opaque, n);
  52. if (!result) exit(EXIT_FAILURE);
  53. return result;
  54. }
  55. void BrotliFree(MemoryManager* m, void* p) {
  56. m->free_func(m->opaque, p);
  57. }
  58. void BrotliWipeOutMemoryManager(MemoryManager* m) {
  59. BROTLI_UNUSED(m);
  60. }
  61. #else /* BROTLI_ENCODER_EXIT_ON_OOM */
  62. static void SortPointers(void** items, const size_t n) {
  63. /* Shell sort. */
  64. static const size_t gaps[] = {23, 10, 4, 1};
  65. int g = 0;
  66. for (; g < 4; ++g) {
  67. size_t gap = gaps[g];
  68. size_t i;
  69. for (i = gap; i < n; ++i) {
  70. size_t j = i;
  71. void* tmp = items[i];
  72. for (; j >= gap && tmp < items[j - gap]; j -= gap) {
  73. items[j] = items[j - gap];
  74. }
  75. items[j] = tmp;
  76. }
  77. }
  78. }
  79. static size_t Annihilate(void** a, size_t a_len, void** b, size_t b_len) {
  80. size_t a_read_index = 0;
  81. size_t b_read_index = 0;
  82. size_t a_write_index = 0;
  83. size_t b_write_index = 0;
  84. size_t annihilated = 0;
  85. while (a_read_index < a_len && b_read_index < b_len) {
  86. if (a[a_read_index] == b[b_read_index]) {
  87. a_read_index++;
  88. b_read_index++;
  89. annihilated++;
  90. } else if (a[a_read_index] < b[b_read_index]) {
  91. a[a_write_index++] = a[a_read_index++];
  92. } else {
  93. b[b_write_index++] = b[b_read_index++];
  94. }
  95. }
  96. while (a_read_index < a_len) a[a_write_index++] = a[a_read_index++];
  97. while (b_read_index < b_len) b[b_write_index++] = b[b_read_index++];
  98. return annihilated;
  99. }
  100. static void CollectGarbagePointers(MemoryManager* m) {
  101. size_t annihilated;
  102. SortPointers(m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated);
  103. SortPointers(m->pointers + NEW_FREED_OFFSET, m->new_freed);
  104. annihilated = Annihilate(
  105. m->pointers + NEW_ALLOCATED_OFFSET, m->new_allocated,
  106. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  107. m->new_allocated -= annihilated;
  108. m->new_freed -= annihilated;
  109. if (m->new_freed != 0) {
  110. annihilated = Annihilate(
  111. m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated,
  112. m->pointers + NEW_FREED_OFFSET, m->new_freed);
  113. m->perm_allocated -= annihilated;
  114. m->new_freed -= annihilated;
  115. assert(m->new_freed == 0);
  116. }
  117. if (m->new_allocated != 0) {
  118. assert(m->perm_allocated + m->new_allocated <= MAX_PERM_ALLOCATED);
  119. memcpy(m->pointers + PERM_ALLOCATED_OFFSET + m->perm_allocated,
  120. m->pointers + NEW_ALLOCATED_OFFSET,
  121. sizeof(void*) * m->new_allocated);
  122. m->perm_allocated += m->new_allocated;
  123. m->new_allocated = 0;
  124. SortPointers(m->pointers + PERM_ALLOCATED_OFFSET, m->perm_allocated);
  125. }
  126. }
  127. void* BrotliAllocate(MemoryManager* m, size_t n) {
  128. void* result = m->alloc_func(m->opaque, n);
  129. if (!result) {
  130. m->is_oom = 1;
  131. return NULL;
  132. }
  133. if (m->new_allocated == MAX_NEW_ALLOCATED) CollectGarbagePointers(m);
  134. m->pointers[NEW_ALLOCATED_OFFSET + (m->new_allocated++)] = result;
  135. return result;
  136. }
  137. void BrotliFree(MemoryManager* m, void* p) {
  138. if (!p) return;
  139. m->free_func(m->opaque, p);
  140. if (m->new_freed == MAX_NEW_FREED) CollectGarbagePointers(m);
  141. m->pointers[NEW_FREED_OFFSET + (m->new_freed++)] = p;
  142. }
  143. void BrotliWipeOutMemoryManager(MemoryManager* m) {
  144. size_t i;
  145. CollectGarbagePointers(m);
  146. /* Now all unfreed pointers are in perm-allocated list. */
  147. for (i = 0; i < m->perm_allocated; ++i) {
  148. m->free_func(m->opaque, m->pointers[PERM_ALLOCATED_OFFSET + i]);
  149. }
  150. m->perm_allocated = 0;
  151. }
  152. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  153. #if defined(__cplusplus) || defined(c_plusplus)
  154. } /* extern "C" */
  155. #endif