Migrated engine to using dlmalloc through eng::

This commit is contained in:
2022-02-24 02:17:41 -05:00
parent acc00289fb
commit f467944095
61 changed files with 631 additions and 590 deletions

View File

@@ -2,8 +2,8 @@
#define CHBUF_SIZE (256*1024)
#define POLLVEC_SIZE (DrivenEngine::MAX_CHAN+1)
static std::unique_ptr<char[]> chbuf;
static std::unique_ptr<struct pollfd[]> pollvec;
static drv::unique_ptr<char[]> chbuf;
static drv::unique_ptr<struct pollfd[]> pollvec;
static MonoClock monoclock;
@@ -30,37 +30,37 @@ static void allocate_buffers() {
pollvec.reset(new struct pollfd[POLLVEC_SIZE]);
}
static void if_error_print_and_exit(const std::string &str) {
static void if_error_print_and_exit(const drv::string &str) {
if (!str.empty()) {
std::cerr << std::endl << "error: " << str << std::endl;
exit(1);
}
}
static std::string_view read_file(const char *fn, char *buf, int bufsize, std::string &err) {
static drv::string_view read_file(const char *fn, char *buf, int bufsize, drv::string &err) {
FILE *f = fopen(fn, "r");
if (f == 0) {
err = std::string("cannot read file") + fn;
err = drv::string("cannot read file") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
return drv::string_view(buf, 0);
}
int nread = fread(buf, 1, bufsize, f);
if (nread < 0) {
err = std::string("cannot read file: ") + fn;
err = drv::string("cannot read file: ") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
return drv::string_view(buf, 0);
}
if (nread == bufsize) {
err = std::string("file too large: ") + fn;
err = drv::string("file too large: ") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
return drv::string_view(buf, 0);
}
err = "";
return std::string_view(buf, nread);
return drv::string_view(buf, nread);
}
static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, std::string_view require_cert) {
static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, drv::string_view require_cert) {
SSL_CTX *ctx = SSL_CTX_new(TLS_method());
SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
@@ -74,12 +74,12 @@ static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, std::string_v
return ctx;
}
static std::string err_print_errors_str() {
static drv::string err_print_errors_str() {
BIO *bio = BIO_new(BIO_s_mem());
ERR_print_errors(bio);
char *buf;
size_t len = BIO_get_mem_data(bio, &buf);
std::string ret(buf, len);
drv::string ret(buf, len);
BIO_free(bio);
return ret;
}
@@ -133,8 +133,8 @@ public:
};
DrivenEngine *driven_;
std::vector<ChanInfo> chans_;
std::map<int, SOCKET> listen_sockets_;
drv::vector<ChanInfo> chans_;
drv::map<int, SOCKET> listen_sockets_;
bool read_console_recently_;
SSL_CTX *ssl_ctx_with_root_certs_;
@@ -143,10 +143,10 @@ public:
void handle_listen_ports() {
const std::vector<int> &listenports = driven_->drv_get_listen_ports();
const auto &listenports = driven_->drv_get_listen_ports();
for (int port : listenports) {
if (listen_sockets_.find(port) == listen_sockets_.end()) {
std::string err;
drv::string err;
SOCKET sock = listen_on_port(port, err);
if_error_print_and_exit(err);
assert(sock != INVALID_SOCKET);
@@ -157,21 +157,21 @@ public:
void handle_lua_source() {
if (driven_->drv_get_rescan_lua_source()) {
std::string err;
std::string_view ctrl = read_file("lua/control.lst", chbuf.get(), CHBUF_SIZE, err);
drv::string err;
drv::string_view ctrl = read_file("lua/control.lst", chbuf.get(), CHBUF_SIZE, err);
if_error_print_and_exit(err);
std::vector<std::string> names = drv::parse_control_lst(ctrl);
drv::vector<drv::string> names = drv::parse_control_lst(ctrl);
driven_->drv_clear_lua_source();
for (const std::string &str : names) {
std::string lfn = std::string("lua/") + str;
std::string_view data = read_file(lfn.c_str(), chbuf.get(), CHBUF_SIZE, err);
for (const drv::string &str : names) {
drv::string lfn = drv::string("lua/") + str;
drv::string_view data = read_file(lfn.c_str(), chbuf.get(), CHBUF_SIZE, err);
if_error_print_and_exit(err);
driven_->drv_add_lua_source(str, data);
}
}
}
void close_channel(ChanInfo &chan, std::string_view err) {
void close_channel(ChanInfo &chan, drv::string_view err) {
// std::cerr << "Closing channel " << chan.chid << std::endl;
assert(chan.state != CHAN_INACTIVE);
// Close and release the SSL channel.
@@ -253,9 +253,9 @@ public:
}
void handle_new_outgoing_sockets() {
const std::vector<int> &chans = driven_->drv_get_new_outgoing();
const auto &chans = driven_->drv_get_new_outgoing();
for (int chid : chans) {
std::string err;
drv::string err;
SOCKET sock = open_connection(driven_->drv_get_target(chid), err);
if (sock == INVALID_SOCKET) {
driven_->drv_notify_close(chid, err);
@@ -270,7 +270,7 @@ public:
}
void accept_connection(int port, SOCKET sock) {
std::string err;
drv::string err;
SOCKET socket = accept_on_socket(sock, err);
if_error_print_and_exit(err);
if (socket != INVALID_SOCKET) {
@@ -281,7 +281,7 @@ public:
}
void advance_plaintext(ChanInfo &chan) {
std::string err;
drv::string err;
// If the channel has no outgoing bytes and has been released,
// just close it.
@@ -418,7 +418,7 @@ public:
void handle_socket_input_output() {
std::string err;
drv::string err;
int mstimeout = read_console_recently_ ? 100 : 1000;
// Peek output buffers and determine channel release flags.