Skip to content

Commit

Permalink
feat: add possibility to compile libcartesi without mmap()
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 8, 2023
1 parent b5e2019 commit fc7f74f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ endif
CXXFLAGS+=$(MYCXXFLAGS)
CFLAGS+=$(MYCFLAGS)
LDFLAGS+=$(MYLDFLAGS)
SOLDFLAGS+=$(MYSOLDFLAGS)

all: luacartesi libcartesi.a grpc hash c-api jsonrpc-remote-cartesi-machine

Expand Down
2 changes: 2 additions & 0 deletions src/json-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <string>
#include <type_traits>

// Disable JSON filesystem support because it is not supported in some targets
#define JSON_HAS_FILESYSTEM 0
#include <json.hpp>

#include "base64.h"
Expand Down
1 change: 0 additions & 1 deletion src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <sys/wait.h>
#include <unistd.h>

#include <json.hpp>
#include <mongoose.h>

#include "base64.h"
Expand Down
1 change: 0 additions & 1 deletion src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <cstdint>
#include <string>

#include <json.hpp>
#include <mongoose.h>

#include "jsonrpc-mg-mgr.h"
Expand Down
8 changes: 7 additions & 1 deletion src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sys/stat.h>
#include <thread>

#include "clint-factory.h"
Expand All @@ -46,6 +45,13 @@
#include "uarch-step.h"
#include "unique-c-ptr.h"

#ifdef _WIN32
#include <direct.h> // mkdir
#define mkdir(a, b) _mkdir(a)
#else
#include <sys/stat.h> // mkdir
#endif

/// \file
/// \brief Cartesi machine implementation

Expand Down
30 changes: 25 additions & 5 deletions src/pma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#include <fcntl.h> // open
#include <sys/mman.h> // mmap, munmap
#include <sys/stat.h> // fstat
#include <unistd.h> // close

#include <cerrno>
#include <cstring>
#include <string>
Expand All @@ -27,14 +22,27 @@
#include "pma.h"
#include "unique-c-ptr.h"

#if (defined(_WIN32) || defined(__wasi__)) && !defined(NO_MMAP)
#define NO_MMAP
#endif

#ifndef NO_MMAP
#include <fcntl.h> // open
#include <sys/mman.h> // mmap, munmap
#include <sys/stat.h> // fstat
#include <unistd.h> // close
#endif

namespace cartesi {

using namespace std::string_literals;

void pma_memory::release(void) {
if (m_backing_file >= 0) {
#ifndef NO_MMAP
munmap(m_host_memory, m_length);
close(m_backing_file);
#endif
m_backing_file = -1;
} else {
std::free(m_host_memory); // NOLINT(cppcoreguidelines-no-malloc)
Expand Down Expand Up @@ -111,6 +119,7 @@ pma_memory::pma_memory(const std::string &description, uint64_t length, const st
}
}

#ifndef NO_MMAP
pma_memory::pma_memory(const std::string &description, uint64_t length, const std::string &path, const mmapd &m) :
m_length{length},
m_host_memory{nullptr},
Expand Down Expand Up @@ -158,6 +167,17 @@ pma_memory::pma_memory(const std::string &description, uint64_t length, const st
m_host_memory = host_memory;
m_backing_file = backing_file;
}
#else
pma_memory::pma_memory(const std::string &description, uint64_t length, const std::string &path, const mmapd &m) :
pma_memory(description, length, path, callocd{}) {
if (path.empty()) {
throw std::runtime_error{"image file must be specified for "s + description};
}
if (m.shared) {
throw std::runtime_error{"shared image is unsupported, when initialization "s + description};
}
}
#endif

pma_memory &pma_memory::operator=(pma_memory &&other) noexcept {
release();
Expand Down
3 changes: 1 addition & 2 deletions src/test-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
#include <tuple>
#include <vector>

#include <json.hpp>

#include "grpc-machine-c-api.h"
#include "json-util.h"
#include "machine-c-api.h"
#include "riscv-constants.h"
#include "test-utils.h"
Expand Down
31 changes: 25 additions & 6 deletions src/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@
//

#include <array>
#include <csignal>
#include <cstdint>
#include <fcntl.h>
#include <iostream>
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>

#if (defined(_WIN32)) && !defined(NO_STDIN_READ)
#define NO_STDIN_READ
#endif

#ifndef NO_STDIN_READ
#include <sys/select.h> // select
#endif

#include <unistd.h> // write/read/close

#if (defined(_WIN32) || defined(__wasi__)) && !defined(NO_TERMIOS)
#define NO_TERMIOS
#endif

#ifndef NO_TERMIOS
#include <termios.h>
#include <fcntl.h> // open
#include <termios.h> // tcgetattr/tcsetattr
#endif

#include "tty.h"
Expand Down Expand Up @@ -74,6 +84,7 @@ static int get_ttyfd(void) {
#endif

static bool try_read_chars_from_stdin(uint64_t wait, char *data, size_t max_len, ssize_t *actual_len) {
#ifndef NO_STDIN_READ
const int fd_max{0};
fd_set rfds{};
timeval tv{};
Expand All @@ -90,6 +101,14 @@ static bool try_read_chars_from_stdin(uint64_t wait, char *data, size_t max_len,
return true;
}
return false;
#else
// No support for reading console inputs for Windows yet
(void) wait;
(void) data;
(void) max_len;
(void) actual_len;
return false;
#endif
}

/// Returns pointer to the global TTY state
Expand Down

0 comments on commit fc7f74f

Please sign in to comment.