]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
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> | |
24 | #include "sigrok.h" | |
25 | #include "sigrok-internal.h" | |
26 | ||
27 | static gpointer new_chunk(struct sr_datastore **ds); | |
28 | ||
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 | */ | |
51 | SR_API int sr_datastore_new(int unitsize, struct sr_datastore **ds) | |
52 | { | |
53 | if (!ds) { | |
54 | sr_err("ds: %s: ds was NULL", __func__); | |
55 | return SR_ERR_ARG; | |
56 | } | |
57 | ||
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 | } | |
63 | ||
64 | if (!(*ds = g_try_malloc(sizeof(struct sr_datastore)))) { | |
65 | sr_err("ds: %s: ds malloc failed", __func__); | |
66 | return SR_ERR_MALLOC; | |
67 | } | |
68 | ||
69 | (*ds)->ds_unitsize = unitsize; | |
70 | (*ds)->num_units = 0; | |
71 | (*ds)->chunklist = NULL; | |
72 | ||
73 | return SR_OK; | |
74 | } | |
75 | ||
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 | */ | |
86 | SR_API int sr_datastore_destroy(struct sr_datastore *ds) | |
87 | { | |
88 | GSList *chunk; | |
89 | ||
90 | if (!ds) { | |
91 | sr_err("ds: %s: ds was NULL", __func__); | |
92 | return SR_ERR_ARG; | |
93 | } | |
94 | ||
95 | for (chunk = ds->chunklist; chunk; chunk = chunk->next) | |
96 | g_free(chunk->data); | |
97 | g_slist_free(ds->chunklist); | |
98 | g_free(ds); | |
99 | ||
100 | /* TODO: Set ds = NULL? */ | |
101 | ||
102 | return SR_OK; | |
103 | } | |
104 | ||
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. | |
114 | * TODO: Handle new_chunk() returning NULL. | |
115 | * TODO: Ideally, 'ds' should be unmodified upon errors. | |
116 | * | |
117 | * @param ds Pointer to the datastore which shall receive the data. | |
118 | * Must not be NULL. | |
119 | * @param data Pointer to the memory buffer containing the data to add. | |
120 | * Must not be NULL. TODO: Data format? | |
121 | * @param length Length of the data to add (in number of bytes). | |
122 | * TODO: Should 0 be allowed as length? | |
123 | * @param in_unitsize The unit size (>= 1) of the input data. | |
124 | * @param probelist Pointer to a list of integers (probe numbers). The probe | |
125 | * numbers in this list are 1-based, i.e. the first probe | |
126 | * is expected to be numbered 1 (not 0!). Must not be NULL. | |
127 | * | |
128 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
129 | * or SR_ERR_ARG upon invalid arguments. If something other than SR_OK | |
130 | * is returned, the value/state of 'ds' is undefined. | |
131 | */ | |
132 | SR_API int sr_datastore_put(struct sr_datastore *ds, void *data, | |
133 | unsigned int length, int in_unitsize, int *probelist) | |
134 | { | |
135 | unsigned int stored; | |
136 | int capacity, size, num_chunks, chunk_bytes_free, chunk_offset; | |
137 | gpointer chunk; | |
138 | ||
139 | /* Avoid compiler warnings. */ | |
140 | (void)in_unitsize; | |
141 | (void)probelist; | |
142 | ||
143 | if (!ds) { | |
144 | sr_err("ds: %s: ds was NULL", __func__); | |
145 | return SR_ERR_ARG; | |
146 | } | |
147 | ||
148 | /* Unitsize must not be 0, we'll divide by 0 otherwise. */ | |
149 | if (ds->ds_unitsize == 0) { | |
150 | sr_err("ds: %s: ds->ds_unitsize was 0", __func__); | |
151 | return SR_ERR_ARG; | |
152 | } | |
153 | ||
154 | if (!data) { | |
155 | sr_err("ds: %s: data was NULL", __func__); | |
156 | return SR_ERR_ARG; | |
157 | } | |
158 | ||
159 | if (in_unitsize < 1) { | |
160 | sr_err("ds: %s: in_unitsize was %d, but it must be >= 1", | |
161 | __func__, in_unitsize); | |
162 | return SR_ERR_ARG; | |
163 | } | |
164 | ||
165 | if (!probelist) { | |
166 | sr_err("ds: %s: probelist was NULL", __func__); | |
167 | return SR_ERR_ARG; | |
168 | } | |
169 | ||
170 | /* Get the last chunk in the list, or create a new one if needed. */ | |
171 | if (ds->chunklist == NULL) | |
172 | chunk = new_chunk(&ds); | |
173 | else | |
174 | chunk = g_slist_last(ds->chunklist)->data; | |
175 | ||
176 | /* Get/calculate number of chunks, free space, etc. */ | |
177 | num_chunks = g_slist_length(ds->chunklist); | |
178 | capacity = (num_chunks * DATASTORE_CHUNKSIZE); | |
179 | chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units); | |
180 | chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) | |
181 | - chunk_bytes_free; | |
182 | ||
183 | stored = 0; | |
184 | while (stored < length) { | |
185 | /* No more free space left, allocate a new chunk. */ | |
186 | if (chunk_bytes_free == 0) { | |
187 | chunk = new_chunk(&ds); | |
188 | chunk_bytes_free = DATASTORE_CHUNKSIZE; | |
189 | chunk_offset = 0; | |
190 | } | |
191 | ||
192 | if (length - stored > (unsigned int)chunk_bytes_free) | |
193 | size = chunk_bytes_free; | |
194 | else | |
195 | /* Last part, won't fill up this chunk. */ | |
196 | size = length - stored; | |
197 | ||
198 | memcpy(chunk + chunk_offset, data + stored, size); | |
199 | chunk_bytes_free -= size; | |
200 | stored += size; | |
201 | } | |
202 | ||
203 | ds->num_units += stored / ds->ds_unitsize; | |
204 | ||
205 | return SR_OK; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Allocate a new memory chunk, append it to the datastore's chunklist. | |
210 | * | |
211 | * The newly allocated chunk is added to the datastore's chunklist by this | |
212 | * function, and the return value additionally points to the new chunk. | |
213 | * | |
214 | * The allocated memory is guaranteed to be cleared. | |
215 | * | |
216 | * TODO: This function should use the datastore's 'chunksize' field instead | |
217 | * of hardcoding DATASTORE_CHUNKSIZE. | |
218 | * TODO: Return int, so we can return SR_OK / SR_ERR_ARG / SR_ERR_MALLOC? | |
219 | * | |
220 | * @param ds Pointer to a variable which holds the datastore structure. | |
221 | * Must not be NULL. The contents of 'ds' are modified in-place. | |
222 | * | |
223 | * @return Pointer to the newly allocated chunk, or NULL upon failure. | |
224 | */ | |
225 | static gpointer new_chunk(struct sr_datastore **ds) | |
226 | { | |
227 | gpointer chunk; | |
228 | ||
229 | if (!ds) { | |
230 | sr_err("ds: %s: ds was NULL", __func__); | |
231 | return NULL; /* TODO: SR_ERR_ARG later? */ | |
232 | } | |
233 | ||
234 | chunk = g_try_malloc0(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize); | |
235 | if (!chunk) { | |
236 | sr_err("ds: %s: chunk malloc failed", __func__); | |
237 | return NULL; /* TODO: SR_ERR_MALLOC later? */ | |
238 | } | |
239 | ||
240 | (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk); | |
241 | ||
242 | return chunk; /* TODO: SR_OK later? */ | |
243 | } |