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