From: Uwe Hermann Date: Sun, 9 May 2010 11:25:03 +0000 (+0200) Subject: Datastore: More error checking. X-Git-Tag: libsigrok-0.1.0~518 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=33247d6acf94bb9119ba7e1a8239b474bdcfa430;p=libsigrok.git Datastore: More error checking. --- diff --git a/datastore.c b/datastore.c index 443ab153..bb59fd58 100644 --- a/datastore.c +++ b/datastore.c @@ -25,11 +25,19 @@ static gpointer new_chunk(struct datastore **ds); +/* TODO: Return int as error status, and the struct as param. */ struct datastore *datastore_new(int unitsize) { struct datastore *ds; - ds = g_malloc(sizeof(struct datastore)); + if (unitsize <= 0) + // return SIGROK_ERR; + return NULL; /* FIXME */ + + if (!(ds = g_malloc(sizeof(struct datastore)))) + // return SIGROK_ERR_MALLOC; + return NULL; /* FIXME */ + ds->ds_unitsize = unitsize; ds->num_units = 0; ds->chunklist = NULL; @@ -37,14 +45,19 @@ struct datastore *datastore_new(int unitsize) return ds; } -void datastore_destroy(struct datastore *ds) +int datastore_destroy(struct datastore *ds) { GSList *chunk; + if (!ds) + return SIGROK_ERR; + for (chunk = ds->chunklist; chunk; chunk = chunk->next) g_free(chunk->data); g_slist_free(ds->chunklist); g_free(ds); + + return SIGROK_OK; } void datastore_put(struct datastore *ds, void *data, unsigned int length, @@ -93,7 +106,9 @@ static gpointer new_chunk(struct datastore **ds) { gpointer chunk; - chunk = g_malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize); + if (!(chunk = malloc(DATASTORE_CHUNKSIZE * (*ds)->ds_unitsize))) + return NULL; + (*ds)->chunklist = g_slist_append((*ds)->chunklist, chunk); return chunk; diff --git a/sigrok.h b/sigrok.h index 6525fa84..bd1e8909 100644 --- a/sigrok.h +++ b/sigrok.h @@ -178,12 +178,12 @@ void sigrok_cleanup(void); struct datastore { /* Size in bytes of the number of units stored in this datastore */ int ds_unitsize; - unsigned int num_units; + unsigned int num_units; /* TODO: uint64_t */ GSList *chunklist; }; struct datastore *datastore_new(int unitsize); -void datastore_destroy(struct datastore *ds); +int datastore_destroy(struct datastore *ds); void datastore_put(struct datastore *ds, void *data, unsigned int length, int in_unitsize, int *probelist);