BSON_NEW_FROM_BUFFER(3)

πŸ’… BSON_NEW_FROM_BUFFER(3) πŸ’…

okay bestie, welcome to the libbson corner of the manual β€” this one's the bson_new_from_buffer page and honestly it's giving "wrap your existing buffer in a shiny new bson_t" energy ✨


✨ SYNOPSIS ✨

bson_t *
bson_new_from_buffer (uint8_t **buf,
                      size_t *buf_len,
                      bson_realloc_func realloc_func,
                      void *realloc_func_ctx);

πŸŽ€ PARAMETERS πŸŽ€

  • buf β€” an out-pointer pointing at a buffer that's holding a serialized BSON document, or it points at NULL. yep, either situation is legal, she's flexible πŸ’…
  • buf_len β€” an out-pointer to how long that buffer is, in bytes πŸ“
  • realloc_func β€” optional bson_realloc_func that gets used for reallocating the buffer. optional!! but iconic when you bring it 🫢
  • realloc_func_ctx β€” optional pointer that'll be handed to realloc_func as the ctx argument

πŸ¦‹ DESCRIPTION πŸ¦‹

So basically: this function creates a brand-new bson_t out of the data you already have. no copying drama, just vibes 🌸

The realloc_func, if you provide it, gets called to resize buf whenever the document later gets expanded β€” like, say, when you call one of the bson_append functions. she grows, she calls your allocator, that's the whole deal ✨

If *buf starts out as NULL, then it gets allocated (using realloc_func, or the default allocator if you didn't pass one) and initialized with an empty BSON document. and then *buf_len is set to 5 β€” yup, 5, that's literally the size of an empty document, no cap πŸ”₯

If *buf is initially non-NULL, then *buf_len must be at least as large as the embedded length sitting in the first four bytes of the BSON data in *buf β€” but it's totally fine if it's bigger than that. any leftover buffer capacity can be used later to grow the BSON document. room to grow = main character energy πŸ‘‘

lowkey important: that "at least as large as" bit is a real requirement, not a suggestion. the embedded length in the first four bytes is the vibe check your buffer has to pass πŸ’€

⭐ RETURNS ⭐

A newly-allocated bson_t if everything slayed, or NULL if it didn't 😭


🌈 AUTHOR 🌈

MongoDB, Inc β€” love them for this fr fr πŸ’œ


2009-present, MongoDB, Inc.


and that's the whole page, bestie πŸŽ€ this was BSON_NEW_FROM_BUFFER(3) from the libbson manual, version 2.5.6, dated Oct 09, 2026 β€” go forth and wrap those buffers πŸ‘‘πŸ’–

πŸ“¦ where this page comes from