Exemplo n.º 1
0
void caml_resize_extensible_buffer(struct caml_extensible_buffer *b,
                                   size_t new_used_size){
  struct caml_extensible_buffer_shape *shape = b->shared_shape;

  /* Add enough padding bytes in the end to make the next element,
     potentially a pointer, at least word-aligned.  This is required
     on some architectures, and yields better performance in any case.
     This element is already guaranteed to begin at a word-aligned
     address. */
  const size_t word_size = sizeof(void*);
  const size_t misalignment = new_used_size % word_size;
  if(misalignment != 0)
    new_used_size += word_size - misalignment;

  /* Make the allocated buffer large enough, if needed: */
  caml_reallocate_extensible_buffer_if_needed(b, new_used_size);

  /* Update the used size and initialize the newly-used part, if any: */
  Lock(shape);
  if(new_used_size > shape->used_size){
    memset(((char*)b->array) + shape->used_size,
           shape->initial_value,
           new_used_size - shape->used_size);
    //fprintf(stderr, "+++++++++++++Initialized with %i from %i to %i\n", initial_value, b->used_size, new_used_size - 1);
  }
  shape->used_size = new_used_size;
  Unlock(shape);
  b->local_used_size = new_used_size;
}
Exemplo n.º 2
0
size_t caml_allocate_from_extensible_buffer(struct caml_extensible_buffer *b,
                                            size_t new_element_size){
  const size_t word_size = sizeof(void*);
  struct caml_extensible_buffer_shape *shape = b->shared_shape;
  size_t beginning_of_this_element;

  Lock(shape);
  beginning_of_this_element = (size_t)shape->used_size;

  /* Reallocate the global array if needed: */
  caml_reallocate_extensible_buffer_if_needed(b, beginning_of_this_element + new_element_size);
  //while((beginning_of_this_element + new_element_size) > b->allocated_size)
  //  caml_reallocate_extensible_buffer(b, b->allocated_size * 2 + 1, initial_value);

  long new_used_size = shape->used_size + new_element_size;

  /* Make sure the used size remains a multiple of the word size, so
     that the next allocated object will be word-aligned; that's
     required on some architectures, and not mandatory but important
     for performance on others: */
  size_t misalignment = new_used_size % word_size;
  if(misalignment != 0)
    new_used_size += word_size - misalignment;

  /* Fill the newly-allocated part: */
  memset(((char*)b->array) + shape->used_size, shape->initial_value, new_used_size - shape->used_size);
  //fprintf(stderr, "+++++++++++++Initialized with %i from %i to %i\n", initial_value, b->used_size, new_used_size - 1);

  shape->used_size = new_used_size;
  Unlock(shape);
  b->local_used_size = new_used_size;

  //printf("%p->used_size is now %i bytes (%i words)\n", (int)b->used_size, (((int)(b->used_size)) / sizeof(void*)));
  return beginning_of_this_element;
}
Exemplo n.º 3
0
void caml_resize_extensible_buffer(struct caml_extensible_buffer *b,
                                   size_t new_used_size,
                                   char initial_value){
  /* Make the allocated buffer large enough, if needed: */
  //fprintf(stderr, "JJ [1] %i\n", (int)new_used_size);
  caml_reallocate_extensible_buffer_if_needed(b, new_used_size);
  //while(new_used_size > b->allocated_size)
  //  caml_reallocate_extensible_buffer(b, b->allocated_size * 2 + 1, initial_value);
  //fprintf(stderr, "JJ [2]\n");

  /* Update the used size and initialize the newly-used part, if any: */
  if(new_used_size > b->used_size){
    memset(((char*)b->array) + b->used_size, initial_value, new_used_size - b->used_size);
    //fprintf(stderr, "+++++++++++++Initialized with %i from %i to %i\n", initial_value, b->used_size, new_used_size - 1);
  }
  b->used_size = new_used_size;
}
Exemplo n.º 4
0
size_t caml_allocate_from_extensible_buffer(struct caml_extensible_buffer *b,
                                            size_t new_element_size,
                                            char initial_value){
  size_t beginning_of_this_element = (size_t)b->used_size;

  /* Reallocate the global array if needed: */
  caml_reallocate_extensible_buffer_if_needed(b, beginning_of_this_element + new_element_size);
  //while((beginning_of_this_element + new_element_size) > b->allocated_size)
  //  caml_reallocate_extensible_buffer(b, b->allocated_size * 2 + 1, initial_value);

  /* Fill the newly-allocated part: */
  long new_used_size = b->used_size + new_element_size;
  memset(((char*)b->array) + b->used_size, initial_value, new_used_size - b->used_size);
  //fprintf(stderr, "+++++++++++++Initialized with %i from %i to %i\n", initial_value, b->used_size, new_used_size - 1);

  b->used_size = new_used_size;

  //printf("%p->used_size is now %i bytes (%i words)\n", (int)b->used_size, (((int)(b->used_size)) / sizeof(void*)));
  return beginning_of_this_element;
}