Buffer memory strategy

Rewrote the whole memory allocation strategy for buffer.
Now data is preserved even in case of failures.
Printf now returns 0 to signal that a retry is needed.
Memory is allocated as it is needed, not by increments.
RefCount memory allocation

Forgot to check one memory allocation when attaching ref_counts.
Memory cap for buffer

Each buffer instance has a memory cap, thus avoiding using all the memory in
one go.
Boundary condition fix

If the size was exactly BUFFER_SIZE, then we were susceptible of
suffering a buffer overrun. This commit fixes that and introduces
unit tests to make sure this never happens.
Merge pull request #295 from cduclos/master

Buffer implementation with a better memory allocation.