Review at https://gerrit.osmocom.org/2968
core/conv/viterbi.c: use static allocation for vdecoder Allocation of a new memory is an expensive operation, which takes place when it's initially unknown, how much memory will we need, or in order to decrease total memory usage. The vdecoder struct wasn't require dynamic allocation itself, so let's use static allocation in order to increase performance. Change-Id: Id1b140d3cb61db7352dcfc217a8fc36091e945ab --- M src/viterbi.c 1 file changed, 19 insertions(+), 23 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/68/2968/1 diff --git a/src/viterbi.c b/src/viterbi.c index 7777f96..9806386 100644 --- a/src/viterbi.c +++ b/src/viterbi.c @@ -498,7 +498,7 @@ } /* Release decoder object */ -static void free_vdec(struct vdecoder *dec) +static void vdec_deinit(struct vdecoder *dec) { if (!dec) return; @@ -509,22 +509,18 @@ vdec_free(dec->paths[0]); free(dec->paths); } - - free(dec); } -/* Allocate decoder object +/* Initialize decoder object with code specific params * Subtract the constraint length K on the normalization interval to * accommodate the initialization path metric at state zero. */ -static struct vdecoder *alloc_vdec(const struct osmo_conv_code *code) +static int vdec_init(struct vdecoder *dec, const struct osmo_conv_code *code) { int i, ns; - struct vdecoder *dec; ns = NUM_STATES(code->K); - dec = (struct vdecoder *) calloc(1, sizeof(struct vdecoder)); dec->n = code->N; dec->k = code->K; dec->recursive = conv_code_recursive(code); @@ -542,7 +538,7 @@ dec->metric_func = osmo_conv_metrics_k5_n4; break; default: - goto fail; + return -EINVAL; } } else if (dec->k == 7) { switch (dec->n) { @@ -556,10 +552,10 @@ dec->metric_func = osmo_conv_metrics_k7_n4; break; default: - goto fail; + return -EINVAL; } } else { - goto fail; + return -EINVAL; } if (code->term == CONV_TERM_FLUSH) @@ -569,24 +565,24 @@ dec->trellis = generate_trellis(code); if (!dec->trellis) - goto fail; + goto enomem; dec->paths = (int16_t **) malloc(sizeof(int16_t *) * dec->len); if (!dec->paths) - goto fail; + goto enomem; dec->paths[0] = vdec_malloc(ns * dec->len); if (!dec->paths[0]) - goto fail; + goto enomem; for (i = 1; i < dec->len; i++) dec->paths[i] = &dec->paths[0][i * ns]; - return dec; + return 0; -fail: - free_vdec(dec); - return NULL; +enomem: + vdec_deinit(dec); + return -ENOMEM; } /* Depuncture sequence with nagative value terminated puncturing matrix */ @@ -697,7 +693,7 @@ const sbit_t *input, ubit_t *output) { int rc; - struct vdecoder *vdec; + struct vdecoder dec; if (!init_complete) osmo_conv_init(); @@ -706,14 +702,14 @@ ((code->K != 5) && (code->K != 7))) return -EINVAL; - vdec = alloc_vdec(code); - if (!vdec) - return -EFAULT; + rc = vdec_init(&dec, code); + if (rc) + return rc; - rc = conv_decode(vdec, input, code->puncture, + rc = conv_decode(&dec, input, code->puncture, output, code->len, code->term); - free_vdec(vdec); + vdec_deinit(&dec); return rc; } -- To view, visit https://gerrit.osmocom.org/2968 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id1b140d3cb61db7352dcfc217a8fc36091e945ab Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Vadim Yanitskiy <axilira...@gmail.com>