Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 23, 2024
1 parent 2499b3d commit 72a7adf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
31 changes: 25 additions & 6 deletions ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ int main()
}
```

> Note: **owner** is actually a macro declared in ownership as **_Owner**.
> Note: **owner** is actually a macro declared in <ownership.h> as **_Owner**.
The ownership mechanism has some rules that will be listed gradually throughout the text.

**Rule:** An **owner object** is always the unique owner of the referenced object.

**Rule:** When owner objects are copied the ownership is transfered.
**Rule:** When owner objects are copied the ownership is transferred.

**Rule:** Before the end of its lifetime, owner objects must move the ownership of the objects they own.

Expand All @@ -66,10 +66,11 @@ int main()

Invoking a function `fclose` is analogous to assignment of the argument `f2`, resulting in the transfer of ownership of `f2` to the function parameter.

Sample - Declaration of fclose
Sample - Declaration of fopen and fclose

```c
void fclose(FILE *owner p);
FILE * _Owner _fopen( const char *filename, const char *mode );
void fclose(FILE * _Owner p);
```
> Note: The cake ownership model does not include the concept of a destroyed or deleted object. Instead, everything is viewed as a transformation, where the object is broken into smaller parts and those parts are moved.
Expand Down Expand Up @@ -278,6 +279,24 @@ int main() {
}
```
### Conversion from owner pointer to void * owner
**Rule:** Assignment or cast of a owner pointer to void * owner requires the pointed object to be empty.
```c
struct X {
char _Owner text;
};
struct X * _Owner make();
int main(){
void * _Owner p = nullptr;
_Owner auto pX = make();
p = pX; //warning
}
```

When the object is created on the stack, we can implement a destructor.

**Sample - Implementing a destructor**
Expand Down Expand Up @@ -753,7 +772,7 @@ void free(void * _Owner _Opt p); //p can be null
#### Zero and Not-Zero state
The **zero** state is used for non-owner objects to complement and support uninitialized checks.
The **zero** state is used for non-pointer objects to complement and support uninitialized checks.
**Sample - The zero state**
Expand Down Expand Up @@ -784,7 +803,7 @@ We can use **static_set** to override states. In the next sample, we annotate th
close(server_socket);
```
The **not-zero** state is used for non-owner objects to indicate the value if not zero.
The **not-zero** state is used in non-pointers objects to indicate the value is not zero.
**any** is a alias for **zero or not-zero** state.
Expand Down
45 changes: 33 additions & 12 deletions src/web/ownership.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ <h1>Cake - C23 and Beyond</h1>
<li>
<a href="#toc_7">Deleting Owner Pointers</a>
</li>
<li>
<a href="#toc_8">Conversion from owner pointer to void * owner</a>
</li>
</ul>
</li>
<li>
<a href="#toc_8">Flow analysis</a>
<a href="#toc_9">Flow analysis</a>
<ul>
<li>
<a href="#toc_9">assert is a statement</a>
<a href="#toc_10">assert is a statement</a>
</li>
</ul>
</li>
<li>
<a href="#toc_10">Ownership Feature Strategy (Inspired by stdbool.h)</a>
<a href="#toc_11">Ownership Feature Strategy (Inspired by stdbool.h)</a>
</li>
</ul>
<p>Last Updated 22/03/2024</p>
Expand Down Expand Up @@ -93,14 +96,14 @@ <h3 id="toc_2">Owner Objects</h3>
</code></pre>

<blockquote>
<p>Note: <strong>owner</strong> is actually a macro declared in ownership as <strong>_Owner</strong>. </p>
<p>Note: <strong>owner</strong> is actually a macro declared in <ownership.h> as <strong>_Owner</strong>. </p>
</blockquote>

<p>The ownership mechanism has some rules that will be listed gradually throughout the text.</p>

<p><strong>Rule:</strong> An <strong>owner object</strong> is always the unique owner of the referenced object.</p>

<p><strong>Rule:</strong> When owner objects are copied the ownership is transfered.</p>
<p><strong>Rule:</strong> When owner objects are copied the ownership is transferred.</p>

<p><strong>Rule:</strong> Before the end of its lifetime, owner objects must move the ownership of the objects they own.</p>

Expand All @@ -120,9 +123,10 @@ <h3 id="toc_2">Owner Objects</h3>

<p>Invoking a function <code>fclose</code> is analogous to assignment of the argument <code>f2</code>, resulting in the transfer of ownership of <code>f2</code> to the function parameter. </p>

<p>Sample - Declaration of fclose</p>
<p>Sample - Declaration of fopen and fclose</p>

<pre><code class="language-c">void fclose(FILE *owner p);
<pre><code class="language-c">FILE * _Owner _fopen( const char *filename, const char *mode );
void fclose(FILE * _Owner p);
</code></pre>

<blockquote>
Expand Down Expand Up @@ -328,6 +332,23 @@ <h3 id="toc_7">Deleting Owner Pointers</h3>
}
</code></pre>

<h3 id="toc_8">Conversion from owner pointer to void * owner</h3>

<p><strong>Rule:</strong> Assignment or cast of a owner pointer to void * owner requires the pointed object to be empty.</p>

<pre><code class="language-c">struct X {
char _Owner text;
};

struct X * _Owner make();

int main(){
void * _Owner p = nullptr;
_Owner auto pX = make();
p = pX; //warning
}
</code></pre>

<p>When the object is created on the stack, we can implement a destructor.</p>

<p><strong>Sample - Implementing a destructor</strong></p>
Expand Down Expand Up @@ -440,7 +461,7 @@ <h3 id="toc_7">Deleting Owner Pointers</h3>

<p>But I think this is quite uncommon.</p>

<h2 id="toc_8">Flow analysis</h2>
<h2 id="toc_9">Flow analysis</h2>

<p>Let&#39;s revisit our first example:</p>

Expand Down Expand Up @@ -781,7 +802,7 @@ <h4>Null and Not-Null state</h4>

<h4>Zero and Not-Zero state</h4>

<p>The <strong>zero</strong> state is used for non-owner objects to complement and support uninitialized checks.</p>
<p>The <strong>zero</strong> state is used for non-pointer objects to complement and support uninitialized checks.</p>

<p><strong>Sample - The zero state</strong></p>

Expand Down Expand Up @@ -810,7 +831,7 @@ <h4>static_set</h4>
close(server_socket);
</code></pre>

<p>The <strong>not-zero</strong> state is used for non-owner objects to indicate the value if not zero.</p>
<p>The <strong>not-zero</strong> state is used in non-pointers objects to indicate the value is not zero.</p>

<p><strong>any</strong> is a alias for <strong>zero or not-zero</strong> state.</p>

Expand Down Expand Up @@ -847,7 +868,7 @@ <h4>static_set</h4>
}
</code></pre>

<h3 id="toc_9">assert is a statement</h3>
<h3 id="toc_10">assert is a statement</h3>

<p>When possible we can use assert that works both as static information and runtime check in debug.</p>

Expand Down Expand Up @@ -883,7 +904,7 @@ <h3 id="toc_9">assert is a statement</h3>
}
</code></pre>

<h2 id="toc_10">Ownership Feature Strategy (Inspired by stdbool.h)</h2>
<h2 id="toc_11">Ownership Feature Strategy (Inspired by stdbool.h)</h2>

<p>If the compiler supports ownership checks and qualifiers such as _Owner, _View, _Obj_view, etc., it must define <code>__STDC_OWNERSHIP__</code>. </p>

Expand Down

0 comments on commit 72a7adf

Please sign in to comment.