]> sigrok.org Git - libsigrok.git/blame - datastore.c
Fix insufficient braces in KHZ() et al.
[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>
24#include "sigrok.h"
25
26static gpointer new_chunk(struct datastore **ds);
27
28
29
30struct datastore *datastore_new(int unitsize)
31{
32 struct datastore *ds;
33
34 ds = g_malloc(sizeof(struct datastore));
35 ds->ds_unitsize = unitsize;
36 ds->num_units = 0;
37 ds->chunklist = NULL;
38
39 return ds;
40}
41
42
43void datastore_destroy(struct datastore *ds)
44{
45 GSList *chunk;
46
47 for(chunk = ds->chunklist; chunk; chunk = chunk->next)
48 g_free(chunk->data);
49 g_slist_free(ds->chunklist);
50 g_free(ds);
51
52}
53
54
55void datastore_put(struct datastore *ds, void *data, unsigned int length, int in_unitsize, int *probelist)
56{
afc8e4de
UH
57 unsigned int stored;
58 int capacity, size, num_chunks, chunk_bytes_free, chunk_offset;
a1bb33af
UH
59 gpointer chunk;
60
afc8e4de
UH
61 /* QUICK HACK */
62 in_unitsize = in_unitsize;
63 probelist = probelist;
64
a1bb33af
UH
65 if(ds->chunklist == NULL)
66 chunk = new_chunk(&ds);
67 else
68 chunk = g_slist_last(ds->chunklist)->data;
69 num_chunks = g_slist_length(ds->chunklist);
70 capacity = (num_chunks * DATASTORE_CHUNKSIZE);
71 chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
72 chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) - chunk_bytes_free;
73 stored = 0;
74 while(stored < length) {
75 if(chunk_bytes_free == 0) {
76 chunk = new_chunk(&ds);
77 chunk_bytes_free = DATASTORE_CHUNKSIZE;
78 chunk_offset = 0;
79 }
80
afc8e4de 81 if(length - stored > (unsigned int)chunk_bytes_free)
a1bb33af
UH
82 size = chunk_bytes_free;
83 else
84 /* last part, won't fill up this chunk */
85 size = length - stored;
86 memcpy(chunk + chunk_offset, data + stored, size);
87 chunk_bytes_free -= size;
88 stored += size;
89 }
90 ds->num_units += stored / ds->ds_unitsize;
91
92}
93
94
95static gpointer new_chunk(struct datastore **ds)
96{
97 gpointer chunk;
98
99 chunk = g_malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize);
100 (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk);
101
102 return chunk;
103}
104
105