amqp_mem.c

filter: Statementstmt 70.8% 85 / 120 Branchbran 52.0% 26 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.2% 111 / 170 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_connection.c ↑ Summary amqp_tcp_socket.c »
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MIT
5 *
6 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
7 * Alan Antonuk. All Rights Reserved.
8 *
9 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
10 * All Rights Reserved.
11 *
12 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
13 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
14 *
15 * Permission is hereby granted, free of charge, to any person
16 * obtaining a copy of this software and associated documentation
17 * files (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy,
19 * modify, merge, publish, distribute, sublicense, and/or sell copies
20 * of the Software, and to permit persons to whom the Software is
21 * furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 * ***** END LICENSE BLOCK *****
35 */
36  
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40  
41 #include "amqp_private.h"
42 #include <assert.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/types.h>
48  
49 0 char const *amqp_version(void)
50 {
51 0 return AMQP_VERSION_STRING;
52 }
53  
54 0 uint32_t amqp_version_number(void)
55 {
56 0 return AMQP_VERSION;
57 }
58  
59 78 void init_amqp_pool(amqp_pool_t *pool, size_t pagesize)
60 {
61 78 ▶ pool->pagesize = pagesize ? pagesize : 4096;
Branch: 1 branch
BranchTrueFalseTotal
pool->pagesize = pagesize ? pagesize : 4096; 78 0 78
62  
63 78 pool->pages.num_blocks = 0;
64 78 pool->pages.blocklist = NULL;
65  
66 78 pool->large_blocks.num_blocks = 0;
67 78 pool->large_blocks.blocklist = NULL;
68  
69 78 pool->next_page = 0;
70 78 pool->alloc_block = NULL;
71 78 pool->alloc_used = 0;
72 78 }
73  
74 238 static void empty_blocklist(amqp_pool_blocklist_t *x)
75 {
76 int i;
77  
78 373 ▶ for (i = 0; i < x->num_blocks; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->num_blocks; i++) { 135 238 373
79 135 free(x->blocklist[i]);
80 }
81 238 ▶ if (x->blocklist != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->blocklist != NULL) { 74 164 238
82 74 free(x->blocklist);
83 }
84 238 x->num_blocks = 0;
85 238 x->blocklist = NULL;
86 238 }
87  
88 133 void recycle_amqp_pool(amqp_pool_t *pool)
89 {
90 133 empty_blocklist(&pool->large_blocks);
91 133 pool->next_page = 0;
92 133 pool->alloc_block = NULL;
93 133 pool->alloc_used = 0;
94 133 }
95  
96 105 void empty_amqp_pool(amqp_pool_t *pool)
97 {
98 105 recycle_amqp_pool(pool);
99 105 empty_blocklist(&pool->pages);
100 105 }
101  
102 /* Returns 1 on success, 0 on failure */
103 135 static int record_pool_block(amqp_pool_blocklist_t *x, void *block)
104 {
105 135 size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
106  
107 135 ▶ if (x->blocklist == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->blocklist == NULL) { 74 61 135
108 74 x->blocklist = malloc(blocklistlength);
109 74 ▶ if (x->blocklist == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->blocklist == NULL) { 0 74 74
110 0 return 0;
111 }
112 } else {
113 61 void *newbl = realloc(x->blocklist, blocklistlength);
114 61 ▶ if (newbl == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (newbl == NULL) { 0 61 61
115 0 return 0;
116 }
117 61 x->blocklist = newbl;
118 }
119  
120 135 x->blocklist[x->num_blocks] = block;
121 135 x->num_blocks++;
122 135 return 1;
123 }
124  
125 1288 void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount)
126 {
127 1288 ▶ if (amount == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (amount == 0) { 0 1288 1288
128 0 return NULL;
129 }
130  
131 1288 amount = (amount + 7) & (~7); /* round up to nearest 8-byte boundary */
132  
133 1288 ▶ if (amount > pool->pagesize) {
Branch: 1 branch
BranchTrueFalseTotal
if (amount > pool->pagesize) { 0 1288 1288
134 0 void *result = calloc(1, amount);
135 0 ▶ if (result == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result == NULL) { 0 0 0
136 0 return NULL;
137 }
138 0 ▶ if (!record_pool_block(&pool->large_blocks, result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!record_pool_block(&pool->large_blocks, result)) { 0 0 0
139 0 return NULL;
140 }
141 0 return result;
142 }
143  
144 1288 ▶ if (pool->alloc_block != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool->alloc_block != NULL) { 1186 102 1288
145 1186 ▶ assert(pool->alloc_used <= pool->pagesize);
Branch: 1 branch
BranchTrueFalseTotal
assert(pool->alloc_used <= pool->pagesize); 0 1186 1186
146  
147 1186 ▶ if (pool->alloc_used + amount <= pool->pagesize) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool->alloc_used + amount <= pool->pagesize) { 1125 61 1186
148 1125 void *result = pool->alloc_block + pool->alloc_used;
149 1125 pool->alloc_used += amount;
150 1125 return result;
151 }
152 }
153  
154 163 ▶ if (pool->next_page >= pool->pages.num_blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool->next_page >= pool->pages.num_blocks) { 135 28 163
155 135 pool->alloc_block = calloc(1, pool->pagesize);
156 135 ▶ if (pool->alloc_block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool->alloc_block == NULL) { 0 135 135
157 0 return NULL;
158 }
159 135 ▶ if (!record_pool_block(&pool->pages, pool->alloc_block)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!record_pool_block(&pool->pages, pool->alloc_block)) { 0 135 135
160 0 return NULL;
161 }
162 135 pool->next_page = pool->pages.num_blocks;
163 } else {
164 28 pool->alloc_block = pool->pages.blocklist[pool->next_page];
165 28 pool->next_page++;
166 }
167  
168 163 pool->alloc_used = amount;
169  
170 163 return pool->alloc_block;
171 }
172  
173 1036 void amqp_pool_alloc_bytes(amqp_pool_t *pool, size_t amount, amqp_bytes_t *output)
174 {
175 1036 output->len = amount;
176 1036 output->bytes = amqp_pool_alloc(pool, amount);
177 1036 }
178  
179 364 amqp_bytes_t amqp_cstring_bytes(char const *cstr)
180 {
181 amqp_bytes_t result;
182 364 result.len = strlen(cstr);
183 364 result.bytes = (void *) cstr;
184 364 return result;
185 }
186  
187 0 amqp_bytes_t amqp_bytes_malloc_dup(amqp_bytes_t src)
188 {
189 amqp_bytes_t result;
190 0 result.len = src.len;
191 0 result.bytes = malloc(src.len);
192 0 ▶ if (result.bytes != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.bytes != NULL) { 0 0 0
193 0 memcpy(result.bytes, src.bytes, src.len);
194 }
195 0 return result;
196 }
197  
198 0 amqp_bytes_t amqp_bytes_malloc(size_t amount)
199 {
200 amqp_bytes_t result;
201 0 result.len = amount;
202 0 result.bytes = malloc(amount); /* will return NULL if it fails */
203 0 return result;
204 }
205  
206 0 void amqp_bytes_free(amqp_bytes_t bytes)
207 {
208 0 free(bytes.bytes);
209 0 }
210  
211 140 amqp_pool_t *amqp_get_or_create_channel_pool(amqp_connection_state_t state, amqp_channel_t channel)
212 {
213 amqp_pool_table_entry_t *entry;
214 140 size_t index = channel % POOL_TABLE_SIZE;
215  
216 140 entry = state->pool_table[index];
217  
218 140 ▶ for ( ; NULL != entry; entry = entry->next) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; NULL != entry; entry = entry->next) { 117 23 140
219 117 ▶ if (channel == entry->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel == entry->channel) { 117 0 117
220 117 return &entry->pool;
221 }
222 }
223  
224 23 entry = malloc(sizeof(amqp_pool_table_entry_t));
225 23 ▶ if (NULL == entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == entry) { 0 23 23
226 0 return NULL;
227 }
228  
229 23 entry->channel = channel;
230 23 entry->next = state->pool_table[index];
231 23 state->pool_table[index] = entry;
232  
233 23 init_amqp_pool(&entry->pool, state->frame_max);
234  
235 23 return &entry->pool;
236 }
237  
238 28 amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state, amqp_channel_t channel)
239 {
240 amqp_pool_table_entry_t *entry;
241 28 size_t index = channel % POOL_TABLE_SIZE;
242  
243 28 entry = state->pool_table[index];
244  
245 28 ▶ for ( ; NULL != entry; entry = entry->next) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; NULL != entry; entry = entry->next) { 28 0 28
246 28 ▶ if (channel == entry->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel == entry->channel) { 28 0 28
247 28 return &entry->pool;
248 }
249 }
250  
251 0 return NULL;
252 }
253  
254 0 int amqp_bytes_equal(amqp_bytes_t r, amqp_bytes_t l) {
255 0 ▶ if (r.len == l.len &&
Branch: 1 branch
BranchTrueFalseTotal
if (r.len == l.len && 0 0 0
256 0 ▶ (r.bytes == l.bytes || 0 == memcmp(r.bytes, l.bytes, r.len))) {
Branch: 2 branches
BranchTrueFalseTotal
(r.bytes == l.bytes || 0 == memcmp(r.bytes, l.bytes, r.len))) { 0 0 0
(r.bytes == l.bytes || 0 == memcmp(r.bytes, l.bytes, r.len))) { 0 0 0
257 0 return 1;
258 }
259 0 return 0;
260 }
« amqp_connection.c ↑ Summary amqp_tcp_socket.c »