Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core.cdata: support for common SHM object types #1082

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,73 @@ Deletes/unmaps a shared memory *frame*. The *frame* directory is unlinked if
*frame* was created by `shm.create_frame`.


### Cdata (core.cdata)

Shared memory siblings of common Cdata types. The following types are
registered with `core.shm`:

- `int8_t` `int16_t` `int32_t` `int64_t`
- `uint8_t` `uint16_t` `uint32_t` `uint64_t`
- `float` `double`
- `bool`

The above types can be read and written using `cdata.read` and `cdata.set`.

Additionally, types for fixed length strings for a handful of lengths are
registered:

- `string64`, `string512`, `string8192`

These can be read and written using `ffi.string` and `ffi.copy`. For safety
reasons it is advised to use the provided copy functions `cdata.string64.copy`,
`cdata.string512.copy`, and `cdata.string8192.copy` for writing these types.

**Example:**

```
myframe = shm.create_frame("myframe",
{pi = {cdata.double, math.pi},
info = {cdata.string512, "placeholder"}})
cdata.read(myframe.pi) * 2 => 6.2831853071796
ffi.string(myframe.info) => "placeholder"
cdata.string512.copy(myframe.info, "Mathematical constant π")
```

— Function **cdata.create** *name*, [*initval*]

Creates and returns a shared memory cdata object by *name*. The type of the
created cdata object is derived from the type suffix in *name*. If *initval* is
supplied the object is initialized to *initval*.

**Example:**

```
cdata.create("pi.double", math.pi)
```

— Function **cdata.open** *name*

Opens and returns the shared memory cdata object by *name* for reading. The
type of the created cdata object is derived from the type suffix in *name*.

— Function **cdata.set** *cdata* *value*

Sets the value of shared memory *cdata* object to *value*.

— Function **cdata.read** *cdata*

Returns the value of shared memory *cdata* object.

— Function **cdata.string64.copy** *cdata*, *string*

— Function **cdata.string512.copy** *cdata*, *string*

— Function **cdata.string8192.copy** *cdata*, *string*

Copies *string* into shared memory *cdata* object. Throws an error if *string*
does not fit the respective string type.


### Counter (core.counter)

Double-buffered shared memory counters. Counters are 64-bit unsigned values.
Expand Down
96 changes: 96 additions & 0 deletions src/core/cdata.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- Use of this source code is governed by the Apache 2.0 license; see COPYING.

-- cdata.lua - Store different kinds of Lua values in SHM objects using ctypes.
-- Semantics / conversions are as described here:
-- http://luajit.org/ext_ffi_semantics.html
--
-- Example:
--
-- local cdata = require("core.cdata")
-- local n = cdata.create("num.double", math.pi)
-- local n2 = cdata.open("num.double")
-- cdata.set(n, 2*math.pi)
-- cdata.read(n2) => 6.2831853071796
--
-- local shm = require("core.shm")
-- local frame = shm.create_frame("myframe", {pi = {cdata.double, math.pi}})

module(..., package.seeall)

local shm = require("core.shm")
local ffi = require("ffi")
local cdata = getfenv()


for _, ctype in ipairs({ 'int8_t', 'uint8_t', 'int16_t', 'uint16_t',
'int32_t', 'uint32_t', 'int64_t', 'uint64_t',
'float', 'double', 'bool' }) do
cdata[ctype] = {}
cdata[ctype].type = shm.register(ctype, cdata[ctype])
cdata[ctype].create = function (name, initval)
local cd = shm.create(name, ctype.."[1]")
if initval then cd[0] = initval end
return cd
end
cdata[ctype].open = function (name)
return shm.open(name, ctype.."[1]", 'readonly')
end
end

function set (cd, value) cd[0] = value end
function read (cd) return cd[0] end


for _, length in ipairs({ 64, 512, 8192 }) do
local type = "string"..length
cdata[type] = {}
cdata[type].type = shm.register(type, cdata[type])
cdata[type].copy = function (cd, str)
assert(not str or #str < length, "argument does not fit string"..length)
ffi.copy(cd, str)
end
cdata[type].create = function (name, initval)
local cd = shm.create(name, "uint8_t["..length.."]")
if initval then cdata[type].copy(cd, initval) end
return cd
end
cdata[type].open = function (name)
return shm.open(name, "uint8_t["..length.."]")
end
end


function create (name, initval)
local _, type = name:match("(.*)[.](.*)$")
return assert(cdata[type], "Unsupported type: "..type).create(name, initval)
end

function open (name)
local _, type = name:match("(.*)[.](.*)$")
return assert(cdata[type], "Unsupported type: "..type).open(name)
end


function selftest ()
local d = create("d.double")
set(d, math.pi)
local d2 = open("d.double")
assert(math.pi == read(d2))
local i64 = 10000000ULL
local i = create("i.int64_t", i64)
assert(read(i) == i64)
local b = create("b.bool", true)
assert(read(b) == true)
set(b, false)
assert(read(b) == false)
local f = shm.create_frame("test", {pi = {cdata.double, math.pi}})
local f2 = shm.open_frame("test")
assert(read(f2.pi) == math.pi)
local s = create("str.string64", "foo")
assert(ffi.string(s) == "foo")
string64.copy(s, "foobar")
local _, err = pcall(string64.copy, s, ("foobar"):rep(100))
assert(err ~= nil)
local s2 = open("str.string64")
assert(ffi.string(s2) == "foobar")
end