Grok  7.6.6
ChunkBuffer.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 
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 }
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
Definition: ChunkBuffer.h:29
size_t data_len
Definition: ChunkBuffer.h:100
void cleanup(void)
Definition: ChunkBuffer.cpp:128
std::vector< grk_buf * > chunks
Definition: ChunkBuffer.h:102
void increment(void)
Definition: ChunkBuffer.cpp:31
bool alloc_and_push_back(size_t len)
Definition: ChunkBuffer.cpp:143
size_t cur_chunk_id
Definition: ChunkBuffer.h:101
size_t get_cur_chunk_offset(void)
Definition: ChunkBuffer.cpp:210
bool zero_copy_read(uint8_t **ptr, size_t chunk_len)
Zero copy read of contiguous chunk from current chunk.
Definition: ChunkBuffer.cpp:168
ChunkBuffer()
Definition: ChunkBuffer.cpp:23
void rewind(void)
Definition: ChunkBuffer.cpp:134
uint8_t * get_cur_chunk_ptr(void)
Definition: ChunkBuffer.cpp:198
~ChunkBuffer()
Definition: ChunkBuffer.cpp:27
size_t skip(size_t nb_bytes)
Definition: ChunkBuffer.cpp:78
grk_buf * push_back(uint8_t *buf, size_t len, bool ownsData)
Definition: ChunkBuffer.cpp:113
size_t get_global_offset(void)
Definition: ChunkBuffer.cpp:216
void incr_cur_chunk_offset(size_t offset)
Definition: ChunkBuffer.cpp:156
bool copy_to_contiguous_buffer(uint8_t *buffer)
Definition: ChunkBuffer.cpp:182
size_t get_cur_chunk_len(void)
Definition: ChunkBuffer.cpp:204
size_t read(void *p_buffer, size_t nb_bytes)
Definition: ChunkBuffer.cpp:42