# f_dump_buffer

Character storage and a cursor, the Fortran derived type behind the C
`f90_f_dump_buffer_*` handles and the C++ `Futile::FDumpBuffer` class.

```fortran
use futile                                  ! or:
use f_utils, only: f_dump_buffer
```

## Definition

Rendered from `flib/f_utils.f90`; the C example is part of the type
documentation in the source and is compiled and run by the `dump_buffer`
regression (`tests/c-bindings/dump_buffer.c`).

- ### *type*  f_utils/f_dump_buffer

  Character storage and cursor metadata for a prospective buffered writer.
  Native Fortran pointer assignment remains shallow: the caller manages
  that storage. C/C++ binding constructors instead make an owned copy,
  and their copy constructor makes a separate allocation. Destroy every
  binding-created object once with its matching binding destructor.
  Empty objects have a null buffer and a zero cursor. The string
  constructor preserves the supplied cursor verbatim; it is not a length.
  The optional f_write buffer branch is an unfinished prototype and is
  not an output/flush API. The tested C binding example follows.
  ```c
  original = f90_f_dump_buffer_type_new(input, 9);
  memset(input, '?', strlen(input));
  free(input);                         /* The buffer must retain the YAML text. */
  check(original, expected, 9);
  clone = f90_f_dump_buffer_copy_constructor(F_TYPE(original));
  FC_FUNC_(dump_buffer_mutate, DUMP_BUFFER_MUTATE)(F_TYPE(original));
  check(clone, expected, 9);           /* Mutating the original cannot change its copy. */
  f90_f_dump_buffer_free(original);
  check(clone, expected, 9);           /* The copy outlives the original. */
  second = f90_f_dump_buffer_copy_constructor(F_TYPE(clone));
  f90_f_dump_buffer_free(second);      /* The opposite destruction order is valid too. */
  check(clone, expected, 9);
  f90_f_dump_buffer_free(clone);
  ```

  * **Type fields:**
    * % buf (\*) [pointer/optional/default=>null) *[**character(len=1)* *]*
    * % ipos *[**integer* *,* *optional/default=0* *]*

## Notes

### Binding ownership

The C `f90_f_dump_buffer_*` functions and C++ `Futile::FDumpBuffer`
constructors copy their input character storage. Copies and C++ assignments
have independent storage: either object may be destroyed first, and changing
the source does not change its copy. Each returned C handle must be freed
exactly once; the C destructor takes its handle by value, so the caller’s old
handle remains invalid after freeing it. The C++ raw-handle constructor adopts
ownership and must not be used to wrap the same handle twice. Initialize
Futile before construction and destroy all objects before finalizing.

### String constructor

The string constructor copies bytes up to the C NUL terminator, excluding that
terminator. Null and empty strings produce null payload pointers. It preserves
the supplied `ipos` cursor metadata verbatim; this value is not the length
of the input. The default empty constructor sets `ipos=0`.

### Fortran assignment

Native Fortran assignment of this pointer-containing type remains shallow;
Fortran callers retain responsibility for their own storage.

### Prototype status and test coverage

The active `dump_buffer` C/C++ regression uses a 16 KiB quoted YAML scalar,
checks every copied byte, parses it with libyaml, and verifies mutation, both
destruction orders, copy assignment, self-assignment and empty buffers. Its
allocation-ledger check rejects remaining tracked buffer allocations. The
optional `f_write(buffer=...)` branch remains an unfinished prototype; these
tests validate binding storage and lifetime, not buffered YAML output or
flushing.

## See also

* [f_subptr_str](f_subptr_str.md) – borrowed character views of existing strings, without copying.
* [f_sizeof](f_sizeof.md) – payload bytes of character storage.
* [Memory handling](../../memory.md) – tracked allocation and the allocation ledger.
