1b8e80941Smrg/**************************************************************************
2b8e80941Smrg *
3b8e80941Smrg * Copyright 2017 Valve Corporation
4b8e80941Smrg * All Rights Reserved.
5b8e80941Smrg *
6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7b8e80941Smrg * copy of this software and associated documentation files (the
8b8e80941Smrg * "Software"), to deal in the Software without restriction, including
9b8e80941Smrg * without limitation the rights to use, copy, modify, merge, publish,
10b8e80941Smrg * distribute, sub license, and/or sell copies of the Software, and to
11b8e80941Smrg * permit persons to whom the Software is furnished to do so, subject to
12b8e80941Smrg * the following conditions:
13b8e80941Smrg *
14b8e80941Smrg * The above copyright notice and this permission notice (including the
15b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
16b8e80941Smrg * of the Software.
17b8e80941Smrg *
18b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20b8e80941Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21b8e80941Smrg * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22b8e80941Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23b8e80941Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24b8e80941Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25b8e80941Smrg *
26b8e80941Smrg **************************************************************************/
27b8e80941Smrg
28b8e80941Smrg/**
29b8e80941Smrg * @file
30b8e80941Smrg * A simple allocator that allocates and release "numbers".
31b8e80941Smrg *
32b8e80941Smrg * @author Samuel Pitoiset <samuel.pitoiset@gmail.com>
33b8e80941Smrg */
34b8e80941Smrg
35b8e80941Smrg#include "util/u_idalloc.h"
36b8e80941Smrg#include "util/u_math.h"
37b8e80941Smrg#include "util/u_memory.h"
38b8e80941Smrg
39b8e80941Smrgvoid
40b8e80941Smrgutil_idalloc_init(struct util_idalloc *buf)
41b8e80941Smrg{
42b8e80941Smrg   memset(buf, 0, sizeof(*buf));
43b8e80941Smrg}
44b8e80941Smrg
45b8e80941Smrgvoid
46b8e80941Smrgutil_idalloc_fini(struct util_idalloc *buf)
47b8e80941Smrg{
48b8e80941Smrg   if (buf->data)
49b8e80941Smrg      free(buf->data);
50b8e80941Smrg}
51b8e80941Smrg
52b8e80941Smrgvoid
53b8e80941Smrgutil_idalloc_resize(struct util_idalloc *buf, unsigned new_num_elements)
54b8e80941Smrg{
55b8e80941Smrg   new_num_elements = align(new_num_elements, 32);
56b8e80941Smrg
57b8e80941Smrg   if (new_num_elements > buf->num_elements) {
58b8e80941Smrg      unsigned i;
59b8e80941Smrg
60b8e80941Smrg      buf->data = realloc(buf->data,
61b8e80941Smrg                          (new_num_elements / 32) * sizeof(*buf->data));
62b8e80941Smrg
63b8e80941Smrg      for (i = buf->num_elements / 32; i < new_num_elements / 32; i++)
64b8e80941Smrg         buf->data[i] = 0;
65b8e80941Smrg      buf->num_elements = new_num_elements;
66b8e80941Smrg   }
67b8e80941Smrg}
68b8e80941Smrg
69b8e80941Smrgunsigned
70b8e80941Smrgutil_idalloc_alloc(struct util_idalloc *buf)
71b8e80941Smrg{
72b8e80941Smrg   unsigned num_elements = buf->num_elements;
73b8e80941Smrg
74b8e80941Smrg   for (unsigned i = 0; i < num_elements / 32; i++) {
75b8e80941Smrg      if (buf->data[i] == 0xffffffff)
76b8e80941Smrg         continue;
77b8e80941Smrg
78b8e80941Smrg      unsigned bit = ffs(~buf->data[i]) - 1;
79b8e80941Smrg      buf->data[i] |= 1u << bit;
80b8e80941Smrg      return i * 32 + bit;
81b8e80941Smrg   }
82b8e80941Smrg
83b8e80941Smrg   /* No slots available, resize and return the first free. */
84b8e80941Smrg   util_idalloc_resize(buf, num_elements * 2);
85b8e80941Smrg
86b8e80941Smrg   buf->data[num_elements / 32] |= 1 << (num_elements % 32);
87b8e80941Smrg
88b8e80941Smrg   return num_elements;
89b8e80941Smrg}
90b8e80941Smrg
91b8e80941Smrgvoid
92b8e80941Smrgutil_idalloc_free(struct util_idalloc *buf, unsigned id)
93b8e80941Smrg{
94b8e80941Smrg   assert(id < buf->num_elements);
95b8e80941Smrg   buf->data[id / 32] &= ~(1 << (id % 32));
96b8e80941Smrg}
97