Debian Codegraph @

lib/base/heimbase.c

raw
1 /*
2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #include "baselocl.h"
37 #include <syslog.h>
38
39 static heim_base_atomic_type tidglobal = HEIM_TID_USER;
40
41 struct heim_base {
42 heim_type_t isa;
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];
47 };
48
49 /* specialized version of base */
50 struct heim_base_mem {
51 heim_type_t isa;
52 heim_base_atomic_type ref_cnt;
53 HEIM_TAILQ_ENTRY(heim_base) autorel;
54 heim_auto_release_t autorelpool;
55 const char *name;
56 void (*dealloc)(void *);
57 uintptr_t isaextra[1];
58 };
59
60 #define PTR2BASE(ptr) (((struct heim_base *)ptr) - 1)
61 #define BASE2PTR(ptr) ((void *)(((struct heim_base *)ptr) + 1))
62
63 #ifdef HEIM_BASE_NEED_ATOMIC_MUTEX
64 HEIMDAL_MUTEX _heim_base_mutex = HEIMDAL_MUTEX_INITIALIZER;
65 #endif
66
67 /*
68 * Auto release structure
69 */
70
71 struct heim_auto_release {
72 HEIM_TAILQ_HEAD(, heim_base) pool;
73 HEIMDAL_MUTEX pool_mutex;
74 struct heim_auto_release *parent;
75 };
76
77
78 /**
79 * Retain object (i.e., take a reference)
80 *
81 * @param object to be released, NULL is ok
82 *
83 * @return the same object as passed in
84 */
85
86 void *
87 heim_retain(void *ptr)
88 {
89 struct heim_base *p;
90
91 if (ptr == NULL || heim_base_is_tagged(ptr))
92 return ptr;
93
94 p = PTR2BASE(ptr);
95 if (p->ref_cnt == heim_base_atomic_max)
96 return ptr;
97
98 if ((heim_base_atomic_inc(&p->ref_cnt) - 1) == 0)
99 heim_abort("resurection");
100 return ptr;
101 }
102
103 /**
104 * Release object, free if reference count reaches zero
105 *
106 * @param object to be released
107 */
108
109 void
110 heim_release(void *ptr)
111 {
112 heim_base_atomic_type old;
113 struct heim_base *p;
114
115 if (ptr == NULL || heim_base_is_tagged(ptr))
116 return;
117
118 p = PTR2BASE(ptr);
119 if (p->ref_cnt == heim_base_atomic_max)
120 return;
121
122 old = heim_base_atomic_dec(&p->ref_cnt) + 1;
123
124 if (old > 1)
125 return;
126
127 if (old == 1) {
128 heim_auto_release_t ar = p->autorelpool;
129 /* remove from autorel pool list */
130 if (ar) {
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);
135 }
136 if (p->isa->dealloc)
137 p->isa->dealloc(ptr);
138 free(p);
139 } else
140 heim_abort("over release");
141 }
142
143 /**
144 * If used require wrapped in autorelease pool
145 */
146
147 heim_string_t
148 heim_description(heim_object_t ptr)
149 {
150 struct heim_base *p = PTR2BASE(ptr);
151 if (p->isa->desc == NULL)
152 return heim_auto_release(heim_string_ref_create(p->isa->name, NULL));
153 return heim_auto_release(p->isa->desc(ptr));
154 }
155
156
157 void
158 _heim_make_permanent(heim_object_t ptr)
159 {
160 struct heim_base *p = PTR2BASE(ptr);
161 p->ref_cnt = heim_base_atomic_max;
162 }
163
164
165 static heim_type_t tagged_isa[9] = {
166 &_heim_number_object,
167 &_heim_null_object,
168 &_heim_bool_object,
169
170 NULL,
171 NULL,
172 NULL,
173
174 NULL,
175 NULL,
176 NULL
177 };
178
179 heim_type_t
180 _heim_get_isa(heim_object_t ptr)
181 {
182 struct heim_base *p;
183 if (heim_base_is_tagged(ptr)) {
184 if (heim_base_is_tagged_object(ptr))
185 return tagged_isa[heim_base_tagged_object_tid(ptr)];
186 heim_abort("not a supported tagged type");
187 }
188 p = PTR2BASE(ptr);
189 return p->isa;
190 }
191
192 /**
193 * Get type ID of object
194 *
195 * @param object object to get type id of
196 *
197 * @return type id of object
198 */
199
200 heim_tid_t
201 heim_get_tid(heim_object_t ptr)
202 {
203 heim_type_t isa = _heim_get_isa(ptr);
204 return isa->tid;
205 }
206
207 /**
208 * Get hash value of object
209 *
210 * @param object object to get hash value for
211 *
212 * @return a hash value
213 */
214
215 unsigned long
216 heim_get_hash(heim_object_t ptr)
217 {
218 heim_type_t isa = _heim_get_isa(ptr);
219 if (isa->hash)
220 return isa->hash(ptr);
221 return (unsigned long)ptr;
222 }
223
224 /**
225 * Compare two objects, returns 0 if equal, can use used for qsort()
226 * and friends.
227 *
228 * @param a first object to compare
229 * @param b first object to compare
230 *
231 * @return 0 if objects are equal
232 */
233
234 int
235 heim_cmp(heim_object_t a, heim_object_t b)
236 {
237 heim_tid_t ta, tb;
238 heim_type_t isa;
239
240 ta = heim_get_tid(a);
241 tb = heim_get_tid(b);
242
243 if (ta != tb)
244 return ta - tb;
245
246 isa = _heim_get_isa(a);
247
248 if (isa->cmp)
249 return isa->cmp(a, b);
250
251 return (uintptr_t)a - (uintptr_t)b;
252 }
253
254 /*
255 * Private - allocates an memory object
256 */
257
258 static void
259 memory_dealloc(void *ptr)
260 {
261 if (ptr) {
262 struct heim_base_mem *p = (struct heim_base_mem *)PTR2BASE(ptr);
263
264 if (p->dealloc)
265 p->dealloc(ptr);
266 }
267 }
268
269 struct heim_type_data memory_object = {
270 HEIM_TID_MEMORY,
271 "memory-object",
272 NULL,
273 memory_dealloc,
274 NULL,
275 NULL,
276 NULL,
277 NULL
278 };
279
280 /**
281 * Allocate memory for an object of anonymous type
282 *
283 * @param size size of object to be allocated
284 * @param name name of ad-hoc type
285 * @param dealloc destructor function
286 *
287 * Objects allocated with this interface do not serialize.
288 *
289 * @return allocated object
290 */
291
292 void *
293 heim_alloc(size_t size, const char *name, heim_type_dealloc dealloc)
294 {
295 /* XXX use posix_memalign */
296
297 struct heim_base_mem *p = calloc(1, size + sizeof(*p));
298 if (p == NULL)
299 return NULL;
300 p->isa = &memory_object;
301 p->ref_cnt = 1;
302 p->name = name;
303 p->dealloc = dealloc;
304 return BASE2PTR(p);
305 }
306
307 heim_type_t
308 _heim_create_type(const char *name,
309 heim_type_init init,
310 heim_type_dealloc dealloc,
311 heim_type_copy copy,
312 heim_type_cmp cmp,
313 heim_type_hash hash,
314 heim_type_description desc)
315 {
316 heim_type_t type;
317
318 type = calloc(1, sizeof(*type));
319 if (type == NULL)
320 return NULL;
321
322 type->tid = heim_base_atomic_inc(&tidglobal);
323 type->name = name;
324 type->init = init;
325 type->dealloc = dealloc;
326 type->copy = copy;
327 type->cmp = cmp;
328 type->hash = hash;
329 type->desc = desc;
330
331 return type;
332 }
333
334 heim_object_t
335 _heim_alloc_object(heim_type_t type, size_t size)
336 {
337 /* XXX should use posix_memalign */
338 struct heim_base *p = calloc(1, size + sizeof(*p));
339 if (p == NULL)
340 return NULL;
341 p->isa = type;
342 p->ref_cnt = 1;
343
344 return BASE2PTR(p);
345 }
346
347 void *
348 _heim_get_isaextra(heim_object_t ptr, size_t idx)
349 {
350 struct heim_base *p = (struct heim_base *)PTR2BASE(ptr);
351
352 heim_assert(ptr != NULL, "internal error");
353 if (p->isa == &memory_object)
354 return NULL;
355 heim_assert(idx < 3, "invalid private heim_base extra data index");
356 return &p->isaextra[idx];
357 }
358
359 heim_tid_t
360 _heim_type_get_tid(heim_type_t type)
361 {
362 return type->tid;
363 }
364
365 #if !defined(WIN32) && !defined(HAVE_DISPATCH_DISPATCH_H) && defined(ENABLE_PTHREAD_SUPPORT)
366 static pthread_once_t once_arg_key_once = PTHREAD_ONCE_INIT;
367 static pthread_key_t once_arg_key;
368
369 static void
370 once_arg_key_once_init(void)
371 {
372 errno = pthread_key_create(&once_arg_key, NULL);
373 if (errno != 0) {
374 fprintf(stderr,
375 "Error: pthread_key_create() failed, cannot continue: %s\n",
376 strerror(errno));
377 abort();
378 }
379 }
380
381 struct once_callback {
382 void (*fn)(void *);
383 void *data;
384 };
385
386 static void
387 once_callback_caller(void)
388 {
389 struct once_callback *once_callback = pthread_getspecific(once_arg_key);
390
391 if (once_callback == NULL) {
392 fprintf(stderr, "Error: pthread_once() calls callback on "
393 "different thread?! Cannot continue.\n");
394 abort();
395 }
396 once_callback->fn(once_callback->data);
397 }
398 #endif
399
400 /**
401 * Call func once and only once
402 *
403 * @param once pointer to a heim_base_once_t
404 * @param ctx context passed to func
405 * @param func function to be called
406 */
407
408 void
409 heim_base_once_f(heim_base_once_t *once, void *ctx, void (*func)(void *))
410 {
411 #if defined(WIN32)
412 /*
413 * With a libroken wrapper for some CAS function and a libroken yield()
414 * wrapper we could make this the default implementation when we have
415 * neither Grand Central nor POSX threads.
416 *
417 * We could also adapt the double-checked lock pattern with CAS
418 * providing the necessary memory barriers in the absence of
419 * portable explicit memory barrier APIs.
420 */
421 /*
422 * We use CAS operations in large part to provide implied memory
423 * barriers.
424 *
425 * State 0 means that func() has never executed.
426 * State 1 means that func() is executing.
427 * State 2 means that func() has completed execution.
428 */
429 if (InterlockedCompareExchange(once, 1L, 0L) == 0L) {
430 /* State is now 1 */
431 (*func)(ctx);
432 (void)InterlockedExchange(once, 2L);
433 /* State is now 2 */
434 } else {
435 /*
436 * The InterlockedCompareExchange is being used to fetch
437 * the current state under a full memory barrier. As long
438 * as the current state is 1 continue to spin.
439 */
440 while (InterlockedCompareExchange(once, 2L, 0L) == 1L)
441 SwitchToThread();
442 }
443 #elif defined(HAVE_DISPATCH_DISPATCH_H)
444 dispatch_once_f(once, ctx, func);
445 #elif defined(ENABLE_PTHREAD_SUPPORT)
446 struct once_callback once_callback;
447
448 once_callback.fn = func;
449 once_callback.data = ctx;
450
451 errno = pthread_once(&once_arg_key_once, once_arg_key_once_init);
452 if (errno != 0) {
453 fprintf(stderr, "Error: pthread_once() failed, cannot continue: %s\n",
454 strerror(errno));
455 abort();
456 }
457 errno = pthread_setspecific(once_arg_key, &once_callback);
458 if (errno != 0) {
459 fprintf(stderr,
460 "Error: pthread_setspecific() failed, cannot continue: %s\n",
461 strerror(errno));
462 abort();
463 }
464 errno = pthread_once(once, once_callback_caller);
465 if (errno != 0) {
466 fprintf(stderr, "Error: pthread_once() failed, cannot continue: %s\n",
467 strerror(errno));
468 abort();
469 }
470 #else
471 static HEIMDAL_MUTEX mutex = HEIMDAL_MUTEX_INITIALIZER;
472 HEIMDAL_MUTEX_lock(&mutex);
473 if (*once == 0) {
474 *once = 1;
475 HEIMDAL_MUTEX_unlock(&mutex);
476 func(ctx);
477 HEIMDAL_MUTEX_lock(&mutex);
478 *once = 2;
479 HEIMDAL_MUTEX_unlock(&mutex);
480 } else if (*once == 2) {
481 HEIMDAL_MUTEX_unlock(&mutex);
482 } else {
483 HEIMDAL_MUTEX_unlock(&mutex);
484 while (1) {
485 struct timeval tv = { 0, 1000 };
486 select(0, NULL, NULL, NULL, &tv);
487 HEIMDAL_MUTEX_lock(&mutex);
488 if (*once == 2)
489 break;
490 HEIMDAL_MUTEX_unlock(&mutex);
491 }
492 HEIMDAL_MUTEX_unlock(&mutex);
493 }
494 #endif
495 }
496
497 /**
498 * Abort and log the failure (using syslog)
499 */
500
501 void
502 heim_abort(const char *fmt, ...)
503 {
504 va_list ap;
505 va_start(ap, fmt);
506 heim_abortv(fmt, ap);
507 va_end(ap);
508 }
509
510 /**
511 * Abort and log the failure (using syslog)
512 */
513
514 void
515 heim_abortv(const char *fmt, va_list ap)
516 {
517 static char str[1024];
518
519 vsnprintf(str, sizeof(str), fmt, ap);
520 syslog(LOG_ERR, "heim_abort: %s", str);
521 abort();
522 }
523
524 /*
525 *
526 */
527
528 static int ar_created = 0;
529 static HEIMDAL_thread_key ar_key;
530
531 struct ar_tls {
532 struct heim_auto_release *head;
533 struct heim_auto_release *current;
534 HEIMDAL_MUTEX tls_mutex;
535 };
536
537 static void
538 ar_tls_delete(void *ptr)
539 {
540 struct ar_tls *tls = ptr;
541 heim_auto_release_t next = NULL;
542
543 if (tls == NULL)
544 return;
545 for (; tls->current != NULL; tls->current = next) {
546 next = tls->current->parent;
547 heim_release(tls->current);
548 }
549 free(tls);
550 }
551
552 static void
553 init_ar_tls(void *ptr)
554 {
555 int ret;
556 HEIMDAL_key_create(&ar_key, ar_tls_delete, ret);
557 if (ret == 0)
558 ar_created = 1;
559 }
560
561 static struct ar_tls *
562 autorel_tls(void)
563 {
564 static heim_base_once_t once = HEIM_BASE_ONCE_INIT;
565 struct ar_tls *arp;
566 int ret;
567
568 heim_base_once_f(&once, NULL, init_ar_tls);
569 if (!ar_created)
570 return NULL;
571
572 arp = HEIMDAL_getspecific(ar_key);
573 if (arp == NULL) {
574
575 arp = calloc(1, sizeof(*arp));
576 if (arp == NULL)
577 return NULL;
578 HEIMDAL_setspecific(ar_key, arp, ret);
579 if (ret) {
580 free(arp);
581 return NULL;
582 }
583 }
584 return arp;
585
586 }
587
588 static void
589 autorel_dealloc(void *ptr)
590 {
591 heim_auto_release_t ar = ptr;
592 struct ar_tls *tls;
593
594 tls = autorel_tls();
595 if (tls == NULL)
596 heim_abort("autorelease pool released on thread w/o autorelease inited");
597
598 heim_auto_release_drain(ar);
599
600 if (!HEIM_TAILQ_EMPTY(&ar->pool))
601 heim_abort("pool not empty after draining");
602
603 HEIMDAL_MUTEX_lock(&tls->tls_mutex);
604 if (tls->current != ptr)
605 heim_abort("autorelease not releaseing top pool");
606
607 tls->current = ar->parent;
608 HEIMDAL_MUTEX_unlock(&tls->tls_mutex);
609 }
610
611 static int
612 autorel_cmp(void *a, void *b)
613 {
614 return (a == b);
615 }
616
617 static unsigned long
618 autorel_hash(void *ptr)
619 {
620 return (unsigned long)ptr;
621 }
622
623
624 static struct heim_type_data _heim_autorel_object = {
625 HEIM_TID_AUTORELEASE,
626 "autorelease-pool",
627 NULL,
628 autorel_dealloc,
629 NULL,
630 autorel_cmp,
631 autorel_hash,
632 NULL
633 };
634
635 /**
636 * Create thread-specific object auto-release pool
637 *
638 * Objects placed on the per-thread auto-release pool (with
639 * heim_auto_release()) can be released in one fell swoop by calling
640 * heim_auto_release_drain().
641 */
642
643 heim_auto_release_t
644 heim_auto_release_create(void)
645 {
646 struct ar_tls *tls = autorel_tls();
647 heim_auto_release_t ar;
648
649 if (tls == NULL)
650 heim_abort("Failed to create/get autorelease head");
651
652 ar = _heim_alloc_object(&_heim_autorel_object, sizeof(struct heim_auto_release));
653 if (ar) {
654 HEIMDAL_MUTEX_lock(&tls->tls_mutex);
655 if (tls->head == NULL)
656 tls->head = ar;
657 ar->parent = tls->current;
658 tls->current = ar;
659 HEIMDAL_MUTEX_unlock(&tls->tls_mutex);
660 }
661
662 return ar;
663 }
664
665 /**
666 * Place the current object on the thread's auto-release pool
667 *
668 * @param ptr object
669 */
670
671 heim_object_t
672 heim_auto_release(heim_object_t ptr)
673 {
674 struct heim_base *p;
675 struct ar_tls *tls;
676 heim_auto_release_t ar;
677
678 if (ptr == NULL || heim_base_is_tagged(ptr))
679 return ptr;
680
681 p = PTR2BASE(ptr);
682 tls = autorel_tls();
683
684 /* drop from old pool */
685 if ((ar = p->autorelpool) != NULL) {
686 HEIMDAL_MUTEX_lock(&ar->pool_mutex);
687 HEIM_TAILQ_REMOVE(&ar->pool, p, autorel);
688 p->autorelpool = NULL;
689 HEIMDAL_MUTEX_unlock(&ar->pool_mutex);
690 }
691
692 if (tls == NULL || (ar = tls->current) == NULL)
693 heim_abort("no auto relase pool in place, would leak");
694
695 HEIMDAL_MUTEX_lock(&ar->pool_mutex);
696 HEIM_TAILQ_INSERT_HEAD(&ar->pool, p, autorel);
697 p->autorelpool = ar;
698 HEIMDAL_MUTEX_unlock(&ar->pool_mutex);
699
700 return ptr;
701 }
702
703 /**
704 * Release all objects on the given auto-release pool
705 */
706
707 void
708 heim_auto_release_drain(heim_auto_release_t autorel)
709 {
710 heim_object_t obj;
711
712 /* release all elements on the tail queue */
713
714 HEIMDAL_MUTEX_lock(&autorel->pool_mutex);
715 while(!HEIM_TAILQ_EMPTY(&autorel->pool)) {
716 obj = HEIM_TAILQ_FIRST(&autorel->pool);
717 HEIMDAL_MUTEX_unlock(&autorel->pool_mutex);
718 heim_release(BASE2PTR(obj));
719 HEIMDAL_MUTEX_lock(&autorel->pool_mutex);
720 }
721 HEIMDAL_MUTEX_unlock(&autorel->pool_mutex);
722 }
723
724 /*
725 * Helper for heim_path_vget() and heim_path_delete(). On success
726 * outputs the node named by the path and the parent node and key
727 * (useful for heim_path_delete()).
728 */
729
730 static heim_object_t
731 heim_path_vget2(heim_object_t ptr, heim_object_t *parent, heim_object_t *key,
732 heim_error_t *error, va_list ap)
733 {
734 heim_object_t path_element;
735 heim_object_t node, next_node;
736 heim_tid_t node_type;
737
738 *parent = NULL;
739 *key = NULL;
740 if (ptr == NULL)
741 return NULL;
742
743 for (node = ptr; node != NULL; ) {
744 path_element = va_arg(ap, heim_object_t);
745 if (path_element == NULL) {
746 *parent = node;
747 *key = path_element;
748 return node;
749 }
750
751 node_type = heim_get_tid(node);
752 switch (node_type) {
753 case HEIM_TID_ARRAY:
754 case HEIM_TID_DICT:
755 case HEIM_TID_DB:
756 break;
757 default:
758 if (node == ptr)
759 heim_abort("heim_path_get() only operates on container types");
760 return NULL;
761 }
762
763 if (node_type == HEIM_TID_DICT) {
764 next_node = heim_dict_get_value(node, path_element);
765 } else if (node_type == HEIM_TID_DB) {
766 next_node = _heim_db_get_value(node, NULL, path_element, NULL);
767 } else if (node_type == HEIM_TID_ARRAY) {
768 int idx = -1;
769
770 if (heim_get_tid(path_element) == HEIM_TID_NUMBER)
771 idx = heim_number_get_int(path_element);
772 if (idx < 0) {
773 if (error)
774 *error = heim_error_create(EINVAL,
775 "heim_path_get() path elements "
776 "for array nodes must be "
777 "numeric and positive");
778 return NULL;
779 }
780 next_node = heim_array_get_value(node, idx);
781 } else {
782 if (error)
783 *error = heim_error_create(EINVAL,
784 "heim_path_get() node in path "
785 "not a container type");
786 return NULL;
787 }
788 node = next_node;
789 }
790 return NULL;
791 }
792
793 /**
794 * Get a node in a heim_object tree by path
795 *
796 * @param ptr tree
797 * @param error error (output)
798 * @param ap NULL-terminated va_list of heim_object_ts that form a path
799 *
800 * @return object (not retained) if found
801 *
802 * @addtogroup heimbase
803 */
804
805 heim_object_t
806 heim_path_vget(heim_object_t ptr, heim_error_t *error, va_list ap)
807 {
808 heim_object_t p, k;
809
810 return heim_path_vget2(ptr, &p, &k, error, ap);
811 }
812
813 /**
814 * Get a node in a tree by path, with retained reference
815 *
816 * @param ptr tree
817 * @param error error (output)
818 * @param ap NULL-terminated va_list of heim_object_ts that form a path
819 *
820 * @return retained object if found
821 *
822 * @addtogroup heimbase
823 */
824
825 heim_object_t
826 heim_path_vcopy(heim_object_t ptr, heim_error_t *error, va_list ap)
827 {
828 heim_object_t p, k;
829
830 return heim_retain(heim_path_vget2(ptr, &p, &k, error, ap));
831 }
832
833 /**
834 * Get a node in a tree by path
835 *
836 * @param ptr tree
837 * @param error error (output)
838 * @param ... NULL-terminated va_list of heim_object_ts that form a path
839 *
840 * @return object (not retained) if found
841 *
842 * @addtogroup heimbase
843 */
844
845 heim_object_t
846 heim_path_get(heim_object_t ptr, heim_error_t *error, ...)
847 {
848 heim_object_t o;
849 heim_object_t p, k;
850 va_list ap;
851
852 if (ptr == NULL)
853 return NULL;
854
855 va_start(ap, error);
856 o = heim_path_vget2(ptr, &p, &k, error, ap);
857 va_end(ap);
858 return o;
859 }
860
861 /**
862 * Get a node in a tree by path, with retained reference
863 *
864 * @param ptr tree
865 * @param error error (output)
866 * @param ... NULL-terminated va_list of heim_object_ts that form a path
867 *
868 * @return retained object if found
869 *
870 * @addtogroup heimbase
871 */
872
873 heim_object_t
874 heim_path_copy(heim_object_t ptr, heim_error_t *error, ...)
875 {
876 heim_object_t o;
877 heim_object_t p, k;
878 va_list ap;
879
880 if (ptr == NULL)
881 return NULL;
882
883 va_start(ap, error);
884 o = heim_retain(heim_path_vget2(ptr, &p, &k, error, ap));
885 va_end(ap);
886 return o;
887 }
888
889 /**
890 * Create a path in a heim_object_t tree
891 *
892 * @param ptr the tree
893 * @param size the size of the heim_dict_t nodes to be created
894 * @param leaf leaf node to be added, if any
895 * @param error error (output)
896 * @param ap NULL-terminated of path component objects
897 *
898 * Create a path of heim_dict_t interior nodes in a given heim_object_t
899 * tree, as necessary, and set/replace a leaf, if given (if leaf is NULL
900 * then the leaf is not deleted).
901 *
902 * @return 0 on success, else a system error
903 *
904 * @addtogroup heimbase
905 */
906
907 int
908 heim_path_vcreate(heim_object_t ptr, size_t size, heim_object_t leaf,
909 heim_error_t *error, va_list ap)
910 {
911 heim_object_t path_element = va_arg(ap, heim_object_t);
912 heim_object_t next_path_element = NULL;
913 heim_object_t node = ptr;
914 heim_object_t next_node = NULL;
915 heim_tid_t node_type;
916 int ret = 0;
917
918 if (ptr == NULL)
919 heim_abort("heim_path_vcreate() does not create root nodes");
920
921 while (path_element != NULL) {
922 next_path_element = va_arg(ap, heim_object_t);
923 node_type = heim_get_tid(node);
924
925 if (node_type == HEIM_TID_DICT) {
926 next_node = heim_dict_get_value(node, path_element);
927 } else if (node_type == HEIM_TID_ARRAY) {
928 int idx = -1;
929
930 if (heim_get_tid(path_element) == HEIM_TID_NUMBER)
931 idx = heim_number_get_int(path_element);
932 if (idx < 0) {
933 if (error)
934 *error = heim_error_create(EINVAL,
935 "heim_path() path elements for "
936 "array nodes must be numeric "
937 "and positive");
938 return EINVAL;
939 }
940 if (idx < heim_array_get_length(node))
941 next_node = heim_array_get_value(node, idx);
942 else
943 next_node = NULL;
944 } else if (node_type == HEIM_TID_DB && next_path_element != NULL) {
945 if (error)
946 *error = heim_error_create(EINVAL, "Interior node is a DB");
947 return EINVAL;
948 }
949
950 if (next_path_element == NULL)
951 break;
952
953 /* Create missing interior node */
954 if (next_node == NULL) {
955 next_node = heim_dict_create(size); /* no arrays or DBs, just dicts */
956 if (next_node == NULL) {
957 ret = ENOMEM;
958 goto err;
959 }
960
961 if (node_type == HEIM_TID_DICT) {
962 ret = heim_dict_set_value(node, path_element, next_node);
963 } else if (node_type == HEIM_TID_ARRAY &&
964 heim_number_get_int(path_element) <= heim_array_get_length(node)) {
965 ret = heim_array_insert_value(node,
966 heim_number_get_int(path_element),
967 next_node);
968 } else {
969 ret = EINVAL;
970 if (error)
971 *error = heim_error_create(ret, "Node in path not a "
972 "container");
973 }
974 heim_release(next_node);
975 if (ret)
976 goto err;
977 }
978
979 path_element = next_path_element;
980 node = next_node;
981 next_node = NULL;
982 }
983
984 if (path_element == NULL)
985 goto err;
986
987 /* Add the leaf */
988 if (leaf != NULL) {
989 if (node_type == HEIM_TID_DICT)
990 ret = heim_dict_set_value(node, path_element, leaf);
991 else
992 ret = heim_array_insert_value(node,
993 heim_number_get_int(path_element),
994 leaf);
995 }
996 return ret;
997
998 err:
999 if (error && !*error) {
1000 if (ret == ENOMEM)
1001 *error = heim_error_create_enomem();
1002 else
1003 *error = heim_error_create(ret, "Could not set "
1004 "dict value");
1005 }
1006 return ret;
1007 }
1008
1009 /**
1010 * Create a path in a heim_object_t tree
1011 *
1012 * @param ptr the tree
1013 * @param size the size of the heim_dict_t nodes to be created
1014 * @param leaf leaf node to be added, if any
1015 * @param error error (output)
1016 * @param ... NULL-terminated list of path component objects
1017 *
1018 * Create a path of heim_dict_t interior nodes in a given heim_object_t
1019 * tree, as necessary, and set/replace a leaf, if given (if leaf is NULL
1020 * then the leaf is not deleted).
1021 *
1022 * @return 0 on success, else a system error
1023 *
1024 * @addtogroup heimbase
1025 */
1026
1027 int
1028 heim_path_create(heim_object_t ptr, size_t size, heim_object_t leaf,
1029 heim_error_t *error, ...)
1030 {
1031 va_list ap;
1032 int ret;
1033
1034 va_start(ap, error);
1035 ret = heim_path_vcreate(ptr, size, leaf, error, ap);
1036 va_end(ap);
1037 return ret;
1038 }
1039
1040 /**
1041 * Delete leaf node named by a path in a heim_object_t tree
1042 *
1043 * @param ptr the tree
1044 * @param error error (output)
1045 * @param ap NULL-terminated list of path component objects
1046 *
1047 * @addtogroup heimbase
1048 */
1049
1050 void
1051 heim_path_vdelete(heim_object_t ptr, heim_error_t *error, va_list ap)
1052 {
1053 heim_object_t parent, key, child;
1054
1055 child = heim_path_vget2(ptr, &parent, &key, error, ap);
1056 if (child != NULL) {
1057 if (heim_get_tid(parent) == HEIM_TID_DICT)
1058 heim_dict_delete_key(parent, key);
1059 else if (heim_get_tid(parent) == HEIM_TID_DB)
1060 heim_db_delete_key(parent, NULL, key, error);
1061 else if (heim_get_tid(parent) == HEIM_TID_ARRAY)
1062 heim_array_delete_value(parent, heim_number_get_int(key));
1063 heim_release(child);
1064 }
1065 }
1066
1067 /**
1068 * Delete leaf node named by a path in a heim_object_t tree
1069 *
1070 * @param ptr the tree
1071 * @param error error (output)
1072 * @param ap NULL-terminated list of path component objects
1073 *
1074 * @addtogroup heimbase
1075 */
1076
1077 void
1078 heim_path_delete(heim_object_t ptr, heim_error_t *error, ...)
1079 {
1080 va_list ap;
1081
1082 va_start(ap, error);
1083 heim_path_vdelete(ptr, error, ap);
1084 va_end(ap);
1085 return;
1086 }
1087