Allocating memory for queued writes


I am trying to implement long writes on the gatt server side. My implementation is stack handled, so I am going for the "GATTS Queued Writes: Stack handled, one or more attributes require authorization" sequence. So far everything has been working fine, whenever I get BLE_EVT_USER_MEM_REQUEST I reply with a large memory block, and long writes from the client side has worked without an issue.

My query is regarding memory optimisation, as I would like to only allocate enough memory for a write to one attribute in the gatt server. In other words, for an attribute of length 20 bytes, I'll need to allocate only 20+X bytes of memory for a queued write to be successful. Our app supports variable length attributes, so the amount of memory given to sd_ble_user_mem_reply() will vary. So my question is, how do I calculate how much memory is needed to perform a long write on one attribute?

We're using softdevice S132 v3.0.0 on nRF52.