File: eggarrayimpl.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (334 lines) | stat: -rw-r--r-- 8,076 bytes parent folder | download | duplicates (3)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright © 2020 Benjamin Otte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include <glib.h>

G_BEGIN_DECLS

#ifndef EGG_ARRAY_TYPE_NAME
#define EGG_ARRAY_TYPE_NAME EggArray
#endif

#ifndef EGG_ARRAY_NAME
#define EGG_ARRAY_NAME egg_array
#endif

#ifndef EGG_ARRAY_ELEMENT_TYPE
#define EGG_ARRAY_ELEMENT_TYPE gpointer
#endif

#ifdef EGG_ARRAY_PREALLOC
#if EGG_ARRAY_PREALLOC == 0
#undef EGG_ARRAY_PREALLOC
#endif
#endif

#ifdef EGG_ARRAY_NULL_TERMINATED
#define EGG_ARRAY_REAL_SIZE(_size) ((_size) + 1)
#define EGG_ARRAY_MAX_SIZE (G_MAXSIZE / sizeof (_T_) - 1)
#else
#define EGG_ARRAY_REAL_SIZE(_size) (_size)
#define EGG_ARRAY_MAX_SIZE (G_MAXSIZE / sizeof (_T_))
#endif

/* make this readable */
#define _T_ EGG_ARRAY_ELEMENT_TYPE
#define EggArray EGG_ARRAY_TYPE_NAME
#define egg_array_paste_more(EGG_ARRAY_NAME, func_name) EGG_ARRAY_NAME ## _ ## func_name
#define egg_array_paste(EGG_ARRAY_NAME, func_name) egg_array_paste_more (EGG_ARRAY_NAME, func_name)
#define egg_array(func_name) egg_array_paste (EGG_ARRAY_NAME, func_name)

typedef struct EggArray EggArray;

struct EggArray
{
  _T_ *start;
  _T_ *end;
  _T_ *end_allocation;
#ifdef EGG_ARRAY_PREALLOC
  _T_ preallocated[EGG_ARRAY_REAL_SIZE(EGG_ARRAY_PREALLOC)];
#endif
};

/* no G_GNUC_UNUSED here, if you don't use an array type, remove it. */
static inline void
egg_array(init) (EggArray *self)
{
#ifdef EGG_ARRAY_PREALLOC
  self->start = self->preallocated;
  self->end = self->start;
  self->end_allocation = self->start + EGG_ARRAY_PREALLOC;
#ifdef EGG_ARRAY_NULL_TERMINATED
  *self->start = *(_T_[1]) { 0 };
#endif
#else
  self->start = NULL;
  self->end = NULL;
  self->end_allocation = NULL;
#endif
}

G_GNUC_UNUSED static inline gsize
egg_array(get_capacity) (const EggArray *self)
{
  return self->end_allocation - self->start;
}

G_GNUC_UNUSED static inline gsize
egg_array(get_size) (const EggArray *self)
{
  return self->end - self->start;
}

static inline void
egg_array(free_elements) (_T_ *start,
                          _T_ *end)
{
#ifdef EGG_ARRAY_FREE_FUNC
  _T_ *e;
  for (e = start; e < end; e++)
#ifdef EGG_ARRAY_BY_VALUE
    EGG_ARRAY_FREE_FUNC (e);
#else
    EGG_ARRAY_FREE_FUNC (*e);
#endif
#endif
}

/* no G_GNUC_UNUSED here */
static inline void
egg_array(clear) (EggArray *self)
{
  egg_array(free_elements) (self->start, self->end);

#ifdef EGG_ARRAY_PREALLOC
  if (self->start != self->preallocated)
#endif
    g_free (self->start);
  egg_array(init) (self);
}

/*
 * egg_array_steal:
 * @self: the array
 *
 * Steals all data in the array and clears the array.
 *
 * If you need to know the size of the data, you should query it
 * beforehand.
 *
 * Returns: The array's data
 **/
G_GNUC_UNUSED static inline _T_ *
egg_array(steal) (EggArray *self)
{
  _T_ *result;

#ifdef EGG_ARRAY_PREALLOC
  if (self->start == self->preallocated)
    {
      gsize size = EGG_ARRAY_REAL_SIZE (egg_array(get_size) (self));
      result = g_new (_T_, size);
      memcpy (result, self->preallocated, sizeof (_T_) * size);
    }
  else
#endif
    result = self->start;

  egg_array(init) (self);

  return result;
}

G_GNUC_UNUSED static inline _T_ *
egg_array(get_data) (const EggArray *self)
{
  return self->start;
}

G_GNUC_UNUSED static inline _T_ *
egg_array(index) (const EggArray *self,
                  gsize           pos)
{
  return self->start + pos;
}

G_GNUC_UNUSED static inline gboolean
egg_array(is_empty) (const EggArray *self)
{
  return self->end == self->start;
}

G_GNUC_UNUSED static inline void
egg_array(reserve) (EggArray *self,
                    gsize      n)
{
  gsize new_capacity, size, capacity;

  if (G_UNLIKELY (n > EGG_ARRAY_MAX_SIZE))
    g_error ("requesting array size of %zu, but maximum size is %zu", n, EGG_ARRAY_MAX_SIZE);

  capacity = egg_array(get_capacity) (self);
  if (n <= capacity)
     return;

  size = egg_array(get_size) (self);
  /* capacity * 2 can overflow, that's why we MAX() */
  new_capacity = MAX (EGG_ARRAY_REAL_SIZE (n), capacity * 2);

#ifdef EGG_ARRAY_PREALLOC
  if (self->start == self->preallocated)
    {
      self->start = g_new (_T_, new_capacity);
      memcpy (self->start, self->preallocated, sizeof (_T_) * EGG_ARRAY_REAL_SIZE (size));
    }
  else
#endif
#ifdef EGG_ARRAY_NULL_TERMINATED
  if (self->start == NULL)
    {
      self->start = g_new (_T_, new_capacity);
      *self->start = *(_T_[1]) { 0 };
    }
  else
#endif
    self->start = g_renew (_T_, self->start, new_capacity);

  self->end = self->start + size;
  self->end_allocation = self->start + new_capacity;
#ifdef EGG_ARRAY_NULL_TERMINATED
  self->end_allocation--;
#endif
}

G_GNUC_UNUSED static inline void
egg_array(splice) (EggArray *self,
                   gsize      pos,
                   gsize      removed,
                   gboolean   stolen,
#ifdef EGG_ARRAY_BY_VALUE
                   const _T_ *additions,
#else
                   _T_       *additions,
#endif
                   gsize      added)
{
  gsize size;
  gsize remaining;

  size = egg_array(get_size) (self);
  g_assert (pos + removed <= size);
  remaining = size - pos - removed;

  if (!stolen)
    egg_array(free_elements) (egg_array(index) (self, pos),
                              egg_array(index) (self, pos + removed));

  egg_array(reserve) (self, size - removed + added);

  if (EGG_ARRAY_REAL_SIZE (remaining) && removed != added)
    memmove (egg_array(index) (self, pos + added),
             egg_array(index) (self, pos + removed),
             EGG_ARRAY_REAL_SIZE (remaining) * sizeof (_T_));

  if (added)
    {
      if (additions)
        memcpy (egg_array(index) (self, pos),
                additions,
                added * sizeof (_T_));
#ifndef EGG_ARRAY_NO_MEMSET
      else
        memset (egg_array(index) (self, pos), 0, added * sizeof (_T_));
#endif
    }


  /* might overflow, but does the right thing */
  self->end += added - removed;
}

G_GNUC_UNUSED static void
egg_array(set_size) (EggArray *self,
                     gsize     new_size)
{
  gsize old_size = egg_array(get_size) (self);
  if (new_size > old_size)
    egg_array(splice) (self, old_size, 0, FALSE, NULL, new_size - old_size);
  else
    egg_array(splice) (self, new_size, old_size - new_size, FALSE, NULL, 0);
}

G_GNUC_UNUSED static void
egg_array(append) (EggArray *self,
#ifdef EGG_ARRAY_BY_VALUE
                   _T_       *value)
#else
                   _T_        value)
#endif
{
  egg_array(splice) (self,
                     egg_array(get_size) (self),
                     0,
                     FALSE,
#ifdef EGG_ARRAY_BY_VALUE
                     value,
#else
                     &value,
#endif
                     1);
}

#ifdef EGG_ARRAY_BY_VALUE
G_GNUC_UNUSED static _T_ *
egg_array(get) (const EggArray *self,
                gsize           pos)
{
  return egg_array(index) (self, pos);
}
#else
G_GNUC_UNUSED static _T_
egg_array(get) (const EggArray *self,
                gsize           pos)
 {
   return *egg_array(index) (self, pos);
 }
#endif

#ifndef EGG_ARRAY_NO_UNDEF

#undef _T_
#undef EggArray
#undef egg_array_paste_more
#undef egg_array_paste
#undef egg_array
#undef EGG_ARRAY_REAL_SIZE
#undef EGG_ARRAY_MAX_SIZE

#undef EGG_ARRAY_BY_VALUE
#undef EGG_ARRAY_ELEMENT_TYPE
#undef EGG_ARRAY_FREE_FUNC
#undef EGG_ARRAY_NAME
#undef EGG_ARRAY_NULL_TERMINATED
#undef EGG_ARRAY_PREALLOC
#undef EGG_ARRAY_TYPE_NAME
#undef EGG_ARRAY_NO_MEMSET
#endif

G_END_DECLS