Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nghttp2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
nghttp2
Commits
6774fa6e
Commit
6774fa6e
authored
Feb 05, 2015
by
Tatsuhiro Tsujikawa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
buffer: Refactor
parent
7baf6f78
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
13 deletions
+13
-13
src/buffer.h
src/buffer.h
+8
-8
src/buffer_test.cc
src/buffer_test.cc
+5
-5
No files found.
src/buffer.h
View file @
6774fa6e
...
...
@@ -29,21 +29,21 @@
#include <cstring>
#include <algorithm>
#include <array>
namespace
nghttp2
{
template
<
size_t
N
>
struct
Buffer
{
Buffer
()
:
pos
(
begin
),
last
(
begin
)
{}
Buffer
()
:
pos
(
std
::
begin
(
buf
)),
last
(
pos
)
{}
// Returns the number of bytes to read.
size_t
rleft
()
const
{
return
last
-
pos
;
}
// Returns the number of bytes this buffer can store.
size_t
wleft
()
const
{
return
begin
+
N
-
last
;
}
size_t
wleft
()
const
{
return
std
::
end
(
buf
)
-
last
;
}
// Writes up to min(wleft(), |count|) bytes from buffer pointed by
// |
buf
|. Returns number of bytes written.
size_t
write
(
const
void
*
buf
,
size_t
count
)
{
// |
src
|. Returns number of bytes written.
size_t
write
(
const
void
*
src
,
size_t
count
)
{
count
=
std
::
min
(
count
,
wleft
());
memcpy
(
last
,
buf
,
count
);
last
+=
count
;
last
=
std
::
copy_n
(
static_cast
<
const
uint8_t
*>
(
src
),
count
,
last
);
return
count
;
}
size_t
write
(
size_t
count
)
{
...
...
@@ -57,8 +57,8 @@ template <size_t N> struct Buffer {
pos
+=
count
;
return
count
;
}
void
reset
()
{
pos
=
last
=
begin
;
}
uint8_t
begin
[
N
]
;
void
reset
()
{
pos
=
last
=
std
::
begin
(
buf
)
;
}
std
::
array
<
uint8_t
,
N
>
buf
;
uint8_t
*
pos
,
*
last
;
};
...
...
src/buffer_test.cc
View file @
6774fa6e
...
...
@@ -45,13 +45,13 @@ void test_buffer_write(void) {
CU_ASSERT
(
3
==
b
.
rleft
());
CU_ASSERT
(
13
==
b
.
wleft
());
CU_ASSERT
(
0
==
b
.
pos
-
b
.
begin
);
CU_ASSERT
(
0
==
b
.
pos
-
std
::
begin
(
b
.
buf
)
);
b
.
drain
(
3
);
CU_ASSERT
(
0
==
b
.
rleft
());
CU_ASSERT
(
13
==
b
.
wleft
());
CU_ASSERT
(
3
==
b
.
pos
-
b
.
begin
);
CU_ASSERT
(
3
==
b
.
pos
-
std
::
begin
(
b
.
buf
)
);
auto
n
=
b
.
write
(
"0123456789ABCDEF"
,
16
);
...
...
@@ -59,20 +59,20 @@ void test_buffer_write(void) {
CU_ASSERT
(
13
==
b
.
rleft
());
CU_ASSERT
(
0
==
b
.
wleft
());
CU_ASSERT
(
3
==
b
.
pos
-
b
.
begin
);
CU_ASSERT
(
3
==
b
.
pos
-
std
::
begin
(
b
.
buf
)
);
CU_ASSERT
(
0
==
memcmp
(
b
.
pos
,
"0123456789ABC"
,
13
));
b
.
reset
();
CU_ASSERT
(
0
==
b
.
rleft
());
CU_ASSERT
(
0
==
b
.
wleft
());
CU_ASSERT
(
0
==
b
.
pos
-
b
.
begin
);
CU_ASSERT
(
0
==
b
.
pos
-
std
::
begin
(
b
.
buf
)
);
b
.
write
(
5
);
CU_ASSERT
(
5
==
b
.
rleft
());
CU_ASSERT
(
11
==
b
.
wleft
());
CU_ASSERT
(
0
==
b
.
pos
-
b
.
begin
);
CU_ASSERT
(
0
==
b
.
pos
-
std
::
begin
(
b
.
buf
)
);
}
}
// namespace nghttp2
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment