aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/idr_ext.h
blob: d082a2115581a3ca08a1b6524ca097c0483fc6b3 (plain)
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
/*
 * include/linux/idr_ext.h		IDR extended
 *
 * Copyright 2017 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#ifndef __IDR_EXT_H__
#define __IDR_EXT_H__

#include <linux/idr.h>

struct idr_ext {
	struct idr	idr_lo;	/* Used for range <0, INT_MAX> */
	struct idr	idr_hi;	/* Used for range <INT_MAX+1, UINT_MAX> */
};

int idr_alloc_ext(struct idr_ext *idrext, void *ptr, unsigned long *index,
		  unsigned long start, unsigned long end, gfp_t gfp);

void *idr_get_next_ext(struct idr_ext *idrext, unsigned long *nextidp);

#define idr_for_each_entry_ext(idr, entry, id)			\
	for (id = 0; ((entry) = idr_get_next_ext(idr, &(id))) != NULL; ++id)

static inline
void idr_init_ext(struct idr_ext *idrext)
{
	idr_init(&idrext->idr_lo);
	idr_init(&idrext->idr_hi);
}

static inline
void idr_destroy_ext(struct idr_ext *idrext)
{
	idr_destroy(&idrext->idr_lo);
	idr_destroy(&idrext->idr_hi);
}

static inline
void idr_remove_ext(struct idr_ext *idrext, unsigned long id)
{
	if (id > UINT_MAX)
		return;

	if (id <= (unsigned long)INT_MAX)
		idr_remove(&idrext->idr_lo, (int)id);
	else
		idr_remove(&idrext->idr_hi, (int)(id - INT_MAX - 1));
}

static inline
void *idr_find_ext(struct idr_ext *idrext, unsigned long id)
{
	if (id > UINT_MAX)
		return NULL;

	if (id <= (unsigned long)INT_MAX)
		return idr_find(&idrext->idr_lo, (int)id);

	return idr_find(&idrext->idr_hi, (int)(id - INT_MAX - 1));
}

static inline
void *idr_replace_ext(struct idr_ext *idrext, void *ptr, unsigned long id)
{
	if (id > UINT_MAX)
		return NULL;

	if (id <= (unsigned long)INT_MAX)
		return idr_replace(&idrext->idr_lo, ptr, (int)id);

	return idr_replace(&idrext->idr_hi, ptr, (int)(id - INT_MAX - 1));
}

#endif /* __IDR_EXT_H__ */