<commit_before>require 'gir_ffi/allocation_helper'

module GirFFI
  module ArgHelper
    def self.object_to_inptr obj
      return obj.to_ptr if obj.respond_to? :to_ptr
      return nil if obj.nil?
      FFI::Pointer.new(obj.object_id)
    end

    def self.int32_array_to_inptr ary
      return nil if ary.nil?
      block = allocate_array_of_type :int32, ary.length
      block.write_array_of_int32 ary
      block
    end

    # TODO: Use alias.
    def self.int_array_to_inptr ary
      int32_array_to_inptr ary
    end

    def self.int16_array_to_inptr ary
      block = allocate_array_of_type :int16, ary.length
      block.write_array_of_int16 ary
      block
    end

    def self.int64_array_to_inptr ary
      block = allocate_array_of_type :int64, ary.length
      block.write_array_of_int64 ary
      block
    end

    def self.int8_array_to_inptr ary
      block = allocate_array_of_type :int8, ary.length
      block.write_array_of_int8 ary
      block
    end

    def self.GType_array_to_inptr ary
      case FFI.type_size(:size_t)
      when 4
	block = allocate_array_of_type :uint32, ary.length
	block.write_array_of_uint32 ary
      when 8
	block = allocate_array_of_type :uint64, ary.length
	block.write_array_of_uint64 ary
      else
	raise RuntimeError, "Unexpected size of :size_t"
      end
      block
    end

    def self.cleanup_ptr ptr
      LibC.free ptr
    end

    def self.cleanup_ptr_ptr ptr
      block = ptr.read_pointer
      LibC.free ptr
      LibC.free block
    end

    # Converts an outptr to a string array, then frees pointers.
    def self.cleanup_ptr_array_ptr ptr, size
      return if ptr.nil?

      block = ptr.read_pointer
      LibC.free ptr

      return if block.null?

      ptrs = block.read_array_of_pointer(size)
      LibC.free block

      ptrs.map do |p|
	LibC.free p unless p.null?
      end
    end<commit_msg>Use more bare-metal memory writer methods, so things work with JRuby.<commit_after>require 'gir_ffi/allocation_helper'

module GirFFI
  module ArgHelper
    def self.object_to_inptr obj
      return obj.to_ptr if obj.respond_to? :to_ptr
      return nil if obj.nil?
      FFI::Pointer.new(obj.object_id)
    end

    def self.typed_array_to_inptr type, ary
      return nil if ary.nil?
      block = allocate_array_of_type type, ary.length
      block.send "put_array_of_#{type}", 0, ary
      block
    end

    def self.int32_array_to_inptr ary
      typed_array_to_inptr :int32, ary
    end

    # TODO: Use alias.
    def self.int_array_to_inptr ary
      int32_array_to_inptr ary
    end

    def self.int16_array_to_inptr ary
      typed_array_to_inptr :int16, ary
    end

    def self.int64_array_to_inptr ary
      typed_array_to_inptr :int64, ary
    end

    def self.int8_array_to_inptr ary
      typed_array_to_inptr :int8, ary
    end

    def self.GType_array_to_inptr ary
      case FFI.type_size(:size_t)
      when 4
	int32_array_to_inptr ary
      when 8
	int64_array_to_inptr ary
      else
	raise RuntimeError, "Unexpected size of :size_t"
      end
    end

    def self.cleanup_ptr ptr
      LibC.free ptr
    end

    def self.cleanup_ptr_ptr ptr
      block = ptr.read_pointer
      LibC.free ptr
      LibC.free block
    end

    # Converts an outptr to a string array, then frees pointers.
    def self.cleanup_ptr_array_ptr ptr, size
      return if ptr.nil?

      block = ptr.read_pointer
      LibC.free ptr

      return if block.null?

      ptrs = block.read_array_of_pointer(size)
      LibC.free block

      ptrs.map do |p|
	LibC.free p unless p.null?
      end
    end
{'fraction_non_alphanumeric': 0.053013392857142856, 'fraction_numerical': 0.015625, 'mean_word_length': 2.4772065955383122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4088794', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1282, 'n_words': 320}