From 354ef711f3359245aee114c0c04c5c25c2bb9025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Mon, 21 Nov 2011 18:46:38 -0800 Subject: [PATCH] restructure --- base/array.c | 8 ++++---- base/heimbase.c | 8 ++++++++ base/heimbase.h | 7 +++++-- base/heimbasepriv.h | 3 +++ base/string.c | 37 +++++++++++++++++++++++++++++++++++++ base/version-script.map | 2 +- lib/krb5/plugin.c | 2 +- 7 files changed, 59 insertions(+), 8 deletions(-) diff --git a/base/array.c b/base/array.c index 9266411aa..ddd63f713 100644 --- a/base/array.c +++ b/base/array.c @@ -124,12 +124,12 @@ heim_array_append_value(heim_array_t array, heim_object_t object) * Iterate over all objects in array * * @param array array to iterate over - * @param fn function to call on each object * @param ctx context passed to fn + * @param fn function to call on each object */ void -heim_array_iterate_f(heim_array_t array, heim_array_iterator_f_t fn, void *ctx) +heim_array_iterate_f(heim_array_t array, void *ctx, heim_array_iterator_f_t fn) { size_t n; for (n = 0; n < array->len; n++) @@ -178,11 +178,11 @@ heim_array_get_length(heim_array_t array) */ heim_object_t -heim_array_copy_value(heim_array_t array, size_t idx) +heim_array_get_value(heim_array_t array, size_t idx) { if (idx >= array->len) heim_abort("index too large"); - return heim_retain(array->val[idx]); + return array->val[idx]; } /** diff --git a/base/heimbase.c b/base/heimbase.c index 7031af9e4..1c02e8b56 100644 --- a/base/heimbase.c +++ b/base/heimbase.c @@ -138,6 +138,14 @@ heim_release(void *ptr) heim_abort("over release"); } +void +_heim_make_permanent(heim_object_t ptr) +{ + struct heim_base *p = PTR2BASE(ptr); + p->ref_cnt = heim_base_atomic_max; +} + + static heim_type_t tagged_isa[9] = { &_heim_number_object, &_heim_null_object, diff --git a/base/heimbase.h b/base/heimbase.h index 02832f149..eee1a9a97 100644 --- a/base/heimbase.h +++ b/base/heimbase.h @@ -122,13 +122,13 @@ heim_tid_t heim_array_get_type_id(void); typedef void (*heim_array_iterator_f_t)(heim_object_t, void *); int heim_array_append_value(heim_array_t, heim_object_t); -void heim_array_iterate_f(heim_array_t, heim_array_iterator_f_t, void *); +void heim_array_iterate_f(heim_array_t, void *, heim_array_iterator_f_t); #ifdef __BLOCKS__ void heim_array_iterate(heim_array_t, void (^)(heim_object_t)); #endif size_t heim_array_get_length(heim_array_t); heim_object_t - heim_array_copy_value(heim_array_t, size_t); + heim_array_get_value(heim_array_t, size_t); void heim_array_delete_value(heim_array_t, size_t); #ifdef __BLOCKS__ void heim_array_filter(heim_array_t, int (^)(heim_object_t)); @@ -166,6 +166,9 @@ heim_string_t heim_string_create_with_bytes(const void *, size_t); heim_tid_t heim_string_get_type_id(void); const char * heim_string_get_utf8(heim_string_t); +#define HSTR(_str) (__heim_string_constant("" _str "")) +heim_string_t __heim_string_constant(const char *); + /* * Number */ diff --git a/base/heimbasepriv.h b/base/heimbasepriv.h index a3bb19853..83a2c5649 100644 --- a/base/heimbasepriv.h +++ b/base/heimbasepriv.h @@ -85,6 +85,9 @@ _heim_alloc_object(heim_type_t type, size_t size); heim_tid_t _heim_type_get_tid(heim_type_t type); +void +_heim_make_permanent(heim_object_t ptr); + /* tagged tid */ extern struct heim_type_data _heim_null_object; extern struct heim_type_data _heim_bool_object; diff --git a/base/string.c b/base/string.c index b2ae18edb..633d5a568 100644 --- a/base/string.c +++ b/base/string.c @@ -120,3 +120,40 @@ heim_string_get_utf8(heim_string_t string) { return (const char *)string; } + +/* + * + */ + +static void +init_string(void *ptr) +{ + heim_dict_t *dict = ptr; + *dict = heim_dict_create(101); + heim_assert(*dict != NULL, "__heim_string_constant"); +} + +heim_string_t +__heim_string_constant(const char *_str) +{ + static HEIMDAL_MUTEX mutex = HEIMDAL_MUTEX_INITIALIZER; + static heim_base_once_t once; + static heim_dict_t dict = NULL; + heim_string_t s, s2; + + heim_base_once_f(&once, &dict, init_string); + s = heim_string_create(_str); + + HEIMDAL_MUTEX_lock(&mutex); + s2 = heim_dict_get_value(dict, s); + if (s2) { + heim_release(s); + s = s2; + } else { + _heim_make_permanent(s); + heim_dict_set_value(dict, s, s); + } + HEIMDAL_MUTEX_unlock(&mutex); + + return s; +} diff --git a/base/version-script.map b/base/version-script.map index 0c4f9e906..a037931d8 100644 --- a/base/version-script.map +++ b/base/version-script.map @@ -3,7 +3,7 @@ HEIMDAL_BASE_1.0 { heim_abort; heim_alloc; heim_array_append_value; - heim_array_copy_value; + heim_array_get_value; heim_array_create; heim_array_delete_value; heim_array_get_length; diff --git a/lib/krb5/plugin.c b/lib/krb5/plugin.c index 216b26eb6..cf0ba2fd9 100644 --- a/lib/krb5/plugin.c +++ b/lib/krb5/plugin.c @@ -623,7 +623,7 @@ _krb5_plugin_run_f(krb5_context context, s.ret = KRB5_PLUGIN_NO_HANDLE; - heim_array_iterate_f(s.result, eval_results, &s); + heim_array_iterate_f(s.result, &s, eval_results); heim_release(s.result); heim_release(s.n);