Coverage Report

Created: 2025-06-24 07:01

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