Coverage Report

Created: 2026-11-13 07:26

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
732k
{
31
732k
  tre_mem_t mem;
32
732k
  if (provided)
33
0
    {
34
0
      mem = provided_block;
35
0
      memset(mem, 0, sizeof(*mem));
36
0
    }
37
732k
  else
38
732k
    mem = xcalloc(1, sizeof(*mem));
39
732k
  if (mem == NULL)
40
0
    return NULL;
41
732k
  return mem;
42
732k
}
43
44
45
/* Frees the memory allocator and all memory allocated with it. */
46
void
47
tre_mem_destroy(tre_mem_t mem)
48
732k
{
49
732k
  tre_list_t *tmp, *l = mem->blocks;
50
51
3.44M
  while (l != NULL)
52
2.71M
    {
53
2.71M
      xfree(l->data);
54
2.71M
      tmp = l->next;
55
2.71M
      xfree(l);
56
2.71M
      l = tmp;
57
2.71M
    }
58
732k
  xfree(mem);
59
732k
}
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
39.8M
{
68
39.8M
  void *ptr;
69
70
39.8M
  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
39.8M
  if (mem->n < size)
91
2.71M
    {
92
      /* We need more memory than is available in the current block.
93
   Allocate a new block. */
94
2.71M
      tre_list_t *l;
95
2.71M
      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
2.71M
      else
108
2.71M
  {
109
2.71M
    size_t block_size;
110
2.71M
    if (size * 8 > TRE_MEM_BLOCK_SIZE)
111
19.0k
      block_size = size * 8;
112
2.69M
    else
113
2.69M
      block_size = TRE_MEM_BLOCK_SIZE;
114
2.71M
    DPRINT(("tre_mem_alloc: allocating new %lu byte block\n",
115
2.71M
      (unsigned long)block_size));
116
2.71M
    l = xmalloc(sizeof(*l));
117
2.71M
    if (l == NULL)
118
0
      {
119
0
        mem->failed = 1;
120
0
        return NULL;
121
0
      }
122
2.71M
    l->data = xmalloc(block_size);
123
2.71M
    if (l->data == NULL)
124
0
      {
125
0
        xfree(l);
126
0
        mem->failed = 1;
127
0
        return NULL;
128
0
      }
129
2.71M
    l->next = NULL;
130
2.71M
    if (mem->current != NULL)
131
1.98M
      mem->current->next = l;
132
2.71M
    if (mem->blocks == NULL)
133
732k
      mem->blocks = l;
134
2.71M
    mem->current = l;
135
2.71M
    mem->ptr = l->data;
136
2.71M
    mem->n = block_size;
137
2.71M
  }
138
2.71M
    }
139
140
  /* Make sure the next pointer will be aligned. */
141
39.8M
  size += ALIGN(mem->ptr + size, anytype);
142
143
  /* Allocate from current block. */
144
39.8M
  ptr = mem->ptr;
145
39.8M
  mem->ptr += size;
146
39.8M
  mem->n -= size;
147
148
  /* Set to zero if needed. */
149
39.8M
  if (zero)
150
37.8M
    memset(ptr, 0, size);
151
152
39.8M
  return ptr;
153
39.8M
}
154
155
/* EOF */