File: gnix_vector.h

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (427 lines) | stat: -rw-r--r-- 13,440 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * Copyright (c) 2015-2016 Los Alamos National Security, LLC.
 *                         All rights reserved.
 * Copyright (c) 2016 Cray Inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef GNIX_VECTOR_H_
#define GNIX_VECTOR_H_

#include "gnix.h"
#include "gnix_util.h"

#include "stdlib.h"
#include "string.h"

/*******************************************************************************
 * DATASTRUCTS
 ******************************************************************************/
typedef enum gnix_vec_state {
	GNIX_VEC_STATE_READY = 0xdeadbeef,
	GNIX_VEC_STATE_DEAD,
} gnix_vec_state_e;

typedef enum gnix_vec_increase {
	GNIX_VEC_INCREASE_ADD,
	GNIX_VEC_INCREASE_MULT,
} gnix_vec_increase_e;

typedef enum gnix_vec_lock {
	GNIX_VEC_UNLOCKED,
	GNIX_VEC_LOCKED,
} gnix_vec_lock_e;

typedef uint32_t gnix_vec_index_t;
typedef void * gnix_vec_entry_t;

/**
 * Set of attributes that MUST be initialized and passed to _gnix_vec_init.
 *
 * @var vec_initial_size	Initial size of the vector
 * @var vec_maximum_size	Maximum size of the vector
 * @var vec_increase_step	Type of step to increase vector by, ADD or MULT
 * @var vec_internal_locking	GNIX_VEC_UNLOCKED for unlocked, otherwise locked
 * @var creator			fn required to properly alloc the vector element
 */
typedef struct gnix_vec_attr {
	uint32_t vec_initial_size;
	uint32_t cur_size;
	uint32_t vec_maximum_size;

	uint32_t vec_increase_step;

	gnix_vec_increase_e vec_increase_type;

	gnix_vec_lock_e vec_internal_locking;
} gnix_vec_attr_t;

struct gnix_vector;

struct gnix_vector_iter {
	struct gnix_vector *vec;
	uint32_t cur_idx;
};

#define GNIX_VECTOR_ITERATOR(_vec, _iter)	\
	struct gnix_vector_iter _iter = {	\
		.vec = (_vec),			\
		.cur_idx = 0,			\
	}

/* Returns the current index of the iterator */
#define GNIX_VECTOR_ITERATOR_IDX(_iter)	((_iter).cur_idx - 1)

/**
 * Vector operations
 *
 * @var insert_last	Insert an entry into the last index of the vector.
 * @var insert_at	Insert an entry into the vector at the given index.
 *
 * @var remove_last	Removes the last element from the vector.
 * @var remove_at	Removes the element at index from the vector.
 *
 * @var last		Return the last element of the vector.
 * @var at		Return the element at the specified index.
 *
 * @var iter_next	Return the element at the current index and move them
 *			index to the next element.
 */
typedef struct gnix_vector_ops {
	int (*resize)(struct gnix_vector *, uint32_t);

	int (*insert_last)(struct gnix_vector *, gnix_vec_entry_t *);
	int (*insert_at)(struct gnix_vector *, gnix_vec_entry_t *,
			 gnix_vec_index_t);

	int (*remove_last)(struct gnix_vector *);
	int (*remove_at)(struct gnix_vector *, gnix_vec_index_t);

	int (*last)(struct gnix_vector *, void **);
	int (*at)(struct gnix_vector *, void **, gnix_vec_index_t);

	gnix_vec_entry_t *(*iter_next)(struct gnix_vector_iter *);
} gnix_vector_ops_t;

/**
 * Vector handle
 *
 * @var state	The current state of the vector instance.
 * @var attr	The attributes of this vector.
 * @var ops	The supported operations on this vector.
 * @var vector	The begging address of the vector.
 * @var size	The current size of the vector.
 * @var lock	A read/write lock for the vector.
 */
typedef struct gnix_vector {
	gnix_vec_state_e state;
	gnix_vec_attr_t attr;
	gnix_vector_ops_t *ops;
	gnix_vec_entry_t *vector;
	rwlock_t lock;
} gnix_vector_t;


/*******************************************************************************
 * API Prototypes
 ******************************************************************************/
/**
 * Create the initial vector.  The user is responsible for initializing the
 * "attr" parameter prior to calling this function.
 *
 * @param[in] vec	the vector to initialize
 * @param[in] attr	the vector attributes
 *
 * @return FI_SUCCESS	Upon successfully creating the vector
 * @return -FI_EINVAL	Upon receiving an invalid parameter
 * @return -FI_ENOMEM	Upon insufficient memory to create the vector
 */
int _gnix_vec_init(struct gnix_vector *vec, gnix_vec_attr_t *attr);

/**
 * Close the vector elements and then the vector.
 *
 * @param[in] vec       the vector to close
 *
 * @return FI_SUCCESS	Upon successfully closing the vector
 * @return -FI_EINVAL	Upon a uninitialized or dead vector
 */
int _gnix_vec_close(gnix_vector_t *vec);

/*******************************************************************************
 * INLINE OPS FNS
 ******************************************************************************/
/**
 * Resize the vector to size.
 *
 * @param[in] vec	the vector to resize
 * @param[in] size	the new size of the vector
 *
 * @return FI_SUCCESS	Upon successfully resizing the vector
 * @return -FI_EINVAL	Upon passing a uninitialized or dead vector, a size
 * 			less than the minimum vector size, or a size greater
 *			than the maximum vector size
 * @return -FI_ENOMEM	Upon running out of memory
 */
static inline int _gnix_vec_resize(gnix_vector_t *vec, uint32_t size)
{
	GNIX_TRACE(FI_LOG_EP_CTRL, "\n");

	if (OFI_UNLIKELY(!vec)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_resize.\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->resize(vec, size);
	}
}

/**
 * Get the element at index in the vector.
 *
 * @param[in]	  vec 	  The vector to return an element from
 * @param[in/out] element The element at the specified index in the vector
 * @param[in]	  index   The index of the desired element
 *
 * @return FI_SUCCESS	Upon successfully returning the element
 * @return -FI_EINVAL	Upon passing a NULL or dead vector
 * @return -FI_ECANCLED Upon attempting to get an empty element
 */
static inline int _gnix_vec_at(gnix_vector_t *vec, void **element,
			       gnix_vec_index_t index)
{
	GNIX_TRACE(FI_LOG_EP_CTRL, "\n");

	if (OFI_UNLIKELY(!vec || !element)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_at\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->at(vec, element, index);
	}
}

/**
 * Get the first element in the vector.
 *
 * @param[in]	  vec The vector to return an element from
 * @param[in/out] element the first element in the vector
 *
 * @return FI_SUCCESS	Upon successfully returning the element
 * @return -FI_EINVAL	Upon passing a NULL or dead vector
 * @return -FI_ECANCLED Upon attempting to get an empty element
 */
static inline int _gnix_vec_last(gnix_vector_t *vec, void **element)
{
	GNIX_TRACE(FI_LOG_EP_CTRL, "\n");

	if (OFI_UNLIKELY(!vec || !element)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_last\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->last(vec, element);
	}
}

/**
 * Get the first element in the vector.
 *
 * @param[in]	  vec The vector to return an element from
 * @param[in/out] element the first element in the vector
 *
 * @return FI_SUCCESS	Upon successfully returning the element
 * @return -FI_EINVAL	Upon passing a NULL or dead vector
 * @return -FI_ECANCLED Upon attempting to get an empty element
 */
static inline int _gnix_vec_first(gnix_vector_t *vec, void **element)
{
	return _gnix_vec_at(vec, element, 0);
}

/**
 * Removes the element at index from the vector.  Note that
 * the user is responsible for properly disconnecting and/or destroying
 * this vector element.
 *
 * @param[in] vec	the vector to remove an entry from
 * @param[in] index	the index of the entry to remove
 *
 * @return FI_SUCCESS	 Upon successfully removing the entry
 * @return -FI_EINVAL	 Upon passing a dead vector
 * @return -FI_ECANCELED Upon attempting to remove an empty entry
 */
static inline int _gnix_vec_remove_at(gnix_vector_t *vec,
				      gnix_vec_index_t index)
{
	GNIX_TRACE(FI_LOG_EP_CTRL, "\n");

	if (OFI_UNLIKELY(!vec)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_remove_at\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->remove_at(vec, index);
	}
}

/**
 * Removes the last element from the vector.  Note that
 * the user is responsible for properly disconnecting and/or destroying
 * this vector element.
 *
 * @param[in] vec	the vector to remove an entry from
 *
 * @return FI_SUCCESS	 Upon successfully removing and destroying the entry
 * @return -FI_EINVAL	 Upon passing a dead entry
 * @return -FI_ECANCELED Upon attempting to remove an empty entry
 */
static inline int _gnix_vec_remove_last(gnix_vector_t *vec)
{
	GNIX_TRACE(FI_LOG_EP_CTRL, "\n");

	if (OFI_UNLIKELY(!vec)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_remove_at\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->remove_last(vec);
	}
}

/**
 * Removes the first element from the vector.  Note that
 * the user is responsible for properly disconnecting and/or destroying
 * this vector element.
 *
 * @param[in] vec	the vector to remove an entry from
 *
 * @return FI_SUCCESS	 Upon successfully removing and destroying the entry
 * @return -FI_EINVAL	 Upon passing a dead entry
 * @return -FI_ECANCELED Upon attempting to remove an empty entry
 */
static inline int _gnix_vec_remove_first(gnix_vector_t *vec)
{
	return _gnix_vec_remove_at(vec, 0);
}

/**
 * Inserts an entry into the vector at the given index. If the current size
 * of the vector is not large enough to satisfy the insertion then the vector
 * will be grown up to the maximum size. If the entry at index is not empty
 * the insertion will be canceled.
 *
 * @param[in] vec	the vector to insert entry into
 * @param[in] entry	the item to insert into the vector
 * @param[in] index	the index to insert the item at
 *
 * @return FI_SUCCESS	 Upon successfully inserting the entry into the vector
 * @return -FI_ENOMEM	 Upon exceeding the available memory
 * @return -FI_EINVAL	 Upon passing a dead or null vector, or an index passed
 *			 the maximum size.
 * @return -FI_ECANCELED Upon an existing non-empty entry being found at index
 * 			 or reaching the maximum vector size.
 */
static inline int _gnix_vec_insert_at(gnix_vector_t *vec,
				      gnix_vec_entry_t *entry,
				      gnix_vec_index_t index)
{
	if (OFI_UNLIKELY(!vec || !entry)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_insert_at\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->insert_at(vec, entry, index);
	}
}

/**
 * Inserts an entry into the last index of the vector. If the entry at the
 * last index is not empty the insertion will be canceled.
 *
 * @param[in] vec	the vector to insert entry into
 * @param[in] entry	the item to insert into the vector
 *
 * @return FI_SUCCESS	 Upon successfully inserting the entry into the vector
 * @return -FI_EINVAL	 Upon passing a dead vector, or a null
 * 			 entry
 * @return -FI_ECANCELED Upon an existing non-empty entry being found at the
 *			 last index
 */
static inline int _gnix_vec_insert_last(gnix_vector_t *vec,
					gnix_vec_entry_t *entry)
{
	if (OFI_UNLIKELY(!vec || !entry)) {
		GNIX_WARN(FI_LOG_EP_CTRL, "Invalid parameter to "
			  "_gnix_vec_insert_last\n");
		return -FI_EINVAL;
	} else {
		return vec->ops->insert_last(vec, entry);
	}
}

/**
 * Inserts an entry into the first index of the vector. If the entry at the
 * first index is not empty the insertion will be canceled.
 *
 * @param[in] vec	the vector to insert entry into
 * @param[in] entry	the item to insert into the vector
 *
 * @return FI_SUCCESS	 Upon successfully inserting the entry into the vector
 * @return -FI_EINVAL	 Upon passing a dead vector, or a null
 * 			 entry
 * @return -FI_ECANCELED Upon an existing non-empty entry being found at index 0
 */
static inline int _gnix_vec_insert_first(gnix_vector_t *vec,
					 gnix_vec_entry_t *entry)
{
	return _gnix_vec_insert_at(vec, entry, 0);
}

/**
 * Return the current element in the vector iterator and move
 * the iterator to the next element.
 *
 * @param iter    pointer to the vector iterator
 * @return        pointer to current element in the vector
 */
static inline
gnix_vec_entry_t *_gnix_vec_iterator_next(struct gnix_vector_iter *iter)
{
	if (iter == NULL) {
		GNIX_WARN(FI_LOG_EP_DATA, "Invalid parameter to"
			  "_gnix_vec_iterator_next\n");
		return NULL;
	} else {
		return iter->vec->ops->iter_next(iter);
	}
}

#endif /* GNIX_VECTOR_H_ */