static int idm_grow(struct index_map *idm, int index) { idm->array[idx_array_index(index)] = calloc(IDX_ENTRY_SIZE, sizeof(void *)); if (!idm->array[idx_array_index(index)]) goto nomem; return index; nomem: errno = ENOMEM; return -1; }
void *idm_clear(struct index_map *idm, int index) { void **entry; void *item; entry = idm->array[idx_array_index(index)]; item = entry[idx_entry_index(index)]; entry[idx_entry_index(index)] = NULL; if (--idm->count[idx_array_index(index)] == 0) { free(idm->array[idx_array_index(index)]); idm->array[idx_array_index(index)] = NULL; } return item; }
void idx_replace(struct indexer *idx, int index, void *item) { union idx_entry *entry; entry = idx->array[idx_array_index(index)]; entry[idx_entry_index(index)].item = item; }
int idm_set(struct index_map *idm, int index, void *item) { void **entry; if (index > IDX_MAX_INDEX) { errno = ENOMEM; return -1; } if (!idm->array[idx_array_index(index)]) { if (idm_grow(idm, index) < 0) return -1; } entry = idm->array[idx_array_index(index)]; entry[idx_entry_index(index)] = item; return index; }
void *idm_clear(struct index_map *idm, int index) { void **entry; void *item; entry = idm->array[idx_array_index(index)]; item = entry[idx_entry_index(index)]; entry[idx_entry_index(index)] = NULL; return item; }
void *idx_remove(struct indexer *idx, int index) { union idx_entry *entry; void *item; entry = idx->array[idx_array_index(index)]; item = entry[idx_entry_index(index)].item; entry[idx_entry_index(index)].next = idx->free_list; idx->free_list = index; return item; }
int idx_insert(struct indexer *idx, void *item) { union idx_entry *entry; int index; if ((index = idx->free_list) == 0) { if ((index = idx_grow(idx)) <= 0) return index; } entry = idx->array[idx_array_index(index)]; idx->free_list = entry[idx_entry_index(index)].next; entry[idx_entry_index(index)].item = item; return index; }