root/opal/mca/pmix/pmix4x/pmix/src/mca/psquash/native/psquash_native.c

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

DEFINITIONS

This source file includes following definitions.
  1. native_init
  2. native_finalize
  3. native_get_max_size
  4. native_encode_int
  5. native_decode_int

   1 /*
   2  * Copyright (c) 2019      IBM Corporation.  All rights reserved.
   3  * Copyright (c) 2019      Mellanox Technologies, Inc.
   4  *                         All rights reserved.
   5  *
   6  * $COPYRIGHT$
   7  *
   8  * Additional copyrights may follow
   9  *
  10  * $HEADER$
  11  */
  12 
  13 #include <src/include/pmix_config.h>
  14 
  15 #include <pmix_common.h>
  16 
  17 #include "src/include/pmix_socket_errno.h"
  18 #include "src/include/pmix_globals.h"
  19 #include "src/util/argv.h"
  20 #include "src/util/error.h"
  21 #include "src/util/output.h"
  22 
  23 #include <unistd.h>
  24 #ifdef HAVE_SYS_TYPES_H
  25 #include <sys/types.h>
  26 #endif
  27 
  28 #include "src/mca/psquash/base/base.h"
  29 #include "psquash_native.h"
  30 
  31 static pmix_status_t native_init(void);
  32 
  33 static void native_finalize(void);
  34 
  35 static pmix_status_t native_get_max_size(pmix_data_type_t type, size_t *size);
  36 
  37 static pmix_status_t native_encode_int(pmix_data_type_t type, void *src,
  38                                        void *dst, size_t *size);
  39 
  40 static pmix_status_t native_decode_int(pmix_data_type_t type, void *src,
  41                                        size_t src_len, void *dest,
  42                                        size_t *dst_size);
  43 
  44 pmix_psquash_base_module_t pmix_psquash_native_module = {
  45     .name = "native",
  46     .int_type_is_encoded = false,
  47     .init = native_init,
  48     .finalize = native_finalize,
  49     .get_max_size = native_get_max_size,
  50     .encode_int = native_encode_int,
  51     .decode_int = native_decode_int
  52 };
  53 
  54 #define NATIVE_PACK_CONVERT(ret, type, val)         \
  55 do {                                                \
  56     (ret) = PMIX_SUCCESS;                           \
  57     switch(type) {                                  \
  58         case PMIX_INT16:                            \
  59         case PMIX_UINT16:{                          \
  60             uint16_t __tmp = (uint16_t)val;         \
  61             val = pmix_htons(__tmp);                \
  62             break;                                  \
  63         }                                           \
  64         case PMIX_INT:                              \
  65         case PMIX_UINT:                             \
  66         case PMIX_INT32:                            \
  67         case PMIX_UINT32:{                          \
  68             uint32_t __tmp = (uint32_t)val;         \
  69             val = htonl(__tmp);                     \
  70             break;                                  \
  71         }                                           \
  72         case PMIX_SIZE:                             \
  73         case PMIX_INT64:                            \
  74         case PMIX_UINT64:{                          \
  75             uint64_t __tmp = (uint64_t)val;         \
  76             val = pmix_hton64(__tmp);               \
  77             break;                                  \
  78         }                                           \
  79         default:                                    \
  80             (ret) = PMIX_ERR_BAD_PARAM;             \
  81     }                                               \
  82 } while (0)
  83 
  84 #define NATIVE_UNPACK_CONVERT(ret, type, val)       \
  85 do {                                                \
  86     (ret) = PMIX_SUCCESS;                           \
  87     switch(type) {                                  \
  88         case PMIX_INT16:                            \
  89         case PMIX_UINT16:{                          \
  90             uint16_t __tmp = (uint16_t)val;         \
  91             val = pmix_ntohs(__tmp);                \
  92             break;                                  \
  93         }                                           \
  94         case PMIX_INT:                              \
  95         case PMIX_UINT:                             \
  96         case PMIX_INT32:                            \
  97         case PMIX_UINT32:{                          \
  98             uint32_t __tmp = (uint32_t)val;         \
  99             val = ntohl(__tmp);                     \
 100             break;                                  \
 101         }                                           \
 102         case PMIX_INT64:                            \
 103         case PMIX_SIZE:                             \
 104         case PMIX_UINT64:{                          \
 105             uint64_t __tmp = (uint64_t)val;         \
 106             val = pmix_ntoh64(__tmp);               \
 107             break;                                  \
 108         }                                           \
 109         default:                                    \
 110             (ret) = PMIX_ERR_BAD_PARAM;             \
 111     }                                               \
 112 } while (0)
 113 
 114 static pmix_status_t native_init(void)
 115 {
 116     pmix_output_verbose(2, pmix_globals.debug_output,
 117                         "psquash: native init");
 118     return PMIX_SUCCESS;
 119 }
 120 
 121 static void native_finalize(void)
 122 {
 123     pmix_output_verbose(2, pmix_globals.debug_output,
 124                         "psquash: native finalize");
 125 }
 126 
 127 static pmix_status_t native_get_max_size(pmix_data_type_t type, size_t *size)
 128 
 129 {
 130     pmix_status_t rc;
 131     PMIX_SQUASH_TYPE_SIZEOF(rc, type, *size);
 132     return rc;
 133 }
 134 
 135 static pmix_status_t native_encode_int(pmix_data_type_t type, void *src,
 136                                        void *dst, size_t *size)
 137 {
 138     pmix_status_t rc;
 139     uint64_t tmp = 0;
 140     size_t val_size;
 141 
 142     PMIX_SQUASH_TYPE_SIZEOF(rc, type, val_size);
 143     if (PMIX_SUCCESS != rc) {
 144         PMIX_ERROR_LOG(rc);
 145         return rc;
 146     }
 147     memcpy(&tmp, src, val_size);
 148     NATIVE_PACK_CONVERT(rc, type, tmp);
 149     if (PMIX_SUCCESS != rc) {
 150         PMIX_ERROR_LOG(rc);
 151         return rc;
 152     }
 153     memcpy(dst, &tmp, val_size);
 154     *size = val_size;
 155 
 156     return PMIX_SUCCESS;
 157 }
 158 
 159 static pmix_status_t native_decode_int(pmix_data_type_t type, void *src,
 160                                        size_t src_len, void *dst,
 161                                        size_t *dst_size)
 162 {
 163     pmix_status_t rc;
 164     uint64_t tmp = 0;
 165     size_t val_size;
 166 
 167     PMIX_SQUASH_TYPE_SIZEOF(rc, type, val_size);
 168     if (PMIX_SUCCESS != rc) {
 169         PMIX_ERROR_LOG(rc);
 170         return rc;
 171     }
 172     /* sanity check */
 173     if (src_len != val_size) {
 174         rc = PMIX_ERR_UNPACK_FAILURE;
 175     }
 176 
 177     memcpy(&tmp, src, val_size);
 178     NATIVE_UNPACK_CONVERT(rc, type, tmp);
 179     if (PMIX_SUCCESS != rc) {
 180         PMIX_ERROR_LOG(rc);
 181         return rc;
 182     }
 183     memcpy(dst, &tmp, val_size);
 184     *dst_size = val_size;
 185 
 186     return PMIX_SUCCESS;
 187 }

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