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#include "ojph_message.h"
49
50namespace ojph {
51
52 extern "C" {
53 void* ojph_aligned_malloc(size_t alignment, size_t size);
54 void ojph_aligned_free(void* pointer);
55 }
56
59 {
60 public:
62 {
63 store = NULL; allocated_data = 0;
64 restart();
65 }
67 {
68 if (store) free(store);
69 }
70
71 template<typename T>
72 void pre_alloc_data(size_t num_ele, ui32 pre_size)
73 {
75 }
76
77 template<typename T>
78 void pre_alloc_obj(size_t num_ele)
79 {
81 }
82
83 void alloc()
84 {
85 assert(preallocation);
87 {
88 // We should be here once only, because, in subsequent, calls we
89 // should have size_data + size_obj <= allocated_data
90 free(store);
92 allocated_data = allocated_data + (allocated_data + 19) / 20; // 5%
93 store = malloc(allocated_data);
94 if (store == NULL)
95 OJPH_ERROR(0x00090001, "malloc failed");
96 }
101 preallocation = false;
102 }
103
104 void restart()
105 {
106 avail_obj = avail_data = NULL;
108 preallocation = true;
109 }
110
111 template<typename T>
112 T* post_alloc_data(size_t num_ele, ui32 pre_size)
113 {
115 (num_ele, pre_size, avail_size_data, avail_data);
116 }
117
118 template<typename T>
119 T* post_alloc_obj(size_t num_ele)
120 {
122 (num_ele, 0, avail_size_obj, avail_obj);
123 }
124
125 private:
126 template<typename T, int N>
127 void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t& sz)
128 {
129 assert(preallocation);
130 num_ele = calc_aligned_size<T, N>(num_ele);
131 size_t total = (num_ele + pre_size) * sizeof(T);
132 total += 2*N - 1;
133
134 sz += total;
135 }
136
137 template<typename T, int N>
138 T* post_alloc_local(size_t num_ele, ui32 pre_size,
139 size_t& avail_sz, void*& avail_p)
140 {
141 assert(!preallocation);
142 num_ele = calc_aligned_size<T, N>(num_ele);
143 size_t total = (num_ele + pre_size) * sizeof(T);
144 total += 2*N - 1;
145
146 T* p = align_ptr<T, N>((T*)avail_p + pre_size);
147 avail_p = (ui8*)avail_p + total;
148 avail_sz -= total;
149 assert((avail_sz & 0x8000000000000000llu) == 0);
150 return p;
151 }
152
157 };
158
161 {
162 public:
163 enum : ui32 {
164 LFT_UNDEFINED = 0x00, // Type is undefined/uninitialized
165 // These flags reflects data size in bytes
166 LFT_BYTE = 0x01, // Set when data is 1 byte (not used)
167 LFT_16BIT = 0x02, // Set when data is 2 bytes (not used)
168 LFT_32BIT = 0x04, // Set when data is 4 bytes
169 LFT_64BIT = 0x08, // Set when data is 8 bytes
170 LFT_INTEGER = 0x10, // Set when data is an integer, in other words
171 // 32bit integer, not 32bit float
172 LFT_SIZE_MASK = 0x0F, // To extract data size
173 };
174
175 public:
177
178 template<typename T>
179 void wrap(T *buffer, size_t num_ele, ui32 pre_size);
180
181 size_t size;
184 union {
185 si32* i32; // 32bit integer type, used for lossless compression
186 si64* i64; // 64bit integer type, used for lossless compression
187 float* f32; // float type, used for lossy compression
188 void* p; // no type is associated with the pointer
189 };
190 };
191
194 {
195 lifting_buf() { line = NULL; active = false; }
197 bool active;
198 };
199
202 {
204 {
205 next_list = NULL;
207 this->buf = (ui8*)this + sizeof(coded_lists);
208 }
209
214 };
215
218 {
219 /*
220 advantage: allocate large chunks of memory
221 */
222
223 public:
227
229 {
230 while (store) { // stores in use
231 stores_list* t = store->next_store;
232 free(store);
233 store = t;
234 }
235 while (avail) { // available stores
236 stores_list* t = avail->next_store;
237 free(avail);
238 avail = t;
239 }
240 }
241
242 void get_buffer(ui32 needed_bytes, coded_lists*& p);
243 void restart();
244
245 private:
247 {
248 // Payload (coded_lists + bitstream) must start at a multiple of 16 bytes.
249 // Otherwise coded_lists::buf can be 4 mod 8, which causes misalignment
250 // on 32-bit architectures. So round sizeof(stores_list) to next
251 // multiple of 16.
252 static constexpr ui32 stores_list_size16()
253 {
254 return (ui32) ((sizeof (stores_list) + 15u) & ~15u);
255 }
256 stores_list(ui32 available_bytes)
257 {
258 this->next_store = NULL;
259 this->orig_size = this->available = available_bytes;
260 this->orig_data = this->data = (ui8*)this + stores_list_size16();
261 }
262 void restart()
263 {
264 this->next_store = NULL;
265 this->available = this->orig_size;
266 this->data = this->orig_data;
267 }
268 static ui32 eval_store_bytes(ui32 available_bytes)
269 { // calculates how many bytes need to be allocated
270 return available_bytes + stores_list_size16();
271 }
275 };
276
277 stores_list* allocate(stores_list** list, ui32 extended_bytes);
278
284 };
285
286
287}
288
289
290#endif // !OJPH_MEM_H
float * f32
Definition ojph_mem.h:187
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:224
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:72
void pre_alloc_obj(size_t num_ele)
Definition ojph_mem.h:78
void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t &sz)
Definition ojph_mem.h:127
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:112
T * post_alloc_obj(size_t num_ele)
Definition ojph_mem.h:119
T * post_alloc_local(size_t num_ele, ui32 pre_size, size_t &avail_sz, void *&avail_p)
Definition ojph_mem.h:138
void * ojph_aligned_malloc(size_t alignment, size_t size)
int64_t si64
Definition ojph_defs.h:57
size_t calc_aligned_size(size_t size)
Definition ojph_arch.h:302
T * align_ptr(T *ptr)
Definition ojph_arch.h:312
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)
#define OJPH_ERROR(t,...)
coded_lists(ui32 size)
Definition ojph_mem.h:203
coded_lists * next_list
Definition ojph_mem.h:210
line_buf * line
Definition ojph_mem.h:196
static ui32 eval_store_bytes(ui32 available_bytes)
Definition ojph_mem.h:268
static constexpr ui32 stores_list_size16()
Definition ojph_mem.h:252