int OPENSSL_SA_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
{
    int i, level = 1;
    ossl_uintmax_t n = posn;
    void **p;
    if (sa == NULL)
        return 0;
    for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
        if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
            break;
    for (;sa->levels < level; sa->levels++) {
        p = alloc_node();
        if (p == NULL)
            return 0;
        p[0] = sa->nodes;
        sa->nodes = p;
    }
    if (sa->top < posn)
        sa->top = posn;
    p = sa->nodes;
    for (level = sa->levels - 1; level > 0; level--) {
        i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
        if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
            return 0;
        p = p[i];
    }
    p += posn & SA_BLOCK_MASK;
    if (val == NULL && *p != NULL)
        sa->nelem--;
    else if (val != NULL && *p == NULL)
        sa->nelem++;
    *p = val;
    return 1;
}
['static int test_register_deregister(void)\n{\n    static const struct {\n        int nid;\n        const char *prop;\n        char *impl;\n    } impls[] = {\n        { 6, "position=1", "a" },\n        { 6, "position=2", "b" },\n        { 6, "position=3", "c" },\n        { 6, "position=4", "d" },\n    };\n    size_t i;\n    int ret = 0;\n    OSSL_METHOD_STORE *store;\n    if (!TEST_ptr(store = ossl_method_store_new(NULL))\n        || !add_property_names("position", NULL))\n        goto err;\n    for (i = 0; i < OSSL_NELEM(impls); i++)\n        if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,\n                                             impls[i].prop, impls[i].impl,\n                                             NULL, NULL))) {\n            TEST_note("iteration %zd", i + 1);\n            goto err;\n        }\n    for (i = 0; i < OSSL_NELEM(impls); i++) {\n        const size_t j = (1 + i * 3) % OSSL_NELEM(impls);\n        int nid = impls[j].nid;\n        void *impl = impls[j].impl;\n        if (!TEST_true(ossl_method_store_remove(store, nid, impl))\n            || !TEST_false(ossl_method_store_remove(store, nid, impl))) {\n            TEST_note("iteration %zd, position %zd", i + 1, j + 1);\n            goto err;\n        }\n    }\n    if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))\n        ret = 1;\nerr:\n    ossl_method_store_free(store);\n    return ret;\n}', 'int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,\n                          int nid, const char *properties, void *method,\n                          int (*method_up_ref)(void *),\n                          void (*method_destruct)(void *))\n{\n    ALGORITHM *alg = NULL;\n    IMPLEMENTATION *impl;\n    int ret = 0;\n    int i;\n    if (nid <= 0 || method == NULL || store == NULL)\n        return 0;\n    if (properties == NULL)\n        properties = "";\n    impl = OPENSSL_malloc(sizeof(*impl));\n    if (impl == NULL)\n        return 0;\n    if (method_up_ref != NULL && !method_up_ref(method)) {\n        OPENSSL_free(impl);\n        return 0;\n    }\n    impl->provider = prov;\n    impl->method = method;\n    impl->method_destruct = method_destruct;\n    ossl_property_write_lock(store);\n    ossl_method_cache_flush(store, nid);\n    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {\n        impl->properties = ossl_parse_property(store->ctx, properties);\n        if (impl->properties == NULL)\n            goto err;\n        ossl_prop_defn_set(store->ctx, properties, impl->properties);\n    }\n    alg = ossl_method_store_retrieve(store, nid);\n    if (alg == NULL) {\n        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL\n                || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL\n                || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)\n            goto err;\n        alg->nid = nid;\n        if (!ossl_method_store_insert(store, alg))\n            goto err;\n    }\n    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {\n        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);\n        if (tmpimpl->provider == impl->provider\n            && tmpimpl->properties == impl->properties)\n            break;\n    }\n    if (i == sk_IMPLEMENTATION_num(alg->impls)\n        && sk_IMPLEMENTATION_push(alg->impls, impl))\n        ret = 1;\n    ossl_property_unlock(store);\n    if (ret == 0)\n        impl_free(impl);\n    return ret;\nerr:\n    ossl_property_unlock(store);\n    alg_cleanup(0, alg);\n    impl_free(impl);\n    return 0;\n}', 'static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)\n{\n        return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);\n}', 'DEFINE_SPARSE_ARRAY_OF(ALGORITHM)', 'int OPENSSL_SA_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)\n{\n    int i, level = 1;\n    ossl_uintmax_t n = posn;\n    void **p;\n    if (sa == NULL)\n        return 0;\n    for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)\n        if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)\n            break;\n    for (;sa->levels < level; sa->levels++) {\n        p = alloc_node();\n        if (p == NULL)\n            return 0;\n        p[0] = sa->nodes;\n        sa->nodes = p;\n    }\n    if (sa->top < posn)\n        sa->top = posn;\n    p = sa->nodes;\n    for (level = sa->levels - 1; level > 0; level--) {\n        i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;\n        if (p[i] == NULL && (p[i] = alloc_node()) == NULL)\n            return 0;\n        p = p[i];\n    }\n    p += posn & SA_BLOCK_MASK;\n    if (val == NULL && *p != NULL)\n        sa->nelem--;\n    else if (val != NULL && *p == NULL)\n        sa->nelem++;\n    *p = val;\n    return 1;\n}']