diff --git a/include/villas/sample.h b/include/villas/sample.h index 62bb67624..7c6d88925 100644 --- a/include/villas/sample.h +++ b/include/villas/sample.h @@ -83,6 +83,8 @@ struct sample { } data[]; /**< Data is in host endianess! */ }; +#define sample_pool(s) ((struct pool *) ((char *) (s) + (s)->pool_off)) + /** Request \p cnt samples from memory pool \p p and initialize them. * The reference count will already be set to 1. * Use the sample_get() function to increase it. */ diff --git a/lib/hooks/map.c b/lib/hooks/map.c index 3a8033246..bc274ecd6 100644 --- a/lib/hooks/map.c +++ b/lib/hooks/map.c @@ -81,7 +81,7 @@ static int map_read(struct hook *h, struct sample *smps[], unsigned *cnt) if (*cnt <= 0) return 0; - ret = sample_alloc((struct pool *) ((char* ) smps[0] + smps[0]->pool_off), tmp, *cnt); + ret = sample_alloc(sample_pool(smps[0]), tmp, *cnt); if (ret != *cnt) return ret; diff --git a/lib/sample.c b/lib/sample.c index e9c16f33d..ad5b86101 100644 --- a/lib/sample.c +++ b/lib/sample.c @@ -46,10 +46,8 @@ int sample_alloc(struct pool *p, struct sample *smps[], int cnt) void sample_free(struct sample *smps[], int cnt) { - for (int i = 0; i < cnt; i++) { - struct pool *p = (struct pool *) ((char *) smps[i] + smps[i]->pool_off); - pool_put(p, smps[i]); - } + for (int i = 0; i < cnt; i++) + pool_put(sample_pool(smps[i]), smps[i]); } int sample_put_many(struct sample *smps[], int cnt) @@ -82,10 +80,8 @@ int sample_put(struct sample *s) int prev = atomic_fetch_sub(&s->refcnt, 1); /* Did we had the last reference? */ - if (prev == 1) { - struct pool *p = (struct pool *) ((char *) s + s->pool_off); - pool_put(p, s); - } + if (prev == 1) + pool_put(sample_pool(s), s); return prev - 1; }