]> sigrok.org Git - libsigrok.git/blame - datastore.c
Datastore: More error checking.
[libsigrok.git] / datastore.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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>
1b452b85 24#include <sigrok.h>
a1bb33af
UH
25
26static gpointer new_chunk(struct datastore **ds);
27
33247d6a 28/* TODO: Return int as error status, and the struct as param. */
a1bb33af
UH
29struct datastore *datastore_new(int unitsize)
30{
31 struct datastore *ds;
32
33247d6a
UH
33 if (unitsize <= 0)
34 // return SIGROK_ERR;
35 return NULL; /* FIXME */
36
37 if (!(ds = g_malloc(sizeof(struct datastore))))
38 // return SIGROK_ERR_MALLOC;
39 return NULL; /* FIXME */
40
a1bb33af
UH
41 ds->ds_unitsize = unitsize;
42 ds->num_units = 0;
43 ds->chunklist = NULL;
44
45 return ds;
46}
47
33247d6a 48int datastore_destroy(struct datastore *ds)
a1bb33af
UH
49{
50 GSList *chunk;
51
33247d6a
UH
52 if (!ds)
53 return SIGROK_ERR;
54
1b452b85 55 for (chunk = ds->chunklist; chunk; chunk = chunk->next)
a1bb33af
UH
56 g_free(chunk->data);
57 g_slist_free(ds->chunklist);
58 g_free(ds);
33247d6a
UH
59
60 return SIGROK_OK;
a1bb33af
UH
61}
62
1b452b85
UH
63void datastore_put(struct datastore *ds, void *data, unsigned int length,
64 int in_unitsize, int *probelist)
a1bb33af 65{
afc8e4de
UH
66 unsigned int stored;
67 int capacity, size, num_chunks, chunk_bytes_free, chunk_offset;
a1bb33af
UH
68 gpointer chunk;
69
afc8e4de
UH
70 /* QUICK HACK */
71 in_unitsize = in_unitsize;
72 probelist = probelist;
73
1b452b85 74 if (ds->chunklist == NULL)
a1bb33af
UH
75 chunk = new_chunk(&ds);
76 else
77 chunk = g_slist_last(ds->chunklist)->data;
1b452b85 78
a1bb33af
UH
79 num_chunks = g_slist_length(ds->chunklist);
80 capacity = (num_chunks * DATASTORE_CHUNKSIZE);
81 chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
1b452b85
UH
82 chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1))
83 - chunk_bytes_free;
a1bb33af 84 stored = 0;
1b452b85
UH
85 while (stored < length) {
86 if (chunk_bytes_free == 0) {
a1bb33af
UH
87 chunk = new_chunk(&ds);
88 chunk_bytes_free = DATASTORE_CHUNKSIZE;
89 chunk_offset = 0;
90 }
91
1b452b85 92 if (length - stored > (unsigned int)chunk_bytes_free)
a1bb33af
UH
93 size = chunk_bytes_free;
94 else
1b452b85 95 /* Last part, won't fill up this chunk. */
a1bb33af 96 size = length - stored;
1b452b85 97
a1bb33af
UH
98 memcpy(chunk + chunk_offset, data + stored, size);
99 chunk_bytes_free -= size;
100 stored += size;
101 }
102 ds->num_units += stored / ds->ds_unitsize;
a1bb33af
UH
103}
104
a1bb33af
UH
105static gpointer new_chunk(struct datastore **ds)
106{
107 gpointer chunk;
108
33247d6a
UH
109 if (!(chunk = malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize)))
110 return NULL;
111
a1bb33af
UH
112 (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk);
113
114 return chunk;
115}