Coverage Report

Created: 2026-11-30 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/extra/tre/tre-mem.c
Line
Count
Source
1
/*
2
  tre-mem.c - TRE memory allocator
3
4
  This software is released under a BSD-style license.
5
  See the file LICENSE for details and copyright.
6
7
*/
8
9
/*
10
  This memory allocator is for allocating small memory blocks efficiently
11
  in terms of memory overhead and execution speed.  The allocated blocks
12
  cannot be freed individually, only all at once.  There can be multiple
13
  allocators, though.
14
*/
15
16
#ifdef HAVE_CONFIG_H
17
#include <config.h>
18
#endif /* HAVE_CONFIG_H */
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include "tre-internal.h"
23
#include "tre-mem.h"
24
#include "xmalloc.h"
25
26
27
/* Returns a new memory allocator or NULL if out of memory. */
28
tre_mem_t
29
tre_mem_new_impl(int provided, void *provided_block)
30
729k
{
31
729k
  tre_mem_t mem;
32
729k
  if (provided)
33
0
    {
34
0
      mem = provided_block;
35
0
      memset(mem, 0, sizeof(*mem));
36
0
    }
37
729k
  else
38
729k
    mem = xcalloc(1, sizeof(*mem));
39
729k
  if (mem == NULL)
40
0
    return NULL;
41
729k
  return mem;
42
729k
}
43
44
45
/* Frees the memory allocator and all memory allocated with it. */
46
void
47
tre_mem_destroy(tre_mem_t mem)
48
729k
{
49
729k
  tre_list_t *tmp, *l = mem->blocks;
50
51
4.14M
  while (l != NULL)
52
3.41M
    {
53
3.41M
      xfree(l->data);
54
3.41M
      tmp = l->next;
55
3.41M
      xfree(l);
56
3.41M
      l = tmp;
57
3.41M
    }
58
729k
  xfree(mem);
59
729k
}
60
61
62
/* Allocates a block of `size' bytes from `mem'.  Returns a pointer to the
63
   allocated block or NULL if an underlying malloc() failed. */
64
void *
65
tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
66
       int zero, size_t size)
67
45.9M
{
68
45.9M
  void *ptr;
69
70
45.9M
  if (mem->failed)
71
0
    {
72
0
      DPRINT(("tre_mem_alloc: oops, called after failure?!\n"));
73
0
      return NULL;
74
0
    }
75
76
#ifdef MALLOC_DEBUGGING
77
  if (!provided)
78
    {
79
      ptr = xmalloc(1);
80
      if (ptr == NULL)
81
  {
82
    DPRINT(("tre_mem_alloc: xmalloc forced failure\n"));
83
    mem->failed = 1;
84
    return NULL;
85
  }
86
      xfree(ptr);
87
    }
88
#endif /* MALLOC_DEBUGGING */
89
90
45.9M
  if (mem->n < size)
91
3.41M
    {
92
      /* We need more memory than is available in the current block.
93
   Allocate a new block. */
94
3.41M
      tre_list_t *l;
95
3.41M
      if (provided)
96
0
  {
97
0
    DPRINT(("tre_mem_alloc: using provided block\n"));
98
0
    if (provided_block == NULL)
99
0
      {
100
0
        DPRINT(("tre_mem_alloc: provided block was NULL\n"));
101
0
        mem->failed = 1;
102
0
        return NULL;
103
0
      }
104
0
    mem->ptr = provided_block;
105
0
    mem->n = TRE_MEM_BLOCK_SIZE;
106
0
  }
107
3.41M
      else
108
3.41M
  {
109
3.41M
    size_t block_size;
110
3.41M
    if (size * 8 > TRE_MEM_BLOCK_SIZE)
111
205k
      block_size = size * 8;
112
3.21M
    else
113
3.21M
      block_size = TRE_MEM_BLOCK_SIZE;
114
3.41M
    DPRINT(("tre_mem_alloc: allocating new %lu byte block\n",
115
3.41M
      (unsigned long)block_size));
116
3.41M
    l = xmalloc(sizeof(*l));
117
3.41M
    if (l == NULL)
118
0
      {
119
0
        mem->failed = 1;
120
0
        return NULL;
121
0
      }
122
3.41M
    l->data = xmalloc(block_size);
123
3.41M
    if (l->data == NULL)
124
0
      {
125
0
        xfree(l);
126
0
        mem->failed = 1;
127
0
        return NULL;
128
0
      }
129
3.41M
    l->next = NULL;
130
3.41M
    if (mem->current != NULL)
131
2.68M
      mem->current->next = l;
132
3.41M
    if (mem->blocks == NULL)
133
729k
      mem->blocks = l;
134
3.41M
    mem->current = l;
135
3.41M
    mem->ptr = l->data;
136
3.41M
    mem->n = block_size;
137
3.41M
  }
138
3.41M
    }
139
140
  /* Make sure the next pointer will be aligned. */
141
45.9M
  size += ALIGN(mem->ptr + size, anytype);
142
143
  /* Allocate from current block. */
144
45.9M
  ptr = mem->ptr;
145
45.9M
  mem->ptr += size;
146
45.9M
  mem->n -= size;
147
148
  /* Set to zero if needed. */
149
45.9M
  if (zero)
150
44.0M
    memset(ptr, 0, size);
151
152
45.9M
  return ptr;
153
45.9M
}
154
155
/* EOF */