从自由线性对象接收到的结构的生命周期是多少?
What is the lifetime of a struct received from a free'd linear object?
考虑:
#include "share/atspre_staload.hats"
%{^
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
%}
typedef statbuf = $extype "struct stat"
extern fun cstat: (string, &statbuf? >> statbuf) -> int = "mac#stat"
extern fun S_ISDIR: int -> bool = "mac#"
extern fun stat_mode: statbuf -> int = "mac#stat_mode"
%{
#define stat_mode(x) x.st_mode
%}
datavtype statbuf_vt = STAT of statbuf
fun stat(path: string): Option_vt(statbuf_vt) =
let
val buf = STAT(_)
val STAT(hole) = buf
val res = cstat(path, hole)
prval _ = fold@(buf)
in
if res = 0 then Some_vt(buf)
else
let
val ~STAT(_) = buf
in
None_vt()
end
end
implement main0() =
let
val path = "/etc/passwd"
val- ~Some_vt(~STAT(buf)) = stat(path)
in
println!(~S_ISDIR(stat_mode(buf)))
end
最后 buf
的生命周期是多少?它似乎不需要释放——valgrind 看到 3 个分配和 3 个释放,没有问题。但是 buf
不是由那个 STAT
线性对象管理的内存的一部分吗?
注意 'buf' 是一个平面结构。任何与模式 ~STAT(buf) 匹配的值都被释放;在它被释放之前,值中的一些内容被复制到位于堆栈上的 'buf' 中。当调用 'main0' 的帧展开时,'buf' 的生命周期结束。提供的代码中没有泄漏。
考虑:
#include "share/atspre_staload.hats"
%{^
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
%}
typedef statbuf = $extype "struct stat"
extern fun cstat: (string, &statbuf? >> statbuf) -> int = "mac#stat"
extern fun S_ISDIR: int -> bool = "mac#"
extern fun stat_mode: statbuf -> int = "mac#stat_mode"
%{
#define stat_mode(x) x.st_mode
%}
datavtype statbuf_vt = STAT of statbuf
fun stat(path: string): Option_vt(statbuf_vt) =
let
val buf = STAT(_)
val STAT(hole) = buf
val res = cstat(path, hole)
prval _ = fold@(buf)
in
if res = 0 then Some_vt(buf)
else
let
val ~STAT(_) = buf
in
None_vt()
end
end
implement main0() =
let
val path = "/etc/passwd"
val- ~Some_vt(~STAT(buf)) = stat(path)
in
println!(~S_ISDIR(stat_mode(buf)))
end
最后 buf
的生命周期是多少?它似乎不需要释放——valgrind 看到 3 个分配和 3 个释放,没有问题。但是 buf
不是由那个 STAT
线性对象管理的内存的一部分吗?
注意 'buf' 是一个平面结构。任何与模式 ~STAT(buf) 匹配的值都被释放;在它被释放之前,值中的一些内容被复制到位于堆栈上的 'buf' 中。当调用 'main0' 的帧展开时,'buf' 的生命周期结束。提供的代码中没有泄漏。