Skip to content

Commit f6e094c

Browse files
authored
Merge pull request #209 from aous72/issues
This address a few issues; PR #200, issues #206 and #207.
2 parents c7145b2 + 269d337 commit f6e094c

File tree

5 files changed

+87
-48
lines changed

5 files changed

+87
-48
lines changed

CMakeLists.txt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1313

1414
## Target architecture
1515
# We use the target architecture to help with arranging files in "source_group" commands.
16-
# The code does not use the results provided by target_arch.cmake, and relies, instead,
16+
# The code does not use the results provided by target_arch.cmake, and relies, instead,
1717
# on its own logic, which matches that in target_arch.cmake, to identify the architecture
1818
include(target_arch.cmake)
1919
target_architecture(OJPH_TARGET_ARCH)
@@ -58,8 +58,8 @@ if (DEFINED OJPH_DISABLE_INTEL_SIMD)
5858
"which is architecture agnostic. If you do not specify any, the default is "
5959
"OJPH_DISABLE_SIMD=OFF.")
6060
set(OJPH_DISABLE_SIMD ${OJPH_DISABLE_INTEL_SIMD})
61-
message(STATUS "OJPH_DISABLE_SIMD is set to ${OJPH_DISABLE_SIMD}")
62-
unset(OJPH_DISABLE_INTEL_SIMD)
61+
message(STATUS "OJPH_DISABLE_SIMD is set to ${OJPH_DISABLE_SIMD}")
62+
unset(OJPH_DISABLE_INTEL_SIMD)
6363
endif()
6464
if (DEFINED OJPH_ENABLE_INTEL_AVX512)
6565
message(STATUS "OJPH_ENABLE_INTEL_AVX512 is being deprecated, use \"OJPH_DISABLE_AVX512\" instead."
@@ -69,21 +69,21 @@ if (DEFINED OJPH_ENABLE_INTEL_AVX512)
6969
else()
7070
set(OJPH_DISABLE_AVX512 ON)
7171
endif()
72-
message(STATUS "OJPH_DISABLE_AVX512 is set to ${OJPH_DISABLE_AVX512}")
72+
message(STATUS "OJPH_DISABLE_AVX512 is set to ${OJPH_DISABLE_AVX512}")
7373
unset(OJPH_ENABLE_INTEL_AVX512)
7474
endif()
7575

7676
## Setting some of the options if EMSCRIPTEN is the compiler
77-
# In previous releases, the cmake script used to produce both non-SIMD and
78-
# SIMD builds in one go. At the time of this writing, all interpreters and
79-
# compilers of WASM code, such as web-browser and node, support SIMD, therefore
80-
# it is time to make the SIMD build the default. In other words, this cmake
77+
# In previous releases, the cmake script used to produce both non-SIMD and
78+
# SIMD builds in one go. At the time of this writing, all interpreters and
79+
# compilers of WASM code, such as web-browser and node, support SIMD, therefore
80+
# it is time to make the SIMD build the default. In other words, this cmake
8181
# script builds only WASM SIMD code by default, if desired, a non-SIMD build
82-
# can be generated using the OJPH_DISABLE_SIMD option (in this case, the
82+
# can be generated using the OJPH_DISABLE_SIMD option (in this case, the
8383
# WASM SIMD code is not generated).
8484
# It is worth remembering that the SIMD/non-SIMD issue arose because it is
8585
# NOT possible to have multiple execution paths in the code, one for non-SIMD
86-
# and one for SIMD, as we do for CPUs, letting the program select, at run-time,
86+
# and one for SIMD, as we do for CPUs, letting the program select, at run-time,
8787
# the best path to follow.
8888
if(EMSCRIPTEN)
8989
set(BUILD_SHARED_LIBS OFF)
@@ -119,16 +119,20 @@ message(STATUS "Building ${CMAKE_BUILD_TYPE}")
119119

120120
## C++ version and flags
121121
# C++14 is needed for gtest, otherwise, C++11 is sufficient for the library
122-
set(CMAKE_CXX_STANDARD 14)
122+
if (NOT CMAKE_CXX_STANDARD)
123+
set(CMAKE_CXX_STANDARD 14)
124+
endif()
125+
message(STATUS "C++ Standard is set to ${CMAKE_CXX_STANDARD}")
126+
123127
if (MSVC)
124128
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
125129
endif()
126130
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
127131
add_compile_options(
128-
-fexceptions
129-
-Wall
130-
-Wextra
131-
-Wconversion
132+
-fexceptions
133+
-Wall
134+
-Wextra
135+
-Wconversion
132136
-Wunused-parameter
133137
)
134138
endif()
@@ -170,6 +174,7 @@ else()
170174
endif()
171175

172176
## Build library and applications
177+
include(GNUInstallDirs)
173178
add_subdirectory(src/core)
174179
if (OJPH_BUILD_EXECUTABLES)
175180
add_subdirectory(src/apps)
@@ -179,8 +184,6 @@ endif()
179184
# Install
180185
################################################################################################
181186

182-
include(GNUInstallDirs)
183-
184187
install(EXPORT openjph-targets
185188
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/openjph
186189
)

src/core/common/ojph_file.h

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// This software is released under the 2-Clause BSD license, included
33
// below.
44
//
5-
// Copyright (c) 2019, Aous Naman
5+
// Copyright (c) 2019, Aous Naman
66
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
77
// Copyright (c) 2019, The University of New South Wales, Australia
8-
//
8+
//
99
// Redistribution and use in source and binary forms, with or without
1010
// modification, are permitted provided that the following conditions are
1111
// met:
12-
//
12+
//
1313
// 1. Redistributions of source code must retain the above copyright
1414
// notice, this list of conditions and the following disclaimer.
15-
//
15+
//
1616
// 2. Redistributions in binary form must reproduce the above copyright
1717
// notice, this list of conditions and the following disclaimer in the
1818
// documentation and/or other materials provided with the distribution.
19-
//
19+
//
2020
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2121
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2222
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
@@ -84,10 +84,10 @@ namespace ojph {
8484

8585
virtual size_t write(const void *ptr, size_t size) = 0;
8686
virtual si64 tell() { return 0; }
87-
virtual int seek(si64 offset, enum outfile_base::seek origin)
88-
{
87+
virtual int seek(si64 offset, enum outfile_base::seek origin)
88+
{
8989
ojph_unused(offset); ojph_unused(origin);
90-
return -1; /* always fail, to remind you to write an implementation */
90+
return -1; /* always fail, to remind you to write an implementation */
9191
}
9292
virtual void flush() {}
9393
virtual void close() {}
@@ -131,19 +131,19 @@ namespace ojph {
131131
/** A destructor */
132132
~mem_outfile() override;
133133

134-
/**
134+
/**
135135
* @brief Call this function to open a memory file.
136136
*
137137
* This function creates a memory buffer to be used for storing
138138
* the generated j2k codestream.
139-
*
139+
*
140140
* @param initial_size is the initial memory buffer size.
141141
* The default value is 2^16.
142142
* @param clear_mem if set to true, all allocated memory is reset to 0
143143
*/
144144
void open(size_t initial_size = 65536, bool clear_mem = false);
145145

146-
/**
146+
/**
147147
* @brief Call this function to write data to the memory file.
148148
*
149149
* This function adds new data to the memory file. The memory buffer
@@ -154,16 +154,16 @@ namespace ojph {
154154
*/
155155
size_t write(const void *ptr, size_t size) override;
156156

157-
/**
158-
* @brief Call this function to know the file size (i.e., number of
157+
/**
158+
* @brief Call this function to know the file size (i.e., number of
159159
* bytes used to store the file).
160160
*
161161
* @return the file size.
162162
*/
163163
si64 tell() override { return cur_ptr - buf; }
164164

165-
/**
166-
* @brief Call this function to change write pointer location; the
165+
/**
166+
* @brief Call this function to change write pointer location; the
167167
* function can expand file storage.
168168
*
169169
* @return 0 on success, non-zero otherwise (not used).
@@ -176,7 +176,7 @@ namespace ojph {
176176
*/
177177
void close() override;
178178

179-
/**
179+
/**
180180
* @brief Call this function to access memory file data.
181181
*
182182
* It is not recommended to store the returned value because buffer
@@ -186,8 +186,8 @@ namespace ojph {
186186
*/
187187
const ui8* get_data() { return buf; }
188188

189-
/**
190-
* @brief Call this function to access memory file data (for const
189+
/**
190+
* @brief Call this function to access memory file data (for const
191191
* objects)
192192
*
193193
* This is similar to the above function, except that it can be used
@@ -197,7 +197,7 @@ namespace ojph {
197197
*/
198198
const ui8* get_data() const { return buf; }
199199

200-
/**
200+
/**
201201
* @brief Call this function to write the memory file data to a file
202202
*
203203
*/
@@ -221,10 +221,10 @@ namespace ojph {
221221
private:
222222
/**
223223
* @brief This function expands storage by x1.5 needed space.
224-
*
224+
*
225225
* It sets cur_ptr correctly, and clears the extended area of the
226226
* buffer. It optionally clear the whole buffer
227-
*
227+
*
228228
* @param new_size New size of the buffer
229229
* @param clear_all Set to true to clear whole buffer, not just expansion
230230
*/
@@ -237,6 +237,9 @@ namespace ojph {
237237
size_t used_size;
238238
ui8 *buf;
239239
ui8 *cur_ptr;
240+
241+
private:
242+
static const size_t ALIGNED_ALLOC_MASK = 4096 - 1;
240243
};
241244

242245
////////////////////////////////////////////////////////////////////////////

src/core/common/ojph_mem.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@
4848

4949
namespace ojph {
5050

51+
////////////////////////////////////////////////////////////////////////////
52+
#ifdef OJPH_COMPILER_MSVC
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+
5174
/////////////////////////////////////////////////////////////////////////////
5275
class mem_fixed_allocator
5376
{

src/core/common/ojph_version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
//***************************************************************************/
3535

3636
#define OPENJPH_VERSION_MAJOR 0
37-
#define OPENJPH_VERSION_MINOR 23
38-
#define OPENJPH_VERSION_PATCH 1
37+
#define OPENJPH_VERSION_MINOR 24
38+
#define OPENJPH_VERSION_PATCH 0

src/core/others/ojph_file.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <cassert>
4444
#include <cstddef>
4545

46+
#include "ojph_mem.h"
4647
#include "ojph_file.h"
4748
#include "ojph_message.h"
4849

@@ -110,7 +111,7 @@ namespace ojph {
110111
mem_outfile::~mem_outfile()
111112
{
112113
if (buf)
113-
free(buf);
114+
ojph_aligned_free(buf);
114115
is_open = clear_mem = false;
115116
buf_size = used_size = 0;
116117
buf = cur_ptr = NULL;
@@ -201,18 +202,27 @@ namespace ojph {
201202
if (needed_size > buf_size)
202203
{
203204
needed_size += (needed_size + 1) >> 1; // x1.5
204-
si64 used_size = tell(); // current used size
205+
// expand buffer to multiples of (ALIGNED_ALLOC_MASK + 1)
206+
needed_size = (needed_size + ALIGNED_ALLOC_MASK) & (~ALIGNED_ALLOC_MASK);
205207

206-
if (this->buf)
207-
this->buf = (ui8*)realloc(this->buf, needed_size);
208-
else
209-
this->buf = (ui8*)malloc(needed_size);
208+
ui8* new_buf;
209+
new_buf = (ui8*)ojph_aligned_malloc(ALIGNED_ALLOC_MASK + 1, needed_size);
210+
if (new_buf == NULL)
211+
OJPH_ERROR(0x00060005, "failed to allocate memory (%zu bytes)",
212+
needed_size);
213+
214+
if (this->buf != NULL)
215+
{
216+
if (!clear_all)
217+
memcpy(new_buf, this->buf, used_size);
218+
ojph_aligned_free(this->buf);
219+
}
220+
this->cur_ptr = new_buf + tell();
221+
this->buf = new_buf;
210222

211223
if (clear_mem && !clear_all) // will be cleared later
212224
memset(this->buf + buf_size, 0, needed_size - this->buf_size);
213-
214225
this->buf_size = needed_size;
215-
this->cur_ptr = this->buf + used_size;
216226
}
217227
if (clear_all)
218228
memset(this->buf, 0, this->buf_size);

0 commit comments

Comments
 (0)