Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_object_memory_associative_array.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Implementation of afw_object_associative_array interface in memory
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 /* Declares and rti/inf defines for interface afw_object_associative_array */
18 #define AFW_IMPLEMENTATION_ID "memory"
20 
21 /* Self */
24 
25  /* Hash table with key of key and value of object instance. */
26  apr_hash_t *objects;
27 
28  /* Reference_count starting at 1 on create. */
29  AFW_ATOMIC afw_integer_t reference_count;
30 
32 
33 
34 
35 /* Create memory object associative array. */
38  const afw_pool_t *p, afw_xctx_t *xctx)
39 {
41  const afw_pool_t *new_p;
42 
43  /* Create new pool for object associative array. */
44  new_p = afw_pool_create(p, xctx);
45 
46  /* Allocate self and initialize. */
47  self = afw_pool_calloc_type(new_p,
49  self->pub.inf = &impl_afw_object_associative_array_inf;
50  self->pub.p = new_p;
51  self->reference_count = 1;
52  self->objects = apr_hash_make(afw_pool_get_apr_pool(new_p));
53 
54  /* Return new instance. */
55  return (afw_object_associative_array_t *)self;
56 }
57 
58 
59 /*
60  * Implementation of method release of interface afw_object_associative_array.
61  */
62 void
63 impl_afw_object_associative_array_release (
64  const afw_object_associative_array_t * instance,
65  afw_xctx_t *xctx)
66 {
67  /* Assign instance pointer to self. */
70 
71  apr_hash_index_t *hi;
72  const void *key;
73  apr_ssize_t klen;
74  const afw_object_t *object;
75 
76  /* Decrement reference count and release object and array's pool if zero. */
77  if (afw_atomic_integer_decrement(&self->reference_count) == 0) {
78  for (hi = apr_hash_first(
79  afw_pool_get_apr_pool(instance->p), self->objects);
80  hi;
81  hi = apr_hash_next(hi))
82  {
83  apr_hash_this(hi, &key, &klen, (void **)&object);
84  afw_object_release(object, xctx);
85  }
86 
87  afw_pool_release(instance->p, xctx);
88  }
89 }
90 
91 
92 
93 /*
94  * Implementation of method add_reference of interface afw_object_associative_array.
95  */
96 void
97 impl_afw_object_associative_array_add_reference (
98  const afw_object_associative_array_t * instance,
99  afw_xctx_t *xctx)
100 {
101  /* Assign instance pointer to self. */
104 
105  /* Increment reference count. */
106  afw_atomic_integer_increment(&self->reference_count);
107 }
108 
109 
110 static void
111 impl_release_object(void *data, void *data2,
112  const afw_pool_t *p, afw_xctx_t *xctx)
113 {
114  afw_object_release((const afw_object_t *)data, xctx);
115 }
116 
117 
118 /*
119  * Implementation of method get of interface afw_object_associative_array.
120  */
121 const afw_object_t *
122 impl_afw_object_associative_array_get (
123  const afw_object_associative_array_t * instance,
124  const afw_utf8_t * key,
125  afw_xctx_t *xctx)
126 {
127  /* Assign instance pointer to self. */
130 
131  const afw_object_t *object;
132 
133  /* Get object associated with key. */
134  object = apr_hash_get(self->objects, key->s, key->len);
135 
136  /*
137  * If object found, add reference and register automatic release when
138  * xctx is released.
139  */
140  if (object) {
141  afw_object_add_reference(object, xctx);
142  afw_pool_register_cleanup_before(xctx->p, (void *)object, NULL,
143  impl_release_object, xctx);
144  }
145 
146  /* Return result. */
147  return object;
148 }
149 
150 
151 
152 /*
153  * Implementation of method get_reference of interface afw_object_associative_array.
154  */
155 const afw_object_t *
156 impl_afw_object_associative_array_get_reference (
157  const afw_object_associative_array_t * instance,
158  const afw_utf8_t * key,
159  afw_xctx_t *xctx)
160 {
161  /* Assign instance pointer to self. */
164 
165  const afw_object_t *object;
166 
167  /* Get object associated with key. */
168  object = apr_hash_get(self->objects, key->s, key->len);
169 
170  /* If object found, add reference. */
171  if (object) {
172  afw_object_add_reference(object, xctx);
173  }
174 
175  /* Return result. */
176  return object;
177 }
178 
179 
180 
181 /*
182  * Implementation of method for_each of interface afw_object_associative_array.
183  */
184 void
186  const afw_object_associative_array_t * instance,
187  void * context,
188  afw_object_cb_t cb,
189  afw_xctx_t *xctx)
190 {
192  AFW_THROW_ERROR_Z(general, "Method not implemented.", xctx);
193 
194 }
195 
196 
197 
198 /*
199  * Implementation of method set of interface afw_object_associative_array.
200  */
201 void
202 impl_afw_object_associative_array_set (
203  const afw_object_associative_array_t * instance,
204  const afw_utf8_t * key,
205  const afw_object_t * object,
206  afw_xctx_t *xctx)
207 {
208  /* Assign instance pointer to self. */
211 
212  const afw_object_t *existing;
213 
214  /* If an object is associated with key, call its release(). */
215  existing = apr_hash_get(self->objects, key->s, key->len);
216  if (existing) {
217  afw_object_release(object, xctx);
218  }
219 
220  /* If object passed, add reference. */
221  if (object) {
222  afw_object_add_reference(object, xctx);
223  }
224 
225  /* Set/remove association. */
226  apr_hash_set(self->objects, key->s, key->len, object);
227 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
afw_integer_t afw_atomic_integer_decrement(AFW_ATOMIC afw_integer_t *mem)
Integer atomic decrement.
Definition: afw_atomic.h:96
afw_integer_t afw_atomic_integer_increment(AFW_ATOMIC afw_integer_t *mem)
Integer atomic increment.
Definition: afw_atomic.h:127
afw_boolean_t(* afw_object_cb_t)(const afw_object_t *object, void *context, afw_xctx_t *xctx)
Typedef for afw_adaptor_session_object callback.
Definition: afw_common.h:1176
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
void impl_afw_object_associative_array_for_each(const afw_object_associative_array_t *instance, void *context, afw_object_cb_t cb, afw_xctx_t *xctx)
#define afw_object_add_reference(instance, xctx)
Call method add_reference of interface afw_object.
#define afw_object_release(instance, xctx)
Call method release of interface afw_object.
afw_object_memory_associative_array_create(const afw_pool_t *p, afw_xctx_t *xctx)
Create memory object associative array.
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
#define afw_pool_release(instance, xctx)
Call method release of interface afw_pool.
#define afw_pool_register_cleanup_before(instance, data, data2, cleanup, xctx)
Call method register_cleanup_before of interface afw_pool.
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
const afw_pool_t * afw_pool_create(const afw_pool_t *parent, afw_xctx_t *xctx)
Create a new pool.
Interface afw_object_associative_array public struct.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.