static VALUE buf_append2()

in ext/puma_http11/io_buffer.c [69:113]


static VALUE buf_append2(int argc, VALUE* argv, VALUE self) {
  struct buf_int* b;
  size_t used, new_size;
  int i;
  VALUE str;

  Data_Get_Struct(self, struct buf_int, b);

  used = b->cur - b->top;
  new_size = used;

  for(i = 0; i < argc; i++) {
    StringValue(argv[i]);

    str = argv[i];

    new_size += RSTRING_LEN(str);
  }

  if(new_size > b->size) {
    size_t n = b->size + (b->size / 2);
    uint8_t* top;
    uint8_t* old;

    new_size = (n > new_size ? n : new_size + BUF_TOLERANCE);

    top = ALLOC_N(uint8_t, new_size);
    old = b->top;
    memcpy(top, old, used);
    b->top = top;
    b->cur = top + used;
    b->size = new_size;
    xfree(old);
  }

  for(i = 0; i < argc; i++) {
    long str_len;
    str = argv[i];
    str_len = RSTRING_LEN(str);
    memcpy(b->cur, RSTRING_PTR(str), str_len);
    b->cur += str_len;
  }

  return self;
}