]>
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 | ||
2aebf78d | 28 | int datastore_new(int unitsize, struct datastore **ds) |
a1bb33af | 29 | { |
2aebf78d | 30 | if (!ds) |
e46b8fb1 | 31 | return SR_ERR; |
a1bb33af | 32 | |
33247d6a | 33 | if (unitsize <= 0) |
e46b8fb1 | 34 | return SR_ERR; /* TODO: Different error? */ |
33247d6a | 35 | |
2aebf78d | 36 | if (!(*ds = g_malloc(sizeof(struct datastore)))) |
e46b8fb1 | 37 | return SR_ERR_MALLOC; |
33247d6a | 38 | |
2aebf78d UH |
39 | (*ds)->ds_unitsize = unitsize; |
40 | (*ds)->num_units = 0; | |
41 | (*ds)->chunklist = NULL; | |
a1bb33af | 42 | |
e46b8fb1 | 43 | return SR_OK; |
a1bb33af UH |
44 | } |
45 | ||
33247d6a | 46 | int datastore_destroy(struct datastore *ds) |
a1bb33af UH |
47 | { |
48 | GSList *chunk; | |
49 | ||
33247d6a | 50 | if (!ds) |
e46b8fb1 | 51 | return SR_ERR; |
484760d1 | 52 | |
1b452b85 | 53 | for (chunk = ds->chunklist; chunk; chunk = chunk->next) |
a1bb33af UH |
54 | g_free(chunk->data); |
55 | g_slist_free(ds->chunklist); | |
56 | g_free(ds); | |
33247d6a | 57 | |
e46b8fb1 | 58 | return SR_OK; |
a1bb33af UH |
59 | } |
60 | ||
1b452b85 UH |
61 | void datastore_put(struct datastore *ds, void *data, unsigned int length, |
62 | int in_unitsize, int *probelist) | |
a1bb33af | 63 | { |
afc8e4de UH |
64 | unsigned int stored; |
65 | int capacity, size, num_chunks, chunk_bytes_free, chunk_offset; | |
a1bb33af UH |
66 | gpointer chunk; |
67 | ||
17e1afcb | 68 | /* Avoid compiler warnings. */ |
afc8e4de UH |
69 | in_unitsize = in_unitsize; |
70 | probelist = probelist; | |
71 | ||
1b452b85 | 72 | if (ds->chunklist == NULL) |
a1bb33af UH |
73 | chunk = new_chunk(&ds); |
74 | else | |
75 | chunk = g_slist_last(ds->chunklist)->data; | |
1b452b85 | 76 | |
a1bb33af UH |
77 | num_chunks = g_slist_length(ds->chunklist); |
78 | capacity = (num_chunks * DATASTORE_CHUNKSIZE); | |
79 | chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units); | |
1b452b85 UH |
80 | chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) |
81 | - chunk_bytes_free; | |
a1bb33af | 82 | stored = 0; |
1b452b85 UH |
83 | while (stored < length) { |
84 | if (chunk_bytes_free == 0) { | |
a1bb33af UH |
85 | chunk = new_chunk(&ds); |
86 | chunk_bytes_free = DATASTORE_CHUNKSIZE; | |
87 | chunk_offset = 0; | |
88 | } | |
89 | ||
1b452b85 | 90 | if (length - stored > (unsigned int)chunk_bytes_free) |
a1bb33af UH |
91 | size = chunk_bytes_free; |
92 | else | |
1b452b85 | 93 | /* Last part, won't fill up this chunk. */ |
a1bb33af | 94 | size = length - stored; |
1b452b85 | 95 | |
a1bb33af UH |
96 | memcpy(chunk + chunk_offset, data + stored, size); |
97 | chunk_bytes_free -= size; | |
98 | stored += size; | |
99 | } | |
100 | ds->num_units += stored / ds->ds_unitsize; | |
a1bb33af UH |
101 | } |
102 | ||
a1bb33af UH |
103 | static gpointer new_chunk(struct datastore **ds) |
104 | { | |
105 | gpointer chunk; | |
106 | ||
33247d6a UH |
107 | if (!(chunk = malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize))) |
108 | return NULL; | |
109 | ||
a1bb33af UH |
110 | (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk); |
111 | ||
112 | return chunk; | |
113 | } |