Grok  9.5.0
SparseBuffer.h
Go to the documentation of this file.
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 #include <vector>
18 
19 #pragma once
20 namespace grk
21 {
22 /* SparseBuffer
23 
24  Manage a list of buffers, which can be treated as one single
25  contiguous buffer.
26  */
28 {
29  SparseBuffer();
30  ~SparseBuffer();
31  grkBufferU8* pushBack(uint8_t* buf, size_t len, bool ownsData);
32  void incrementCurrentChunkOffset(size_t offset);
33  size_t getCurrentChunkLength(void);
34  // Treat segmented buffer as single contiguous buffer, and get current pointer
35  uint8_t* getCurrentChunkPtr(void);
36  // Reset all offsets to zero, and set current chunk to beginning of list
37  void rewind(void);
38  size_t skip(size_t numBytes);
39  void increment(void);
40  size_t read(void* buffer, size_t numBytes);
41 
42  private:
43  // Treat segmented buffer as single contiguous buffer, and get current offset
44  size_t getGlobalOffset(void);
45  // Copy all chunks, in sequence, into contiguous array
46  bool copyToContiguousBuffer(uint8_t* buffer);
47  // Clean up internal resources
48  void cleanup(void);
49  /*
50  Return current pointer, stored in ptr variable, and advance chunk buffer
51  offset by chunk_len
52  */
53  bool zeroCopyRead(uint8_t** ptr, size_t chunk_len);
54  size_t getCurrentChunkOffset(void);
55  void pushBack(grkBufferU8* chunk);
56  size_t dataLength; /* total length of all chunks*/
57  size_t currentChunkId; /* current index into chunk vector */
58  std::vector<grkBufferU8*> chunks;
59 };
60 
61 } // namespace grk
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20
Definition: SparseBuffer.h:28
void rewind(void)
Definition: SparseBuffer.cpp:122
~SparseBuffer()
Definition: SparseBuffer.cpp:24
bool zeroCopyRead(uint8_t **ptr, size_t chunk_len)
Zero copy read of contiguous chunk from current chunk.
Definition: SparseBuffer.cpp:143
SparseBuffer()
Definition: SparseBuffer.cpp:23
grkBufferU8 * pushBack(uint8_t *buf, size_t len, bool ownsData)
Definition: SparseBuffer.cpp:102
size_t getCurrentChunkOffset(void)
Definition: SparseBuffer.cpp:179
size_t read(void *buffer, size_t numBytes)
Definition: SparseBuffer.cpp:38
void cleanup(void)
Definition: SparseBuffer.cpp:116
void increment(void)
Definition: SparseBuffer.cpp:28
size_t currentChunkId
Definition: SparseBuffer.h:57
bool copyToContiguousBuffer(uint8_t *buffer)
Definition: SparseBuffer.cpp:155
uint8_t * getCurrentChunkPtr(void)
Definition: SparseBuffer.cpp:169
size_t getGlobalOffset(void)
Definition: SparseBuffer.cpp:184
std::vector< grkBufferU8 * > chunks
Definition: SparseBuffer.h:58
void incrementCurrentChunkOffset(size_t offset)
Definition: SparseBuffer.cpp:132
size_t getCurrentChunkLength(void)
Definition: SparseBuffer.cpp:174
size_t dataLength
Definition: SparseBuffer.h:56
size_t skip(size_t numBytes)
Definition: SparseBuffer.cpp:70