Debian Codegraph @

Modules/_zstd/buffer.h

raw
1 /* Low level interface to the Zstandard algorithm & the zstd library. */
2
3 #ifndef ZSTD_BUFFER_H
4 #define ZSTD_BUFFER_H
5
6 #include "pycore_blocks_output_buffer.h"
7
8 #include <zstd.h> // ZSTD_outBuffer
9
10 /* Blocks output buffer wrapper code */
11
12 /* Initialize the buffer, and grow the buffer.
13 Return 0 on success
14 Return -1 on failure */
15 static inline int
16 _OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
17 Py_ssize_t max_length)
18 {
19 /* Ensure .list was set to NULL */
20 assert(buffer->list == NULL);
21
22 Py_ssize_t res = _BlocksOutputBuffer_InitAndGrow(buffer, max_length,
23 &ob->dst);
24 if (res < 0) {
25 return -1;
26 }
27 ob->size = (size_t) res;
28 ob->pos = 0;
29 return 0;
30 }
31
32 /* Initialize the buffer, with an initial size.
33 init_size: the initial size.
34 Return 0 on success
35 Return -1 on failure */
36 static inline int
37 _OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
38 Py_ssize_t max_length, Py_ssize_t init_size)
39 {
40 Py_ssize_t block_size;
41
42 /* Ensure .list was set to NULL */
43 assert(buffer->list == NULL);
44
45 /* Get block size */
46 if (0 <= max_length && max_length < init_size) {
47 block_size = max_length;
48 }
49 else {
50 block_size = init_size;
51 }
52
53 Py_ssize_t res = _BlocksOutputBuffer_InitWithSize(buffer, block_size,
54 &ob->dst);
55 if (res < 0) {
56 return -1;
57 }
58 // Set max_length, InitWithSize doesn't do this
59 buffer->max_length = max_length;
60 ob->size = (size_t) res;
61 ob->pos = 0;
62 return 0;
63 }
64
65 /* Grow the buffer.
66 Return 0 on success
67 Return -1 on failure */
68 static inline int
69 _OutputBuffer_Grow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
70 {
71 assert(ob->pos == ob->size);
72 Py_ssize_t res = _BlocksOutputBuffer_Grow(buffer, &ob->dst, 0);
73 if (res < 0) {
74 return -1;
75 }
76 ob->size = (size_t) res;
77 ob->pos = 0;
78 return 0;
79 }
80
81 /* Finish the buffer.
82 Return a bytes object on success
83 Return NULL on failure */
84 static inline PyObject *
85 _OutputBuffer_Finish(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
86 {
87 return _BlocksOutputBuffer_Finish(buffer, ob->size - ob->pos);
88 }
89
90 /* Clean up the buffer */
91 static inline void
92 _OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
93 {
94 _BlocksOutputBuffer_OnError(buffer);
95 }
96
97 /* Whether the output data has reached max_length.
98 The avail_out must be 0, please check it before calling. */
99 static inline int
100 _OutputBuffer_ReachedMaxLength(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
101 {
102 /* Ensure (data size == allocated size) */
103 assert(ob->pos == ob->size);
104
105 return buffer->allocated == buffer->max_length;
106 }
107
108 #endif // !ZSTD_BUFFER_H