本文简要介绍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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。