File Coverage

amqp_mem.c
Criterion Covered Total %
statement 25 117 21.3
branch 2 50 4.0
condition n/a
subroutine n/a
pod n/a
total 27 167 16.1


line stmt bran cond sub pod time code
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
10             #include
11             #include
12             #include
13             #include
14             #include
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 33           void init_amqp_pool(amqp_pool_t *pool, size_t pagesize) {
21 33 50         pool->pagesize = pagesize ? pagesize : 4096;
22              
23 33           pool->pages.num_blocks = 0;
24 33           pool->pages.blocklist = NULL;
25              
26 33           pool->large_blocks.num_blocks = 0;
27 33           pool->large_blocks.blocklist = NULL;
28              
29 33           pool->next_page = 0;
30 33           pool->alloc_block = NULL;
31 33           pool->alloc_used = 0;
32 33           }
33              
34 132           static void empty_blocklist(amqp_pool_blocklist_t *x) {
35             int i;
36              
37 132 50         if (x->blocklist != NULL) {
38 0 0         for (i = 0; i < x->num_blocks; i++) {
39 0           free(x->blocklist[i]);
40             }
41 0           free(x->blocklist);
42             }
43 132           x->num_blocks = 0;
44 132           x->blocklist = NULL;
45 132           }
46              
47 66           void recycle_amqp_pool(amqp_pool_t *pool) {
48 66           empty_blocklist(&pool->large_blocks);
49 66           pool->next_page = 0;
50 66           pool->alloc_block = NULL;
51 66           pool->alloc_used = 0;
52 66           }
53              
54 66           void empty_amqp_pool(amqp_pool_t *pool) {
55 66           recycle_amqp_pool(pool);
56 66           empty_blocklist(&pool->pages);
57 66           }
58              
59             /* Returns 1 on success, 0 on failure */
60 0           static int record_pool_block(amqp_pool_blocklist_t *x, void *block) {
61 0           size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
62              
63 0 0         if (x->blocklist == NULL) {
64 0           x->blocklist = malloc(blocklistlength);
65 0 0         if (x->blocklist == NULL) {
66 0           return 0;
67             }
68             } else {
69 0           void *newbl = realloc(x->blocklist, blocklistlength);
70 0 0         if (newbl == NULL) {
71 0           return 0;
72             }
73 0           x->blocklist = newbl;
74             }
75              
76 0           x->blocklist[x->num_blocks] = block;
77 0           x->num_blocks++;
78 0           return 1;
79             }
80              
81 0           void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
82 0 0         if (amount == 0) {
83 0           return NULL;
84             }
85              
86 0           amount = (amount + 7) & (~7); /* round up to nearest 8-byte boundary */
87              
88 0 0         if (amount > pool->pagesize) {
89 0           void *result = calloc(1, amount);
90 0 0         if (result == NULL) {
91 0           return NULL;
92             }
93 0 0         if (!record_pool_block(&pool->large_blocks, result)) {
94 0           free(result);
95 0           return NULL;
96             }
97 0           return result;
98             }
99              
100 0 0         if (pool->alloc_block != NULL) {
101 0 0         assert(pool->alloc_used <= pool->pagesize);
102              
103 0 0         if (pool->alloc_used + amount <= pool->pagesize) {
104 0           void *result = pool->alloc_block + pool->alloc_used;
105 0           pool->alloc_used += amount;
106 0           return result;
107             }
108             }
109              
110 0 0         if (pool->next_page >= pool->pages.num_blocks) {
111 0           pool->alloc_block = calloc(1, pool->pagesize);
112 0 0         if (pool->alloc_block == NULL) {
113 0           return NULL;
114             }
115 0 0         if (!record_pool_block(&pool->pages, pool->alloc_block)) {
116 0           return NULL;
117             }
118 0           pool->next_page = pool->pages.num_blocks;
119             } else {
120 0           pool->alloc_block = pool->pages.blocklist[pool->next_page];
121 0           pool->next_page++;
122             }
123              
124 0           pool->alloc_used = amount;
125              
126 0           return pool->alloc_block;
127             }
128              
129 0           void amqp_pool_alloc_bytes(amqp_pool_t *pool, size_t amount,
130             amqp_bytes_t *output) {
131 0           output->len = amount;
132 0           output->bytes = amqp_pool_alloc(pool, amount);
133 0           }
134              
135 0           amqp_bytes_t amqp_cstring_bytes(char const *cstr) {
136             amqp_bytes_t result;
137 0           result.len = strlen(cstr);
138 0           result.bytes = (void *)cstr;
139 0           return result;
140             }
141              
142 0           amqp_bytes_t amqp_bytes_malloc_dup(amqp_bytes_t src) {
143             amqp_bytes_t result;
144 0           result.len = src.len;
145 0           result.bytes = malloc(src.len);
146 0 0         if (result.bytes != NULL) {
147 0           memcpy(result.bytes, src.bytes, src.len);
148             }
149 0           return result;
150             }
151              
152 0           amqp_bytes_t amqp_bytes_malloc(size_t amount) {
153             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             }
158              
159 0           void amqp_bytes_free(amqp_bytes_t bytes) { free(bytes.bytes); }
160              
161 0           amqp_pool_t *amqp_get_or_create_channel_pool(amqp_connection_state_t state,
162             amqp_channel_t channel) {
163             amqp_pool_table_entry_t *entry;
164 0           size_t index = channel % POOL_TABLE_SIZE;
165              
166 0           entry = state->pool_table[index];
167              
168 0 0         for (; NULL != entry; entry = entry->next) {
169 0 0         if (channel == entry->channel) {
170 0           return &entry->pool;
171             }
172             }
173              
174 0           entry = malloc(sizeof(amqp_pool_table_entry_t));
175 0 0         if (NULL == entry) {
176 0           return NULL;
177             }
178              
179 0           entry->channel = channel;
180 0           entry->next = state->pool_table[index];
181 0           state->pool_table[index] = entry;
182              
183 0           init_amqp_pool(&entry->pool, state->frame_max);
184              
185 0           return &entry->pool;
186             }
187              
188 0           amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state,
189             amqp_channel_t channel) {
190             amqp_pool_table_entry_t *entry;
191 0           size_t index = channel % POOL_TABLE_SIZE;
192              
193 0           entry = state->pool_table[index];
194              
195 0 0         for (; NULL != entry; entry = entry->next) {
196 0 0         if (channel == entry->channel) {
197 0           return &entry->pool;
198             }
199             }
200              
201 0           return NULL;
202             }
203              
204 0           int amqp_bytes_equal(amqp_bytes_t r, amqp_bytes_t l) {
205 0 0         if (r.len == l.len &&
206 0 0         (r.bytes == l.bytes || 0 == memcmp(r.bytes, l.bytes, r.len))) {
    0          
207 0           return 1;
208             }
209 0           return 0;
210             }