root/opal/dss/dss_load_unload.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_dss_unload
  2. opal_dss_load
  3. opal_dss_copy_payload
  4. opal_value_load
  5. opal_value_unload
  6. opal_value_xfer

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2006 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2014-2018 Intel, Inc. All rights reserved.
  13  * Copyright (c) 2015-2017 Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2018      IBM Corporation.  All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 
  23 /*
  24  * DSS Buffer Operations
  25  */
  26 #include "opal_config.h"
  27 
  28 #include "opal/util/error.h"
  29 
  30 #include "opal/dss/dss_internal.h"
  31 
  32 
  33 int opal_dss_unload(opal_buffer_t *buffer, void **payload,
  34                     int32_t *bytes_used)
  35 {
  36     /* check that buffer is not null */
  37     if (!buffer) {
  38         return OPAL_ERR_BAD_PARAM;
  39     }
  40 
  41     /* were we given someplace to point to the payload */
  42     if (NULL == payload) {
  43         return OPAL_ERR_BAD_PARAM;
  44     }
  45 
  46     /* anything in the buffer - if not, nothing to do */
  47     if (NULL == buffer->base_ptr || 0 == buffer->bytes_used) {
  48         *payload = NULL;
  49         *bytes_used = 0;
  50         return OPAL_SUCCESS;
  51     }
  52 
  53     /* if nothing has been unpacked, we can pass the entire
  54      * region back and protect it - no need to copy. This is
  55      * an optimization */
  56     if (buffer->unpack_ptr == buffer->base_ptr) {
  57         *payload = buffer->base_ptr;
  58         *bytes_used = buffer->bytes_used;
  59         buffer->base_ptr = NULL;
  60         buffer->unpack_ptr = NULL;
  61         buffer->pack_ptr = NULL;
  62         buffer->bytes_used = 0;
  63         return OPAL_SUCCESS;
  64     }
  65 
  66     /* okay, we have something to provide - pass it back */
  67     *bytes_used = buffer->bytes_used - (buffer->unpack_ptr - buffer->base_ptr);
  68     if (0 == (*bytes_used)) {
  69         *payload = NULL;
  70     } else {
  71         /* we cannot just set the pointer as it might be
  72          * partway in a malloc'd region */
  73         *payload = (void*)malloc(*bytes_used);
  74         memcpy(*payload, buffer->unpack_ptr, *bytes_used);
  75     }
  76 
  77     /* All done */
  78 
  79     return OPAL_SUCCESS;
  80 }
  81 
  82 
  83 int opal_dss_load(opal_buffer_t *buffer, void *payload,
  84                   int32_t bytes_used)
  85 {
  86     /* check to see if the buffer has been initialized */
  87     if (NULL == buffer) {
  88         return OPAL_ERR_BAD_PARAM;
  89     }
  90 
  91     /* check if buffer already has payload - free it if so */
  92     if (NULL != buffer->base_ptr) {
  93         free(buffer->base_ptr);
  94     }
  95 
  96     /* if it's a NULL payload, just set things and return */
  97     if (NULL == payload) {
  98         buffer->base_ptr = NULL;
  99         buffer->pack_ptr = buffer->base_ptr;
 100         buffer->unpack_ptr = buffer->base_ptr;
 101         buffer->bytes_used = 0;
 102         buffer->bytes_allocated = 0;
 103         return OPAL_SUCCESS;
 104     }
 105 
 106     /* populate the buffer */
 107     buffer->base_ptr = (char*)payload;
 108 
 109     /* set pack/unpack pointers */
 110     buffer->pack_ptr = ((char*)buffer->base_ptr) + bytes_used;
 111     buffer->unpack_ptr = buffer->base_ptr;
 112 
 113     /* set counts for size and space */
 114     buffer->bytes_allocated = buffer->bytes_used = bytes_used;
 115 
 116     /* All done */
 117 
 118     return OPAL_SUCCESS;
 119 }
 120 
 121 
 122 /* Copy the UNPACKED portion of a source buffer into a destination buffer
 123  * The complete contents of the src buffer are NOT copied - only that
 124  * portion that has not been previously unpacked is copied.
 125  */
 126 int opal_dss_copy_payload(opal_buffer_t *dest, opal_buffer_t *src)
 127 {
 128     char *dst_ptr;
 129     int32_t bytes_left;
 130 
 131     /* ensure we have valid source and destination */
 132     if (NULL == dest || NULL == src) {
 133         return OPAL_ERR_BAD_PARAM;
 134     }
 135 
 136     /* if the dest is already populated, check to ensure that both
 137      * source and dest are of the same buffer type
 138      */
 139     if (0 != dest->bytes_used) {
 140         if (dest->type != src->type) {
 141             return OPAL_ERR_BUFFER;
 142         }
 143     }
 144 
 145     /* either the dest was empty or the two types already match -
 146      * either way, just ensure the two types DO match
 147      */
 148     dest->type = src->type;
 149 
 150     /* compute how much of the src buffer remains unpacked
 151      * buffer->bytes_used is the total number of bytes in the buffer that
 152      * have been packed. However, we may have already unpacked some of
 153      * that data. We only want to unload what remains unpacked. This
 154      * means we have to look at how much of the buffer remains "used"
 155      * beyond the unpack_ptr
 156      */
 157     bytes_left = src->bytes_used - (src->unpack_ptr - src->base_ptr);
 158 
 159     /* if nothing is left, then nothing to do */
 160     if (0 == bytes_left) {
 161         return OPAL_SUCCESS;
 162     }
 163 
 164     /* add room to the dest for the src buffer's payload */
 165     if (NULL == (dst_ptr = opal_dss_buffer_extend(dest, bytes_left))) {
 166         return OPAL_ERR_OUT_OF_RESOURCE;
 167     }
 168 
 169     /* copy the src payload to the specified location in dest */
 170     memcpy(dst_ptr, src->unpack_ptr, bytes_left);
 171 
 172     /* adjust the dest buffer's bookkeeping */
 173     dest->bytes_used += bytes_left;
 174     dest->pack_ptr = ((char*)dest->pack_ptr) + bytes_left;
 175 
 176     return OPAL_SUCCESS;
 177 }
 178 
 179 int opal_value_load(opal_value_t *kv,
 180                     void *data, opal_data_type_t type)
 181 {
 182     opal_byte_object_t *boptr;
 183     struct timeval *tv;
 184 
 185     kv->type = type;
 186     if (NULL == data && OPAL_STRING != type && OPAL_BYTE_OBJECT != type) {
 187         /* just set the fields to zero */
 188         memset(&kv->data, 0, sizeof(kv->data));
 189         return OPAL_SUCCESS;
 190     }
 191 
 192     switch (type) {
 193     case OPAL_BOOL:
 194         kv->data.flag = *(bool*)(data);
 195         break;
 196     case OPAL_BYTE:
 197         kv->data.byte = *(uint8_t*)(data);
 198         break;
 199     case OPAL_STRING:
 200         if (NULL != kv->data.string) {
 201             free(kv->data.string);
 202         }
 203         if (NULL != data) {
 204             kv->data.string = strdup( (const char *) data);
 205         } else {
 206             kv->data.string = NULL;
 207         }
 208         break;
 209     case OPAL_SIZE:
 210         kv->data.size = *(size_t*)(data);
 211         break;
 212     case OPAL_PID:
 213         kv->data.pid = *(pid_t*)(data);
 214         break;
 215 
 216     case OPAL_INT:
 217         kv->data.integer = *(int*)(data);
 218         break;
 219     case OPAL_INT8:
 220         kv->data.int8 = *(int8_t*)(data);
 221         break;
 222     case OPAL_INT16:
 223         kv->data.int16 = *(int16_t*)(data);
 224         break;
 225     case OPAL_INT32:
 226         kv->data.int32 = *(int32_t*)(data);
 227         break;
 228     case OPAL_INT64:
 229         kv->data.int64 = *(int64_t*)(data);
 230         break;
 231 
 232     case OPAL_UINT:
 233         kv->data.uint = *(unsigned int*)(data);
 234         break;
 235     case OPAL_UINT8:
 236         kv->data.uint8 = *(uint8_t*)(data);
 237         break;
 238     case OPAL_UINT16:
 239         kv->data.uint16 = *(uint16_t*)(data);
 240         break;
 241     case OPAL_UINT32:
 242         kv->data.uint32 = *(uint32_t*)data;
 243         break;
 244     case OPAL_UINT64:
 245         kv->data.uint64 = *(uint64_t*)(data);
 246         break;
 247 
 248     case OPAL_BYTE_OBJECT:
 249         if (NULL != kv->data.bo.bytes) {
 250             free(kv->data.bo.bytes);
 251         }
 252         boptr = (opal_byte_object_t*)data;
 253         if (NULL != boptr && NULL != boptr->bytes && 0 < boptr->size) {
 254             kv->data.bo.bytes = (uint8_t *) malloc(boptr->size);
 255             memcpy(kv->data.bo.bytes, boptr->bytes, boptr->size);
 256             kv->data.bo.size = boptr->size;
 257         } else {
 258             kv->data.bo.bytes = NULL;
 259             kv->data.bo.size = 0;
 260         }
 261         break;
 262 
 263     case OPAL_FLOAT:
 264         kv->data.fval = *(float*)(data);
 265         break;
 266 
 267     case OPAL_TIMEVAL:
 268         tv = (struct timeval*)data;
 269         kv->data.tv.tv_sec = tv->tv_sec;
 270         kv->data.tv.tv_usec = tv->tv_usec;
 271         break;
 272 
 273     case OPAL_PTR:
 274         kv->data.ptr = data;
 275         break;
 276 
 277     default:
 278         OPAL_ERROR_LOG(OPAL_ERR_NOT_SUPPORTED);
 279         return OPAL_ERR_NOT_SUPPORTED;
 280     }
 281     return OPAL_SUCCESS;
 282 }
 283 
 284 int opal_value_unload(opal_value_t *kv,
 285                       void **data, opal_data_type_t type)
 286 {
 287     opal_byte_object_t *boptr;
 288 
 289     if (type != kv->type) {
 290         return OPAL_ERR_TYPE_MISMATCH;
 291     }
 292     if (NULL == data ||
 293         (OPAL_STRING != type && OPAL_BYTE_OBJECT != type && NULL == *data)) {
 294         OPAL_ERROR_LOG(OPAL_ERR_BAD_PARAM);
 295         return OPAL_ERR_BAD_PARAM;
 296     }
 297 
 298     switch (type) {
 299     case OPAL_BOOL:
 300         memcpy(*data, &kv->data.flag, sizeof(bool));
 301         break;
 302     case OPAL_BYTE:
 303         memcpy(*data, &kv->data.byte, sizeof(uint8_t));
 304         break;
 305     case OPAL_STRING:
 306         if (NULL != kv->data.string) {
 307             *data = strdup(kv->data.string);
 308         } else {
 309             *data = NULL;
 310         }
 311         break;
 312     case OPAL_SIZE:
 313         memcpy(*data, &kv->data.size, sizeof(size_t));
 314         break;
 315     case OPAL_PID:
 316         memcpy(*data, &kv->data.pid, sizeof(pid_t));
 317         break;
 318 
 319     case OPAL_INT:
 320         memcpy(*data, &kv->data.integer, sizeof(int));
 321         break;
 322     case OPAL_INT8:
 323         memcpy(*data, &kv->data.int8, sizeof(int8_t));
 324         break;
 325     case OPAL_INT16:
 326         memcpy(*data, &kv->data.int16, sizeof(int16_t));
 327         break;
 328     case OPAL_INT32:
 329         memcpy(*data, &kv->data.int32, sizeof(int32_t));
 330         break;
 331     case OPAL_INT64:
 332         memcpy(*data, &kv->data.int64, sizeof(int64_t));
 333         break;
 334 
 335     case OPAL_UINT:
 336         memcpy(*data, &kv->data.uint, sizeof(unsigned int));
 337         break;
 338     case OPAL_UINT8:
 339         memcpy(*data, &kv->data.uint8, 1);
 340         break;
 341     case OPAL_UINT16:
 342         memcpy(*data, &kv->data.uint16, 2);
 343         break;
 344     case OPAL_UINT32:
 345         memcpy(*data, &kv->data.uint32, 4);
 346         break;
 347     case OPAL_UINT64:
 348         memcpy(*data, &kv->data.uint64, 8);
 349         break;
 350 
 351     case OPAL_BYTE_OBJECT:
 352         boptr = (opal_byte_object_t*)malloc(sizeof(opal_byte_object_t));
 353         if (NULL != kv->data.bo.bytes && 0 < kv->data.bo.size) {
 354             boptr->bytes = (uint8_t *) malloc(kv->data.bo.size);
 355             memcpy(boptr->bytes, kv->data.bo.bytes, kv->data.bo.size);
 356             boptr->size = kv->data.bo.size;
 357         } else {
 358             boptr->bytes = NULL;
 359             boptr->size = 0;
 360         }
 361         *data = boptr;
 362         break;
 363 
 364     case OPAL_FLOAT:
 365         memcpy(*data, &kv->data.fval, sizeof(float));
 366         break;
 367 
 368     case OPAL_TIMEVAL:
 369         memcpy(*data, &kv->data.tv, sizeof(struct timeval));
 370         break;
 371 
 372     case OPAL_PTR:
 373         *data = kv->data.ptr;
 374         break;
 375 
 376     case OPAL_VPID:
 377         memcpy(*data, &kv->data.name.vpid, sizeof(opal_vpid_t));
 378         break;
 379 
 380     default:
 381         OPAL_ERROR_LOG(OPAL_ERR_NOT_SUPPORTED);
 382         return OPAL_ERR_NOT_SUPPORTED;
 383     }
 384     return OPAL_SUCCESS;
 385 }
 386 
 387 int opal_value_xfer(opal_value_t *dest,
 388                     opal_value_t *src)
 389 {
 390     opal_byte_object_t *boptr;
 391 
 392     if (NULL != src->key) {
 393         dest->key = strdup(src->key);
 394     }
 395     dest->type = src->type;
 396 
 397     switch (src->type) {
 398     case OPAL_BOOL:
 399         dest->data.flag = src->data.flag;
 400         break;
 401     case OPAL_BYTE:
 402         dest->data.byte = src->data.byte;
 403         break;
 404     case OPAL_STRING:
 405         if (NULL != dest->data.string) {
 406             free(dest->data.string);
 407         }
 408         if (NULL != src->data.string) {
 409             dest->data.string = strdup(src->data.string);
 410         } else {
 411             dest->data.string = NULL;
 412         }
 413         break;
 414     case OPAL_SIZE:
 415         dest->data.size = src->data.size;
 416         break;
 417     case OPAL_PID:
 418         dest->data.pid = src->data.pid;
 419         break;
 420 
 421     case OPAL_INT:
 422         dest->data.integer = src->data.integer;
 423         break;
 424     case OPAL_INT8:
 425         dest->data.int8 = src->data.int8;
 426         break;
 427     case OPAL_INT16:
 428         dest->data.int16 = src->data.int16;
 429         break;
 430     case OPAL_INT32:
 431         dest->data.int32 = src->data.int32;
 432         break;
 433     case OPAL_INT64:
 434         dest->data.int64 = src->data.int64;
 435         break;
 436 
 437     case OPAL_UINT:
 438         dest->data.uint = src->data.uint;
 439         break;
 440     case OPAL_UINT8:
 441         dest->data.uint8 = src->data.uint8;
 442         break;
 443     case OPAL_UINT16:
 444         dest->data.uint16 = src->data.uint16;
 445         break;
 446     case OPAL_UINT32:
 447         dest->data.uint32 = src->data.uint32;
 448         break;
 449     case OPAL_UINT64:
 450         dest->data.uint64 = src->data.uint64;
 451         break;
 452 
 453     case OPAL_BYTE_OBJECT:
 454         if (NULL != dest->data.bo.bytes) {
 455             free(dest->data.bo.bytes);
 456         }
 457         boptr = &src->data.bo;
 458         if (NULL != boptr && NULL != boptr->bytes && 0 < boptr->size) {
 459             dest->data.bo.bytes = (uint8_t *) malloc(boptr->size);
 460             memcpy(dest->data.bo.bytes, boptr->bytes, boptr->size);
 461             dest->data.bo.size = boptr->size;
 462         } else {
 463             dest->data.bo.bytes = NULL;
 464             dest->data.bo.size = 0;
 465         }
 466         break;
 467 
 468     case OPAL_FLOAT:
 469         dest->data.fval = src->data.fval;
 470         break;
 471 
 472     case OPAL_TIMEVAL:
 473         dest->data.tv.tv_sec = src->data.tv.tv_sec;
 474         dest->data.tv.tv_usec = src->data.tv.tv_usec;
 475         break;
 476 
 477     case OPAL_PTR:
 478         dest->data.ptr = src->data.ptr;
 479         break;
 480 
 481     default:
 482         OPAL_ERROR_LOG(OPAL_ERR_NOT_SUPPORTED);
 483         return OPAL_ERR_NOT_SUPPORTED;
 484     }
 485     return OPAL_SUCCESS;
 486 }

/* [<][>][^][v][top][bottom][index][help] */