Skip to content

Commit

Permalink
Refactor object deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 7, 2024
1 parent 11128a0 commit a767b0b
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 308 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ libcconfigspace_la_SOURCES = \
utlist.h \
cconfigspace.c \
cconfigspace_internal.h \
cconfigspace_deserialize.h \
error_stack.c \
error_stack_internal.h \
interval.c \
Expand Down
240 changes: 34 additions & 206 deletions src/cconfigspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,24 @@ _ccs_object_serialize_size_with_opts(
_ccs_object_serialize_options_t *opts)
{
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
*buffer_size = _ccs_serialize_header_size(format);
CCS_VALIDATE(
obj->ops->serialize_size(object, format, buffer_size, opts));
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_object_header_serialize_size_with_opts(
ccs_object_t object,
ccs_serialize_format_t format,
size_t *buffer_size,
_ccs_object_serialize_options_t *opts)
{
*buffer_size = _ccs_serialize_header_size(format);
CCS_VALIDATE(_ccs_object_serialize_size_with_opts(
object, format, buffer_size, opts));
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_object_serialize_size(
ccs_object_t object,
Expand All @@ -325,11 +337,25 @@ _ccs_object_serialize_size(
CCS_VALIDATE(_ccs_object_serialize_options(
format, CCS_SERIALIZE_OPERATION_SIZE, args, &opts));
CCS_CHECK_PTR(p_buffer_size);
CCS_VALIDATE(_ccs_object_serialize_size_with_opts(
CCS_VALIDATE(_ccs_object_header_serialize_size_with_opts(
object, format, p_buffer_size, &opts));
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_object_serialize_with_opts(
ccs_object_t object,
ccs_serialize_format_t format,
size_t *buffer_size,
char **buffer,
_ccs_object_serialize_options_t *opts)
{
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
CCS_VALIDATE(obj->ops->serialize(
object, format, buffer_size, buffer, opts));
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_object_serialize_memory_with_opts(
ccs_object_t object,
Expand All @@ -338,11 +364,10 @@ _ccs_object_serialize_memory_with_opts(
char *buffer,
_ccs_object_serialize_options_t *opts)
{
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
size_t total_size = buffer_size;
char *buffer_start = buffer;
size_t total_size = buffer_size;
char *buffer_start = buffer;
CCS_VALIDATE(_ccs_serialize_header(format, &buffer_size, &buffer, 0));
CCS_VALIDATE(obj->ops->serialize(
CCS_VALIDATE(_ccs_object_serialize_with_opts(
object, format, &buffer_size, &buffer, opts));
CCS_VALIDATE(_ccs_serialize_header(
format, &total_size, &buffer_start, total_size - buffer_size));
Expand Down Expand Up @@ -390,7 +415,7 @@ _ccs_object_serialize_file(
CCS_REFUTE(fd == -1, CCS_RESULT_ERROR_INVALID_FILE_PATH);
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_serialize_size_with_opts(
_ccs_object_header_serialize_size_with_opts(
object, format, &buffer_size, &opts),
err_file_fd);
buffer = (char *)mmap(
Expand Down Expand Up @@ -460,7 +485,7 @@ _ccs_object_serialize_file_descriptor(
/* if non blocking start or blocking */
if (!pstate || !pstate->base) {
size_t object_size;
CCS_VALIDATE(_ccs_object_serialize_size_with_opts(
CCS_VALIDATE(_ccs_object_header_serialize_size_with_opts(
object, format, &object_size, &opts));
/* initialize user_state */
if (!pstate) {
Expand Down Expand Up @@ -561,204 +586,7 @@ ccs_object_serialize(
return res;
}

#include "rng_deserialize.h"
#include "distribution_deserialize.h"
#include "parameter_deserialize.h"
#include "expression_deserialize.h"
#include "feature_space_deserialize.h"
#include "configuration_space_deserialize.h"
#include "distribution_space_deserialize.h"
#include "objective_space_deserialize.h"
#include "configuration_deserialize.h"
#include "evaluation_deserialize.h"
#include "features_deserialize.h"
#include "features_evaluation_deserialize.h"
#include "tuner_deserialize.h"
#include "features_tuner_deserialize.h"
#include "map_deserialize.h"
#include "tree_deserialize.h"
#include "tree_space_deserialize.h"
#include "tree_configuration_deserialize.h"
#include "tree_evaluation_deserialize.h"
#include "tree_tuner_deserialize.h"

static inline ccs_result_t
_ccs_object_deserialize_options(
ccs_serialize_format_t format,
ccs_serialize_operation_t operation,
va_list args,
_ccs_object_deserialize_options_t *opts)
{
(void)format;
ccs_deserialize_option_t opt =
(ccs_deserialize_option_t)va_arg(args, int32_t);
while (opt != CCS_DESERIALIZE_OPTION_END) {
switch (opt) {
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);
break;
case CCS_DESERIALIZE_OPTION_VECTOR:
opts->vector = va_arg(args, void *);
CCS_CHECK_PTR(opts->vector);
break;
case CCS_DESERIALIZE_OPTION_DATA:
opts->data = va_arg(args, void *);
break;
case CCS_DESERIALIZE_OPTION_NON_BLOCKING:
CCS_REFUTE(
operation !=
CCS_SERIALIZE_OPERATION_FILE_DESCRIPTOR,
CCS_RESULT_ERROR_INVALID_VALUE);
opts->ppfd_state =
va_arg(args, _ccs_file_descriptor_state_t **);
CCS_CHECK_PTR(opts->ppfd_state);
break;
case CCS_DESERIALIZE_OPTION_CALLBACK:
opts->deserialize_callback =
va_arg(args, ccs_object_deserialize_callback_t);
CCS_CHECK_PTR(opts->deserialize_callback);
opts->deserialize_user_data = va_arg(args, void *);
break;
default:
CCS_RAISE(
CCS_RESULT_ERROR_INVALID_VALUE,
"Unsupported deserialization option: %d", opt);
}
opt = (ccs_deserialize_option_t)va_arg(args, int32_t);
}
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_object_deserialize_with_opts(
ccs_object_t *object_ret,
ccs_serialize_format_t format,
uint32_t version,
size_t *buffer_size,
const char **buffer,
_ccs_object_deserialize_options_t *opts)
{
switch (format) {
case CCS_SERIALIZE_FORMAT_BINARY: {
ccs_object_type_t otype;
CCS_VALIDATE(_ccs_peek_bin_ccs_object_type(
&otype, buffer_size, buffer));
switch (otype) {
case CCS_OBJECT_TYPE_RNG:
CCS_VALIDATE(_ccs_rng_deserialize(
(ccs_rng_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_DISTRIBUTION:
CCS_VALIDATE(_ccs_distribution_deserialize(
(ccs_distribution_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_PARAMETER:
CCS_VALIDATE(_ccs_parameter_deserialize(
(ccs_parameter_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_EXPRESSION:
CCS_VALIDATE(_ccs_expression_deserialize(
(ccs_expression_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_FEATURE_SPACE:
CCS_VALIDATE(_ccs_feature_space_deserialize(
(ccs_feature_space_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_CONFIGURATION_SPACE:
CCS_VALIDATE(_ccs_configuration_space_deserialize(
(ccs_configuration_space_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_OBJECTIVE_SPACE:
CCS_VALIDATE(_ccs_objective_space_deserialize(
(ccs_objective_space_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_CONFIGURATION:
CCS_VALIDATE(_ccs_configuration_deserialize(
(ccs_configuration_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_EVALUATION:
CCS_VALIDATE(_ccs_evaluation_deserialize(
(ccs_evaluation_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_FEATURES:
CCS_VALIDATE(_ccs_features_deserialize(
(ccs_features_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_FEATURES_EVALUATION:
CCS_VALIDATE(_ccs_features_evaluation_deserialize(
(ccs_features_evaluation_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TUNER:
CCS_VALIDATE(_ccs_tuner_deserialize(
(ccs_tuner_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_FEATURES_TUNER:
CCS_VALIDATE(_ccs_features_tuner_deserialize(
(ccs_features_tuner_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_MAP:
CCS_VALIDATE(_ccs_map_deserialize(
(ccs_map_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TREE:
CCS_VALIDATE(_ccs_tree_deserialize(
(ccs_tree_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TREE_SPACE:
CCS_VALIDATE(_ccs_tree_space_deserialize(
(ccs_tree_space_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TREE_CONFIGURATION:
CCS_VALIDATE(_ccs_tree_configuration_deserialize(
(ccs_tree_configuration_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TREE_EVALUATION:
CCS_VALIDATE(_ccs_tree_evaluation_deserialize(
(ccs_tree_evaluation_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_TREE_TUNER:
CCS_VALIDATE(_ccs_tree_tuner_deserialize(
(ccs_tree_tuner_t *)object_ret, format, version,
buffer_size, buffer, opts));
break;
case CCS_OBJECT_TYPE_DISTRIBUTION_SPACE:
CCS_VALIDATE(_ccs_distribution_space_deserialize(
(ccs_distribution_space_t *)object_ret, format,
version, buffer_size, buffer, opts));
break;
default:
CCS_RAISE(
CCS_RESULT_ERROR_UNSUPPORTED_OPERATION,
"Unsupported object type: %d", otype);
}
break;
}
default:
CCS_RAISE(
CCS_RESULT_ERROR_INVALID_VALUE,
"Unsupported serialization format: %d", format);
}
return CCS_RESULT_SUCCESS;
}
#include "cconfigspace_deserialize.h"

static inline ccs_result_t
_ccs_object_deserialize(
Expand Down
Loading

0 comments on commit a767b0b

Please sign in to comment.