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