mongodb/mongo-c-driver — pull request #93 by noa-- (2013-03-19 18:32:57+00:00)
Title: BSON: Smooth out memory handling of data buffers

BSON object changes:

- Add flag `ownsData` to BSON object which controls whether `bson_destroy`
deallocates the data block and whether `bson_ensure_space` may reallocate
it
- Add error flag `BSON_DOES_NOT_OWN_DATA`, set when
` bson_ensure_space( )` needs to expand an unowned data block

API changes:

- Rename `bson_init_data` to `bson_init_unfinished_data`
(will anyone even want to use this?) and add parameters for the size
and ownership of the block
- Add parameter to `bson_init_finished_data` for the ownership of the block
- Add `bson_init_finished_data_with_copy( )` to more easily create
subobjects which can outlive their parents
- Rename iterator functions which initialize bson objects and add
a parameter to enable copying subobject data to the heap:
`bson_iterator_code_scope( )` is now `bson_iterator_code_scope_init( )`
and
`bson_iterator_subobject( )` is now `bson_iterator_subobject_init( )`.
Fix up these functions to either copy the blocks or return BSON objects
with unowned blocks. Same for `gridfile_get_metadata( )`.

Minor changes:

- Return status code instead of void from all init functions (since
`bson_malloc` can return 0)
- Replace `_bson_reset` with `_bson_zero`, and call it from all init functions
- Factor `_bson_init_size` into `bson_init_size`
- Remove `bson_shared_empty_data` check in `bson_destroy`
- Add helper function `bson_finished_data_size( )` to get the size of a
finalized BSON data block, and use it where applicable
- Rewrite `bson_empty( )` and `bson_copy( )`