File Coverage

amqp_mem.c
Criterion Covered Total %
statement 92 117 78.6
branch 30 50 60.0
condition n/a
subroutine n/a
pod n/a
total 122 167 73.0


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 125           void init_amqp_pool(amqp_pool_t *pool, size_t pagesize) {
53 125 50         pool->pagesize = pagesize ? pagesize : 4096;
54              
55 125           pool->pages.num_blocks = 0;
56 125           pool->pages.blocklist = NULL;
57              
58 125           pool->large_blocks.num_blocks = 0;
59 125           pool->large_blocks.blocklist = NULL;
60              
61 125           pool->next_page = 0;
62 125           pool->alloc_block = NULL;
63 125           pool->alloc_used = 0;
64 125           }
65              
66 966           static void empty_blocklist(amqp_pool_blocklist_t *x) {
67             int i;
68              
69 966 100         if (x->blocklist != NULL) {
70 369 100         for (i = 0; i < x->num_blocks; i++) {
71 232           free(x->blocklist[i]);
72             }
73 137           free(x->blocklist);
74             }
75 966           x->num_blocks = 0;
76 966           x->blocklist = NULL;
77 966           }
78              
79 813           void recycle_amqp_pool(amqp_pool_t *pool) {
80 813           empty_blocklist(&pool->large_blocks);
81 813           pool->next_page = 0;
82 813           pool->alloc_block = NULL;
83 813           pool->alloc_used = 0;
84 813           }
85              
86 153           void empty_amqp_pool(amqp_pool_t *pool) {
87 153           recycle_amqp_pool(pool);
88 153           empty_blocklist(&pool->pages);
89 153           }
90              
91             /* Returns 1 on success, 0 on failure */
92 237           static int record_pool_block(amqp_pool_blocklist_t *x, void *block) {
93 237           size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
94              
95 237 100         if (x->blocklist == NULL) {
96 140           x->blocklist = malloc(blocklistlength);
97 140 50         if (x->blocklist == NULL) {
98 0           return 0;
99             }
100             } else {
101 97           void *newbl = realloc(x->blocklist, blocklistlength);
102 97 50         if (newbl == NULL) {
103 0           return 0;
104             }
105 97           x->blocklist = newbl;
106             }
107              
108 237           x->blocklist[x->num_blocks] = block;
109 237           x->num_blocks++;
110 237           return 1;
111             }
112              
113 2619           void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
114 2619 50         if (amount == 0) {
115 0           return NULL;
116             }
117              
118 2619           amount = (amount + 7) & (~7); /* round up to nearest 8-byte boundary */
119              
120 2619 100         if (amount > pool->pagesize) {
121 28           void *result = calloc(1, amount);
122 28 50         if (result == NULL) {
123 0           return NULL;
124             }
125 28 50         if (!record_pool_block(&pool->large_blocks, result)) {
126 0           free(result);
127 0           return NULL;
128             }
129 28           return result;
130             }
131              
132 2591 100         if (pool->alloc_block != NULL) {
133 2238 50         assert(pool->alloc_used <= pool->pagesize);
134              
135 2238 100         if (pool->alloc_used + amount <= pool->pagesize) {
136 2151           void *result = pool->alloc_block + pool->alloc_used;
137 2151           pool->alloc_used += amount;
138 2151           return result;
139             }
140             }
141              
142 440 100         if (pool->next_page >= pool->pages.num_blocks) {
143 209           pool->alloc_block = calloc(1, pool->pagesize);
144 209 50         if (pool->alloc_block == NULL) {
145 0           return NULL;
146             }
147 209 50         if (!record_pool_block(&pool->pages, pool->alloc_block)) {
148 0           return NULL;
149             }
150 209           pool->next_page = pool->pages.num_blocks;
151             } else {
152 231           pool->alloc_block = pool->pages.blocklist[pool->next_page];
153 231           pool->next_page++;
154             }
155              
156 440           pool->alloc_used = amount;
157              
158 440           return pool->alloc_block;
159             }
160              
161 1845           void amqp_pool_alloc_bytes(amqp_pool_t *pool, size_t amount,
162             amqp_bytes_t *output) {
163 1845           output->len = amount;
164 1845           output->bytes = amqp_pool_alloc(pool, amount);
165 1845           }
166              
167 1155           amqp_bytes_t amqp_cstring_bytes(char const *cstr) {
168             amqp_bytes_t result;
169 1155           result.len = strlen(cstr);
170 1155           result.bytes = (void *)cstr;
171 1155           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 1006           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 1006           size_t index = channel % POOL_TABLE_SIZE;
197              
198 1006           entry = state->pool_table[index];
199              
200 1006 100         for (; NULL != entry; entry = entry->next) {
201 951 50         if (channel == entry->channel) {
202 951           return &entry->pool;
203             }
204             }
205              
206 55           entry = malloc(sizeof(amqp_pool_table_entry_t));
207 55 50         if (NULL == entry) {
208 0           return NULL;
209             }
210              
211 55           entry->channel = channel;
212 55           entry->next = state->pool_table[index];
213 55           state->pool_table[index] = entry;
214              
215 55           init_amqp_pool(&entry->pool, state->frame_max);
216              
217 55           return &entry->pool;
218             }
219              
220 479           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 479           size_t index = channel % POOL_TABLE_SIZE;
224              
225 479           entry = state->pool_table[index];
226              
227 479 50         for (; NULL != entry; entry = entry->next) {
228 479 50         if (channel == entry->channel) {
229 479           return &entry->pool;
230             }
231             }
232              
233 0           return NULL;
234             }
235              
236 6           int amqp_bytes_equal(amqp_bytes_t r, amqp_bytes_t l) {
237 6 50         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 6           return 0;
242             }