GArray * g_array_append_vals (GArray *array, gconstpointer data, guint len) { GArrayPriv *priv = (GArrayPriv*)array; g_return_val_if_fail (array != NULL, NULL); ensure_capacity (priv, priv->array.len + len + (priv->zero_terminated ? 1 : 0)); memmove (element_offset (priv, priv->array.len), data, element_length (priv, len)); priv->array.len += len; if (priv->zero_terminated) { memset (element_offset (priv, priv->array.len), 0, priv->element_size); } return array; }
GArray* g_array_insert_vals (GArray *array, guint index_, gconstpointer data, guint len) { GArrayPriv *priv = (GArrayPriv*)array; guint extra = (priv->zero_terminated ? 1 : 0); g_return_val_if_fail (array != NULL, NULL); ensure_capacity (priv, array->len + len + extra); /* first move the existing elements out of the way */ memmove (element_offset (priv, index_ + len), element_offset (priv, index_), element_length (priv, array->len - index_)); /* then copy the new elements into the array */ memmove (element_offset (priv, index_), data, element_length (priv, len)); array->len += len; if (priv->zero_terminated) { memset (element_offset (priv, priv->array.len), 0, priv->element_size); } return array; }
GArray* g_array_remove_index_fast (GArray *array, guint index_) { GArrayPriv *priv = (GArrayPriv*)array; g_return_val_if_fail (array != NULL, NULL); memmove (element_offset (priv, index_), element_offset (priv, array->len - 1), element_length (priv, 1)); array->len --; if (priv->zero_terminated) { memset (element_offset (priv, priv->array.len), 0, priv->element_size); } return array; }
static void ensure_capacity (GArrayPriv *priv, guint capacity) { guint new_capacity; if (capacity <= priv->capacity) return; new_capacity = (capacity + 63) & ~63; priv->array.data = g_realloc (priv->array.data, element_length (priv, new_capacity)); if (priv->clear_) { memset (element_offset (priv, priv->capacity), 0, element_length (priv, new_capacity - priv->capacity)); } priv->capacity = new_capacity; }
static void ensure_capacity (GArrayPriv *priv, int capacity) { int new_capacity = MAX (priv->capacity, INITIAL_CAPACITY); if (capacity < priv->capacity) return; while (new_capacity < capacity) { new_capacity <<= 1; } capacity = new_capacity; priv->array.data = (gchar*)g_realloc (priv->array.data, element_length (priv, capacity)); if (priv->clear_) { memset (element_offset (priv, priv->capacity), 0, element_length (priv, capacity - priv->capacity)); } priv->capacity = capacity; }