Skip to content

Commit 8ecdca2

Browse files
author
Aaron Boxer
committed
cache: add common cache state interface
1 parent ef47ba8 commit 8ecdca2

35 files changed

+263
-248
lines changed

src/lib/jp2/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ include_directories(
5656
${CMAKE_CURRENT_SOURCE_DIR}/tile
5757
${CMAKE_CURRENT_SOURCE_DIR}/filters
5858
${CMAKE_CURRENT_SOURCE_DIR}/../highway
59+
${CMAKE_CURRENT_SOURCE_DIR}/cache
5960
)
6061

6162
# Defines the source code for executables
@@ -76,8 +77,6 @@ set(GROK_LIBRARY_SRCS
7677
${CMAKE_CURRENT_SOURCE_DIR}/util/MemStream.cpp
7778
${CMAKE_CURRENT_SOURCE_DIR}/util/MemStream.h
7879
${CMAKE_CURRENT_SOURCE_DIR}/util/grk_intmath.h
79-
${CMAKE_CURRENT_SOURCE_DIR}/util/MemManager.cpp
80-
${CMAKE_CURRENT_SOURCE_DIR}/util/MemManager.h
8180
${CMAKE_CURRENT_SOURCE_DIR}/util/util.h
8281
${CMAKE_CURRENT_SOURCE_DIR}/util/util.cpp
8382
${CMAKE_CURRENT_SOURCE_DIR}/util/CPUArch.h
@@ -132,8 +131,11 @@ set(GROK_LIBRARY_SRCS
132131
${CMAKE_CURRENT_SOURCE_DIR}/codestream/markers/PPMMarker.cpp
133132
${CMAKE_CURRENT_SOURCE_DIR}/codestream/markers/SOTMarker.h
134133
${CMAKE_CURRENT_SOURCE_DIR}/codestream/markers/SOTMarker.cpp
135-
${CMAKE_CURRENT_SOURCE_DIR}/codestream/TileCache.cpp
136-
${CMAKE_CURRENT_SOURCE_DIR}/codestream/TileCache.h
134+
135+
${CMAKE_CURRENT_SOURCE_DIR}/cache/TileCache.cpp
136+
${CMAKE_CURRENT_SOURCE_DIR}/cache/TileCache.h
137+
${CMAKE_CURRENT_SOURCE_DIR}/cache/MemManager.cpp
138+
${CMAKE_CURRENT_SOURCE_DIR}/cache/MemManager.h
137139

138140
${CMAKE_CURRENT_SOURCE_DIR}/point_transform/mct.cpp
139141
${CMAKE_CURRENT_SOURCE_DIR}/point_transform/mct.h

src/lib/jp2/cache/ICacheable.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2016-2021 Grok Image Compression Inc.
3+
*
4+
* This source code is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This source code is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#pragma once
18+
19+
namespace grk {
20+
21+
enum GrkCacheState {
22+
GRK_CACHE_STATE_CLOSED,
23+
GRK_CACHE_STATE_OPEN,
24+
GRK_CACHE_STATE_ERROR
25+
};
26+
27+
28+
class ICacheable {
29+
public:
30+
ICacheable() : m_state(GRK_CACHE_STATE_CLOSED)
31+
{}
32+
virtual ~ICacheable() = default;
33+
bool isOpen(void){
34+
return m_state == GRK_CACHE_STATE_OPEN;
35+
}
36+
bool isClosed(void){
37+
return m_state == GRK_CACHE_STATE_CLOSED;
38+
}
39+
bool isError(void){
40+
return m_state == GRK_CACHE_STATE_ERROR;
41+
}
42+
void setCacheState(GrkCacheState state){
43+
m_state = state;
44+
}
45+
private:
46+
GrkCacheState m_state;
47+
48+
};
49+
50+
}
51+

src/lib/jp2/util/MemManager.cpp renamed to src/lib/jp2/cache/MemManager.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535

3636
namespace grk {
3737

38-
const uint32_t grk_alignment = 32;
38+
const uint32_t GrkAlignment = 32;
3939

4040
uint32_t grkMakeAlignedWidth(uint32_t width){
4141
assert(width);
42-
return (uint32_t)((((uint64_t)width + grk_alignment - 1)/grk_alignment) * grk_alignment);
42+
return (uint32_t)((((uint64_t)width + GrkAlignment - 1)/GrkAlignment) * GrkAlignment);
4343
}
44-
45-
static inline void* grk_aligned_alloc_n(size_t alignment, size_t size) {
44+
static inline void* grkAlignedAllocN(size_t alignment, size_t size) {
4645
void *ptr;
4746

4847
/* alignment shall be power of 2 */
@@ -102,24 +101,22 @@ static inline void* grk_aligned_alloc_n(size_t alignment, size_t size) {
102101
#endif
103102
return ptr;
104103
}
105-
void* grk_malloc(size_t size) {
104+
void* grkMalloc(size_t size) {
106105
if (size == 0U) /* prevent implementation defined behavior of realloc */
107106
return nullptr;
108107

109108
return malloc(size);
110109
}
111-
void* grk_calloc(size_t num, size_t size) {
110+
void* grkCalloc(size_t num, size_t size) {
112111
if (num == 0 || size == 0)
113112
/* prevent implementation defined behavior of realloc */
114113
return nullptr;
115114

116115
return calloc(num, size);
117116
}
118-
119117
void* grkAlignedMalloc(size_t size) {
120-
return grk_aligned_alloc_n(default_align, size);
118+
return grkAlignedAllocN(default_align, size);
121119
}
122-
123120
void grkAlignedFree(void *ptr) {
124121
#if defined(GROK_HAVE_POSIX_MEMALIGN) || defined(GROK_HAVE_ALIGNED_ALLOC) || defined(GROK_HAVE_MEMALIGN)
125122
free(ptr);
@@ -131,14 +128,13 @@ void grkAlignedFree(void *ptr) {
131128
free(((void**) ptr)[-1]);
132129
#endif
133130
}
134-
135-
void* grk_realloc(void *ptr, size_t new_size) {
131+
void* grkRealloc(void *ptr, size_t new_size) {
136132
if (new_size == 0U)/* prevent implementation defined behavior of realloc */
137133
return nullptr;
138134

139135
return realloc(ptr, new_size);
140136
}
141-
void grk_free(void *ptr) {
137+
void grkFree(void *ptr) {
142138
if (ptr)
143139
free(ptr);
144140
}

src/lib/jp2/util/MemManager.h renamed to src/lib/jp2/cache/MemManager.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,46 @@
2525

2626
namespace grk {
2727

28-
29-
30-
3128
const size_t default_align = 64;
3229

3330
uint32_t grkMakeAlignedWidth(uint32_t width);
34-
3531
/**
3632
Allocate an uninitialized memory block
3733
@param size Bytes to allocate
3834
@return a void pointer to the allocated space, or nullptr if there is insufficient memory available
3935
*/
40-
void* grk_malloc(size_t size);
41-
36+
void* grkMalloc(size_t size);
4237
/**
4338
Allocate a memory block with elements initialized to 0
4439
@param numOfElements Blocks to allocate
4540
@param sizeOfElements Bytes per block to allocate
4641
@return a void pointer to the allocated space, or nullptr if there is insufficient memory available
4742
*/
48-
void* grk_calloc(size_t numOfElements, size_t sizeOfElements);
49-
43+
void* grkCalloc(size_t numOfElements, size_t sizeOfElements);
5044
/**
5145
Allocate memory aligned to a 16 byte boundary
5246
@param size Bytes to allocate
5347
@return a void pointer to the allocated space, or nullptr if there is insufficient memory available
5448
*/
5549
void* grkAlignedMalloc(size_t size);
5650
void grkAlignedFree(void *ptr);
57-
5851
/**
5952
Reallocate memory blocks.
6053
@param m Pointer to previously allocated memory block
6154
@param s New size in bytes
6255
@return a void pointer to the reallocated (and possibly moved) memory block
6356
*/
64-
void* grk_realloc(void *m, size_t s);
65-
57+
void* grkRealloc(void *m, size_t s);
6658
/**
6759
Deallocates or frees a memory block.
6860
@param m Previously allocated memory block to be freed
6961
*/
70-
void grk_free(void *m);
62+
void grkFree(void *m);
7163

7264
#if defined(__GNUC__) && !defined(GROK_SKIP_POISON)
7365
#pragma GCC poison malloc calloc realloc free
7466
#endif
7567

76-
7768
template<typename T> struct AllocatorVanilla{
7869
T* alloc(size_t length) {
7970
return new T[length];
@@ -193,7 +184,8 @@ template <typename T, template <typename TT> typename A > struct grkBuffer : A<T
193184
size_t offset; /* current offset into array */
194185
size_t len; /* length of array */
195186
bool owns_data; /* true if buffer manages the buf array */
196-
} ;
187+
};
188+
197189
using grkBufferU8 = grkBuffer<uint8_t, AllocatorVanilla >;
198190
using grkBufferU8Aligned = grkBuffer<uint8_t, AllocatorAligned >;
199191

@@ -296,11 +288,4 @@ template <typename T, template <typename TT> typename A> struct grkBuffer2d : pu
296288
uint32_t stride;
297289
} ;
298290

299-
300-
301-
/* ----------------------------------------------------------------------- */
302-
/*@}*/
303-
304-
/*@}*/
305-
306291
}

src/lib/jp2/codestream/TileCache.cpp renamed to src/lib/jp2/cache/TileCache.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ TileCache::~TileCache() {
3939
bool TileCache::empty(){
4040
return m_cache.empty();
4141
}
42-
4342
TileCacheEntry* TileCache::put(uint16_t tileIndex, TileProcessor *processor){
4443
TileCacheEntry *entry = nullptr;
4544
if (m_cache.find(tileIndex) != m_cache.end()) {
@@ -59,7 +58,6 @@ TileCacheEntry* TileCache::get(uint16_t tileIndex){
5958

6059
return nullptr;
6160
}
62-
6361
void TileCache::setStrategy(GRK_TILE_CACHE_STRATEGY strategy){
6462
m_strategy = strategy;
6563
}
File renamed without changes.

src/lib/jp2/codestream/CodeStream.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ CodeStream::~CodeStream(){
3838
grk_object_unref(&m_headerImage->obj);
3939
m_cp.destroy();
4040
if (cstr_index) {
41-
grk_free(cstr_index->marker);
41+
grkFree(cstr_index->marker);
4242
if (cstr_index->tile_index) {
4343
for (uint32_t i = 0; i < cstr_index->nb_of_tiles; i++) {
44-
grk_free(cstr_index->tile_index[i].tp_index);
45-
grk_free(cstr_index->tile_index[i].marker);
44+
grkFree(cstr_index->tile_index[i].tp_index);
45+
grkFree(cstr_index->tile_index[i].marker);
4646
}
47-
grk_free(cstr_index->tile_index);
47+
grkFree(cstr_index->tile_index);
4848
}
49-
grk_free(cstr_index);
49+
grkFree(cstr_index);
5050
}
5151
}
5252
CodingParams* CodeStream::getCodingParams(void){

0 commit comments

Comments
 (0)