in source/local_cache.c [638:700]
static void put_entry_for_encrypt(
struct aws_cryptosdk_materials_cache *generic_cache,
struct aws_cryptosdk_materials_cache_entry **ret_entry,
const struct aws_cryptosdk_enc_materials *materials,
struct aws_cryptosdk_cache_usage_stats initial_usage,
const struct aws_hash_table *enc_ctx,
const struct aws_byte_buf *cache_id) {
struct aws_cryptosdk_local_cache *cache = (struct aws_cryptosdk_local_cache *)generic_cache;
*ret_entry = NULL;
if (aws_mutex_lock(&cache->mutex)) {
return;
}
struct local_cache_entry *entry = new_entry(cache, cache_id);
if (!entry) {
goto out;
}
aws_atomic_init_int(&entry->usage_bytes, initial_usage.bytes_encrypted);
aws_atomic_init_int(&entry->usage_messages, initial_usage.messages_encrypted);
if (!(entry->enc_materials = aws_cryptosdk_enc_materials_new(cache->allocator, materials->alg))) {
goto out;
}
if (copy_enc_materials(cache->allocator, entry->enc_materials, materials)) {
goto out;
}
if (aws_cryptosdk_enc_ctx_init(cache->allocator, &entry->enc_ctx)) {
goto out;
}
if (aws_cryptosdk_enc_ctx_clone(cache->allocator, &entry->enc_ctx, enc_ctx)) {
goto out;
}
if (materials->signctx) {
if (aws_cryptosdk_sig_get_privkey(materials->signctx, cache->allocator, &entry->key_materials)) {
goto out;
}
}
if (!locked_insert_entry(cache, entry)) {
/* Prevent the entry from being freed - and prepare to return it */
*ret_entry = (struct aws_cryptosdk_materials_cache_entry *)entry;
aws_atomic_fetch_add_explicit(&entry->refcount, 1, aws_memory_order_acq_rel);
entry = NULL;
}
out:
if (entry) {
/*
* If entry is non-NULL, it means we didn't successfully insert the entry.
* We will therefore destroy it immediately.
*/
destroy_cache_entry(entry);
}
if (aws_mutex_unlock(&cache->mutex)) {
abort();
}
}