Skip to content

Commit

Permalink
qvector_reverse does not require malloc for tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hojun-Cho committed May 13, 2024
1 parent 20d7435 commit 5e1bf35
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/containers/qvector.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,21 +830,16 @@ void qvector_reverse(qvector_t *vector) {

int i;
int j;
void *tmp = malloc(vector->objsize);
if (tmp == NULL) {
errno = ENOMEM;
return;
}

for (i = 0, j = vector->num - 1; i < j; i++, j--) {
void *data1 = (unsigned char *)vector->data + i * vector->objsize;
void *data2 = (unsigned char *)vector->data + j * vector->objsize;
unsigned char *data1 = (unsigned char *)vector->data + i * vector->objsize;
unsigned char *data2 = (unsigned char *)vector->data + j * vector->objsize;

memcpy(tmp, data1, vector->objsize);
memcpy(data1, data2, vector->objsize);
memcpy(data2, tmp, vector->objsize);
for (int k = 0; k < vector->objsize; k++) {
unsigned char tmp = data1[k];
data1[k] = data2[k];
data2[k] = tmp;
}
}
free(tmp);

vector->unlock(vector);
}
Expand Down

0 comments on commit 5e1bf35

Please sign in to comment.