More work on moving engine into dlmalloc heap

This commit is contained in:
2022-02-25 19:57:23 -05:00
parent 08f6aa2092
commit ff932dba10
52 changed files with 351 additions and 484 deletions

View File

@@ -49,13 +49,13 @@
#include "wrap-string.hpp"
#include "wrap-deque.hpp"
#include "wrap-unordered-map.hpp"
#include "wrap-ostream.hpp"
#include "streambuffer.hpp"
#include "debugcollector.hpp"
#include "util.hpp"
#include <cassert>
#include <ostream>
class AnimStep {
friend class AnimQueue;

View File

@@ -1,7 +1,7 @@
#include "wrap-algorithm.hpp"
#include <cstring>
#include <algorithm>
#include "debugcollector.hpp"
#include "util.hpp"
@@ -56,7 +56,7 @@ bool DebugCollector::do_header() {
return true;
}
void DebugCollector::dump(eng::ostream &os) {
void DebugCollector::dump(std::ostream &os) {
flush();
for (const eng::string &line : lines_) {
os << line << std::endl;

View File

@@ -3,9 +3,10 @@
#include "wrap-vector.hpp"
#include "wrap-string.hpp"
#include "wrap-memory.hpp"
#include "wrap-sstream.hpp"
#include "wrap-ostream.hpp"
#include <ostream>
#include <memory>
class DebugCollector {
private:
@@ -28,7 +29,7 @@ public:
DebugCollector(const eng::string &targets);
bool do_header();
bool do_line();
void dump(eng::ostream &os);
void dump(std::ostream &os);
friend class DebugBlock;
};

View File

@@ -1,30 +1,32 @@
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "wrap-utility.hpp"
#include <utility>
#include <iostream>
#include <cstring>
#include "drivenengine.hpp"
static eng::vector<eng::pair<eng::string, DrivenEngineMaker>> makers;
DrivenEngineReg *DrivenEngineReg::All;
void DrivenEngine::register_maker(const char *kind, DrivenEngineMaker maker) {
eng::string skind(kind);
makers.push_back(std::make_pair(skind, maker));
DrivenEngineReg::DrivenEngineReg(const char *n, DrivenEngineMaker fn) {
name = n;
maker = fn;
next = All;
All = this;
}
void DrivenEngine::print_usage(eng::ostream &strm, const char *progname) {
void DrivenEngine::print_usage(std::ostream &strm, const char *progname) {
strm << "Usage: " << progname << " <mode>" << std::endl;
for (const auto &mpair : makers) {
strm << " Mode can be: " << mpair.first << std::endl;
for (auto reg = DrivenEngineReg::All; reg != nullptr; reg=reg->next) {
strm << " Mode can be: " << reg->name << std::endl;
}
}
UniqueDrivenEngine DrivenEngine::make(const char *kind) {
for (const auto &mpair : makers) {
if (strcmp(mpair.first.c_str(), kind) == 0) {
return mpair.second();
for (auto reg = DrivenEngineReg::All; reg != nullptr; reg=reg->next) {
if (strcmp(reg->name, kind) == 0) {
return reg->maker();
}
}
return nullptr;
@@ -38,8 +40,8 @@ Channel::Channel(DrivenEngine *de, int chid, int port, const eng::string &target
readline_lastc_ = 0;
desired_prompt_ = "";
stop_driver_ = stop;
sb_in_ = std::make_shared<StreamBuffer>();
sb_out_ = std::make_shared<StreamBuffer>();
sb_in_ = eng::make_shared<StreamBuffer>();
sb_out_ = eng::make_shared<StreamBuffer>();
sb_drvout_ = sb_out_;
}
@@ -160,7 +162,7 @@ double DrivenEngine::get_clock() {
SharedChannel DrivenEngine::new_outgoing_channel(const eng::string &target) {
int chid = find_unused_chid();
new_outgoing_.push_back(chid);
SharedChannel result = std::make_shared<Channel>(this, chid, 0, target, stop_driver_);
SharedChannel result = eng::make_shared<Channel>(this, chid, 0, target, stop_driver_);
channels_[chid] = result;
return result;
}
@@ -250,7 +252,7 @@ void DrivenEngine::drv_notify_close(int chid, std::string_view err) {
int DrivenEngine::drv_notify_accept(int port) {
int chid = find_unused_chid();
channels_[chid] = std::make_shared<Channel>(this, chid, port, "", stop_driver_);
channels_[chid] = eng::make_shared<Channel>(this, chid, port, "", stop_driver_);
accepted_channels_.push_back(channels_[chid]);
return chid;
}
@@ -288,8 +290,8 @@ bool DrivenEngine::drv_get_stop_driver() const {
DrivenEngine::DrivenEngine() {
next_unused_chid_ = 1;
stdio_channel_ = std::make_shared<Channel>(this, 0, 0, "", false);
stdio_channel_->sb_drvout_ = std::make_shared<StreamBuffer>();
stdio_channel_ = eng::make_shared<Channel>(this, 0, 0, "", false);
stdio_channel_->sb_drvout_ = eng::make_shared<StreamBuffer>();
channels_[0] = stdio_channel_;
rescan_lua_source_ = true;
clock_ = 0.0;

View File

@@ -92,16 +92,18 @@
#ifndef DRIVENENGINE_HPP
#define DRIVENENGINE_HPP
#include "wrap-memory.hpp"
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "wrap-ostream.hpp"
#include <ostream>
#include <memory>
#include <string_view>
#include "util.hpp"
#include "streambuffer.hpp"
class DrivenEngine;
using UniqueDrivenEngine = eng::unique_ptr<DrivenEngine>;
using UniqueDrivenEngine = std::unique_ptr<DrivenEngine>;
using DrivenEngineMaker = UniqueDrivenEngine (*)();
class Channel {
@@ -162,13 +164,13 @@ private:
int chid_;
// These are the in/out buffers presented to the user.
eng::shared_ptr<StreamBuffer> sb_in_;
eng::shared_ptr<StreamBuffer> sb_out_;
std::shared_ptr<StreamBuffer> sb_in_;
std::shared_ptr<StreamBuffer> sb_out_;
// If this is stdio, we inject tty echoes into the output stream.
// This buffer holds the users output interleaved with the tty echoes.
// In any other channel, this is just another pointer to sb_out.
eng::shared_ptr<StreamBuffer> sb_drvout_;
std::shared_ptr<StreamBuffer> sb_drvout_;
int port_;
bool closed_;
@@ -186,10 +188,20 @@ private:
friend class DrivenEngine;
};
using SharedChannel = eng::shared_ptr<Channel>;
using SharedChannel = std::shared_ptr<Channel>;
class DrivenEngine {
public:
//////////////////////////////////////////////////////////////
//
// Build the named engine
//
//////////////////////////////////////////////////////////////
static UniqueDrivenEngine make(const char *name);
static void print_usage(std::ostream &strm, const char *progname);
//////////////////////////////////////////////////////////////
//
// The following methods are the 'engine' side of the pipe.
@@ -254,7 +266,7 @@ public:
// Obtain the output buffer of the stdio channel as an ostream.
//
eng::ostream &stdostream() { return get_stdio_channel()->out()->ostream(); }
std::ostream &stdostream() { return get_stdio_channel()->out()->ostream(); }
// Fetches the lua source, and takes ownership of it. The DrivenEngine
// no longer contains the source after calling this.
@@ -408,19 +420,6 @@ public:
static void set(DrivenEngine *de);
static DrivenEngine *get();
public:
//////////////////////////////////////////////////////////////
//
// The Registry of DrivenEngineMakers.
//
//////////////////////////////////////////////////////////////
static void register_maker(const char *kind, DrivenEngineMaker maker);
static void print_usage(eng::ostream &strm, const char *progname);
static UniqueDrivenEngine make(const char *kind);
private:
// Find a currently-unused channel ID. Channel IDs
// are small integers that are reused.
@@ -443,4 +442,19 @@ private:
friend class Channel;
};
struct DrivenEngineReg {
const char *name;
DrivenEngineMaker maker;
DrivenEngineReg *next;
static DrivenEngineReg *All;
DrivenEngineReg(const char *name, DrivenEngineMaker f);
};
#define DrivenEngineDefine(name, cname) \
UniqueDrivenEngine dengmake_##cname() { \
return UniqueDrivenEngine(new cname); \
} \
DrivenEngineReg dengreg_##cname(name, dengmake_##cname);
#endif // DRIVENENGINE_HPP

View File

@@ -2,8 +2,8 @@
#define CHBUF_SIZE (256*1024)
#define POLLVEC_SIZE (DrivenEngine::MAX_CHAN+1)
static drv::unique_ptr<char[]> chbuf;
static drv::unique_ptr<struct pollfd[]> pollvec;
static std::unique_ptr<char[]> chbuf;
static std::unique_ptr<struct pollfd[]> pollvec;
static MonoClock monoclock;
@@ -13,45 +13,33 @@ namespace util {
}
}
static void initialize_engine() {
SourceDB::register_lua_builtins();
DrivenEngine::register_maker("textgame", make_TextGame);
DrivenEngine::register_maker("lpxclient", make_LpxClient);
DrivenEngine::register_maker("lpxserver", make_LpxServer);
DrivenEngine::register_maker("driverstubtest", make_DriverStubTest);
DrivenEngine::register_maker("driverwebservertest", make_DriverWebServerTest);
DrivenEngine::register_maker("driverdnsfailtest", make_DriverDNSFailTest);
DrivenEngine::register_maker("driverprintclocktest", make_DriverPrintClockTest);
DrivenEngine::register_maker("unittest", make_RunUnitTests);
}
static void allocate_buffers() {
chbuf.reset(new char[CHBUF_SIZE]);
pollvec.reset(new struct pollfd[POLLVEC_SIZE]);
}
static void if_error_print_and_exit(const drv::string &str) {
static void if_error_print_and_exit(const std::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, drv::string &err) {
static std::string_view read_file(const char *fn, char *buf, int bufsize, std::string &err) {
FILE *f = fopen(fn, "r");
if (f == 0) {
err = drv::string("cannot read file") + fn;
err = std::string("cannot read file") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
}
int nread = fread(buf, 1, bufsize, f);
if (nread < 0) {
err = drv::string("cannot read file: ") + fn;
err = std::string("cannot read file: ") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
}
if (nread == bufsize) {
err = drv::string("file too large: ") + fn;
err = std::string("file too large: ") + fn;
buf[0] = 0;
return std::string_view(buf, 0);
}
@@ -74,12 +62,12 @@ static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, std::string_v
return ctx;
}
static drv::string err_print_errors_str() {
static std::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);
drv::string ret(buf, len);
std::string ret(buf, len);
BIO_free(bio);
return ret;
}
@@ -133,8 +121,8 @@ public:
};
DrivenEngine *driven_;
drv::vector<ChanInfo> chans_;
drv::map<int, SOCKET> listen_sockets_;
std::vector<ChanInfo> chans_;
std::map<int, SOCKET> listen_sockets_;
bool read_console_recently_;
SSL_CTX *ssl_ctx_with_root_certs_;
@@ -146,7 +134,7 @@ public:
const auto &listenports = driven_->drv_get_listen_ports();
for (int port : listenports) {
if (listen_sockets_.find(port) == listen_sockets_.end()) {
drv::string err;
std::string err;
SOCKET sock = listen_on_port(port, err);
if_error_print_and_exit(err);
assert(sock != INVALID_SOCKET);
@@ -157,13 +145,13 @@ public:
void handle_lua_source() {
if (driven_->drv_get_rescan_lua_source()) {
drv::string err;
std::string err;
std::string_view ctrl = read_file("lua/control.lst", chbuf.get(), CHBUF_SIZE, err);
if_error_print_and_exit(err);
drv::vector<drv::string> names = drv::parse_control_lst(ctrl);
std::vector<std::string> names = drv::parse_control_lst(ctrl);
driven_->drv_clear_lua_source();
for (const drv::string &str : names) {
drv::string lfn = drv::string("lua/") + str;
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);
if_error_print_and_exit(err);
driven_->drv_add_lua_source(str, data);
@@ -255,7 +243,7 @@ public:
void handle_new_outgoing_sockets() {
const auto &chans = driven_->drv_get_new_outgoing();
for (int chid : chans) {
drv::string err;
std::string err;
SOCKET sock = open_connection(driven_->drv_get_target(chid), err);
if (sock == INVALID_SOCKET) {
driven_->drv_notify_close(chid, err);
@@ -270,7 +258,7 @@ public:
}
void accept_connection(int port, SOCKET sock) {
drv::string err;
std::string err;
SOCKET socket = accept_on_socket(sock, err);
if_error_print_and_exit(err);
if (socket != INVALID_SOCKET) {
@@ -281,7 +269,7 @@ public:
}
void advance_plaintext(ChanInfo &chan) {
drv::string err;
std::string err;
// If the channel has no outgoing bytes and has been released,
// just close it.
@@ -418,7 +406,7 @@ public:
void handle_socket_input_output() {
drv::string err;
std::string err;
int mstimeout = read_console_recently_ ? 100 : 1000;
// Peek output buffers and determine channel release flags.

View File

@@ -6,10 +6,6 @@
#include "drivenengine.hpp"
#include "dummycert.hpp"
#include "util.hpp"
#include "textgame.hpp"
#include "lpxclient.hpp"
#include "lpxserver.hpp"
#include "eng-tests.hpp"
#include "source.hpp"
#include <iostream>
@@ -43,7 +39,7 @@ const int INVALID_SOCKET = -1;
struct termios orig_termios;
static drv::string strerror_str(int err) {
static std::string strerror_str(int err) {
char errbuf[256];
return strerror_r(errno, errbuf, 256);
}
@@ -73,7 +69,7 @@ static void enable_tty_raw() {
assert(status >= 0);
}
static SOCKET open_connection(std::string_view target, drv::string &err) {
static SOCKET open_connection(std::string_view target, std::string &err) {
struct addrinfo *addrs = nullptr;
struct addrinfo *goodaddr = nullptr;
struct addrinfo hints;
@@ -86,7 +82,7 @@ static SOCKET open_connection(std::string_view target, drv::string &err) {
hints.ai_flags = AI_NUMERICSERV;
err.clear();
drv::string host, port;
std::string host, port;
drv::split_host_port(target, host, port);
int status = getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs);
if (status != 0) {
@@ -119,7 +115,7 @@ error_general:
return INVALID_SOCKET;
}
static SOCKET listen_on_port(int port, drv::string &err) {
static SOCKET listen_on_port(int port, std::string &err) {
int status, enable;
err.clear();
@@ -150,7 +146,7 @@ error_errno:
return INVALID_SOCKET;
}
static SOCKET accept_on_socket(SOCKET listen_socket, drv::string &err) {
static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
err.clear();
SOCKET chsock = accept(listen_socket, nullptr, nullptr);
if (chsock >= 0) {
@@ -170,7 +166,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, drv::string &err) {
// zero: would block
// negative: channel closed, possibly cleanly or possibly with error
//
static int socket_send(SOCKET socket, const char *bytes, int nbytes, drv::string &err) {
static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string &err) {
err.clear();
int wbytes = send(socket, bytes, nbytes, 0);
if (wbytes < 0) {
@@ -185,7 +181,7 @@ static int socket_send(SOCKET socket, const char *bytes, int nbytes, drv::string
}
}
static int socket_recv(SOCKET socket, char *bytes, int nbytes, drv::string &err) {
static int socket_recv(SOCKET socket, char *bytes, int nbytes, std::string &err) {
err.clear();
int nrecv = recv(socket, bytes, nbytes, 0);
if (nrecv < 0) {
@@ -206,7 +202,7 @@ static int socket_close(SOCKET socket) {
return close(socket);
}
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, drv::string &err) {
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, std::string &err) {
// socket_poll is implicitly expected to also poll stdin,
// if the OS allows that. Linux does, so we add stdin to the
// poll vector. The poll vector is required to have at
@@ -272,7 +268,7 @@ int main(int argc, char **argv)
allocate_buffers();
enable_tty_raw();
OPENSSL_init_ssl(0, NULL);
initialize_engine();
SourceDB::register_lua_builtins();
driver_drive(argc, argv);
}

View File

@@ -9,10 +9,6 @@
#include "drivenengine.hpp"
#include "dummycert.hpp"
#include "util.hpp"
#include "textgame.hpp"
#include "lpxclient.hpp"
#include "lpxserver.hpp"
#include "eng-tests.hpp"
#include "source.hpp"
#include <iostream>
@@ -35,8 +31,8 @@
#define CHBUF_SIZE (256*1024)
#define POLLVEC_SIZE (DrivenEngine::MAX_CHAN+1)
static drv::unique_ptr<char[]> chbuf;
static drv::unique_ptr<struct pollfd[]> pollvec;
static std::unique_ptr<char[]> chbuf;
static std::unique_ptr<struct pollfd[]> pollvec;
static void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket
@@ -44,7 +40,7 @@ static void set_nonblocking(SOCKET sock) {
assert(status == 0);
}
static drv::string winsock_error_string(int errcode) {
static std::string winsock_error_string(int errcode) {
drv::ostringstream oss;
oss << "error " << errcode;
return oss.str();
@@ -55,11 +51,11 @@ static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) {
if (addr->ai_family == AF_INET) {
return addr;
}
}drv::string
}std::string
return nullptr;
}
static SOCKET open_connection(std::string_view target, drv::string &err) {
static SOCKET open_connection(std::string_view target, std::string &err) {
PADDRINFOA addrs = nullptr;
PADDRINFOA goodaddr = nullptr;
SOCKET sock = INVALID_SOCKET;
@@ -107,7 +103,7 @@ error:
return SOCKET_ERROR;
}
SOCKET listen_on_port(int port, drv::string &err) {
SOCKET listen_on_port(int port, std::string &err) {
int status;
err.clear();
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
@@ -140,7 +136,7 @@ error:
return SOCKET_ERROR;
}
static SOCKET accept_on_socket(SOCKET listen_socket, drv::string &err) {
static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
SOCKET chsock = accept(listen_socket, nullptr, nullptr);
if (chsock != INVALID_SOCKET) {
set_nonblocking(chsock);
@@ -156,7 +152,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, drv::string &err) {
}
}
static int socket_send(SOCKET socket, const char *bytes, int nbytes, drv::string &err) {
static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string &err) {
err.clear();
int wbytes = send(socket, bytes, nbytes, 0);
if (wbytes == SOCKET_ERROR) {
@@ -173,7 +169,7 @@ static int socket_send(SOCKET socket, const char *bytes, int nbytes, drv::string
}
}
static int socket_recv(SOCKET socket, char *bytes, int nbytes, drv::string &err) {
static int socket_recv(SOCKET socket, char *bytes, int nbytes, std::string &err) {
err.clear();
int nrecv = recv(socket, bytes, nbytes, 0);
if (nrecv < 0) {
@@ -195,7 +191,7 @@ static int socket_close(SOCKET socket) {
return closesocket(socket);
}
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, drv::string &err) {
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, std::string &err) {
int status = WSAPoll(pollvec, pollcount, mstimeout);
if (status < 0) {
err = winsock_error_string(WSAGetLastError());
@@ -276,7 +272,7 @@ int main(int argc, char **argv)
allocate_buffers();
init_winsock();
OPENSSL_init_ssl(0, NULL);
initialize_engine();
SourceDB::register_lua_builtins();
driver_drive(argc, argv);
}

View File

@@ -8,10 +8,9 @@
#include "luastack.hpp"
#include "util.hpp"
namespace drv {
void split_host_port(std::string_view target, drv::string &host, drv::string &port) {
void split_host_port(std::string_view target, std::string &host, std::string &port) {
size_t lastcolon = target.rfind(':');
if (lastcolon == std::string_view::npos) {
host = ""; port = ""; return;
@@ -23,8 +22,8 @@ void split_host_port(std::string_view target, drv::string &host, drv::string &po
}
}
drv::vector<drv::string> parse_control_lst(std::string_view ctrl) {
drv::vector<drv::string> result;
std::vector<std::string> parse_control_lst(std::string_view ctrl) {
std::vector<std::string> result;
while (!ctrl.empty()) {
std::string_view line = util::sv_read_line(ctrl);
std::string_view trimmed = util::sv_trim(line);
@@ -40,7 +39,7 @@ drv::vector<drv::string> parse_control_lst(std::string_view ctrl) {
LuaDefine(unittests_driverutil, "", "some unit tests") {
// Test split_host_port
drv::string host, port;
std::string host, port;
drv::split_host_port("stanford.edu:80", host, port);
LuaAssertStrEq(L, host, "stanford.edu");
LuaAssertStrEq(L, port, "80");

View File

@@ -9,9 +9,9 @@
namespace drv {
void split_host_port(std::string_view target, drv::string &host, drv::string &port);
void split_host_port(std::string_view target, std::string &host, std::string &port);
drv::vector<drv::string> parse_control_lst(std::string_view ctrl);
std::vector<std::string> parse_control_lst(std::string_view ctrl);
}

View File

@@ -1,6 +1,5 @@
#include "wrap-string.hpp"
#include "eng-tests.hpp"
#include "drivenengine.hpp"
#include "streambuffer.hpp"
#include "world.hpp"
@@ -122,23 +121,9 @@ private:
}
};
UniqueDrivenEngine make_DriverStubTest() {
return UniqueDrivenEngine(new DriverStubTest);
}
UniqueDrivenEngine make_DriverWebServerTest() {
return UniqueDrivenEngine(new DriverWebServerTest);
}
UniqueDrivenEngine make_DriverDNSFailTest() {
return UniqueDrivenEngine(new DriverDNSFailTest);
}
UniqueDrivenEngine make_DriverPrintClockTest() {
return UniqueDrivenEngine(new DriverPrintClockTest);
}
UniqueDrivenEngine make_RunUnitTests() {
return UniqueDrivenEngine(new RunUnitTests);
}
DrivenEngineDefine("driverstubtest", DriverStubTest);
DrivenEngineDefine("driverwebservertest", DriverWebServerTest);
DrivenEngineDefine("driverdnsfailtest", DriverDNSFailTest);
DrivenEngineDefine("driverprintclocktest", DriverPrintClockTest);
DrivenEngineDefine("rununittests", RunUnitTests);

View File

@@ -1,14 +1,5 @@
#ifndef DRIVERTESTS_HPP
#define DRIVERTESTS_HPP
#include "drivenengine.hpp"
UniqueDrivenEngine make_DriverStubTest();
UniqueDrivenEngine make_DriverListenTest();
UniqueDrivenEngine make_DriverWebServerTest();
UniqueDrivenEngine make_DriverDNSFailTest();
UniqueDrivenEngine make_DriverPrintClockTest();
UniqueDrivenEngine make_RunUnitTests();
#endif // DRIVERTESTS_HPP

View File

@@ -1,10 +1,11 @@
#include "wrap-map.hpp"
#include "wrap-sstream.hpp"
#include "wrap-deque.hpp"
#include "wrap-ostream.hpp"
#include "idalloc.hpp"
#include <ostream>
static bool ranges_equal(const eng::deque<int64_t> &dq, int64_t a, int64_t b, int64_t c) {
if (dq.size() != 3) return false;

View File

@@ -68,13 +68,12 @@
#include "wrap-map.hpp"
#include "wrap-sstream.hpp"
#include "wrap-deque.hpp"
#include "wrap-ostream.hpp"
#include "luastack.hpp"
#include "streambuffer.hpp"
#include "debugcollector.hpp"
#include <cstdint>
#include <ostream>
class IdGlobalPool {
public:

View File

@@ -1,15 +1,15 @@
#include "wrap-memory.hpp"
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "drivenengine.hpp"
#include "lpxclient.hpp"
#include "world.hpp"
#include "luaconsole.hpp"
#include "invocation.hpp"
#include "util.hpp"
#include "printbuffer.hpp"
#include <memory>
class LpxClient : public DrivenEngine {
public:
using StringVec = LuaConsole::StringVec;
@@ -282,7 +282,5 @@ public:
}
};
UniqueDrivenEngine make_LpxClient() {
return UniqueDrivenEngine(new LpxClient);
}
DrivenEngineDefine("lpxclient", LpxClient);

View File

@@ -1,8 +1,4 @@
#ifndef LPXCLIENT_HPP
#define LPXCLIENT_HPP
#include "drivenengine.hpp"
UniqueDrivenEngine make_LpxClient();
#endif // LPXCLIENT_HPP

View File

@@ -1,21 +1,21 @@
#include "wrap-memory.hpp"
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "lpxserver.hpp"
#include "world.hpp"
#include "drivenengine.hpp"
#include "luaconsole.hpp"
#include "util.hpp"
#include "printbuffer.hpp"
class Client {
#include <memory>
class Client : public eng::heap {
public:
int64_t actor_id_;
SharedChannel channel_;
UniqueWorld sync_;
};
using UniqueClient = eng::unique_ptr<Client>;
using UniqueClient = std::unique_ptr<Client>;
using ClientVector = eng::vector<UniqueClient>;
class LpxServer : public DrivenEngine {
@@ -179,7 +179,5 @@ public:
}
};
UniqueDrivenEngine make_LpxServer() {
return UniqueDrivenEngine(new LpxServer);
}
DrivenEngineDefine("lpxserver", LpxServer);

View File

@@ -1,9 +1,5 @@
#ifndef LPXSERVER_HPP
#define LPXSERVER_HPP
#include "drivenengine.hpp"
UniqueDrivenEngine make_LpxServer();
#endif // LPXSERVER_HPP

View File

@@ -1,3 +1,5 @@
#include "two-mallocs.hpp"
#include "luastack.hpp"
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
@@ -8,7 +10,7 @@
#include <iostream>
LuaConsole::LuaConsole() {
lua_state_ = luaL_newstate();
lua_state_ = LuaStack::newstate(lalloc_dlmalloc);
clear_raw_input();
}

View File

@@ -10,7 +10,7 @@
LuaSnap::LuaSnap() {
state_ = luaL_newstate();
state_ = LuaStack::newstate(lalloc_dlmalloc);
LuaStack LS(state_);
// Create the persist table and the unpersist table.

View File

@@ -1,6 +1,7 @@
#include "luastack.hpp"
#include <iostream>
#include <cassert>
#include <cstdio>
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaNilMarker LuaNil;
@@ -11,20 +12,12 @@ LuaFunctionReg::LuaFunctionReg(const char *n, const char *a, const char *d, lua_
args_ = a;
docs_ = d;
func_ = f;
next_ = LuaFunctionRegistry;
LuaFunctionRegistry = this;
}
LuaFunctionReg::List LuaFunctionReg::all() {
LuaFunctionReg::List result;
for (LuaFunctionReg *r = LuaFunctionRegistry; r != 0; r = r->next_) {
result.push_back(r);
}
return result;
next_ = All;
All = this;
}
const LuaFunctionReg *LuaFunctionReg::lookup(lua_CFunction fn) {
for (const LuaFunctionReg *r = LuaFunctionRegistry; r != 0; r = r->next_) {
for (const LuaFunctionReg *r = All; r != 0; r = r->next_) {
if (r->func_ == fn) {
return r;
}
@@ -32,8 +25,21 @@ const LuaFunctionReg *LuaFunctionReg::lookup(lua_CFunction fn) {
return nullptr;
}
LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry;
LuaFunctionReg *LuaFunctionReg::All;
static int panicf(lua_State *L) {
const char *p = lua_tostring(L, -1);
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", p);
fflush(stderr);
exit(1);
}
lua_State *LuaStack::newstate (lua_Alloc allocf) {
lua_State *L = lua_newstate(allocf, NULL);
if (L) lua_atpanic(L, &panicf);
return L;
}
bool LuaStack::ckboolean(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TBOOLEAN);
@@ -154,7 +160,7 @@ lua_State *LuaStack::newthread(LuaSlot target) const {
return result;
}
bool LuaStack::validclassname(const eng::string &cname) {
bool LuaStack::validclassname(std::string_view cname) {
if (cname.empty()) return false;
if (cname == "_G") return false;
return true;
@@ -243,15 +249,7 @@ eng::string LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
}
}
eng::string LuaStack::getclass(LuaSlot tab, const char *name) const {
push_any_value(name);
LuaSpecial classname(lua_gettop(L_));
eng::string err = getclass(tab, classname);
lua_pop(L_, 1);
return err;
}
eng::string LuaStack::getclass(LuaSlot tab, const eng::string &name) const {
eng::string LuaStack::getclass(LuaSlot tab, std::string_view name) const {
push_any_value(name);
LuaSpecial classname(lua_gettop(L_));
eng::string err = getclass(tab, classname);
@@ -288,14 +286,7 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
LS.result();
}
void LuaStack::makeclass(LuaSlot tab, const char *name) const {
push_any_value(name);
LuaSpecial classname(lua_gettop(L_));
makeclass(tab, classname);
lua_pop(L_, 1);
}
void LuaStack::makeclass(LuaSlot tab, const eng::string &name) const {
void LuaStack::makeclass(LuaSlot tab, std::string_view name) const {
push_any_value(name);
LuaSpecial classname(lua_gettop(L_));
makeclass(tab, classname);

View File

@@ -155,7 +155,6 @@
#define LUASTACK_HPP
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
extern "C" {
#include "lua.h"
@@ -296,6 +295,7 @@ private:
void push_any_value(LuaNilMarker s) const { lua_pushnil(L_); }
void push_any_value(LuaSlot s) const { lua_pushvalue(L_, s); }
void push_any_value(const eng::string &s) const { lua_pushlstring(L_, s.c_str(), s.size()); }
void push_any_value(std::string_view s) const { lua_pushlstring(L_, s.data(), s.size()); }
void push_any_value(const char *s) const { lua_pushstring(L_, s); }
void push_any_value(float s) const { lua_pushnumber(L_, s); }
void push_any_value(double s) const { lua_pushnumber(L_, s); }
@@ -334,6 +334,8 @@ public:
int result();
public:
static lua_State *newstate (lua_Alloc allocf);
lua_State *state() const { return L_; }
int type(LuaSlot s) const { return lua_type(L_, s); }
@@ -379,7 +381,7 @@ public:
// Return true if the classname is legal.
bool validclassname(LuaSlot value) const;
static bool validclassname(const eng::string &cname);
static bool validclassname(std::string_view cname);
// Return the class name if x is a valid classtab.
// Otherwise, returns empty string. If nonempty, the
@@ -392,15 +394,13 @@ public:
// There are lots of error conditions, including such things
// as no such class, corrupted class, classname invalid, etc.
eng::string getclass(LuaSlot tab, LuaSlot name) const;
eng::string getclass(LuaSlot tab, const char *name) const;
eng::string getclass(LuaSlot tab, const eng::string &name) const;
eng::string getclass(LuaSlot tab, std::string_view name) const;
// Create a class, or look up an existing class.
// WARNING: this routine assert-fails if the parameter is not
// a valid classname. Check the classname before calling this!
void makeclass(LuaSlot tab, LuaSlot name) const;
void makeclass(LuaSlot tab, const char *name) const;
void makeclass(LuaSlot tab, const eng::string &name) const;
void makeclass(LuaSlot tab, std::string_view name) const;
// Get the ID of a tangible. It's a little weird to put this in
// this module.
@@ -478,19 +478,16 @@ private:
lua_CFunction func_;
LuaFunctionReg *next_;
static LuaFunctionReg *LuaFunctionRegistry;
public:
using List = eng::vector<LuaFunctionReg *>;
static LuaFunctionReg *All;
LuaFunctionReg(const char *name, const char *args, const char *docs, lua_CFunction f);
static List all();
static const LuaFunctionReg *lookup(lua_CFunction fn);
const char *get_name() const { return name_; }
const char *get_args() const { return args_; }
const char *get_docs() const { return docs_; }
lua_CFunction get_func() const { return func_; }
LuaFunctionReg *next() const { return next_; }
void set_func(lua_CFunction fn) { func_ = fn; }
};

View File

@@ -1,9 +1,9 @@
#include "wrap-algorithm.hpp"
#include "luastack.hpp"
#include "util.hpp"
#include "planemap.hpp"
#include <algorithm>
#include <cmath>

View File

@@ -1,6 +1,5 @@
#include "wrap-ostream.hpp"
#include <ostream>
#include "pprint.hpp"
#include "util.hpp"
#include "table.hpp"
@@ -8,7 +7,7 @@
#include <iostream>
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, eng::ostream *os) {
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, std::ostream *os) {
int tt = LS.type(val);
switch (tt) {
case LUA_TNIL:
@@ -103,7 +102,7 @@ struct Inspector {
bool indent;
int maxlen;
bool anyindent;
eng::ostream *stream;
std::ostream *stream;
};
static void tabify(Inspector &insp, int level) {
@@ -235,7 +234,7 @@ static void pprint_r(Inspector &insp, int level, LuaSlot root) {
LS.result();
}
void pprint(LuaStack &LS0, LuaSlot root, bool indent, eng::ostream *os) {
void pprint(LuaStack &LS0, LuaSlot root, bool indent, std::ostream *os) {
Inspector insp;
LuaStack LS(LS0.state(), insp.ids);
findtables(LS, root, insp.ids);

View File

@@ -19,10 +19,8 @@
#ifndef PPRINT_HPP
#define PPRINT_HPP
#include "wrap-ostream.hpp"
#include "luastack.hpp"
#include <ostream>
// Atomic print to a stream.
//
@@ -30,10 +28,10 @@
// it just prints "<table>". This routine is the heart of the lua
// primitives 'print' and 'tostring'.
//
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, eng::ostream *os);
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, std::ostream *os);
// Pretty print to a stream.
//
void pprint(LuaStack &LS, LuaSlot val, bool indent, eng::ostream *os);
void pprint(LuaStack &LS, LuaSlot val, bool indent, std::ostream *os);
#endif // PPRINT_HPP

View File

@@ -1,11 +1,11 @@
#include "wrap-algorithm.hpp"
#include "wrap-sstream.hpp"
#include "printbuffer.hpp"
#include <algorithm>
#include <cstdio>
struct PrintBufferCore {
struct PrintBufferCore : public eng::heap {
// The most recent lines printed.
eng::deque<eng::string> lines_;
@@ -176,7 +176,7 @@ void PrintBuffer::patch(StreamBuffer *sb, DebugCollector *dbc) {
}
}
bool PrintChanneler::channel(const PrintBuffer *printbuffer, eng::ostream &ostream) {
bool PrintChanneler::channel(const PrintBuffer *printbuffer, std::ostream &ostream) {
if (printbuffer == nullptr) return false;
if (printbuffer->first_line() > line_) {
line_ = printbuffer->first_line();

View File

@@ -79,18 +79,17 @@
#include "wrap-deque.hpp"
#include "wrap-string.hpp"
#include "wrap-memory.hpp"
#include "wrap-ostream.hpp"
#include <ostream>
#include "streambuffer.hpp"
#include "util.hpp"
#include "invocation.hpp"
#include "debugcollector.hpp"
#include <memory>
struct PrintBufferCore;
class PrintBuffer {
class PrintBuffer : public eng::heap {
private:
PrintBufferCore *core_;
@@ -139,7 +138,7 @@ public:
};
class PrintChanneler {
class PrintChanneler : public eng::heap {
private:
int64_t line_;
public:
@@ -151,7 +150,7 @@ public:
// Copy any new lines from the printbuffer to the stdostream.
// Update the current line number. Return true if the printbuffer
// contains any lines that have already been channeled.
bool channel(const PrintBuffer *pb, eng::ostream &ostream);
bool channel(const PrintBuffer *pb, std::ostream &ostream);
// Generate an invocation that removes unnecessary lines from the
// printbuffer.

View File

@@ -1,11 +1,12 @@
#include "wrap-sstream.hpp"
#include "wrap-ostream.hpp"
#include "sched.hpp"
#include "streambuffer.hpp"
#include "luastack.hpp"
#include <ostream>
bool SchedEntry::operator < (const SchedEntry &other) const {
if (clock_ < other.clock_) return true;
if (clock_ > other.clock_) return false;

View File

@@ -3,7 +3,6 @@
#include "wrap-vector.hpp"
#include "wrap-map.hpp"
#include "wrap-set.hpp"
#include "wrap-algorithm.hpp"
#include "wrap-sstream.hpp"
#include "util.hpp"
@@ -13,6 +12,7 @@
#include "source.hpp"
#include "luasnap.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
@@ -54,10 +54,10 @@ LuaDefine(classname, "classtable", "get the class name from a class table") {
return LS.result();
}
static void get_reg_name(const LuaFunctionReg *reg, eng::string &classname, eng::string &funcname) {
eng::string name = reg->get_name();
static void get_reg_name(const LuaFunctionReg *reg, std::string_view &classname, std::string_view &funcname) {
std::string_view name(reg->get_name());
size_t upos = name.find('_');
if (upos == eng::string::npos) {
if (upos == std::string_view::npos) {
funcname = name;
classname = "";
} else {
@@ -337,11 +337,10 @@ static void source_clear_globals(lua_State *L) {
static void source_load_cfunctions(lua_State *L) {
LuaVar classobj;
LuaStack LS(L, classobj);
auto regs = LuaFunctionReg::all();
for (const LuaFunctionReg *r : regs) {
for (auto r = LuaFunctionReg::All; r != nullptr; r=r->next()) {
lua_CFunction func = r->get_func();
eng::string classname;
eng::string funcname;
std::string_view classname;
std::string_view funcname;
get_reg_name(r, classname, funcname);
if (classname.empty()) {
LS.getglobaltable(classobj);
@@ -497,17 +496,17 @@ void SourceDB::deserialize_source(util::LuaSourceVec *sv, StreamBuffer *sb) {
}
}
// This function should not touch the dlmalloc heap.
void SourceDB::register_lua_builtins() {
lua_State *L = luaL_newstate();
lua_State *L = LuaStack::newstate(lalloc_malloc);
luaL_openlibs(L);
LuaVar globals,classtab,func;
LuaStack LS(L, globals, classtab, func);
LS.getglobaltable(globals);
auto regs = LuaFunctionReg::all();
for (LuaFunctionReg *reg : regs) {
for (auto reg = LuaFunctionReg::All; reg != nullptr; reg=reg->next()) {
if (reg->get_func() == nullptr) {
eng::string funcname;
eng::string classname;
std::string_view funcname;
std::string_view classname;
get_reg_name(reg, classname, funcname);
if (classname.empty()) {
LS.rawget(func, globals, funcname);
@@ -540,8 +539,8 @@ eng::string SourceDB::function_docs(const LuaStack &LS0, LuaSlot fn) {
if (reg == nullptr) {
return "";
}
eng::string classname;
eng::string funcname;
std::string_view classname;
std::string_view funcname;
get_reg_name(reg, classname, funcname);
eng::ostringstream oss;
util::StringVec docs = util::split_docstring(reg->get_docs());

View File

@@ -504,18 +504,18 @@ public:
}
};
class StreamBufferOStream : public eng::ostream {
class StreamBufferOStream : public std::ostream {
private:
StreamBufferWriter writer_;
public:
StreamBufferOStream(StreamBuffer *t) : eng::ostream(nullptr), writer_(t) {
StreamBufferOStream(StreamBuffer *t) : std::ostream(nullptr), writer_(t) {
rdbuf(&writer_);
}
virtual ~StreamBufferOStream() {
}
};
eng::ostream &StreamBuffer::ostream() {
std::ostream &StreamBuffer::ostream() {
if (ostream_ == nullptr) {
ostream_.reset(new StreamBufferOStream(this));
}

View File

@@ -214,14 +214,14 @@
#include "wrap-string.hpp"
#include "wrap-sstream.hpp"
#include "wrap-utility.hpp"
#include <utility>
#include <cstdint>
#include <cassert>
#include "luastack.hpp"
#include "util.hpp"
#include <cstdint>
#include <cassert>
class StreamException
{
public:
@@ -404,7 +404,7 @@ public:
void *lua_reader_ud(int64_t bytes);
// Get an ostream that writes into the StreamBuffer.
eng::ostream &ostream();
std::ostream &ostream();
private:
// Start and end of the allocated block.
@@ -429,7 +429,7 @@ private:
int64_t lua_reader_size_;
// The ostream. Only allocated on demand.
eng::unique_ptr<eng::ostream> ostream_;
std::unique_ptr<std::ostream> ostream_;
// Initialize with a new buffer.
void init(bool fixed, bool owned, char *buf, int64_t size);

View File

@@ -1,8 +1,6 @@
#include "wrap-vector.hpp"
#include "wrap-string.hpp"
#include "wrap-memory.hpp"
#include "wrap-algorithm.hpp"
#include "luastack.hpp"
#include "util.hpp"
@@ -10,11 +8,13 @@
#include "invocation.hpp"
#include "world.hpp"
#include "traceback.hpp"
#include "textgame.hpp"
#include "luaconsole.hpp"
#include "pprint.hpp"
#include "printbuffer.hpp"
#include "drivenengine.hpp"
#include <memory>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <csignal>
@@ -131,3 +131,4 @@ private:
UniqueDrivenEngine make_TextGame() {
return UniqueDrivenEngine(new TextGame);
}
static DrivenEngineReg reg_TextGame("textgame", make_TextGame);

View File

@@ -1,9 +1,53 @@
//
// Two Mallocs:
//
// The purpose of this file is to put all engine data structures into
// a private heap that isn't used by any other library. This helps
// achieve determinism when playing a replay log.
//
// For the engine's heap, we chose Doug Lea's Malloc (dlmalloc). It's
// a good general-purpose single-threaded malloc. It's probably not
// the fastest any more (it was, once), but it's still quite good. It
// may be possible to replace dlmalloc with another malloc, but
// dlmalloc is pretty easy to work with.
//
// In order to get all engine data structures into the dlmalloc heap,
// you need to jump through quite a few hoops:
//
// * When using STL classes, you need to use the 'eng' variant of those
// classes:
//
// use 'eng::string' instead of std::string (include "wrap-string.hpp")
// use 'eng::vector' instead of std::vector (include "wrap-vector.hpp")
// use 'eng::map' instead of std::map (include "wrap-map.hpp")
// etc.
//
// These eng classes allocate memory from dlmalloc instead of malloc.
//
// * All your classes must derive from eng::heap. This adds a custom
// operator new and operator delete to your class.
//
// * Use eng::make_shared and eng::make_unique instead of std::make_shared
// and std::make_unique.
//
// * Simple classes like std::pair, std::string_view, std::less,
// std::hash, and so forth don't allocate memory. Those classes
// are not wrapped. There is no eng::pair, eng:less, etc.
//
// * Be aware that most C++ streams use the malloc heap, and there's no
// way to change that. Fortunately, eng::ostringstream uses the
// dlmalloc heap.
//
// * Failing to jump through all these hoops won't break your code in
// any obvious way - you'll just have some of your data structures in
// the malloc heap instead of the dlmalloc heap. This can break
// determinism of replay.
//
#ifndef TWO_MALLOCS_HPP
#define TWO_MALLOCS_HPP
#include <cstddef>
#include <memory>
// dlmalloc is only used on linux.
extern "C" {
@@ -21,15 +65,35 @@ void* dlrealloc(void *p, size_t x) { return realloc(p,x); }
// Return the current state of the dlmalloc allocator as a 30-bit hash.
extern int dlmalloc_hash();
// EngAllocator: a class meant to be used as an STL Allocator.
// An allocator for lua states that uses dlmalloc and dlfree.
inline void *lalloc_dlmalloc (void *ud, void *ptr, size_t osize, size_t nsize) {
if (nsize == 0) {
dlfree(ptr);
return NULL;
} else {
return dlrealloc(ptr, nsize);
}
}
// An allocator for lua states that uses malloc and free.
inline void *lalloc_malloc (void *ud, void *ptr, size_t osize, size_t nsize) {
if (nsize == 0) {
free(ptr);
return NULL;
} else {
return realloc(ptr, nsize);
}
}
// eng:allocator: a class meant to be used as an STL Allocator.
// Causes objects to be allocated using dlmalloc and dlfree.
template <class T>
class EngAllocator
class dlmalloc_allocator
{
public:
using value_type = T;
EngAllocator() noexcept {}
template <class U> EngAllocator(EngAllocator<U> const&) noexcept {}
dlmalloc_allocator() noexcept {}
template <class U> dlmalloc_allocator(dlmalloc_allocator<U> const&) noexcept {}
value_type* allocate(std::size_t n)
{
@@ -43,49 +107,51 @@ public:
};
template <class T, class U>
bool operator==(EngAllocator<T> const&, EngAllocator<U> const&) noexcept
bool operator==(const dlmalloc_allocator<T> &, const dlmalloc_allocator<U> &) noexcept
{
return true;
}
template <class T, class U>
bool operator!=(EngAllocator<T> const&, EngAllocator<U> const&) noexcept
bool operator!=(const dlmalloc_allocator<T> &, const dlmalloc_allocator<U> &) noexcept
{
return false;
}
// DrvAllocator: a class meant to be used as an STL Allocator.
// Causes objects to be allocated using malloc and free.
template <class T>
class DrvAllocator
{
public:
using value_type = T;
DrvAllocator() noexcept {}
template <class U> DrvAllocator(DrvAllocator<U> const&) noexcept {}
value_type* allocate(std::size_t n)
{
return static_cast<value_type*>(dlmalloc(n*sizeof(value_type)));
}
void deallocate(value_type* p, std::size_t) noexcept
{
dlfree(p);
}
};
template <class T, class U>
bool operator==(DrvAllocator<T> const&, DrvAllocator<U> const&) noexcept
{
return true;
// Another name for dlmalloc_allocator
namespace eng {
template <class T>
using allocator = dlmalloc_allocator<T>;
}
template <class T, class U>
bool operator!=(DrvAllocator<T> const&, DrvAllocator<U> const&) noexcept
{
return false;
}
// eng::heap a class meant to be used as a base class.
// Causes 'new' and 'delete' for this class to use dlmalloc and dlfree.
namespace eng {
class heap {
public:
void *operator new(size_t size)
{
return dlmalloc(size);
}
void operator delete(void *p, size_t size)
{
return dlfree(p);
}
};
} // namespace
// eng::make_shared allocates shared objects in the dlmalloc heap.
namespace eng {
template<class T, class... Args>
inline std::shared_ptr<T> make_shared(Args&&... args) {
static eng::allocator<T> alloc;
return std::allocate_shared<T>(alloc, args...);
}
template<class T, class... Args>
inline std::unique_ptr<T> make_unique(Args&&... args) {
return std::make_unique<T>(args...);
}
} // namespace eng
#endif // TWO_MALLOCS_HPP

View File

@@ -1,6 +1,7 @@
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "wrap-algorithm.hpp"
#include <algorithm>
#include "util.hpp"
@@ -8,7 +9,6 @@
#include <sys/stat.h>
#include <iomanip>
#include <cassert>
#include <fstream>
#include <cstdlib>
#include <cmath>
@@ -41,7 +41,7 @@ bool is_identifier(const eng::string &str) {
return true;
}
void quote_string(const eng::string &s, eng::ostream *os) {
void quote_string(const eng::string &s, std::ostream *os) {
bool anysq = false;
bool anydq = false;
for (char c : s) {
@@ -363,22 +363,22 @@ eng::string XYZ::debug_string() const {
} // namespace util
eng::ostream &operator<<(eng::ostream &oss, const util::hex64 &v) {
std::ostream &operator<<(std::ostream &oss, const util::hex64 &v) {
oss << "0x" << std::setw(16) << std::setfill('0') << std::hex;
return oss;
}
eng::ostream &operator<<(eng::ostream &oss, const util::hex32 &v) {
std::ostream &operator<<(std::ostream &oss, const util::hex32 &v) {
oss << "0x" << std::setw(8) << std::setfill('0') << std::hex;
return oss;
}
eng::ostream &operator<<(eng::ostream &oss, const util::hex16 &v) {
std::ostream &operator<<(std::ostream &oss, const util::hex16 &v) {
oss << "0x" << std::setw(4) << std::setfill('0') << std::hex;
return oss;
}
eng::ostream &operator<<(eng::ostream &oss, const util::hex8 &v) {
std::ostream &operator<<(std::ostream &oss, const util::hex8 &v) {
oss << "0x" << std::setw(2) << std::setfill('0') << std::hex;
return oss;
}

View File

@@ -4,12 +4,12 @@
#include "wrap-string.hpp"
#include "wrap-set.hpp"
#include "wrap-map.hpp"
#include "wrap-algorithm.hpp"
#include "wrap-vector.hpp"
#include "wrap-sstream.hpp"
#include "wrap-ostream.hpp"
#include "wrap-memory.hpp"
#include "wrap-utility.hpp"
#include <ostream>
#include <memory>
#include <utility>
#include <algorithm>
#include <string_view>
#include "luastack.hpp"
@@ -32,11 +32,11 @@ enum MessageType {
};
using StringVec = eng::vector<eng::string>;
using StringPair = eng::pair<eng::string, eng::string>;
using StringPair = std::pair<eng::string, eng::string>;
using StringSet = eng::set<eng::string>;
using LuaSourceVec = eng::vector<StringPair>;
using LuaSourcePtr = eng::unique_ptr<LuaSourceVec>;
using HashValue = eng::pair<uint64_t, uint64_t>;
using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
using HashValue = std::pair<uint64_t, uint64_t>;
using IdVector = eng::vector<int64_t>;
// Return seconds elapsed, for profiling purposes.
@@ -46,7 +46,7 @@ double profiling_clock();
bool is_identifier(const eng::string &str);
// Output a string to a stream using Lua string escaping and quoting.
void quote_string(const eng::string &str, eng::ostream *os);
void quote_string(const eng::string &str, std::ostream *os);
// ID vector quick create.
IdVector id_vector_create(int64_t id1=-1, int64_t id2=-1, int64_t id3=-1, int64_t id4=-1);
@@ -128,8 +128,8 @@ bool is_lua_comment(const eng::string &line);
// Remove nullptrs from a vector of unique pointers.
template<class T>
void remove_nullptrs(eng::vector<eng::unique_ptr<T>> &vec) {
eng::unique_ptr<T> nullp;
void remove_nullptrs(eng::vector<std::unique_ptr<T>> &vec) {
std::unique_ptr<T> nullp;
auto iter = std::remove(vec.begin(), vec.end(), nullp);
vec.erase(iter, vec.end());
}
@@ -144,7 +144,7 @@ struct XYZ {
eng::string debug_string() const;
};
// These are formatting directives that can be sent to a eng::ostream.
// These are formatting directives that can be sent to a std::ostream.
class hex64 {};
class hex32 {};
class hex16 {};
@@ -158,9 +158,9 @@ public:
} // namespace util
eng::ostream &operator<<(eng::ostream &oss, const util::hex64 &v);
eng::ostream &operator<<(eng::ostream &oss, const util::hex32 &v);
eng::ostream &operator<<(eng::ostream &oss, const util::hex16 &v);
eng::ostream &operator<<(eng::ostream &oss, const util::hex8 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex64 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex32 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex16 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex8 &v);
#endif // UTIL_HPP

View File

@@ -314,7 +314,7 @@ LuaDefine(tangible_nopredict, "",
LuaDefine(pprint, "obj1,obj2,...",
"|Pretty-print object or objects.") {
World *w = World::fetch_global_pointer(L);
eng::ostream *ostream = w->lthread_print_stream();
std::ostream *ostream = w->lthread_print_stream();
LuaStack LS(L);
for (int i = 1; i <= lua_gettop(L); i++) {
LuaSpecial root(i);
@@ -327,7 +327,7 @@ LuaDefine(pprint, "obj1,obj2,...",
LuaDefine(print, "obj1,obj2,...",
"|Print object or objects.") {
World *w = World::fetch_global_pointer(L);
eng::ostream *ostream = w->lthread_print_stream();
std::ostream *ostream = w->lthread_print_stream();
LuaStack LS(L);
int n = lua_gettop(L);
for (int i = 1; i <= n; i++) {
@@ -342,7 +342,7 @@ LuaDefine(print, "obj1,obj2,...",
LuaDefine(doc, "function",
"|Print documentation for specified function.") {
World *w = World::fetch_global_pointer(L);
eng::ostream *ostream = w->lthread_print_stream();
std::ostream *ostream = w->lthread_print_stream();
LuaArg func;
LuaStack LS(L, func);
eng::string doc = SourceDB::function_docs(LS, func);

View File

@@ -294,7 +294,7 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
// If there's an error message, print it.
// Otherwise, pretty-print the results.
eng::ostream *ostream = lthread_print_stream();
std::ostream *ostream = lthread_print_stream();
if (status == LUA_OK) {
for (int i = top + 1; i <= lua_gettop(L); i++) {
LuaSpecial root(i);
@@ -683,7 +683,7 @@ void World::run_scheduled_threads() {
lua_State *CO = LS.ckthread(thread);
open_lthread_state(LS.ckinteger(actorid), sched.place_id(), LS.ckboolean(useppool), true);
int status = lua_resume(CO, nullptr, LS.ckint(nargs));
eng::ostream *ostream = lthread_print_stream();
std::ostream *ostream = lthread_print_stream();
// Three possible outcomes: finished, yielded, or errored.
if (!util::world_type_authoritative(world_type_)) {
@@ -786,7 +786,7 @@ void World::close_lthread_state() {
clear_lthread_state();
}
eng::ostream *World::lthread_print_stream() const {
std::ostream *World::lthread_print_stream() const {
if (lthread_prints_ != nullptr) {
return lthread_prints_.get();
} else {

View File

@@ -101,7 +101,7 @@ eng::string World::paired_tables_debug_string(lua_State *master) const {
LuaVar mntmap, sntmap, mtab, stab, mtid, stid;
LuaStack MLS(master, mntmap, mtab, mtid);
LuaStack SLS(synch, sntmap, stab, stid);
eng::vector<eng::pair<eng::string, eng::string>> result;
eng::vector<std::pair<eng::string, eng::string>> result;
eng::ostringstream oss;
// Fetch the numbered tables map.

View File

@@ -3,11 +3,12 @@
#define WORLD_HPP
#include "wrap-set.hpp"
#include "wrap-utility.hpp"
#include "wrap-memory.hpp"
#include "wrap-unordered-map.hpp"
#include "wrap-map.hpp"
#include <memory>
#include <utility>
#include "luastack.hpp"
#include "planemap.hpp"
#include "idalloc.hpp"
@@ -87,7 +88,7 @@ public:
void configure_id_pool_for_actor() { id_player_pool_.set_fifo_capacity(3); id_player_pool_.refill(); }
};
using UniqueTangible = eng::unique_ptr<Tangible>;
using UniqueTangible = std::unique_ptr<Tangible>;
class World {
public:
@@ -247,7 +248,7 @@ public:
void open_lthread_state(int64_t actor_id, int64_t place_id, bool ppool, bool prints);
void close_lthread_state();
eng::ostream *lthread_print_stream() const;
std::ostream *lthread_print_stream() const;
// Allocate a single ID.
//
@@ -489,7 +490,7 @@ private:
int64_t lthread_actor_id_;
int64_t lthread_place_id_;
int64_t lthread_use_ppool_;
eng::unique_ptr<eng::ostringstream> lthread_prints_;
std::unique_ptr<eng::ostringstream> lthread_prints_;
friend class Tangible;
friend int lfn_tangible_animate(lua_State *L);
@@ -502,7 +503,7 @@ private:
friend int lfn_tangible_scan(lua_State *L);
};
using UniqueWorld = eng::unique_ptr<World>;
using UniqueWorld = std::unique_ptr<World>;
#endif // WORLD_HPP