* Insert an entry to B-tree lookup table. * * @param bt * Pointer to B-tree structure. * @param entry * Pointer to new entry to insert. * * @return * 0 on success, -1 on failure. */
| 166 | * 0 on success, -1 on failure. |
| 167 | */ |
| 168 | static int |
| 169 | mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry) |
| 170 | { |
| 171 | struct mr_cache_entry *lkp_tbl; |
| 172 | uint32_t idx = 0; |
| 173 | size_t shift; |
| 174 | |
| 175 | MLX5_ASSERT(bt != NULL); |
| 176 | MLX5_ASSERT(bt->len <= bt->size); |
| 177 | MLX5_ASSERT(bt->len > 0); |
| 178 | lkp_tbl = *bt->table; |
| 179 | /* Find out the slot for insertion. */ |
| 180 | if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) { |
| 181 | DRV_LOG(DEBUG, |
| 182 | "abort insertion to B-tree(%p): already exist at" |
| 183 | " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x", |
| 184 | (void *)bt, idx, entry->start, entry->end, entry->lkey); |
| 185 | /* Already exist, return. */ |
| 186 | return 0; |
| 187 | } |
| 188 | /* Caller must ensure that there is enough place for a new entry. */ |
| 189 | MLX5_ASSERT(bt->len < bt->size); |
| 190 | /* Insert entry. */ |
| 191 | ++idx; |
| 192 | shift = (bt->len - idx) * sizeof(struct mr_cache_entry); |
| 193 | if (shift) |
| 194 | memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift); |
| 195 | lkp_tbl[idx] = *entry; |
| 196 | bt->len++; |
| 197 | DRV_LOG(DEBUG, |
| 198 | "inserted B-tree(%p)[%u]," |
| 199 | " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x", |
| 200 | (void *)bt, idx, entry->start, entry->end, entry->lkey); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Initialize B-tree and allocate memory for lookup table. |
no test coverage detected