2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
2010-04-15 18:07:16 +00:00
|
|
|
#include <sigrok.h>
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
static gpointer new_chunk(struct datastore **ds);
|
|
|
|
|
2010-05-09 11:32:58 +00:00
|
|
|
int datastore_new(int unitsize, struct datastore **ds)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2010-05-09 11:32:58 +00:00
|
|
|
if (!ds)
|
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-05-09 11:25:03 +00:00
|
|
|
if (unitsize <= 0)
|
2010-05-09 11:32:58 +00:00
|
|
|
return SIGROK_ERR; /* TODO: Different error? */
|
2010-05-09 11:25:03 +00:00
|
|
|
|
2010-05-09 11:32:58 +00:00
|
|
|
if (!(*ds = g_malloc(sizeof(struct datastore))))
|
|
|
|
return SIGROK_ERR_MALLOC;
|
2010-05-09 11:25:03 +00:00
|
|
|
|
2010-05-09 11:32:58 +00:00
|
|
|
(*ds)->ds_unitsize = unitsize;
|
|
|
|
(*ds)->num_units = 0;
|
|
|
|
(*ds)->chunklist = NULL;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-05-09 11:32:58 +00:00
|
|
|
return SIGROK_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:25:03 +00:00
|
|
|
int datastore_destroy(struct datastore *ds)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
GSList *chunk;
|
|
|
|
|
2010-05-09 11:25:03 +00:00
|
|
|
if (!ds)
|
|
|
|
return SIGROK_ERR;
|
2011-01-15 14:43:25 +00:00
|
|
|
|
2010-04-15 18:07:16 +00:00
|
|
|
for (chunk = ds->chunklist; chunk; chunk = chunk->next)
|
2010-04-02 18:18:27 +00:00
|
|
|
g_free(chunk->data);
|
|
|
|
g_slist_free(ds->chunklist);
|
|
|
|
g_free(ds);
|
2010-05-09 11:25:03 +00:00
|
|
|
|
|
|
|
return SIGROK_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:07:16 +00:00
|
|
|
void datastore_put(struct datastore *ds, void *data, unsigned int length,
|
|
|
|
int in_unitsize, int *probelist)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int stored;
|
|
|
|
int capacity, size, num_chunks, chunk_bytes_free, chunk_offset;
|
2010-04-02 18:18:27 +00:00
|
|
|
gpointer chunk;
|
|
|
|
|
2011-01-13 22:50:34 +00:00
|
|
|
/* Avoid compiler warnings. */
|
2010-04-09 20:18:46 +00:00
|
|
|
in_unitsize = in_unitsize;
|
|
|
|
probelist = probelist;
|
|
|
|
|
2010-04-15 18:07:16 +00:00
|
|
|
if (ds->chunklist == NULL)
|
2010-04-02 18:18:27 +00:00
|
|
|
chunk = new_chunk(&ds);
|
|
|
|
else
|
|
|
|
chunk = g_slist_last(ds->chunklist)->data;
|
2010-04-15 18:07:16 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
num_chunks = g_slist_length(ds->chunklist);
|
|
|
|
capacity = (num_chunks * DATASTORE_CHUNKSIZE);
|
|
|
|
chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
|
2010-04-15 18:07:16 +00:00
|
|
|
chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1))
|
|
|
|
- chunk_bytes_free;
|
2010-04-02 18:18:27 +00:00
|
|
|
stored = 0;
|
2010-04-15 18:07:16 +00:00
|
|
|
while (stored < length) {
|
|
|
|
if (chunk_bytes_free == 0) {
|
2010-04-02 18:18:27 +00:00
|
|
|
chunk = new_chunk(&ds);
|
|
|
|
chunk_bytes_free = DATASTORE_CHUNKSIZE;
|
|
|
|
chunk_offset = 0;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:07:16 +00:00
|
|
|
if (length - stored > (unsigned int)chunk_bytes_free)
|
2010-04-02 18:18:27 +00:00
|
|
|
size = chunk_bytes_free;
|
|
|
|
else
|
2010-04-15 18:07:16 +00:00
|
|
|
/* Last part, won't fill up this chunk. */
|
2010-04-02 18:18:27 +00:00
|
|
|
size = length - stored;
|
2010-04-15 18:07:16 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
memcpy(chunk + chunk_offset, data + stored, size);
|
|
|
|
chunk_bytes_free -= size;
|
|
|
|
stored += size;
|
|
|
|
}
|
|
|
|
ds->num_units += stored / ds->ds_unitsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer new_chunk(struct datastore **ds)
|
|
|
|
{
|
|
|
|
gpointer chunk;
|
|
|
|
|
2010-05-09 11:25:03 +00:00
|
|
|
if (!(chunk = malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize)))
|
|
|
|
return NULL;
|
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
(*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk);
|
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|