third_party/modest/source/mycore/utils/mcobject.c

filter: Statementstmt 69.2% 63 / 91 Branchbran 48.0% 24 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 61.7% 87 / 141 SCAR 0
CC
0
Cov
0%
CRAP 0
« third_party/modest/source/myhtml/stream.c ↑ Summary third_party/modest/source/myencoding/detect.c »
1 /*
2 Copyright (C) 2015-2017 Alexander Borisov
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 Author: lex.borisov@gmail.com (Alexander Borisov)
19 */
20  
21 #include "mycore/utils/mcobject.h"
22  
23 589 mcobject_t * mcobject_create(void)
24 {
25 589 return mycore_calloc(1, sizeof(mcobject_t));
26 }
27  
28 589 mystatus_t mcobject_init(mcobject_t *mcobject, size_t chunk_size, size_t struct_size)
29 {
30 589 mcobject->struct_size = struct_size;
31 589 mcobject->chunk_size = chunk_size;
32
33 589 mcobject->chunk = NULL;
34
35 589 mcobject->cache_length = 0;
36 589 mcobject->cache_size = chunk_size;
37 589 mcobject->cache = (void**)mycore_malloc(sizeof(void*) * mcobject->cache_size);
38
39 589 ▶ if(mcobject->cache == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->cache == NULL) 0 589 589
40 0 return MyCORE_STATUS_MCOBJECT_ERROR_CACHE_CREATE;
41
42 589 return MyCORE_STATUS_OK;
43 }
44  
45 862 void mcobject_clean(mcobject_t *mcobject)
46 {
47 862 ▶ if(mcobject->chunk == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->chunk == NULL) 371 491 862
48 371 return;
49
50 491 mcobject_chunk_t* chunk = mcobject->chunk;
51
52 491 ▶ while(chunk->next)
Branch: 1 branch
BranchTrueFalseTotal
while(chunk->next) 0 491 491
53 0 chunk = chunk->next;
54
55 982 ▶ while(chunk) {
Branch: 1 branch
BranchTrueFalseTotal
while(chunk) { 491 491 982
56 491 mcobject_chunk_t* tmp = chunk->prev;
57
58 491 ▶ if(chunk->begin) {
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->begin) { 491 0 491
59 491 mycore_free(chunk->begin);
60 }
61
62 491 mycore_free(chunk);
63
64 491 chunk = tmp;
65 }
66
67 491 mcobject->chunk = NULL;
68 491 mcobject->cache_length = 0;
69 }
70  
71 588 mcobject_t * mcobject_destroy(mcobject_t *mcobject, bool destroy_self)
72 {
73 588 ▶ if(mcobject == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject == NULL) 0 588 588
74 0 return NULL;
75
76 588 mcobject_clean(mcobject);
77
78 588 ▶ if(mcobject->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->cache) { 588 0 588
79 588 mycore_free(mcobject->cache);
80 588 mcobject->cache = NULL;
81 }
82
83 588 ▶ if(destroy_self) {
Branch: 1 branch
BranchTrueFalseTotal
if(destroy_self) { 588 0 588
84 588 mycore_free(mcobject);
85 588 return NULL;
86 }
87
88 0 return mcobject;
89 }
90  
91 492 void mcobject_chunk_malloc(mcobject_t* mcobject, mystatus_t* status)
92 {
93 492 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 492 0 492
94 492 *status = MyCORE_STATUS_OK;
95
96 mcobject_chunk_t* chunk;
97
98 492 ▶ if(mcobject->chunk && mcobject->chunk->next) {
Branch: 2 branches
BranchTrueFalseTotal
if(mcobject->chunk && mcobject->chunk->next) { 0 492 492
if(mcobject->chunk && mcobject->chunk->next) { 0 0 0
99 0 mcobject->chunk = mcobject->chunk->next;
100
101 0 mcobject->chunk->length = 0;
102
103 0 return;
104 }
105 else {
106 492 chunk = mycore_calloc(1, sizeof(mcobject_chunk_t));
107
108 492 ▶ if(chunk == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(chunk == NULL) { 0 492 492
109 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
110 0 *status = MyCORE_STATUS_MCOBJECT_ERROR_CHUNK_CREATE;
111
112 0 return;
113 }
114
115 492 chunk->begin = mycore_malloc(mcobject->struct_size * mcobject->chunk_size);
116
117 492 ▶ if(chunk->begin == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->begin == NULL) { 0 492 492
118 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
119 0 *status = MyCORE_STATUS_MCOBJECT_ERROR_CHUNK_INIT;
120
121 0 mycore_free(chunk);
122 0 return;
123 }
124
125 492 chunk->size = mcobject->chunk_size;
126 }
127
128 492 ▶ if(mcobject->chunk == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->chunk == NULL) { 492 0 492
129 492 mcobject->chunk = chunk;
130 492 return;
131 }
132
133 0 chunk->prev = mcobject->chunk;
134 0 mcobject->chunk->next = chunk;
135
136 0 mcobject->chunk = chunk;
137 }
138  
139 719 void * mcobject_malloc(mcobject_t *mcobject, mystatus_t* status)
140 {
141 719 ▶ if(mcobject->cache_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->cache_length) { 165 554 719
142 165 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 165 165
143 0 *status = MyCORE_STATUS_OK;
144
145 165 mcobject->cache_length--;
146 165 return mcobject->cache[ mcobject->cache_length ];
147 }
148
149 554 mcobject_chunk_t* chunk = mcobject->chunk;
150
151 554 ▶ if(chunk == NULL || chunk->length >= chunk->size)
Branch: 2 branches
BranchTrueFalseTotal
if(chunk == NULL || chunk->length >= chunk->size) 62 492 554
if(chunk == NULL || chunk->length >= chunk->size) 0 62 62
152 {
153 mystatus_t ns_status;
154 492 mcobject_chunk_malloc(mcobject, &ns_status);
155
156 492 ▶ if(ns_status) {
Branch: 1 branch
BranchTrueFalseTotal
if(ns_status) { 0 492 492
157 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
158 0 *status = ns_status;
159
160 0 return NULL;
161 }
162
163 492 chunk = mcobject->chunk;
164 }
165
166 554 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 554 554
167 0 *status = MyCORE_STATUS_OK;
168
169 554 chunk->length++;
170 554 return &chunk->begin[((chunk->length - 1) * mcobject->struct_size)];
171 }
172  
173 442 mystatus_t mcobject_free(mcobject_t *mcobject, void *entry)
174 {
175 442 ▶ if(mcobject->cache_length >= mcobject->cache_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(mcobject->cache_length >= mcobject->cache_size) { 0 442 442
176 0 size_t new_size = mcobject->cache_size << 1;
177
178 0 void **tmp = (void**)mycore_realloc(mcobject->cache, sizeof(void*) * new_size);
179
180 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
181 0 mcobject->cache = tmp;
182 0 mcobject->cache_size = new_size;
183 }
184 else
185 0 return MyCORE_STATUS_MCOBJECT_ERROR_CACHE_REALLOC;
186 }
187
188 442 mcobject->cache[ mcobject->cache_length ] = entry;
189 442 mcobject->cache_length++;
190
191 442 return MyCORE_STATUS_OK;
192 }
193  
194  
« third_party/modest/source/myhtml/stream.c ↑ Summary third_party/modest/source/myencoding/detect.c »