Skip to content

Commit 098b462

Browse files
committed
修改部分内存问题
1 parent 37be509 commit 098b462

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

CodeFormatServer/src/Protocol/ProtocolBuffer.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,27 @@
33

44
ProtocolBuffer::ProtocolBuffer(std::size_t capacity)
55
: _writeIndex(0),
6-
_readBuffer(capacity, 0),
6+
// _readBuffer(capacity, 0),
7+
_textProtocol(capacity, 0),
78
_contentLength(0),
8-
_bodyStartIndex(0)
9+
_bodyStartIndex(0),
10+
_normalCapacity(capacity)
911
{
1012
}
1113

1214
char* ProtocolBuffer::GetWritableCursor()
1315
{
14-
return _readBuffer.data();
16+
return _textProtocol.data() + _writeIndex;
1517
}
1618

1719

1820
std::size_t ProtocolBuffer::GetRestCapacity()
1921
{
20-
return _readBuffer.size();
22+
return _textProtocol.size() - _writeIndex;
2123
}
2224

23-
void ProtocolBuffer::WriteBuff(std::size_t size)
25+
void ProtocolBuffer::SetWriteSize(std::size_t size)
2426
{
25-
if (_textProtocol.size() < _writeIndex + size)
26-
{
27-
if (_bodyStartIndex != 0 && _contentLength != 0 && _writeIndex < (_bodyStartIndex + _contentLength))
28-
{
29-
_textProtocol.resize(std::max(_writeIndex + size, _bodyStartIndex + _contentLength));
30-
}
31-
else
32-
{
33-
_textProtocol.resize(_writeIndex + size);
34-
}
35-
}
36-
37-
std::copy_n(_readBuffer.begin(), size, _textProtocol.begin() + _writeIndex);
3827
_writeIndex += size;
3928
}
4029

@@ -62,6 +51,15 @@ std::string_view ProtocolBuffer::ReadOneProtocol()
6251
return "";
6352
}
6453

54+
void ProtocolBuffer::FitCapacity()
55+
{
56+
auto oneProtocolCapacity = _contentLength + _bodyStartIndex;
57+
if (oneProtocolCapacity > _normalCapacity && oneProtocolCapacity > _textProtocol.size())
58+
{
59+
_textProtocol.resize(oneProtocolCapacity);
60+
}
61+
}
62+
6563
void ProtocolBuffer::Reset()
6664
{
6765
if (_writeIndex > _contentLength + _bodyStartIndex)
@@ -70,15 +68,25 @@ void ProtocolBuffer::Reset()
7068
std::copy_n(_textProtocol.data() + doneIndex, _writeIndex - doneIndex, _textProtocol.data());
7169

7270
_writeIndex -= doneIndex;
73-
_textProtocol.resize(std::max(_writeIndex, _readBuffer.size()));
74-
_textProtocol.shrink_to_fit();
71+
if (_writeIndex > _normalCapacity)
72+
{
73+
_textProtocol.resize(_writeIndex);
74+
_textProtocol.shrink_to_fit();
75+
}
76+
else
77+
{
78+
_textProtocol.resize(_normalCapacity);
79+
_textProtocol.shrink_to_fit();
80+
}
7581
}
7682
else
7783
{
7884
_writeIndex = 0;
79-
_textProtocol.clear();
80-
_textProtocol.resize(_readBuffer.size());
81-
_textProtocol.shrink_to_fit();
85+
if (_textProtocol.size() > _normalCapacity)
86+
{
87+
_textProtocol.resize(_normalCapacity);
88+
_textProtocol.shrink_to_fit();
89+
}
8290
}
8391

8492
_contentLength = 0;
@@ -121,8 +129,10 @@ bool ProtocolBuffer::TryParseHead()
121129
}
122130
auto lengthStr = text.substr(colonPosition + 1, lineEndPosition - colonPosition - 1);
123131
_contentLength = std::stoi(std::string(lengthStr));
132+
124133
}
125134
index = lineEndPosition + 2;
135+
_bodyStartIndex = index + 2;
126136
state = ParseState::CRLF;
127137
break;
128138
}

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ void SocketIOSession::Run()
2626
goto endLoop;
2727
}
2828

29-
_protocolBuffer.WriteBuff(readSize);
29+
_protocolBuffer.SetWriteSize(readSize);
3030

3131
if (_protocolBuffer.CanReadOneProtocol())
3232
{
3333
break;
3434
}
35+
36+
_protocolBuffer.FitCapacity();
37+
// if(_protocolBuffer.)
3538
}
3639
while (true);
3740

CodeFormatServer/src/Session/StandardIOSession.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ void StandardIOSession::Run()
2222
goto endLoop;
2323
}
2424

25-
_protocolBuffer.WriteBuff(readSize);
25+
_protocolBuffer.SetWriteSize(readSize);
2626

2727
if (_protocolBuffer.CanReadOneProtocol())
2828
{
2929
break;
3030
}
3131

32+
_protocolBuffer.FitCapacity();
33+
3234
} while (true);
3335

3436
do {

LuaParser/src/LuaParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void LuaParser::BuildAst()
5454
{
5555
_errors.push_back(tokeError);
5656
}
57+
_tokenParser->ReleaseTokens();
5758
}
5859

5960
std::shared_ptr<LuaAstNode> LuaParser::GetAst()

LuaParser/src/LuaTokenParser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ bool LuaTokenParser::HasError() const
233233
return !_errors.empty();
234234
}
235235

236+
void LuaTokenParser::ReleaseTokens()
237+
{
238+
_tokens.clear();
239+
_tokens.shrink_to_fit();
240+
241+
_commentTokens.clear();
242+
_commentTokens.shrink_to_fit();
243+
}
244+
236245
LuaTokenType LuaTokenParser::Lex()
237246
{
238247
ResetBuffer();
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string>
4+
#include <list>
45

56
class ProtocolBuffer
67
{
@@ -11,24 +12,24 @@ class ProtocolBuffer
1112

1213
std::size_t GetRestCapacity();
1314

14-
void WriteBuff(std::size_t size);
15+
void SetWriteSize(std::size_t size);
1516

1617
bool CanReadOneProtocol();
1718

1819
std::string_view ReadOneProtocol();
1920

21+
void FitCapacity();
22+
2023
void Reset();
2124
private:
2225
bool TryParseHead();
2326
// 代表从这个位置开始可写
2427
std::size_t _writeIndex;
25-
std::string _textProtocol;
2628

27-
std::string _readBuffer;
29+
std::string _textProtocol;
2830

2931
std::size_t _contentLength;
3032
std::size_t _bodyStartIndex;
31-
32-
33+
std::size_t _normalCapacity;
3334
};
3435

include/LuaParser/LuaTokenParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class LuaTokenParser
4343

4444
bool HasError() const;
4545

46+
void ReleaseTokens();
4647
private:
4748
static std::map<std::string, LuaTokenType, std::less<>> LuaReserved;
4849

0 commit comments

Comments
 (0)