aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/examples/example-refcount.c
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-08-31 21:08:56 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-08-31 21:11:20 -0800
commit7dd774b32ed3a0a9353ad77e7dd2f89fca9afbaf (patch)
tree49c9fb1393a1e4eac4128e3d710a911219dff0bf /examples/example-refcount.c
parentf3b26aea269e9933732cda78e72eb8d7a4c8c387 (diff)
downloadbindletools-7dd774b32ed3a0a9353ad77e7dd2f89fca9afbaf.tar.gz
bindletools-7dd774b32ed3a0a9353ad77e7dd2f89fca9afbaf.tar.bz2
bindletools-7dd774b32ed3a0a9353ad77e7dd2f89fca9afbaf.tar.xz
bindletools-7dd774b32ed3a0a9353ad77e7dd2f89fca9afbaf.zip
doc: adding man page for bindle_refcount.3
Diffstat (limited to 'examples/example-refcount.c')
-rw-r--r--examples/example-refcount.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/examples/example-refcount.c b/examples/example-refcount.c
new file mode 100644
index 0000000..1464f0f
--- /dev/null
+++ b/examples/example-refcount.c
@@ -0,0 +1,174 @@
1/*
2 * Bindle Binaries Tools
3 * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of David M. Syzdek nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31#define _EXAMPLES_EXAMPLE_REFCOUNT_C 1
32
33
34///////////////
35// //
36// Headers //
37// //
38///////////////
39#pragma mark - Headers
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <stdint.h>
45#include <bindle.h>
46
47
48///////////////////
49// //
50// Definitions //
51// //
52///////////////////
53#pragma mark - Definitions
54
55#undef PROGRAM_NAME
56#define PROGRAM_NAME "example-refcount"
57
58
59//////////////////
60// //
61// Data Types //
62// //
63//////////////////
64#pragma mark - Data Types
65
66struct student
67{
68 bindle_obj_t obj;
69 int year;
70 int grade;
71 char * name;
72};
73
74
75/////////////////
76// //
77// Variables //
78// //
79/////////////////
80#pragma mark - Variables
81
82
83//////////////////
84// //
85// Prototypes //
86// //
87//////////////////
88#pragma mark - Prototypes
89
90int
91main(
92 void );
93
94
95static void
96student_free(
97 void * ptr );
98
99
100static struct student *
101student_init(
102 const char * name,
103 int year );
104
105
106/////////////////
107// //
108// Functions //
109// //
110/////////////////
111#pragma mark - Functions
112
113int
114main(
115 void )
116{
117 struct student * student;
118 if ((student = student_init("John Doe", 5)) == NULL)
119 {
120 fprintf(stderr, "out of virtual memory\n");
121 return(1);
122 };
123 printf("1st reference count: %i\n", bindle_retain_count(&student->obj));
124 bindle_retain(&student->obj);
125 printf("2nd reference count: %i\n", bindle_retain_count(&student->obj));
126 bindle_free(student);
127 printf("3rd reference count: %i\n", bindle_retain_count(&student->obj));
128 bindle_free(student);
129 return(0);
130}
131
132
133void
134student_free(
135 void * ptr )
136{
137 struct student * student;
138 const char * name;
139 if ((student = ptr) == NULL)
140 return;
141 name = ((student->name)) ? student->name : "unknown";
142 printf("Freeing data struct for \"%s\".\n", name);
143 if ((student->name))
144 free(student->name);
145 free(student);
146 return;
147}
148
149
150struct student *
151student_init(
152 const char * name,
153 int year )
154{
155 size_t s;
156 struct student * person;
157 s = sizeof(struct student);
158 if ((person = bindle_alloc(s, &student_free)) == NULL)
159 {
160 fprintf(stderr, "out of virtual memory\n");
161 return(NULL);
162 };
163 person->year = year;
164 if ((person->name = bindle_strdup(name)) == NULL)
165 {
166 fprintf(stderr, "out of virtual memory\n");
167 bindle_free(person);
168 return(NULL);
169 };
170 return(bindle_retain(&person->obj));
171}
172
173/* end of source */
174