File: ofi_indexer.h

package info (click to toggle)
libfabric 2.1.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,108 kB
  • sloc: ansic: 387,262; python: 3,171; sh: 2,555; makefile: 1,313; cpp: 617; perl: 474; ruby: 123; asm: 27
file content (320 lines) | stat: -rw-r--r-- 8,891 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2011 Intel Corporation.  All rights reserved.
 * Copyright (c) 2016 Cisco Systems, 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 _OFI_INDEXER_H_
#define _OFI_INDEXER_H_

#include "config.h"

#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>

#include <ofi_osd.h>


/*
 * Indexer:
 * The indexer is used to associate a pointer with an integer value.
 * This allows passing the integer to other users, including remote
 * peers or processes.  The integer can be used by the owner of the
 * indexer to retrieve the stored pointer.  The integer value is selected
 * by the indexer by selecting the first available unused value.
 *
 * The pointers are stored using a double-lookup array, which grows
 * dynamically.  This helps conserve memory when only a few objects are
 * stored in the indexer.
 *
 * Synchronization must be provided by the caller.  Caller must
 * initialize the indexer by setting free_list and size to 0.
 */

struct ofi_idx_entry {
	void *item;
	int   next;
};

/* User's index is a bit field of format: [chunk_id:offset] */
#define OFI_IDX_CHUNK_BITS 10
#define OFI_IDX_OFFSET_BITS 10

#define OFI_IDX_CHUNK_SIZE (1 << OFI_IDX_OFFSET_BITS)
#define OFI_IDX_MAX_CHUNKS (1 << OFI_IDX_CHUNK_BITS)

#define OFI_IDX_MAX_INDEX  ((OFI_IDX_MAX_CHUNKS * OFI_IDX_CHUNK_SIZE) - 1)

struct indexer
{
	struct ofi_idx_entry 	*chunk[OFI_IDX_MAX_CHUNKS];
	int		 	free_list;
	/* Array size (used): [0, OFI_IDX_MAX_CHUNKS) */
	int		 	size;
};

#define ofi_idx_chunk_id(index) (index >> OFI_IDX_OFFSET_BITS)
#define ofi_idx_offset(index) (index & (OFI_IDX_CHUNK_SIZE - 1))

int ofi_idx_insert(struct indexer *idx, void *item);
void *ofi_idx_remove(struct indexer *idx, int index);
void *ofi_idx_remove_ordered(struct indexer *idx, int index);
void ofi_idx_replace(struct indexer *idx, int index, void *item);
void ofi_idx_reset(struct indexer *idx);

static inline int ofi_idx_is_valid(struct indexer *idx, int index)
{
	return (index > 0) && (index < idx->size * OFI_IDX_CHUNK_SIZE);
}

static inline struct ofi_idx_entry *
ofi_idx_chunk(struct indexer *idx, int index)
{
	assert(ofi_idx_is_valid(idx, index));
	return idx->chunk[ofi_idx_chunk_id(index)];
}

static inline void *ofi_idx_at(struct indexer *idx, int index)
{
	return (ofi_idx_chunk(idx, index) + ofi_idx_offset(index))->item;
}

static inline void *ofi_idx_lookup(struct indexer *idx, int index)
{
	return ofi_idx_is_valid(idx, index) ? ofi_idx_at(idx, index) : NULL;
}

static inline bool ofi_idx_free_list_empty(struct indexer *idx)
{
	return (idx->free_list == 0);
}


/*
 * Byte Indexer Map:
 * Indexer & map with an index that fits into a byte.  Index 0 is invalid.
 *
 * Synchronization must be provided by the caller.  Caller must
 * initialize structure to 0.
 */

enum ofi_byte_idx_type {
	OFI_BYTE_IDX_UNKNOWN,
	OFI_BYTE_IDX_INDEX,
	OFI_BYTE_IDX_MAP,
};

struct ofi_byte_idx
{
	struct ofi_idx_entry *data;
	uint8_t free_list;
	OFI_DBG_VAR(enum ofi_byte_idx_type, type)
};

bool ofi_byte_idx_grow(struct ofi_byte_idx *idx);

static inline uint8_t ofi_byte_idx_insert(struct ofi_byte_idx *idx, void *item)
{
	uint8_t index;

	assert(idx->type == OFI_BYTE_IDX_UNKNOWN ||
	       idx->type == OFI_BYTE_IDX_INDEX);
	OFI_DBG_SET(idx->type, OFI_BYTE_IDX_INDEX);

	index = idx->free_list;
	if (index == 0) {
		if (!ofi_byte_idx_grow(idx))
			return 0;
		index = idx->free_list;
	}

	idx->free_list = (uint8_t) idx->data[index].next;
	idx->data[index].item = item;
	idx->data[index].next = -1;
	return index;
}

static inline void *ofi_byte_idx_remove(struct ofi_byte_idx *idx, uint8_t index)
{
	void *item;

	assert(idx->type == OFI_BYTE_IDX_INDEX);

	if (idx->data[index].next != -1)
		return NULL;

	item = idx->data[index].item;
	idx->data[index].item = NULL;
	idx->data[index].next = idx->free_list;
	idx->free_list = index;
	return item;
}

static inline void *ofi_byte_idx_at(struct ofi_byte_idx *idx, uint8_t index)
{
	return idx->data[index].item;
}

static inline void *ofi_byte_idx_lookup(struct ofi_byte_idx *idx, uint8_t index)
{
	return (idx->data && idx->data[index].next == -1) ?
		idx->data[index].item : NULL;
}

static inline uint8_t
ofi_byte_idx_set(struct ofi_byte_idx *idx, uint8_t index, void *item)
{
	assert(idx->type == OFI_BYTE_IDX_UNKNOWN ||
	       idx->type == OFI_BYTE_IDX_MAP);
	OFI_DBG_SET(idx->type, OFI_BYTE_IDX_MAP);

	if (!idx->data && !ofi_byte_idx_grow(idx))
		return 0;

	assert(idx->data[index].next != -1);
	idx->data[index].item = item;
	idx->data[index].next = -1;
	return index;
}

static inline void *
ofi_byte_idx_clear(struct ofi_byte_idx *idx, uint8_t index)
{
	void *item;

	assert(idx->type == OFI_BYTE_IDX_MAP);
	assert(idx->data);
	assert(idx->data[index].next == -1);

	item = idx->data[index].item;
	idx->data[index].item = NULL;
	idx->data[index].next = 0;
	return item;
}


/*
 * Index map:
 * The index map is similar in concept to the indexer.  It allows the user
 * to associate an integer with a pointer.  The difference between the index
 * map and indexer, is that the user of the index map selects the index.  This
 * results in the index map behaving the same as a standard array.
 *
 * The index map stores pointers using a double-lookup table.  This minimizes
 * the memory footprint relative to using a standard array when the selected
 * integer values are sparse.
 *
 * Synchronization must be provided by the caller.  Caller must initialize
 * the index map by setting it to 0.
 */

struct index_map
{
	void **chunk[OFI_IDX_MAX_CHUNKS];
	int count[OFI_IDX_MAX_CHUNKS];
};

int ofi_idm_set(struct index_map *idm, int index, void *item);
void *ofi_idm_clear(struct index_map *idm, int index);
void ofi_idm_reset(struct index_map *idm, void (*callback)(void *item));

static inline void **ofi_idm_chunk(struct index_map *idm, int index)
{
	assert(idm->chunk);
	return idm->chunk[ofi_idx_chunk_id(index)];
}

static inline void *ofi_idm_at(struct index_map *idm, int index)
{
	void **chunk;
	chunk = ofi_idm_chunk(idm, index);
	assert(chunk && idm->count[ofi_idx_chunk_id(index)]);
	return chunk[ofi_idx_offset(index)];
}

static inline void *ofi_idm_lookup(struct index_map *idm, int index)
{
	return ((index <= OFI_IDX_MAX_INDEX) && ofi_idm_chunk(idm, index)) ?
		ofi_idm_at(idm, index) : NULL;
}


struct ofi_dyn_arr
{
	char *chunk[OFI_IDX_MAX_CHUNKS];
	size_t item_size;
	void (*init)(struct ofi_dyn_arr *arr, void *item);
};

static inline void
ofi_array_init(struct ofi_dyn_arr *arr, size_t item_size,
	       void (*init)(struct ofi_dyn_arr *arr, void *item))
{
	memset(arr, 0, sizeof(*arr));
	arr->item_size = item_size;
	arr->init = init;
}

int ofi_array_grow(struct ofi_dyn_arr *arr, int index);
/* Returning non-zero from callback breaks iteration */
int ofi_array_iter(struct ofi_dyn_arr *arr, void *context,
		   int (*callback)(struct ofi_dyn_arr *arr, void *item,
				   void *context));
void ofi_array_destroy(struct ofi_dyn_arr *arr);

static inline char *ofi_array_chunk(struct ofi_dyn_arr *arr, int index)
{
	assert(arr->chunk);
	return arr->chunk[ofi_idx_chunk_id(index)];
}

static inline void *
ofi_array_item(struct ofi_dyn_arr *arr, char *chunk, int offset)
{
	return chunk + arr->item_size * offset;
}

static inline void *ofi_array_at(struct ofi_dyn_arr *arr, int index)
{
	assert(index <= OFI_IDX_MAX_INDEX);

	if (!ofi_array_chunk(arr, index)) {
		if (ofi_array_grow(arr, index) < 0)
			return NULL;
	}

	return ofi_array_item(arr, ofi_array_chunk(arr, index),
			      ofi_idx_offset(index));
}

#endif /* _OFI_INDEXER_H_ */