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