Skip to content

Commit

Permalink
[C] Refine types of publication params.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed May 2, 2019
1 parent 37a21fc commit d0bafa2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
18 changes: 8 additions & 10 deletions aeron-driver/src/main/c/aeron_driver_conductor.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,7 @@ aeron_ipc_publication_t *aeron_driver_conductor_get_or_add_ipc_publication(
if (ensure_capacity_result >= 0)
{
int32_t session_id = conductor->next_session_id++;
int32_t initial_term_id = params->is_replay ?
(int32_t)params->initial_term_id : aeron_randomised_int32();
int32_t initial_term_id = params->is_replay ? params->initial_term_id : aeron_randomised_int32();
aeron_position_t pub_pos_position;
aeron_position_t pub_lmt_position;

Expand All @@ -719,9 +718,9 @@ aeron_ipc_publication_t *aeron_driver_conductor_get_or_add_ipc_publication(
if (params->is_replay)
{
int64_t position = aeron_logbuffer_compute_position(
(int32_t)params->term_id,
(int32_t)params->term_offset,
(size_t)aeron_number_of_trailing_zeroes((int32_t)params->term_length),
params->term_id,
params->term_offset,
(size_t)aeron_number_of_trailing_zeroes(params->term_length),
initial_term_id);

aeron_counter_set_ordered(pub_pos_position.value_addr, position);
Expand Down Expand Up @@ -812,8 +811,7 @@ aeron_network_publication_t *aeron_driver_conductor_get_or_add_network_publicati
if (ensure_capacity_result >= 0)
{
int32_t session_id = conductor->next_session_id++;
int32_t initial_term_id = params->is_replay ?
(int32_t)params->initial_term_id : aeron_randomised_int32();
int32_t initial_term_id = params->is_replay ? params->initial_term_id : aeron_randomised_int32();

aeron_position_t pub_pos_position;
aeron_position_t pub_lmt_position;
Expand Down Expand Up @@ -853,9 +851,9 @@ aeron_network_publication_t *aeron_driver_conductor_get_or_add_network_publicati
if (params->is_replay)
{
int64_t position = aeron_logbuffer_compute_position(
(int32_t)params->term_id,
(int32_t)params->term_offset,
(size_t)aeron_number_of_trailing_zeroes((int32_t)params->term_length),
params->term_id,
params->term_offset,
(size_t)aeron_number_of_trailing_zeroes(params->term_length),
initial_term_id);

aeron_counter_set_ordered(pub_pos_position.value_addr, position);
Expand Down
42 changes: 23 additions & 19 deletions aeron-driver/src/main/c/uri/aeron_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,15 @@ int aeron_uri_publication_params(

if (is_exclusive)
{
const char *initial_term_id_str = NULL;
const char *term_id_str = NULL;
const char *term_offset_str = NULL;
int count = 0;

initial_term_id_str = aeron_uri_find_param_value(uri_params, AERON_URI_INITIAL_TERM_ID_KEY);
const char *initial_term_id_str = aeron_uri_find_param_value(uri_params, AERON_URI_INITIAL_TERM_ID_KEY);
count += initial_term_id_str ? 1 : 0;

term_id_str = aeron_uri_find_param_value(uri_params, AERON_URI_TERM_ID_KEY);
const char *term_id_str = aeron_uri_find_param_value(uri_params, AERON_URI_TERM_ID_KEY);
count += term_id_str ? 1 : 0;

term_offset_str = aeron_uri_find_param_value(uri_params, AERON_URI_TERM_OFFSET_KEY);
const char *term_offset_str = aeron_uri_find_param_value(uri_params, AERON_URI_TERM_OFFSET_KEY);
count += term_offset_str ? 1 : 0;

if (count > 0)
Expand All @@ -444,7 +441,8 @@ int aeron_uri_publication_params(

errno = 0;
end_ptr = NULL;
if (((params->initial_term_id = strtoll(initial_term_id_str, &end_ptr, 0)) == 0 && 0 != errno) ||
int64_t initial_term_id = 0;
if (((initial_term_id = strtoll(initial_term_id_str, &end_ptr, 0)) == 0 && 0 != errno) ||
end_ptr == initial_term_id_str)
{
aeron_set_err(EINVAL, "could not parse %s in URI", AERON_URI_INITIAL_TERM_ID_KEY);
Expand All @@ -453,7 +451,8 @@ int aeron_uri_publication_params(

errno = 0;
end_ptr = NULL;
if (((params->term_id = strtoll(term_id_str, &end_ptr, 0)) == 0 && 0 != errno) ||
int64_t term_id = 0;
if (((term_id = strtoll(term_id_str, &end_ptr, 0)) == 0 && 0 != errno) ||
end_ptr == term_id_str)
{
aeron_set_err(EINVAL, "could not parse %s in URI", AERON_URI_TERM_ID_KEY);
Expand All @@ -462,52 +461,57 @@ int aeron_uri_publication_params(

errno = 0;
end_ptr = NULL;
if (((params->term_offset = strtoull(term_offset_str, &end_ptr, 0)) == 0 && 0 != errno) ||
uint64_t term_offset = 0;
if (((term_offset = strtoull(term_offset_str, &end_ptr, 0)) == 0 && 0 != errno) ||
end_ptr == term_offset_str)
{
aeron_set_err(EINVAL, "could not parse %s in URI", AERON_URI_TERM_OFFSET_KEY);
return -1;
}

if (params->initial_term_id < INT32_MIN || params->initial_term_id > INT32_MAX)
if (initial_term_id < INT32_MIN || initial_term_id > INT32_MAX)
{
aeron_set_err(
EINVAL,
"Params %s=%" PRId64 " out of range", AERON_URI_INITIAL_TERM_ID_KEY, params->initial_term_id);
"Params %s=%" PRId64 " out of range", AERON_URI_INITIAL_TERM_ID_KEY, initial_term_id);
return -1;
}

if (params->term_id < INT32_MIN || params->term_id > INT32_MAX)
if (term_id < INT32_MIN || term_id > INT32_MAX)
{
aeron_set_err(EINVAL, "Params %s=%" PRId64 " out of range", AERON_URI_TERM_ID_KEY, params->term_id);
aeron_set_err(EINVAL, "Params %s=%" PRId64 " out of range", AERON_URI_TERM_ID_KEY, term_id);
return -1;
}

if (((int32_t)params->term_id - (int32_t)params->initial_term_id) < 0)
if (((int32_t)term_id - (int32_t)initial_term_id) < 0)
{
aeron_set_err(
EINVAL,
"Param difference greater than 2^31 - 1: %s=%" PRId64 " %s=%" PRId64,
AERON_URI_INITIAL_TERM_ID_KEY,
params->initial_term_id,
initial_term_id,
AERON_URI_TERM_OFFSET_KEY,
params->term_id);
term_id);
return -1;
}

if (params->term_offset > params->term_length)
if (term_offset > params->term_length)
{
aeron_set_err(
EINVAL,
"Param %s=%" PRIu64 " > %s=%" PRIu64,
AERON_URI_TERM_OFFSET_KEY,
params->term_offset,
term_offset,
AERON_URI_TERM_LENGTH_KEY,
params->term_length);
return -1;
}

if ((params->term_offset & (AERON_LOGBUFFER_FRAME_ALIGNMENT - 1)) != 0)
params->term_offset = term_offset;
params->initial_term_id = (int32_t)initial_term_id;
params->term_id = (int32_t)term_id;

if ((params->term_offset & (AERON_LOGBUFFER_FRAME_ALIGNMENT - 1u)) != 0)
{
aeron_set_err(
EINVAL,
Expand Down
8 changes: 4 additions & 4 deletions aeron-driver/src/main/c/uri/aeron_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ typedef struct aeron_uri_publication_params_stct
bool is_replay;
bool is_sparse;
bool signal_eos;
size_t term_length;
size_t mtu_length;
size_t term_length;
size_t term_offset;
int32_t initial_term_id;
int32_t term_id;
uint64_t linger_timeout_ns;
int64_t initial_term_id;
int64_t term_id;
uint64_t term_offset;
}
aeron_uri_publication_params_t;

Expand Down

0 comments on commit d0bafa2

Please sign in to comment.