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. */
| 2122 | * the result, and can be called repeatedly to enlarge the result array. |
| 2123 | */ |
| 2124 | int *getKeysPrepareResult(getKeysResult *result, int numkeys) { |
| 2125 | /* GETKEYS_RESULT_INIT initializes keys to NULL, point it to the pre-allocated stack |
| 2126 | * buffer here. */ |
| 2127 | if (!result->keys) { |
| 2128 | serverAssert(!result->numkeys); |
| 2129 | result->keys = result->keysbuf; |
| 2130 | } |
| 2131 | |
| 2132 | /* Resize if necessary */ |
| 2133 | if (numkeys > result->size) { |
| 2134 | if (result->keys != result->keysbuf) { |
| 2135 | /* We're not using a static buffer, just (re)alloc */ |
| 2136 | result->keys = (int*)zrealloc(result->keys, numkeys * sizeof(int)); |
| 2137 | } else { |
| 2138 | /* We are using a static buffer, copy its contents */ |
| 2139 | result->keys = (int*)zmalloc(numkeys * sizeof(int)); |
| 2140 | if (result->numkeys) |
| 2141 | memcpy(result->keys, result->keysbuf, result->numkeys * sizeof(int)); |
| 2142 | } |
| 2143 | result->size = numkeys; |
| 2144 | } |
| 2145 | |
| 2146 | return result->keys; |
| 2147 | } |
| 2148 | |
| 2149 | /* The base case is to use the keys position as given in the command table |
| 2150 | * (firstkey, lastkey, step). */ |
no test coverage detected