proxy_create (Proxy **res, CK_FUNCTION_LIST **loaded,
	      Mapping *mappings, unsigned int n_mappings)
{
	CK_RV rv = CKR_OK;
	Proxy *py;

	py = calloc (1, sizeof (Proxy));
	return_val_if_fail (py != NULL, CKR_HOST_MEMORY);

	py->forkid = p11_forkid;
	py->last_id = 0;

	py->inited = modules_dup (loaded);
	if (py->inited == NULL) {
		proxy_free (py, 0);
		return_val_if_reached (CKR_HOST_MEMORY);
	}

	rv = p11_kit_modules_initialize (py->inited, NULL);

	if (rv == CKR_OK) {
		rv = proxy_list_slots (py, mappings, n_mappings);
	}

	if (rv != CKR_OK) {
		proxy_free (py, 1);
		return rv;
	}

	py->sessions = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, NULL, free);
	if (py->sessions == NULL) {
		proxy_free (py, 1);
		return_val_if_reached (CKR_HOST_MEMORY);
	}
	py->refs = 1;

	*res = py;
	return CKR_OK;
}
proxy_create (Proxy **res, CK_FUNCTION_LIST **loaded,
	      Mapping *mappings, unsigned int n_mappings)
{
	CK_RV rv = CKR_OK;
	Proxy *py;

	py = calloc (1, sizeof (Proxy));
	return_val_if_fail (py != NULL, CKR_HOST_MEMORY);

	py->forkid = p11_forkid;
	py->last_id = 0;

	py->inited = modules_dup (loaded);
	if (py->inited == NULL) {
		proxy_free (py, 0);
		return_val_if_reached (CKR_HOST_MEMORY);
	}

	rv = p11_kit_modules_initialize (py->inited, NULL);

	if (rv == CKR_OK) {
		rv = proxy_list_slots (py, mappings, n_mappings);
	}

	if (rv != CKR_OK) {
		proxy_free (py, 1);
		return rv;
	}

	py->sessions = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, NULL, free);
	if (py->sessions == NULL) {
		proxy_free (py, 1);
		return_val_if_reached (CKR_HOST_MEMORY);
	}
	py->refs = 1;

	*res = py;
	return CKR_OK;
}