static const char * _eina_stringshare_small_bucket_insert_at( Eina_Stringshare_Small_Bucket **p_bucket, const char *str, unsigned char length, int idx) { Eina_Stringshare_Small_Bucket *bucket = *p_bucket; int todo, off; char *snew; if (!bucket) { *p_bucket = bucket = calloc(1, sizeof(*bucket)); if (!bucket) { eina_error_set(EINA_ERROR_OUT_OF_MEMORY); return NULL; } } if (bucket->count + 1 >= bucket->size) { int size = bucket->size + EINA_STRINGSHARE_SMALL_BUCKET_STEP; if (!_eina_stringshare_small_bucket_resize(bucket, size)) return NULL; } snew = malloc(length + 1); if (!snew) { eina_error_set(EINA_ERROR_OUT_OF_MEMORY); return NULL; } memcpy(snew, str, length); snew[length] = '\0'; off = idx + 1; todo = bucket->count - idx; if (todo > 0) { memmove((void *)(bucket->strings + off), bucket->strings + idx, todo * sizeof(bucket->strings[0])); memmove(bucket->lengths + off, bucket->lengths + idx, todo * sizeof(bucket->lengths[0])); memmove(bucket->references + off, bucket->references + idx, todo * sizeof(bucket->references[0])); } bucket->strings[idx] = snew; bucket->lengths[idx] = length; bucket->references[idx] = 1; bucket->count++; return snew; }
static void _eina_stringshare_small_bucket_remove_at( Eina_Stringshare_Small_Bucket **p_bucket, int idx) { Eina_Stringshare_Small_Bucket *bucket = *p_bucket; int todo, off; if (bucket->references[idx] > 1) { bucket->references[idx]--; return; } free((char *)bucket->strings[idx]); if (bucket->count == 1) { free((void *)bucket->strings); free(bucket->lengths); free(bucket->references); free(bucket); *p_bucket = NULL; return; } bucket->count--; if (idx == bucket->count) goto end; off = idx + 1; todo = bucket->count - idx; memmove((void *)(bucket->strings + idx), bucket->strings + off, todo * sizeof(bucket->strings[0])); memmove(bucket->lengths + idx, bucket->lengths + off, todo * sizeof(bucket->lengths[0])); memmove(bucket->references + idx, bucket->references + off, todo * sizeof(bucket->references[0])); end: if (bucket->count + EINA_STRINGSHARE_SMALL_BUCKET_STEP < bucket->size) { int size = bucket->size - EINA_STRINGSHARE_SMALL_BUCKET_STEP; _eina_stringshare_small_bucket_resize(bucket, size); } }