本文簡要介紹ruby語言中 Fiddle::Pointer.malloc
的用法。
用法
Fiddle::Pointer.malloc(size, freefunc = nil) → fiddle pointer instance
Fiddle::Pointer.malloc(size, freefunc) { |pointer| ... } → ...
例子
# Automatically freeing the pointer when the block is exited - recommended Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) do |pointer| ... end # Manually freeing but relying on the garbage collector otherwise pointer = Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) ... pointer.call_free # Relying on the garbage collector - may lead to unlimited memory allocated before freeing any, but safe pointer = Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) ... # Only manually freeing pointer = Fiddle::Pointer.malloc(size) begin ... ensure Fiddle.free pointer end # No free function and no call to free - the native memory will leak if the pointer is garbage collected pointer = Fiddle::Pointer.malloc(size) ...
分配 size
字節的內存並將其與可選的 freefunc
相關聯。
如果提供了一個塊,則指針將被屈服於該塊而不是被返回,並且該塊的返回值將被返回。如果有塊,則必須提供 freefunc
。
如果提供了 freefunc
,它將被調用一次,當指針被垃圾收集時,或者如果提供了塊,或者當用戶調用 call_free
時,以先發生者為準。 freefunc
必須是指向 Fiddle::Function
的函數或實例的地址。
相關用法
- Ruby PrettyPrint.current_group用法及代碼示例
- Ruby Pathname.<=>用法及代碼示例
- Ruby Pathname.children用法及代碼示例
- Ruby Process.groups用法及代碼示例
- Ruby Process.wait2用法及代碼示例
- Ruby Process.getpgrp用法及代碼示例
- Ruby Proc.eql?用法及代碼示例
- Ruby PTY.open用法及代碼示例
- Ruby PrettyPrint.genspace用法及代碼示例
- Ruby PKey.sign_raw用法及代碼示例
- Ruby Profiler模塊用法及代碼示例
- Ruby Process.setproctitle用法及代碼示例
- Ruby PPMethods.comma_breakable用法及代碼示例
- Ruby Process.setrlimit用法及代碼示例
- Ruby Proc.prc ==用法及代碼示例
- Ruby Profiler.raw_data用法及代碼示例
- Ruby Process.uid用法及代碼示例
- Ruby Pathname.descend用法及代碼示例
- Ruby Process.pid用法及代碼示例
- Ruby Proc.ruby2_keywords用法及代碼示例
- Ruby Pathname.getwd用法及代碼示例
- Ruby PKey.generate_parameters用法及代碼示例
- Ruby Proc.new用法及代碼示例
- Ruby Process.detach用法及代碼示例
- Ruby Pathname.ascend用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Pointer.malloc。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。