-
Notifications
You must be signed in to change notification settings - Fork 100
Add Ctypes_memory.keep_alive function
#777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -198,6 +198,24 @@ val allocate_n : ?finalise:('a ptr -> unit) -> 'a typ -> count:int -> 'a ptr | |||||
| memory is allocated with libc's [calloc] and is guaranteed to be | ||||||
| zero-filled. *) | ||||||
|
|
||||||
| val keep_alive : 'a -> unit | ||||||
| (** Inserting [keep_alive x;] in a sequence of expressions ensures that | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| the garbage collector will not collect [x] until after that [keep_alive] | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| has returned. | ||||||
|
|
||||||
| For example: | ||||||
| {[ | ||||||
| let strchr = Foreign.foreign "strchr" (ptr char @-> char @-> returning (ptr char)) in | ||||||
| let p = CArray.of_string "abc" in | ||||||
| let q = strchr (CArray.start p) 'a' in | ||||||
| let () = Gc.compact () in | ||||||
| let () = Printf.printf "%c\n" !@q in | ||||||
| keep_alive p; | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ]} | ||||||
| Without the [keep_alive p] at the bottom, [p] could be collected during the | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| [Gc.compact ()] call, which would make [q] an invalid pointer into the | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| now-collected [p], leading to undefined behavior. *) | ||||||
|
Comment on lines
+208
to
+217
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice example, but I think it would be even stronger without the call to |
||||||
|
|
||||||
| val ptr_compare : 'a ptr -> 'a ptr -> int | ||||||
| (** If [p] and [q] are pointers to elements [i] and [j] of the same array then | ||||||
| [ptr_compare p q] compares the indexes of the elements. The result is | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -131,4 +131,7 @@ let view : type a b l m. (a, b, l) t -> (m option, _) Ctypes_ptr.Fat.t -> b = | |||||||||
| | Dims3 (d1, d2, d3) -> view3 kind ~dims:[| d1; d2; d3 |] ptr layout in | ||||||||||
| match Ctypes_ptr.Fat.managed ptr with | ||||||||||
| | None -> ba | ||||||||||
| | Some src -> Gc.finalise (fun _ -> Ctypes_memory_stubs.use_value src) ba; ba | ||||||||||
| | Some src -> | ||||||||||
| (* To avoid a dependence on Ctypes_memory, we redefine keep_alive here. *) | ||||||||||
| let keep_alive x = ignore (Sys.opaque_identity x) in | ||||||||||
| Gc.finalise (fun _ -> keep_alive src) ba; ba | ||||||||||
|
Comment on lines
+135
to
+137
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to change the version