Refactor buffer_resize_if_needed function in buffer.c