Debian Codegraph @

src/python/backports/zstd/_cffi/_blocks_output_buffer.py

raw
1 from backports.zstd._cffi._common import _ffi, _lib, _new_nonzero
2
3 unable_allocate_msg = "Unable to allocate output buffer."
4 OUTPUT_BUFFER_MAX_BLOCK_SIZE = 256 * 1024 * 1024
5 KB = 1024
6 MB = 1024 * 1024
7 BUFFER_BLOCK_SIZE = [
8 32 * KB,
9 64 * KB,
10 256 * KB,
11 1 * MB,
12 4 * MB,
13 8 * MB,
14 16 * MB,
15 16 * MB,
16 32 * MB,
17 32 * MB,
18 32 * MB,
19 32 * MB,
20 64 * MB,
21 64 * MB,
22 128 * MB,
23 128 * MB,
24 OUTPUT_BUFFER_MAX_BLOCK_SIZE,
25 ]
26
27
28 class BlocksOutputBuffer:
29 def init_and_grow(self, max_length):
30 # get block size
31 if 0 <= max_length < BUFFER_BLOCK_SIZE[0]:
32 block_size = max_length
33 else:
34 block_size = BUFFER_BLOCK_SIZE[0]
35
36 # the first block
37 b = _new_nonzero("char[]", block_size)
38 if b == _ffi.NULL:
39 raise MemoryError
40
41 # create the list
42 self.list = [b]
43
44 # set variables
45 self.allocated = block_size
46 self.max_length = max_length
47
48 return block_size, b
49
50 def init_with_size(self, init_size):
51 # the first block
52 b = _new_nonzero("char[]", init_size)
53 if b == _ffi.NULL:
54 raise MemoryError
55
56 # create the list
57 self.list = [b]
58
59 # set variables
60 self.allocated = init_size
61 self.max_length = -1
62
63 return init_size, b
64
65 def grow(self, avail_out):
66 list_len = len(self.list)
67
68 # ensure no gaps in the data
69 if avail_out != 0:
70 raise SystemError("avail_out is non-zero in _BlocksOutputBuffer_Grow().")
71
72 # get block size
73 if list_len < len(BUFFER_BLOCK_SIZE):
74 block_size = BUFFER_BLOCK_SIZE[list_len]
75 else:
76 block_size = BUFFER_BLOCK_SIZE[-1]
77
78 # check max_length
79 if self.max_length >= 0:
80 # if (rest == 0), should not grow the buffer.
81 rest = self.max_length - self.allocated
82 assert rest > 0
83
84 # block_size of the last block
85 if block_size > rest:
86 block_size = rest
87
88 # create the block
89 b = _new_nonzero("char[]", block_size)
90 if b == _ffi.NULL:
91 raise MemoryError(unable_allocate_msg)
92 self.list.append(b)
93
94 # set variables
95 self.allocated += block_size
96
97 return block_size, b
98
99 def finish(self, avail_out):
100 list_len = len(self.list)
101
102 # fast path for single block
103 if (list_len == 1 and avail_out == 0) or (
104 list_len == 2 and len(self.list[1]) == avail_out
105 ):
106 block = self.list[0]
107 self.list.clear()
108 return bytes(_ffi.buffer(block))
109
110 # final bytes object
111 result = _new_nonzero("char[]", self.allocated - avail_out)
112 if result == _ffi.NULL:
113 raise MemoryError(unable_allocate_msg)
114
115 # memory copy
116 if list_len > 0:
117 posi = 0
118
119 # blocks except the last one
120 for block in self.list[:-1]:
121 block_len = len(block)
122 _ffi.memmove(result + posi, block, block_len)
123 posi += block_len
124 # the last block
125 block = self.list[-1]
126 _ffi.memmove(result + posi, block, len(block) - avail_out)
127 else:
128 assert len(result) == 0
129
130 self.list.clear()
131 return bytes(_ffi.buffer(result))