Skip to content

Commit 1fb143b

Browse files
masaori335Walt Karas
andcommitted
Introduce LocalBuffer
Co-authored-by: Walt Karas <wkaras@verizonmedia.com>
1 parent e872a70 commit 1fb143b

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

include/tscpp/util/LocalBuffer.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/** @file
2+
3+
LocalBuffer
4+
5+
@section license License
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
*/
23+
24+
#pragma once
25+
26+
namespace ts
27+
{
28+
template <std::size_t EstSizeBound = 1024> class LocalBuffer
29+
{
30+
public:
31+
LocalBuffer(std::size_t size) : _ptr(size == 0 ? nullptr : (size > EstSizeBound ? new uint8_t[size] : _buf)) {}
32+
~LocalBuffer()
33+
{
34+
if (_ptr && _ptr != _buf) {
35+
delete[] _ptr;
36+
}
37+
}
38+
39+
uint8_t *
40+
get() const
41+
{
42+
return _ptr;
43+
}
44+
45+
private:
46+
uint8_t _buf[EstSizeBound];
47+
uint8_t *const _ptr;
48+
};
49+
} // namespace ts

src/tscpp/util/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test_tscpputil_CXXFLAGS = -Wno-array-bounds $(AM_CXXFLAGS)
3838
test_tscpputil_LDADD = libtscpputil.la
3939
test_tscpputil_SOURCES = \
4040
unit_tests/unit_test_main.cc \
41+
unit_tests/test_LocalBuffer.cc \
4142
unit_tests/test_MemSpan.cc \
4243
unit_tests/test_PostScript.cc \
4344
unit_tests/test_TextView.cc \
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/** @file
2+
3+
Unit tests for LocalBuffer
4+
5+
@section license License
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
*/
23+
24+
#include "catch.hpp"
25+
#include "tscpp/util/LocalBuffer.h"
26+
27+
#include <cstring>
28+
29+
TEST_CASE("LocalBuffer", "[libts][LocalBuffer]")
30+
{
31+
SECTION("EstSizeBound = default")
32+
{
33+
SECTION("size = 0")
34+
{
35+
const size_t len = 0;
36+
LocalBuffer local_buffer(len);
37+
uint8_t *buf = local_buffer.get();
38+
39+
CHECK(buf == nullptr);
40+
}
41+
42+
SECTION("size = 1024")
43+
{
44+
const size_t len = 1024;
45+
LocalBuffer local_buffer(len);
46+
uint8_t *buf = local_buffer.get();
47+
48+
memset(buf, 0xAA, len);
49+
50+
CHECK(buf[0] == 0xAA);
51+
CHECK(buf[len - 1] == 0xAA);
52+
}
53+
54+
SECTION("size = 2048")
55+
{
56+
const size_t len = 2048;
57+
LocalBuffer local_buffer(len);
58+
uint8_t *buf = local_buffer.get();
59+
60+
memset(buf, 0xAA, len);
61+
62+
CHECK(buf[0] == 0xAA);
63+
CHECK(buf[len - 1] == 0xAA);
64+
}
65+
}
66+
67+
SECTION("EstSizeBound = 2048")
68+
{
69+
SECTION("size = 1024")
70+
{
71+
const size_t len = 1024;
72+
LocalBuffer<2048> local_buffer(len);
73+
uint8_t *buf = local_buffer.get();
74+
75+
memset(buf, 0xAA, len);
76+
77+
CHECK(buf[0] == 0xAA);
78+
CHECK(buf[len - 1] == 0xAA);
79+
}
80+
81+
SECTION("size = 2048")
82+
{
83+
const size_t len = 2048;
84+
LocalBuffer<2048> local_buffer(len);
85+
uint8_t *buf = local_buffer.get();
86+
87+
memset(buf, 0xAA, len);
88+
89+
CHECK(buf[0] == 0xAA);
90+
CHECK(buf[len - 1] == 0xAA);
91+
}
92+
93+
SECTION("size = 4096")
94+
{
95+
const size_t len = 4096;
96+
LocalBuffer<2048> local_buffer(len);
97+
uint8_t *buf = local_buffer.get();
98+
99+
memset(buf, 0xAA, len);
100+
101+
CHECK(buf[0] == 0xAA);
102+
CHECK(buf[len - 1] == 0xAA);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)