-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbuffer.jl
More file actions
239 lines (208 loc) · 6.48 KB
/
buffer.jl
File metadata and controls
239 lines (208 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Buffer
# ======
# Data Layout
# -----------
#
# Buffered data are stored in `data` and three position fields are used to keep
# track of marked data, buffered data and margin.
#
# marked buffer margin
# |<-------->||<-------->||<-------->|
# data ....xxxxxxxxxxxxXXXXXXXXXXXX............
# ^ ^ ^ ^ ^
# position 1 markpos bufferpos marginpos lastindex(data)
#
# `markpos` is positive iff there are marked data; otherwise it is set to zero.
# `markpos` ≤ `bufferpos` ≤ `marginpos` must hold whenever possible.
mutable struct Buffer
# data and positions (see above)
data::Vector{UInt8}
markpos::Int
bufferpos::Int
marginpos::Int
# the total number of transcoded bytes
transcoded::Int64
function Buffer(data::Vector{UInt8}, marginpos::Integer=length(data)+1)
@assert 1 <= marginpos <= length(data)+1
return new(data, 0, 1, marginpos, 0)
end
end
function Buffer(size::Integer = 0)
return Buffer(Vector{UInt8}(undef, size), 1)
end
function Buffer(data::Base.CodeUnits{UInt8}, args...)
return Buffer(Vector{UInt8}(data), args...)
end
function Base.length(buf::Buffer)
return length(buf.data)
end
function bufferptr(buf::Buffer)
return pointer(buf.data, buf.bufferpos)
end
function buffersize(buf::Buffer)
return buf.marginpos - buf.bufferpos
end
function buffermem(buf::Buffer)
return Memory(bufferptr(buf), buffersize(buf))
end
function marginptr(buf::Buffer)
return pointer(buf.data, buf.marginpos)
end
function marginsize(buf::Buffer)
return lastindex(buf.data) - buf.marginpos + 1
end
function marginmem(buf::Buffer)
return Memory(marginptr(buf), marginsize(buf))
end
function ismarked(buf::Buffer)
return buf.markpos != 0
end
function mark!(buf::Buffer)
return buf.markpos = buf.bufferpos
end
function unmark!(buf::Buffer)
if buf.markpos == 0
return false
else
buf.markpos = 0
return true
end
end
function reset!(buf::Buffer)
@assert buf.markpos > 0
buf.bufferpos = buf.markpos
buf.markpos = 0
return buf.bufferpos
end
# Notify that `n` bytes are consumed from `buf`.
function consumed!(buf::Buffer, n::Integer; transcode::Bool = false)
buf.bufferpos += n
if transcode
buf.transcoded += n
end
return buf
end
# Notify that `n` bytes are supplied to `buf`.
function supplied!(buf::Buffer, n::Integer; transcode::Bool = false)
buf.marginpos += n
if transcode
buf.transcoded += n
end
return buf
end
# Discard buffered data and initialize positions.
function initbuffer!(buf::Buffer)
buf.markpos = buf.transcoded = 0
buf.bufferpos = buf.marginpos = 1
return buf
end
# Remove all buffered data.
function emptybuffer!(buf::Buffer)
buf.marginpos = buf.bufferpos
return buf
end
# Make margin with ≥`minsize` and return the size of it.
# If eager is true, it tries to move data even when the buffer has enough margin.
function makemargin!(buf::Buffer, minsize::Integer; eager::Bool = false)
@assert minsize ≥ 0
if buffersize(buf) == 0 && buf.markpos == 0
buf.bufferpos = buf.marginpos = 1
end
if marginsize(buf) < minsize || eager
# datapos refer to the leftmost position of data that must not be
# discarded. We can left-shift to discard all data before this
if buf.markpos == 0
# If data is not marked we must not discard buffered (nonconsumed) data
datapos = buf.bufferpos
datasize = buffersize(buf)
else
# Else, we must not consume marked data
# (Since markpos ≤ bufferpos, we do not consume buffered data either)
datapos = buf.markpos
datasize = buf.marginpos - buf.markpos
end
# Shift data left in buffer to make space for new data
copyto!(buf.data, 1, buf.data, datapos, datasize)
shift = datapos - 1
if buf.markpos > 0
buf.markpos -= shift
end
buf.bufferpos -= shift
buf.marginpos -= shift
end
# If there is still not enough margin, we expand buffer.
# At least enough for minsize, but otherwise 1.5 times
if marginsize(buf) < minsize
datasize = length(buf.data)
resize!(buf.data, max(buf.marginpos + minsize - 1, datasize + div(datasize, 2)))
end
@assert marginsize(buf) ≥ minsize
return marginsize(buf)
end
# Read a byte.
function readbyte!(buf::Buffer)
b = buf.data[buf.bufferpos]
consumed!(buf, 1)
return b
end
# Write a byte.
function writebyte!(buf::Buffer, b::UInt8)
buf.data[buf.marginpos] = b
supplied!(buf, 1)
return 1
end
# Skip `n` bytes in the buffer.
function skipbuffer!(buf::Buffer, n::Integer)
@assert n ≤ buffersize(buf)
consumed!(buf, n)
return buf
end
# Take the ownership of the marked data.
function takemarked!(buf::Buffer)
@assert buf.markpos > 0
sz = buf.marginpos - buf.markpos
copyto!(buf.data, 1, buf.data, buf.markpos, sz)
initbuffer!(buf)
return resize!(buf.data, sz)
end
# Copy data from `data` to `buf`.
function copydata!(buf::Buffer, data::Ptr{UInt8}, nbytes::Integer)
makemargin!(buf, nbytes)
GC.@preserve buf unsafe_copyto!(marginptr(buf), data, nbytes)
supplied!(buf, nbytes)
return buf
end
# Copy data from `data` to `buf`.
function copydata!(buf::Buffer, data::Buffer, nbytes::Integer = length(data))
return copydata!(buf, bufferptr(data), nbytes)
end
# Copy data from `buf` to `data`.
function copydata!(data::Ptr{UInt8}, buf::Buffer, nbytes::Integer)
# NOTE: It's caller's responsibility to ensure that the buffer has at least
# nbytes.
@assert buffersize(buf) ≥ nbytes
GC.@preserve buf unsafe_copyto!(data, bufferptr(buf), nbytes)
consumed!(buf, nbytes)
return data
end
# Insert data to the current buffer.
function insertdata!(buf::Buffer, data::Ptr{UInt8}, nbytes::Integer)
makemargin!(buf, nbytes)
copyto!(buf.data, buf.bufferpos + nbytes, buf.data, buf.bufferpos, buffersize(buf))
GC.@preserve buf unsafe_copyto!(bufferptr(buf), data, nbytes)
supplied!(buf, nbytes)
return buf
end
# Find the first occurrence of a specific byte.
function findbyte(buf::Buffer, byte::UInt8)
p = GC.@preserve buf ccall(
:memchr,
Ptr{UInt8},
(Ptr{UInt8}, Cint, Csize_t),
pointer(buf.data, buf.bufferpos), byte, buffersize(buf))
if p == C_NULL
return marginptr(buf)
else
return p
end
end