Skip to content

Commit 273520c

Browse files
committed
refactor: first commit
1 parent 6938166 commit 273520c

File tree

12 files changed

+7587
-1
lines changed

12 files changed

+7587
-1
lines changed

.clang-format

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -1
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: false
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: true
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Inline
15+
AllowShortIfStatementsOnASingleLine: true
16+
AllowShortLoopsOnASingleLine: true
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: None
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: true
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: false
28+
AfterNamespace: false
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
AfterExternBlock: false
33+
BeforeCatch: false
34+
BeforeElse: false
35+
IndentBraces: false
36+
SplitEmptyFunction: true
37+
SplitEmptyRecord: true
38+
SplitEmptyNamespace: true
39+
BreakBeforeBinaryOperators: None
40+
BreakBeforeBraces: Attach
41+
BreakBeforeInheritanceComma: false
42+
BreakBeforeTernaryOperators: true
43+
BreakConstructorInitializersBeforeComma: false
44+
BreakConstructorInitializers: BeforeColon
45+
BreakAfterJavaFieldAnnotations: false
46+
BreakStringLiterals: true
47+
ColumnLimit: 80
48+
CommentPragmas: '^ IWYU pragma:'
49+
CompactNamespaces: false
50+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
51+
ConstructorInitializerIndentWidth: 4
52+
ContinuationIndentWidth: 4
53+
Cpp11BracedListStyle: true
54+
DerivePointerAlignment: false
55+
DisableFormat: false
56+
ExperimentalAutoDetectBinPacking: false
57+
FixNamespaceComments: true
58+
ForEachMacros:
59+
- foreach
60+
- Q_FOREACH
61+
- BOOST_FOREACH
62+
IncludeBlocks: Preserve
63+
IncludeCategories:
64+
- Regex: '^<ext/.*\.h>'
65+
Priority: 2
66+
- Regex: '^<.*\.h>'
67+
Priority: 1
68+
- Regex: '^<.*'
69+
Priority: 2
70+
- Regex: '.*'
71+
Priority: 3
72+
IncludeIsMainRegex: '([-_](test|unittest))?$'
73+
IndentCaseLabels: true
74+
IndentPPDirectives: None
75+
IndentWidth: 2
76+
IndentWrappedFunctionNames: false
77+
JavaScriptQuotes: Leave
78+
JavaScriptWrapImports: true
79+
KeepEmptyLinesAtTheStartOfBlocks: false
80+
MacroBlockBegin: ''
81+
MacroBlockEnd: ''
82+
MaxEmptyLinesToKeep: 1
83+
NamespaceIndentation: None
84+
ObjCBlockIndentWidth: 2
85+
ObjCSpaceAfterProperty: false
86+
ObjCSpaceBeforeProtocolList: false
87+
PenaltyBreakAssignment: 2
88+
PenaltyBreakBeforeFirstCallParameter: 1
89+
PenaltyBreakComment: 300
90+
PenaltyBreakFirstLessLess: 120
91+
PenaltyBreakString: 1000
92+
PenaltyExcessCharacter: 1000000
93+
PenaltyReturnTypeOnItsOwnLine: 200
94+
PointerAlignment: Left
95+
ReflowComments: true
96+
SortIncludes: true
97+
SortUsingDeclarations: true
98+
SpaceAfterCStyleCast: false
99+
SpaceAfterTemplateKeyword: true
100+
SpaceBeforeAssignmentOperators: true
101+
SpaceBeforeParens: ControlStatements
102+
SpaceInEmptyParentheses: false
103+
SpacesBeforeTrailingComments: 2
104+
SpacesInAngles: false
105+
SpacesInContainerLiterals: true
106+
SpacesInCStyleCastParentheses: false
107+
SpacesInParentheses: false
108+
SpacesInSquareBrackets: false
109+
Standard: Auto
110+
TabWidth: 8
111+
UseTab: Never

.cpplint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CPPLINT.cfg

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 2
10+
11+
[Makefile]
12+
indent_style = tab

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
.node-modules-installed
35+
.cpplinted
36+
.clang-formatted
37+
coverage
38+
coverage.info
39+
build
40+
node_modules
41+

CPPLINT.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set noparent
2+
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint,-readability/casting
3+
linelength=80

Makefile

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
V ?= 0
2+
CC = g++
3+
TEST_COV ?= 0
4+
5+
CFLAGS = -Wall -fPIC
6+
FLAGS_OPTIMIZATION ?= -O3
7+
FLAGS_DEBUG ?= -g -ggdb -O0
8+
9+
ifeq ($(TEST_COV),1)
10+
CFLAGS += -fprofile-arcs -ftest-coverage
11+
endif
12+
13+
node := $(shell which node)
14+
npm := $(shell which npm)
15+
clang-format-bin := ./tools/node_modules/.bin/clang-format
16+
cpplint-bin := ./tools/node_modules/.bin/cpplint
17+
18+
BUILDTYPE ?= Debug
19+
RESIZABLE_BUFFER_HEADER_FILE := include/resizable_buffer.h
20+
TEST_FILES := test/test.cc
21+
22+
TEST_EXE := build/test
23+
RELEASE_FLAGS := $(FLAGS_OPTIMIZATION) $(CFLAGS)
24+
TEST_G_EXE := build/test_g
25+
DEBUG_FLAGS := $(FLAGS_DEBUG) $(CFLAGS)
26+
27+
.PHONY: all test
28+
ifeq ($(BUILDTYPE),Release)
29+
all: $(TEST_EXE)
30+
test: all
31+
$(TEST_EXE) -d
32+
else
33+
all: $(TEST_G_EXE)
34+
test: all
35+
$(TEST_G_EXE) -d
36+
endif
37+
38+
.PHONY: dev-prepare clang-format cpplint
39+
dev-prepare: .node-modules-installed
40+
clang-format: .clang-formatted
41+
cpplint: .cpplinted
42+
43+
.node-modules-installed: tools/package.json
44+
if [ ! -a $(npm) ]; then \
45+
echo "npm not found"; \
46+
exit 1; \
47+
else \
48+
cd tools && rm -rf node_modules && $(npm) install && cd ..; \
49+
fi
50+
touch .node-modules-installed
51+
52+
$(clang-format-bin): .node-modules-installed
53+
$(cpplint-bin): .node-modules-installed
54+
55+
.clang-formatted: $(clang-format-bin) $(RESIZABLE_BUFFER_HEADER_FILE) $(TEST_FILES)
56+
$(clang-format-bin) -i --style=file $(RESIZABLE_BUFFER_HEADER_FILE) $(TEST_FILES)
57+
touch .clang-formatted
58+
59+
.cpplinted: $(cpplint-bin) $(RESIZABLE_BUFFER_HEADER_FILE) $(TEST_FILES)
60+
$(cpplint-bin) $(RESIZABLE_BUFFER_HEADER_FILE) $(TEST_FILES)
61+
touch .cpplinted
62+
63+
$(TEST_EXE).o: test/* include/*.h
64+
mkdir -p build
65+
$(CC) $(RELEASE_FLAGS) -c test/test.cc -o $@
66+
67+
$(TEST_G_EXE).o: test/* include/*.h
68+
mkdir -p build
69+
$(CC) $(DEBUG_FLAGS) -c test/test.cc -o $@
70+
71+
$(TEST_EXE): $(TEST_EXE).o
72+
$(CC) $(RELEASE_FLAGS) $(TEST_EXE).o -o $(TEST_EXE)
73+
if [ ! -a $(TEST_EXE) ]; then \
74+
echo "test executable not found"; \
75+
exit 1; \
76+
fi
77+
78+
$(TEST_G_EXE): $(TEST_G_EXE).o
79+
$(CC) $(DEBUG_FLAGS) $(TEST_G_EXE).o -o $(TEST_G_EXE)
80+
if [ ! -a $(TEST_G_EXE) ]; then \
81+
echo "test executable not found"; \
82+
exit 1; \
83+
fi
84+
85+
.PHONY: coverage
86+
coverage:
87+
$(make) test TEST_COV=1
88+
lcov --capture \
89+
--directory $(CURDIR) \
90+
--base-directory $(CURDIR) \
91+
--include $(CURDIR)/include/resizable_buffer.h \
92+
--output-file $(CURDIR)/coverage.info
93+
genhtml $(CURDIR)/coverage.info --output-directory $(CURDIR)/coverage
94+
rm -f $(CURDIR)/coverage.info

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
# resizable-buffer
2-
The resizable buffer for C++.
2+
3+
The resizable buffer for C++ with a single header file.
4+
5+
## Requirement
6+
7+
No addtional requirement if you're just using it. But if you're developing
8+
resizable-buffer, you may need:
9+
10+
+ [lcov](https://linux.die.net/man/1/lcov): For testing coverage;
11+
+ [Node.js](https://nodejs.org/en/):For some useful scripts like cpplint, clang-format, etc;
12+
13+
## Run Test Cases
14+
15+
```shell
16+
$ make test
17+
```

include/resizable_buffer.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#ifndef INCLUDE_RESIZABLE_BUFFER_H_
2+
#define INCLUDE_RESIZABLE_BUFFER_H_
3+
4+
#include <assert.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <unistd.h>
9+
10+
// Refer: https://github.com/XadillaX/node-sfml/blob/main/src/resizable_buffer.h
11+
namespace rb {
12+
13+
template <typename T>
14+
struct ReleasedResizableBuffer {
15+
T* buffer;
16+
size_t length;
17+
size_t byte_length;
18+
19+
inline void Free() {
20+
if (buffer) {
21+
free(buffer);
22+
buffer = nullptr;
23+
}
24+
}
25+
};
26+
27+
template <typename T>
28+
class ResizableBuffer {
29+
public:
30+
inline ResizableBuffer()
31+
: _buffer(nullptr),
32+
_length(0),
33+
_real_length(0),
34+
_byte_length(0),
35+
_page_size(getpagesize()) {}
36+
37+
explicit ResizableBuffer(size_t initial_length) : ResizableBuffer() {
38+
Realloc(initial_length);
39+
}
40+
41+
inline ~ResizableBuffer() {
42+
if (_buffer) {
43+
free(_buffer);
44+
_buffer = nullptr;
45+
}
46+
}
47+
48+
inline void Realloc(size_t new_length) {
49+
if (new_length == _real_length) {
50+
return;
51+
} else if (new_length < _real_length) {
52+
_length = new_length;
53+
_byte_length = _length * sizeof(T);
54+
return;
55+
}
56+
57+
if (_buffer != nullptr) {
58+
free(_buffer);
59+
_buffer = nullptr;
60+
}
61+
62+
_length = new_length;
63+
_real_length = _length;
64+
_byte_length = _length * sizeof(T);
65+
66+
if (_byte_length >= _page_size) {
67+
assert(posix_memalign(reinterpret_cast<void**>(&_buffer),
68+
_page_size,
69+
_byte_length) == 0);
70+
} else {
71+
_buffer = reinterpret_cast<T*>(malloc(_byte_length));
72+
}
73+
74+
memset(byte_buffer(), 0, _byte_length);
75+
}
76+
77+
inline ReleasedResizableBuffer<T> Release() {
78+
ReleasedResizableBuffer<T> ret;
79+
ret.buffer = _length ? _buffer : nullptr;
80+
ret.length = _length;
81+
ret.byte_length = _byte_length;
82+
83+
_buffer = _length ? nullptr : _buffer;
84+
_real_length = _length ? 0 : _real_length;
85+
86+
_length = 0;
87+
_byte_length = 0;
88+
89+
return ret;
90+
}
91+
92+
inline T* operator*() { return _buffer; }
93+
inline const T* operator*() const { return _buffer; }
94+
95+
inline T* buffer() { return _buffer; }
96+
inline const T* buffer() const { return _buffer; }
97+
98+
inline size_t length() const { return _length; }
99+
inline size_t byte_length() const { return _byte_length; }
100+
101+
inline char* byte_buffer() { return reinterpret_cast<char*>(_buffer); }
102+
inline const char* byte_buffer() const {
103+
return reinterpret_cast<const char*>(_buffer);
104+
}
105+
106+
inline unsigned char* unsigned_byte_buffer() {
107+
return reinterpret_cast<unsigned char*>(_buffer);
108+
}
109+
inline const unsigned char* unsigned_byte_buffer() const {
110+
return reinterpret_cast<const unsigned char*>(_buffer);
111+
}
112+
113+
private:
114+
T* _buffer;
115+
size_t _length;
116+
size_t _real_length;
117+
size_t _byte_length;
118+
size_t _page_size;
119+
};
120+
121+
} // namespace rb
122+
123+
#endif // INCLUDE_RESIZABLE_BUFFER_H_

0 commit comments

Comments
 (0)