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
52#ifdef OJPH_OS_WINDOWS
53 inline void* ojph_aligned_malloc(size_t alignment, size_t size)
54 {
55 return _aligned_malloc(size, alignment);
56 }
57
58 inline void ojph_aligned_free(void* pointer)
59 {
60 return _aligned_free(pointer);
61 }
62#else
63 inline void* ojph_aligned_malloc(size_t alignment, size_t size)
64 {
65 return aligned_alloc(alignment, size);
66 }
67
68 inline void ojph_aligned_free(void* pointer)
69 {
70 return free(pointer);
71 }
72#endif
73
76 {
77 public:
79 {
80 store = NULL; allocated_data = 0;
81 restart();
82 }
84 {
85 if (store) free(store);
86 }
87
88 template<typename T>
89 void pre_alloc_data(size_t num_ele, ui32 pre_size)
90 {
92 }
93
94 template<typename T>
95 void pre_alloc_obj(size_t num_ele)
96 {
98 }
99
100 void alloc()
101 {
102 assert(preallocation);
104 {
105 // We should be here once only, because, in subsequent, calls we
106 // should have size_data + size_obj <= allocated_data
107 free(store);
109 allocated_data = allocated_data + (allocated_data + 19) / 20; // 5%
110 store = malloc(allocated_data);
111 if (store == NULL)
112 throw "malloc failed";
113 }
118 preallocation = false;
119 }
120
121 void restart()
122 {
123 avail_obj = avail_data = NULL;
125 preallocation = true;
126 }
127
128 template<typename T>
129 T* post_alloc_data(size_t num_ele, ui32 pre_size)
130 {
132 (num_ele, pre_size, avail_size_data, avail_data);
133 }
134
135 template<typename T>
136 T* post_alloc_obj(size_t num_ele)
137 {
139 (num_ele, 0, avail_size_obj, avail_obj);
140 }
141
142 private:
143 template<typename T, int N>
144 void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t& sz)
145 {
146 assert(preallocation);
147 num_ele = calc_aligned_size<T, N>(num_ele);
148 size_t total = (num_ele + pre_size) * sizeof(T);
149 total += 2*N - 1;
150
151 sz += total;
152 }
153
154 template<typename T, int N>
155 T* post_alloc_local(size_t num_ele, ui32 pre_size,
156 size_t& avail_sz, void*& avail_p)
157 {
158 assert(!preallocation);
159 num_ele = calc_aligned_size<T, N>(num_ele);
160 size_t total = (num_ele + pre_size) * sizeof(T);
161 total += 2*N - 1;
162
163 T* p = align_ptr<T, N>((T*)avail_p + pre_size);
164 avail_p = (ui8*)avail_p + total;
165 avail_sz -= total;
166 assert((avail_sz & 0x8000000000000000llu) == 0);
167 return p;
168 }
169
174 };
175
178 {
179 public:
180 enum : ui32 {
181 LFT_UNDEFINED = 0x00, // Type is undefined/uninitialized
182 // These flags reflects data size in bytes
183 LFT_BYTE = 0x01, // Set when data is 1 byte (not used)
184 LFT_16BIT = 0x02, // Set when data is 2 bytes (not used)
185 LFT_32BIT = 0x04, // Set when data is 4 bytes
186 LFT_64BIT = 0x08, // Set when data is 8 bytes
187 LFT_INTEGER = 0x10, // Set when data is an integer, in other words
188 // 32bit integer, not 32bit float
189 LFT_SIZE_MASK = 0x0F, // To extract data size
190 };
191
192 public:
194
195 template<typename T>
196 void wrap(T *buffer, size_t num_ele, ui32 pre_size);
197
198 size_t size;
201 union {
202 si32* i32; // 32bit integer type, used for lossless compression
203 si64* i64; // 64bit integer type, used for lossless compression
204 float* f32; // float type, used for lossy compression
205 void* p; // no type is associated with the pointer
206 };
207 };
208
211 {
212 lifting_buf() { line = NULL; active = false; }
214 bool active;
215 };
216
219 {
221 {
222 next_list = NULL;
224 this->buf = (ui8*)this + sizeof(coded_lists);
225 }
226
231 };
232
235 {
236 /*
237 advantage: allocate large chunks of memory
238 */
239
240 public:
244
246 {
247 while (store) { // stores in use
248 stores_list* t = store->next_store;
249 free(store);
250 store = t;
251 }
252 while (avail) { // available stores
253 stores_list* t = avail->next_store;
254 free(avail);
255 avail = t;
256 }
257 }
258
259 void get_buffer(ui32 needed_bytes, coded_lists*& p);
260 void restart();
261
262 private:
264 {
265 stores_list(ui32 available_bytes)
266 {
267 this->next_store = NULL;
268 this->orig_size = this->available = available_bytes;
269 this->orig_data = this->data = (ui8*)this + sizeof(stores_list);
270 }
271 void restart()
272 {
273 this->next_store = NULL;
274 this->available = this->orig_size;
275 this->data = this->orig_data;
276 }
277 static ui32 eval_store_bytes(ui32 available_bytes)
278 { // calculates how many bytes need to be allocated
279 return available_bytes + (ui32)sizeof(stores_list);
280 }
284 };
285
286 stores_list* allocate(stores_list** list, ui32 extended_bytes);
287
293 };
294
295
296}
297
298
299#endif // !OJPH_MEM_H
float * f32
Definition ojph_mem.h:204
void wrap(T *buffer, size_t num_ele, ui32 pre_size)
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:113
stores_list * allocate(stores_list **list, ui32 extended_bytes)
Definition ojph_mem.cpp:92
mem_elastic_allocator(ui32 chunk_size)
Definition ojph_mem.h:241
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:89
void pre_alloc_obj(size_t num_ele)
Definition ojph_mem.h:95
void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t &sz)
Definition ojph_mem.h:144
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:129
T * post_alloc_obj(size_t num_ele)
Definition ojph_mem.h:136
T * post_alloc_local(size_t num_ele, ui32 pre_size, size_t &avail_sz, void *&avail_p)
Definition ojph_mem.h:155
void * ojph_aligned_malloc(size_t alignment, size_t size)
Definition ojph_mem.h:63
int64_t si64
Definition ojph_defs.h:57
size_t calc_aligned_size(size_t size)
Definition ojph_arch.h:298
T * align_ptr(T *ptr)
Definition ojph_arch.h:308
int32_t si32
Definition ojph_defs.h:55
uint32_t ui32
Definition ojph_defs.h:54
uint8_t ui8
Definition ojph_defs.h:50
void ojph_aligned_free(void *pointer)
Definition ojph_mem.h:68
coded_lists(ui32 size)
Definition ojph_mem.h:220
coded_lists * next_list
Definition ojph_mem.h:227
line_buf * line
Definition ojph_mem.h:213
static ui32 eval_store_bytes(ui32 available_bytes)
Definition ojph_mem.h:277