Memory-safe contexts and memory buffers