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