Compare commits

...

3 Commits

Author SHA1 Message Date
Ian Gulliver
109a9be193 Fix typo 2019-05-18 23:45:02 -07:00
Ian Gulliver
858b291f46 Google style guide 2019-05-18 12:11:20 -07:00
flamingcow
2102147a2f Fix asan, add tsan and ubsan 2019-05-09 21:37:04 -07:00
6 changed files with 262 additions and 120 deletions

151
.clang-format Normal file
View 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
...

View File

@@ -14,7 +14,7 @@ firebuf.o: $(objects)
gold -z relro -z now -r --output=$@ $+ gold -z relro -z now -r --output=$@ $+
firebuf.so: $(objects) firebuf.so: $(objects)
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS) $(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 $@ $<
@@ -24,4 +24,12 @@ clean:
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

View File

@@ -3,90 +3,73 @@
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) {
return nullptr; return nullptr;
} }
const auto *ret = &const_buf_[start_]; const auto *ret = &const_buf_[start_];
start_ += len; start_ += len;
return ret; return ret;
} }
bool ConstBuffer::Discard(size_t len) { bool ConstBuffer::Discard(size_t len) {
if (len > ReadMaxLen()) { if (len > ReadMaxLen()) {
return false; return false;
} }
static_cast<void>(Read(len)); static_cast<void>(Read(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), len_ = len;
size_(size) {
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;
} }
memcpy(WritePtr(), str.data(), str.size()); memcpy(WritePtr(), str.data(), str.size());
Wrote(str.size()); Wrote(str.size());
return true; return true;
} }
void Buffer::Wrote(ssize_t len) { void Buffer::Wrote(ssize_t len) {
CHECK_LE(len, WriteMaxLen()); CHECK_LE(len, WriteMaxLen());
len_ += len; len_ += len;
} }
void Buffer::Consume() { void Buffer::Consume() {
if (commit_ == 0) { if (commit_ == 0) {
return; return;
} }
memmove(buf_, &buf_[commit_], len_ - commit_); memmove(buf_, &buf_[commit_], len_ - commit_);
len_ -= commit_; len_ -= commit_;
start_ -= commit_; start_ -= commit_;
commit_ = 0; commit_ = 0;
} }
void Buffer::Reset() { void Buffer::Reset() {
ConstBuffer::Reset(); ConstBuffer::Reset();
len_ = 0; len_ = 0;
} }
} // namespace firebuf } // namespace firebuf

View File

@@ -7,51 +7,53 @@
namespace firebuf { namespace firebuf {
class ConstBuffer { class ConstBuffer {
public: public:
ConstBuffer(const char *buf, size_t len); ConstBuffer(const char *buf, size_t len);
[[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
void Commit(); // commit read position void Commit(); // commit read position
virtual void Reset(); // full reset virtual void Reset(); // full reset
protected: protected:
const char *const_buf_; const char *const_buf_;
size_t len_; size_t len_;
size_t start_ = 0; size_t start_ = 0;
size_t commit_ = 0; size_t commit_ = 0;
}; };
class Buffer : public ConstBuffer { class Buffer : public ConstBuffer {
public: public:
Buffer(char *buf, size_t size, size_t len); Buffer(char *buf, size_t size, size_t len);
Buffer(size_t size); Buffer(size_t size);
[[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
void Reset() override; // full reset void Reset() override; // full reset
protected: protected:
std::unique_ptr<char[]> own_buf_; std::unique_ptr<char[]> own_buf_;
char *buf_; char *buf_;
const size_t size_; const size_t size_;
}; };
template<class T> const T *ConstBuffer::ReadObj() { template <class T>
if (ReadMaxLen() < sizeof(T)) { const T *ConstBuffer::ReadObj() {
return nullptr; if (ReadMaxLen() < sizeof(T)) {
} return nullptr;
return reinterpret_cast<const T*>(Read(sizeof(T))); }
return reinterpret_cast<const T *>(Read(sizeof(T)));
} }
} // namespace firebuf } // namespace firebuf

View File

@@ -2,28 +2,26 @@
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) { if (WriteMaxLen() == 0) {
LOG(ERROR) << "buffer full"; LOG(ERROR) << "buffer full";
return false; 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) {
return true; return true;
} }
PLOG(ERROR) << "read()"; PLOG(ERROR) << "read()";
return false; return false;
} }
if (read_len == 0) { if (read_len == 0) {
return false; return false;
} }
Wrote(read_len); Wrote(read_len);
return true; return true;
} }
} // namespace firebuf } // namespace firebuf

View File

@@ -5,13 +5,13 @@
namespace firebuf { namespace firebuf {
class StreamBuffer : public Buffer { class StreamBuffer : public Buffer {
public: public:
StreamBuffer(int sock, size_t size); StreamBuffer(int sock, size_t size);
[[nodiscard]] bool Refill(); [[nodiscard]] bool Refill();
private: private:
int sock_; int sock_;
}; };
} // namespace firebuf } // namespace firebuf