Merge "asterisk.c: When astcanary dies on linux, reset priority on all threads."
[asterisk/asterisk.git] / main / sorcery.c
index cb24cae..9f8c35c 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include "asterisk/logger.h"
 #include "asterisk/sorcery.h"
@@ -43,7 +43,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/taskprocessor.h"
 #include "asterisk/threadpool.h"
 #include "asterisk/json.h"
-#include "asterisk/format_pref.h"
+#include "asterisk/vector.h"
 
 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
 #undef open
@@ -58,9 +58,64 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /*! \brief Number of buckets for instances (should be prime for performance reasons) */
 #define INSTANCE_BUCKETS 17
 
+/*! \brief Number of buckets for object fields (should be prime for performance reasons) */
+#define OBJECT_FIELD_BUCKETS 29
+
+#define NOTIFY_GENERIC_OBSERVERS(container, type, callback, ...) ({ \
+       struct ao2_iterator i = ao2_iterator_init(container, 0); \
+       struct type *observer; \
+       ao2_rdlock(container); \
+       while ((observer = ao2_iterator_next(&i))) { \
+               if (observer->callbacks->callback) { \
+                       observer->callbacks->callback(__VA_ARGS__); \
+               } \
+               ao2_cleanup(observer); \
+       } \
+       ao2_unlock(container); \
+       ao2_iterator_cleanup(&i); \
+})
+
+#define NOTIFY_GLOBAL_OBSERVERS(container, callback, ...) \
+       NOTIFY_GENERIC_OBSERVERS(container, sorcery_global_observer, callback, __VA_ARGS__)
+
+#define NOTIFY_INSTANCE_OBSERVERS(container, callback, ...) \
+       NOTIFY_GENERIC_OBSERVERS(container, sorcery_instance_observer, callback, __VA_ARGS__)
+
+#define NOTIFY_WIZARD_OBSERVERS(container, callback, ...) \
+       NOTIFY_GENERIC_OBSERVERS(container, sorcery_wizard_observer, callback, __VA_ARGS__)
+
 /*! \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 */
@@ -74,6 +129,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 */
@@ -94,7 +152,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;
@@ -138,6 +196,9 @@ struct ast_sorcery_object_field {
        /*! \brief Name of the field */
        char name[MAX_OBJECT_FIELD];
 
+       /*! \brief The compiled name regex if name is a regex */
+       regex_t *name_regex;
+
        /*! \brief Callback function for translation of a single value */
        sorcery_field_handler handler;
 
@@ -148,22 +209,14 @@ struct ast_sorcery_object_field {
        intptr_t args[];
 };
 
-/*! \brief Structure for a wizard instance which operates on objects */
-struct ast_sorcery_object_wizard {
-       /*! \brief Wizard interface itself */
-       struct ast_sorcery_wizard *wizard;
-
-       /*! \brief Unique data for the wizard */
-       void *data;
-
-       /*! \brief Wizard is acting as an object cache */
-       unsigned int caching:1;
-};
-
 /*! \brief Full structure for sorcery */
 struct ast_sorcery {
        /*! \brief Container for known object types */
        struct ao2_container *types;
+
+       /*! \brief Observers */
+       struct ao2_container *observers;
+
        /*! \brief The name of the module owning this sorcery instance */
        char module_name[0];
 };
@@ -183,6 +236,30 @@ struct sorcery_load_details {
 /*! \brief Registered sorcery wizards */
 static struct ao2_container *wizards;
 
+/* The following 3 observer wrappers must name their
+ * external observer 'callbacks' and it must be
+ * the first member of the structure.  Common macros
+ * and container callbacks depend on it.
+ */
+
+/*! \brief A global observer wrapper */
+struct sorcery_global_observer {
+       const struct ast_sorcery_global_observer *callbacks;
+};
+
+/*! \brief An instance observer wrapper */
+struct sorcery_instance_observer {
+       const struct ast_sorcery_instance_observer *callbacks;
+};
+
+/*! \brief A wizard observer wrapper */
+struct sorcery_wizard_observer {
+       const struct ast_sorcery_wizard_observer *callbacks;
+};
+
+/*! \brief Registered global observers */
+struct ao2_container *observers;
+
 /*! \brief Registered sorcery instances */
 static struct ao2_container *instances;
 
@@ -216,6 +293,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]);
@@ -230,16 +313,16 @@ 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)
 {
-       char tmp_buf[256];
-       struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + args[0]);
-       ast_codec_pref_string(pref, tmp_buf, sizeof(tmp_buf));
-       return !(*buf = ast_strdup(tmp_buf));
+       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)));
 }
 
 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type 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;
@@ -258,41 +341,41 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
 /*! \brief Hashing function for sorcery wizards */
 static int sorcery_wizard_hash(const void *obj, const int flags)
 {
-    const struct ast_sorcery_wizard *object;
-    const char *key;
+       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->name;
-        break;
-    default:
-        ast_assert(0);
-        return 0;
-    }
-    return ast_str_hash(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_wizard *object_left = obj;
-       const struct ast_sorcery_wizard *object_right = arg;
+       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->name;
+               right_key = object_right->callbacks.name;
                /* Fall through */
        case OBJ_SEARCH_KEY:
-               cmp = strcmp(object_left->name, right_key);
+               cmp = strcmp(object_left->callbacks.name, right_key);
                break;
        case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(object_left->name, right_key, strlen(right_key));
+               cmp = strncmp(object_left->callbacks.name, right_key, strlen(right_key));
                break;
        default:
                cmp = 0;
@@ -304,18 +387,63 @@ static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
        return CMP_MATCH;
 }
 
-/*! \brief Cleanup function */
-static void sorcery_exit(void)
+/*! \brief Hashing function for sorcery wizards */
+static int object_type_field_hash(const void *obj, const int flags)
 {
-       ast_threadpool_shutdown(threadpool);
-       threadpool = NULL;
+       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 Cleanup function for graceful shutdowns */
 static void sorcery_cleanup(void)
 {
+       ast_threadpool_shutdown(threadpool);
+       threadpool = NULL;
        ao2_cleanup(wizards);
        wizards = NULL;
+       ao2_cleanup(observers);
+       observers = NULL;
        ao2_cleanup(instances);
        instances = NULL;
 }
@@ -351,22 +479,22 @@ static int sorcery_instance_cmp(void *obj, void *arg, int flags)
 /*! \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;
+       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);
+       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);
 }
 
 int ast_sorcery_init(void)
@@ -380,33 +508,46 @@ 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;
+       }
+
+       wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
+               sorcery_wizard_hash, NULL, sorcery_wizard_cmp);
+       if (!wizards) {
+               sorcery_cleanup();
                return -1;
        }
 
-       if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
-               ast_threadpool_shutdown(threadpool);
+       observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
+       if (!observers) {
+               sorcery_cleanup();
                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_instance_hash, NULL, sorcery_instance_cmp);
        if (!instances) {
                sorcery_cleanup();
-               sorcery_exit();
                return -1;
        }
 
        ast_register_cleanup(sorcery_cleanup);
-       ast_register_atexit(sorcery_exit);
 
        return 0;
 }
 
+static void sorcery_internal_wizard_destructor(void *obj)
+{
+       struct ast_sorcery_internal_wizard *wizard = obj;
+
+       ao2_cleanup(wizard->observers);
+}
+
 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
 {
-       struct ast_sorcery_wizard *wizard;
+       struct ast_sorcery_internal_wizard *wizard;
        int res = -1;
 
        ast_assert(!ast_strlen_zero(interface->name));
@@ -419,18 +560,26 @@ int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, st
                goto done;
        }
 
-       if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
+       if (!(wizard = ao2_alloc(sizeof(*wizard), sorcery_internal_wizard_destructor))) {
                goto done;
        }
 
-       *wizard = *interface;
-       wizard->module = module;
+       wizard->callbacks = *interface;
+       wizard->callbacks.module = module;
+
+       wizard->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
+       if (!wizard->observers) {
+               goto done;
+       }
 
        ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
        res = 0;
 
        ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
 
+       NOTIFY_GLOBAL_OBSERVERS(observers, wizard_registered,
+               interface->name, interface);
+
 done:
        ao2_cleanup(wizard);
        ao2_unlock(wizards);
@@ -440,9 +589,13 @@ done:
 
 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
 {
-       RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
+       struct ast_sorcery_internal_wizard *wizard =
+               interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL;
 
        if (wizard) {
+               NOTIFY_GLOBAL_OBSERVERS(observers, wizard_unregistering, wizard->callbacks.name, &wizard->callbacks);
+               ao2_unlink(wizards, wizard);
+               ao2_ref(wizard, -1);
                ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
                return 0;
        } else {
@@ -450,33 +603,127 @@ int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
        }
 }
 
+/*! \brief Internal callback function for removing a generic observer */
+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 : 0;
+}
+
+int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks)
+{
+       struct sorcery_global_observer *cb;
+
+       cb = ao2_alloc(sizeof(*cb), NULL);
+       if (!cb) {
+               return -1;
+       }
+
+       cb->callbacks = callbacks;
+       ao2_link(observers, cb);
+       ao2_ref(cb, -1);
+
+       return 0;
+}
+
+void ast_sorcery_global_observer_remove(
+       const struct ast_sorcery_global_observer *callbacks)
+{
+       ao2_callback(observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
+}
+
+int ast_sorcery_instance_observer_add(struct ast_sorcery *sorcery,
+       const struct ast_sorcery_instance_observer *callbacks)
+{
+       struct sorcery_instance_observer *cb;
+
+       cb = ao2_alloc(sizeof(*cb), NULL);
+       if (!cb) {
+               return -1;
+       }
+
+       cb->callbacks = callbacks;
+       ao2_link(sorcery->observers, cb);
+       ao2_ref(cb, -1);
+
+       return 0;
+}
+
+void ast_sorcery_instance_observer_remove(struct ast_sorcery *sorcery,
+       const struct ast_sorcery_instance_observer *callbacks)
+{
+       ao2_callback(sorcery->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
+}
+
+int ast_sorcery_wizard_observer_add(struct ast_sorcery_wizard *interface,
+       const struct ast_sorcery_wizard_observer *callbacks)
+{
+       RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
+               interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
+                       ao2_cleanup);
+
+       if (wizard) {
+               struct sorcery_wizard_observer *cb;
+
+               cb = ao2_alloc(sizeof(*cb), NULL);
+               if (!cb) {
+                       return -1;
+               }
+
+               cb->callbacks = callbacks;
+               ao2_link(wizard->observers, cb);
+               ao2_ref(cb, -1);
+
+               return 0;
+       }
+
+       return -1;
+}
+
+void ast_sorcery_wizard_observer_remove(struct ast_sorcery_wizard *interface,
+       const struct ast_sorcery_wizard_observer *callbacks)
+{
+       RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
+               interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
+                       ao2_cleanup);
+
+       if (wizard) {
+               ao2_callback(wizard->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
+       }
+}
+
 /*! \brief Destructor called when sorcery structure is destroyed */
 static void sorcery_destructor(void *obj)
 {
        struct ast_sorcery *sorcery = obj;
 
+       if (sorcery->observers) {
+               NOTIFY_GLOBAL_OBSERVERS(observers, instance_destroying, sorcery->module_name, sorcery);
+       }
+       ao2_cleanup(sorcery->observers);
        ao2_cleanup(sorcery->types);
 }
 
 /*! \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;
+       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);
+       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);
 }
 
 /*! \brief Comparator function for sorcery types */
@@ -528,9 +775,25 @@ struct ast_sorcery *__ast_sorcery_open(const char *module_name)
                goto done;
        }
 
+       if (!(sorcery->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL))) {
+               ao2_ref(sorcery, -1);
+               sorcery = NULL;
+               goto done;
+       }
+
        strcpy(sorcery->module_name, module_name); /* Safe */
+
+       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;
+       }
+
        ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
 
+       NOTIFY_GLOBAL_OBSERVERS(observers, instance_created, module_name, sorcery);
+
 done:
        ao2_unlock(instances);
        return sorcery;
@@ -547,7 +810,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);
 
@@ -564,45 +830,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, NULL))) {
+       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, 1, NULL, NULL))) {
+       object_type->fields = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
+               OBJECT_FIELD_BUCKETS, object_type_field_hash, NULL, object_type_field_cmp);
+       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;
        }
@@ -621,61 +894,176 @@ static void sorcery_object_wizard_destructor(void *obj)
 {
        struct ast_sorcery_object_wizard *object_wizard = obj;
 
-       if (object_wizard->data) {
-               object_wizard->wizard->close(object_wizard->data);
+       if (object_wizard->data && object_wizard->wizard->callbacks.close) {
+               object_wizard->wizard->callbacks.close(object_wizard->data);
        }
 
        if (object_wizard->wizard) {
-               ast_module_unref(object_wizard->wizard->module);
+               ast_module_unref(object_wizard->wizard->callbacks.module);
        }
+
+       ao2_cleanup(object_wizard->wizard);
 }
 
-/*! \brief Internal function which creates an object type and adds a wizard mapping */
-static int 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_wizard *, wizard, ao2_find(wizards, name, 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) {
-               return -1;
+       if (!wizard) {
+               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;
+       } else if (!object_wizard) {
+               return AST_SORCERY_APPLY_FAIL;
        }
 
        if (!object_type) {
                if (!(object_type = sorcery_object_type_alloc(type, module))) {
-                       return -1;
+                       return AST_SORCERY_APPLY_FAIL;
                }
                created = 1;
        }
 
-       if (wizard->open && !(object_wizard->data = wizard->open(data))) {
-               return -1;
+       AST_VECTOR_RW_WRLOCK(&object_type->wizards);
+       if (!created) {
+               struct ast_sorcery_object_wizard *found;
+
+#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);
+                       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+                       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);
+               return AST_SORCERY_APPLY_FAIL;
        }
 
-       ast_module_ref(wizard->module);
+       ast_module_ref(wizard->callbacks.module);
 
-       object_wizard->wizard = wizard;
+       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);
        }
 
-       return 0;
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, wizard_mapped,
+               sorcery->module_name, sorcery, type, &wizard->callbacks, data, object_wizard->data);
+
+       return AST_SORCERY_APPLY_SUCCESS;
 }
 
-int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
+/*! \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 };
        struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
        struct ast_variable *mapping;
-       int res = 0;
+       int res = AST_SORCERY_APPLY_SUCCESS;
 
-       if (!config || config == CONFIG_STATUS_FILEINVALID) {
-               return -1;
+       if (!config) {
+               return AST_SORCERY_APPLY_NO_CONFIGURATION;
+       }
+
+       if (config == CONFIG_STATUS_FILEINVALID) {
+               return AST_SORCERY_APPLY_FAIL;
        }
 
        for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
@@ -698,8 +1086,8 @@ int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, co
                }
 
                /* Any error immediately causes us to stop */
-               if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching)) {
-                       res = -1;
+               if (__ast_sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
+                       res = AST_SORCERY_APPLY_FAIL;
                        break;
                }
        }
@@ -709,16 +1097,16 @@ int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, co
        return res;
 }
 
-int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
+enum ast_sorcery_apply_result __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
 
        /* Defaults can not be added if any existing mapping exists */
        if (object_type) {
-               return -1;
+               return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
        }
 
-       return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
+       return __ast_sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
 }
 
 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
@@ -739,6 +1127,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);
@@ -767,9 +1174,26 @@ int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type,
                return -1;
        }
 
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, object_type_registered,
+               sorcery->module_name, sorcery, 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);
@@ -792,18 +1216,42 @@ void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char
        object_type->diff = diff;
 }
 
+static void sorcery_object_field_destructor(void *obj)
+{
+       struct ast_sorcery_object_field *object_field = obj;
+
+       if (object_field->name_regex) {
+               regfree(object_field->name_regex);
+               ast_free(object_field->name_regex);
+       }
+}
+
 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
 {
+#define MAX_REGEX_ERROR_LEN 128
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
+       int rc;
 
-       if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
+       if (!object_type || !object_type->type.item_alloc || !config_handler
+               || !(object_field = ao2_alloc(sizeof(*object_field), sorcery_object_field_destructor))) {
                return -1;
        }
 
        ast_copy_string(object_field->name, regex, sizeof(object_field->name));
        object_field->multiple_handler = sorcery_handler;
 
+       if (!(object_field->name_regex = ast_calloc(1, sizeof(regex_t)))) {
+               return -1;
+       }
+
+       if ((rc = regcomp(object_field->name_regex, regex, REG_EXTENDED | REG_NOSUB))) {
+               char *regerr = ast_alloca(MAX_REGEX_ERROR_LEN);
+               regerror(rc, object_field->name_regex, regerr, MAX_REGEX_ERROR_LEN);
+               ast_log(LOG_ERROR, "Regular expression '%s' failed to compile: %s\n", regex, regerr);
+               return -1;
+       }
+
        ao2_link(object_type->fields, object_field);
        __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
 
@@ -811,7 +1259,7 @@ int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *
 }
 
 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
-                                       aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
+                                       aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, unsigned int alias, size_t argc, ...)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
@@ -840,7 +1288,9 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
        }
        va_end(args);
 
-       ao2_link(object_type->fields, object_field);
+       if (!alias) {
+               ao2_link(object_type->fields, object_field);
+       }
 
        /* TODO: Improve this hack */
        if (!argc) {
@@ -875,16 +1325,16 @@ 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->load : wizard->wizard->reload;
+       load = !details->reload ? wizard->wizard->callbacks.load : wizard->wizard->callbacks.reload;
 
        if (load) {
+               NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loading,
+                       wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
+
                load(wizard->data, details->sorcery, details->type);
+
+               NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loaded,
+                       wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
        }
 
        return 0;
@@ -902,8 +1352,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;
        }
@@ -947,13 +1399,35 @@ 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;
-       ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
+
+       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);
+
+       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);
                }
        }
@@ -968,7 +1442,13 @@ void ast_sorcery_load(const struct ast_sorcery *sorcery)
                .reload = 0,
        };
 
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
+               sorcery->module_name, sorcery, 0);
+
        ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
+
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
+               sorcery->module_name, sorcery, 0);
 }
 
 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
@@ -993,7 +1473,14 @@ void ast_sorcery_reload(const struct ast_sorcery *sorcery)
                .reload = 1,
        };
 
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
+               sorcery->module_name, sorcery, 1);
+
        ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
+
+       NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
+               sorcery->module_name, sorcery, 1);
+
 }
 
 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
@@ -1143,10 +1630,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);
@@ -1197,20 +1687,6 @@ int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object,
        return res;
 }
 
-static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
-{
-       const struct ast_variable *field;
-
-       /* Search the linked list of fields to find the correct one */
-       for (field = fields; field; field = field->next) {
-               if (!strcmp(field->name, name)) {
-                       return field;
-               }
-       }
-
-       return NULL;
-}
-
 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
 {
        const struct ast_variable *field;
@@ -1224,9 +1700,9 @@ int ast_sorcery_changeset_create(const struct ast_variable *original, const stru
        }
 
        for (field = modified; field; field = field->next) {
-               const struct ast_variable *old = sorcery_find_field(original, field->name);
+               const char *old_value = ast_variable_find_in_list(original, field->name);
 
-               if (!old || strcmp(old->value, field->value)) {
+               if (!old_value || strcmp(old_value, field->value)) {
                        struct ast_variable *tmp;
 
                        if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
@@ -1260,9 +1736,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) {
@@ -1293,7 +1786,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)) {
@@ -1370,11 +1868,11 @@ static int sorcery_cache_create(void *obj, void *arg, int flags)
        const struct ast_sorcery_object_wizard *object_wizard = obj;
        const struct sorcery_details *details = arg;
 
-       if (!object_wizard->caching || !object_wizard->wizard->create) {
+       if (!object_wizard->caching || !object_wizard->wizard->callbacks.create) {
                return 0;
        }
 
-       object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
+       object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj);
 
        return 0;
 }
@@ -1383,31 +1881,36 @@ void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *
 {
        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 || ast_strlen_zero(id)) {
                return NULL;
        }
 
-       i = ao2_iterator_init(object_type->wizards, 0);
-       for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
-               if (wizard->wizard->retrieve_id &&
-                       !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
+       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);
 
        return object;
 }
@@ -1416,8 +1919,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) {
@@ -1431,34 +1933,35 @@ 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->retrieve_multiple) {
-                               wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
+                       if (wizard->wizard->callbacks.retrieve_multiple) {
+                               wizard->wizard->callbacks.retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
                        }
-               } else if (fields && wizard->wizard->retrieve_fields) {
-                       if (wizard->wizard->retrieve_fields) {
-                               object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
+               } else if (fields && wizard->wizard->callbacks.retrieve_fields) {
+                       if (wizard->wizard->callbacks.retrieve_fields) {
+                               object = wizard->wizard->callbacks.retrieve_fields(sorcery, wizard->data, object_type->name, fields);
                        }
                }
 
-               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;
 }
@@ -1467,33 +1970,45 @@ 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)) {
-               if (!wizard->wizard->retrieve_regex) {
+       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->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
+               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)
+static int sorcery_wizard_create(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.create) {
+               ast_debug(5, "Sorcery wizard '%s' does not support creation\n", object_wizard->wizard->callbacks.name);
+               return 0;
+       }
 
-       return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
+       if (object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
+
+       return CMP_MATCH;
 }
 
 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
@@ -1523,7 +2038,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,
@@ -1533,15 +2050,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 (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, 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_create(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_create,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
+                       }
                }
        }
 
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
        return object_wizard ? 0 : -1;
 }
 
@@ -1569,20 +2108,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->update && !object_wizard->wizard->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,
@@ -1592,15 +2138,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;
 }
 
@@ -1628,20 +2196,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;
+       }
 
-       return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
-               !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
+       if (object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj)) {
+               return 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,
@@ -1651,18 +2226,69 @@ 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;
 }
 
+int ast_sorcery_is_stale(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);
+       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");
+               }
+       }
+       AST_VECTOR_RW_UNLOCK(&object_type->wizards);
+
+       return res;
+}
+
 void ast_sorcery_unref(struct ast_sorcery *sorcery)
 {
        if (sorcery) {
@@ -1681,6 +2307,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;
@@ -1758,7 +2390,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)
@@ -1778,10 +2410,103 @@ void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *
                sorcery_observer_remove, cbs);
 }
 
-int ast_sorcery_object_id_compare(const void *obj_left, const void *obj_right, int flags)
+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(object_right);
+               /* Fall through */
+       case OBJ_SEARCH_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(object_left), right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       return cmp;
+}
+
+int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
 {
-       if (!obj_left || !obj_right) {
+       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_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               key = ast_sorcery_object_get_id(obj);
+               break;
+       default:
+               /* Hash can only work on something with a full key. */
+               ast_assert(0);
                return 0;
        }
-       return strcmp(ast_sorcery_object_get_id(obj_left), ast_sorcery_object_get_id(obj_right));
+       return ast_str_hash(key);
+}
+
+struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
+               const char *type)
+{
+       return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
+}
+
+static int is_registered_cb(void *obj, void *arg, int flags)
+{
+       struct ast_sorcery_object_field *object_field = obj;
+       char *name = arg;
+       int rc = 0;
+
+       if (object_field->name_regex
+               && !regexec(object_field->name_regex, name, 0, NULL, 0)) {
+               rc = CMP_MATCH;
+       }
+
+       return rc;
+}
+
+int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
+               const char *field_name)
+{
+       struct ast_sorcery_object_field *object_field;
+       int res = 1;
+
+       ast_assert(object_type != NULL);
+
+       object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
+
+       if (!object_field) {
+               object_field = ao2_callback(object_type->fields, 0, is_registered_cb, (char *)field_name);
+       }
+
+       if (!object_field) {
+               res = 0;
+       }
+
+       ao2_cleanup(object_field);
+       return res;
+}
+
+const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery)
+{
+       return sorcery->module_name;
 }