Lots of work on removing malloc from driver
This commit is contained in:
@@ -1,5 +1,32 @@
|
||||
#include "drivenengine.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
static std::vector<std::pair<std::string, DrivenEngineMaker>> makers;
|
||||
|
||||
void DrivenEngine::register_maker(const char *kind, DrivenEngineMaker maker) {
|
||||
std::string skind(kind);
|
||||
makers.push_back(std::make_pair(skind, maker));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
UniqueDrivenEngine DrivenEngine::make(const char *kind) {
|
||||
for (const auto &mpair : makers) {
|
||||
if (strcmp(mpair.first.c_str(), kind) == 0) {
|
||||
return mpair.second();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Channel::Channel(DrivenEngine *de, int chid, int port, const std::string &target, bool stop) {
|
||||
chid_ = chid;
|
||||
@@ -127,14 +154,14 @@ int DrivenEngine::find_unused_chid() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Channel *DrivenEngine::get_chid(int chid) {
|
||||
Channel *DrivenEngine::get_chid(int chid) const {
|
||||
assert(unsigned(chid) < MAX_CHAN);
|
||||
assert(channels_[chid].get() != nullptr);
|
||||
return channels_[chid].get();
|
||||
}
|
||||
|
||||
void DrivenEngine::listen_port(int port) {
|
||||
listen_ports_.insert(port);
|
||||
listen_ports_.push_back(port);
|
||||
}
|
||||
|
||||
double DrivenEngine::get_clock() {
|
||||
@@ -143,7 +170,7 @@ double DrivenEngine::get_clock() {
|
||||
|
||||
SharedChannel DrivenEngine::new_outgoing_channel(const std::string &target) {
|
||||
int chid = find_unused_chid();
|
||||
new_outgoing_.insert(chid);
|
||||
new_outgoing_.push_back(chid);
|
||||
SharedChannel result = std::make_shared<Channel>(this, chid, 0, target, stop_driver_);
|
||||
channels_[chid] = result;
|
||||
return result;
|
||||
@@ -180,30 +207,33 @@ void DrivenEngine::stop_driver() {
|
||||
}
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_get_listen_ports(std::set<int> &ports) {
|
||||
ports = listen_ports_;
|
||||
const std::vector<int> &DrivenEngine::drv_get_listen_ports() const {
|
||||
return listen_ports_;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_get_new_outgoing(std::set<int> &channels) {
|
||||
channels = std::move(new_outgoing_);
|
||||
const std::vector<int> &DrivenEngine::drv_get_new_outgoing() const {
|
||||
return new_outgoing_;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_clear_new_outgoing() {
|
||||
new_outgoing_.clear();
|
||||
}
|
||||
|
||||
const std::string &DrivenEngine::drv_get_target(int chid) {
|
||||
const std::string &DrivenEngine::drv_get_target(int chid) const {
|
||||
return get_chid(chid)->target_;
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_outgoing_empty(int chid) {
|
||||
bool DrivenEngine::drv_outgoing_empty(int chid) const {
|
||||
int nbytes; const char *bytes;
|
||||
drv_peek_outgoing(chid, &nbytes, &bytes);
|
||||
return (nbytes == 0);
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_get_channel_released(int chid) {
|
||||
bool DrivenEngine::drv_get_channel_released(int chid) const {
|
||||
return channels_[chid].use_count() == 1;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_peek_outgoing(int chid, int *nbytes, const char **bytes) {
|
||||
void DrivenEngine::drv_peek_outgoing(int chid, int *nbytes, const char **bytes) const {
|
||||
return get_chid(chid)->peek_outgoing(nbytes, bytes);
|
||||
}
|
||||
|
||||
@@ -222,7 +252,7 @@ void DrivenEngine::drv_recv_incoming(int chid, int nbytes, const char *bytes) {
|
||||
}
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_notify_close(int chid, const std::string &err) {
|
||||
void DrivenEngine::drv_notify_close(int chid, std::string_view err) {
|
||||
Channel *ch = get_chid(chid);
|
||||
ch->closed_ = true;
|
||||
ch->error_ = err;
|
||||
@@ -236,8 +266,20 @@ int DrivenEngine::drv_notify_accept(int port) {
|
||||
return chid;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_set_lua_source(util::LuaSourcePtr source) {
|
||||
lua_source_ = std::move(source);
|
||||
void DrivenEngine::drv_clear_lua_source() {
|
||||
lua_source_.reset();
|
||||
rescan_lua_source_ = false;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_add_lua_source(const char *fn, const char *data) {
|
||||
if (lua_source_ == nullptr) {
|
||||
lua_source_.reset(new util::LuaSourceVec);
|
||||
}
|
||||
lua_source_->emplace_back(std::string(fn), std::string(data));
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_set_lua_source(util::LuaSourcePtr src) {
|
||||
lua_source_ = std::move(src);
|
||||
rescan_lua_source_ = false;
|
||||
}
|
||||
|
||||
@@ -250,11 +292,11 @@ void DrivenEngine::drv_invoke_event_update(double clock) {
|
||||
event_update();
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_get_rescan_lua_source() {
|
||||
bool DrivenEngine::drv_get_rescan_lua_source() const {
|
||||
return rescan_lua_source_;
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_get_stop_driver() {
|
||||
bool DrivenEngine::drv_get_stop_driver() const {
|
||||
return stop_driver_;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user