void * bson_realloc (void *mem, /* IN */ size_t num_bytes) /* IN */ { /* * Not all platforms are guaranteed to free() the memory if a call to * realloc() with a size of zero occurs. Windows, Linux, and FreeBSD do, * however, OS X does not. */ if (BSON_UNLIKELY (num_bytes == 0)) { bson_free (mem); return NULL; } #ifdef __APPLE__ mem = reallocf (mem, num_bytes); #else mem = realloc (mem, num_bytes); #endif if (BSON_UNLIKELY (!mem)) { abort (); } return mem; }
bool _mongoc_rpc_scatter_reply_header_only (mongoc_rpc_t *rpc, const uint8_t *buf, size_t buflen) { if (BSON_UNLIKELY (buflen < sizeof (mongoc_rpc_reply_header_t))) { return false; } return _mongoc_rpc_scatter_reply_header (&rpc->reply_header, buf, buflen); }
void * bson_malloc0 (size_t num_bytes) /* IN */ { void *mem = NULL; if (BSON_LIKELY (num_bytes)) { if (BSON_UNLIKELY (!(mem = gMemVtable.calloc (1, num_bytes)))) { abort (); } } return mem; }
void * bson_malloc (size_t num_bytes) /* IN */ { void *mem = NULL; if (BSON_LIKELY (num_bytes)) { if (BSON_UNLIKELY (!(mem = gMemVtable.malloc (num_bytes)))) { fprintf (stderr, "Failure to allocate memory in bson_malloc(). errno: %d.\n", errno); abort (); } } return mem; }
void * bson_realloc (void *mem, /* IN */ size_t num_bytes) /* IN */ { /* * Not all platforms are guaranteed to free() the memory if a call to * realloc() with a size of zero occurs. Windows, Linux, and FreeBSD do, * however, OS X does not. */ if (BSON_UNLIKELY (num_bytes == 0)) { gMemVtable.free (mem); return NULL; } mem = gMemVtable.realloc (mem, num_bytes); if (BSON_UNLIKELY (!mem)) { fprintf (stderr, "Failure to re-allocate memory in bson_realloc(). errno: %d.\n", errno); abort (); } return mem; }
bool mongoc_gridfs_file_error (mongoc_gridfs_file_t *file, bson_error_t *error) { BSON_ASSERT (file); BSON_ASSERT (error); if (BSON_UNLIKELY (file->error.domain)) { bson_set_error (error, file->error.domain, file->error.code, "%s", file->error.message); RETURN (true); } RETURN (false); }
bool _mongoc_rpc_scatter (mongoc_rpc_t *rpc, const uint8_t *buf, size_t buflen) { mongoc_opcode_t opcode; bson_return_val_if_fail(rpc, false); bson_return_val_if_fail(buf, false); bson_return_val_if_fail(buflen, false); memset (rpc, 0, sizeof *rpc); if (BSON_UNLIKELY(buflen < 16)) { return false; } if (!_mongoc_rpc_scatter_header(&rpc->header, buf, 16)) { return false; } opcode = BSON_UINT32_FROM_LE(rpc->header.opcode); switch (opcode) { case MONGOC_OPCODE_REPLY: return _mongoc_rpc_scatter_reply(&rpc->reply, buf, buflen); case MONGOC_OPCODE_MSG: return _mongoc_rpc_scatter_msg(&rpc->msg, buf, buflen); case MONGOC_OPCODE_UPDATE: return _mongoc_rpc_scatter_update(&rpc->update, buf, buflen); case MONGOC_OPCODE_INSERT: return _mongoc_rpc_scatter_insert(&rpc->insert, buf, buflen); case MONGOC_OPCODE_QUERY: return _mongoc_rpc_scatter_query(&rpc->query, buf, buflen); case MONGOC_OPCODE_GET_MORE: return _mongoc_rpc_scatter_get_more(&rpc->get_more, buf, buflen); case MONGOC_OPCODE_DELETE: return _mongoc_rpc_scatter_delete(&rpc->delete, buf, buflen); case MONGOC_OPCODE_KILL_CURSORS: return _mongoc_rpc_scatter_kill_cursors(&rpc->kill_cursors, buf, buflen); default: MONGOC_WARNING("Unknown rpc type: 0x%08x", opcode); return false; } }
void bson_string_append_c (bson_string_t *string, /* IN */ char c) /* IN */ { char cc[2]; BSON_ASSERT (string); if (BSON_UNLIKELY (string->alloc == (string->len + 1))) { cc [0] = c; cc [1] = '\0'; bson_string_append (string, cc); return; } string->str [string->len++] = c; string->str [string->len] = '\0'; }
bson_bool_t mongoc_gridfs_file_error (mongoc_gridfs_file_t *file, bson_error_t *error) { BSON_ASSERT(file); BSON_ASSERT(error); if (BSON_UNLIKELY(file->failed)) { bson_set_error(error, file->error.domain, file->error.code, "%s", file->error.message); RETURN(TRUE); } RETURN(FALSE); }
bson_bool_t _mongoc_cursor_error (mongoc_cursor_t *cursor, bson_error_t *error) { ENTRY; bson_return_val_if_fail(cursor, FALSE); if (BSON_UNLIKELY(cursor->failed)) { bson_set_error(error, cursor->error.domain, cursor->error.code, "%s", cursor->error.message); RETURN(TRUE); } RETURN(FALSE); }
bson_bool_t _mongoc_cursor_next (mongoc_cursor_t *cursor, const bson_t **bson) { const bson_t *b; bson_bool_t eof; ENTRY; BSON_ASSERT(cursor); if (cursor->client->in_exhaust && ! cursor->in_exhaust) { bson_set_error(&cursor->error, MONGOC_ERROR_CLIENT, MONGOC_ERROR_CLIENT_IN_EXHAUST, "Another cursor derived from this client is in exhaust."); cursor->failed = TRUE; RETURN(FALSE); } if (bson) { *bson = NULL; } if (cursor->limit && cursor->count >= cursor->limit) { return FALSE; } /* * Short circuit if we are finished already. */ if (BSON_UNLIKELY(cursor->done)) { RETURN(FALSE); } /* * Check to see if we need to send a GET_MORE for more results. */ if (!cursor->sent) { if (!_mongoc_cursor_query(cursor)) { RETURN(FALSE); } } else if (BSON_UNLIKELY(cursor->end_of_event)) { if (!_mongoc_cursor_get_more(cursor)) { RETURN(FALSE); } } /* * Read the next BSON document from the event. */ eof = FALSE; b = bson_reader_read(cursor->reader, &eof); cursor->end_of_event = eof; cursor->done = cursor->end_of_event && ( (cursor->in_exhaust && !cursor->rpc.reply.cursor_id) || (!b && !(cursor->flags & MONGOC_QUERY_TAILABLE_CURSOR)) ); /* * Do a supplimental check to see if we had a corrupted reply in the * document stream. */ if (!b && !eof) { cursor->failed = TRUE; bson_set_error(&cursor->error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_PROTOCOL_INVALID_REPLY, "The reply was corrupt."); RETURN(FALSE); } if (bson) { *bson = b; } RETURN(!!b); }
bool _mongoc_cursor_next (mongoc_cursor_t *cursor, const bson_t **bson) { const bson_t *b; bool eof; ENTRY; BSON_ASSERT (cursor); if (bson) { *bson = NULL; } if (cursor->done || cursor->failed) { bson_set_error (&cursor->error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_CURSOR_INVALID_CURSOR, "Cannot advance a completed or failed cursor."); RETURN (false); } /* * We cannot proceed if another cursor is receiving results in exhaust mode. */ if (cursor->client->in_exhaust && !cursor->in_exhaust) { bson_set_error (&cursor->error, MONGOC_ERROR_CLIENT, MONGOC_ERROR_CLIENT_IN_EXHAUST, "Another cursor derived from this client is in exhaust."); cursor->failed = true; RETURN (false); } /* * If we reached our limit, make sure we mark this as done and do not try to * make further progress. */ if (cursor->limit && cursor->count >= cursor->limit) { cursor->done = true; RETURN (false); } /* * Try to read the next document from the reader if it exists, we might * get NULL back and EOF, in which case we need to submit a getmore. */ if (cursor->reader) { eof = false; b = bson_reader_read (cursor->reader, &eof); cursor->end_of_event = eof; if (b) { GOTO (complete); } } /* * Check to see if we need to send a GET_MORE for more results. */ if (!cursor->sent) { if (!_mongoc_cursor_query (cursor)) { RETURN (false); } } else if (BSON_UNLIKELY (cursor->end_of_event) && cursor->rpc.reply.cursor_id) { if (!_mongoc_cursor_get_more (cursor)) { RETURN (false); } } eof = false; b = bson_reader_read (cursor->reader, &eof); cursor->end_of_event = eof; complete: cursor->done = (cursor->end_of_event && ((cursor->in_exhaust && !cursor->rpc.reply.cursor_id) || (!b && !(cursor->flags & MONGOC_QUERY_TAILABLE_CURSOR)))); /* * Do a supplimental check to see if we had a corrupted reply in the * document stream. */ if (!b && !eof) { cursor->failed = true; bson_set_error (&cursor->error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_PROTOCOL_INVALID_REPLY, "The reply was corrupt."); RETURN (false); } if (bson) { *bson = b; } RETURN (!!b); }