Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/libiberty/objalloc.c
Line
Count
Source
1
/* objalloc.c -- routines to allocate memory for objects
2
   Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor, Cygnus Solutions.
4
5
This program is free software; you can redistribute it and/or modify it
6
under the terms of the GNU General Public License as published by the
7
Free Software Foundation; either version 2, or (at your option) any
8
later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, 51 Franklin Street - Fifth Floor,
18
Boston, MA 02110-1301, USA.  */
19
20
#include "config.h"
21
#include "ansidecl.h"
22
23
#include "objalloc.h"
24
25
/* Get a definition for NULL.  */
26
#include <stdio.h>
27
28
#if VMS
29
#include <stdlib.h>
30
#include <unixlib.h>
31
#else
32
33
/* Get a definition for size_t.  */
34
#include <stddef.h>
35
36
#ifdef HAVE_STDLIB_H
37
#include <stdlib.h>
38
#else
39
/* For systems with larger pointers than ints, this must be declared.  */
40
extern void *malloc (size_t);
41
extern void free (void *);
42
#endif
43
44
#endif
45
46
/* These routines allocate space for an object.  Freeing allocated
47
   space may or may not free all more recently allocated space.
48
49
   We handle large and small allocation requests differently.  If we
50
   don't have enough space in the current block, and the allocation
51
   request is for more than 512 bytes, we simply pass it through to
52
   malloc.  */
53
54
/* The objalloc structure is defined in objalloc.h.  */
55
56
/* This structure appears at the start of each chunk.  */
57
58
struct objalloc_chunk
59
{
60
  /* Next chunk.  */
61
  struct objalloc_chunk *next;
62
  /* If this chunk contains large objects, this is the value of
63
     current_ptr when this chunk was allocated.  If this chunk
64
     contains small objects, this is NULL.  */
65
  char *current_ptr;
66
};
67
68
/* The aligned size of objalloc_chunk.  */
69
70
#define CHUNK_HEADER_SIZE         \
71
95.4M
  ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
72
95.4M
   &~ (OBJALLOC_ALIGN - 1))
73
74
/* We ask for this much memory each time we create a chunk which is to
75
   hold small objects.  */
76
77
203M
#define CHUNK_SIZE (4096 - 32)
78
79
/* A request for this amount or more is just passed through to malloc.  */
80
81
11.4M
#define BIG_REQUEST (512)
82
83
/* Create an objalloc structure.  */
84
85
struct objalloc *
86
objalloc_create (void)
87
28.9M
{
88
28.9M
  struct objalloc *ret;
89
28.9M
  struct objalloc_chunk *chunk;
90
91
28.9M
  ret = (struct objalloc *) malloc (sizeof *ret);
92
28.9M
  if (ret == NULL)
93
0
    return NULL;
94
95
28.9M
  ret->chunks = (void *) malloc (CHUNK_SIZE);
96
28.9M
  if (ret->chunks == NULL)
97
0
    {
98
0
      free (ret);
99
0
      return NULL;
100
0
    }
101
102
28.9M
  chunk = (struct objalloc_chunk *) ret->chunks;
103
28.9M
  chunk->next = NULL;
104
28.9M
  chunk->current_ptr = NULL;
105
106
28.9M
  ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
107
28.9M
  ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
108
109
28.9M
  return ret;
110
28.9M
}
111
112
/* Allocate space from an objalloc structure.  */
113
114
void *
115
_objalloc_alloc (struct objalloc *o, unsigned long original_len)
116
11.4M
{
117
11.4M
  unsigned long len = original_len;
118
119
  /* We avoid confusion from zero sized objects by always allocating
120
     at least 1 byte.  */
121
11.4M
  if (len == 0)
122
0
    len = 1;
123
124
11.4M
  len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
125
126
  /* Check for overflow in the alignment operation above and the
127
     malloc argument below. */
128
11.4M
  if (len + CHUNK_HEADER_SIZE < original_len)
129
0
    return NULL;
130
131
11.4M
  if (len <= o->current_space)
132
0
    {
133
0
      o->current_ptr += len;
134
0
      o->current_space -= len;
135
0
      return (void *) (o->current_ptr - len);
136
0
    }
137
138
11.4M
  if (len >= BIG_REQUEST)
139
10.7M
    {
140
10.7M
      char *ret;
141
10.7M
      struct objalloc_chunk *chunk;
142
143
10.7M
      ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
144
10.7M
      if (ret == NULL)
145
0
  return NULL;
146
147
10.7M
      chunk = (struct objalloc_chunk *) ret;
148
10.7M
      chunk->next = (struct objalloc_chunk *) o->chunks;
149
10.7M
      chunk->current_ptr = o->current_ptr;
150
151
10.7M
      o->chunks = (void *) chunk;
152
153
10.7M
      return (void *) (ret + CHUNK_HEADER_SIZE);
154
10.7M
    }
155
706k
  else
156
706k
    {
157
706k
      struct objalloc_chunk *chunk;
158
159
706k
      chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
160
706k
      if (chunk == NULL)
161
0
  return NULL;
162
706k
      chunk->next = (struct objalloc_chunk *) o->chunks;
163
706k
      chunk->current_ptr = NULL;
164
165
706k
      o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
166
706k
      o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
167
168
706k
      o->chunks = (void *) chunk;
169
170
706k
      return objalloc_alloc (o, len);
171
706k
    }
172
11.4M
}
173
174
/* Free an entire objalloc structure.  */
175
176
void
177
objalloc_free (struct objalloc *o)
178
38.2M
{
179
38.2M
  struct objalloc_chunk *l;
180
181
  /* Handle a nullptr as being a no-op. */
182
38.2M
  if (o == NULL)
183
9.31M
    return;
184
185
28.9M
  l = (struct objalloc_chunk *) o->chunks;
186
68.4M
  while (l != NULL)
187
39.4M
    {
188
39.4M
      struct objalloc_chunk *next;
189
190
39.4M
      next = l->next;
191
39.4M
      free (l);
192
39.4M
      l = next;
193
39.4M
    }
194
195
28.9M
  free (o);
196
28.9M
}
197
198
/* Free a block from an objalloc structure.  This also frees all more
199
   recently allocated blocks.  */
200
201
void
202
objalloc_free_block (struct objalloc *o, void *block)
203
72.1M
{
204
72.1M
  struct objalloc_chunk *p, *small;
205
72.1M
  char *b = (char *) block;
206
207
  /* First set P to the chunk which contains the block we are freeing,
208
     and set Q to the last small object chunk we see before P.  */
209
72.1M
  small = NULL;
210
75.3M
  for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
211
75.3M
    {
212
75.3M
      if (p->current_ptr == NULL)
213
72.3M
  {
214
72.3M
    if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
215
72.1M
      break;
216
242k
    small = p;
217
242k
  }
218
3.02M
      else
219
3.02M
  {
220
3.02M
    if (b == (char *) p + CHUNK_HEADER_SIZE)
221
11.6k
      break;
222
3.02M
  }
223
75.3M
    }
224
225
  /* If we can't find the chunk, the caller has made a mistake.  */
226
72.1M
  if (p == NULL)
227
0
    abort ();
228
229
72.1M
  if (p->current_ptr == NULL)
230
72.1M
    {
231
72.1M
      struct objalloc_chunk *q;
232
72.1M
      struct objalloc_chunk *first;
233
234
      /* The block is in a chunk containing small objects.  We can
235
   free every chunk through SMALL, because they have certainly
236
   been allocated more recently.  After SMALL, we will not see
237
   any chunks containing small objects; we can free any big
238
   chunk if the current_ptr is greater than or equal to B.  We
239
   can then reset the new current_ptr to B.  */
240
241
72.1M
      first = NULL;
242
72.1M
      q = (struct objalloc_chunk *) o->chunks;
243
75.3M
      while (q != p)
244
3.23M
  {
245
3.23M
    struct objalloc_chunk *next;
246
247
3.23M
    next = q->next;
248
3.23M
    if (small != NULL)
249
378k
      {
250
378k
        if (small == q)
251
83.7k
    small = NULL;
252
378k
        free (q);
253
378k
      }
254
2.85M
    else if (q->current_ptr > b)
255
551k
      free (q);
256
2.30M
    else if (first == NULL)
257
1.23M
      first = q;
258
259
3.23M
    q = next;
260
3.23M
  }
261
262
72.1M
      if (first == NULL)
263
70.8M
  first = p;
264
72.1M
      o->chunks = (void *) first;
265
266
      /* Now start allocating from this small block again.  */
267
72.1M
      o->current_ptr = b;
268
72.1M
      o->current_space = ((char *) p + CHUNK_SIZE) - b;
269
72.1M
    }
270
11.6k
  else
271
11.6k
    {
272
11.6k
      struct objalloc_chunk *q;
273
11.6k
      char *current_ptr;
274
275
      /* This block is in a large chunk by itself.  We can free
276
         everything on the list up to and including this block.  We
277
         then start allocating from the next chunk containing small
278
         objects, setting current_ptr from the value stored with the
279
         large chunk we are freeing.  */
280
281
11.6k
      current_ptr = p->current_ptr;
282
11.6k
      p = p->next;
283
284
11.6k
      q = (struct objalloc_chunk *) o->chunks;
285
41.4k
      while (q != p)
286
29.7k
  {
287
29.7k
    struct objalloc_chunk *next;
288
289
29.7k
    next = q->next;
290
29.7k
    free (q);
291
29.7k
    q = next;
292
29.7k
  }
293
294
11.6k
      o->chunks = (void *) p;
295
296
15.8k
      while (p->current_ptr != NULL)
297
4.19k
  p = p->next;
298
299
11.6k
      o->current_ptr = current_ptr;
300
11.6k
      o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
301
11.6k
    }
302
72.1M
}