2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
2012-02-13 13:31:51 +00:00
|
|
|
* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
|
2010-04-02 18:18:27 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2012-07-04 22:55:07 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-11-11 11:44:16 +00:00
|
|
|
/* Message logging helpers with driver-specific prefix string. */
|
|
|
|
#define DRIVER_LOG_DOMAIN "datastore: "
|
|
|
|
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
|
2012-10-21 22:30:12 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Creating, using, or destroying libsigrok datastores.
|
|
|
|
*/
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup grp_datastore Datastore
|
|
|
|
*
|
|
|
|
* Creating, using, or destroying libsigrok datastores.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-02-08 20:22:10 +00:00
|
|
|
static gpointer new_chunk(struct sr_datastore **ds);
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
/**
|
|
|
|
* Create a new datastore with the specified unit size.
|
|
|
|
*
|
|
|
|
* The unit size is fixed once the datastore is created, and cannot be
|
|
|
|
* changed later on, neither can data be added to the datastore with
|
|
|
|
* different unit sizes later.
|
|
|
|
*
|
|
|
|
* It is the caller's responsibility to free the allocated memory of the
|
|
|
|
* datastore via the sr_datastore_destroy() function, if no longer needed.
|
|
|
|
*
|
2012-10-21 21:24:42 +00:00
|
|
|
* @todo Unitsize should probably be unsigned int or uint32_t or similar.
|
|
|
|
* @todo This function should have a 'chunksize' parameter, and
|
2011-12-25 18:59:15 +00:00
|
|
|
* struct sr_datastore a 'chunksize' field.
|
|
|
|
*
|
|
|
|
* @param unitsize The unit size (>= 1) to be used for this datastore.
|
|
|
|
* @param ds Pointer to a variable which will hold the newly created
|
|
|
|
* datastore structure.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
|
|
|
|
* or SR_ERR_ARG upon invalid arguments. If something other than SR_OK
|
|
|
|
* is returned, the value of 'ds' is undefined.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_datastore_new(int unitsize, struct sr_datastore **ds)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2011-12-25 18:59:15 +00:00
|
|
|
if (!ds) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: ds was NULL", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
if (unitsize <= 0) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: unitsize was %d, but it must be >= 1",
|
2011-12-25 18:59:15 +00:00
|
|
|
__func__, unitsize);
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
2010-05-09 11:25:03 +00:00
|
|
|
|
2011-04-16 12:17:51 +00:00
|
|
|
if (!(*ds = g_try_malloc(sizeof(struct sr_datastore)))) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: ds malloc failed", __func__);
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2011-04-16 12:17:51 +00:00
|
|
|
}
|
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
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
/**
|
|
|
|
* Destroy the specified datastore and free the memory used by it.
|
|
|
|
*
|
|
|
|
* This will free the memory used by the data in the datastore's 'chunklist',
|
|
|
|
* by the chunklist data structure itself, and by the datastore struct.
|
|
|
|
*
|
|
|
|
* @param ds The datastore to destroy.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_datastore_destroy(struct sr_datastore *ds)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
GSList *chunk;
|
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
if (!ds) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: ds was NULL", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
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);
|
2012-03-04 14:08:11 +00:00
|
|
|
ds = NULL;
|
2011-12-25 18:59:15 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
/**
|
|
|
|
* Append some data to the specified datastore.
|
|
|
|
*
|
2012-10-21 21:24:42 +00:00
|
|
|
* @todo More elaborate function description.
|
|
|
|
* @todo This function should use the (not yet available) 'chunksize' field
|
2011-12-25 18:59:15 +00:00
|
|
|
* of struct sr_datastore (instead of hardcoding DATASTORE_CHUNKSIZE).
|
2012-10-21 21:24:42 +00:00
|
|
|
* @todo in_unitsize and probelist are unused?
|
|
|
|
* @todo A few of the parameters can be const.
|
|
|
|
* @todo Ideally, 'ds' should be unmodified upon errors.
|
|
|
|
* @todo Should 0 be allowed as length?
|
|
|
|
* @todo Specify/document the data format of the 'data' parameter.
|
2011-12-25 18:59:15 +00:00
|
|
|
*
|
|
|
|
* @param ds Pointer to the datastore which shall receive the data.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @param data Pointer to the memory buffer containing the data to add.
|
2012-10-21 21:24:42 +00:00
|
|
|
* Must not be NULL.
|
2011-12-25 18:59:15 +00:00
|
|
|
* @param length Length of the data to add (in number of bytes).
|
|
|
|
* @param in_unitsize The unit size (>= 1) of the input data.
|
|
|
|
* @param probelist Pointer to a list of integers (probe numbers). The probe
|
|
|
|
* numbers in this list are 1-based, i.e. the first probe
|
|
|
|
* is expected to be numbered 1 (not 0!). Must not be NULL.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
|
|
|
|
* or SR_ERR_ARG upon invalid arguments. If something other than SR_OK
|
|
|
|
* is returned, the value/state of 'ds' is undefined.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_datastore_put(struct sr_datastore *ds, void *data,
|
2012-03-21 18:28:43 +00:00
|
|
|
unsigned int length, int in_unitsize, const 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-12-25 18:59:15 +00:00
|
|
|
if (!ds) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: ds was NULL", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unitsize must not be 0, we'll divide by 0 otherwise. */
|
|
|
|
if (ds->ds_unitsize == 0) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: ds->ds_unitsize was 0", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: data was NULL", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_unitsize < 1) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: in_unitsize was %d, but it must be >= 1",
|
2011-12-25 18:59:15 +00:00
|
|
|
__func__, in_unitsize);
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!probelist) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: probelist was NULL", __func__);
|
2011-12-25 18:59:15 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the last chunk in the list, or create a new one if needed. */
|
2012-02-29 18:11:09 +00:00
|
|
|
if (ds->chunklist == NULL) {
|
|
|
|
if (!(chunk = new_chunk(&ds))) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: couldn't allocate new chunk", __func__);
|
2012-02-29 18:11:09 +00:00
|
|
|
return SR_ERR_MALLOC;
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-02 18:18:27 +00:00
|
|
|
chunk = g_slist_last(ds->chunklist)->data;
|
2012-02-29 18:11:09 +00:00
|
|
|
}
|
2010-04-15 18:07:16 +00:00
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
/* Get/calculate number of chunks, free space, etc. */
|
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;
|
2011-12-25 18:59:15 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
stored = 0;
|
2010-04-15 18:07:16 +00:00
|
|
|
while (stored < length) {
|
2011-12-25 18:59:15 +00:00
|
|
|
/* No more free space left, allocate a new chunk. */
|
2010-04-15 18:07:16 +00:00
|
|
|
if (chunk_bytes_free == 0) {
|
2012-02-29 18:11:09 +00:00
|
|
|
if (!(chunk = new_chunk(&ds))) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: couldn't allocate new chunk",
|
2012-02-29 18:11:09 +00:00
|
|
|
__func__);
|
|
|
|
return SR_ERR_MALLOC;
|
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-12-25 18:59:15 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
ds->num_units += stored / ds->ds_unitsize;
|
2011-12-25 18:59:15 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
/**
|
|
|
|
* Allocate a new memory chunk, append it to the datastore's chunklist.
|
|
|
|
*
|
|
|
|
* The newly allocated chunk is added to the datastore's chunklist by this
|
|
|
|
* function, and the return value additionally points to the new chunk.
|
|
|
|
*
|
|
|
|
* The allocated memory is guaranteed to be cleared.
|
|
|
|
*
|
2012-10-21 21:24:42 +00:00
|
|
|
* @todo This function should use the datastore's 'chunksize' field instead
|
2011-12-25 18:59:15 +00:00
|
|
|
* of hardcoding DATASTORE_CHUNKSIZE.
|
2012-10-21 21:24:42 +00:00
|
|
|
* @todo Return int, so we can return SR_OK / SR_ERR_ARG / SR_ERR_MALLOC?
|
2011-12-25 18:59:15 +00:00
|
|
|
*
|
|
|
|
* @param ds Pointer to a variable which holds the datastore structure.
|
|
|
|
* Must not be NULL. The contents of 'ds' are modified in-place.
|
|
|
|
*
|
|
|
|
* @return Pointer to the newly allocated chunk, or NULL upon failure.
|
|
|
|
*/
|
2011-02-08 20:22:10 +00:00
|
|
|
static gpointer new_chunk(struct sr_datastore **ds)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
gpointer chunk;
|
|
|
|
|
2012-02-29 18:11:09 +00:00
|
|
|
/* Note: Caller checked that ds != NULL. */
|
2011-12-25 18:59:15 +00:00
|
|
|
|
|
|
|
chunk = g_try_malloc0(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize);
|
|
|
|
if (!chunk) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: chunk malloc failed (ds_unitsize was %u)",
|
2012-06-19 21:33:50 +00:00
|
|
|
__func__, (*ds)->ds_unitsize);
|
2011-12-25 18:59:15 +00:00
|
|
|
return NULL; /* TODO: SR_ERR_MALLOC later? */
|
|
|
|
}
|
2010-05-09 11:25:03 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
(*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk);
|
|
|
|
|
2011-12-25 18:59:15 +00:00
|
|
|
return chunk; /* TODO: SR_OK later? */
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
2012-10-21 14:13:36 +00:00
|
|
|
|
|
|
|
/** @} */
|