2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static heim_base_atomic_type tidglobal
= HEIM_TID_USER
;
43 heim_base_atomic_type ref_cnt
;
44 HEIM_TAILQ_ENTRY(heim_base
) autorel
;
45 heim_auto_release_t autorelpool
;
46 uintptr_t isaextra
[3];
49 /* specialized version of base */
50 struct heim_base_mem
{
52 heim_base_atomic_type ref_cnt
;
53 HEIM_TAILQ_ENTRY(heim_base
) autorel
;
54 heim_auto_release_t autorelpool
;
56 void (*dealloc
)(void *);
57 uintptr_t isaextra
[1];
60 #define PTR2BASE(ptr) (((struct heim_base *)ptr) - 1)
61 #define BASE2PTR(ptr) ((void *)(((struct heim_base *)ptr) + 1))
63 #ifdef HEIM_BASE_NEED_ATOMIC_MUTEX
64 HEIMDAL_MUTEX _heim_base_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
68 * Auto release structure
71 struct heim_auto_release
{
72 HEIM_TAILQ_HEAD(, heim_base
) pool
;
73 HEIMDAL_MUTEX pool_mutex
;
74 struct heim_auto_release
*parent
;
79 * Retain object (i.e., take a reference)
81 * @param object to be released, NULL is ok
83 * @return the same object as passed in
87 heim_retain(void *ptr
)
89 struct heim_base
*p
= PTR2BASE(ptr
);
91 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
94 if (p
->ref_cnt
== heim_base_atomic_max
)
97 if ((heim_base_atomic_inc(&p
->ref_cnt
) - 1) == 0)
98 heim_abort("resurection");
103 * Release object, free if reference count reaches zero
105 * @param object to be released
109 heim_release(void *ptr
)
111 heim_base_atomic_type old
;
112 struct heim_base
*p
= PTR2BASE(ptr
);
114 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
117 if (p
->ref_cnt
== heim_base_atomic_max
)
120 old
= heim_base_atomic_dec(&p
->ref_cnt
) + 1;
126 heim_auto_release_t ar
= p
->autorelpool
;
127 /* remove from autorel pool list */
129 p
->autorelpool
= NULL
;
130 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
131 HEIM_TAILQ_REMOVE(&ar
->pool
, p
, autorel
);
132 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
135 p
->isa
->dealloc(ptr
);
138 heim_abort("over release");
142 * If used require wrapped in autorelease pool
146 heim_description(heim_object_t ptr
)
148 struct heim_base
*p
= PTR2BASE(ptr
);
149 if (p
->isa
->desc
== NULL
)
150 return heim_auto_release(heim_string_ref_create(p
->isa
->name
, NULL
));
151 return heim_auto_release(p
->isa
->desc(ptr
));
156 _heim_make_permanent(heim_object_t ptr
)
158 struct heim_base
*p
= PTR2BASE(ptr
);
159 p
->ref_cnt
= heim_base_atomic_max
;
163 static heim_type_t tagged_isa
[9] = {
164 &_heim_number_object
,
178 _heim_get_isa(heim_object_t ptr
)
181 if (heim_base_is_tagged(ptr
)) {
182 if (heim_base_is_tagged_object(ptr
))
183 return tagged_isa
[heim_base_tagged_object_tid(ptr
)];
184 heim_abort("not a supported tagged type");
191 * Get type ID of object
193 * @param object object to get type id of
195 * @return type id of object
199 heim_get_tid(heim_object_t ptr
)
201 heim_type_t isa
= _heim_get_isa(ptr
);
206 * Get hash value of object
208 * @param object object to get hash value for
210 * @return a hash value
214 heim_get_hash(heim_object_t ptr
)
216 heim_type_t isa
= _heim_get_isa(ptr
);
218 return isa
->hash(ptr
);
219 return (unsigned long)ptr
;
223 * Compare two objects, returns 0 if equal, can use used for qsort()
226 * @param a first object to compare
227 * @param b first object to compare
229 * @return 0 if objects are equal
233 heim_cmp(heim_object_t a
, heim_object_t b
)
238 ta
= heim_get_tid(a
);
239 tb
= heim_get_tid(b
);
244 isa
= _heim_get_isa(a
);
247 return isa
->cmp(a
, b
);
249 return (uintptr_t)a
- (uintptr_t)b
;
253 * Private - allocates an memory object
257 memory_dealloc(void *ptr
)
259 struct heim_base_mem
*p
= (struct heim_base_mem
*)PTR2BASE(ptr
);
264 struct heim_type_data memory_object
= {
276 * Allocate memory for an object of anonymous type
278 * @param size size of object to be allocated
279 * @param name name of ad-hoc type
280 * @param dealloc destructor function
282 * Objects allocated with this interface do not serialize.
284 * @return allocated object
288 heim_alloc(size_t size
, const char *name
, heim_type_dealloc dealloc
)
290 /* XXX use posix_memalign */
292 struct heim_base_mem
*p
= calloc(1, size
+ sizeof(*p
));
295 p
->isa
= &memory_object
;
298 p
->dealloc
= dealloc
;
303 _heim_create_type(const char *name
,
305 heim_type_dealloc dealloc
,
309 heim_type_description desc
)
313 type
= calloc(1, sizeof(*type
));
317 type
->tid
= heim_base_atomic_inc(&tidglobal
);
320 type
->dealloc
= dealloc
;
330 _heim_alloc_object(heim_type_t type
, size_t size
)
332 /* XXX should use posix_memalign */
333 struct heim_base
*p
= calloc(1, size
+ sizeof(*p
));
343 _heim_get_isaextra(heim_object_t ptr
, size_t idx
)
345 struct heim_base
*p
= (struct heim_base
*)PTR2BASE(ptr
);
347 heim_assert(ptr
!= NULL
, "internal error");
348 if (p
->isa
== &memory_object
)
350 heim_assert(idx
< 3, "invalid private heim_base extra data index");
351 return &p
->isaextra
[idx
];
355 _heim_type_get_tid(heim_type_t type
)
361 * Call func once and only once
363 * @param once pointer to a heim_base_once_t
364 * @param ctx context passed to func
365 * @param func function to be called
369 heim_base_once_f(heim_base_once_t
*once
, void *ctx
, void (*func
)(void *))
371 #ifdef HAVE_DISPATCH_DISPATCH_H
372 dispatch_once_f(once
, ctx
, func
);
374 static HEIMDAL_MUTEX mutex
= HEIMDAL_MUTEX_INITIALIZER
;
375 HEIMDAL_MUTEX_lock(&mutex
);
378 HEIMDAL_MUTEX_unlock(&mutex
);
380 HEIMDAL_MUTEX_lock(&mutex
);
382 HEIMDAL_MUTEX_unlock(&mutex
);
383 } else if (*once
== 2) {
384 HEIMDAL_MUTEX_unlock(&mutex
);
386 HEIMDAL_MUTEX_unlock(&mutex
);
388 struct timeval tv
= { 0, 1000 };
389 select(0, NULL
, NULL
, NULL
, &tv
);
390 HEIMDAL_MUTEX_lock(&mutex
);
393 HEIMDAL_MUTEX_unlock(&mutex
);
395 HEIMDAL_MUTEX_unlock(&mutex
);
401 * Abort and log the failure (using syslog)
405 heim_abort(const char *fmt
, ...)
409 heim_abortv(fmt
, ap
);
414 * Abort and log the failure (using syslog)
418 heim_abortv(const char *fmt
, va_list ap
)
420 static char str
[1024];
422 vsnprintf(str
, sizeof(str
), fmt
, ap
);
423 syslog(LOG_ERR
, "heim_abort: %s", str
);
431 static int ar_created
= 0;
432 static HEIMDAL_thread_key ar_key
;
435 struct heim_auto_release
*head
;
436 struct heim_auto_release
*current
;
437 HEIMDAL_MUTEX tls_mutex
;
441 ar_tls_delete(void *ptr
)
443 struct ar_tls
*tls
= ptr
;
445 heim_release(tls
->head
);
450 init_ar_tls(void *ptr
)
453 HEIMDAL_key_create(&ar_key
, ar_tls_delete
, ret
);
458 static struct ar_tls
*
461 static heim_base_once_t once
= HEIM_BASE_ONCE_INIT
;
465 heim_base_once_f(&once
, NULL
, init_ar_tls
);
469 arp
= HEIMDAL_getspecific(ar_key
);
472 arp
= calloc(1, sizeof(*arp
));
475 HEIMDAL_setspecific(ar_key
, arp
, ret
);
486 autorel_dealloc(void *ptr
)
488 heim_auto_release_t ar
= ptr
;
493 heim_abort("autorelease pool released on thread w/o autorelease inited");
495 heim_auto_release_drain(ar
);
497 if (!HEIM_TAILQ_EMPTY(&ar
->pool
))
498 heim_abort("pool not empty after draining");
500 HEIMDAL_MUTEX_lock(&tls
->tls_mutex
);
501 if (tls
->current
!= ptr
)
502 heim_abort("autorelease not releaseing top pool");
504 if (tls
->current
!= tls
->head
)
505 tls
->current
= ar
->parent
;
506 HEIMDAL_MUTEX_unlock(&tls
->tls_mutex
);
510 autorel_cmp(void *a
, void *b
)
516 autorel_hash(void *ptr
)
518 return (unsigned long)ptr
;
522 static struct heim_type_data _heim_autorel_object
= {
523 HEIM_TID_AUTORELEASE
,
534 * Create thread-specific object auto-release pool
536 * Objects placed on the per-thread auto-release pool (with
537 * heim_auto_release()) can be released in one fell swoop by calling
538 * heim_auto_release_drain().
542 heim_auto_release_create(void)
544 struct ar_tls
*tls
= autorel_tls();
545 heim_auto_release_t ar
;
548 heim_abort("Failed to create/get autorelease head");
550 ar
= _heim_alloc_object(&_heim_autorel_object
, sizeof(struct heim_auto_release
));
552 HEIMDAL_MUTEX_lock(&tls
->tls_mutex
);
553 if (tls
->head
== NULL
)
555 ar
->parent
= tls
->current
;
557 HEIMDAL_MUTEX_unlock(&tls
->tls_mutex
);
564 * Place the current object on the thread's auto-release pool
570 heim_auto_release(heim_object_t ptr
)
572 struct heim_base
*p
= PTR2BASE(ptr
);
573 struct ar_tls
*tls
= autorel_tls();
574 heim_auto_release_t ar
;
576 if (ptr
== NULL
|| heim_base_is_tagged(ptr
))
579 /* drop from old pool */
580 if ((ar
= p
->autorelpool
) != NULL
) {
581 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
582 HEIM_TAILQ_REMOVE(&ar
->pool
, p
, autorel
);
583 p
->autorelpool
= NULL
;
584 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
587 if (tls
== NULL
|| (ar
= tls
->current
) == NULL
)
588 heim_abort("no auto relase pool in place, would leak");
590 HEIMDAL_MUTEX_lock(&ar
->pool_mutex
);
591 HEIM_TAILQ_INSERT_HEAD(&ar
->pool
, p
, autorel
);
593 HEIMDAL_MUTEX_unlock(&ar
->pool_mutex
);
599 * Release all objects on the given auto-release pool
603 heim_auto_release_drain(heim_auto_release_t autorel
)
607 /* release all elements on the tail queue */
609 HEIMDAL_MUTEX_lock(&autorel
->pool_mutex
);
610 while(!HEIM_TAILQ_EMPTY(&autorel
->pool
)) {
611 obj
= HEIM_TAILQ_FIRST(&autorel
->pool
);
612 HEIMDAL_MUTEX_unlock(&autorel
->pool_mutex
);
613 heim_release(BASE2PTR(obj
));
614 HEIMDAL_MUTEX_lock(&autorel
->pool_mutex
);
616 HEIMDAL_MUTEX_unlock(&autorel
->pool_mutex
);
620 * Helper for heim_path_vget() and heim_path_delete(). On success
621 * outputs the node named by the path and the parent node and key
622 * (useful for heim_path_delete()).
626 heim_path_vget2(heim_object_t ptr
, heim_object_t
*parent
, heim_object_t
*key
,
627 heim_error_t
*error
, va_list ap
)
629 heim_object_t path_element
;
630 heim_object_t node
, next_node
;
631 heim_tid_t node_type
;
638 for (node
= ptr
; node
!= NULL
; ) {
639 path_element
= va_arg(ap
, heim_object_t
);
640 if (path_element
== NULL
) {
646 node_type
= heim_get_tid(node
);
654 heim_abort("heim_path_get() only operates on container types");
658 if (node_type
== HEIM_TID_DICT
) {
659 next_node
= heim_dict_get_value(node
, path_element
);
660 } else if (node_type
== HEIM_TID_DB
) {
661 next_node
= _heim_db_get_value(node
, NULL
, path_element
, NULL
);
662 } else if (node_type
== HEIM_TID_ARRAY
) {
665 if (heim_get_tid(path_element
) == HEIM_TID_NUMBER
)
666 idx
= heim_number_get_int(path_element
);
669 *error
= heim_error_create(EINVAL
,
670 "heim_path_get() path elements "
671 "for array nodes must be "
672 "numeric and positive");
675 next_node
= heim_array_get_value(node
, idx
);
678 *error
= heim_error_create(EINVAL
,
679 "heim_path_get() node in path "
680 "not a container type");
689 * Get a node in a heim_object tree by path
692 * @param error error (output)
693 * @param ap NULL-terminated va_list of heim_object_ts that form a path
695 * @return object (not retained) if found
697 * @addtogroup heimbase
701 heim_path_vget(heim_object_t ptr
, heim_error_t
*error
, va_list ap
)
705 return heim_path_vget2(ptr
, &p
, &k
, error
, ap
);
709 * Get a node in a tree by path, with retained reference
712 * @param error error (output)
713 * @param ap NULL-terminated va_list of heim_object_ts that form a path
715 * @return retained object if found
717 * @addtogroup heimbase
721 heim_path_vcopy(heim_object_t ptr
, heim_error_t
*error
, va_list ap
)
725 return heim_retain(heim_path_vget2(ptr
, &p
, &k
, error
, ap
));
729 * Get a node in a tree by path
732 * @param error error (output)
733 * @param ... NULL-terminated va_list of heim_object_ts that form a path
735 * @return object (not retained) if found
737 * @addtogroup heimbase
741 heim_path_get(heim_object_t ptr
, heim_error_t
*error
, ...)
751 o
= heim_path_vget2(ptr
, &p
, &k
, error
, ap
);
757 * Get a node in a tree by path, with retained reference
760 * @param error error (output)
761 * @param ... NULL-terminated va_list of heim_object_ts that form a path
763 * @return retained object if found
765 * @addtogroup heimbase
769 heim_path_copy(heim_object_t ptr
, heim_error_t
*error
, ...)
779 o
= heim_retain(heim_path_vget2(ptr
, &p
, &k
, error
, ap
));
785 * Create a path in a heim_object_t tree
787 * @param ptr the tree
788 * @param size the size of the heim_dict_t nodes to be created
789 * @param leaf leaf node to be added, if any
790 * @param error error (output)
791 * @param ap NULL-terminated of path component objects
793 * Create a path of heim_dict_t interior nodes in a given heim_object_t
794 * tree, as necessary, and set/replace a leaf, if given (if leaf is NULL
795 * then the leaf is not deleted).
797 * @return 0 on success, else a system error
799 * @addtogroup heimbase
803 heim_path_vcreate(heim_object_t ptr
, size_t size
, heim_object_t leaf
,
804 heim_error_t
*error
, va_list ap
)
806 heim_object_t path_element
= va_arg(ap
, heim_object_t
);
807 heim_object_t next_path_element
= NULL
;
808 heim_object_t node
= ptr
;
809 heim_object_t next_node
= NULL
;
810 heim_tid_t node_type
;
814 heim_abort("heim_path_vcreate() does not create root nodes");
816 while (path_element
!= NULL
) {
817 next_path_element
= va_arg(ap
, heim_object_t
);
818 node_type
= heim_get_tid(node
);
820 if (node_type
== HEIM_TID_DICT
) {
821 next_node
= heim_dict_get_value(node
, path_element
);
822 } else if (node_type
== HEIM_TID_ARRAY
) {
825 if (heim_get_tid(path_element
) == HEIM_TID_NUMBER
)
826 idx
= heim_number_get_int(path_element
);
829 *error
= heim_error_create(EINVAL
,
830 "heim_path() path elements for "
831 "array nodes must be numeric "
835 if (idx
< heim_array_get_length(node
))
836 next_node
= heim_array_get_value(node
, idx
);
839 } else if (node_type
== HEIM_TID_DB
&& next_path_element
!= NULL
) {
841 *error
= heim_error_create(EINVAL
, "Interior node is a DB");
845 if (next_path_element
== NULL
)
848 /* Create missing interior node */
849 if (next_node
== NULL
) {
850 next_node
= heim_dict_create(size
); /* no arrays or DBs, just dicts */
851 if (next_node
== NULL
) {
856 if (node_type
== HEIM_TID_DICT
) {
857 ret
= heim_dict_set_value(node
, path_element
, next_node
);
858 } else if (node_type
== HEIM_TID_ARRAY
&&
859 heim_number_get_int(path_element
) <= heim_array_get_length(node
)) {
860 ret
= heim_array_insert_value(node
,
861 heim_number_get_int(path_element
),
866 *error
= heim_error_create(ret
, "Node in path not a "
870 heim_release(next_node
);
875 path_element
= next_path_element
;
880 if (path_element
== NULL
)
885 if (node_type
== HEIM_TID_DICT
)
886 ret
= heim_dict_set_value(node
, path_element
, leaf
);
888 ret
= heim_array_insert_value(node
,
889 heim_number_get_int(path_element
),
895 if (error
&& !*error
) {
897 *error
= heim_error_create_enomem();
899 *error
= heim_error_create(ret
, "Could not set "
906 * Create a path in a heim_object_t tree
908 * @param ptr the tree
909 * @param size the size of the heim_dict_t nodes to be created
910 * @param leaf leaf node to be added, if any
911 * @param error error (output)
912 * @param ... NULL-terminated list of path component objects
914 * Create a path of heim_dict_t interior nodes in a given heim_object_t
915 * tree, as necessary, and set/replace a leaf, if given (if leaf is NULL
916 * then the leaf is not deleted).
918 * @return 0 on success, else a system error
920 * @addtogroup heimbase
924 heim_path_create(heim_object_t ptr
, size_t size
, heim_object_t leaf
,
925 heim_error_t
*error
, ...)
931 ret
= heim_path_vcreate(ptr
, size
, leaf
, error
, ap
);
937 * Delete leaf node named by a path in a heim_object_t tree
939 * @param ptr the tree
940 * @param error error (output)
941 * @param ap NULL-terminated list of path component objects
943 * @addtogroup heimbase
947 heim_path_vdelete(heim_object_t ptr
, heim_error_t
*error
, va_list ap
)
949 heim_object_t parent
, key
, child
;
951 child
= heim_path_vget2(ptr
, &parent
, &key
, error
, ap
);
953 if (heim_get_tid(parent
) == HEIM_TID_DICT
)
954 heim_dict_delete_key(parent
, key
);
955 else if (heim_get_tid(parent
) == HEIM_TID_DB
)
956 heim_db_delete_key(parent
, NULL
, key
, error
);
957 else if (heim_get_tid(parent
) == HEIM_TID_ARRAY
)
958 heim_array_delete_value(parent
, heim_number_get_int(key
));
964 * Delete leaf node named by a path in a heim_object_t tree
966 * @param ptr the tree
967 * @param error error (output)
968 * @param ap NULL-terminated list of path component objects
970 * @addtogroup heimbase
974 heim_path_delete(heim_object_t ptr
, heim_error_t
*error
, ...)
979 heim_path_vdelete(ptr
, error
, ap
);