Refactor buffer.c: split and rename functions.