diff --git a/include/villas/sample.h b/include/villas/sample.h index 58b2f8915..444507462 100644 --- a/include/villas/sample.h +++ b/include/villas/sample.h @@ -42,7 +42,7 @@ struct sample { int capacity; /**< The number of values in sample::values for which memory is reserved. */ atomic_int refcnt; /**< Reference counter. */ - struct pool *pool; /**< This sample is belong to this memory pool. */ + uintptr_t pool_off; /**< This sample is belong to this memory pool (relative pointer). */ struct node *source; /**< The node from which this sample originates. */ /** All timestamps are seconds / nano seconds after 1.1.1970 UTC */ @@ -113,4 +113,4 @@ int sample_fprint(FILE *f, struct sample *s, int flags); */ int sample_fscan(FILE *f, struct sample *s, int *flags); -#endif /* _SAMPLE_H_ */ \ No newline at end of file +#endif /* _SAMPLE_H_ */ diff --git a/lib/sample.c b/lib/sample.c index e2397f7a2..db6785f5c 100644 --- a/lib/sample.c +++ b/lib/sample.c @@ -19,7 +19,7 @@ int sample_alloc(struct pool *p, struct sample *smps[], int cnt) { for (int i = 0; i < ret; i++) { smps[i]->capacity = (p->blocksz - sizeof(**smps)) / sizeof(smps[0]->data[0]); - smps[i]->pool = p; + smps[i]->pool_off = (char *) p - (char *) smps[i]; } return ret; @@ -27,8 +27,10 @@ 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++) - pool_put(smps[i]->pool, smps[i]); + for (int i = 0; i < cnt; i++) { + struct pool *p = (struct pool *) ((char *) smps[i] + smps[i]->pool_off); + pool_put(p, smps[i]); + } } int sample_get(struct sample *s) @@ -40,9 +42,11 @@ int sample_put(struct sample *s) { int prev = atomic_fetch_sub(&s->refcnt, 1); - /* Did we had the last refernce? */ - if (prev == 1) - pool_put(s->pool, s); + /* Did we had the last reference? */ + if (prev == 1) { + struct pool *p = (struct pool *) ((char *) s + s->pool_off); + pool_put(p, s); + } return prev - 1; } @@ -173,4 +177,4 @@ skip: if (fgets(line, sizeof(line), f) == NULL) goto skip; return sample_scan(line, s, fl); -} \ No newline at end of file +}