Coverage Report

Created: 2026-10-28 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/librabbitmq/librabbitmq/amqp_mem.c
Line
Count
Source
1
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2
// SPDX-License-Identifier: mit
3
4
#ifdef HAVE_CONFIG_H
5
#include "config.h"
6
#endif
7
8
#include "amqp_private.h"
9
#include <assert.h>
10
#include <stdint.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <sys/types.h>
15
16
0
char const *amqp_version(void) { return AMQP_VERSION_STRING; }
17
18
0
uint32_t amqp_version_number(void) { return AMQP_VERSION; }
19
20
13.4k
void init_amqp_pool(amqp_pool_t *pool, size_t pagesize) {
21
13.4k
  pool->pagesize = pagesize ? pagesize : 4096;
22
23
13.4k
  pool->pages.num_blocks = 0;
24
13.4k
  pool->pages.blocklist = NULL;
25
26
13.4k
  pool->large_blocks.num_blocks = 0;
27
13.4k
  pool->large_blocks.blocklist = NULL;
28
29
13.4k
  pool->next_page = 0;
30
13.4k
  pool->alloc_block = NULL;
31
13.4k
  pool->alloc_used = 0;
32
13.4k
}
33
34
30.3k
static void empty_blocklist(amqp_pool_blocklist_t *x) {
35
30.3k
  int i;
36
37
30.3k
  if (x->blocklist != NULL) {
38
21.0k
    for (i = 0; i < x->num_blocks; i++) {
39
12.6k
      free(x->blocklist[i]);
40
12.6k
    }
41
8.40k
    free(x->blocklist);
42
8.40k
  }
43
30.3k
  x->num_blocks = 0;
44
30.3k
  x->blocklist = NULL;
45
30.3k
}
46
47
16.9k
void recycle_amqp_pool(amqp_pool_t *pool) {
48
16.9k
  empty_blocklist(&pool->large_blocks);
49
16.9k
  pool->next_page = 0;
50
16.9k
  pool->alloc_block = NULL;
51
16.9k
  pool->alloc_used = 0;
52
16.9k
}
53
54
13.4k
void empty_amqp_pool(amqp_pool_t *pool) {
55
13.4k
  recycle_amqp_pool(pool);
56
13.4k
  empty_blocklist(&pool->pages);
57
13.4k
}
58
59
/* Returns 1 on success, 0 on failure */
60
12.6k
static int record_pool_block(amqp_pool_blocklist_t *x, void *block) {
61
12.6k
  size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
62
63
12.6k
  if (x->blocklist == NULL) {
64
8.40k
    x->blocklist = malloc(blocklistlength);
65
8.40k
    if (x->blocklist == NULL) {
66
0
      return 0;
67
0
    }
68
8.40k
  } else {
69
4.23k
    void *newbl = realloc(x->blocklist, blocklistlength);
70
4.23k
    if (newbl == NULL) {
71
0
      return 0;
72
0
    }
73
4.23k
    x->blocklist = newbl;
74
4.23k
  }
75
76
12.6k
  x->blocklist[x->num_blocks] = block;
77
12.6k
  x->num_blocks++;
78
12.6k
  return 1;
79
12.6k
}
80
81
1.18M
void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
82
1.18M
  if (amount == 0) {
83
16.9k
    return NULL;
84
16.9k
  }
85
86
1.16M
  amount = (amount + 7) & (~7); /* round up to nearest 8-byte boundary */
87
88
1.16M
  if (amount > pool->pagesize) {
89
229
    void *result = calloc(1, amount);
90
229
    if (result == NULL) {
91
0
      return NULL;
92
0
    }
93
229
    if (!record_pool_block(&pool->large_blocks, result)) {
94
0
      free(result);
95
0
      return NULL;
96
0
    }
97
229
    return result;
98
229
  }
99
100
1.16M
  if (pool->alloc_block != NULL) {
101
1.15M
    assert(pool->alloc_used <= pool->pagesize);
102
103
1.15M
    if (pool->alloc_used + amount <= pool->pagesize) {
104
1.14M
      void *result = pool->alloc_block + pool->alloc_used;
105
1.14M
      pool->alloc_used += amount;
106
1.14M
      return result;
107
1.14M
    }
108
1.15M
  }
109
110
15.2k
  if (pool->next_page >= pool->pages.num_blocks) {
111
12.4k
    pool->alloc_block = calloc(1, pool->pagesize);
112
12.4k
    if (pool->alloc_block == NULL) {
113
0
      return NULL;
114
0
    }
115
12.4k
    if (!record_pool_block(&pool->pages, pool->alloc_block)) {
116
0
      return NULL;
117
0
    }
118
12.4k
    pool->next_page = pool->pages.num_blocks;
119
12.4k
  } else {
120
2.80k
    pool->alloc_block = pool->pages.blocklist[pool->next_page];
121
2.80k
    pool->next_page++;
122
2.80k
  }
123
124
15.2k
  pool->alloc_used = amount;
125
126
15.2k
  return pool->alloc_block;
127
15.2k
}
128
129
void amqp_pool_alloc_bytes(amqp_pool_t *pool, size_t amount,
130
305k
                           amqp_bytes_t *output) {
131
305k
  output->len = amount;
132
305k
  output->bytes = amqp_pool_alloc(pool, amount);
133
305k
}
134
135
15.5k
amqp_bytes_t amqp_cstring_bytes(char const *cstr) {
136
15.5k
  amqp_bytes_t result;
137
15.5k
  result.len = strlen(cstr);
138
15.5k
  result.bytes = (void *)cstr;
139
15.5k
  return result;
140
15.5k
}
141
142
0
amqp_bytes_t amqp_bytes_malloc_dup(amqp_bytes_t src) {
143
0
  amqp_bytes_t result;
144
0
  result.len = src.len;
145
0
  result.bytes = malloc(src.len);
146
0
  if (result.bytes != NULL) {
147
0
    memcpy(result.bytes, src.bytes, src.len);
148
0
  }
149
0
  return result;
150
0
}
151
152
0
amqp_bytes_t amqp_bytes_malloc(size_t amount) {
153
0
  amqp_bytes_t result;
154
0
  result.len = amount;
155
0
  result.bytes = malloc(amount); /* will return NULL if it fails */
156
0
  return result;
157
0
}
158
159
0
void amqp_bytes_free(amqp_bytes_t bytes) { free(bytes.bytes); }
160
161
amqp_pool_t *amqp_get_or_create_channel_pool(amqp_connection_state_t state,
162
860k
                                             amqp_channel_t channel) {
163
860k
  amqp_pool_table_entry_t *entry;
164
860k
  size_t index = channel % POOL_TABLE_SIZE;
165
166
860k
  entry = state->pool_table[index];
167
168
1.79M
  for (; NULL != entry; entry = entry->next) {
169
1.79M
    if (channel == entry->channel) {
170
853k
      return &entry->pool;
171
853k
    }
172
1.79M
  }
173
174
7.01k
  entry = malloc(sizeof(amqp_pool_table_entry_t));
175
7.01k
  if (NULL == entry) {
176
0
    return NULL;
177
0
  }
178
179
7.01k
  entry->channel = channel;
180
7.01k
  entry->next = state->pool_table[index];
181
7.01k
  state->pool_table[index] = entry;
182
183
7.01k
  init_amqp_pool(&entry->pool, state->frame_max);
184
185
7.01k
  return &entry->pool;
186
7.01k
}
187
188
amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state,
189
3.46k
                                   amqp_channel_t channel) {
190
3.46k
  amqp_pool_table_entry_t *entry;
191
3.46k
  size_t index = channel % POOL_TABLE_SIZE;
192
193
3.46k
  entry = state->pool_table[index];
194
195
4.45k
  for (; NULL != entry; entry = entry->next) {
196
4.45k
    if (channel == entry->channel) {
197
3.46k
      return &entry->pool;
198
3.46k
    }
199
4.45k
  }
200
201
0
  return NULL;
202
3.46k
}
203
204
0
int amqp_bytes_equal(amqp_bytes_t r, amqp_bytes_t l) {
205
0
  if (r.len == l.len &&
206
0
      (r.bytes == l.bytes || 0 == memcmp(r.bytes, l.bytes, r.len))) {
207
0
    return 1;
208
0
  }
209
0
  return 0;
210
0
}