# question about the calculation of overload bytes in function add_extent()

Sun Apr 5 10:06:38 CEST 2020

```Question about the calculation of overload memory in function add_extent()
in heapmem.c line:607(the source is listed below)
In the source code, notes that"overhead memory we need for storing our meta
data as calculated by HEAPMEM_ARENA_SIZE()".(The source of
HEAPMEM_ARENA_SIZE macro is also listed below.)
@@To simplify the problem，we ignore the alignment.@@
>From the HEAPMEM_ARENA_SIZE macro in code, we can draw the conclusion
HEAPMEM_ARENA_SIZE(size)=__size+sizeof(struct
heapmem_extent)+(__size/HEAPMEM_PAGE_SIZE)*HEAPMEM_PGMAP_BYTES
So, the overhead i think is：
overhead = HEAPMEM_ARENA_SIZE(size)-size = sizeof(struct
heapmem_extent)+(__size/HEAPMEM_PAGE_SIZE)*HEAPMEM_PGMAP_BYTES
If we use the representation explained in the source code, then
overhead = o = e+(a*p)/m
It's not equals to the calculation in the source, which is
o = ((a * m + e * p) / (p + m))

@representation explained in source code(the whole source code is listed
below)
* o = overhead
* e = sizeof(heapmem_extent)
* p = HEAPMEM_PAGE_SIZE
* m = HEAPMEM_PGMAP_BYTES  =sizeof(struct heapmem_pgentry)

Note: "__size/HEAPMEM_PAGE_SIZE" equals to "__size>>
HEAPMEM_PAGE_SHIFT"(which is the orignal code)

@source code of macro HEAPMEM_ARENA_SIZE
#define __HEAPMEM_MAP_SIZE(__nrpages) \
((__nrpages) * HEAPMEM_PGMAP_BYTES)
/*note by myslef :(__size)>>HEAPMEM_PAGE_SHIFT is the number of pages of
memory pool
__HEAPMEM_MAP_SIZE((__size) >> HEAPMEM_PAGE_SHIFT)calculats the memory size
for storing the struct of heapmem_pgentry。*/
#define __HEAPMEM_ARENA_SIZE(__size) \
(__size + \
__align_to(sizeof(struct heapmem_extent) + \
__HEAPMEM_MAP_SIZE((__size) >> HEAPMEM_PAGE_SHIFT), \
HEAPMEM_MIN_ALIGN))

/*
* Calculate the minimal size of the memory arena needed to contain a
* heap of __user_size bytes, including our meta data for managing it.
```

More information about the Xenomai mailing list