| 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 |
|
| 49 |
namespace ojph { |
| 50 |
|
| 51 |
///////////////////////////////////////////////////////////////////////////// |
| 52 |
class mem_fixed_allocator |
| 53 |
{ |
| 54 |
public: |
| 55 |
mem_fixed_allocator() |
| 56 |
{ |
| 57 |
avail_obj = avail_data = store = NULL; |
| 58 |
avail_size_obj = avail_size_data = size_obj = size_data = 0; |
| 59 |
} |
| 60 |
~mem_fixed_allocator() |
| 61 |
{ |
| 62 |
if (store) free(store); |
| 63 |
} |
| 64 |
|
| 65 |
template<typename T> |
| 66 |
void pre_alloc_data(size_t num_ele, ui32 pre_size) |
| 67 |
{ |
| 68 |
pre_alloc_local<T, byte_alignment>(num_ele, pre_size, size_data); |
| 69 |
} |
| 70 |
|
| 71 |
template<typename T> |
| 72 |
void pre_alloc_obj(size_t num_ele) |
| 73 |
{ |
| 74 |
pre_alloc_local<T, object_alignment>(num_ele, 0, size_obj); |
| 75 |
} |
| 76 |
|
| 77 |
void alloc() |
| 78 |
{ |
| 79 |
assert(store == NULL); |
| 80 |
avail_obj = store = malloc(size_data + size_obj); |
| 81 |
avail_data = (ui8*)store + size_obj; |
| 82 |
if (store == NULL) |
| 83 |
throw "malloc failed"; |
| 84 |
avail_size_obj = size_obj; |
| 85 |
avail_size_data = size_data; |
| 86 |
} |
| 87 |
|
| 88 |
template<typename T> |
| 89 |
T* post_alloc_data(size_t num_ele, ui32 pre_size) |
| 90 |
{ |
| 91 |
return post_alloc_local<T, byte_alignment> |
| 92 |
(num_ele, pre_size, avail_size_data, avail_data); |
| 93 |
} |
| 94 |
|
| 95 |
template<typename T> |
| 96 |
T* post_alloc_obj(size_t num_ele) |
| 97 |
{ |
| 98 |
return post_alloc_local<T, object_alignment> |
| 99 |
(num_ele, 0, avail_size_obj, avail_obj); |
| 100 |
} |
| 101 |
|
| 102 |
private: |
| 103 |
template<typename T, int N> |
| 104 |
void pre_alloc_local(size_t num_ele, ui32 pre_size, size_t& sz) |
| 105 |
{ |
| 106 |
assert(store == NULL); |
| 107 |
num_ele = calc_aligned_size<T, N>(num_ele); |
| 108 |
size_t total = (num_ele + pre_size) * sizeof(T); |
| 109 |
total += 2*N - 1; |
| 110 |
|
| 111 |
sz += total; |
| 112 |
} |
| 113 |
|
| 114 |
template<typename T, int N> |
| 115 |
T* post_alloc_local(size_t num_ele, ui32 pre_size, |
| 116 |
size_t& avail_sz, void*& avail_p) |
| 117 |
{ |
| 118 |
assert(store != NULL); |
| 119 |
num_ele = calc_aligned_size<T, N>(num_ele); |
| 120 |
size_t total = (num_ele + pre_size) * sizeof(T); |
| 121 |
total += 2*N - 1; |
| 122 |
|
| 123 |
T* p = align_ptr<T, N>((T*)avail_p + pre_size); |
| 124 |
avail_p = (ui8*)avail_p + total; |
| 125 |
avail_sz -= total; |
| 126 |
assert((avail_sz & 0x8000000000000000llu) == 0); |
| 127 |
return p; |
| 128 |
} |
| 129 |
|
| 130 |
void *store, *avail_data, *avail_obj; |
| 131 |
size_t size_data, size_obj, avail_size_obj, avail_size_data; |
| 132 |
}; |
| 133 |
|
| 134 |
///////////////////////////////////////////////////////////////////////////// |
| 135 |
struct line_buf |
| 136 |
{ |
| 137 |
template<typename T> |
| 138 |
void pre_alloc(mem_fixed_allocator *p, size_t num_ele, ui32 pre_size) |
| 139 |
{ |
| 140 |
memset(this, 0, sizeof(line_buf)); |
| 141 |
p->pre_alloc_data<T>(num_ele, pre_size); |
| 142 |
size = num_ele; |
| 143 |
this->pre_size = pre_size; |
| 144 |
} |
| 145 |
|
| 146 |
template<typename T> |
| 147 |
void finalize_alloc(mem_fixed_allocator *p); |
| 148 |
|
| 149 |
template<typename T> |
| 150 |
void wrap(T *buffer, size_t num_ele, ui32 pre_size); |
| 151 |
|
| 152 |
size_t size; |
| 153 |
ui32 pre_size; |
| 154 |
union { |
| 155 |
si32* i32; |
| 156 |
float* f32; |
| 157 |
}; |
| 158 |
}; |
| 159 |
|
| 160 |
///////////////////////////////////////////////////////////////////////////// |
| 161 |
struct coded_lists |
| 162 |
{ |
| 163 |
coded_lists(ui32 size) |
| 164 |
{ |
| 165 |
next_list = NULL; |
| 166 |
avail_size = buf_size = size; |
| 167 |
this->buf = (ui8*)this + sizeof(coded_lists); |
| 168 |
} |
| 169 |
|
| 170 |
coded_lists* next_list; |
| 171 |
ui32 buf_size; |
| 172 |
ui32 avail_size; |
| 173 |
ui8* buf; |
| 174 |
}; |
| 175 |
|
| 176 |
///////////////////////////////////////////////////////////////////////////// |
| 177 |
class mem_elastic_allocator |
| 178 |
{ |
| 179 |
/* |
| 180 |
advantage: allocate large chunks of memory |
| 181 |
*/ |
| 182 |
|
| 183 |
public: |
| 184 |
mem_elastic_allocator(ui32 chunk_size) |
| 185 |
: chunk_size(chunk_size) |
| 186 |
{ cur_store = store = NULL; total_allocated = 0; } |
| 187 |
|
| 188 |
~mem_elastic_allocator() |
| 189 |
{ |
| 190 |
while (store) { |
| 191 |
stores_list* t = store->next_store; |
| 192 |
free(store); |
| 193 |
store = t; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
void get_buffer(ui32 needed_bytes, coded_lists*& p); |
| 198 |
|
| 199 |
private: |
| 200 |
struct stores_list |
| 201 |
{ |
| 202 |
stores_list(ui32 chunk_size) |
| 203 |
{ |
| 204 |
this->next_store = NULL; |
| 205 |
this->available = chunk_size - (ui32)sizeof(stores_list); |
| 206 |
this->data = (char*)this + sizeof(stores_list); |
| 207 |
} |
| 208 |
stores_list *next_store; |
| 209 |
ui32 available; |
| 210 |
char* data; |
| 211 |
}; |
| 212 |
|
| 213 |
stores_list *store, *cur_store; |
| 214 |
size_t total_allocated; |
| 215 |
const ui32 chunk_size; |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
#endif // !OJPH_MEM_H |