Lots of work on removing malloc from driver

This commit is contained in:
2022-02-17 20:02:08 -05:00
parent 9d7bf8b0f9
commit 467f25b927
17 changed files with 1301 additions and 197 deletions

View File

@@ -2,6 +2,8 @@
#define _WIN32_WINNT 0x0600
#include "driver.hpp"
#include "umm-malloc.hpp"
#include "driver-util.hpp"
#include "util.hpp"
#include "drivenengine.hpp"
#include "dummycert.hpp"
@@ -23,18 +25,12 @@
#include <openssl/bio.h>
#include <openssl/pem.h>
using PollVector = std::vector<struct pollfd>;
static void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket
int status = ioctlsocket(sock, FIONBIO, &mode);
assert(status == 0);
}
static void enable_tty_raw() {
// Do nothing on windows.
}
static std::string winsock_error_string(int errcode) {
std::ostringstream oss;
oss << "error " << errcode;
@@ -46,21 +42,21 @@ static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) {
if (addr->ai_family == AF_INET) {
return addr;
}
}
}std::string
return nullptr;
}
static SOCKET open_connection(const std::string &target, std::string &err) {
static SOCKET open_connection(std::string_view target, UmmString &err) {
PADDRINFOA addrs = nullptr;
PADDRINFOA goodaddr = nullptr;
SOCKET sock = INVALID_SOCKET;
std::string host, port;
std::string_view host, port;
err.clear();
util::split_host_port(target, host, port);
int status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
int status = getaddrinfo(host.data(), port.data(), nullptr, &addrs);
while (status == WSATRY_AGAIN) {
status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
status = getaddrinfo(host.data(), port.data(), nullptr, &addrs);
}
if (status == WSAHOST_NOT_FOUND) {
err = "host not found";
@@ -98,7 +94,7 @@ error:
return SOCKET_ERROR;
}
SOCKET listen_on_port(int port, std::string &err) {
SOCKET listen_on_port(int port, UmmString &err) {
int status;
err.clear();
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
@@ -131,11 +127,10 @@ error:
return SOCKET_ERROR;
}
static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
static SOCKET accept_on_socket(SOCKET listen_socket, UmmString &err) {
SOCKET chsock = accept(listen_socket, nullptr, nullptr);
if (chsock != INVALID_SOCKET) {
set_nonblocking(chsock);
std::cerr << "accepted socket is " << chsock << std::endl;
return chsock;
} else {
int errcode = WSAGetLastError();
@@ -148,7 +143,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
}
}
static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string &err) {
static int socket_send(SOCKET socket, const char *bytes, int nbytes, UmmString &err) {
err.clear();
int wbytes = send(socket, bytes, nbytes, 0);
if (wbytes == SOCKET_ERROR) {
@@ -165,7 +160,7 @@ static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string
}
}
static int socket_recv(SOCKET socket, char *bytes, int nbytes, std::string &err) {
static int socket_recv(SOCKET socket, char *bytes, int nbytes, UmmString &err) {
err.clear();
int nrecv = recv(socket, bytes, nbytes, 0);
if (nrecv < 0) {
@@ -187,8 +182,8 @@ static int socket_close(SOCKET socket) {
return closesocket(socket);
}
static int socket_poll(PollVector &pollvec, int mstimeout, std::string &err) {
int status = WSAPoll(&pollvec[0], pollvec.size(), mstimeout);
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, UmmString &err) {
int status = WSAPoll(pollvec, pollcount, mstimeout);
if (status < 0) {
err = winsock_error_string(WSAGetLastError());
return -1;
@@ -196,7 +191,7 @@ static int socket_poll(PollVector &pollvec, int mstimeout, std::string &err) {
return status;
}
static void socket_init() {
static void init_winsock() {
WSADATA data;
int errcode = WSAStartup(2, &data);
if (errcode != 0) {
@@ -205,10 +200,6 @@ static void socket_init() {
}
}
static void socket_uninit() {
// Nothing needed.
}
static int console_write(const char *bytes, int nbytes) {
if (nbytes == 0) return 0;
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -243,17 +234,8 @@ static int console_read(char *bytes, int nbytes) {
}
}
// The last element in the vector is supposed to be
// for polling stdio. But on windows, you can't poll
// stdio, so on windows, we remove the last element from
// the vector and we reduce mstimeout instead.
static void fill_stdio_pollfd(PollVector &pollvec, int &mstimeout, bool read_console_recently) {
pollvec.pop_back();
if (mstimeout > 100) mstimeout = 100;
}
static void disable_randomization(int argc, char *argv[]) {
// Do nothing.
void driver_sysinit(int argc, char *argv[]) {
init_winsock();
}
class MonoClock {