X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=datastore.c;h=f45353a0f08c8c17447cda583a96d48002ab3159;hb=0caa1ef0cb0b2231e232a7d1681e4e13fe838035;hp=fccb4d5b0fd73bf2026f4d43f58019e8d4b2f0a9;hpb=15278f3e9cf4c4a4a6c331e042f9935709343c82;p=libsigrok.git diff --git a/datastore.c b/datastore.c index fccb4d5b..f45353a0 100644 --- a/datastore.c +++ b/datastore.c @@ -1,7 +1,7 @@ /* * This file is part of the sigrok project. * - * Copyright (C) 2010 Bert Vermeulen + * Copyright (C) 2010-2012 Bert Vermeulen * * 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 @@ -21,8 +21,8 @@ #include #include #include -#include -#include +#include "sigrok.h" +#include "sigrok-internal.h" static gpointer new_chunk(struct sr_datastore **ds); @@ -48,7 +48,7 @@ static gpointer new_chunk(struct sr_datastore **ds); * or SR_ERR_ARG upon invalid arguments. If something other than SR_OK * is returned, the value of 'ds' is undefined. */ -int sr_datastore_new(int unitsize, struct sr_datastore **ds) +SR_API int sr_datastore_new(int unitsize, struct sr_datastore **ds) { if (!ds) { sr_err("ds: %s: ds was NULL", __func__); @@ -83,7 +83,7 @@ int sr_datastore_new(int unitsize, struct sr_datastore **ds) * * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. */ -int sr_datastore_destroy(struct sr_datastore *ds) +SR_API int sr_datastore_destroy(struct sr_datastore *ds) { GSList *chunk; @@ -96,8 +96,7 @@ int sr_datastore_destroy(struct sr_datastore *ds) g_free(chunk->data); g_slist_free(ds->chunklist); g_free(ds); - - /* TODO: Set ds = NULL? */ + ds = NULL; return SR_OK; } @@ -111,7 +110,6 @@ int sr_datastore_destroy(struct sr_datastore *ds) * of struct sr_datastore (instead of hardcoding DATASTORE_CHUNKSIZE). * TODO: in_unitsize and probelist are unused? * TODO: A few of the parameters can be const. - * TODO: Handle new_chunk() returning NULL. * TODO: Ideally, 'ds' should be unmodified upon errors. * * @param ds Pointer to the datastore which shall receive the data. @@ -129,17 +127,13 @@ int sr_datastore_destroy(struct sr_datastore *ds) * or SR_ERR_ARG upon invalid arguments. If something other than SR_OK * is returned, the value/state of 'ds' is undefined. */ -int sr_datastore_put(struct sr_datastore *ds, void *data, unsigned int length, - int in_unitsize, int *probelist) +SR_API int sr_datastore_put(struct sr_datastore *ds, void *data, + unsigned int length, int in_unitsize, const int *probelist) { unsigned int stored; int capacity, size, num_chunks, chunk_bytes_free, chunk_offset; gpointer chunk; - /* Avoid compiler warnings. */ - (void)in_unitsize; - (void)probelist; - if (!ds) { sr_err("ds: %s: ds was NULL", __func__); return SR_ERR_ARG; @@ -168,10 +162,14 @@ int sr_datastore_put(struct sr_datastore *ds, void *data, unsigned int length, } /* Get the last chunk in the list, or create a new one if needed. */ - if (ds->chunklist == NULL) - chunk = new_chunk(&ds); - else + if (ds->chunklist == NULL) { + if (!(chunk = new_chunk(&ds))) { + sr_err("ds: %s: couldn't allocate new chunk", __func__); + return SR_ERR_MALLOC; + } + } else { chunk = g_slist_last(ds->chunklist)->data; + } /* Get/calculate number of chunks, free space, etc. */ num_chunks = g_slist_length(ds->chunklist); @@ -184,7 +182,11 @@ int sr_datastore_put(struct sr_datastore *ds, void *data, unsigned int length, while (stored < length) { /* No more free space left, allocate a new chunk. */ if (chunk_bytes_free == 0) { - chunk = new_chunk(&ds); + if (!(chunk = new_chunk(&ds))) { + sr_err("ds: %s: couldn't allocate new chunk", + __func__); + return SR_ERR_MALLOC; + } chunk_bytes_free = DATASTORE_CHUNKSIZE; chunk_offset = 0; } @@ -226,14 +228,12 @@ static gpointer new_chunk(struct sr_datastore **ds) { gpointer chunk; - if (!ds) { - sr_err("ds: %s: ds was NULL", __func__); - return NULL; /* TODO: SR_ERR_ARG later? */ - } + /* Note: Caller checked that ds != NULL. */ chunk = g_try_malloc0(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize); if (!chunk) { - sr_err("ds: %s: chunk malloc failed", __func__); + sr_err("ds: %s: chunk malloc failed (ds_unitsize was %u)", + __func__, (*ds)->ds_unitsize); return NULL; /* TODO: SR_ERR_MALLOC later? */ }