summaryrefslogtreecommitdiff
path: root/gladeui/glade-id-allocator.c
blob: 92611f98ff51eb956cc272fd9b15c509401786be (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
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
/*
 * Copyright (C) 2004 Owen Taylor
 *
 * Authors:
 *   Owen Taylor  <otaylor@redhat.com>
 *
 * Modified by the Glade developers
 * 
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "glade-id-allocator.h"

#include <glib.h>
#include <string.h>

#define INITIAL_WORDS 4

struct _GladeIDAllocator
{
  guint n_words;
  guint32 *data;
};

/**
 * glade_id_allocator_new:
 *
 * Returns: a new #GladeIDAllocator
 */
GladeIDAllocator *
glade_id_allocator_new (void)
{
  GladeIDAllocator *allocator = g_slice_new (GladeIDAllocator);

  allocator->n_words = INITIAL_WORDS;
  allocator->data    = g_new (guint32, INITIAL_WORDS);

  memset (allocator->data, 0xff, INITIAL_WORDS * sizeof (guint32));

  return allocator;
}

/**
 * glade_id_allocator_destroy:
 * @allocator: a #GladeIDAllocator
 *
 * Frees @allocator and its associated memory
 */
void
glade_id_allocator_destroy (GladeIDAllocator *allocator)
{
  g_return_if_fail (allocator != NULL);

  g_free (allocator->data);
  g_slice_free (GladeIDAllocator, allocator);
}

static inline gint
first_set_bit (guint32 word)
{
  static const char table[16] = {
    4, 0, 1, 0,
    2, 0, 1, 0,
    3, 0, 1, 0,
    2, 0, 1, 0
  };

  gint result = 0;

  if ((word & 0xffff) == 0)
    {
      word >>= 16;
      result += 16;
    }

  if ((word & 0xff) == 0)
    {
      word >>= 8;
      result += 8;
    }

  if ((word & 0xf) == 0)
    {
      word >>= 4;
      result += 4;
    }

  return result + table[word & 0xf];
}

/**
 * glade_id_allocator_allocate:
 * @allocator: a #GladeIDAllocator
 *
 * TODO: write me
 * Returns: the id
 */
guint
glade_id_allocator_allocate (GladeIDAllocator *allocator)
{
  guint i;

  g_return_val_if_fail (allocator != NULL, 0);

  for (i = 0; i < allocator->n_words; i++)
    {
      if (allocator->data[i] != 0)
        {
          gint free_bit = first_set_bit (allocator->data[i]);
          allocator->data[i] &= ~(1 << free_bit);

          return 32 * i + free_bit + 1;
        }
    }

  {
    guint n_words = allocator->n_words;

    allocator->data = g_renew (guint32, allocator->data, n_words * 2);
    memset (&allocator->data[n_words], 0xff, n_words * sizeof (guint32));
    allocator->n_words = n_words * 2;

    allocator->data[n_words] = 0xffffffff - 1;

    return 32 * n_words + 1;
  }
}

/**
 * glade_id_allocator_release:
 * @allocator: a #GladeIDAllocator
 * @id: the id given by glade_id_allocator_allocate()
 *
 * TODO: write me
 */
void
glade_id_allocator_release (GladeIDAllocator *allocator, guint id)
{
  guint word_idx;

  g_return_if_fail (allocator != NULL);

  /* Allocated ids start with 1 */
  if (id > 0)
    {
      id = id - 1;
      word_idx = id >> 5;

      /* Tollerate releasing ids that were never allocated with the allocator 
       * or are out of range... when we load Glade files with huge numbers it happens
       * that loaded unallocated ids are out of range
       */
      if (word_idx < allocator->n_words)
        allocator->data[word_idx] |= 1 << (id & 31);
    }
}

#ifdef GLADE_ID_ALLOCATOR_TEST
int
main (int argc, char **argv)
{
  GladeIDAllocator *allocator = glade_id_allocator_new ();
  guint i;
  guint iter;

  for (i = 0; i < 1000; i++)
    {
      guint id = glade_id_allocator_allocate (allocator);
      g_assert (id == i);
    }

  for (i = 0; i < 1000; i++)
    glade_id_allocator_release (allocator, i);

  for (iter = 0; iter < 10000; iter++)
    {
      for (i = 0; i < 1000; i++)
        glade_id_allocator_allocate (allocator);

      for (i = 0; i < 1000; i++)
        glade_id_allocator_release (allocator, i);
    }

  glade_id_allocator_destroy (allocator);

  return 0;
}
#endif