![]() |
|
|||
File indexing completed on 2025-02-22 10:47:20
0001 /* 0002 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana 0003 * University Research and Technology 0004 * Corporation. All rights reserved. 0005 * Copyright (c) 2004-2006 The University of Tennessee and The University 0006 * of Tennessee Research Foundation. All rights 0007 * reserved. 0008 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 0009 * University of Stuttgart. All rights reserved. 0010 * Copyright (c) 2004-2005 The Regents of the University of California. 0011 * All rights reserved. 0012 * Copyright (c) 2016-2020 Intel, Inc. All rights reserved. 0013 * Copyright (c) 2021-2022 Nanook Consulting. All rights reserved. 0014 * $COPYRIGHT$ 0015 * 0016 * Additional copyrights may follow 0017 * 0018 * $HEADER$ 0019 */ 0020 0021 #ifndef PMIX_VALUE_ARRAY_H 0022 #define PMIX_VALUE_ARRAY_H 0023 0024 #include "src/include/pmix_config.h" 0025 0026 #include <string.h> 0027 #ifdef HAVE_STRINGS_H 0028 # include <strings.h> 0029 #endif /* HAVE_STRINGS_H */ 0030 0031 #include "src/class/pmix_object.h" 0032 #if PMIX_ENABLE_DEBUG 0033 # include "src/util/pmix_output.h" 0034 #endif 0035 #include "pmix_common.h" 0036 0037 BEGIN_C_DECLS 0038 0039 /* 0040 * @file Array of elements maintained by value. 0041 */ 0042 0043 struct pmix_value_array_t { 0044 pmix_object_t super; 0045 unsigned char *array_items; 0046 size_t array_item_sizeof; 0047 size_t array_size; 0048 size_t array_alloc_size; 0049 }; 0050 typedef struct pmix_value_array_t pmix_value_array_t; 0051 0052 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_value_array_t); 0053 0054 /** 0055 * Initialize the array to hold items by value. This routine must 0056 * be called prior to using the array. 0057 * 0058 * @param array The array to initialize (IN). 0059 * @param item_size The sizeof each array element (IN). 0060 * @return PMIX error code 0061 * 0062 * Note that there is no corresponding "finalize" function -- use 0063 * OBJ_DESTRUCT (for stack arrays) or OBJ_RELEASE (for heap arrays) to 0064 * delete it. 0065 */ 0066 0067 static inline int pmix_value_array_init(pmix_value_array_t *array, size_t item_sizeof) 0068 { 0069 array->array_item_sizeof = item_sizeof; 0070 array->array_alloc_size = 1; 0071 array->array_size = 0; 0072 array->array_items = (unsigned char *) realloc(array->array_items, 0073 item_sizeof * array->array_alloc_size); 0074 return (NULL != array->array_items) ? PMIX_SUCCESS : PMIX_ERR_OUT_OF_RESOURCE; 0075 } 0076 0077 /** 0078 * Reserve space in the array for new elements, but do not change the size. 0079 * 0080 * @param array The input array (IN). 0081 * @param size The anticipated size of the array (IN). 0082 * @return PMIX error code. 0083 */ 0084 0085 static inline int pmix_value_array_reserve(pmix_value_array_t *array, size_t size) 0086 { 0087 if (size > array->array_alloc_size) { 0088 array->array_items = (unsigned char *) realloc(array->array_items, 0089 array->array_item_sizeof * size); 0090 if (NULL == array->array_items) { 0091 array->array_size = 0; 0092 array->array_alloc_size = 0; 0093 return PMIX_ERR_OUT_OF_RESOURCE; 0094 } 0095 array->array_alloc_size = size; 0096 } 0097 return PMIX_SUCCESS; 0098 } 0099 0100 /** 0101 * Retrieves the number of elements in the array. 0102 * 0103 * @param array The input array (IN). 0104 * @return The number of elements currently in use. 0105 */ 0106 0107 static inline size_t pmix_value_array_get_size(pmix_value_array_t *array) 0108 { 0109 return array->array_size; 0110 } 0111 0112 /** 0113 * Set the number of elements in the array. 0114 * 0115 * @param array The input array (IN). 0116 * @param size The new array size. 0117 * 0118 * @return PMIX error code. 0119 * 0120 * Note that resizing the array to a smaller size may not change 0121 * the underlying memory allocated by the array. However, setting 0122 * the size larger than the current allocation will grow it. In either 0123 * case, if the routine is successful, pmix_value_array_get_size() will 0124 * return the new size. 0125 */ 0126 0127 PMIX_EXPORT int pmix_value_array_set_size(pmix_value_array_t *array, size_t size); 0128 0129 /** 0130 * Macro to retrieve an item from the array by value. 0131 * 0132 * @param array The input array (IN). 0133 * @param item_type The C datatype of the array item (IN). 0134 * @param item_index The array index (IN). 0135 * 0136 * @returns item The requested item. 0137 * 0138 * Note that this does not change the size of the array - this macro is 0139 * strictly for performance - the user assumes the responsibility of 0140 * ensuring the array index is valid (0 <= item index < array size). 0141 */ 0142 0143 #define PMIX_VALUE_ARRAY_GET_ITEM(array, item_type, item_index) \ 0144 ((item_type *) ((array)->array_items))[item_index] 0145 0146 /** 0147 * Retrieve an item from the array by reference. 0148 * 0149 * @param array The input array (IN). 0150 * @param item_index The array index (IN). 0151 * 0152 * @return ptr Pointer to the requested item. 0153 * 0154 * Note that if the specified item_index is larger than the current 0155 * array size, the array is grown to satisfy the request. 0156 */ 0157 0158 static inline void *pmix_value_array_get_item(pmix_value_array_t *array, size_t item_index) 0159 { 0160 if (item_index >= array->array_size 0161 && pmix_value_array_set_size(array, item_index + 1) != PMIX_SUCCESS) 0162 return NULL; 0163 return array->array_items + (item_index * array->array_item_sizeof); 0164 } 0165 0166 /** 0167 * Macro to set an array element by value. 0168 * 0169 * @param array The input array (IN). 0170 * @param item_type The C datatype of the array item (IN). 0171 * @param item_index The array index (IN). 0172 * @param item_value The new value for the specified index (IN). 0173 * 0174 * Note that this does not change the size of the array - this macro is 0175 * strictly for performance - the user assumes the responsibility of 0176 * ensuring the array index is valid (0 <= item index < array size). 0177 * 0178 * It is safe to free the item after returning from this call; it is 0179 * copied into the array by value. 0180 */ 0181 0182 #define PMIX_VALUE_ARRAY_SET_ITEM(array, item_type, item_index, item_value) \ 0183 (((item_type *) ((array)->array_items))[item_index] = item_value) 0184 0185 /** 0186 * Set an array element by value. 0187 * 0188 * @param array The input array (IN). 0189 * @param item_index The array index (IN). 0190 * @param item_value A pointer to the item, which is copied into 0191 * the array. 0192 * 0193 * @return PMIX error code. 0194 * 0195 * It is safe to free the item after returning from this call; it is 0196 * copied into the array by value. 0197 */ 0198 0199 static inline int pmix_value_array_set_item(pmix_value_array_t *array, size_t item_index, 0200 const void *item) 0201 { 0202 int rc; 0203 if (item_index >= array->array_size 0204 && (rc = pmix_value_array_set_size(array, item_index + 1)) != PMIX_SUCCESS) 0205 return rc; 0206 memcpy(array->array_items + (item_index * array->array_item_sizeof), item, 0207 array->array_item_sizeof); 0208 return PMIX_SUCCESS; 0209 } 0210 0211 /** 0212 * Appends an item to the end of the array. 0213 * 0214 * @param array The input array (IN). 0215 * @param item A pointer to the item to append, which is copied 0216 * into the array. 0217 * 0218 * @return PMIX error code 0219 * 0220 * This will grow the array if it is not large enough to contain the 0221 * item. It is safe to free the item after returning from this call; 0222 * it is copied by value into the array. 0223 */ 0224 0225 static inline int pmix_value_array_append_item(pmix_value_array_t *array, const void *item) 0226 { 0227 return pmix_value_array_set_item(array, array->array_size, item); 0228 } 0229 0230 /** 0231 * Remove a specific item from the array. 0232 * 0233 * @param array The input array (IN). 0234 * @param item_index The index to remove, which must be less than 0235 * the current array size (IN). 0236 * 0237 * @return PMIX error code. 0238 * 0239 * All elements following this index are shifted down. 0240 */ 0241 0242 static inline int pmix_value_array_remove_item(pmix_value_array_t *array, size_t item_index) 0243 { 0244 #if PMIX_ENABLE_DEBUG 0245 if (item_index >= array->array_size) { 0246 pmix_output(0, "pmix_value_array_remove_item: invalid index %lu\n", 0247 (unsigned long) item_index); 0248 return PMIX_ERR_BAD_PARAM; 0249 } 0250 #endif 0251 memmove(array->array_items + (array->array_item_sizeof * item_index), 0252 array->array_items + (array->array_item_sizeof * (item_index + 1)), 0253 array->array_item_sizeof * (array->array_size - item_index - 1)); 0254 array->array_size--; 0255 return PMIX_SUCCESS; 0256 } 0257 0258 /** 0259 * Get the base pointer of the underlying array. 0260 * 0261 * @param array The input array (IN). 0262 * @param array_type The C datatype of the array (IN). 0263 * 0264 * @returns ptr Pointer to the actual array. 0265 * 0266 * This function is helpful when you need to iterate through an 0267 * entire array; simply get the base value of the array and use native 0268 * C to iterate through it manually. This can have better performance 0269 * than looping over PMIX_VALUE_ARRAY_GET_ITEM() and 0270 * PMIX_VALUE_ARRAY_SET_ITEM() because it will [potentially] reduce the 0271 * number of pointer dereferences. 0272 */ 0273 0274 #define PMIX_VALUE_ARRAY_GET_BASE(array, item_type) ((item_type *) ((array)->array_items)) 0275 0276 END_C_DECLS 0277 0278 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |