/* This expands the list value so there's more extra space. Growth is done relative to the current size of the list, because why not? */ static void make_extra_space_in_list(lily_list_val *lv) { /* There's probably room for improvement here, later on. */ int extra = (lv->num_values + 8) >> 2; lv->elems = lily_realloc(lv->elems, (lv->num_values + extra) * sizeof(lily_value *)); lv->extra_space = extra; }
void lily_u16_write_prep(lily_buffer_u16 *b, uint32_t needed) { if (b->pos + needed > b->size) { while ((b->pos + needed) > b->size) b->size *= 2; b->data = lily_realloc(b->data, sizeof(uint16_t) * b->size); } }
void lily_u16_write_1(lily_buffer_u16 *b, uint16_t one) { if (b->pos + 1 > b->size) { b->size *= 2; b->data = lily_realloc(b->data, b->size * sizeof(uint16_t)); } b->data[b->pos] = one; b->pos++; }
void lily_u16_write_2(lily_buffer_u16 *b, uint16_t one, uint16_t two) { if (b->pos + 2 > b->size) { b->size *= 2; b->data = lily_realloc(b->data, b->size * sizeof(uint16_t)); } b->data[b->pos ] = one; b->data[b->pos + 1] = two; b->pos += 2; }
void lily_u16_inject(lily_buffer_u16 *b, int where, uint16_t value) { if (b->pos + 1 == b->size) { b->size *= 2; b->data = lily_realloc(b->data, b->size * sizeof(uint16_t)); } int move_by = b->pos - where; memmove(b->data+where+1, b->data+where, move_by * sizeof(uint16_t)); b->pos++; b->data[where] = value; }
void lily_u16_write_4(lily_buffer_u16 *b, uint16_t one, uint16_t two, uint16_t three, uint16_t four) { if (b->pos + 4 > b->size) { b->size *= 2; b->data = lily_realloc(b->data, b->size * sizeof(uint16_t)); } b->data[b->pos ] = one; b->data[b->pos + 1] = two; b->data[b->pos + 2] = three; b->data[b->pos + 3] = four; b->pos += 4; }
void lily_u16_write_6(lily_buffer_u16 *b, uint16_t one, uint16_t two, uint16_t three, uint16_t four, uint16_t five, uint16_t six) { if (b->pos + 6 > b->size) { b->size *= 2; b->data = lily_realloc(b->data, b->size * sizeof(uint16_t)); } b->data[b->pos ] = one; b->data[b->pos + 1] = two; b->data[b->pos + 2] = three; b->data[b->pos + 3] = four; b->data[b->pos + 4] = five; b->data[b->pos + 5] = six; b->pos += 6; }