Last active
February 15, 2021 07:26
-
-
Save kojix2/5e7102a0818261e1f840ff04b7fb7bc5 to your computer and use it in GitHub Desktop.
Ruby FFI bitfields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "ffi" | |
module FFI | |
class BitStruct < Struct | |
class << self | |
# def union_layout(*args) | |
# Class.new(FFI::Union) { layout(*args) } | |
# end | |
# def struct_layout(*args) | |
# Class.new(FFI::Struct) { layout(*args) } | |
# end | |
module BitFieldsModule | |
def [](name) | |
bit_fields = self.class.bit_fields_map | |
parent, start, width = bit_fields[name] | |
if parent | |
(super(parent) >> start) & ((1 << width) - 1) | |
else | |
super(name) | |
end | |
end | |
end | |
private_constant :BitFieldsModule | |
attr_reader :bit_fields_map | |
def bitfields(*args) | |
unless instance_variable_defined?(:@bit_fields) | |
@bit_fields_map = {} | |
prepend BitFieldsModule | |
end | |
parent = args.shift | |
labels = [] | |
widths = [] | |
args.each_slice(2) do |l, w| | |
labels << l | |
widths << w | |
end | |
starts = widths.inject([0]) do |result, w| | |
result << (result.last + w) | |
end | |
labels.zip(starts, widths).each do |l, s, w| | |
@bit_fields_map[l] = [parent, s, w] | |
end | |
end | |
end | |
end | |
end |
Author
kojix2
commented
Jan 28, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment