MoonGen
 All Files Functions Variables Pages
bitmask.lua
Go to the documentation of this file.
1 ---------------------------------
2 --- @file bitmask.lua
3 --- @brief Bitmask ...
4 --- @todo TODO docu
5 ---------------------------------
6 
7 local ffi = require "ffi"
8 ffi.cdef [[
9 struct mg_bitmask{
10  uint16_t size;
11  uint16_t n_blocks;
12  uint64_t mask[0];
13 };
14 struct mg_bitmask * mg_bitmask_create(uint16_t size);
15 void mg_bitmask_free(struct mg_bitmask * mask);
16 void mg_bitmask_set_n_one(struct mg_bitmask * mask, uint16_t n);
17 void mg_bitmask_set_all_one(struct mg_bitmask * mask);
18 void mg_bitmask_clear_all(struct mg_bitmask * mask);
19 uint8_t mg_bitmask_get_bit(struct mg_bitmask * mask, uint16_t n);
20 void mg_bitmask_set_bit(struct mg_bitmask * mask, uint16_t n);
21 void mg_bitmask_clear_bit(struct mg_bitmask * mask, uint16_t n);
22 void mg_bitmask_and(struct mg_bitmask * mask1, struct mg_bitmask * mask2, struct mg_bitmask * result);
23 void mg_bitmask_xor(struct mg_bitmask * mask1, struct mg_bitmask * mask2, struct mg_bitmask * result);
24 void mg_bitmask_or(struct mg_bitmask * mask1, struct mg_bitmask * mask2, struct mg_bitmask * result);
25 void mg_bitmask_not(struct mg_bitmask * mask1, struct mg_bitmask * result);
26 ]]
27 
28 
29 mod = {}
30 
31 local mg_bitMask = {}
32 --mg_bitMask.__index = mg_bitMask
33 
34 --- Create a Bitmask
35 --- The mask is internally built from blocks of 64bit integers. Hence a Bitmask
36 --- of a size <<64 yields significant overhead
37 --- @param size Size of the bitmask in number of bits
38 --- @return Wrapper table around the bitmask
39 function mod.createBitMask(size)
40  return setmetatable({
41  bitmask = ffi.gc(ffi.C.mg_bitmask_create(size), function (self)
42  print("I HAVE BEEN DESTRUCTED")
43  ffi.C.mg_bitmask_free(self)
44  end )
45  }, mg_bitMask)
46 end
47 
48 ---
49 --- @param bitmasks
50 --- @todo TODO: think of a better solution - meh
51 function mod.linkToArray(bitmasks)
52  array = ffi.new("struct mg_bitmask*[?]", #bitmasks)
53  local i = 0
54  for _,m in pairs(bitmasks) do
55  array[i] = m.bitmask
56  i = i + 1
57  end
58  return array
59 end
60 
61 --- sets the first n bits in a bitmask to 1
62 --- other bits remain unchanged
63 --- @param n
64 function mg_bitMask:setN(n)
65  ffi.C.mg_bitmask_set_n_one(self.bitmask, n)
66  return self
67 end
68 
69 --- sets all bits in a bitmask to 0
70 function mg_bitMask:clearAll()
71  ffi.C.mg_bitmask_clear_all(self.bitmask)
72  return self
73 end
74 
75 --- sets all bits in a bitmask to 1
76 function mg_bitMask:setAll()
77  ffi.C.mg_bitmask_set_all_one(self.bitmask)
78  return self
79 end
80 
81 --- Index metamethod for mg_bitMask
82 --- @param x Bit index. Index starts at 1 according to the LUA standard (1 indexes the first bit in the bitmask)
83 --- @return For numeric indices: true, when corresponding bit is 1, false otherwise.
84 function mg_bitMask:__index(x)
85  -- access
86  --print(" bit access")
87  --print(" type " .. type(x))
88  --print(" x = " .. tostring(x))
89  if(type(x) == "number") then
90  return (ffi.C.mg_bitmask_get_bit(self.bitmask, x - 1) ~= 0)
91  else
92  return mg_bitMask[x]
93  end
94 end
95 
96 --- Newindex metamethod for mg_bitMask
97 --- @param x Bit index. Index starts at 1 according to the LUA standard (1 indexes the first bit in the bitmask)
98 --- @param y Assigned value to the index (bit is cleared for y==0 and set otherwise)
99 function mg_bitMask:__newindex(x, y)
100  --print ("new index")
101  if(y == 0) then
102  -- clear bit
103  --print("clear")
104  return ffi.C.mg_bitmask_clear_bit(self.bitmask, x - 1)
105  else
106  -- set bit
107  --print("set")
108  return ffi.C.mg_bitmask_set_bit(self.bitmask, x - 1)
109  end
110 end
111 
112 do
113  local function it(self, i)
114  if i >= self.bitmask.size then
115  return nil
116  end
117  return i + 1, self[i+1]
118  end
119 
120  function mg_bitMask.__ipairs(self)
121  return it, self, 0
122  end
123 end
124 
125 --- Bitwise and
126 --- @param mask1
127 --- @param mask2
128 --- @param result
129 --- result = mask1 band mask2
130 function mod.band(mask1, mask2, result)
131  ffi.C.mg_bitmask_and(mask1.bitmask, mask2.bitmask, result.bitmask)
132 end
133 
134 --- Bitwise or
135 --- @param mask1
136 --- @param mask2
137 --- @param result
138 --- result = mask1 bor mask2
139 function mod.bor(mask1, mask2, result)
140  ffi.C.mg_bitmask_or(mask1.bitmask, mask2.bitmask, result.bitmask)
141 end
142 
143 --- Bitwise xor
144 --- @param mask1
145 --- @param mask2
146 --- @param result
147 --- result = bask1 bxor mask2
148 function mod.bxor(mask1, mask2, result)
149  ffi.C.mg_bitmask_xor(mask1.bitmask, mask2.bitmask, result.bitmask)
150 end
151 
152 --- Bitwise not
153 --- @param mask
154 --- @param result
155 --- result = not mask
156 function mod.bnot(mask, result)
157  ffi.C.mg_bitmask_not(mask.bitmask, result.bitmask)
158 end
159 
160 return mod
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
function mg_bitMask clearAll()
sets all bits in a bitmask to 0
function mod band(mask1, mask2, result)
Bitwise and.
function ipsecICV set(icv)
Set the IPsec ICV.
function mod new(n)
function mod bor(mask1, mask2, result)
Bitwise or.
function mod bxor(mask1, mask2, result)
Bitwise xor.
function mg_bitMask setAll()
sets all bits in a bitmask to 1
local mod
high-level dpdk wrapper
Definition: dpdk.lua:6
function mg_bitMask __index(x)
Index metamethod for mg_bitMask.
function mod linkToArray(bitmasks)
function mg_bitMask __newindex(x, y)
Newindex metamethod for mg_bitMask.
function mod bnot(mask, result)
Bitwise not.
n
Create a new array of memory buffers (initialized to nil).
Definition: memory.lua:76
function mg_bitMask setN(n)
sets the first n bits in a bitmask to 1 other bits remain unchanged