Module: OS::Mac::FFI::CoreFoundation Private

Extended by:
NativeLibrary
Defined in:
os/mac/ffi/core_foundation.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

CoreFoundation.framework wrapper

Methods that create Core Foundation objects follow the Create Rule: the caller owns the result and must release it deterministically with CoreFoundation.release, usually via CoreFoundation.with_release_pool. Constant accessors return borrowed references that must not be released. Ownership must not be handed to the garbage collector: a GC-time CFRelease can run on the child side of fork (e.g. in Utils.popen), where Core Foundation is not fork-safe. https://github.com/Homebrew/brew/issues/23606

Defined Under Namespace

Classes: ReleasePool

Class Method Summary collapse

Class Method Details

.dictionary_create(hash) ⇒ Fiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFDictionary.h:

CFDictionaryRef CFDictionaryCreate(
CFAllocatorRef allocator,
const void **keys,
const void **values,
CFIndex numValues,
const CFDictionaryKeyCallBacks *keyCallBacks,
const CFDictionaryValueCallBacks *valueCallBacks);

Parameters:

  • hash (Hash{Fiddle::Pointer => Fiddle::Pointer})

Returns:

  • (Fiddle::Pointer)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'os/mac/ffi/core_foundation.rb', line 118

def self.dictionary_create(hash)
  size = Fiddle::SIZEOF_VOIDP * hash.size
  Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) do |keys|
    Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) do |values|
      # Convert array of pointers to continous stream of pointers in the C buffer
      keys[0, size] = hash.keys.pack("J*")
      values[0, size] = hash.values.pack("J*")
      return function(
        "CFDictionaryCreate",
        [
          Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP,
          Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP
        ],
        Fiddle::TYPE_VOIDP,
      ).call(
        nil, keys, values, hash.size, type_dictionary_key_call_backs, type_dictionary_value_call_backs
      )
    end
  end
end

.release(ptr) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

CoreFoundation/CFBase.h:

void CFRelease(CFTypeRef cf);

Parameters:

  • ptr (Fiddle::Pointer)


63
64
65
66
67
# File 'os/mac/ffi/core_foundation.rb', line 63

def self.release(ptr)
  return if ptr.null?

  function("CFRelease", [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID).call(ptr)
end

.string_create(string) ⇒ Fiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFString.h:

CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);

Parameters:

Returns:

  • (Fiddle::Pointer)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'os/mac/ffi/core_foundation.rb', line 87

def self.string_create(string)
  cf_encoding = case string.encoding
  when Encoding::UTF_8
    0x08000100 # kCFStringEncodingUTF8
  when Encoding::US_ASCII
    0x0600 # kCFStringEncodingASCII
  when Encoding::ASCII_8BIT, Encoding::ISO8859_1
    # ASCII-8BIT could be anything, so just use Latin-1
    0x0201 # kCFStringEncodingISOLatin1
  else
    # Try convert to UTF-8 and move on
    string = string.encode(Encoding::UTF_8)
    0x08000100
  end

  function(
    "CFStringCreateWithCString",
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_CONST_STRING, Fiddle::TYPE_UINT32_T],
    Fiddle::TYPE_VOIDP,
  ).call(nil, string, cf_encoding)
end

.type_dictionary_key_call_backsFiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFDictionary.h:

extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;

Returns:

  • (Fiddle::Pointer)


72
# File 'os/mac/ffi/core_foundation.rb', line 72

def self.type_dictionary_key_call_backs = constant("kCFTypeDictionaryKeyCallBacks")

.type_dictionary_value_call_backsFiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFDictionary.h:

extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks

Returns:

  • (Fiddle::Pointer)


77
# File 'os/mac/ffi/core_foundation.rb', line 77

def self.type_dictionary_value_call_backs = constant("kCFTypeDictionaryValueCallBacks")

.url_create_with_file_system_path(path) ⇒ Fiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFURL.h:

CFURLRef CFURLCreateWithFileSystemPath(CFAllocatorRef allocator,
CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory);

Parameters:

  • path (Fiddle::Pointer)

Returns:

  • (Fiddle::Pointer)


143
144
145
146
147
148
149
# File 'os/mac/ffi/core_foundation.rb', line 143

def self.url_create_with_file_system_path(path)
  function(
    "CFURLCreateWithFileSystemPath",
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG, Fiddle::TYPE_BOOL],
    Fiddle::TYPE_VOIDP,
  ).call(nil, path, 0, false)
end

.url_quarantine_properties_keyFiddle::Pointer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFURL.h:

extern const CFStringRef kCFURLQuarantinePropertiesKey;

Returns:

  • (Fiddle::Pointer)


82
# File 'os/mac/ffi/core_foundation.rb', line 82

def self.url_quarantine_properties_key = constant("kCFURLQuarantinePropertiesKey", dereference: true)

.url_set_resource_property_for_key(url, key, value) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

CoreFoundation/CFURL.h:

Boolean CFURLSetResourcePropertyForKey(CFURLRef url, CFStringRef key, CFTypeRef value, CFErrorRef *error);

Parameters:

  • url (Fiddle::Pointer)
  • key (Fiddle::Pointer)
  • value (Fiddle::Pointer)

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'os/mac/ffi/core_foundation.rb', line 154

def self.url_set_resource_property_for_key(url, key, value)
  function(
    "CFURLSetResourcePropertyForKey",
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
    Fiddle::TYPE_BOOL,
  ).call(url, key, value, nil)
end

.with_release_pool(&_block) ⇒ T.type_parameter(:U)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Yields a ReleasePool and drains it when the block finishes, releasing every tracked object even if the block raises.

Parameters:

  • _block (T.proc.params(pool: ReleasePool).returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U))


53
54
55
56
57
58
# File 'os/mac/ffi/core_foundation.rb', line 53

def self.with_release_pool(&_block)
  pool = ReleasePool.new
  yield pool
ensure
  pool&.drain
end