summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jpegli/memory_manager.cc
blob: f6530d8f024a4bb7cf0c72a29598bd6c8ee595db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jpegli/memory_manager.h"

#include <string.h>

#include <hwy/aligned_allocator.h>
#include <vector>

#include "lib/jpegli/common_internal.h"
#include "lib/jpegli/error.h"

struct jvirt_sarray_control {
  JSAMPARRAY full_buffer;
  size_t numrows;
  JDIMENSION maxaccess;
};

struct jvirt_barray_control {
  JBLOCKARRAY full_buffer;
  size_t numrows;
  JDIMENSION maxaccess;
};

namespace jpegli {

namespace {

struct MemoryManager {
  struct jpeg_memory_mgr pub;
  std::vector<void*> owned_ptrs[2 * JPOOL_NUMPOOLS];
  uint64_t pool_memory_usage[2 * JPOOL_NUMPOOLS];
  uint64_t total_memory_usage;
  uint64_t peak_memory_usage;
};

void* Alloc(j_common_ptr cinfo, int pool_id, size_t sizeofobject) {
  MemoryManager* mem = reinterpret_cast<MemoryManager*>(cinfo->mem);
  if (pool_id < 0 || pool_id >= 2 * JPOOL_NUMPOOLS) {
    JPEGLI_ERROR("Invalid pool id %d", pool_id);
  }
  if (mem->pub.max_memory_to_use > 0 &&
      mem->total_memory_usage + static_cast<uint64_t>(sizeofobject) >
          static_cast<uint64_t>(mem->pub.max_memory_to_use)) {
    JPEGLI_ERROR("Total memory usage exceeding %ld",
                 mem->pub.max_memory_to_use);
  }
  void* p;
  if (pool_id < JPOOL_NUMPOOLS) {
    p = malloc(sizeofobject);
  } else {
    p = hwy::AllocateAlignedBytes(sizeofobject, nullptr, nullptr);
  }
  if (p == nullptr) {
    JPEGLI_ERROR("Out of memory");
  }
  mem->owned_ptrs[pool_id].push_back(p);
  mem->pool_memory_usage[pool_id] += sizeofobject;
  mem->total_memory_usage += sizeofobject;
  mem->peak_memory_usage =
      std::max(mem->peak_memory_usage, mem->total_memory_usage);
  return p;
}

template <typename T>
T** Alloc2dArray(j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow,
                 JDIMENSION numrows) {
  T** array = Allocate<T*>(cinfo, numrows, pool_id);
  // Always use aligned allocator for large 2d arrays.
  if (pool_id < JPOOL_NUMPOOLS) {
    pool_id += JPOOL_NUMPOOLS;
  }
  size_t stride = RoundUpTo(samplesperrow, HWY_ALIGNMENT);
  T* buffer = Allocate<T>(cinfo, numrows * stride, pool_id);
  for (size_t i = 0; i < numrows; ++i) {
    array[i] = &buffer[i * stride];
  }
  return array;
}

template <typename Control, typename T>
Control* RequestVirtualArray(j_common_ptr cinfo, int pool_id, boolean pre_zero,
                             JDIMENSION samplesperrow, JDIMENSION numrows,
                             JDIMENSION maxaccess) {
  if (pool_id != JPOOL_IMAGE) {
    JPEGLI_ERROR("Only image lifetime virtual arrays are supported.");
  }
  Control* p = Allocate<Control>(cinfo, 1, pool_id);
  p->full_buffer = Alloc2dArray<T>(cinfo, pool_id, samplesperrow, numrows);
  p->numrows = numrows;
  p->maxaccess = maxaccess;
  if (pre_zero) {
    for (size_t i = 0; i < numrows; ++i) {
      memset(p->full_buffer[i], 0, samplesperrow * sizeof(T));
    }
  }
  return p;
}

void RealizeVirtualArrays(j_common_ptr cinfo) {
  // Nothing to do, the full arrays were realized at request time already.
}

template <typename Control, typename T>
T** AccessVirtualArray(j_common_ptr cinfo, Control* ptr, JDIMENSION start_row,
                       JDIMENSION num_rows, boolean writable) {
  if (num_rows > ptr->maxaccess) {
    JPEGLI_ERROR("Invalid virtual array access, num rows %u vs max rows %u",
                 num_rows, ptr->maxaccess);
  }
  if (start_row + num_rows > ptr->numrows) {
    JPEGLI_ERROR("Invalid virtual array access, %u vs %u total rows",
                 start_row + num_rows, ptr->numrows);
  }
  if (ptr->full_buffer == nullptr) {
    JPEGLI_ERROR("Invalid virtual array access, array not realized.");
  }
  return ptr->full_buffer + start_row;
}

void ClearPool(j_common_ptr cinfo, int pool_id) {
  MemoryManager* mem = reinterpret_cast<MemoryManager*>(cinfo->mem);
  mem->owned_ptrs[pool_id].clear();
  mem->total_memory_usage -= mem->pool_memory_usage[pool_id];
  mem->pool_memory_usage[pool_id] = 0;
}

void FreePool(j_common_ptr cinfo, int pool_id) {
  MemoryManager* mem = reinterpret_cast<MemoryManager*>(cinfo->mem);
  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) {
    JPEGLI_ERROR("Invalid pool id %d", pool_id);
  }
  for (void* ptr : mem->owned_ptrs[pool_id]) {
    free(ptr);
  }
  ClearPool(cinfo, pool_id);
  for (void* ptr : mem->owned_ptrs[JPOOL_NUMPOOLS + pool_id]) {
    hwy::FreeAlignedBytes(ptr, nullptr, nullptr);
  }
  ClearPool(cinfo, JPOOL_NUMPOOLS + pool_id);
}

void SelfDestruct(j_common_ptr cinfo) {
  MemoryManager* mem = reinterpret_cast<MemoryManager*>(cinfo->mem);
  for (int pool_id = 0; pool_id < JPOOL_NUMPOOLS; ++pool_id) {
    FreePool(cinfo, pool_id);
  }
  delete mem;
  cinfo->mem = nullptr;
}

}  // namespace

void InitMemoryManager(j_common_ptr cinfo) {
  MemoryManager* mem = new MemoryManager;
  mem->pub.alloc_small = jpegli::Alloc;
  mem->pub.alloc_large = jpegli::Alloc;
  mem->pub.alloc_sarray = jpegli::Alloc2dArray<JSAMPLE>;
  mem->pub.alloc_barray = jpegli::Alloc2dArray<JBLOCK>;
  mem->pub.request_virt_sarray =
      jpegli::RequestVirtualArray<jvirt_sarray_control, JSAMPLE>;
  mem->pub.request_virt_barray =
      jpegli::RequestVirtualArray<jvirt_barray_control, JBLOCK>;
  mem->pub.realize_virt_arrays = jpegli::RealizeVirtualArrays;
  mem->pub.access_virt_sarray =
      jpegli::AccessVirtualArray<jvirt_sarray_control, JSAMPLE>;
  mem->pub.access_virt_barray =
      jpegli::AccessVirtualArray<jvirt_barray_control, JBLOCK>;
  mem->pub.free_pool = jpegli::FreePool;
  mem->pub.self_destruct = jpegli::SelfDestruct;
  mem->pub.max_memory_to_use = 0;
  mem->total_memory_usage = 0;
  mem->peak_memory_usage = 0;
  memset(mem->pool_memory_usage, 0, sizeof(mem->pool_memory_usage));
  cinfo->mem = reinterpret_cast<struct jpeg_memory_mgr*>(mem);
}

}  // namespace jpegli