BuildSystem: Remove unused variables.
[asterisk/asterisk.git] / main / sorcery.c
index bd919a4..c79675c 100644 (file)
@@ -29,8 +29,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/logger.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/astobj2.h"
@@ -43,6 +41,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/taskprocessor.h"
 #include "asterisk/threadpool.h"
 #include "asterisk/json.h"
+#include "asterisk/vector.h"
 
 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
 #undef open
@@ -86,6 +85,35 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /*! \brief Thread pool for observers */
 static struct ast_threadpool *threadpool;
 
+/*! \brief Structure for an internal wizard instance */
+struct ast_sorcery_internal_wizard {
+       /*!
+        * \brief Wizard interface itself
+        * \warning Callbacks must always be declared first in this structure
+        * so an ao2_ref on &callbacks will adjust the ref count on
+        * internal_wizard.
+        */
+       struct ast_sorcery_wizard callbacks;
+
+       /*! \brief Observers */
+       struct ao2_container *observers;
+};
+
+/*! \brief Structure for a wizard instance which operates on objects */
+struct ast_sorcery_object_wizard {
+       /*! \brief Wizard interface itself */
+       struct ast_sorcery_internal_wizard *wizard;
+
+       /*! \brief Unique data for the wizard */
+       void *data;
+
+       /*! \brief Wizard is acting as an object cache */
+       unsigned int caching:1;
+};
+
+/*! \brief Interface for a sorcery object type wizards */
+AST_VECTOR_RW(ast_sorcery_object_wizards, struct ast_sorcery_object_wizard *);
+
 /*! \brief Structure for internal sorcery object information */
 struct ast_sorcery_object {
        /*! \brief Unique identifier of this object */
@@ -99,6 +127,9 @@ struct ast_sorcery_object {
 
        /*! \brief Extended object fields */
        struct ast_variable *extended;
+
+       /*! \brief Time that the object was created */
+       struct timeval created;
 };
 
 /*! \brief Structure for registered object type */
@@ -119,7 +150,7 @@ struct ast_sorcery_object_type {
        sorcery_diff_handler diff;
 
        /*! \brief Wizard instances */
-       struct ao2_container *wizards;
+       struct ast_sorcery_object_wizards wizards;
 
        /*! \brief Object fields */
        struct ao2_container *fields;
@@ -176,25 +207,11 @@ struct ast_sorcery_object_field {
        intptr_t args[];
 };
 
-/*! \brief Structure for an internal wizard instance */
-struct ast_sorcery_internal_wizard {
-       /*! \brief Wizard interface itself */
-       struct ast_sorcery_wizard callbacks;
-
-       /*! \brief Observers */
-       struct ao2_container *observers;
-};
-
-/*! \brief Structure for a wizard instance which operates on objects */
-struct ast_sorcery_object_wizard {
-       /*! \brief Wizard interface itself */
-       struct ast_sorcery_internal_wizard *wizard;
-
-       /*! \brief Unique data for the wizard */
-       void *data;
-
-       /*! \brief Wizard is acting as an object cache */
-       unsigned int caching:1;
+/*! \brief Proxy object for sorcery */
+struct sorcery_proxy {
+       AO2_WEAKPROXY();
+       /*! \brief The name of the module owning this sorcery instance */
+       char module_name[0];
 };
 
 /*! \brief Full structure for sorcery */
@@ -205,8 +222,8 @@ struct ast_sorcery {
        /*! \brief Observers */
        struct ao2_container *observers;
 
-       /*! \brief The name of the module owning this sorcery instance */
-       char module_name[0];
+       /*! \brief Pointer to module_name in the associated sorcery_proxy. */
+       char *module_name;
 };
 
 /*! \brief Structure for passing load/reload details */
@@ -281,6 +298,12 @@ static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
        return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
 }
 
+static int yesno_handler_fn(const void *obj, const intptr_t *args, char **buf)
+{
+       unsigned int *field = (unsigned int *)(obj + args[0]);
+       return !(*buf = ast_strdup(*field ? "yes" : "no")) ? -1 : 0;
+}
+
 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
        struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
@@ -295,7 +318,7 @@ static int chararray_handler_fn(const void *obj, const intptr_t *args, char **bu
 
 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
-       struct ast_str *codec_buf = ast_str_alloca(64);
+       struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
        struct ast_format_cap **cap = (struct ast_format_cap **)(obj + args[0]);
        return !(*buf = ast_strdup(ast_format_cap_get_names(*cap, &codec_buf)));
 }
@@ -304,6 +327,7 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
 {
        switch(type) {
        case OPT_BOOL_T: return bool_handler_fn;
+       case OPT_YESNO_T: return yesno_handler_fn;
        case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
        case OPT_CODEC_T: return codec_handler_fn;
        case OPT_DOUBLE_T: return double_handler_fn;
@@ -319,113 +343,18 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
        return NULL;
 }
 
-/*! \brief Hashing function for sorcery wizards */
-static int sorcery_wizard_hash(const void *obj, const int flags)
-{
-       const struct ast_sorcery_internal_wizard *object;
-       const char *key;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_KEY:
-               key = obj;
-               break;
-       case OBJ_SEARCH_OBJECT:
-               object = obj;
-               key = object->callbacks.name;
-               break;
-       default:
-               ast_assert(0);
-               return 0;
-       }
-       return ast_str_hash(key);
-}
-
-/*! \brief Comparator function for sorcery wizards */
-static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
-{
-       const struct ast_sorcery_internal_wizard *object_left = obj;
-       const struct ast_sorcery_internal_wizard *object_right = arg;
-       const char *right_key = arg;
-       int cmp;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = object_right->callbacks.name;
-               /* Fall through */
-       case OBJ_SEARCH_KEY:
-               cmp = strcmp(object_left->callbacks.name, right_key);
-               break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(object_left->callbacks.name, right_key, strlen(right_key));
-               break;
-       default:
-               cmp = 0;
-               break;
-       }
-       if (cmp) {
-               return 0;
-       }
-       return CMP_MATCH;
-}
-
-/*! \brief Hashing function for sorcery wizards */
-static int object_type_field_hash(const void *obj, const int flags)
-{
-       const struct ast_sorcery_object_field *object_field;
-       const char *key;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_KEY:
-               key = obj;
-               break;
-       case OBJ_SEARCH_OBJECT:
-               object_field = obj;
-               key = object_field->name;
-               break;
-       default:
-               ast_assert(0);
-               return 0;
-       }
-       return ast_str_hash(key);
-}
-
-static int object_type_field_cmp(void *obj, void *arg, int flags)
-{
-       const struct ast_sorcery_object_field *field_left = obj;
-       const struct ast_sorcery_object_field *field_right = arg;
-       const char *right_key = arg;
-       int cmp;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = field_right->name;
-               /* Fall through */
-       case OBJ_SEARCH_KEY:
-               cmp = strcmp(field_left->name, right_key);
-               break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(field_left->name, right_key, strlen(right_key));
-               break;
-       default:
-               cmp = 0;
-               break;
-       }
-       if (cmp) {
-               return 0;
-       }
-       return CMP_MATCH;
-}
+/*! \brief Hashing and comparison functions for sorcery wizards */
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_internal_wizard, callbacks.name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_internal_wizard, callbacks.name)
 
-/*! \brief Cleanup function */
-static void sorcery_exit(void)
-{
-       ast_threadpool_shutdown(threadpool);
-       threadpool = NULL;
-}
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_field, name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_field, name)
 
 /*! \brief Cleanup function for graceful shutdowns */
 static void sorcery_cleanup(void)
 {
+       ast_threadpool_shutdown(threadpool);
+       threadpool = NULL;
        ao2_cleanup(wizards);
        wizards = NULL;
        ao2_cleanup(observers);
@@ -435,53 +364,10 @@ static void sorcery_cleanup(void)
 }
 
 /*! \brief Compare function for sorcery instances */
-static int sorcery_instance_cmp(void *obj, void *arg, int flags)
-{
-       const struct ast_sorcery *object_left = obj;
-       const struct ast_sorcery *object_right = arg;
-       const char *right_key = arg;
-       int cmp;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = object_right->module_name;
-               /* Fall through */
-       case OBJ_SEARCH_KEY:
-               cmp = strcmp(object_left->module_name, right_key);
-               break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
-               break;
-       default:
-               cmp = 0;
-               break;
-       }
-       if (cmp) {
-               return 0;
-       }
-       return CMP_MATCH;
-}
+AO2_STRING_FIELD_CMP_FN(sorcery_proxy, module_name)
 
 /*! \brief Hashing function for sorcery instances */
-static int sorcery_instance_hash(const void *obj, const int flags)
-{
-       const struct ast_sorcery *object;
-       const char *key;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_KEY:
-               key = obj;
-               break;
-       case OBJ_SEARCH_OBJECT:
-               object = obj;
-               key = object->module_name;
-               break;
-       default:
-               ast_assert(0);
-               return 0;
-       }
-       return ast_str_hash(key);
-}
+AO2_STRING_FIELD_HASH_FN(sorcery_proxy, module_name)
 
 int ast_sorcery_init(void)
 {
@@ -494,33 +380,32 @@ int ast_sorcery_init(void)
        };
        ast_assert(wizards == NULL);
 
-       if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
-               threadpool = NULL;
+       threadpool = ast_threadpool_create("Sorcery", NULL, &options);
+       if (!threadpool) {
                return -1;
        }
 
-       if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
-               ast_threadpool_shutdown(threadpool);
+       wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
+               ast_sorcery_internal_wizard_hash_fn, NULL, ast_sorcery_internal_wizard_cmp_fn);
+       if (!wizards) {
+               sorcery_cleanup();
                return -1;
        }
 
        observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
        if (!observers) {
                sorcery_cleanup();
-               sorcery_exit();
                return -1;
        }
 
-       instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
-               sorcery_instance_hash, sorcery_instance_cmp);
+       instances = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, INSTANCE_BUCKETS,
+               sorcery_proxy_hash_fn, NULL, sorcery_proxy_cmp_fn);
        if (!instances) {
                sorcery_cleanup();
-               sorcery_exit();
                return -1;
        }
 
        ast_register_cleanup(sorcery_cleanup);
-       ast_register_atexit(sorcery_exit);
 
        return 0;
 }
@@ -595,7 +480,7 @@ static int sorcery_generic_observer_remove(void *obj, void *arg, int flags)
 {
        const struct sorcery_global_observer *observer = obj;
 
-       return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
+       return (observer->callbacks == arg) ? CMP_MATCH : 0;
 }
 
 int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks)
@@ -693,103 +578,88 @@ static void sorcery_destructor(void *obj)
 }
 
 /*! \brief Hashing function for sorcery types */
-static int sorcery_type_hash(const void *obj, const int flags)
-{
-       const struct ast_sorcery_object_type *object;
-       const char *key;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_KEY:
-               key = obj;
-               break;
-       case OBJ_SEARCH_OBJECT:
-               object = obj;
-               key = object->name;
-               break;
-       default:
-               ast_assert(0);
-               return 0;
-       }
-       return ast_str_hash(key);
-}
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_type, name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_type, name)
 
-/*! \brief Comparator function for sorcery types */
-static int sorcery_type_cmp(void *obj, void *arg, int flags)
+static void sorcery_proxy_cb(void *weakproxy, void *data)
 {
-       const struct ast_sorcery_object_type *object_left = obj;
-       const struct ast_sorcery_object_type *object_right = arg;
-       const char *right_key = arg;
-       int cmp;
-
-       switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = object_right->name;
-               /* Fall through */
-       case OBJ_SEARCH_KEY:
-               cmp = strcmp(object_left->name, right_key);
-               break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(object_left->name, right_key, strlen(right_key));
-               break;
-       default:
-               cmp = 0;
-               break;
-       }
-       if (cmp) {
-               return 0;
-       }
-       return CMP_MATCH;
+       ao2_unlink(instances, weakproxy);
 }
 
-struct ast_sorcery *__ast_sorcery_open(const char *module_name)
+struct ast_sorcery *__ast_sorcery_open(const char *module_name, const char *file, int line, const char *func)
 {
+       struct sorcery_proxy *proxy;
        struct ast_sorcery *sorcery;
 
        ast_assert(module_name != NULL);
 
        ao2_wrlock(instances);
-       if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
-               goto done;
+       sorcery = __ao2_weakproxy_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK,
+               __PRETTY_FUNCTION__, file, line, func);
+       if (sorcery) {
+               ao2_unlock(instances);
+
+               return sorcery;
        }
 
-       if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
-               goto done;
+       proxy = ao2_t_weakproxy_alloc(sizeof(*proxy) + strlen(module_name) + 1, NULL, module_name);
+       if (!proxy) {
+               goto failure_cleanup;
        }
+       strcpy(proxy->module_name, module_name); /* Safe */
 
-       if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
-               ao2_ref(sorcery, -1);
-               sorcery = NULL;
-               goto done;
+       sorcery = __ao2_alloc(sizeof(*sorcery), sorcery_destructor, AO2_ALLOC_OPT_LOCK_MUTEX, module_name, file, line, func);
+       if (!sorcery) {
+               goto failure_cleanup;
        }
 
-       if (!(sorcery->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL))) {
-               ao2_ref(sorcery, -1);
-               sorcery = NULL;
-               goto done;
+       sorcery->module_name = proxy->module_name;
+
+       /* We have exclusive access to proxy and sorcery, no need for locking here. */
+       if (ao2_t_weakproxy_set_object(proxy, sorcery, OBJ_NOLOCK, "weakproxy link")) {
+               goto failure_cleanup;
+       }
+
+       if (ao2_weakproxy_subscribe(proxy, sorcery_proxy_cb, NULL, OBJ_NOLOCK)) {
+               goto failure_cleanup;
        }
 
-       strcpy(sorcery->module_name, module_name); /* Safe */
+       sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS,
+               ast_sorcery_object_type_hash_fn, ast_sorcery_object_type_cmp_fn);
+       if (!sorcery->types) {
+               goto failure_cleanup;
+       }
+
+       if (!(sorcery->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL))) {
+               goto failure_cleanup;
+       }
 
        if (__ast_sorcery_apply_config(sorcery, module_name, module_name) == AST_SORCERY_APPLY_FAIL) {
                ast_log(LOG_ERROR, "Error attempting to apply configuration %s to sorcery.\n", module_name);
-               ao2_cleanup(sorcery);
-               sorcery = NULL;
-               goto done;
+               goto failure_cleanup;
        }
 
-       ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
+       ao2_link_flags(instances, proxy, OBJ_NOLOCK);
+       ao2_ref(proxy, -1);
 
        NOTIFY_GLOBAL_OBSERVERS(observers, instance_created, module_name, sorcery);
 
-done:
        ao2_unlock(instances);
        return sorcery;
+
+failure_cleanup:
+       /* cleanup of sorcery may result in locking instances, so make sure we unlock first. */
+       ao2_unlock(instances);
+       ao2_cleanup(sorcery);
+       ao2_cleanup(proxy);
+
+       return NULL;
 }
 
 /*! \brief Search function for sorcery instances */
 struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
 {
-       return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
+       return ao2_weakproxy_find(instances, module_name, OBJ_SEARCH_KEY, "");
 }
 
 /*! \brief Destructor function for object types */
@@ -797,7 +667,10 @@ static void sorcery_object_type_destructor(void *obj)
 {
        struct ast_sorcery_object_type *object_type = obj;
 
-       ao2_cleanup(object_type->wizards);
+       AST_VECTOR_RW_WRLOCK(&object_type->wizards);
+       AST_VECTOR_CALLBACK_VOID(&object_type->wizards, ao2_cleanup);
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+       AST_VECTOR_RW_FREE(&object_type->wizards);
        ao2_cleanup(object_type->fields);
        ao2_cleanup(object_type->observers);
 
@@ -814,46 +687,52 @@ static void sorcery_object_type_destructor(void *obj)
 /*! \brief Internal function which allocates an object type structure */
 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
 {
+#define INITIAL_WIZARD_VECTOR_SIZE 5
        struct ast_sorcery_object_type *object_type;
-       char uuid[AST_UUID_STR_LEN];
+       char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
 
        if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
                return NULL;
        }
 
        /* Order matters for object wizards */
-       if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, sorcery_wizard_cmp))) {
+       if (AST_VECTOR_RW_INIT(&object_type->wizards, INITIAL_WIZARD_VECTOR_SIZE) != 0) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, OBJECT_FIELD_BUCKETS,
-                                       object_type_field_hash, object_type_field_cmp))) {
+       object_type->fields = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
+               OBJECT_FIELD_BUCKETS, ast_sorcery_object_field_hash_fn, NULL, ast_sorcery_object_field_cmp_fn);
+       if (!object_type->fields) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
+       object_type->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
+               NULL, NULL);
+       if (!object_type->observers) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
+       object_type->info = ast_calloc(1,
+               sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0]));
+       if (!object_type->info) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
+       object_type->file = ast_calloc(1,
+               sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0]));
+       if (!object_type->file) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
-               ao2_ref(object_type, -1);
-               return NULL;
-       }
+       /* Create name with seq number appended. */
+       ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "sorcery/%s", type);
 
-       if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
+       if (!(object_type->serializer = ast_threadpool_serializer(tps_name, threadpool))) {
                ao2_ref(object_type, -1);
                return NULL;
        }
@@ -872,7 +751,7 @@ static void sorcery_object_wizard_destructor(void *obj)
 {
        struct ast_sorcery_object_wizard *object_wizard = obj;
 
-       if (object_wizard->data) {
+       if (object_wizard->data && object_wizard->wizard->callbacks.close) {
                object_wizard->wizard->callbacks.close(object_wizard->data);
        }
 
@@ -883,48 +762,136 @@ static void sorcery_object_wizard_destructor(void *obj)
        ao2_cleanup(object_wizard->wizard);
 }
 
-/*! \brief Internal function which creates an object type and adds a wizard mapping */
-enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
-               const char *type, const char *module, const char *name, const char *data, unsigned int caching)
+/*! \brief Return the number of wizards mapped to an object type */
+int ast_sorcery_get_wizard_mapping_count(struct ast_sorcery *sorcery,
+       const char *type)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+
+       if (!object_type) {
+               return -1;
+       }
+
+       return AST_VECTOR_SIZE(&object_type->wizards);
+}
+
+int ast_sorcery_get_wizard_mapping(struct ast_sorcery *sorcery,
+       const char *type, int index, struct ast_sorcery_wizard **wizard, void **data)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       struct ast_sorcery_object_wizard *owizard;
+
+       if (!object_type) {
+               return -1;
+       }
+
+       if (index < 0 || index >= AST_VECTOR_SIZE(&object_type->wizards)) {
+               return -1;
+       }
+
+       owizard = AST_VECTOR_GET(&object_type->wizards, index);
+
+       if (wizard != NULL) {
+               *wizard = &(owizard->wizard->callbacks);
+               ao2_bump(owizard->wizard);
+       } else {
+               return -1;
+       }
+
+       if (data != NULL) {
+               *data = owizard->data;
+       }
+
+       return 0;
+}
+
+/*! \brief Internal function removes a wizard mapping */
+int __ast_sorcery_remove_wizard_mapping(struct ast_sorcery *sorcery,
+               const char *type, const char *module, const char *name)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       int res;
+
+       if (!object_type) {
+               return -1;
+       }
+
+       AST_VECTOR_RW_WRLOCK(&object_type->wizards);
+#define WIZARD_NAME_COMPARE(a, b) (strcmp((a)->wizard->callbacks.name, (b)) == 0)
+       res = AST_VECTOR_REMOVE_CMP_ORDERED(&object_type->wizards, name, WIZARD_NAME_COMPARE, ao2_cleanup);
+#undef WIZARD_NAME_COMPARE
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
+       return res;
+}
+
+/*! \brief Internal function which creates an object type and inserts a wizard mapping */
+enum ast_sorcery_apply_result __ast_sorcery_insert_wizard_mapping(struct ast_sorcery *sorcery,
+               const char *type, const char *module, const char *name, const char *data,
+               unsigned int caching, int position)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_internal_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
        int created = 0;
 
-       if (!wizard || !object_wizard) {
+       if (!object_wizard) {
+               return AST_SORCERY_APPLY_FAIL;
+       }
+
+       if (!wizard || wizard->callbacks.module != ast_module_running_ref(wizard->callbacks.module)) {
+               ast_log(LOG_ERROR, "Wizard '%s' could not be applied to object type '%s' as it was not found\n",
+                       name, type);
                return AST_SORCERY_APPLY_FAIL;
        }
 
        if (!object_type) {
                if (!(object_type = sorcery_object_type_alloc(type, module))) {
+                       ast_module_unref(wizard->callbacks.module);
                        return AST_SORCERY_APPLY_FAIL;
                }
                created = 1;
        }
 
+       AST_VECTOR_RW_WRLOCK(&object_type->wizards);
        if (!created) {
-               struct ast_sorcery_wizard *found;
+               struct ast_sorcery_object_wizard *found;
 
-               found = ao2_find(object_type->wizards, wizard, OBJ_SEARCH_OBJECT);
+#define WIZARD_COMPARE(a, b) ((a)->wizard == (b))
+               found = AST_VECTOR_GET_CMP(&object_type->wizards, wizard, WIZARD_COMPARE);
+#undef WIZARD_COMPARE
                if (found) {
                        ast_debug(1, "Wizard %s already applied to object type %s\n",
                                        wizard->callbacks.name, object_type->name);
-                       ao2_cleanup(found);
+                       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+                       ast_module_unref(wizard->callbacks.module);
                        return AST_SORCERY_APPLY_DUPLICATE;
                }
        }
 
+       ast_debug(5, "Calling wizard %s open callback on object type %s\n",
+               name, object_type->name);
        if (wizard->callbacks.open && !(object_wizard->data = wizard->callbacks.open(data))) {
+               ast_log(LOG_WARNING, "Wizard '%s' failed to open mapping for object type '%s' with data: %s\n",
+                       name, object_type->name, S_OR(data, ""));
+               AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+               ast_module_unref(wizard->callbacks.module);
                return AST_SORCERY_APPLY_FAIL;
        }
 
-       ast_module_ref(wizard->callbacks.module);
-
        object_wizard->wizard = ao2_bump(wizard);
        object_wizard->caching = caching;
 
-       ao2_link(object_type->wizards, object_wizard);
+       if (position == AST_SORCERY_WIZARD_POSITION_LAST) {
+               position = AST_VECTOR_SIZE(&object_type->wizards);
+       }
+
+       if (AST_VECTOR_INSERT_AT(&object_type->wizards, position, object_wizard) != 0) {
+               AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+               return AST_SORCERY_APPLY_FAIL;
+       }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+       ao2_bump(object_wizard);
 
        if (created) {
                ao2_link(sorcery->types, object_type);
@@ -936,6 +903,14 @@ enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorc
        return AST_SORCERY_APPLY_SUCCESS;
 }
 
+/*! \brief Internal function which creates an object type and adds a wizard mapping */
+enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
+               const char *type, const char *module, const char *name, const char *data, unsigned int caching)
+{
+       return __ast_sorcery_insert_wizard_mapping(sorcery, type, module, name, data,
+               caching, AST_SORCERY_WIZARD_POSITION_LAST);
+}
+
 enum ast_sorcery_apply_result  __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
 {
        struct ast_flags flags = { 0 };
@@ -1012,6 +987,25 @@ static int sorcery_extended_fields_handler(const void *obj, struct ast_variable
        return 0;
 }
 
+int ast_sorcery_object_unregister(struct ast_sorcery *sorcery, const char *type)
+{
+       struct ast_sorcery_object_type *object_type;
+       int res = -1;
+
+       ao2_wrlock(sorcery->types);
+       object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+       if (object_type && object_type->type.type == ACO_ITEM) {
+               ao2_unlink_flags(sorcery->types, object_type, OBJ_NOLOCK);
+               res = 0;
+       }
+       ao2_unlock(sorcery->types);
+
+       /* XXX may need to add an instance unregister observer callback on success. */
+
+       ao2_cleanup(object_type);
+       return res;
+}
+
 int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
@@ -1046,6 +1040,20 @@ int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type,
        return 0;
 }
 
+int ast_sorcery_object_set_congestion_levels(struct ast_sorcery *sorcery, const char *type, long low_water, long high_water)
+{
+       struct ast_sorcery_object_type *object_type;
+       int res = -1;
+
+       object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
+       if (object_type) {
+               res = ast_taskprocessor_alert_set_levels(object_type->serializer,
+                       low_water, high_water);
+               ao2_ref(object_type, -1);
+       }
+       return res;
+}
+
 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
@@ -1074,6 +1082,7 @@ static void sorcery_object_field_destructor(void *obj)
 
        if (object_field->name_regex) {
                regfree(object_field->name_regex);
+               ast_free(object_field->name_regex);
        }
 }
 
@@ -1176,12 +1185,6 @@ static int sorcery_wizard_load(void *obj, void *arg, int flags)
        struct sorcery_load_details *details = arg;
        void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
 
-       if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
-               ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
-                       "maintaining previous values\n", details->type);
-               return 0;
-       }
-
        load = !details->reload ? wizard->wizard->callbacks.load : wizard->wizard->callbacks.reload;
 
        if (load) {
@@ -1209,8 +1212,10 @@ static void sorcery_observer_invocation_destroy(void *obj)
 /*! \brief Allocator function for observer invocation */
 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
 {
-       struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
+       struct sorcery_observer_invocation *invocation;
 
+       invocation = ao2_alloc_options(sizeof(*invocation),
+               sorcery_observer_invocation_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
        if (!invocation) {
                return NULL;
        }
@@ -1254,24 +1259,39 @@ static int sorcery_object_load(void *obj, void *arg, int flags)
        struct ast_sorcery_object_type *type = obj;
        struct sorcery_load_details *details = arg;
 
+       if (!type->type.item_alloc) {
+               return 0;
+       }
+
        details->type = type->name;
 
+       if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
+               ast_log(LOG_NOTICE, "Type '%s' is not reloadable, maintaining previous values\n",
+                       details->type);
+               return 0;
+       }
+
        NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loading,
                details->sorcery->module_name, details->sorcery, type->name, details->reload);
 
-       ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
+       AST_VECTOR_RW_RDLOCK(&type->wizards);
+       AST_VECTOR_CALLBACK(&type->wizards, sorcery_wizard_load, NULL, details, 0);
+       AST_VECTOR_RW_UNLOCK(&type->wizards);
+
+       NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loaded,
+               details->sorcery->module_name, details->sorcery, type->name, details->reload);
 
        if (ao2_container_count(type->observers)) {
-               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
+               struct sorcery_observer_invocation *invocation;
 
-               if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
+               invocation = sorcery_observer_invocation_alloc(type, NULL);
+               if (invocation
+                       && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded,
+                               invocation)) {
                        ao2_cleanup(invocation);
                }
        }
 
-       NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loaded,
-               details->sorcery->module_name, details->sorcery, type->name, details->reload);
-
        return 0;
 }
 
@@ -1440,6 +1460,7 @@ struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sor
        int res = 0;
 
        if (!object_type || !json) {
+               ast_json_unref(json);
                return NULL;
        }
 
@@ -1470,10 +1491,13 @@ struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sor
                        char *buf = NULL;
                        struct ast_json *value = NULL;
 
-                       if ((res = object_field->handler(object, object_field->args, &buf))
+                       if (object_field->handler(object, object_field->args, &buf)
                                || !(value = ast_json_string_create(buf))
                                || ast_json_object_set(json, object_field->name, value)) {
-                               res = -1;
+                               ast_free(buf);
+                               ast_debug(5, "Skipping field '%s' for object type '%s'\n",
+                                       object_field->name, object_type->name);
+                               continue;
                        }
 
                        ast_free(buf);
@@ -1573,9 +1597,26 @@ static void sorcery_object_destructor(void *object)
        ast_free(details->object->id);
 }
 
+void *ast_sorcery_lockable_alloc(size_t size, ao2_destructor_fn destructor, void *lockobj)
+{
+       void *object = ao2_alloc_with_lockobj(size + sizeof(struct ast_sorcery_object),
+               sorcery_object_destructor, lockobj, "");
+       struct ast_sorcery_object_details *details = object;
+
+       if (!object) {
+               return NULL;
+       }
+
+       details->object = object + size;
+       details->object->destructor = destructor;
+
+       return object;
+}
+
 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
 {
-       void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+       void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object),
+               sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
        struct ast_sorcery_object_details *details = object;
 
        if (!object) {
@@ -1606,7 +1647,12 @@ void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, con
        } else {
                details->object->id = ast_strdup(id);
        }
+       if (!details->object->id) {
+               ao2_ref(details, -1);
+               return NULL;
+       }
 
+       details->object->created = ast_tvnow();
        ast_copy_string(details->object->type, type, sizeof(details->object->type));
 
        if (aco_set_defaults(&object_type->type, id, details)) {
@@ -1694,34 +1740,45 @@ static int sorcery_cache_create(void *obj, void *arg, int flags)
 
 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
 {
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       struct ast_sorcery_object_type *object_type;
        void *object = NULL;
-       struct ao2_iterator i;
-       struct ast_sorcery_object_wizard *wizard;
+       int i;
        unsigned int cached = 0;
 
-       if (!object_type || ast_strlen_zero(id)) {
+       if (ast_strlen_zero(id)) {
+               return NULL;
+       }
+
+       object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
+       if (!object_type) {
                return NULL;
        }
 
-       i = ao2_iterator_init(object_type->wizards, 0);
-       for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               struct ast_sorcery_object_wizard *wizard =
+                       AST_VECTOR_GET(&object_type->wizards, i);
+
                if (wizard->wizard->callbacks.retrieve_id &&
                        !(object = wizard->wizard->callbacks.retrieve_id(sorcery, wizard->data, object_type->name, id))) {
                        continue;
                }
 
                cached = wizard->caching;
-
-               ao2_ref(wizard, -1);
                break;
        }
-       ao2_iterator_destroy(&i);
 
        if (!cached && object) {
-               ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
+               struct sorcery_details sdetails = {
+                       .sorcery = sorcery,
+                       .obj = object,
+               };
+
+               AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, &sdetails, 0);
        }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
+       ao2_ref(object_type, -1);
        return object;
 }
 
@@ -1729,8 +1786,7 @@ void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const ch
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        void *object = NULL;
-       struct ao2_iterator i;
-       struct ast_sorcery_object_wizard *wizard;
+       int i;
        unsigned int cached = 0;
 
        if (!object_type) {
@@ -1744,9 +1800,11 @@ void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const ch
                }
        }
 
-       /* Inquire with the available wizards for retrieval */
-       i = ao2_iterator_init(object_type->wizards, 0);
-       for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               struct ast_sorcery_object_wizard *wizard =
+                       AST_VECTOR_GET(&object_type->wizards, i);
+
                if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
                        if (wizard->wizard->callbacks.retrieve_multiple) {
                                wizard->wizard->callbacks.retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
@@ -1757,21 +1815,20 @@ void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const ch
                        }
                }
 
-               if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
+               if (((flags & AST_RETRIEVE_FLAG_MULTIPLE) && (!ao2_container_count(object) || !wizard->caching)) || !object) {
                        continue;
                }
 
                cached = wizard->caching;
 
-               ao2_ref(wizard, -1);
                break;
        }
-       ao2_iterator_destroy(&i);
 
        /* If we are returning a single object and it came from a non-cache source create it in any caches */
        if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
-               ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
+               AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, object, 0);
        }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
        return object;
 }
@@ -1780,39 +1837,75 @@ struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *so
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        struct ao2_container *objects;
-       struct ao2_iterator i;
-       struct ast_sorcery_object_wizard *wizard;
+       int i;
 
        if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
                return NULL;
        }
 
-       i = ao2_iterator_init(object_type->wizards, 0);
-       for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               struct ast_sorcery_object_wizard *wizard =
+                       AST_VECTOR_GET(&object_type->wizards, i);
+
                if (!wizard->wizard->callbacks.retrieve_regex) {
                        continue;
                }
 
                wizard->wizard->callbacks.retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
+
+               if (wizard->caching && ao2_container_count(objects)) {
+                       break;
+               }
        }
-       ao2_iterator_destroy(&i);
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
        return objects;
 }
 
-/*! \brief Internal function which returns if the wizard has created the object */
-static int sorcery_wizard_create(void *obj, void *arg, int flags)
+struct ao2_container *ast_sorcery_retrieve_by_prefix(const struct ast_sorcery *sorcery, const char *type, const char *prefix, const size_t prefix_len)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       struct ao2_container *objects;
+       int i;
+
+       if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
+               return NULL;
+       }
+
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               struct ast_sorcery_object_wizard *wizard =
+                       AST_VECTOR_GET(&object_type->wizards, i);
+
+               if (!wizard->wizard->callbacks.retrieve_prefix) {
+                       continue;
+               }
+
+               wizard->wizard->callbacks.retrieve_prefix(sorcery, wizard->data, object_type->name, objects, prefix, prefix_len);
+
+               if (wizard->caching && ao2_container_count(objects)) {
+                       break;
+               }
+       }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
+       return objects;
+}
 
+/*! \brief Internal function which returns if the wizard has created the object */
+static int sorcery_wizard_create(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
+{
        if (!object_wizard->wizard->callbacks.create) {
-               ast_assert(0);
-               ast_log(LOG_ERROR, "Sorcery wizard '%s' doesn't contain a 'create' virtual function.\n",
-                       object_wizard->wizard->callbacks.name);
+               ast_debug(5, "Sorcery wizard '%s' does not support creation\n", object_wizard->wizard->callbacks.name);
+               return 0;
+       }
+
+       if (object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) {
                return 0;
        }
-       return (!object_wizard->caching && !object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
+
+       return CMP_MATCH;
 }
 
 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
@@ -1842,7 +1935,9 @@ int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
-       RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
+       struct ast_sorcery_object_wizard *object_wizard = NULL;
+       struct ast_sorcery_object_wizard *found_wizard;
+       int i;
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
                .obj = object,
@@ -1852,15 +1947,37 @@ int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
-               ao2_container_count(object_type->observers)) {
-               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+               if (!found_wizard->caching
+                       && sorcery_wizard_create(found_wizard, &sdetails) == CMP_MATCH) {
+                       object_wizard = found_wizard;
+               }
+       }
+
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_create(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
 
-               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
-                       ao2_cleanup(invocation);
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
+                       }
                }
        }
 
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
        return object_wizard ? 0 : -1;
 }
 
@@ -1888,20 +2005,27 @@ static int sorcery_observers_notify_update(void *data)
 }
 
 /*! \brief Internal function which returns if a wizard has updated the object */
-static int sorcery_wizard_update(void *obj, void *arg, int flags)
+static int sorcery_wizard_update(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
+       if (!object_wizard->wizard->callbacks.update) {
+               ast_debug(5, "Sorcery wizard '%s' does not support updating\n", object_wizard->wizard->callbacks.name);
+               return 0;
+       }
+
+       if (object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
 
-       return (object_wizard->wizard->callbacks.update && !object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj) &&
-               !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
+       return CMP_MATCH;
 }
 
 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
-       RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
+       struct ast_sorcery_object_wizard *object_wizard = NULL;
+       struct ast_sorcery_object_wizard *found_wizard;
+       int i;
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
                .obj = object,
@@ -1911,15 +2035,37 @@ int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
-               ao2_container_count(object_type->observers)) {
-               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+               if (!found_wizard->caching
+                       && sorcery_wizard_update(found_wizard, &sdetails) == CMP_MATCH) {
+                       object_wizard = found_wizard;
+               }
+       }
 
-               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
-                       ao2_cleanup(invocation);
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_update(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
+
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
+                       }
                }
        }
 
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
        return object_wizard ? 0 : -1;
 }
 
@@ -1947,20 +2093,27 @@ static int sorcery_observers_notify_delete(void *data)
 }
 
 /*! \brief Internal function which returns if a wizard has deleted the object */
-static int sorcery_wizard_delete(void *obj, void *arg, int flags)
+static int sorcery_wizard_delete(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
+       if (!object_wizard->wizard->callbacks.delete) {
+               ast_debug(5, "Sorcery wizard '%s' does not support deletion\n", object_wizard->wizard->callbacks.name);
+               return 0;
+       }
+
+       if (object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
 
-       return (object_wizard->wizard->callbacks.delete && !object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj) &&
-               !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
+       return CMP_MATCH;
 }
 
 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
-       RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
+       struct ast_sorcery_object_wizard *object_wizard = NULL;
+       struct ast_sorcery_object_wizard *found_wizard;
+       int i;
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
                .obj = object,
@@ -1970,28 +2123,67 @@ int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
-               ao2_container_count(object_type->observers)) {
-               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+               if (!found_wizard->caching
+                       && sorcery_wizard_delete(found_wizard, &sdetails) == CMP_MATCH) {
+                       object_wizard = found_wizard;
+               }
+       }
 
-               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
-                       ao2_cleanup(invocation);
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_delete(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
+
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
+                       }
                }
        }
 
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
        return object_wizard ? 0 : -1;
 }
 
-void ast_sorcery_unref(struct ast_sorcery *sorcery)
+int ast_sorcery_is_stale(const struct ast_sorcery *sorcery, void *object)
 {
-       if (sorcery) {
-               /* One ref for what we just released, the other for the instances container. */
-               ao2_wrlock(instances);
-               if (ao2_ref(sorcery, -1) == 2) {
-                       ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
+       const struct ast_sorcery_object_details *details = object;
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
+       struct ast_sorcery_object_wizard *found_wizard;
+       int res = 0;
+       int i;
+
+       if (!object_type) {
+               return -1;
+       }
+
+       AST_VECTOR_RW_RDLOCK(&object_type->wizards);
+       for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+               found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+
+               if (found_wizard->wizard->callbacks.is_stale) {
+                       res |= found_wizard->wizard->callbacks.is_stale(sorcery, found_wizard->data, object);
+                       ast_debug(5, "After calling wizard '%s', object '%s' is %s\n",
+                               found_wizard->wizard->callbacks.name,
+                               ast_sorcery_object_get_id(object),
+                               res ? "stale" : "not stale");
                }
-               ao2_unlock(instances);
        }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
+       return res;
 }
 
 const char *ast_sorcery_object_get_id(const void *object)
@@ -2000,6 +2192,12 @@ const char *ast_sorcery_object_get_id(const void *object)
        return details->object->id;
 }
 
+const struct timeval ast_sorcery_object_get_created(const void *object)
+{
+       const struct ast_sorcery_object_details *details = object;
+       return details->object->created;
+}
+
 const char *ast_sorcery_object_get_type(const void *object)
 {
        const struct ast_sorcery_object_details *details = object;
@@ -2077,7 +2275,7 @@ static int sorcery_observer_remove(void *obj, void *arg, int flags)
 {
        const struct ast_sorcery_object_type_observer *observer = obj;
 
-       return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
+       return (observer->callbacks == arg) ? CMP_MATCH : 0;
 }
 
 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
@@ -2099,18 +2297,20 @@ void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *
 
 int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
 {
+       const void *object_left = obj;
+       const void *object_right = arg;
        const char *right_key = arg;
        int cmp;
 
        switch (flags & OBJ_SEARCH_MASK) {
        case OBJ_SEARCH_OBJECT:
-               right_key = ast_sorcery_object_get_id(arg);
+               right_key = ast_sorcery_object_get_id(object_right);
                /* Fall through */
        case OBJ_SEARCH_KEY:
-               cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
+               cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
                break;
        case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
+               cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
                break;
        default:
                cmp = 0;
@@ -2121,37 +2321,32 @@ int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
 
 int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
 {
-       const char *right_key = arg;
-       int cmp = 0;
+       int cmp;
+
+       cmp = ast_sorcery_object_id_sort(obj, arg, flags);
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
+}
+
+int ast_sorcery_object_id_hash(const void *obj, int flags)
+{
+       const char *key;
 
        switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = ast_sorcery_object_get_id(arg);
-               /* Fall through */
        case OBJ_SEARCH_KEY:
-               if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
-                       cmp = CMP_MATCH | CMP_STOP;
-               }
+               key = obj;
                break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
-                       cmp = CMP_MATCH;
-               }
+       case OBJ_SEARCH_OBJECT:
+               key = ast_sorcery_object_get_id(obj);
                break;
        default:
-               cmp = 0;
-               break;
-       }
-       return cmp;
-}
-
-int ast_sorcery_object_id_hash(const void *obj, int flags) {
-       if (flags & OBJ_SEARCH_OBJECT) {
-               return ast_str_hash(ast_sorcery_object_get_id(obj));
-       } else if (flags & OBJ_SEARCH_KEY) {
-               return ast_str_hash(obj);
+               /* Hash can only work on something with a full key. */
+               ast_assert(0);
+               return 0;
        }
-       return -1;
+       return ast_str_hash(key);
 }
 
 struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
@@ -2168,7 +2363,7 @@ static int is_registered_cb(void *obj, void *arg, int flags)
 
        if (object_field->name_regex
                && !regexec(object_field->name_regex, name, 0, NULL, 0)) {
-               rc = CMP_MATCH | CMP_STOP;
+               rc = CMP_MATCH;
        }
 
        return rc;
@@ -2195,3 +2390,8 @@ int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type
        ao2_cleanup(object_field);
        return res;
 }
+
+const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery)
+{
+       return sorcery->module_name;
+}