Please use memcpy(temp_buffer, buffer->buffer, buffer->used); rather than looping.  It is certainly more efficient.