Skip to content

Commit

Permalink
Factor object internal deserialization an handle mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 20, 2024
1 parent a6a9cff commit 45963e5
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 316 deletions.
8 changes: 4 additions & 4 deletions src/cconfigspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ _ccs_object_deserialize(
{
uint32_t version;
size_t size;
_ccs_object_deserialize_options_t opts = {NULL, CCS_TRUE, NULL, NULL,
NULL, NULL, NULL};
_ccs_object_deserialize_options_t opts = {NULL, CCS_FALSE, NULL, NULL,
NULL, NULL, NULL};
CCS_VALIDATE(_ccs_object_deserialize_options(
format, operation, args, &opts));
CCS_VALIDATE(_ccs_deserialize_header(
Expand Down Expand Up @@ -700,8 +700,8 @@ _ccs_object_deserialize_file_descriptor(
int non_blocking;
size_t header_size;
ssize_t offset;
_ccs_object_deserialize_options_t opts = {NULL, CCS_TRUE, NULL, NULL,
NULL, NULL, NULL};
_ccs_object_deserialize_options_t opts = {NULL, CCS_FALSE, NULL, NULL,
NULL, NULL, NULL};
_ccs_file_descriptor_state_t state = {NULL, 0, NULL, 0, -1, 0};
_ccs_file_descriptor_state_t *pstate = NULL;
fd = va_arg(args, int);
Expand Down
22 changes: 15 additions & 7 deletions src/cconfigspace_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ _ccs_object_deserialize_options(
case CCS_DESERIALIZE_OPTION_HANDLE_MAP:
opts->handle_map = va_arg(args, ccs_map_t);
CCS_CHECK_OBJ(opts->handle_map, CCS_OBJECT_TYPE_MAP);
opts->map_values = CCS_TRUE;
break;
case CCS_DESERIALIZE_OPTION_VECTOR:
opts->vector = va_arg(args, void *);
Expand Down Expand Up @@ -222,16 +223,16 @@ _ccs_object_deserialize_with_opts(
const char **buffer,
_ccs_object_deserialize_options_t *opts)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
ccs_object_t obj;
ccs_result_t err = CCS_RESULT_SUCCESS;
ccs_object_t obj, handle;
_ccs_object_internal_t obj_internal;
switch (format) {
case CCS_SERIALIZE_FORMAT_BINARY: {
ccs_object_type_t otype;
CCS_VALIDATE(_ccs_peek_bin_ccs_object_type(
&otype, buffer_size, buffer));
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj_internal, buffer_size, buffer, &handle));
CCS_VALIDATE(_ccs_object_deserialize_with_opts_type(
&obj, otype, format, version, buffer_size, buffer,
opts));
&obj, obj_internal.type, format, version, buffer_size,
buffer, opts));
} break;
default:
CCS_RAISE(
Expand All @@ -243,6 +244,13 @@ _ccs_object_deserialize_with_opts(
_ccs_object_deserialize_user_data(
obj, format, version, buffer_size, buffer, opts),
errobj);
if (opts->map_values)
CCS_VALIDATE_ERR_GOTO(
err,
_ccs_object_handle_check_add(
opts->handle_map, handle, obj),
errobj);

*object_ret = obj;
return CCS_RESULT_SUCCESS;
errobj:
Expand Down
3 changes: 1 addition & 2 deletions src/cconfigspace_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,7 @@ _ccs_object_serialize_size_with_opts(
_ccs_object_serialize_options_t *opts)
{
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
CCS_VALIDATE(
obj->ops->serialize_size(object, format, cum_size, opts));
CCS_VALIDATE(obj->ops->serialize_size(object, format, cum_size, opts));
CCS_VALIDATE(_ccs_object_serialize_user_data_size(
object, format, cum_size, opts));
return CCS_RESULT_SUCCESS;
Expand Down
23 changes: 3 additions & 20 deletions src/configuration_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,14 @@ _ccs_deserialize_bin_configuration(
{
CCS_CHECK_OBJ(opts->handle_map, CCS_OBJECT_TYPE_MAP);
_ccs_object_deserialize_options_t new_opts = *opts;
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_datum_t d;
ccs_configuration_space_t cs;
ccs_configuration_t configuration;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_CONFIGURATION,
CCS_RESULT_ERROR_INVALID_TYPE);

new_opts.map_values = CCS_FALSE;
_ccs_configuration_data_mock_t data = {
{NULL, 0, NULL}, CCS_FALSE, NULL};
new_opts.map_values = CCS_FALSE;
_ccs_configuration_data_mock_t data = {
{NULL, 0, NULL}, CCS_FALSE, NULL};
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_deserialize_bin_ccs_configuration_data(
Expand All @@ -76,18 +69,8 @@ _ccs_deserialize_bin_configuration(
data.base.values, &configuration),
end);

if (opts->map_values)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)configuration),
err_configuration);
*configuration_ret = configuration;
goto end;

err_configuration:
ccs_release_object(configuration);
end:
if (data.features)
ccs_release_object(data.features);
Expand Down
19 changes: 3 additions & 16 deletions src/configuration_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,9 @@ _ccs_deserialize_bin_configuration_space(
_ccs_object_deserialize_options_t *opts)
{
_ccs_object_deserialize_options_t new_opts = *opts;
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_CONFIGURATION_SPACE,
CCS_RESULT_ERROR_INVALID_TYPE);
ccs_result_t res = CCS_RESULT_SUCCESS;

new_opts.map_values = CCS_TRUE;
new_opts.map_values = CCS_TRUE;
CCS_VALIDATE(ccs_create_map(&new_opts.handle_map));

_ccs_configuration_space_data_mock_t data = {
Expand All @@ -126,7 +119,7 @@ _ccs_deserialize_bin_configuration_space(
data.forbidden_clauses, data.feature_space, data.rng,
configuration_space_ret),
end);
if (opts && opts->map_values && opts->handle_map) {
if (opts->map_values) {
if (data.feature_space_handle)
CCS_VALIDATE_ERR_GOTO(
res,
Expand All @@ -135,12 +128,6 @@ _ccs_deserialize_bin_configuration_space(
data.feature_space_handle,
(ccs_object_t)data.feature_space),
err_configuration_space);
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)*configuration_space_ret),
err_configuration_space);
}
goto end;

Expand Down
21 changes: 0 additions & 21 deletions src/distribution_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,6 @@ _ccs_deserialize_bin_distribution(
const char **buffer,
_ccs_object_deserialize_options_t *opts)
{
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_result_t res;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_DISTRIBUTION,
CCS_RESULT_ERROR_INVALID_TYPE);

ccs_distribution_type_t dtype;
CCS_VALIDATE(_ccs_peek_bin_ccs_distribution_type(
&dtype, buffer_size, buffer));
Expand Down Expand Up @@ -355,19 +346,7 @@ _ccs_deserialize_bin_distribution(
CCS_RESULT_ERROR_UNSUPPORTED_OPERATION,
"Unsupported distribution type: %d", dtype);
}
if (opts && opts->handle_map)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)*distribution_ret),
err_dis);

return CCS_RESULT_SUCCESS;
err_dis:
ccs_release_object(*distribution_ret);
*distribution_ret = NULL;
return res;
}

static ccs_result_t
Expand Down
22 changes: 4 additions & 18 deletions src/distribution_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,10 @@ _ccs_deserialize_bin_distribution_space(
_ccs_object_deserialize_options_t new_opts = *opts;
new_opts.map_values = CCS_FALSE;
new_opts.handle_map = NULL;
_ccs_object_internal_t obj;
ccs_datum_t d;
ccs_configuration_space_t cs;
ccs_object_t handle;
ccs_distribution_space_t distrib_space;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_DISTRIBUTION_SPACE,
CCS_RESULT_ERROR_INVALID_TYPE);
ccs_datum_t d;
ccs_configuration_space_t cs;
ccs_distribution_space_t distrib_space;
ccs_result_t res = CCS_RESULT_SUCCESS;

_ccs_distribution_space_data_mock_t data = {NULL, 0, 0,
NULL, NULL, NULL};
Expand Down Expand Up @@ -123,13 +116,6 @@ _ccs_deserialize_bin_distribution_space(
err_distribution_space);
indices += data.dimensions[i];
}
if (opts->map_values)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)distrib_space),
err_distribution_space);
*distribution_space_ret = distrib_space;
goto end;

Expand Down
24 changes: 3 additions & 21 deletions src/evaluation_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,14 @@ _ccs_deserialize_bin_ccs_evaluation(
{
CCS_CHECK_OBJ(opts->handle_map, CCS_OBJECT_TYPE_MAP);
_ccs_object_deserialize_options_t new_opts = *opts;
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_datum_t d;
ccs_objective_space_t os;
ccs_evaluation_t evaluation;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_EVALUATION,
CCS_RESULT_ERROR_INVALID_TYPE);

new_opts.map_values = CCS_FALSE;
_ccs_evaluation_data_mock_t data = {
{NULL, 0, NULL}, NULL, CCS_RESULT_SUCCESS};
new_opts.map_values = CCS_FALSE;
_ccs_evaluation_data_mock_t data = {
{NULL, 0, NULL}, NULL, CCS_RESULT_SUCCESS};
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_deserialize_bin_ccs_evaluation_data(
Expand All @@ -77,18 +70,7 @@ _ccs_deserialize_bin_ccs_evaluation(
data.base.num_values, data.base.values, &evaluation),
end);

if (opts->map_values)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)evaluation),
err_evaluation);
*evaluation_ret = evaluation;
goto end;

err_evaluation:
ccs_release_object(evaluation);
end:
if (data.configuration)
ccs_release_object(data.configuration);
Expand Down
24 changes: 2 additions & 22 deletions src/expression_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,10 @@ _ccs_deserialize_bin_expression(
const char **buffer,
_ccs_object_deserialize_options_t *opts)
{
ccs_expression_type_t dtype;
_ccs_object_deserialize_options_t new_opts = *opts;
new_opts.map_values = CCS_FALSE;
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_result_t res;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_EXPRESSION,
CCS_RESULT_ERROR_INVALID_TYPE);

ccs_expression_type_t dtype;
new_opts.map_values = CCS_FALSE;
CCS_VALIDATE(
_ccs_peek_bin_ccs_expression_type(&dtype, buffer_size, buffer));
switch (dtype) {
Expand All @@ -192,19 +184,7 @@ _ccs_deserialize_bin_expression(
expression_ret, version, buffer_size, buffer,
&new_opts));
}
if (opts && opts->map_values && opts->handle_map)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)*expression_ret),
err_exp);

return CCS_RESULT_SUCCESS;
err_exp:
ccs_release_object(*expression_ret);
*expression_ret = NULL;
return res;
}

static ccs_result_t
Expand Down
24 changes: 3 additions & 21 deletions src/feature_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ _ccs_deserialize_bin_feature_space(
const char **buffer,
_ccs_object_deserialize_options_t *opts)
{
ccs_result_t res = CCS_RESULT_SUCCESS;
_ccs_object_deserialize_options_t new_opts = *opts;
new_opts.map_values = CCS_FALSE;
new_opts.handle_map = NULL;
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_FEATURE_SPACE,
CCS_RESULT_ERROR_INVALID_TYPE);

new_opts.map_values = CCS_FALSE;
_ccs_context_data_mock_t data;
CCS_VALIDATE_ERR_GOTO(
res,
Expand All @@ -34,17 +26,7 @@ _ccs_deserialize_bin_feature_space(
data.name, data.num_parameters, data.parameters,
feature_space_ret),
end);
if (opts && opts->map_values && opts->handle_map)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)*feature_space_ret),
err_feature_space);
goto end;
err_feature_space:
ccs_release_object(*feature_space_ret);
*feature_space_ret = NULL;

end:
if (data.parameters) {
for (size_t i = 0; i < data.num_parameters; i++)
Expand Down
25 changes: 3 additions & 22 deletions src/features_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ _ccs_deserialize_bin_features(
_ccs_object_deserialize_options_t *opts)
{
CCS_CHECK_OBJ(opts->handle_map, CCS_OBJECT_TYPE_MAP);
_ccs_object_internal_t obj;
ccs_object_t handle;
ccs_datum_t d;
ccs_feature_space_t cs;
ccs_result_t res = CCS_RESULT_SUCCESS;
CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal(
&obj, buffer_size, buffer, &handle));
CCS_REFUTE(
obj.type != CCS_OBJECT_TYPE_FEATURES,
CCS_RESULT_ERROR_INVALID_TYPE);
ccs_datum_t d;
ccs_feature_space_t cs;
ccs_result_t res = CCS_RESULT_SUCCESS;

_ccs_binding_data_t data = {NULL, 0, NULL};
CCS_VALIDATE_ERR_GOTO(
Expand All @@ -45,18 +38,6 @@ _ccs_deserialize_bin_features(
cs, data.num_values, data.values, features_ret),
end);

if (opts->map_values)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, handle,
(ccs_object_t)*features_ret),
err_features);
goto end;

err_features:
ccs_release_object(*features_ret);
*features_ret = NULL;
end:
if (data.values)
free(data.values);
Expand Down
Loading

0 comments on commit 45963e5

Please sign in to comment.