Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove new/delete in net #1503

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/net/examples/binlog_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char* argv[]) {
std::string ip(argv[1]);
int port = atoi(argv[2]);

NetCli* rcli = NewRedisCli();
std::unique_ptr<NetCli> rcli(NewRedisCli());
rcli->set_connect_timeout(3000);

Status s = rcli->Connect(ip, port, "127.0.0.1");
Expand Down
7 changes: 2 additions & 5 deletions src/net/examples/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* my_conn_factory = new MyConnFactory();
ServerThread* st = NewDispatchThread(port, 4, my_conn_factory, 1000);
std::unique_ptr<ConnFactory> my_conn_factory = std::make_unique<MyConnFactory>();
std::unique_ptr<ServerThread> st(NewDispatchThread(port, 4, my_conn_factory.get(), 1000));

if (st->StartThread() != 0) {
printf("StartThread error happened!\n");
Expand All @@ -109,8 +109,5 @@ int main(int argc, char* argv[]) {
}
st->StopThread();

delete st;
delete my_conn_factory;

return 0;
}
7 changes: 2 additions & 5 deletions src/net/examples/https_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* my_conn_factory = new MyConnFactory();
ServerThread* st = NewDispatchThread(port, 4, my_conn_factory, 1000);
std::unique_ptr<ConnFactory> my_conn_factory = std::make_unique<MyConnFactory>();
std::unique_ptr<ServerThread> st(NewDispatchThread(port, 4, my_conn_factory.get(), 1000));

#if __ENABLE_SSL
if (st->EnableSecurity("/complete_path_to/host.crt", "/complete_path_to/host.key") != 0) {
Expand All @@ -117,8 +117,5 @@ int main(int argc, char* argv[]) {
}
st->StopThread();

delete st;
delete my_conn_factory;

return 0;
}
7 changes: 2 additions & 5 deletions src/net/examples/mydispatch_srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ static void SignalSetup() {

int main() {
SignalSetup();
ConnFactory* my_conn_factory = new MyConnFactory();
ServerThread* st = NewDispatchThread(9211, 10, my_conn_factory, 1000);
std::unique_ptr<ConnFactory> my_conn_factory = std::make_unique<MyConnFactory>();
std::unique_ptr<ServerThread> st(NewDispatchThread(9211, 10, my_conn_factory.get(), 1000));

if (st->StartThread() != 0) {
printf("StartThread error happened!\n");
Expand All @@ -88,8 +88,5 @@ int main() {
}
st->StopThread();

delete st;
delete my_conn_factory;

return 0;
}
7 changes: 2 additions & 5 deletions src/net/examples/myholy_srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* conn_factory = new MyConnFactory();
std::unique_ptr<ConnFactory> conn_factory = std::make_unique<MyConnFactory>();

ServerThread* my_thread = NewHolyThread(my_port, conn_factory);
std::unique_ptr<ServerThread> my_thread(NewHolyThread(my_port, conn_factory.get()));
if (my_thread->StartThread() != 0) {
printf("StartThread error happened!\n");
exit(-1);
Expand All @@ -93,8 +93,5 @@ int main(int argc, char* argv[]) {
}
my_thread->StopThread();

delete my_thread;
delete conn_factory;

return 0;
}
4 changes: 1 addition & 3 deletions src/net/examples/myholy_srv_chandle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int main(int argc, char* argv[]) {
MyConnFactory conn_factory;
MyServerHandle handle;

ServerThread* my_thread = NewHolyThread(my_port, &conn_factory, 1000, &handle);
std::unique_ptr<ServerThread> my_thread(NewHolyThread(my_port, &conn_factory, 1000, &handle));
if (my_thread->StartThread() != 0) {
printf("StartThread error happened!\n");
exit(-1);
Expand All @@ -118,7 +118,5 @@ int main(int argc, char* argv[]) {
}
my_thread->StopThread();

delete my_thread;

return 0;
}
4 changes: 2 additions & 2 deletions src/net/examples/myproto_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <memory>

#include "myproto.pb.h"
#include "net/include/net_cli.h"
Expand All @@ -18,7 +19,7 @@ int main(int argc, char* argv[]) {
std::string ip(argv[1]);
int port = atoi(argv[2]);

NetCli* cli = NewPbCli();
std::unique_ptr<NetCli> cli(NewPbCli());

Status s = cli->Connect(ip, port);
if (!s.ok()) {
Expand Down Expand Up @@ -48,6 +49,5 @@ int main(int argc, char* argv[]) {
}
cli->Close();

delete cli;
return 0;
}
12 changes: 6 additions & 6 deletions src/net/examples/myredis_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MyConn::MyConn(int fd, const std::string& ip_port, Thread* thread, void* worker_
// Handle worker_specific_data ...
}

ClientThread* client;
std::unique_ptr<ClientThread> client;
int sendto_port;
int MyConn::DealMessage(const RedisCmdArgsType& argv, std::string* response) {
sleep(1);
Expand Down Expand Up @@ -95,10 +95,11 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* conn_factory = new MyConnFactory();
std::unique_ptr<ConnFactory> conn_factory = std::make_unique<MyConnFactory>();
//"handle" will be deleted within "client->StopThread()"
ClientHandle* handle = new ClientHandle();

client = new ClientThread(conn_factory, 3000, 60, handle, nullptr);
client = std::make_unique<ClientThread>(conn_factory.get(), 3000, 60, handle, nullptr);

if (client->StartThread() != 0) {
printf("StartThread error happened!\n");
Expand All @@ -107,11 +108,10 @@ int main(int argc, char* argv[]) {
running.store(true);
while (running.load()) {
sleep(1);
DoCronWork(client, sendto_port);
DoCronWork(client.get(), sendto_port);
}

client->StopThread();
delete client;
delete conn_factory;
client.reset();
return 0;
}
8 changes: 3 additions & 5 deletions src/net/examples/myredis_srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* conn_factory = new MyConnFactory();
std::unique_ptr<ConnFactory> conn_factory = std::make_unique<MyConnFactory>();

ServerThread* my_thread = new HolyThread(my_port, conn_factory, 1000, nullptr, false);
std::unique_ptr<ServerThread> my_thread =
std::make_unique<HolyThread>(my_port, conn_factory.get(), 1000, nullptr, false);
if (my_thread->StartThread() != 0) {
printf("StartThread error happened!\n");
exit(-1);
Expand All @@ -109,8 +110,5 @@ int main(int argc, char* argv[]) {
}
my_thread->StopThread();

delete my_thread;
delete conn_factory;

return 0;
}
3 changes: 1 addition & 2 deletions src/net/examples/performance/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) {
std::string ip(argv[1]);
int port = atoi(argv[2]);

NetCli* cli = NewPbCli();
std::unique_ptr<NetCli> cli(NewPbCli());

Status s = cli->Connect(ip, port);
if (!s.ok()) {
Expand All @@ -44,6 +44,5 @@ int main(int argc, char* argv[]) {
}
cli->Close();

delete cli;
return 0;
}
4 changes: 1 addition & 3 deletions src/net/examples/performance/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {

SignalSetup();

ServerThread* st_thread = NewDispatchThread(ip, port, 24, &conn_factory, 1000);
std::unique_ptr<ServerThread> st_thread(NewDispatchThread(ip, port, 24, &conn_factory, 1000));
st_thread->StartThread();
uint64_t st, ed;

Expand All @@ -99,7 +99,5 @@ int main(int argc, char* argv[]) {
}
st_thread->StopThread();

delete st_thread;

return 0;
}
2 changes: 1 addition & 1 deletion src/net/examples/redis_cli_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char* argv[]) {
ret = net::SerializeRedisCommand(vec, &str);
printf(" 2. Serialize by vec return %d, (%s)\n", ret, str.c_str());

NetCli* rcli = NewRedisCli();
std::unique_ptr<NetCli> rcli(NewRedisCli());
rcli->set_connect_timeout(3000);

// redis v3.2+ protect mode will block other ip
Expand Down
2 changes: 1 addition & 1 deletion src/net/examples/redis_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char* argv[]) {
std::string ip(argv[1]);
int port = atoi(argv[2]);

NetCli* rcli = NewRedisCli();
std::unique_ptr<NetCli> rcli(NewRedisCli());
rcli->set_connect_timeout(3000);

Status s = rcli->Connect(ip, port, "127.0.0.1");
Expand Down
7 changes: 2 additions & 5 deletions src/net/examples/simple_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ int main(int argc, char* argv[]) {

SignalSetup();

ConnFactory* my_conn_factory = new MyConnFactory();
ServerThread* st = NewDispatchThread(port, 4, my_conn_factory, 1000);
std::unique_ptr<ConnFactory> my_conn_factory = std::make_unique<MyConnFactory>();
std::unique_ptr<ServerThread> st(NewDispatchThread(port, 4, my_conn_factory.get(), 1000));

if (st->StartThread() != 0) {
printf("StartThread error happened!\n");
Expand All @@ -89,8 +89,5 @@ int main(int argc, char* argv[]) {
}
st->StopThread();

delete st;
delete my_conn_factory;

return 0;
}
4 changes: 2 additions & 2 deletions src/net/include/net_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#ifndef NET_INCLUDE_NET_CLI_H_
#define NET_INCLUDE_NET_CLI_H_

#include <memory>
#include <string>

#include "pstd/include/pstd_status.h"

using pstd::Status;
Expand Down Expand Up @@ -47,7 +47,7 @@ class NetCli {

private:
struct Rep;
Rep* rep_;
std::shared_ptr<Rep> rep_;
int set_tcp_nodelay();

NetCli(const NetCli&);
Expand Down
2 changes: 1 addition & 1 deletion src/net/include/server_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ServerThread : public Thread {
*/
int port_ = -1;
std::set<std::string> ips_;
std::vector<ServerSocket*> server_sockets_;
std::vector<std::shared_ptr<ServerSocket>> server_sockets_;
std::set<int32_t> server_fds_;

virtual int InitHandle();
Expand Down
21 changes: 9 additions & 12 deletions src/net/src/dispatch_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ DispatchThread::DispatchThread(int port, int work_num, ConnFactory* conn_factory
last_thread_(0),
work_num_(work_num),
queue_limit_(queue_limit) {
worker_thread_ = new WorkerThread*[work_num_];
for (int i = 0; i < work_num_; i++) {
worker_thread_[i] = new WorkerThread(conn_factory, this, queue_limit, cron_interval);
worker_thread_.emplace_back(std::make_shared<WorkerThread>(conn_factory, this, queue_limit, cron_interval));
}
}

Expand All @@ -32,9 +31,9 @@ DispatchThread::DispatchThread(const std::string& ip, int port, int work_num, Co
last_thread_(0),
work_num_(work_num),
queue_limit_(queue_limit) {
worker_thread_ = new WorkerThread*[work_num_];

for (int i = 0; i < work_num_; i++) {
worker_thread_[i] = new WorkerThread(conn_factory, this, queue_limit, cron_interval);
worker_thread_.emplace_back(std::make_shared<WorkerThread>(conn_factory, this, queue_limit, cron_interval));
}
}

Expand All @@ -44,17 +43,15 @@ DispatchThread::DispatchThread(const std::set<std::string>& ips, int port, int w
last_thread_(0),
work_num_(work_num),
queue_limit_(queue_limit) {
worker_thread_ = new WorkerThread*[work_num_];

for (int i = 0; i < work_num_; i++) {
worker_thread_[i] = new WorkerThread(conn_factory, this, queue_limit, cron_interval);
worker_thread_.emplace_back(std::make_shared<WorkerThread>(conn_factory, this, queue_limit, cron_interval));

}
}

DispatchThread::~DispatchThread() {
for (int i = 0; i < work_num_; i++) {
delete worker_thread_[i];
}
delete[] worker_thread_;

}

int DispatchThread::StartThread() {
Expand Down Expand Up @@ -129,7 +126,7 @@ std::shared_ptr<NetConn> DispatchThread::MoveConnOut(int fd) {
}

void DispatchThread::MoveConnIn(std::shared_ptr<NetConn> conn, const NotifyType& type) {
WorkerThread* worker_thread = worker_thread_[last_thread_];
std::shared_ptr<WorkerThread> worker_thread = worker_thread_[last_thread_];
bool success = worker_thread->MoveConnIn(conn, type, true);
if (success) {
last_thread_ = (last_thread_ + 1) % work_num_;
Expand All @@ -155,7 +152,7 @@ void DispatchThread::HandleNewConn(const int connfd, const std::string& ip_port)
int next_thread = last_thread_;
bool find = false;
for (int cnt = 0; cnt < work_num_; cnt++) {
WorkerThread* worker_thread = worker_thread_[next_thread];
std::shared_ptr<WorkerThread> worker_thread = worker_thread_[next_thread];
find = worker_thread->MoveConnIn(ti, false);
if (find) {
last_thread_ = (next_thread + 1) % work_num_;
Expand Down
2 changes: 1 addition & 1 deletion src/net/src/dispatch_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DispatchThread : public ServerThread {
/*
* This is the work threads
*/
WorkerThread** worker_thread_;
std::vector<std::shared_ptr<WorkerThread>> worker_thread_;
int queue_limit_;
std::map<WorkerThread*, void*> localdata_;

Expand Down
Loading
Loading