Efficient algorithms for buffer allocation