2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 static gpointer new_chunk(struct datastore **ds);
30 struct datastore *datastore_new(int unitsize)
34 ds = g_malloc(sizeof(struct datastore));
35 ds->ds_unitsize = unitsize;
43 void datastore_destroy(struct datastore *ds)
47 for(chunk = ds->chunklist; chunk; chunk = chunk->next)
49 g_slist_free(ds->chunklist);
55 void datastore_put(struct datastore *ds, void *data, unsigned int length, int in_unitsize, int *probelist)
57 int capacity, stored, size, num_chunks, chunk_bytes_free, chunk_offset;
60 if(ds->chunklist == NULL)
61 chunk = new_chunk(&ds);
63 chunk = g_slist_last(ds->chunklist)->data;
64 num_chunks = g_slist_length(ds->chunklist);
65 capacity = (num_chunks * DATASTORE_CHUNKSIZE);
66 chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
67 chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) - chunk_bytes_free;
69 while(stored < length) {
70 if(chunk_bytes_free == 0) {
71 chunk = new_chunk(&ds);
72 chunk_bytes_free = DATASTORE_CHUNKSIZE;
76 if(length - stored > chunk_bytes_free)
77 size = chunk_bytes_free;
79 /* last part, won't fill up this chunk */
80 size = length - stored;
81 memcpy(chunk + chunk_offset, data + stored, size);
82 chunk_bytes_free -= size;
85 ds->num_units += stored / ds->ds_unitsize;
90 static gpointer new_chunk(struct datastore **ds)
94 chunk = g_malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize);
95 (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk);