{"action":"created","issue":{"url":"https://api.github.com/repos/ocaml/ocaml/issues/724","repository_url":"https://api.github.com/repos/ocaml/ocaml","labels_url":"https://api.github.com/repos/ocaml/ocaml/issues/724/labels{/name}","comments_url":"https://api.github.com/repos/ocaml/ocaml/issues/724/comments","events_url":"https://api.github.com/repos/ocaml/ocaml/issues/724/events","html_url":"https://github.com/ocaml/ocaml/pull/724","id":168047587,"number":724,"title":"Add compiler support for raw pointers","user":{"login":"diml","id":1251584,"avatar_url":"https://avatars.githubusercontent.com/u/1251584?v=3","gravatar_id":"","url":"https://api.github.com/users/diml","html_url":"https://github.com/diml","followers_url":"https://api.github.com/users/diml/followers","following_url":"https://api.github.com/users/diml/following{/other_user}","gists_url":"https://api.github.com/users/diml/gists{/gist_id}","starred_url":"https://api.github.com/users/diml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diml/subscriptions","organizations_url":"https://api.github.com/users/diml/orgs","repos_url":"https://api.github.com/users/diml/repos","events_url":"https://api.github.com/users/diml/events{/privacy}","received_events_url":"https://api.github.com/users/diml/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/ocaml/ocaml/milestones/2","html_url":"https://github.com/ocaml/ocaml/milestones/4.04","labels_url":"https://api.github.com/repos/ocaml/ocaml/milestones/2/labels","id":1464889,"number":2,"title":"4.04","description":"","creator":{"login":"damiendoligez","id":19104,"avatar_url":"https://avatars.githubusercontent.com/u/19104?v=3","gravatar_id":"","url":"https://api.github.com/users/damiendoligez","html_url":"https://github.com/damiendoligez","followers_url":"https://api.github.com/users/damiendoligez/followers","following_url":"https://api.github.com/users/damiendoligez/following{/other_user}","gists_url":"https://api.github.com/users/damiendoligez/gists{/gist_id}","starred_url":"https://api.github.com/users/damiendoligez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/damiendoligez/subscriptions","organizations_url":"https://api.github.com/users/damiendoligez/orgs","repos_url":"https://api.github.com/users/damiendoligez/repos","events_url":"https://api.github.com/users/damiendoligez/events{/privacy}","received_events_url":"https://api.github.com/users/damiendoligez/received_events","type":"User","site_admin":false},"open_issues":66,"closed_issues":72,"state":"open","created_at":"2015-12-17T12:44:22Z","updated_at":"2016-07-28T09:09:16Z","due_on":"2016-07-17T22:00:00Z","closed_at":null},"comments":1,"created_at":"2016-07-28T09:08:07Z","updated_at":"2016-07-28T09:14:20Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/ocaml/ocaml/pulls/724","html_url":"https://github.com/ocaml/ocaml/pull/724","diff_url":"https://github.com/ocaml/ocaml/pull/724.diff","patch_url":"https://github.com/ocaml/ocaml/pull/724.patch"},"body":"This PR takes part of #652, completes it and gets it ready for review. We have several use cases at Jane Street where this feature is a pre-requisite and we'd like to have it for 4.04.\r\n\r\n## Synopsis\r\n\r\nThis patch adds primitives for reading and writing 1, 2, 4 and 8 bytes integers from/to memory outside of the OCaml heap. Memory locations are naturally represented as native integers, which are the same size as pointers in OCaml.\r\n\r\nThe new primitives are for advanced users only and are not yet exposed in the stdlib. We have some ideas to type-check compiler primitives and we'll be working on this for the next version of the compiler, which will address concerns expressed in the original PR.\r\n\r\n## Motivation\r\n\r\nAs mentioned in the original PR, this allows to manipulate more C data structures directly from OCaml.\r\n\r\nAt Jane Street we have at least two use cases for this:\r\n\r\n#### 1. C bindings\r\n\r\nWe have some code binding C function that tries to manipulate C pointers in OCaml. While this allow to write less C code, it is often hard to reason about correctness regarding the GC. This feature would improve this as the GC doesn't look at these pointer at all.\r\n\r\n#### 2. Lighter interface to C memory\r\n\r\nIn several places, we'd like a lighter interface than bigarrays to C memory. One reason is that we have some code where the bigarray indirection has a non-negligible cost. With this patch, we would first assert that the pointer is 2-aligned, then store it in an integer. When using the primitives we would convert it back to a nativeint and the compiler will be able to get rid of the temporary boxing.\r\n\r\nAnother reason is that we often know exactly the scope of such a bigarray and we'd prefer to release the memory explicitly rather than rely on the GC to do it, especially if the memory buffer is short lived.\r\n\r\nWe thought about just using regular OCaml string allocated outside the heap. However this is unsafe if the external memory can be freed.\r\n\r\n#### Details\r\n\r\nSeveral %-primitives are added: `%load{8,16,32,64}` for loading integers and `%store{8,16,32,64}` for storing them. These primitives take a pointer represented as a `nativeint`. The types used for the integers to read/write are the same as for the string/bigstring primitives: `char`, `int`, `int32` and `int64`.\r\n\r\nFor the byte-code stubs, given that they are very similar to the string primitives, I moved the code for reading/writing from `str.c` to inline functions in `byterun/get_set.h` and used them in both `str.c` and the new `ptr.c` file.\r\n\r\nI also copied the tests for the string primitives in `testsuite/tests/prim-bigstring/string_access.ml` as `testsuite/tests/prim-bigstring/ptr_access.ml` and adapted them.\r\n\r\n## Notes\r\n\r\nThe original patch also added primitives for aligned reads. I wasn't totally sure what to do for the byte-code versions, and in order to keep this PR simple I didn't included them. I leave this part for future work. The current primitives already allow to do quite a lot.\r\n\r\nI also renamed the new constructors for `Lambda.primitive` to be more consistent with the current string/bigstring primitives.\r\n\r\n@bobot I tried to start my work from the branch you sent me, but there was too much refactoring. In order to keep this patch small it was easier for me to start from the original PR. I let you submit your refactoring in another PR.\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/ocaml/ocaml/issues/comments/235842471","html_url":"https://github.com/ocaml/ocaml/pull/724#issuecomment-235842471","issue_url":"https://api.github.com/repos/ocaml/ocaml/issues/724","id":235842471,"user":{"login":"mshinwell","id":1315488,"avatar_url":"https://avatars.githubusercontent.com/u/1315488?v=3","gravatar_id":"","url":"https://api.github.com/users/mshinwell","html_url":"https://github.com/mshinwell","followers_url":"https://api.github.com/users/mshinwell/followers","following_url":"https://api.github.com/users/mshinwell/following{/other_user}","gists_url":"https://api.github.com/users/mshinwell/gists{/gist_id}","starred_url":"https://api.github.com/users/mshinwell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshinwell/subscriptions","organizations_url":"https://api.github.com/users/mshinwell/orgs","repos_url":"https://api.github.com/users/mshinwell/repos","events_url":"https://api.github.com/users/mshinwell/events{/privacy}","received_events_url":"https://api.github.com/users/mshinwell/received_events","type":"User","site_admin":false},"created_at":"2016-07-28T09:14:20Z","updated_at":"2016-07-28T09:14:20Z","body":"One other point: I don't yet know of a compelling argument as to why a raw pointer API should be exposed within the compiler distribution.  It seems a good candidate for a separate library for expert users only."}}
{"actor":{"display_login":"mshinwell"}}