Compare commits
8 Commits
fa20ad9d29
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
109a9be193 | ||
|
|
858b291f46 | ||
|
|
2102147a2f | ||
|
|
944483d13f | ||
|
|
4d9486e281 | ||
|
|
49766e29f4 | ||
|
|
63b3584cfa | ||
|
|
1577c34062 |
151
.clang-format
Normal file
151
.clang-format
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
# BasedOnStyle: Google
|
||||||
|
AccessModifierOffset: -1
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignConsecutiveDeclarations: false
|
||||||
|
AlignEscapedNewlines: Left
|
||||||
|
AlignOperands: true
|
||||||
|
AlignTrailingComments: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: false
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: true
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: true
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BraceWrapping:
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: false
|
||||||
|
AfterEnum: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInheritanceComma: false
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializersBeforeComma: false
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 80
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: true
|
||||||
|
DisableFormat: false
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^<ext/.*\.h>'
|
||||||
|
Priority: 2
|
||||||
|
- Regex: '^<.*\.h>'
|
||||||
|
Priority: 1
|
||||||
|
- Regex: '^<.*'
|
||||||
|
Priority: 2
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 3
|
||||||
|
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentWidth: 2
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Never
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 1
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 200
|
||||||
|
PointerAlignment: Left
|
||||||
|
RawStringFormats:
|
||||||
|
- Language: Cpp
|
||||||
|
Delimiters:
|
||||||
|
- cc
|
||||||
|
- CC
|
||||||
|
- cpp
|
||||||
|
- Cpp
|
||||||
|
- CPP
|
||||||
|
- 'c++'
|
||||||
|
- 'C++'
|
||||||
|
CanonicalDelimiter: ''
|
||||||
|
BasedOnStyle: google
|
||||||
|
- Language: TextProto
|
||||||
|
Delimiters:
|
||||||
|
- pb
|
||||||
|
- PB
|
||||||
|
- proto
|
||||||
|
- PROTO
|
||||||
|
EnclosingFunctions:
|
||||||
|
- EqualsProto
|
||||||
|
- EquivToProto
|
||||||
|
- PARSE_PARTIAL_TEXT_PROTO
|
||||||
|
- PARSE_TEST_PROTO
|
||||||
|
- PARSE_TEXT_PROTO
|
||||||
|
- ParseTextOrDie
|
||||||
|
- ParseTextProtoOrDie
|
||||||
|
CanonicalDelimiter: ''
|
||||||
|
BasedOnStyle: google
|
||||||
|
ReflowComments: true
|
||||||
|
SortIncludes: true
|
||||||
|
SortUsingDeclarations: true
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 2
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Auto
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: Never
|
||||||
|
...
|
||||||
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*.a
|
*.a
|
||||||
*.o
|
*.o
|
||||||
|
*.so
|
||||||
|
|||||||
24
Makefile
24
Makefile
@@ -1,19 +1,35 @@
|
|||||||
FIRE_CXX ?= clang++
|
FIRE_CXX ?= clang++
|
||||||
FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror
|
FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror -Wextra -fPIE -fPIC -fstack-protector-strong -fsanitize=safe-stack -fsanitize=safe-stack
|
||||||
|
FIRE_LDFLAGS ?= -fuse-ld=gold -flto -Wl,-z,relro -Wl,-z,now
|
||||||
|
FIRE_LDLIBS ?= -lglog
|
||||||
|
|
||||||
all: firebuf.a
|
all: firebuf.a firebuf.o firebuf.so
|
||||||
|
|
||||||
objects = stream_buffer.o buffer.o
|
objects = stream_buffer.o buffer.o
|
||||||
|
|
||||||
firebuf.a: $(objects)
|
firebuf.a: $(objects)
|
||||||
ar rcs $@ $^
|
ar rcs $@ $^
|
||||||
|
|
||||||
|
firebuf.o: $(objects)
|
||||||
|
gold -z relro -z now -r --output=$@ $+
|
||||||
|
|
||||||
|
firebuf.so: $(objects)
|
||||||
|
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDLIBS)
|
||||||
|
|
||||||
%.o: %.cc *.h Makefile
|
%.o: %.cc *.h Makefile
|
||||||
$(FIRE_CXX) $(FIRE_CXXFLAGS) -c -o $@ $<
|
$(FIRE_CXX) $(FIRE_CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm --force *.o *.a
|
rm --force *.so *.o *.a
|
||||||
|
|
||||||
asan:
|
asan:
|
||||||
$(MAKE) clean
|
$(MAKE) clean
|
||||||
FIRE_CXXFLAGS="-O1 -g -fsanitize=address -fno-omit-frame-pointer -std=gnu++2a -Wall -Werror" $(MAKE) all
|
FIRE_CXXFLAGS="-O1 -g -fsanitize=address -fno-omit-frame-pointer -std=gnu++2a -fPIE -fPIC" $(MAKE) all
|
||||||
|
|
||||||
|
tsan:
|
||||||
|
$(MAKE) clean
|
||||||
|
FIRE_CXXFLAGS="-O1 -g -fsanitize=thread -std=gnu++2a -fPIE -fPIC" $(MAKE) all
|
||||||
|
|
||||||
|
ubsan:
|
||||||
|
$(MAKE) clean
|
||||||
|
FIRE_CXXFLAGS="-O1 -g -fsanitize=undefined -std=gnu++2a -fPIE -fPIC" $(MAKE) all
|
||||||
|
|||||||
37
buffer.cc
37
buffer.cc
@@ -3,12 +3,9 @@
|
|||||||
namespace firebuf {
|
namespace firebuf {
|
||||||
|
|
||||||
ConstBuffer::ConstBuffer(const char *buf, size_t len)
|
ConstBuffer::ConstBuffer(const char *buf, size_t len)
|
||||||
: const_buf_(buf),
|
: const_buf_(buf), len_(len) {}
|
||||||
len_(len) {}
|
|
||||||
|
|
||||||
size_t ConstBuffer::ReadMaxLen() const {
|
size_t ConstBuffer::ReadMaxLen() const { return len_ - start_; }
|
||||||
return len_ - start_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *ConstBuffer::Read(size_t len) {
|
const char *ConstBuffer::Read(size_t len) {
|
||||||
if (ReadMaxLen() < len) {
|
if (ReadMaxLen() < len) {
|
||||||
@@ -27,40 +24,26 @@ bool ConstBuffer::Discard(size_t len) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConstBuffer::ResetRead() {
|
void ConstBuffer::ResetRead() { start_ = commit_; }
|
||||||
start_ = commit_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConstBuffer::Commit() {
|
void ConstBuffer::Commit() { commit_ = start_; }
|
||||||
commit_ = start_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConstBuffer::Reset() {
|
|
||||||
commit_ = start_ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
void ConstBuffer::Reset() { commit_ = start_ = 0; }
|
||||||
|
|
||||||
Buffer::Buffer(char *buf, size_t size, size_t len)
|
Buffer::Buffer(char *buf, size_t size, size_t len)
|
||||||
: ConstBuffer(buf, size),
|
: ConstBuffer(buf, size), buf_(buf), size_(size) {
|
||||||
buf_(buf),
|
|
||||||
size_(size) {
|
|
||||||
len_ = len;
|
len_ = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer::Buffer(size_t size)
|
Buffer::Buffer(size_t size) : Buffer(new char[size], size, 0) {
|
||||||
: Buffer(new char[size], size, 0) {
|
|
||||||
own_buf_.reset(buf_);
|
own_buf_.reset(buf_);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *Buffer::WritePtr() {
|
char *Buffer::WritePtr() { return &buf_[len_]; }
|
||||||
return &buf_[len_];
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Buffer::WriteMaxLen() const {
|
size_t Buffer::WriteMaxLen() const { return size_ - len_; }
|
||||||
return size_ - len_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Buffer::Write(const std::string_view& str) {
|
bool Buffer::Write(const std::string_view &str) {
|
||||||
if (WriteMaxLen() < str.size()) {
|
if (WriteMaxLen() < str.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
10
buffer.h
10
buffer.h
@@ -12,7 +12,8 @@ class ConstBuffer {
|
|||||||
|
|
||||||
[[nodiscard]] size_t ReadMaxLen() const;
|
[[nodiscard]] size_t ReadMaxLen() const;
|
||||||
[[nodiscard]] virtual const char *Read(size_t len);
|
[[nodiscard]] virtual const char *Read(size_t len);
|
||||||
template<class T> [[nodiscard]] const T *ReadObj();
|
template <class T>
|
||||||
|
[[nodiscard]] const T *ReadObj();
|
||||||
|
|
||||||
bool Discard(size_t len); // like Read() but don't use the result
|
bool Discard(size_t len); // like Read() but don't use the result
|
||||||
void ResetRead(); // next read from last commit
|
void ResetRead(); // next read from last commit
|
||||||
@@ -34,7 +35,7 @@ class Buffer : public ConstBuffer {
|
|||||||
|
|
||||||
[[nodiscard]] char *WritePtr();
|
[[nodiscard]] char *WritePtr();
|
||||||
[[nodiscard]] size_t WriteMaxLen() const;
|
[[nodiscard]] size_t WriteMaxLen() const;
|
||||||
bool Write(const std::string_view& str);
|
bool Write(const std::string_view &str);
|
||||||
void Wrote(ssize_t len);
|
void Wrote(ssize_t len);
|
||||||
|
|
||||||
void Consume(); // discard up to last commit
|
void Consume(); // discard up to last commit
|
||||||
@@ -47,11 +48,12 @@ class Buffer : public ConstBuffer {
|
|||||||
const size_t size_;
|
const size_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T> const T *ConstBuffer::ReadObj() {
|
template <class T>
|
||||||
|
const T *ConstBuffer::ReadObj() {
|
||||||
if (ReadMaxLen() < sizeof(T)) {
|
if (ReadMaxLen() < sizeof(T)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return reinterpret_cast<const T*>(Read(sizeof(T)));
|
return reinterpret_cast<const T *>(Read(sizeof(T)));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace firebuf
|
} // namespace firebuf
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
namespace firebuf {
|
namespace firebuf {
|
||||||
|
|
||||||
StreamBuffer::StreamBuffer(int sock, size_t size)
|
StreamBuffer::StreamBuffer(int sock, size_t size) : Buffer(size), sock_(sock) {}
|
||||||
: Buffer(size),
|
|
||||||
sock_(sock) {}
|
|
||||||
|
|
||||||
bool StreamBuffer::Refill() {
|
bool StreamBuffer::Refill() {
|
||||||
|
if (WriteMaxLen() == 0) {
|
||||||
|
LOG(ERROR) << "buffer full";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
auto read_len = read(sock_, WritePtr(), WriteMaxLen());
|
auto read_len = read(sock_, WritePtr(), WriteMaxLen());
|
||||||
if (read_len == -1) {
|
if (read_len == -1) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
|
|||||||
Reference in New Issue
Block a user