Coverage Report

Created: 2026-10-31 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/gobject/gatomicarray.c
Line
Count
Source
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.org>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "config.h"
21
22
#include "../glib/gvalgrind.h"
23
#include <string.h>
24
25
#include "gatomicarray.h"
26
27
/* A GAtomicArray is a growable, mutable array of data
28
 * generally of the form of a header of a specific size and
29
 * then an array of items of a fixed size.
30
 *
31
 * It is possible to do lock-less read transactions from the
32
 * array without any protection against other reads or writes,
33
 * but such read operation must be aware that the data in the
34
 * atomic array can change at any time during the transaction,
35
 * and only at the end can we verify if the transaction succeeded
36
 * or not. Thus the reading transaction cannot for instance
37
 * dereference a pointer in the array inside the transaction.
38
 *
39
 * The size of an array however cannot change during a read
40
 * transaction.
41
 *
42
 * Writes to the array is done in a copy-update style, but there
43
 * is no real protection against multiple writers overwriting each
44
 * others updates, so writes must be protected by an external lock.
45
 */
46
47
G_LOCK_DEFINE_STATIC (array);
48
49
typedef struct _FreeListNode FreeListNode;
50
struct _FreeListNode {
51
  FreeListNode *next;
52
};
53
54
/* This is really a list of array memory blocks, using the
55
 * first item as the next pointer to chain them together.
56
 * Protected by array lock */
57
static FreeListNode *freelist = NULL;
58
59
/* must hold array lock */
60
static gpointer
61
freelist_alloc (gsize size, gboolean reuse)
62
0
{
63
0
  gpointer mem;
64
0
  FreeListNode *free, **prev;
65
0
  gsize real_size;
66
67
0
  if (reuse)
68
0
    {
69
0
      for (free = freelist, prev = &freelist; free != NULL; prev = &free->next, free = free->next)
70
0
  {
71
0
    if (G_ATOMIC_ARRAY_DATA_SIZE (free) == size)
72
0
      {
73
0
        *prev = free->next;
74
0
        return (gpointer)free;
75
0
      }
76
0
  }
77
0
    }
78
79
0
  real_size = sizeof (gsize) + MAX (size, sizeof (FreeListNode));
80
0
  mem = g_slice_alloc (real_size);
81
0
  mem = ((char *) mem) + sizeof (gsize);
82
0
  G_ATOMIC_ARRAY_DATA_SIZE (mem) = size;
83
84
0
#if ENABLE_VALGRIND
85
0
  VALGRIND_MALLOCLIKE_BLOCK (mem, real_size - sizeof (gsize), FALSE, FALSE);
86
0
#endif
87
88
0
  return mem;
89
0
}
90
91
/* must hold array lock */
92
static void
93
freelist_free (gpointer mem)
94
0
{
95
0
  FreeListNode *free;
96
97
0
  free = mem;
98
0
  free->next = freelist;
99
0
  freelist = free;
100
0
}
101
102
void
103
_g_atomic_array_init (GAtomicArray *array)
104
160
{
105
160
  array->data = NULL;
106
160
}
107
108
/* Get a copy of the data (if non-NULL) that
109
 * can be changed and then re-applied with
110
 * g_atomic_array_update().
111
 *
112
 * If additional_element_size is > 0 then
113
 * then the new memory chunk is that much
114
 * larger, or there were no data we return
115
 * a chunk of header_size + additional_element_size.
116
 * This means you can use this to grow the
117
 * array part and it handles the first element
118
 * being added automatically.
119
 *
120
 * We don't support shrinking arrays, as if
121
 * we then re-grow we may reuse an old pointer
122
 * value and confuse the transaction check.
123
 */
124
gpointer
125
_g_atomic_array_copy (GAtomicArray *array,
126
          gsize header_size,
127
          gsize additional_element_size)
128
200
{
129
200
  guint8 *new, *old;
130
200
  gsize old_size, new_size;
131
132
200
  G_LOCK (array);
133
200
  old = g_atomic_pointer_get (&array->data);
134
200
  if (old)
135
0
    {
136
0
      old_size = G_ATOMIC_ARRAY_DATA_SIZE (old);
137
0
      new_size = old_size + additional_element_size;
138
      /* Don't reuse if copying to same size, as this may end
139
   up reusing the same pointer for the same array thus
140
   confusing the transaction check */
141
0
      new = freelist_alloc (new_size, additional_element_size != 0);
142
0
      memcpy (new, old, old_size);
143
0
    }
144
200
  else if (additional_element_size != 0)
145
0
    {
146
0
      new_size = header_size + additional_element_size;
147
0
      new = freelist_alloc (new_size, TRUE);
148
0
    }
149
200
  else
150
200
    new = NULL;
151
200
  G_UNLOCK (array);
152
200
  return new;
153
200
}
154
155
/* Replace the data in the array with the new data,
156
 * freeing the old data (for reuse). The new data may
157
 * not be smaller than the current data.
158
 */
159
void
160
_g_atomic_array_update (GAtomicArray *array,
161
      gpointer new_data)
162
0
{
163
0
  guint8 *old;
164
165
0
  G_LOCK (array);
166
0
  old = g_atomic_pointer_exchange (&array->data, new_data);
167
168
#ifdef G_DISABLE_ASSERT
169
  if (old && G_ATOMIC_ARRAY_DATA_SIZE (new_data) < G_ATOMIC_ARRAY_DATA_SIZE (old))
170
    {
171
      g_atomic_pointer_set (&array->data, old);
172
      g_return_if_reached ();
173
    }
174
#else
175
0
  g_assert (old == NULL || G_ATOMIC_ARRAY_DATA_SIZE (old) <= G_ATOMIC_ARRAY_DATA_SIZE (new_data));
176
0
#endif
177
178
0
  if (old)
179
0
    freelist_free (old);
180
0
  G_UNLOCK (array);
181
0
}