OpenJPH: /build/openjph/src/OpenJPH/src/core/openjph/ojph_mem.h Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_mem.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2019, Aous Naman
6// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2019, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_mem.h
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
39#ifndef OJPH_MEM_H
40#define OJPH_MEM_H
41
42#include <cstdlib>
43#include <cassert>
44#include <cstring>
45#include <type_traits>
46
47#include "ojph_arch.h"
48
49namespace ojph {
50
51 extern "C" {
52 void* ojph_aligned_malloc(size_t alignment, size_t size);
53 void ojph_aligned_free(void* pointer);
54 }
55
57 class mem_fixed_allocator
58 {
59 public:
60 mem_fixed_allocator()
61 {
62 store = NULL; allocated_data = 0;
63 restart();
64 }
65 ~mem_fixed_allocator()
66 {
67 if (store) free(store);
68 }
69
70 template<typename T>
71 void pre_alloc_data(size_t num_ele, ui32 pre_size)
72 {
73 pre_alloc_local<T, byte_alignment>(num_ele, pre_size, size_data);
74 }
75
76 template<typename T>
77 void pre_alloc_obj(size_t num_ele)
78 {
79 pre_alloc_local<T, object_alignment>(num_ele, 0, size_obj);
80 }
81
82 void alloc()
83 {
84 assert(preallocation);
85 if (size_data + size_obj > allocated_data)
86 {
87 // We should be here once only, because, in subsequent, calls we
88 // should have size_data + size_obj <= allocated_data
89 free(store);
90 allocated_data = size_data + size_obj;
91 allocated_data = allocated_data + (allocated_data + 19) / 20; // 5%
92 store = malloc(allocated_data);
93 if (store == NULL)
94 throw "malloc failed";
95 }
96 avail_obj = store;
97 avail_data = (ui8*)store + size_obj;
98 avail_size_obj = size_obj;
99 avail_size_data = size_data;
100 preallocation = false;
101 }
102
103 void restart()
104 {
105 avail_obj = avail_data = NULL;
106 avail_size_obj = avail_size_data = size_obj = size_data = 0;
107 preallocation = true;
108 }
109
110 template<typename T>
111 T* post_alloc_data(size_t num_ele, ui32 pre_size)
112 {
113 return post_alloc_local<T, byte_alignment>
114 (num_ele, pre_size, avail_size_data, avail_data);
115 }
116
117 template<typename T>
118 T* post_alloc_obj(size_t num_ele)
119 {
120 return post_alloc_local<T, object_alignment>
121 (num_ele, 0, avail_size_obj, avail_obj);
122 }
123
124 private:
125 template<typename T, int N>
126 void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t& sz)
127 {
128 assert(preallocation);
129 num_ele = calc_aligned_size<T, N>(num_ele);
130 size_t total = (num_ele + pre_size) * sizeof(T);
131 total += 2*N - 1;
132
133 sz += total;
134 }
135
136 template<typename T, int N>
137 T* post_alloc_local(size_t num_ele, ui32 pre_size,
138 size_t& avail_sz, void*& avail_p)
139 {
140 assert(!preallocation);
141 num_ele = calc_aligned_size<T, N>(num_ele);
142 size_t total = (num_ele + pre_size) * sizeof(T);
143 total += 2*N - 1;
144
145 T* p = align_ptr<T, N>((T*)avail_p + pre_size);
146 avail_p = (ui8*)avail_p + total;
147 avail_sz -= total;
148 assert((avail_sz & 0x8000000000000000llu) == 0);
149 return p;
150 }
151
152 void *store, *avail_data, *avail_obj;
153 size_t size_data, size_obj, avail_size_obj, avail_size_data;
154 size_t allocated_data;
155 bool preallocation;
156 };
157
159 class line_buf
160 {
161 public:
162 enum : ui32 {
163 LFT_UNDEFINED = 0x00, // Type is undefined/uninitialized
164 // These flags reflects data size in bytes
165 LFT_BYTE = 0x01, // Set when data is 1 byte (not used)
166 LFT_16BIT = 0x02, // Set when data is 2 bytes (not used)
167 LFT_32BIT = 0x04, // Set when data is 4 bytes
168 LFT_64BIT = 0x08, // Set when data is 8 bytes
169 LFT_INTEGER = 0x10, // Set when data is an integer, in other words
170 // 32bit integer, not 32bit float
171 LFT_SIZE_MASK = 0x0F, // To extract data size
172 };
173
174 public:
175 line_buf() : size(0), pre_size(0), flags(LFT_UNDEFINED), i32(0) {}
176
177 template<typename T>
178 void wrap(T *buffer, size_t num_ele, ui32 pre_size);
179
180 size_t size;
181 ui32 pre_size;
182 ui32 flags;
183 union {
184 si32* i32; // 32bit integer type, used for lossless compression
185 si64* i64; // 64bit integer type, used for lossless compression
186 float* f32; // float type, used for lossy compression
187 void* p; // no type is associated with the pointer
188 };
189 };
190
192 struct lifting_buf
193 {
194 lifting_buf() { line = NULL; active = false; }
195 line_buf *line;
196 bool active;
197 };
198
200 struct coded_lists
201 {
202 coded_lists(ui32 size)
203 {
204 next_list = NULL;
205 avail_size = buf_size = size;
206 this->buf = (ui8*)this + sizeof(coded_lists);
207 }
208
209 coded_lists* next_list;
210 ui32 buf_size;
211 ui32 avail_size;
212 ui8* buf;
213 };
214
216 class mem_elastic_allocator
217 {
218 /*
219 advantage: allocate large chunks of memory
220 */
221
222 public:
223 mem_elastic_allocator(ui32 chunk_size)
224 : chunk_size(chunk_size)
225 { cur_store = store = avail = NULL; total_allocated = 0; }
226
227 ~mem_elastic_allocator()
228 {
229 while (store) { // stores in use
230 stores_list* t = store->next_store;
231 free(store);
232 store = t;
233 }
234 while (avail) { // available stores
235 stores_list* t = avail->next_store;
236 free(avail);
237 avail = t;
238 }
239 }
240
241 void get_buffer(ui32 needed_bytes, coded_lists*& p);
242 void restart();
243
244 private:
245 struct stores_list
246 {
247 stores_list(ui32 available_bytes)
248 {
249 this->next_store = NULL;
250 this->orig_size = this->available = available_bytes;
251 this->orig_data = this->data = (ui8*)this + sizeof(stores_list);
252 }
253 void restart()
254 {
255 this->next_store = NULL;
256 this->available = this->orig_size;
257 this->data = this->orig_data;
258 }
259 static ui32 eval_store_bytes(ui32 available_bytes)
260 { // calculates how many bytes need to be allocated
261 return available_bytes + (ui32)sizeof(stores_list);
262 }
263 stores_list *next_store;
264 ui8 *orig_data, *data;
265 ui32 orig_size, available;
266 };
267
268 stores_list* allocate(stores_list** list, ui32 extended_bytes);
269
270 stores_list *store;
271 stores_list *cur_store;
272 stores_list *avail;
273 size_t total_allocated;
274 const ui32 chunk_size;
275 };
276
277
278}
279
280
281#endif // !OJPH_MEM_H
ojph::line_buf
Definition ojph_mem.h:160
ojph::line_buf::LFT_INTEGER
@ LFT_INTEGER
Definition ojph_mem.h:169
ojph::line_buf::LFT_UNDEFINED
@ LFT_UNDEFINED
Definition ojph_mem.h:163
ojph::line_buf::LFT_SIZE_MASK
@ LFT_SIZE_MASK
Definition ojph_mem.h:171
ojph::line_buf::LFT_32BIT
@ LFT_32BIT
Definition ojph_mem.h:167
ojph::line_buf::LFT_BYTE
@ LFT_BYTE
Definition ojph_mem.h:165
ojph::line_buf::LFT_64BIT
@ LFT_64BIT
Definition ojph_mem.h:168
ojph::line_buf::LFT_16BIT
@ LFT_16BIT
Definition ojph_mem.h:166
ojph::line_buf::i64
si64 * i64
Definition ojph_mem.h:185
ojph::line_buf::size
size_t size
Definition ojph_mem.h:180
ojph::line_buf::p
void * p
Definition ojph_mem.h:187
ojph::line_buf::flags
ui32 flags
Definition ojph_mem.h:182
ojph::line_buf::f32
float * f32
Definition ojph_mem.h:186
ojph::line_buf::line_buf
line_buf()
Definition ojph_mem.h:175
ojph::line_buf::pre_size
ui32 pre_size
Definition ojph_mem.h:181
ojph::line_buf::wrap
void wrap(T *buffer, size_t num_ele, ui32 pre_size)
ojph::line_buf::i32
si32 * i32
Definition ojph_mem.h:184
ojph::mem_elastic_allocator::chunk_size
const ui32 chunk_size
Definition ojph_mem.h:274
ojph::mem_elastic_allocator::get_buffer
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:113
ojph::mem_elastic_allocator::store
stores_list * store
Definition ojph_mem.h:270
ojph::mem_elastic_allocator::restart
void restart()
Definition ojph_mem.cpp:130
ojph::mem_elastic_allocator::allocate
stores_list * allocate(stores_list **list, ui32 extended_bytes)
Definition ojph_mem.cpp:92
ojph::mem_elastic_allocator::~mem_elastic_allocator
~mem_elastic_allocator()
Definition ojph_mem.h:227
ojph::mem_elastic_allocator::mem_elastic_allocator
mem_elastic_allocator(ui32 chunk_size)
Definition ojph_mem.h:223
ojph::mem_elastic_allocator::avail
stores_list * avail
Definition ojph_mem.h:272
ojph::mem_elastic_allocator::cur_store
stores_list * cur_store
Definition ojph_mem.h:271
ojph::mem_elastic_allocator::total_allocated
size_t total_allocated
Definition ojph_mem.h:273
ojph::mem_fixed_allocator::pre_alloc_data
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:71
ojph::mem_fixed_allocator::preallocation
bool preallocation
Definition ojph_mem.h:155
ojph::mem_fixed_allocator::restart
void restart()
Definition ojph_mem.h:103
ojph::mem_fixed_allocator::alloc
void alloc()
Definition ojph_mem.h:82
ojph::mem_fixed_allocator::pre_alloc_obj
void pre_alloc_obj(size_t num_ele)
Definition ojph_mem.h:77
ojph::mem_fixed_allocator::avail_obj
void * avail_obj
Definition ojph_mem.h:152
ojph::mem_fixed_allocator::store
void * store
Definition ojph_mem.h:152
ojph::mem_fixed_allocator::pre_alloc_local
void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t &sz)
Definition ojph_mem.h:126
ojph::mem_fixed_allocator::size_data
size_t size_data
Definition ojph_mem.h:153
ojph::mem_fixed_allocator::post_alloc_data
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:111
ojph::mem_fixed_allocator::avail_data
void * avail_data
Definition ojph_mem.h:152
ojph::mem_fixed_allocator::avail_size_data
size_t avail_size_data
Definition ojph_mem.h:153
ojph::mem_fixed_allocator::avail_size_obj
size_t avail_size_obj
Definition ojph_mem.h:153
ojph::mem_fixed_allocator::allocated_data
size_t allocated_data
Definition ojph_mem.h:154
ojph::mem_fixed_allocator::~mem_fixed_allocator
~mem_fixed_allocator()
Definition ojph_mem.h:65
ojph::mem_fixed_allocator::mem_fixed_allocator
mem_fixed_allocator()
Definition ojph_mem.h:60
ojph::mem_fixed_allocator::size_obj
size_t size_obj
Definition ojph_mem.h:153
ojph::mem_fixed_allocator::post_alloc_obj
T * post_alloc_obj(size_t num_ele)
Definition ojph_mem.h:118
ojph::mem_fixed_allocator::post_alloc_local
T * post_alloc_local(size_t num_ele, ui32 pre_size, size_t &avail_sz, void *&avail_p)
Definition ojph_mem.h:137
ojph
Definition ojph_img_io.h:52
ojph::ojph_aligned_malloc
void * ojph_aligned_malloc(size_t alignment, size_t size)
ojph::si64
int64_t si64
Definition ojph_defs.h:57
ojph::calc_aligned_size
size_t calc_aligned_size(size_t size)
Definition ojph_arch.h:302
ojph::align_ptr
T * align_ptr(T *ptr)
Definition ojph_arch.h:312
ojph::si32
int32_t si32
Definition ojph_defs.h:55
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition ojph_defs.h:50
ojph::ojph_aligned_free
void ojph_aligned_free(void *pointer)
ojph_arch.h
ojph::coded_lists
Definition ojph_mem.h:201
ojph::coded_lists::buf_size
ui32 buf_size
Definition ojph_mem.h:210
ojph::coded_lists::coded_lists
coded_lists(ui32 size)
Definition ojph_mem.h:202
ojph::coded_lists::buf
ui8 * buf
Definition ojph_mem.h:212
ojph::coded_lists::next_list
coded_lists * next_list
Definition ojph_mem.h:209
ojph::coded_lists::avail_size
ui32 avail_size
Definition ojph_mem.h:211
ojph::lifting_buf::lifting_buf
lifting_buf()
Definition ojph_mem.h:194
ojph::lifting_buf::line
line_buf * line
Definition ojph_mem.h:195
ojph::lifting_buf::active
bool active
Definition ojph_mem.h:196
ojph::mem_elastic_allocator::stores_list
Definition ojph_mem.h:246
ojph::mem_elastic_allocator::stores_list::eval_store_bytes
static ui32 eval_store_bytes(ui32 available_bytes)
Definition ojph_mem.h:259
ojph::mem_elastic_allocator::stores_list::available
ui32 available
Definition ojph_mem.h:265
ojph::mem_elastic_allocator::stores_list::data
ui8 * data
Definition ojph_mem.h:264
ojph::mem_elastic_allocator::stores_list::stores_list
stores_list(ui32 available_bytes)
Definition ojph_mem.h:247
ojph::mem_elastic_allocator::stores_list::restart
void restart()
Definition ojph_mem.h:253
ojph::mem_elastic_allocator::stores_list::orig_data
ui8 * orig_data
Definition ojph_mem.h:264
ojph::mem_elastic_allocator::stores_list::next_store
stores_list * next_store
Definition ojph_mem.h:263
ojph::mem_elastic_allocator::stores_list::orig_size
ui32 orig_size
Definition ojph_mem.h:265
ojph::size
Definition ojph_base.h:48