Jinoh Kang (@iamahuman) commented about server/mapping.c:
SHARED_WRITE_END;
}
+static int grow_session_mapping(void) +{
- unsigned int capacity;
- mem_size_t size;
- int unix_fd;
- void *tmp;
- capacity = session.shared->object_capacity * 3 / 2;
- size = offsetof(session_shm_t, objects[capacity]);
- size = (size + page_mask) & ~((mem_size_t)page_mask);
`grow_file` corrupts the file if we the size decreases, so other callers of `grow_file()` explicitly guards against this condition. I think we should do it here too:
```suggestion:-0+0 size = max(size, session_mapping->size); ```
Note that the "size ↔ capacity" translation is not a round-trip, and going from capacity back to size may actually result in the size decreasing if the element size becomes sufficiently greater than page size (plus certain overhead accounting for existing mapping size):
$$ \newcommand{\NP}{\mathsf{NewPages}} \newcommand{\OP}{\mathsf{OldPages}} \newcommand{\Ps}{\mathsf{PageSize}} \newcommand{\Hs}{\mathsf{HeadSize}} \newcommand{\Es}{\mathsf{EltSize}} \newcommand{\ceil}[1]{\mathopen{}\left\lceil#1\right\rceil\mathclose{}} \newcommand{\floor}[1]{\mathopen{}\left\lfloor#1\right\rfloor\mathclose{}} \newcommand{\paren}[1]{\mathopen{}\left(#1\right)\mathclose{}} \displaystyle \[1ex] \begin{align*} \NP&=\ceil{\paren{\tfrac32\floor{{\paren{\OP\cdot\Ps - \Hs}}/{\Es}}\cdot\Es + \Hs}/{\Ps}}\ &=\ceil{\paren{\tfrac32\paren{{\paren{\OP\cdot\Ps - \Hs}}/{\Es} - k}\cdot\Es + \Hs}/{\Ps}}\ &=\ceil{\OP - \paren{k\cdot\Es - \tfrac13\paren{\OP\cdot\Ps - \Hs}}/\paren{\tfrac23\cdot\Ps}}\ &<\OP \end{align*} $$
where
$$ \newcommand{\NP}{\mathsf{NewPages}} \newcommand{\OP}{\mathsf{OldPages}} \newcommand{\Ps}{\mathsf{PageSize}} \newcommand{\Hs}{\mathsf{HeadSize}} \newcommand{\Es}{\mathsf{EltSize}} \begin{align*} \mathbb{N}\ni\NP&=\text{New \texttt{session_mapping->size / page_size}}\ \mathbb{N}\ni\OP&=\text{Old \texttt{session_mapping->size / page_size}}\ \mathbb{N}\ni\Ps&=\text{\texttt{page_size}}>0\ \mathbb{N}\ni\Hs&=\text{\texttt{offsetof(session_shm_t, objects[0])}}>0\ \mathbb{N}\ni\Es&=\text{\texttt{sizeof(session_obj_t)}}>0\ 0&\leq k< 1&\text{(truncated fractional part)}\ k\cdot\Es &> \tfrac23\cdot\Ps + \tfrac13\left(\OP\cdot\Ps - \Hs\right) \ \left\lfloor\cdot\right\rfloor&=\mathrm{floor}!\left(\cdot\right)\ \left\lceil\cdot\right\rceil&=\mathrm{ceil}!\left(\cdot\right)\ \end{align*} $$