From 40cf3e0cca0a347bf8b6b44832fdcc358e068630 Mon Sep 17 00:00:00 2001 From: drbrain Date: Thu, 31 May 2012 00:27:34 +0000 Subject: * ext/dl/lib/dl/struct.rb (DL::CStructEntity::size): Refactored ::size to remove unused variables and simplify using newer ruby features. * test/dl/test_c_struct_entry.rb: Test to validate refactoring Reviewed by Aaron Patterson git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/dl/lib/dl/struct.rb | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'ext/dl') diff --git a/ext/dl/lib/dl/struct.rb b/ext/dl/lib/dl/struct.rb index 628adb21c2..e6f7ba710b 100644 --- a/ext/dl/lib/dl/struct.rb +++ b/ext/dl/lib/dl/struct.rb @@ -93,26 +93,18 @@ module DL # => 24 def CStructEntity.size(types) offset = 0 - max_align = 0 - types.each_with_index{|t,i| - orig_offset = offset - if( t.is_a?(Array) ) - align = PackInfo::ALIGN_MAP[t[0]] - offset = PackInfo.align(orig_offset, align) - size = offset - orig_offset - offset += (PackInfo::SIZE_MAP[t[0]] * t[1]) - else - align = PackInfo::ALIGN_MAP[t] - offset = PackInfo.align(orig_offset, align) - size = offset - orig_offset - offset += PackInfo::SIZE_MAP[t] - end - if (max_align < align) - max_align = align - end - } - offset = PackInfo.align(offset, max_align) - offset + + max_align = types.map { |type, count = 1| + last_offset = offset + + align = PackInfo::ALIGN_MAP[type] + offset = PackInfo.align(last_offset, align) + + (PackInfo::SIZE_MAP[type] * count) + + align + }.max + + PackInfo.align(offset, max_align) end # Wraps the C pointer +addr+ as a C struct with the given +types+. The C -- cgit v1.2.3