* l2_init: * Initilizes the l2_table. * It creates the slots of MAX_TABLE_SIZE multiplied by MAX_BUCKET_SIZE. * * @l2tbl: pointer to * @size: number of hash value entries. must be power of 2, greater than 0, and * less than equal to MAX_TABLE_SIZE (2^30) * @bucket: number of slots per hash value. must be power of 2, greater than 0, * and less than equal to MAX_BUCKET_SIZE (
| 57 | * and less than equal to MAX_BUCKET_SIZE (4) |
| 58 | */ |
| 59 | static int l2_init(struct l2_table *l2tbl, int size, int bucket) { |
| 60 | if (size <= 0 || size > MAX_TABLE_SIZE || !is_power_of_2(size)) { |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
| 64 | if (bucket <= 0 || bucket > MAX_BUCKET_SIZE || !is_power_of_2(bucket)) { |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | if (l2tbl == nullptr) { |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | l2tbl->table = new(std::nothrow) l2_entry[size * bucket]{}; |
| 73 | |
| 74 | if (l2tbl->table == nullptr) { |
| 75 | return -ENOMEM; |
| 76 | } |
| 77 | |
| 78 | l2tbl->size = size; |
| 79 | l2tbl->bucket = bucket; |
| 80 | |
| 81 | /* calculates the log_2 (size) */ |
| 82 | l2tbl->size_power = 0; |
| 83 | while (size > 1) { |
| 84 | size = size >> 1; |
| 85 | l2tbl->size_power += 1; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int l2_deinit(struct l2_table *l2tbl) { |
| 92 | if (l2tbl == nullptr || l2tbl->table == nullptr || l2tbl->size == 0 || |
no test coverage detected