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