Skip to content

Commit

Permalink
packet: add a metatable to the packet ctype
Browse files Browse the repository at this point in the history
This makes the paket an object, and allows for example p:free() instead
of packet.free(p)

Add a new method `physical` to get the physical address of packet.data
  • Loading branch information
Nikolay Nikolaev committed Dec 22, 2015
1 parent 7c99720 commit 394783a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/core/packet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ end
-- Return pointer to packet data.
function data (p) return p.data end

-- Return physical address of packet data
function physical (p)
return memory.memory.virtual_to_physical(p.data)
end

-- Return packet data length.
function length (p) return p.length end

Expand All @@ -109,3 +114,21 @@ function preallocate_step()
packet_allocation_step = 2 * packet_allocation_step
end

function dump(p, w)
w = w or io.write
for i = 0, p.length-1 do
if i % 16 == 0 then w('\n', bit.tohex(i, -4), ': ') end
w(bit.tohex(p.data[i], -2), ' ')
end
w('\n')
end

ffi.metatype(packet_t, {__index = {
clone = clone,
append = append,
prepend = prepend,
shiftleft = shiftleft,
free = free,
physical = physical,
dump = dump,
}})

0 comments on commit 394783a

Please sign in to comment.