Skip to content

Commit

Permalink
lib.yang.list: use lib.yang.ctype
Browse files Browse the repository at this point in the history
This ensures that structs inherit lib.yang.ctype's structural typing
properties.

Note: this removes a bunch of ((packed)) __attribute__s.
Have to be careful about hashing.
  • Loading branch information
eugeneia committed Nov 9, 2022
1 parent 4bf08f8 commit bb9eca2
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/lib/yang/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- COPYING.
module(..., package.seeall)

local typeof = require("lib.yang.ctype").typeof
local murmur = require("lib.hash.murmur")
local lib = require("core.lib")
local ffi = require("ffi")
Expand Down Expand Up @@ -342,30 +343,23 @@ function List:type_info (type)
return assert(self.type_map[type], "Unsupported type: "..type)
end

List.type_cache = {}

function List:cached_type (t)
if not self.type_cache[t] then
self.type_cache[t] = ffi.typeof(t)
end
return self.type_cache[t]
end

List.node_t = List:cached_type [[
List.node_t = ffi.typeof [[
struct {
uint16_t occupied, leaf;
uint16_t occupied;
uint16_t leaf;
uint32_t parent;
uint32_t children[16];
}
]]

List.list_ts = [[
struct {
uint32_t prev, next;
uint32_t prev;
uint32_t next;
}
]]

List.string_t = List:cached_type [[
List.string_t = ffi.typeof [[
struct {
uint16_t len;
uint8_t str[1];
Expand All @@ -376,7 +370,15 @@ List.optional_ts = [[
struct {
struct { %s %s; } value;
bool present;
} __attribute__((packed))
}
]]

List.leaf_ts = [[
struct {
%s list;
%s keys;
%s members;
}
]]

function List:_new (keys, members)
Expand All @@ -388,8 +390,8 @@ function List:_new (keys, members)
local members_ts = self:build_type(members)
self.keys = keys
self.members = members
self.keys_t = self:cached_type(keys_ts)
self.leaf_t = self:cached_type(self:build_leaf_type(keys_ts, members_ts))
self.keys_t = typeof(keys_ts)
self.leaf_t = typeof(self:build_leaf_type(keys_ts, members_ts))
self.hashin = self.keys_t()
return self
end
Expand Down Expand Up @@ -445,13 +447,12 @@ function List:build_type (fields)
end
t = t..("%s %s; "):format(ct, name)
end
t = t.."} __attribute__((packed))"
t = t.."}"
return t
end

function List:build_leaf_type (keys_ts, members_ts)
return ("struct { %s list; %s keys; %s members; } __attribute__((packed))")
:format(self.list_ts, keys_ts, members_ts)
return self.leaf_ts:format(self.list_ts, keys_ts, members_ts)
end

function List:heap_cast (t, o)
Expand Down Expand Up @@ -1144,9 +1145,10 @@ function selftest_list ()
assert(l.lvalues.o[1].bar == false)

-- Test struct
local ts = "struct { uint16_t x; uint16_t y; }"
local l = List:new(
{id={type='string'}},
{value={type='struct', ts="struct { uint16_t x, y; }"}}
{value={type='struct', ts=ts}}
)
l:add_entry {id="foo", value={x=1, y=2}}
l:add_entry {id="foo1", value={x=2, y=3}}
Expand All @@ -1164,7 +1166,7 @@ function selftest_list ()
-- Test optional struct
local l = List:new(
{id={type='string'}},
{value={type='struct', ts="struct { uint16_t x, y; }", optional=true}}
{value={type='struct', ts=ts, optional=true}}
)
l:add_entry {id="foo"}
l:add_entry {id="foo1", value={x=2, y=3}}
Expand Down

0 comments on commit bb9eca2

Please sign in to comment.