This is consistent with the logic in mp_get_buffer, and the code here is an inlined version of that function.