From efdc5cdff3497df222d47e81b28fae4185426c5a Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 22 Oct 2016 20:47:36 -0400 Subject: [PATCH] added sample_{alloc,free} --- include/villas/sample.h | 9 +++++++-- lib/sample.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/villas/sample.h b/include/villas/sample.h index 5d2db263d..7870d3cd9 100644 --- a/include/villas/sample.h +++ b/include/villas/sample.h @@ -60,8 +60,13 @@ struct sample { } data[]; }; -/** Request \p cnt samples from memory pool \p p and initialize them. */ -int sample_get_many(struct pool *p, struct sample *smps[], int cnt); +/** Request \p cnt samples from memory pool \p p and initialize them. + * This will leave the reference count of the sample to zero. + * Use the sample_get() function to increase it. */ +int sample_alloc(struct pool *p, struct sample *smps[], int cnt); + +/** Release an array of samples back to their pools */ +void sample_free(struct sample *smps[], int cnt); /** Increase reference count of sample */ int sample_get(struct sample *s); diff --git a/lib/sample.c b/lib/sample.c index f3040d3a0..058a2ad30 100644 --- a/lib/sample.c +++ b/lib/sample.c @@ -12,19 +12,27 @@ #include "sample.h" #include "timing.h" -int sample_get_many(struct pool *p, struct sample *smps[], int cnt) { +int sample_alloc(struct pool *p, struct sample *smps[], int cnt) { int ret; ret = pool_get_many(p, (void **) smps, cnt); if (ret < 0) return ret; - for (int i = 0; i < ret; i++) + for (int i = 0; i < ret; i++) { smps[i]->capacity = (p->blocksz - sizeof(**smps)) / sizeof(smps[0]->data[0]); + smps[i]->pool = p; + } return ret; } +void sample_free(struct sample *smps[], int cnt) +{ + for (int i = 0; i < cnt; i++) + pool_put(smps[i]->pool, smps[i]); +} + int sample_get(struct sample *s) { return atomic_fetch_add(&s->refcnt, 1) + 1;