diff options
author | Araq <rumpf_a@web.de> | 2014-04-09 08:37:08 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-04-09 08:37:08 +0200 |
commit | d0f013477b16520eefff69b861d2f26744463880 (patch) | |
tree | ff9a4f7c6b942a4b0d8dcecf8814c1d1ed19bd40 /doc/manual.txt | |
parent | 54935e2e70b819853a09ef4582b8cdc2526c1744 (diff) | |
download | Nim-d0f013477b16520eefff69b861d2f26744463880.tar.gz |
refined and documented regionized pointers
Diffstat (limited to 'doc/manual.txt')
-rw-r--r-- | doc/manual.txt | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/doc/manual.txt b/doc/manual.txt index 6526ba2cb..298f6329b 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -1366,6 +1366,65 @@ not nilable pointers. The details of this analysis are still to be specified here. +Memory regions +-------------- + +The types ``ref`` and ``ptr`` can get an optional `region`:idx: annotation. +A region has to be an object type. + +Regions are very useful to separate user space and kernel memory in the +development of OS kernels: + +.. code-block:: nimrod + type + Kernel = object + Userspace = object + + var a: ptr[Kernel, Stat] + var b: ptr[Userspace, Stat] + + # the following does not compile as the pointer types are incompatible: + a = b + +In order to make generic code easier tor write ``ptr T`` is a subtype +of ``ptr[R, T]`` for any ``R``. + +Furthermore the subtype relation of the region object types is lifted to +the pointer types: If ``A <: B`` then ``ptr[T, A] <: ptr[T, B]``. This can be +used to model subregions of memory. As a special typing rule ``ptr[R, T]`` is +not compatible to ``pointer`` to prevent the following from compiling: + +.. code-block:: nimrod + # from system + proc dealloc(p: pointer) + + # wrap some scripting language + type + PythonsHeap = object + PyObjectHeader = object + rc: int + typ: pointer + PyObject = ptr[PythonsHeap, PyObjectHeader] + + proc createPyObject(): PyObject {.importc: "...".} + proc destroyPyObject(x: PyObject) {.importc: "...".} + + var foo = createPyObject() + # type error here, how convenient: + dealloc(foo) + + +Future directions: + +* Memory regions might become available for ``string`` and ``seq`` too. +* Bultin regions like ``private``, ``global`` and ``local`` will + prove very useful for the upcoming OpenCL target. +* Bultin "regions" can model ``lent`` and ``unique`` pointers. +* Syntactially ``ptr`` might become an infix operator so that ``region ptr T`` + is transformed into ``ptr[region, T]``. + + + Procedural type --------------- A `procedural type`:idx: is internally a pointer to a procedure. ``nil`` is |