From b86a552a5b4ca4bf07f33bf95dc6298232031c12 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 7 Jul 2017 18:26:49 +0100 Subject: [PATCH] Add support for mmap(2) --- libs/core/mmap.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++ libs/core/pxt.h | 14 ++++++++ libs/core/pxt.json | 1 + libs/core/shims.d.ts | 25 +++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 libs/core/mmap.cpp diff --git a/libs/core/mmap.cpp b/libs/core/mmap.cpp new file mode 100644 index 00000000..08f9a315 --- /dev/null +++ b/libs/core/mmap.cpp @@ -0,0 +1,76 @@ +#include "pxt.h" + +#include +#include +#include +#include +#include + +namespace pxt { +PXT_VTABLE_CTOR(MMap) { + length = 0; + fd = -1; + data = 0; +} + +void MMap::print() { + DMESG("MMap %p r=%d len=%d fd=%d data=%p", this, refcnt, length, fd, data); +} + +void MMap::destroy() { + munmap(data, length); + close(fd); +} +} + +namespace control { + +/** Create new file mapping in memory */ +//% +MMap *mmap(String filename, int size, int offset) { + int fd = open(filename->data, O_RDWR, 0); + if (fd < 0) + return 0; + + void *data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); + if (data == MAP_FAILED) { + close(fd); + return 0; + } + + auto r = new MMap(); + r->fd = fd; + r->length = size; + r->data = (uint8_t*)data; + return r; +} +} + +namespace MMapMethods { + +/** + * Write a number in specified format in the buffer. + */ +//% +void setNumber(MMap *buf, NumberFormat format, int offset, TNumber value) { + if (offset < 0) + return; + setNumberCore(buf->data + offset, buf->length - offset, format, value); +} + +/** + * Read a number in specified format from the buffer. + */ +//% +TNumber getNumber(MMap *buf, NumberFormat format, int offset) { + if (offset < 0) + return fromInt(0); + return getNumberCore(buf->data + offset, buf->length - offset, format); +} + +/** Returns the length of a Buffer object. */ +//% property +int length(MMap *s) { + return s->length; +} +} \ No newline at end of file diff --git a/libs/core/pxt.h b/libs/core/pxt.h index 74560f0b..6419d527 100644 --- a/libs/core/pxt.h +++ b/libs/core/pxt.h @@ -14,6 +14,20 @@ class Button; typedef Button *Button_; extern "C" void target_init(); + + +class MMap : public RefObject { + public: + int length; + int fd; + uint8_t *data; + + MMap(); + void destroy(); + void print(); +}; + + } #define DEVICE_EVT_ANY 0 diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 44c36cdf..d1851513 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -7,6 +7,7 @@ "pxt.h", "pxtcore.h", "linux.cpp", + "mmap.cpp", "control.cpp", "buttons.cpp", "screen.cpp", diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index e65e473f..fd82c46d 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -1,4 +1,29 @@ // Auto-generated. Do not edit. +declare namespace control { + + /** Create new file mapping in memory */ + //% shim=control::mmap + function mmap(filename: string, size: int32, offset: int32): MMap; +} + + +declare interface MMap { + /** + * Write a number in specified format in the buffer. + */ + //% shim=MMapMethods::setNumber + setNumber(format: NumberFormat, offset: int32, value: number): void; + + /** + * Read a number in specified format from the buffer. + */ + //% shim=MMapMethods::getNumber + getNumber(format: NumberFormat, offset: int32): number; + + /** Returns the length of a Buffer object. */ + //% property shim=MMapMethods::length + length: int32; +} declare namespace control { /**