Skip to content

Commit

Permalink
Prevent parameters inclusion in several contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 10, 2024
1 parent f080e5e commit 79a6622
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
5 changes: 3 additions & 2 deletions include/cconfigspace/configuration_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ extern "C" {
* rng is not NULL and is not a valid CCS rng
* @return #CCS_RESULT_ERROR_INVALID_PARAMETER if a parameter's type is
* CCS_PARAMETER_TYPE_STRING; or if a parameter appears more than once in \p
* parameters; or if two or more parameters share the same name; or if an
* expression references a parameter that is not in the configuration space
* parameters; or if two or more parameters share the same name; or if a
* paramater is already part of another context; or if an expression references
* a parameter that is not in the configuration space
* @return #CCS_RESULT_ERROR_INVALID_CONFIGURATION if adding one of the provided
* forbidden clause would render the default configuration invalid
* @return #CCS_RESULT_ERROR_INVALID_GRAPH if the addition of the conditions
Expand Down
3 changes: 2 additions & 1 deletion include/cconfigspace/feature_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extern "C" {
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if a parameter is not a valid CCS
* parameter
* @return #CCS_RESULT_ERROR_INVALID_PARAMETER if a parameter appears more than
* once in \p parameters; or if two or more parameters share the same name
* once in \p parameters; or if two or more parameters share the same name; or
* if a paramater is already part of another context
* @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was a lack of memory to
* allocate the new feature space
* @remarks
Expand Down
3 changes: 2 additions & 1 deletion include/cconfigspace/objective_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ typedef enum ccs_objective_type_e ccs_objective_type_t;
* parameter; or if an expressions is not a valid CCS expression
* @return #CCS_RESULT_ERROR_INVALID_PARAMETER if a parameter appears more than
* once in \p parameters; or if two or more parameters share the same name; or
* if an expression references a parameter that is not in \p parameters
* if a paramater is already part of another context; or if an expression
* references a parameter that is not in \p parameters
* @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was a lack of memory to
* allocate the new objective space
* @remarks
Expand Down
6 changes: 5 additions & 1 deletion src/configuration_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ _ccs_configuration_space_del(ccs_object_t object)
_ccs_configuration_space_data_t *data = configuration_space->data;

for (size_t i = 0; i < data->num_parameters; i++) {
if (data->parameters[i])
if (data->parameters[i]) {
_ccs_parameter_release_ownership(
data->parameters[i],
(ccs_context_t)configuration_space);
ccs_release_object(data->parameters[i]);
}
if (data->conditions[i])
ccs_release_object(data->conditions[i]);
if (data->parents[i])
Expand Down
6 changes: 6 additions & 0 deletions src/context_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ _ccs_context_add_parameter(
ccs_parameter_t parameter,
size_t index)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
const char *name;
size_t sz_name;
_ccs_parameter_index_hash_t *parameter_hash;
Expand All @@ -169,8 +170,13 @@ _ccs_context_add_parameter(
sz_name, parameter_hash);

CCS_VALIDATE(ccs_retain_object(parameter));
CCS_VALIDATE_ERR_GOTO(
err, _ccs_parameter_take_ownership(parameter, context), error);
context->data->parameters[index] = parameter;
return CCS_RESULT_SUCCESS;
error:
ccs_release_object(parameter);
return err;
}
#undef uthash_nonfatal_oom

Expand Down
5 changes: 4 additions & 1 deletion src/feature_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ _ccs_feature_space_del(ccs_object_t object)
ccs_parameter_t *parameters = feature_space->data->parameters;

for (size_t i = 0; i < num_parameters; i++)
if (parameters[i])
if (parameters[i]) {
_ccs_parameter_release_ownership(
parameters[i], (ccs_context_t)feature_space);
ccs_release_object(parameters[i]);
}

HASH_CLEAR(hh_name, feature_space->data->name_hash);
HASH_CLEAR(hh_handle, feature_space->data->handle_hash);
Expand Down
5 changes: 4 additions & 1 deletion src/objective_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ _ccs_objective_space_del(ccs_object_t object)
if (search_space)
ccs_release_object(search_space);
for (size_t i = 0; i < num_parameters; i++)
if (parameters[i])
if (parameters[i]) {
_ccs_parameter_release_ownership(
parameters[i], (ccs_context_t)objective_space);
ccs_release_object(parameters[i]);
}

for (size_t i = 0; i < num_objectives; i++)
if (objectives[i].expression)
Expand Down
33 changes: 33 additions & 0 deletions src/parameter_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct _ccs_parameter_s {

struct _ccs_parameter_common_data_s {
ccs_parameter_type_t type;
// a weak erference to a context to prevent multiple inclusion
ccs_context_t ctx;
const char *name;
ccs_datum_t default_value;
ccs_interval_t interval;
Expand Down Expand Up @@ -155,4 +157,35 @@ _ccs_deserialize_bin_ccs_parameter_numerical_data(
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_ccs_parameter_take_ownership(ccs_parameter_t param, ccs_context_t ctx)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
_ccs_parameter_common_data_t *data =
(_ccs_parameter_common_data_t *)param->data;
CCS_OBJ_WRLOCK(param);
CCS_REFUTE_ERR_GOTO(
err, data->ctx, CCS_RESULT_ERROR_INVALID_PARAMETER, error);
data->ctx = ctx;
error:
CCS_OBJ_UNLOCK(param);
return err;
}

static inline ccs_result_t
_ccs_parameter_release_ownership(ccs_parameter_t param, ccs_context_t ctx)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
_ccs_parameter_common_data_t *data =
(_ccs_parameter_common_data_t *)param->data;
CCS_OBJ_WRLOCK(param);
CCS_REFUTE_ERR_GOTO(
err, data->ctx != ctx, CCS_RESULT_ERROR_INVALID_PARAMETER,
error);
data->ctx = NULL;
error:
CCS_OBJ_UNLOCK(param);
return err;
}

#endif //_PARAMETER_INTERNAL_H

0 comments on commit 79a6622

Please sign in to comment.