Grok  7.6.2
ChunkBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2020 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 
18 #include <vector>
19 
20 #pragma once
21 namespace grk {
22 
23 /* ChunkBuffer
24 
25  Store a list of buffers, or chunks, which can be treated as one single
26  contiguous buffer.
27 
28  */
29 struct ChunkBuffer {
30  ChunkBuffer();
31  ~ChunkBuffer();
32 
33 
34 
35  grk_buf* push_back(uint8_t *buf, size_t len, bool ownsData);
36 
37  /*
38  Allocate array and add to the back of the chunk buffer
39  */
40  bool alloc_and_push_back(size_t len);
41 
42  /*
43  Increment offset of current chunk
44  */
45  void incr_cur_chunk_offset(size_t offset);
46 
47  /*
48  Get length of current chunk
49  */
50  size_t get_cur_chunk_len(void);
51 
52  /*
53  Treat segmented buffer as single contiguous buffer, and get current pointer
54  */
55  uint8_t* get_cur_chunk_ptr(void);
56 
57  /*
58  Reset all offsets to zero, and set current chunk to beginning of list
59  */
60  void rewind(void);
61 
62  size_t skip(size_t nb_bytes);
63 
64  void increment(void);
65 
66  size_t read(void *p_buffer, size_t nb_bytes);
67 
68 private:
69  /*
70  Treat segmented buffer as single contiguous buffer, and get current offset
71  */
72  size_t get_global_offset(void);
73 
74 
75  /*
76  Copy all chunks, in sequence, into contiguous array
77  */
78  bool copy_to_contiguous_buffer(uint8_t *buffer);
79 
80  /*
81  Clean up internal resources
82  */
83  void cleanup(void);
84 
85 
86  /*
87  Return current pointer, stored in ptr variable, and advance chunk buffer
88  offset by chunk_len
89  */
90  bool zero_copy_read(uint8_t **ptr, size_t chunk_len);
91 
92 
93  /*
94  Get offset of current chunk
95  */
96  size_t get_cur_chunk_offset(void);
97 
98  void push_back(grk_buf *chunk);
99 
100  size_t data_len; /* total length of all chunks*/
101  size_t cur_chunk_id; /* current index into chunk vector */
102  std::vector<grk_buf*> chunks;
103 };
104 
105 }
grk::ChunkBuffer::cur_chunk_id
size_t cur_chunk_id
Definition: ChunkBuffer.h:101
grk::grk_buffer< uint8_t >
grk::ChunkBuffer::cleanup
void cleanup(void)
Definition: ChunkBuffer.cpp:128
grk::ChunkBuffer::read
size_t read(void *p_buffer, size_t nb_bytes)
Definition: ChunkBuffer.cpp:42
grk::grk_buffer::offset
size_t offset
Definition: util.h:264
grk::ChunkBuffer::get_cur_chunk_offset
size_t get_cur_chunk_offset(void)
Definition: ChunkBuffer.cpp:210
grk::ChunkBuffer::chunks
std::vector< grk_buf * > chunks
Definition: ChunkBuffer.h:102
grk::ChunkBuffer::increment
void increment(void)
Definition: ChunkBuffer.cpp:31
grk::GRK_WARN
void GRK_WARN(const char *fmt,...)
Definition: logger.cpp:49
grk::ChunkBuffer::skip
size_t skip(size_t nb_bytes)
Definition: ChunkBuffer.cpp:78
grk::grk_buf
grk_buffer< uint8_t > grk_buf
Definition: util.h:268
grk::ChunkBuffer::push_back
grk_buf * push_back(uint8_t *buf, size_t len, bool ownsData)
Definition: ChunkBuffer.cpp:113
grk::ChunkBuffer::get_global_offset
size_t get_global_offset(void)
Definition: ChunkBuffer.cpp:216
grk::ChunkBuffer::copy_to_contiguous_buffer
bool copy_to_contiguous_buffer(uint8_t *buffer)
Definition: ChunkBuffer.cpp:182
grk::ChunkBuffer::data_len
size_t data_len
Definition: ChunkBuffer.h:100
grk
Copyright (C) 2016-2020 Grok Image Compression Inc.
Definition: BitIO.cpp:23
grk::ChunkBuffer::alloc_and_push_back
bool alloc_and_push_back(size_t len)
Definition: ChunkBuffer.cpp:143
grk::ChunkBuffer::get_cur_chunk_ptr
uint8_t * get_cur_chunk_ptr(void)
Definition: ChunkBuffer.cpp:198
grk::grk_buffer::len
size_t len
Definition: util.h:265
grk_includes.h
grk::ChunkBuffer::~ChunkBuffer
~ChunkBuffer()
Definition: ChunkBuffer.cpp:27
grk::ChunkBuffer::rewind
void rewind(void)
Definition: ChunkBuffer.cpp:134
grk::ChunkBuffer::get_cur_chunk_len
size_t get_cur_chunk_len(void)
Definition: ChunkBuffer.cpp:204
grk::ChunkBuffer
Definition: ChunkBuffer.h:29
grk::ChunkBuffer::incr_cur_chunk_offset
void incr_cur_chunk_offset(size_t offset)
Definition: ChunkBuffer.cpp:156
grk::ChunkBuffer::ChunkBuffer
ChunkBuffer()
Definition: ChunkBuffer.cpp:23
grk::ChunkBuffer::zero_copy_read
bool zero_copy_read(uint8_t **ptr, size_t chunk_len)
Zero copy read of contiguous chunk from current chunk.
Definition: ChunkBuffer.cpp:168