Lines Matching refs:axmap
3 * the previous. Hence an 'axmap', since we axe each previous layer into a
24 #include "axmap.h"
46 struct axmap {
61 void axmap_reset(struct axmap *axmap)
65 for (i = 0; i < axmap->nr_levels; i++) {
66 struct axmap_level *al = &axmap->levels[i];
71 axmap->first_free = 0;
74 void axmap_free(struct axmap *axmap)
78 if (!axmap)
81 for (i = 0; i < axmap->nr_levels; i++)
82 free(axmap->levels[i].map);
84 free(axmap->levels);
85 free(axmap);
88 struct axmap *axmap_new(unsigned long nr_bits)
90 struct axmap *axmap;
93 axmap = malloc(sizeof(*axmap));
94 if (!axmap)
104 axmap->nr_levels = levels;
105 axmap->levels = malloc(axmap->nr_levels * sizeof(struct axmap_level));
106 axmap->nr_bits = nr_bits;
108 for (i = 0; i < axmap->nr_levels; i++) {
109 struct axmap_level *al = &axmap->levels[i];
120 axmap_reset(axmap);
121 return axmap;
123 for (i = 0; i < axmap->nr_levels; i++)
124 if (axmap->levels[i].map)
125 free(axmap->levels[i].map);
127 free(axmap->levels);
128 free(axmap);
132 static int axmap_handler(struct axmap *axmap, uint64_t bit_nr,
139 for (i = 0; i < axmap->nr_levels; i++) {
144 al = &axmap->levels[i];
153 static int axmap_handler_topdown(struct axmap *axmap, uint64_t bit_nr,
158 int i, level = axmap->nr_levels;
160 for (i = axmap->nr_levels - 1; i >= 0; i--) {
165 al = &axmap->levels[i];
184 void axmap_clear(struct axmap *axmap, uint64_t bit_nr)
186 axmap_handler(axmap, bit_nr, axmap_clear_fn, NULL);
254 static void __axmap_set(struct axmap *axmap, uint64_t bit_nr,
259 if (axmap->first_free >= bit_nr &&
260 axmap->first_free < bit_nr + data->nr_bits)
261 axmap->first_free = -1ULL;
263 if (bit_nr > axmap->nr_bits)
265 else if (bit_nr + nr_bits > axmap->nr_bits)
266 nr_bits = axmap->nr_bits - bit_nr;
270 axmap_handler(axmap, bit_nr, axmap_set_fn, data);
286 void axmap_set(struct axmap *axmap, uint64_t bit_nr)
290 __axmap_set(axmap, bit_nr, &data);
293 unsigned int axmap_set_nr(struct axmap *axmap, uint64_t bit_nr, unsigned int nr_bits)
306 __axmap_set(axmap, bit_nr, &data);
324 int axmap_isset(struct axmap *axmap, uint64_t bit_nr)
326 if (bit_nr <= axmap->nr_bits)
327 return axmap_handler_topdown(axmap, bit_nr, axmap_isset_fn, NULL);
332 static uint64_t axmap_find_first_free(struct axmap *axmap, unsigned int level,
343 struct axmap_level *al = &axmap->levels[i];
366 if (ret < axmap->nr_bits)
372 static uint64_t axmap_first_free(struct axmap *axmap)
374 if (firstfree_valid(axmap))
375 return axmap->first_free;
377 axmap->first_free = axmap_find_first_free(axmap, axmap->nr_levels - 1, 0);
378 return axmap->first_free;
409 uint64_t axmap_next_free(struct axmap *axmap, uint64_t bit_nr)
414 if (firstfree_valid(axmap) && bit_nr < axmap->first_free)
415 return axmap->first_free;
417 if (!axmap_handler(axmap, bit_nr, axmap_next_free_fn, &data))
418 return axmap_first_free(axmap);
427 ret = axmap_find_first_free(axmap, data.level, data.offset);
431 return axmap_first_free(axmap);