1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

make pool pointer in struct sample relative

This commit is contained in:
Georg Reinke 2017-04-04 12:32:48 +02:00
parent 5e850090cf
commit bc188e4a2d
2 changed files with 13 additions and 9 deletions

View file

@ -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_ */
#endif /* _SAMPLE_H_ */

View file

@ -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);
}
}