Skip to content

Commit

Permalink
UCP: Fix 2nd round comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
leibin2014 committed Jul 2, 2020
1 parent 408f8bc commit dc50318
Showing 1 changed file with 21 additions and 73 deletions.
94 changes: 21 additions & 73 deletions src/ucp/core/ucp_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,60 +256,6 @@ ucp_mem_map_params2uct_flags(const ucp_mem_map_params_t *params)
return flags;
}

/* Matrix of behavior
* |--------------------------------------------------------------------------------|
* | parameter | value |
* |-----------|--------------------------------------------------------------------|
* | ALLOCATE | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
* | FIXED | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1 |
* | addr | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 |
* |-----------|--------|-----------|-----|-----|-----|-----------|-----|-----------|
* | result | err if | alloc/reg | err | reg | err | alloc/reg | err | alloc/reg |
* | | len >0 | | | | | (hint) | | (fixed) |
* |--------------------------------------------------------------------------------|
*/
static inline ucs_status_t ucp_mem_map_check_and_adjust_params(ucp_mem_map_params_t *params)
{
if (!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_LENGTH)) {
ucs_error("The length value for mapping memory isn't set: %s",
ucs_status_string(UCS_ERR_INVALID_PARAM));
return UCS_ERR_INVALID_PARAM;
}

/* First of all, define all fields */
if (!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_ADDRESS)) {
params->field_mask |= UCP_MEM_MAP_PARAM_FIELD_ADDRESS;
params->address = NULL;
}

if (!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_FLAGS)) {
params->field_mask |= UCP_MEM_MAP_PARAM_FIELD_FLAGS;
params->flags = 0;
}

if ((params->flags & UCP_MEM_MAP_FIXED) &&
(!params->address ||
((uintptr_t)params->address % ucs_get_page_size()))) {
ucs_error("UCP_MEM_MAP_FIXED flag requires page aligned address");
return UCS_ERR_INVALID_PARAM;
}

/* Now, lets check the rest of erroneous cases from the matrix */
if (params->address == NULL) {
if (!(params->flags & UCP_MEM_MAP_ALLOCATE) && (params->length > 0)) {
ucs_error("Undefined address with nonzero length requires "
"UCP_MEM_MAP_ALLOCATE flag");
return UCS_ERR_INVALID_PARAM;
}
} else if (!(params->flags & UCP_MEM_MAP_ALLOCATE) &&
(params->flags & UCP_MEM_MAP_FIXED)) {
ucs_error("Wrong combination of flags when address is defined");
return UCS_ERR_INVALID_PARAM;
}

return UCS_OK;
}

static inline int ucp_mem_map_is_allocate(const ucp_mem_map_params_t *params)
{
return (params->field_mask & UCP_MEM_MAP_PARAM_FIELD_FLAGS) &&
Expand Down Expand Up @@ -409,11 +355,24 @@ static ucs_status_t ucp_mem_unmap_common(ucp_context_h context, ucp_mem_h memh)
return status;
}

/* Matrix of behavior
* |--------------------------------------------------------------------------------|
* | parameter | value |
* |-----------|--------------------------------------------------------------------|
* | ALLOCATE | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
* | FIXED | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1 |
* | addr | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 |
* |-----------|--------|-----------|-----|-----|-----|-----------|-----|-----------|
* | result | err if | alloc/reg | err | reg | err | alloc/reg | err | alloc/reg |
* | | len >0 | | | | | (hint) | | (fixed) |
* |--------------------------------------------------------------------------------|
*/
ucs_status_t ucp_mem_map(ucp_context_h context, const ucp_mem_map_params_t *params,
ucp_mem_h *memh_p)
{
ucs_status_t status;
void *address;
unsigned flags;

/* always acquire context lock */
UCP_THREAD_CS_ENTER(&context->mt_lock);
Expand All @@ -425,29 +384,24 @@ ucs_status_t ucp_mem_map(ucp_context_h context, const ucp_mem_map_params_t *para
goto out;
}

if (((params->field_mask & UCP_MEM_MAP_PARAM_FIELD_FLAGS) &&
(params->flags & UCP_MEM_MAP_FIXED)) &&
(!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_ADDRESS) ||
((params->field_mask & UCP_MEM_MAP_PARAM_FIELD_ADDRESS) &&
((uintptr_t)params->address % ucs_get_page_size())))) {
address = UCP_PARAM_VALUE(MEM_MAP, params, address, ADDRESS, NULL);
flags = UCP_PARAM_VALUE(MEM_MAP, params, flags, FLAGS, 0);

if ((flags & UCP_MEM_MAP_FIXED) &&
((uintptr_t)address % ucs_get_page_size())) {
ucs_error("UCP_MEM_MAP_FIXED flag requires page aligned address");
status = UCS_ERR_INVALID_PARAM;
goto out;
}

if (!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_ADDRESS) ||
params->address == NULL) {
if (((params->field_mask & UCP_MEM_MAP_PARAM_FIELD_FLAGS) &&
(!(params->flags & UCP_MEM_MAP_ALLOCATE))) &&
( params->length > 0)) {
if (address == NULL) {
if (!(flags & UCP_MEM_MAP_ALLOCATE) && (params->length > 0)) {
ucs_error("Undefined address with nonzero length requires "
"UCP_MEM_MAP_ALLOCATE flag");
status = UCS_ERR_INVALID_PARAM;
goto out;
}
} else if ((params->field_mask & UCP_MEM_MAP_PARAM_FIELD_FLAGS) &&
!(params->flags & UCP_MEM_MAP_ALLOCATE) &&
(params->flags & UCP_MEM_MAP_FIXED)) {
} else if (!(flags & UCP_MEM_MAP_ALLOCATE) && (flags & UCP_MEM_MAP_FIXED)) {
ucs_error("Wrong combination of flags when address is defined");
status = UCS_ERR_INVALID_PARAM;
goto out;
Expand All @@ -460,12 +414,6 @@ ucs_status_t ucp_mem_map(ucp_context_h context, const ucp_mem_map_params_t *para
goto out;
}

if (!(params->field_mask & UCP_MEM_MAP_PARAM_FIELD_ADDRESS)) {
address = NULL;
} else {
address = params->address;
}

status = ucp_mem_map_common(context, address, params->length,
ucp_mem_map_params2uct_flags(params),
ucp_mem_map_is_allocate(params),
Expand Down

0 comments on commit dc50318

Please sign in to comment.