| 1 | struct {
|
|---|
| 2 | float f;
|
|---|
| 3 | } Foo;
|
|---|
| 4 |
|
|---|
| 5 | struct {
|
|---|
| 6 | Foo *foo;
|
|---|
| 7 | } Bar;
|
|---|
| 8 |
|
|---|
| 9 | Foo *psFooAlloc () {
|
|---|
| 10 | Foo *foo = (Foo *) psAlloc (1);
|
|---|
| 11 | return (foo);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | psFooFree (Foo *foo) {
|
|---|
| 15 | psFree (foo);
|
|---|
| 16 | return;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | Bar *psBarAlloc () {
|
|---|
| 20 | Bar *bar = (Bar *) psAlloc (1);
|
|---|
| 21 | bar[0].foo = NULL;
|
|---|
| 22 | return (bar);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | psBarFree (Bar *bar) {
|
|---|
| 26 | psFooFree (bar[0].foo);
|
|---|
| 27 | psFree (bar);
|
|---|
| 28 | return;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | Bar *psBarSet (Foo *foo) {
|
|---|
| 32 | Bar *bar = NULL;
|
|---|
| 33 | bar = psBarAlloc ();
|
|---|
| 34 | bar[0].foo = foo;
|
|---|
| 35 | IncrRef (foo);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | main () {
|
|---|
| 39 |
|
|---|
| 40 | Foo *foo = NULL;
|
|---|
| 41 | Bar *bar = NULL;
|
|---|
| 42 |
|
|---|
| 43 | foo = psFooAlloc (); // Nref(foo) = 1
|
|---|
| 44 | bar = psBarSet (foo); // Nref(foo) = 2
|
|---|
| 45 | psFooFree (foo); // Nref(foo) = 1
|
|---|
| 46 | psBarFree (bar); // Nref(foo) = 0
|
|---|
| 47 | }
|
|---|