Skip to content

Commit ea0f7cf

Browse files
committed
fix: memory leak, improved code readability
1 parent 6285ca4 commit ea0f7cf

File tree

11 files changed

+167
-131
lines changed

11 files changed

+167
-131
lines changed

CMakeLists.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS})
172172

173173
MESSAGE(STATUS "CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS})
174174

175+
# Line Reading configs
176+
IF(USE_REPLXX)
177+
ADD_DEFINITIONS(-DUSE_REPLXX=1)
178+
MESSAGE(STATUS "Using replxx for line reading")
179+
FIND_PACKAGE(Threads REQUIRED)
180+
FIND_PACKAGE(replxx REQUIRED)
181+
ELSE()
182+
ADD_DEFINITIONS(-DUSE_REPLXX=0)
183+
MESSAGE(STATUS "Using linenoise for line reading")
184+
ENDIF()
185+
175186
# ADD_SUBDIRECTORY(src bin) bin 为目标目录, 可以省略
176187
# ADD_SUBDIRECTORY(deps)
177188
ADD_SUBDIRECTORY(src)
@@ -184,14 +195,3 @@ ENDIF(WITH_BENCHMARK)
184195
IF(WITH_UNIT_TESTS)
185196
ADD_SUBDIRECTORY(unittest)
186197
ENDIF(WITH_UNIT_TESTS)
187-
188-
# Line Reading configs
189-
IF(USE_REPLXX)
190-
ADD_DEFINITIONS(-DUSE_REPLXX=1)
191-
MESSAGE(STATUS "Using replxx for line reading")
192-
FIND_PACKAGE(Threads REQUIRED)
193-
FIND_PACKAGE(replxx REQUIRED)
194-
ELSE()
195-
ADD_DEFINITIONS(-DUSE_REPLXX=0)
196-
MESSAGE(STATUS "Using linenoise for line reading")
197-
ENDIF()

src/common/linereader/line_interface.h

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
You may obtain a copy of Mulan PSL v2 at:
5+
http://license.coscl.org.cn/MulanPSL2
6+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
See the Mulan PSL v2 for more details. */
10+
11+
//
12+
// Created by Willaaaaaaa in 2025
13+
//
14+
15+
#include "common/linereader/line_reader.h"
16+
17+
namespace common {
18+
LineReader LineReaderManager::reader_;
19+
bool LineReaderManager::is_first_call_ = true;
20+
21+
char* LineReaderManager::my_readline(const char* prompt, const std::string& history_file) {
22+
if (is_first_call_) {
23+
reader_.history_load(history_file);
24+
is_first_call_ = false;
25+
}
26+
27+
char* line = reader_.input(prompt);
28+
if (line == nullptr) {
29+
return nullptr;
30+
}
31+
32+
bool is_valid_input = false;
33+
for (auto c = line; *c != '\0'; ++c) {
34+
if (!isspace(*c)) {
35+
is_valid_input = true;
36+
break;
37+
}
38+
}
39+
40+
if (is_valid_input) {
41+
reader_.history_add(line);
42+
}
43+
44+
return line;
45+
}
46+
47+
bool LineReaderManager::is_exit_command(const char* cmd, const std::string& history_file) {
48+
bool is_exit = 0 == strncasecmp("exit", cmd, 4) ||
49+
0 == strncasecmp("bye", cmd, 3) ||
50+
0 == strncasecmp("\\q", cmd, 2) ||
51+
0 == strncasecmp("interrupted", cmd, 11);
52+
53+
if (is_exit) {
54+
reader_.history_save(history_file);
55+
}
56+
57+
return is_exit;
58+
}
59+
} // namespace common
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
You may obtain a copy of Mulan PSL v2 at:
5+
http://license.coscl.org.cn/MulanPSL2
6+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
See the Mulan PSL v2 for more details. */
10+
11+
//
12+
// Created by Willaaaaaaa in 2025
13+
//
14+
15+
#ifndef COMMON_LINE_READER
16+
#define COMMON_LINE_READER
17+
18+
#if USE_REPLXX
19+
#include "replxx.hxx"
20+
using LineReader = replxx::Replxx;
21+
#else
22+
#include "common/linereader/linenoise_reader.h"
23+
using LineReader = LinenoiseReader;
24+
#endif
25+
26+
namespace common {
27+
class LineReaderManager {
28+
public:
29+
/**
30+
* @brief Read a line from input
31+
* @param prompt The prompt to display
32+
* @param history_file path/to/file
33+
* @return char* to input string or nullptr
34+
*/
35+
static char* my_readline(const char* prompt, const std::string& history_file);
36+
37+
/**
38+
* @brief Check if the command is an exit command
39+
* @param cmd The input command
40+
* @param history_file path/to/file
41+
* @return True if the command is an exit command
42+
*/
43+
static bool is_exit_command(const char* cmd, const std::string& history_file);
44+
45+
private:
46+
static LineReader reader_;
47+
static bool is_first_call_;
48+
};
49+
} // namespace common
50+
51+
#endif // COMMON_LINE_READER

src/common/linereader/linereader.cpp renamed to src/common/linereader/linenoise_reader.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,42 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
See the Mulan PSL v2 for more details. */
1010

1111
//
12-
// Created by Willaaaaaaa on 2025
12+
// Created by Willaaaaaaa in 2025
1313
//
1414

15-
#include "common/linereader/linereader.h"
15+
#include "common/linereader/linenoise_reader.h"
1616
#include "common/linereader/linenoise.h"
1717
#include "common/lang/string.h"
1818

19-
namespace common {
20-
LineReader::LineReader() {
19+
LinenoiseReader::LinenoiseReader() {
2120
linenoiseHistorySetMaxLen(1000);
2221
linenoiseSetMultiLine(0);
2322
}
2423

25-
char* LineReader::input(const char* prompt) {
24+
char* LinenoiseReader::input(const char* prompt) {
2625
return linenoise(prompt);
2726
}
2827

29-
bool LineReader::history_load(const std::string& history_file) {
28+
bool LinenoiseReader::history_load(const std::string& history_file) {
3029
history_file_ = history_file;
3130
return linenoiseHistoryLoad(history_file.c_str()) == 0;
3231
}
3332

34-
bool LineReader::history_save(const std::string& history_file) const {
33+
bool LinenoiseReader::history_save(const std::string& history_file) const {
3534
return linenoiseHistorySave(history_file.c_str()) == 0;
3635
}
3736

38-
bool LineReader::history_add(const char* line) {
37+
bool LinenoiseReader::history_add(const char* line) {
3938
if (line != nullptr && *line != '\0') {
4039
return linenoiseHistoryAdd(line) == 0;
4140
}
4241
return false;
4342
}
4443

45-
void LineReader::history_set_max_len(int len) {
44+
void LinenoiseReader::history_set_max_len(int len) {
4645
linenoiseHistorySetMaxLen(len);
4746
}
4847

49-
void LineReader::clear_screen() {
48+
void LinenoiseReader::clear_screen() {
5049
linenoiseClearScreen();
5150
}
52-
} // namespace common

src/common/linereader/linereader.h renamed to src/common/linereader/linenoise_reader.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
See the Mulan PSL v2 for more details. */
1010

1111
//
12-
// Created by Willaaaaaaa on 2025
12+
// Created by Willaaaaaaa in 2025
1313
//
1414

15-
#ifndef COMMON_LINEREADER_H
16-
#define COMMON_LINEREADER_H
15+
#ifndef COMMON_LINENOISE_WRAPPER_H
16+
#define COMMON_LINENOISE_WRAPPER_H
1717

1818
#include "common/lang/string.h"
1919

20-
namespace common {
2120
/**
2221
* @brief A wrapper class for linenoise lib
2322
* @details Support history memory...
2423
*/
25-
class LineReader {
24+
class LinenoiseReader {
2625
public:
27-
LineReader();
28-
~LineReader() = default;
26+
LinenoiseReader();
27+
~LinenoiseReader() = default;
2928

3029
/**
3130
* @brief Read input with C string
@@ -69,6 +68,5 @@ class LineReader {
6968
private:
7069
std::string history_file_;
7170
};
72-
} // namespace common
7371

74-
#endif // COMMON_LINEREADER_H
72+
#endif // COMMON_LINENOISE_WRAPPER_H

src/obclient/client.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ See the Mulan PSL v2 for more details. */
2929

3030
#include "common/defs.h"
3131
#include "common/lang/string.h"
32-
#include "common/linereader/line_interface.h"
32+
#include "common/linereader/line_reader.h"
3333

3434
#define MAX_MEM_BUFFER_SIZE 8192
3535
#define PORT_DEFAULT 6789
@@ -45,7 +45,7 @@ const std::string LINE_HISTORY_FILE = "./.obclient.history";
4545
*/
4646
bool is_exit_command(const char *cmd)
4747
{
48-
return is_exit_command(cmd, LINE_HISTORY_FILE);
48+
return LineReaderManager::is_exit_command(cmd, LINE_HISTORY_FILE);
4949
}
5050

5151
int init_unix_sock(const char *unix_sock_path)
@@ -142,9 +142,8 @@ int main(int argc, char *argv[])
142142

143143
char *input_command = nullptr;
144144
static time_t previous_history_save_time = 0;
145-
static LineInterface reader;
146145

147-
while ((input_command = my_readline(prompt_str, LINE_HISTORY_FILE)) != nullptr) {
146+
while ((input_command = LineReaderManager::my_readline(prompt_str, LINE_HISTORY_FILE)) != nullptr) {
148147
if (common::is_blank(input_command)) {
149148
free(input_command);
150149
input_command = nullptr;
@@ -158,7 +157,7 @@ int main(int argc, char *argv[])
158157
}
159158

160159
if (time(NULL) - previous_history_save_time > 5) {
161-
reader.history_save(LINE_HISTORY_FILE);
160+
LineReaderManager::is_exit_command("", LINE_HISTORY_FILE);
162161
previous_history_save_time = time(NULL);
163162
}
164163

@@ -203,7 +202,7 @@ int main(int argc, char *argv[])
203202
}
204203
close(sockfd);
205204

206-
reader.history_save(LINE_HISTORY_FILE);
205+
LineReaderManager::is_exit_command("", LINE_HISTORY_FILE);
207206
printf("Command history saved to: %s\n", LINE_HISTORY_FILE.c_str());
208207

209208
return 0;

src/oblsm/client/cliutil/parser.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ See the Mulan PSL v2 for more details. */
1515
#include "common/lang/string.h"
1616
#include "common/log/log.h"
1717
#include "common/sys/rc.h"
18+
#include "common/linereader/line_reader.h"
19+
20+
using common::LineReaderManager;
1821

1922
namespace oceanbase {
2023
std::string my_readline(const std::string &prompt)
2124
{
22-
char* line = ::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
25+
char* line = LineReaderManager::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
2326
if (line == nullptr) {
2427
return "";
2528
}

src/oblsm/client/cliutil/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ See the Mulan PSL v2 for more details. */
2323
#include "common/lang/string.h"
2424
#include "common/lang/string_view.h"
2525
#include "oblsm/client/cliutil/defs.h"
26-
#include "common/linereader/line_interface.h"
26+
#include "common/linereader/line_reader.h"
2727

2828
#define MAX_MEM_BUFFER_SIZE 8192
2929
namespace oceanbase {

0 commit comments

Comments
 (0)