filter.c: Error handling, code simplification.

This commit is contained in:
Uwe Hermann 2010-05-09 19:36:43 +02:00
parent 5f8c4cb363
commit fdebec2108
1 changed files with 22 additions and 20 deletions

View File

@ -35,33 +35,35 @@ int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
int num_enabled_probes, out_bit, i; int num_enabled_probes, out_bit, i;
uint64_t sample_in, sample_out; uint64_t sample_in, sample_out;
*data_out = malloc(length_in); if (!(*data_out = malloc(length_in)))
return SIGROK_ERR_MALLOC;
num_enabled_probes = 0; num_enabled_probes = 0;
for (i = 0; probelist[i]; i++) for (i = 0; probelist[i]; i++)
num_enabled_probes++; num_enabled_probes++;
if (num_enabled_probes != in_unitsize * 8) { if (num_enabled_probes == in_unitsize * 8) {
/* All probes are used -- no need to compress anything. */
memcpy(*data_out, data_in, length_in);
*length_out = length_in;
return SIGROK_OK;
}
/* If we reached this point, not all probes are used, so "compress". */
in_offset = out_offset = 0; in_offset = out_offset = 0;
while (in_offset <= length_in - in_unitsize) { while (in_offset <= length_in - in_unitsize) {
memcpy(&sample_in, data_in + in_offset, in_unitsize); memcpy(&sample_in, data_in + in_offset, in_unitsize);
sample_out = 0; sample_out = out_bit = 0;
out_bit = 0;
for (i = 0; probelist[i]; i++) { for (i = 0; probelist[i]; i++) {
if (sample_in & (1 << (probelist[i] - 1))) if (sample_in & (1 << (probelist[i] - 1)))
sample_out |= 1 << out_bit; sample_out |= (1 << out_bit);
out_bit++; out_bit++;
} }
memcpy((*data_out) + out_offset, &sample_out, memcpy((*data_out) + out_offset, &sample_out, out_unitsize);
out_unitsize);
in_offset += in_unitsize; in_offset += in_unitsize;
out_offset += out_unitsize; out_offset += out_unitsize;
} }
*length_out = out_offset; *length_out = out_offset;
} else {
/* All probes are used -- no need to compress anything. */
memcpy(*data_out, data_in, length_in);
*length_out = length_in;
}
return SIGROK_OK; return SIGROK_OK;
} }