]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stdint.h> | |
22 | #include <string.h> | |
23 | #include <glib.h> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
a1bb33af | 26 | |
393fb9cb UH |
27 | /** |
28 | * @file | |
29 | * | |
30 | * Creating, using, or destroying libsigrok datastores. | |
31 | */ | |
32 | ||
7b870c38 UH |
33 | /** |
34 | * @defgroup grp_datastore Datastore | |
35 | * | |
36 | * Creating, using, or destroying libsigrok datastores. | |
37 | * | |
38 | * @{ | |
39 | */ | |
40 | ||
c4911129 | 41 | static gpointer new_chunk(struct sr_datastore **ds); |
a1bb33af | 42 | |
15278f3e UH |
43 | /** |
44 | * Create a new datastore with the specified unit size. | |
45 | * | |
46 | * The unit size is fixed once the datastore is created, and cannot be | |
47 | * changed later on, neither can data be added to the datastore with | |
48 | * different unit sizes later. | |
49 | * | |
50 | * It is the caller's responsibility to free the allocated memory of the | |
51 | * datastore via the sr_datastore_destroy() function, if no longer needed. | |
52 | * | |
6b2d8d3e UH |
53 | * @todo Unitsize should probably be unsigned int or uint32_t or similar. |
54 | * @todo This function should have a 'chunksize' parameter, and | |
15278f3e UH |
55 | * struct sr_datastore a 'chunksize' field. |
56 | * | |
57 | * @param unitsize The unit size (>= 1) to be used for this datastore. | |
58 | * @param ds Pointer to a variable which will hold the newly created | |
59 | * datastore structure. | |
60 | * | |
61 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
62 | * or SR_ERR_ARG upon invalid arguments. If something other than SR_OK | |
63 | * is returned, the value of 'ds' is undefined. | |
64 | */ | |
1a081ca6 | 65 | SR_API int sr_datastore_new(int unitsize, struct sr_datastore **ds) |
a1bb33af | 66 | { |
15278f3e UH |
67 | if (!ds) { |
68 | sr_err("ds: %s: ds was NULL", __func__); | |
69 | return SR_ERR_ARG; | |
70 | } | |
a1bb33af | 71 | |
15278f3e UH |
72 | if (unitsize <= 0) { |
73 | sr_err("ds: %s: unitsize was %d, but it must be >= 1", | |
74 | __func__, unitsize); | |
75 | return SR_ERR_ARG; | |
76 | } | |
33247d6a | 77 | |
b53738ba UH |
78 | if (!(*ds = g_try_malloc(sizeof(struct sr_datastore)))) { |
79 | sr_err("ds: %s: ds malloc failed", __func__); | |
e46b8fb1 | 80 | return SR_ERR_MALLOC; |
b53738ba | 81 | } |
33247d6a | 82 | |
2aebf78d UH |
83 | (*ds)->ds_unitsize = unitsize; |
84 | (*ds)->num_units = 0; | |
85 | (*ds)->chunklist = NULL; | |
a1bb33af | 86 | |
e46b8fb1 | 87 | return SR_OK; |
a1bb33af UH |
88 | } |
89 | ||
15278f3e UH |
90 | /** |
91 | * Destroy the specified datastore and free the memory used by it. | |
92 | * | |
93 | * This will free the memory used by the data in the datastore's 'chunklist', | |
94 | * by the chunklist data structure itself, and by the datastore struct. | |
95 | * | |
96 | * @param ds The datastore to destroy. | |
97 | * | |
98 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
99 | */ | |
1a081ca6 | 100 | SR_API int sr_datastore_destroy(struct sr_datastore *ds) |
a1bb33af UH |
101 | { |
102 | GSList *chunk; | |
103 | ||
15278f3e UH |
104 | if (!ds) { |
105 | sr_err("ds: %s: ds was NULL", __func__); | |
106 | return SR_ERR_ARG; | |
107 | } | |
484760d1 | 108 | |
1b452b85 | 109 | for (chunk = ds->chunklist; chunk; chunk = chunk->next) |
a1bb33af UH |
110 | g_free(chunk->data); |
111 | g_slist_free(ds->chunklist); | |
112 | g_free(ds); | |
0abee507 | 113 | ds = NULL; |
15278f3e | 114 | |
e46b8fb1 | 115 | return SR_OK; |
a1bb33af UH |
116 | } |
117 | ||
15278f3e UH |
118 | /** |
119 | * Append some data to the specified datastore. | |
120 | * | |
6b2d8d3e UH |
121 | * @todo More elaborate function description. |
122 | * @todo This function should use the (not yet available) 'chunksize' field | |
15278f3e | 123 | * of struct sr_datastore (instead of hardcoding DATASTORE_CHUNKSIZE). |
6b2d8d3e UH |
124 | * @todo in_unitsize and probelist are unused? |
125 | * @todo A few of the parameters can be const. | |
126 | * @todo Ideally, 'ds' should be unmodified upon errors. | |
127 | * @todo Should 0 be allowed as length? | |
128 | * @todo Specify/document the data format of the 'data' parameter. | |
15278f3e UH |
129 | * |
130 | * @param ds Pointer to the datastore which shall receive the data. | |
131 | * Must not be NULL. | |
132 | * @param data Pointer to the memory buffer containing the data to add. | |
6b2d8d3e | 133 | * Must not be NULL. |
15278f3e | 134 | * @param length Length of the data to add (in number of bytes). |
15278f3e UH |
135 | * @param in_unitsize The unit size (>= 1) of the input data. |
136 | * @param probelist Pointer to a list of integers (probe numbers). The probe | |
137 | * numbers in this list are 1-based, i.e. the first probe | |
138 | * is expected to be numbered 1 (not 0!). Must not be NULL. | |
139 | * | |
140 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
141 | * or SR_ERR_ARG upon invalid arguments. If something other than SR_OK | |
142 | * is returned, the value/state of 'ds' is undefined. | |
143 | */ | |
1a081ca6 | 144 | SR_API int sr_datastore_put(struct sr_datastore *ds, void *data, |
8ec95d22 | 145 | unsigned int length, int in_unitsize, const int *probelist) |
a1bb33af | 146 | { |
afc8e4de UH |
147 | unsigned int stored; |
148 | int capacity, size, num_chunks, chunk_bytes_free, chunk_offset; | |
a1bb33af UH |
149 | gpointer chunk; |
150 | ||
15278f3e UH |
151 | if (!ds) { |
152 | sr_err("ds: %s: ds was NULL", __func__); | |
153 | return SR_ERR_ARG; | |
154 | } | |
155 | ||
156 | /* Unitsize must not be 0, we'll divide by 0 otherwise. */ | |
157 | if (ds->ds_unitsize == 0) { | |
158 | sr_err("ds: %s: ds->ds_unitsize was 0", __func__); | |
159 | return SR_ERR_ARG; | |
160 | } | |
161 | ||
162 | if (!data) { | |
163 | sr_err("ds: %s: data was NULL", __func__); | |
164 | return SR_ERR_ARG; | |
165 | } | |
166 | ||
167 | if (in_unitsize < 1) { | |
168 | sr_err("ds: %s: in_unitsize was %d, but it must be >= 1", | |
169 | __func__, in_unitsize); | |
170 | return SR_ERR_ARG; | |
171 | } | |
172 | ||
173 | if (!probelist) { | |
174 | sr_err("ds: %s: probelist was NULL", __func__); | |
175 | return SR_ERR_ARG; | |
176 | } | |
177 | ||
178 | /* Get the last chunk in the list, or create a new one if needed. */ | |
7b8ca864 UH |
179 | if (ds->chunklist == NULL) { |
180 | if (!(chunk = new_chunk(&ds))) { | |
181 | sr_err("ds: %s: couldn't allocate new chunk", __func__); | |
182 | return SR_ERR_MALLOC; | |
183 | } | |
184 | } else { | |
a1bb33af | 185 | chunk = g_slist_last(ds->chunklist)->data; |
7b8ca864 | 186 | } |
1b452b85 | 187 | |
15278f3e | 188 | /* Get/calculate number of chunks, free space, etc. */ |
a1bb33af UH |
189 | num_chunks = g_slist_length(ds->chunklist); |
190 | capacity = (num_chunks * DATASTORE_CHUNKSIZE); | |
191 | chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units); | |
1b452b85 UH |
192 | chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) |
193 | - chunk_bytes_free; | |
15278f3e | 194 | |
a1bb33af | 195 | stored = 0; |
1b452b85 | 196 | while (stored < length) { |
15278f3e | 197 | /* No more free space left, allocate a new chunk. */ |
1b452b85 | 198 | if (chunk_bytes_free == 0) { |
7b8ca864 UH |
199 | if (!(chunk = new_chunk(&ds))) { |
200 | sr_err("ds: %s: couldn't allocate new chunk", | |
201 | __func__); | |
202 | return SR_ERR_MALLOC; | |
203 | } | |
a1bb33af UH |
204 | chunk_bytes_free = DATASTORE_CHUNKSIZE; |
205 | chunk_offset = 0; | |
206 | } | |
207 | ||
1b452b85 | 208 | if (length - stored > (unsigned int)chunk_bytes_free) |
a1bb33af UH |
209 | size = chunk_bytes_free; |
210 | else | |
1b452b85 | 211 | /* Last part, won't fill up this chunk. */ |
a1bb33af | 212 | size = length - stored; |
1b452b85 | 213 | |
a1bb33af UH |
214 | memcpy(chunk + chunk_offset, data + stored, size); |
215 | chunk_bytes_free -= size; | |
216 | stored += size; | |
217 | } | |
15278f3e | 218 | |
a1bb33af | 219 | ds->num_units += stored / ds->ds_unitsize; |
15278f3e UH |
220 | |
221 | return SR_OK; | |
a1bb33af UH |
222 | } |
223 | ||
15278f3e UH |
224 | /** |
225 | * Allocate a new memory chunk, append it to the datastore's chunklist. | |
226 | * | |
227 | * The newly allocated chunk is added to the datastore's chunklist by this | |
228 | * function, and the return value additionally points to the new chunk. | |
229 | * | |
230 | * The allocated memory is guaranteed to be cleared. | |
231 | * | |
6b2d8d3e | 232 | * @todo This function should use the datastore's 'chunksize' field instead |
15278f3e | 233 | * of hardcoding DATASTORE_CHUNKSIZE. |
6b2d8d3e | 234 | * @todo Return int, so we can return SR_OK / SR_ERR_ARG / SR_ERR_MALLOC? |
15278f3e UH |
235 | * |
236 | * @param ds Pointer to a variable which holds the datastore structure. | |
237 | * Must not be NULL. The contents of 'ds' are modified in-place. | |
238 | * | |
239 | * @return Pointer to the newly allocated chunk, or NULL upon failure. | |
240 | */ | |
c4911129 | 241 | static gpointer new_chunk(struct sr_datastore **ds) |
a1bb33af UH |
242 | { |
243 | gpointer chunk; | |
244 | ||
7b8ca864 | 245 | /* Note: Caller checked that ds != NULL. */ |
15278f3e UH |
246 | |
247 | chunk = g_try_malloc0(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize); | |
248 | if (!chunk) { | |
c13536fa UH |
249 | sr_err("ds: %s: chunk malloc failed (ds_unitsize was %u)", |
250 | __func__, (*ds)->ds_unitsize); | |
15278f3e UH |
251 | return NULL; /* TODO: SR_ERR_MALLOC later? */ |
252 | } | |
33247d6a | 253 | |
a1bb33af UH |
254 | (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk); |
255 | ||
15278f3e | 256 | return chunk; /* TODO: SR_OK later? */ |
a1bb33af | 257 | } |
7b870c38 UH |
258 | |
259 | /** @} */ |