1 /* $NetBSD: heimbase.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $ */
4 * Copyright (c) 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static heim_base_atomic_type tidglobal
= HEIM_TID_USER
;
45 heim_base_atomic_type ref_cnt
;
46 HEIM_TAILQ_ENTRY(heim_base
) autorel
;
47 heim_auto_release_t autorelpool
;
48 uintptr_t isaextra
[3];
51 /* specialized version of base */
52 struct heim_base_mem
{
54 heim_base_atomic_type ref_cnt
;
55 HEIM_TAILQ_ENTRY(heim_base
) autorel
;
56 heim_auto_release_t autorelpool
;
58 void (*dealloc
)(void *);
59 uintptr_t isaextra
[1];
62 #define PTR2BASE(ptr) (((struct heim_base *)ptr) - 1)
63 #define BASE2PTR(ptr) ((void *)(((struct heim_base *)ptr) + 1))
65 #ifdef HEIM_BASE_NEED_ATOMIC_MUTEX
66 HEIMDAL_MUTEX _heim_base_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
70 * Auto release structure
73 struct heim_auto_release
{
74 HEIM_TAILQ_HEAD(, heim_base
) pool
;
75 HEIMDAL_MUTEX pool_mutex
;
76 struct heim_auto_release
*parent
;
83 * @param object to be released, NULL is ok
85 * @return the same object as passed in
89 heim_retain(void *ptr
)
91 struct heim_base
*p
= PTR2BASE(ptr
);
93 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
96 if (p
->ref_cnt
== heim_base_atomic_max
)
99 if ((heim_base_atomic_inc(&p
->ref_cnt
) - 1) == 0)
100 heim_abort("resurection");
105 * Release object, free is reference count reaches zero
107 * @param object to be released
111 heim_release(void *ptr
)
113 heim_base_atomic_type old
;
114 struct heim_base
*p
= PTR2BASE(ptr
);
116 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
119 if (p
->ref_cnt
== heim_base_atomic_max
)
122 old
= heim_base_atomic_dec(&p
->ref_cnt
) + 1;
128 heim_auto_release_t ar
= p
->autorelpool
;
129 /* remove from autorel pool list */
131 p
->autorelpool
= NULL
;
132 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
133 HEIM_TAILQ_REMOVE(&ar
->pool
, p
, autorel
);
134 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
137 p
->isa
->dealloc(ptr
);
140 heim_abort("over release");
143 static heim_type_t tagged_isa
[9] = {
144 &_heim_number_object
,
158 _heim_get_isa(heim_object_t ptr
)
161 if (heim_base_is_tagged(ptr
)) {
162 if (heim_base_is_tagged_object(ptr
))
163 return tagged_isa
[heim_base_tagged_object_tid(ptr
)];
164 heim_abort("not a supported tagged type");
171 * Get type ID of object
173 * @param object object to get type id of
175 * @return type id of object
179 heim_get_tid(heim_object_t ptr
)
181 heim_type_t isa
= _heim_get_isa(ptr
);
186 * Get hash value of object
188 * @param object object to get hash value for
190 * @return a hash value
194 heim_get_hash(heim_object_t ptr
)
196 heim_type_t isa
= _heim_get_isa(ptr
);
198 return isa
->hash(ptr
);
199 return (unsigned long)ptr
;
203 * Compare two objects, returns 0 if equal, can use used for qsort()
206 * @param a first object to compare
207 * @param b first object to compare
209 * @return 0 if objects are equal
213 heim_cmp(heim_object_t a
, heim_object_t b
)
218 ta
= heim_get_tid(a
);
219 tb
= heim_get_tid(b
);
224 isa
= _heim_get_isa(a
);
227 return isa
->cmp(a
, b
);
229 return (uintptr_t)a
- (uintptr_t)b
;
233 * Private - allocates an memory object
237 memory_dealloc(void *ptr
)
239 struct heim_base_mem
*p
= (struct heim_base_mem
*)PTR2BASE(ptr
);
244 struct heim_type_data memory_object
= {
255 heim_alloc(size_t size
, const char *name
, heim_type_dealloc dealloc
)
257 /* XXX use posix_memalign */
259 struct heim_base_mem
*p
= calloc(1, size
+ sizeof(*p
));
262 p
->isa
= &memory_object
;
265 p
->dealloc
= dealloc
;
270 _heim_create_type(const char *name
,
272 heim_type_dealloc dealloc
,
279 type
= calloc(1, sizeof(*type
));
283 type
->tid
= heim_base_atomic_inc(&tidglobal
);
286 type
->dealloc
= dealloc
;
295 _heim_alloc_object(heim_type_t type
, size_t size
)
297 /* XXX should use posix_memalign */
298 struct heim_base
*p
= calloc(1, size
+ sizeof(*p
));
308 _heim_type_get_tid(heim_type_t type
)
314 * Call func once and only once
316 * @param once pointer to a heim_base_once_t
317 * @param ctx context passed to func
318 * @param func function to be called
322 heim_base_once_f(heim_base_once_t
*once
, void *ctx
, void (*func
)(void *))
324 #ifdef HAVE_DISPATCH_DISPATCH_H
325 dispatch_once_f(once
, ctx
, func
);
327 static HEIMDAL_MUTEX mutex
= HEIMDAL_MUTEX_INITIALIZER
;
328 HEIMDAL_MUTEX_lock(&mutex
);
331 HEIMDAL_MUTEX_unlock(&mutex
);
333 HEIMDAL_MUTEX_lock(&mutex
);
335 HEIMDAL_MUTEX_unlock(&mutex
);
336 } else if (*once
== 2) {
337 HEIMDAL_MUTEX_unlock(&mutex
);
339 HEIMDAL_MUTEX_unlock(&mutex
);
341 struct timeval tv
= { 0, 1000 };
342 select(0, NULL
, NULL
, NULL
, &tv
);
343 HEIMDAL_MUTEX_lock(&mutex
);
346 HEIMDAL_MUTEX_unlock(&mutex
);
348 HEIMDAL_MUTEX_unlock(&mutex
);
354 * Abort and log the failure (using syslog)
358 heim_abort(const char *fmt
, ...)
362 heim_abortv(fmt
, ap
);
367 * Abort and log the failure (using syslog)
371 heim_abortv(const char *fmt
, va_list ap
)
373 static char str
[1024];
375 vsnprintf(str
, sizeof(str
), fmt
, ap
);
376 syslog(LOG_ERR
, "heim_abort: %s", str
);
384 static int ar_created
= 0;
385 static HEIMDAL_thread_key ar_key
;
388 struct heim_auto_release
*head
;
389 struct heim_auto_release
*current
;
390 HEIMDAL_MUTEX tls_mutex
;
394 ar_tls_delete(void *ptr
)
396 struct ar_tls
*tls
= ptr
;
398 heim_release(tls
->head
);
403 init_ar_tls(void *ptr
)
406 HEIMDAL_key_create(&ar_key
, ar_tls_delete
, ret
);
411 static struct ar_tls
*
414 static heim_base_once_t once
= HEIM_BASE_ONCE_INIT
;
418 heim_base_once_f(&once
, NULL
, init_ar_tls
);
422 arp
= HEIMDAL_getspecific(ar_key
);
425 arp
= calloc(1, sizeof(*arp
));
428 HEIMDAL_setspecific(ar_key
, arp
, ret
);
439 autorel_dealloc(void *ptr
)
441 heim_auto_release_t ar
= ptr
;
446 heim_abort("autorelease pool released on thread w/o autorelease inited");
448 heim_auto_release_drain(ar
);
450 if (!HEIM_TAILQ_EMPTY(&ar
->pool
))
451 heim_abort("pool not empty after draining");
453 HEIMDAL_MUTEX_lock(&tls
->tls_mutex
);
454 if (tls
->current
!= ptr
)
455 heim_abort("autorelease not releaseing top pool");
457 if (tls
->current
!= tls
->head
)
458 tls
->current
= ar
->parent
;
459 HEIMDAL_MUTEX_unlock(&tls
->tls_mutex
);
463 autorel_cmp(void *a
, void *b
)
469 autorel_hash(void *ptr
)
471 return (unsigned long)ptr
;
475 static struct heim_type_data _heim_autorel_object
= {
476 HEIM_TID_AUTORELEASE
,
490 heim_auto_release_create(void)
492 struct ar_tls
*tls
= autorel_tls();
493 heim_auto_release_t ar
;
496 heim_abort("Failed to create/get autorelease head");
498 ar
= _heim_alloc_object(&_heim_autorel_object
, sizeof(struct heim_auto_release
));
500 HEIMDAL_MUTEX_lock(&tls
->tls_mutex
);
501 if (tls
->head
== NULL
)
503 ar
->parent
= tls
->current
;
505 HEIMDAL_MUTEX_unlock(&tls
->tls_mutex
);
512 * Mark the current object as a
516 heim_auto_release(heim_object_t ptr
)
518 struct heim_base
*p
= PTR2BASE(ptr
);
519 struct ar_tls
*tls
= autorel_tls();
520 heim_auto_release_t ar
;
522 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
525 /* drop from old pool */
526 if ((ar
= p
->autorelpool
) != NULL
) {
527 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
528 HEIM_TAILQ_REMOVE(&ar
->pool
, p
, autorel
);
529 p
->autorelpool
= NULL
;
530 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
533 if (tls
== NULL
|| (ar
= tls
->current
) == NULL
)
534 heim_abort("no auto relase pool in place, would leak");
536 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
537 HEIM_TAILQ_INSERT_HEAD(&ar
->pool
, p
, autorel
);
539 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
547 heim_auto_release_drain(heim_auto_release_t autorel
)
551 /* release all elements on the tail queue */
553 HEIMDAL_MUTEX_lock(&autorel
->pool_mutex
);
554 while(!HEIM_TAILQ_EMPTY(&autorel
->pool
)) {
555 obj
= HEIM_TAILQ_FIRST(&autorel
->pool
);
556 HEIMDAL_MUTEX_unlock(&autorel
->pool_mutex
);
557 heim_release(BASE2PTR(obj
));
558 HEIMDAL_MUTEX_lock(&autorel
->pool_mutex
);
560 HEIMDAL_MUTEX_unlock(&autorel
->pool_mutex
);