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_query_cache_stochastic(void)\n{\n    const int max = 10000, tail = 10;\n    OSSL_METHOD_STORE *store;\n    int i, res = 0;\n    char buf[50];\n    void *result;\n    int errors = 0;\n    int v[10001];\n    if (!TEST_ptr(store = ossl_method_store_new())\n        || !add_property_names("n", NULL))\n        goto err;\n    for (i = 1; i <= max; i++) {\n        v[i] = 2 * i;\n        BIO_snprintf(buf, sizeof(buf), "n=%d\\n", i);\n        if (!TEST_true(ossl_method_store_add(store, i, buf, "abc", NULL))\n                || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i))\n                || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",\n                                                          "miss"))) {\n            TEST_note("iteration %d", i);\n            goto err;\n        }\n    }\n    for (i = 1; i <= max; i++) {\n        BIO_snprintf(buf, sizeof(buf), "n=%d\\n", i);\n        if (!ossl_method_store_cache_get(store, i, buf, &result)\n            || result != v + i)\n            errors++;\n    }\n    res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);\nerr:\n    ossl_method_store_free(store);\n    return res;\n}', 'int ossl_method_store_add(OSSL_METHOD_STORE *store,\n                          int nid, const char *properties,\n                          void *method, void (*method_destruct)(void *))\n{\n    ALGORITHM *alg = NULL;\n    IMPLEMENTATION *impl;\n    int ret = 0;\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    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(properties)) == NULL) {\n        if ((impl->properties = ossl_parse_property(properties)) == NULL)\n            goto err;\n        ossl_prop_defn_set(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    if (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}']