Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/idr.c
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-2.0-only */
2
/*
3
 * 2002-10-18  written by Jim Houston jim.houston@ccur.com
4
 *  Copyright (C) 2002 by Concurrent Computer Corporation
5
 */
6
7
#include <errno.h>
8
#include <linux/idr.h>
9
#include <linux/bug.h>
10
#include <linux/limits.h>
11
#include <linux/minmax.h>
12
#include <xfuncs.h>
13
14
struct idr *__idr_find(struct idr *head, int lookup_id)
15
0
{
16
0
  struct idr *cursor;
17
18
0
  list_for_each_entry(cursor, &head->list, list) {
19
0
    if (cursor->id == lookup_id)
20
0
      return cursor;
21
0
  }
22
23
0
  return NULL;
24
0
}
25
26
/**
27
 * idr_for_each() - Iterate through all stored pointers.
28
 * @idr: IDR handle.
29
 * @fn: Function to be called for each pointer.
30
 * @data: Data passed to callback function.
31
 *
32
 * The callback function will be called for each entry in @idr, passing
33
 * the ID, the entry and @data.
34
 *
35
 * If @fn returns anything other than %0, the iteration stops and that
36
 * value is returned from this function.
37
 */
38
int idr_for_each(const struct idr *idr,
39
     int (*fn)(int id, void *p, void *data), void *data)
40
0
{
41
0
  const struct idr *pos, *tmp;
42
0
  int ret;
43
44
0
  list_for_each_entry_safe(pos, tmp, &idr->list, list) {
45
0
    ret = fn(pos->id, pos->ptr, data);
46
0
    if (ret)
47
0
      return ret;
48
0
  }
49
50
0
  return 0;
51
0
}
52
53
static struct idr *__idr_alloc(void *ptr, u32 start)
54
0
{
55
0
  struct idr *idr;
56
57
0
  idr = xmalloc(sizeof(*idr));
58
0
  idr->id = start;
59
0
  idr->ptr = ptr;
60
61
0
  return idr;
62
0
}
63
64
/**
65
 * idr_alloc_u32() - Allocate an ID.
66
 * @head: IDR handle.
67
 * @ptr: Pointer to be associated with the new ID.
68
 * @nextid: Pointer to an ID.
69
 * @max: The maximum ID to allocate (inclusive).
70
 * @gfp: Memory allocation flags.
71
 *
72
 * Allocates an unused ID in the range specified by @nextid and @max.
73
 * Note that @max is inclusive whereas the @end parameter to idr_alloc()
74
 * is exclusive. The new ID is assigned to @nextid before the pointer
75
 * is inserted into the IDR.
76
 *
77
 * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
78
 * or -ENOSPC if no free IDs could be found.  If an error occurred,
79
 * @nextid is unchanged.
80
 */
81
int idr_alloc_u32(struct idr *head, void *ptr, u32 *nextid,
82
      unsigned long max, gfp_t gfp)
83
0
{
84
0
  struct idr *idr, *prev, *next;
85
86
0
  if (list_empty(&head->list)) {
87
0
    idr = __idr_alloc(ptr, *nextid);
88
0
    list_add_tail(&idr->list, &head->list);
89
0
    return 0;
90
0
  }
91
92
0
  next = list_first_entry(&head->list, struct idr, list);
93
0
  if (*nextid < next->id) {
94
0
    idr = __idr_alloc(ptr, *nextid);
95
0
    list_add(&idr->list, &head->list);
96
0
    return 0;
97
0
  }
98
99
0
  prev = list_last_entry(&head->list, struct idr, list);
100
0
  if (prev->id < max) {
101
0
    *nextid = max_t(u32, prev->id + 1, *nextid);
102
0
    idr = __idr_alloc(ptr, *nextid);
103
0
    list_add_tail(&idr->list, &head->list);
104
0
    return 0;
105
0
  }
106
107
0
  list_for_each_entry_safe(prev, next, &head->list, list) {
108
    /* at the end of the list */
109
0
    if (&next->list == &head->list)
110
0
      break;
111
112
0
    if (prev->id > max)
113
0
      break;
114
0
    if (next->id < *nextid)
115
0
      continue;
116
117
0
    *nextid = max_t(u32, prev->id + 1, *nextid);
118
0
    idr = __idr_alloc(ptr, *nextid);
119
0
    list_add(&idr->list, &prev->list);
120
0
    return 0;
121
0
  }
122
123
0
  return -ENOSPC;
124
0
}
125
126
/**
127
 * idr_alloc() - Allocate an ID.
128
 * @head: IDR handle.
129
 * @ptr: Pointer to be associated with the new ID.
130
 * @start: The minimum ID (inclusive).
131
 * @end: The maximum ID (exclusive).
132
 * @gfp: Memory allocation flags.
133
 *
134
 * Allocates an unused ID in the range specified by @start and @end.  If
135
 * @end is <= 0, it is treated as one larger than %INT_MAX.  This allows
136
 * callers to use @start + N as @end as long as N is within integer range.
137
 *
138
 * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
139
 * or -ENOSPC if no free IDs could be found.
140
 */
141
int idr_alloc(struct idr *head, void *ptr, int start, int end, gfp_t gfp)
142
0
{
143
0
  u32 id = start;
144
0
  int ret;
145
146
0
  if (WARN_ON_ONCE(start < 0))
147
0
    return -EINVAL;
148
149
0
  ret = idr_alloc_u32(head, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
150
0
  if (ret)
151
0
    return ret;
152
153
0
  return id;
154
0
}
155
156
static void __idr_remove(struct idr *idr)
157
0
{
158
0
  list_del(&idr->list);
159
0
  free(idr);
160
0
}
161
162
void idr_remove(struct idr *head, int id)
163
0
{
164
0
  struct idr *idr = __idr_find(head, id);
165
0
  if (!idr)
166
0
    return;
167
168
0
  __idr_remove(idr);
169
0
}
170
171
void idr_destroy(struct idr *idr)
172
0
{
173
0
  struct idr *pos, *tmp;
174
175
0
  if (!idr)
176
0
    return;
177
178
0
  list_for_each_entry_safe(pos, tmp, &idr->list, list)
179
0
    __idr_remove(pos);
180
0
}