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