Coverage Report

Created: 2026-07-30 06:49

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
11.1k
void init_amqp_pool(amqp_pool_t *pool, size_t pagesize) {
21
11.1k
  pool->pagesize = pagesize ? pagesize : 4096;
22
23
11.1k
  pool->pages.num_blocks = 0;
24
11.1k
  pool->pages.blocklist = NULL;
25
26
11.1k
  pool->large_blocks.num_blocks = 0;
27
11.1k
  pool->large_blocks.blocklist = NULL;
28
29
11.1k
  pool->next_page = 0;
30
11.1k
  pool->alloc_block = NULL;
31
11.1k
  pool->alloc_used = 0;
32
11.1k
}
33
34
42.0k
static void empty_blocklist(amqp_pool_blocklist_t *x) {
35
42.0k
  int i;
36
37
42.0k
  if (x->blocklist != NULL) {
38
12.5k
    for (i = 0; i < x->num_blocks; i++) {
39
6.37k
      free(x->blocklist[i]);
40
6.37k
    }
41
6.22k
    free(x->blocklist);
42
6.22k
  }
43
42.0k
  x->num_blocks = 0;
44
42.0k
  x->blocklist = NULL;
45
42.0k
}
46
47
30.8k
void recycle_amqp_pool(amqp_pool_t *pool) {
48
30.8k
  empty_blocklist(&pool->large_blocks);
49
30.8k
  pool->next_page = 0;
50
30.8k
  pool->alloc_block = NULL;
51
30.8k
  pool->alloc_used = 0;
52
30.8k
}
53
54
11.1k
void empty_amqp_pool(amqp_pool_t *pool) {
55
11.1k
  recycle_amqp_pool(pool);
56
11.1k
  empty_blocklist(&pool->pages);
57
11.1k
}
58
59
/* Returns 1 on success, 0 on failure */
60
6.37k
static int record_pool_block(amqp_pool_blocklist_t *x, void *block) {
61
6.37k
  size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
62
63
6.37k
  if (x->blocklist == NULL) {
64
6.22k
    x->blocklist = malloc(blocklistlength);
65
6.22k
    if (x->blocklist == NULL) {
66
0
      return 0;
67
0
    }
68
6.22k
  } else {
69
153
    void *newbl = realloc(x->blocklist, blocklistlength);
70
153
    if (newbl == NULL) {
71
0
      return 0;
72
0
    }
73
153
    x->blocklist = newbl;
74
153
  }
75
76
6.37k
  x->blocklist[x->num_blocks] = block;
77
6.37k
  x->num_blocks++;
78
6.37k
  return 1;
79
6.37k
}
80
81
77.8k
void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
82
77.8k
  if (amount == 0) {
83
2.49k
    return NULL;
84
2.49k
  }
85
86
75.3k
  amount = (amount + 7) & (~7); /* round up to nearest 8-byte boundary */
87
88
75.3k
  if (amount > pool->pagesize) {
89
82
    void *result = calloc(1, amount);
90
82
    if (result == NULL) {
91
0
      return NULL;
92
0
    }
93
82
    if (!record_pool_block(&pool->large_blocks, result)) {
94
0
      free(result);
95
0
      return NULL;
96
0
    }
97
82
    return result;
98
82
  }
99
100
75.2k
  if (pool->alloc_block != NULL) {
101
52.0k
    assert(pool->alloc_used <= pool->pagesize);
102
103
52.0k
    if (pool->alloc_used + amount <= pool->pagesize) {
104
51.9k
      void *result = pool->alloc_block + pool->alloc_used;
105
51.9k
      pool->alloc_used += amount;
106
51.9k
      return result;
107
51.9k
    }
108
52.0k
  }
109
110
23.3k
  if (pool->next_page >= pool->pages.num_blocks) {
111
6.29k
    pool->alloc_block = calloc(1, pool->pagesize);
112
6.29k
    if (pool->alloc_block == NULL) {
113
0
      return NULL;
114
0
    }
115
6.29k
    if (!record_pool_block(&pool->pages, pool->alloc_block)) {
116
0
      return NULL;
117
0
    }
118
6.29k
    pool->next_page = pool->pages.num_blocks;
119
17.0k
  } else {
120
17.0k
    pool->alloc_block = pool->pages.blocklist[pool->next_page];
121
17.0k
    pool->next_page++;
122
17.0k
  }
123
124
23.3k
  pool->alloc_used = amount;
125
126
23.3k
  return pool->alloc_block;
127
23.3k
}
128
129
void amqp_pool_alloc_bytes(amqp_pool_t *pool, size_t amount,
130
50.9k
                           amqp_bytes_t *output) {
131
50.9k
  output->len = amount;
132
50.9k
  output->bytes = amqp_pool_alloc(pool, amount);
133
50.9k
}
134
135
0
amqp_bytes_t amqp_cstring_bytes(char const *cstr) {
136
0
  amqp_bytes_t result;
137
0
  result.len = strlen(cstr);
138
0
  result.bytes = (void *)cstr;
139
0
  return result;
140
0
}
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
101k
                                             amqp_channel_t channel) {
163
101k
  amqp_pool_table_entry_t *entry;
164
101k
  size_t index = channel % POOL_TABLE_SIZE;
165
166
101k
  entry = state->pool_table[index];
167
168
208k
  for (; NULL != entry; entry = entry->next) {
169
202k
    if (channel == entry->channel) {
170
95.5k
      return &entry->pool;
171
95.5k
    }
172
202k
  }
173
174
6.18k
  entry = malloc(sizeof(amqp_pool_table_entry_t));
175
6.18k
  if (NULL == entry) {
176
0
    return NULL;
177
0
  }
178
179
6.18k
  entry->channel = channel;
180
6.18k
  entry->next = state->pool_table[index];
181
6.18k
  state->pool_table[index] = entry;
182
183
6.18k
  init_amqp_pool(&entry->pool, state->frame_max);
184
185
6.18k
  return &entry->pool;
186
6.18k
}
187
188
amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state,
189
19.7k
                                   amqp_channel_t channel) {
190
19.7k
  amqp_pool_table_entry_t *entry;
191
19.7k
  size_t index = channel % POOL_TABLE_SIZE;
192
193
19.7k
  entry = state->pool_table[index];
194
195
33.9k
  for (; NULL != entry; entry = entry->next) {
196
33.9k
    if (channel == entry->channel) {
197
19.7k
      return &entry->pool;
198
19.7k
    }
199
33.9k
  }
200
201
42
  return NULL;
202
19.7k
}
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
}