Beispiel #1
0
static void *
bson_yajl_realloc_func (void   *ctx,
                        void   *ptr,
                        size_t  sz)
{
   return bson_realloc (ptr, sz);
}
Beispiel #2
0
int bson_ensure_space( bson *b, const size_t bytesNeeded ) {
    size_t pos = b->cur - b->data;
    char *orig = b->data;
    int new_size;

    check_mongo_object( (void*)b );

    if ( (int)pos + bytesNeeded <= (size_t)b->dataSize )
        return BSON_OK;

    new_size = (int)(1.5 * ( b->dataSize + bytesNeeded ));

    if( new_size < b->dataSize ) {
        if( ( b->dataSize + bytesNeeded ) < INT_MAX )
            new_size = INT_MAX;
        else {
            b->err = BSON_SIZE_OVERFLOW;
            return BSON_ERROR;
        }
    }

    b->data = (char*) bson_realloc( b->data, new_size );
    if ( !b->data )
        bson_fatal_msg( !!b->data, "realloc() failed" );

    b->dataSize = new_size;
    b->cur += b->data - orig;

    return BSON_OK;
}
Beispiel #3
0
void *
bson_realloc_ctx (void   *mem,        /* IN */
                  size_t  num_bytes,  /* IN */
                  void   *ctx)        /* IN */
{
   return bson_realloc (mem, num_bytes);
}
Beispiel #4
0
int bson_ensure_space( bson *b, const size_t bytesNeeded ) {
    size_t pos = _bson_position(b);
    char *orig = b->data;
    int new_size;

    if ( pos + bytesNeeded <= (size_t) b->dataSize )
        return BSON_OK;

    new_size = (int) ( 1.5 * ( b->dataSize + bytesNeeded ) );

    if( new_size < b->dataSize ) {
        if( ( b->dataSize + bytesNeeded ) < INT_MAX )
            new_size = INT_MAX;
        else {
            b->err = BSON_SIZE_OVERFLOW;
            return BSON_ERROR;
        }
    }

    if ( ! b->ownsData ) {
        b->err = BSON_DOES_NOT_OWN_DATA;
        return BSON_ERROR;
    }

    b->data = bson_realloc( b->data, new_size );
    if ( !b->data )
        bson_fatal_msg( !!b->data, "realloc() failed" );

    b->dataSize = new_size;
    b->cur += b->data - orig;

    return BSON_OK;
}
Beispiel #5
0
static int _bson_append_grow_stack( bson * b ) {
    if ( !b->stackPtr ) {
        // If this is an empty bson structure, initially use the struct-local (fixed-size) stack
        b->stackPtr = b->stack;
        b->stackSize = sizeof( b->stack ) / sizeof( size_t );
    }
    else if ( b->stackPtr == b->stack ) {
        // Once we require additional capacity, set up a dynamically resized stack
        size_t *new_stack = ( size_t * ) bson_malloc( 2 * sizeof( b->stack ) );
        if ( new_stack ) {
            b->stackPtr = new_stack;
            b->stackSize = 2 * sizeof( b->stack ) / sizeof( size_t );
            memcpy( b->stackPtr, b->stack, sizeof( b->stack ) );
        }
        else {
            return BSON_ERROR;
        }
    }
    else {
        // Double the capacity of the dynamically-resized stack
        size_t *new_stack = ( size_t * ) bson_realloc( b->stackPtr, ( b->stackSize * 2 ) * sizeof( size_t ) );
        if ( new_stack ) {
            b->stackPtr = new_stack;
            b->stackSize *= 2;
        }
        else {
            return BSON_ERROR;
        }
    }
    return BSON_OK;
}
Beispiel #6
0
char *
bson_strdupv_printf (const char *format, /* IN */
                     va_list     args)   /* IN */
{
   va_list my_args;
   char *buf;
   int len = 32;
   int n;

   bson_return_val_if_fail (format, NULL);

   buf = bson_malloc0 (len);

   while (true) {
      va_copy (my_args, args);
      n = bson_vsnprintf (buf, len, format, my_args);
      va_end (my_args);

      if (n > -1 && n < len) {
         return buf;
      }

      if (n > -1) {
         len = n + 1;
      } else {
         len *= 2;
      }

      buf = bson_realloc (buf, len);
   }
}
Beispiel #7
0
void
bson_string_truncate (bson_string_t *string, /* IN */
                      uint32_t       len)    /* IN */
{
   uint32_t alloc;

   bson_return_if_fail (string);
   bson_return_if_fail (len < INT_MAX);

   alloc = len + 1;

   if (alloc < 16) {
      alloc = 16;
   }

   if (!bson_is_power_of_two (alloc)) {
      alloc = (uint32_t)bson_next_power_of_two ((size_t)alloc);
   }

   string->str = bson_realloc (string->str, alloc);
   string->alloc = alloc;
   string->len = len;

   string->str [string->len] = '\0';
}
Beispiel #8
0
int bson_ensure_space( bson *b, const int bytesNeeded ) {
    int pos = b->cur - b->data;
    char *orig = b->data;
    int new_size;

    if ( pos + bytesNeeded <= b->dataSize )
        return BSON_OK;

    new_size = 1.5 * ( b->dataSize + bytesNeeded );

    if( new_size < b->dataSize ) {
        if( ( b->dataSize + bytesNeeded ) < INT_MAX )
            new_size = INT_MAX;
        else {
            b->err = BSON_SIZE_OVERFLOW;
            return BSON_ERROR;
        }
    }

    b->data = bson_realloc( b->data, new_size );
    if ( !b->data )
        bson_fatal_msg( !!b->data, "realloc() failed" );

    b->dataSize = new_size;
    b->cur += b->data - orig;

    return BSON_OK;
}
Beispiel #9
0
static void *
test_bson_writer_custom_realloc_helper (void  *mem,
                                        size_t num_bytes,
                                        void  *ctx)
{
   int * x = (int *)ctx;

   *x = *x + 1;
   return bson_realloc(mem, num_bytes);
}
Beispiel #10
0
void
bson_string_append_c (bson_string_t *string,
                      char           c)
{
   bson_return_if_fail(string);

   string->str = bson_realloc(string->str, string->len + 1);
   string->str[string->len-1] = c;
   string->len++;
   string->str[string->len-1] = '\0';
}
Beispiel #11
0
static void
_bson_reader_fd_grow_buffer (bson_reader_fd_t *reader)
{
   bson_size_t size;

   bson_return_if_fail (reader);

   size = reader->len * 2;
   reader->data = bson_realloc (reader->data, size);
   reader->len = size;
}
Beispiel #12
0
static void
_bson_reader_handle_grow_buffer (bson_reader_handle_t *reader) /* IN */
{
   size_t size;

   bson_return_if_fail (reader);

   size = reader->len * 2;
   reader->data = bson_realloc (reader->data, size);
   reader->len = size;
}
Beispiel #13
0
void
bson_string_append (bson_string_t *string,
                    const char    *str)
{
   bson_uint32_t len;

   bson_return_if_fail(string);
   bson_return_if_fail(str);

   len = strlen(str);
   string->str = bson_realloc(string->str, string->len + len);
   memcpy(&string->str[string->len - 1], str, len);
   string->len += len;
   string->str[string->len-1] = '\0';
}
Beispiel #14
0
void
bson_string_append (bson_string_t *string, /* IN */
                    const char    *str)    /* IN */
{
   uint32_t len;

   bson_return_if_fail (string);
   bson_return_if_fail (str);

   len = (uint32_t)strlen (str);

   if ((string->alloc - string->len - 1) < len) {
      string->alloc += len;
      if (!bson_is_power_of_two (string->alloc)) {
         string->alloc = (uint32_t)bson_next_power_of_two ((size_t)string->alloc);
      }
      string->str = bson_realloc (string->str, string->alloc);
   }

   memcpy (string->str + string->len, str, len);
   string->len += len;
   string->str [string->len] = '\0';
}
Beispiel #15
0
void
_mongoc_array_append_vals (mongoc_array_t *array,
                           const void     *data,
                           uint32_t   n_elements)
{
   size_t len;
   size_t off;
   size_t next_size;

   BSON_ASSERT (array);
   BSON_ASSERT (data);

   off = array->element_size * array->len;
   len = (size_t)n_elements * array->element_size;
   if ((off + len) > array->allocated) {
      next_size = bson_next_power_of_two(off + len);
      array->data = (void *)bson_realloc(array->data, next_size);
      array->allocated = next_size;
   }

   memcpy((uint8_t *)array->data + off, data, len);

   array->len += n_elements;
}
Beispiel #16
0
void gridfile_write_buffer( gridfile *gfile, const char *data,
    gridfs_offset length ) {

    int bytes_left = 0;
    int data_partial_len = 0;
    int chunks_to_write = 0;
    char *buffer;
    bson *oChunk;
    gridfs_offset to_write = length + gfile->pending_len;

    if ( to_write < DEFAULT_CHUNK_SIZE ) { /* Less than one chunk to write */
        if( gfile->pending_data ) {
            gfile->pending_data = ( char * )bson_realloc( ( void * )gfile->pending_data, gfile->pending_len + to_write );
            memcpy( gfile->pending_data + gfile->pending_len, data, length );
        } else if ( to_write > 0 ) {
            gfile->pending_data = ( char * )bson_malloc( to_write );
            memcpy( gfile->pending_data, data, length );
        }
        gfile->pending_len += length;

    } else { /* At least one chunk of data to write */

        /* If there's a pending chunk to be written, we need to combine
         * the buffer provided up to DEFAULT_CHUNK_SIZE.
         */
        if ( gfile->pending_len > 0 ) {
            chunks_to_write = to_write / DEFAULT_CHUNK_SIZE;
            bytes_left = to_write % DEFAULT_CHUNK_SIZE;

            data_partial_len = DEFAULT_CHUNK_SIZE - gfile->pending_len;
            buffer = ( char * )bson_malloc( DEFAULT_CHUNK_SIZE );
            memcpy( buffer, gfile->pending_data, gfile->pending_len );
            memcpy( buffer + gfile->pending_len, data, data_partial_len );

            oChunk = chunk_new( gfile->id, gfile->chunk_num, buffer, DEFAULT_CHUNK_SIZE );
            mongo_insert( gfile->gfs->client, gfile->gfs->chunks_ns, oChunk );
            chunk_free( oChunk );
            gfile->chunk_num++;
            gfile->length += DEFAULT_CHUNK_SIZE;
            data += data_partial_len;

            chunks_to_write--;

            bson_free( buffer );
        }

        while( chunks_to_write > 0 ) {
            oChunk = chunk_new( gfile->id, gfile->chunk_num, data, DEFAULT_CHUNK_SIZE );
            mongo_insert( gfile->gfs->client, gfile->gfs->chunks_ns, oChunk );
            chunk_free( oChunk );
            gfile->chunk_num++;
            chunks_to_write--;
            gfile->length += DEFAULT_CHUNK_SIZE;
            data += DEFAULT_CHUNK_SIZE;
        }

        bson_free( gfile->pending_data );

        /* If there are any leftover bytes, store them as pending data. */
        if( bytes_left == 0 )
            gfile->pending_data = NULL;
        else {
            gfile->pending_data = ( char * )bson_malloc( bytes_left );
            memcpy( gfile->pending_data, data, bytes_left );
        }

        gfile->pending_len = bytes_left;
    }
}
/**
 * _mongoc_buffer_fill:
 * @buffer: A mongoc_buffer_t.
 * @stream: A stream to read from.
 * @min_bytes: The minumum number of bytes to read.
 * @error: A location for a bson_error_t or NULL.
 *
 * Attempts to fill the entire buffer, or at least @min_bytes.
 *
 * Returns: The number of buffered bytes, or -1 on failure.
 */
ssize_t
_mongoc_buffer_fill (mongoc_buffer_t *buffer,
                     mongoc_stream_t *stream,
                     size_t           min_bytes,
                     int32_t          timeout_msec,
                     bson_error_t    *error)
{
    ssize_t ret;
    size_t avail_bytes;

    ENTRY;

    bson_return_val_if_fail(buffer, false);
    bson_return_val_if_fail(stream, false);
    bson_return_val_if_fail(min_bytes >= 0, false);

    BSON_ASSERT(buffer->data);
    BSON_ASSERT(buffer->datalen);

    if (min_bytes <= buffer->len) {
        RETURN (buffer->len);
    }

    min_bytes -= buffer->len;

    if (buffer->len) {
        memmove (&buffer->data[0], &buffer->data[buffer->off], buffer->len);
    }

    buffer->off = 0;

    if (!SPACE_FOR (buffer, min_bytes)) {
        buffer->datalen = bson_next_power_of_two ((uint32_t)(buffer->len + min_bytes));
        buffer->data = bson_realloc (buffer->data, buffer->datalen);
    }

    avail_bytes = buffer->datalen - buffer->len;

    ret = mongoc_stream_read (stream,
                              &buffer->data[buffer->off + buffer->len],
                              avail_bytes, min_bytes, timeout_msec);

    if (ret == -1) {
        bson_set_error (error,
                        MONGOC_ERROR_STREAM,
                        MONGOC_ERROR_STREAM_SOCKET,
                        "Failed to buffer %u bytes within %d milliseconds.",
                        (unsigned)min_bytes, (int)timeout_msec);
        RETURN (-1);
    }

    buffer->len += ret;

    if (buffer->len < min_bytes) {
        bson_set_error (error,
                        MONGOC_ERROR_STREAM,
                        MONGOC_ERROR_STREAM_SOCKET,
                        "Could only buffer %u of %u bytes in %d milliseconds.",
                        (unsigned)buffer->len,
                        (unsigned)min_bytes,
                        (int)timeout_msec);
        RETURN (-1);
    }

    RETURN (buffer->len);
}