File Coverage

amqp_mem.c
Criterion Covered Total %
statement 89 117 76.0
branch 29 50 58.0
condition n/a
subroutine n/a
pod n/a
total 118 167 70.6


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