Prepare the getKeysResult struct to hold numkeys, either by using the * pre-allocated keysbuf or by allocating a new array on the heap. * * This function must be called at least once before starting to populate * the result, and can be called repeatedly to enlarge the result array. */
| 1573 | * the result, and can be called repeatedly to enlarge the result array. |
| 1574 | */ |
| 1575 | int *getKeysPrepareResult(getKeysResult *result, int numkeys) { |
| 1576 | /* GETKEYS_RESULT_INIT initializes keys to NULL, point it to the pre-allocated stack |
| 1577 | * buffer here. */ |
| 1578 | if (!result->keys) { |
| 1579 | serverAssert(!result->numkeys); |
| 1580 | result->keys = result->keysbuf; |
| 1581 | } |
| 1582 | |
| 1583 | /* Resize if necessary */ |
| 1584 | if (numkeys > result->size) { |
| 1585 | if (result->keys != result->keysbuf) { |
| 1586 | /* We're not using a static buffer, just (re)alloc */ |
| 1587 | result->keys = zrealloc(result->keys, numkeys * sizeof(int)); |
| 1588 | } else { |
| 1589 | /* We are using a static buffer, copy its contents */ |
| 1590 | result->keys = zmalloc(numkeys * sizeof(int)); |
| 1591 | if (result->numkeys) |
| 1592 | memcpy(result->keys, result->keysbuf, result->numkeys * sizeof(int)); |
| 1593 | } |
| 1594 | result->size = numkeys; |
| 1595 | } |
| 1596 | |
| 1597 | return result->keys; |
| 1598 | } |
| 1599 | |
| 1600 | /* The base case is to use the keys position as given in the command table |
| 1601 | * (firstkey, lastkey, step). */ |
no test coverage detected