From a8e37bafb94b29ee59947da24466e987915acd44 Mon Sep 17 00:00:00 2001 From: angt Date: Wed, 13 Jan 2016 16:19:58 +0100 Subject: [PATCH] Use realloc(): easier, better, faster, stronger --- src/main.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 6868bcd..e7eb500 100644 --- a/src/main.c +++ b/src/main.c @@ -567,33 +567,35 @@ void tcp_entry_free (struct tcp_entry *te) void sa_insert_elem (struct seq_array *sa, uint32_t i, uint32_t seq, uint32_t size) { - struct seq_elem *old = sa->elem; + if (sa->countelem = malloc((sa->count+1)*sizeof(struct seq_elem)); + if (!(sa->count&7)) { + struct seq_elem *tmp = realloc(sa->elem, (sa->count+8)*sizeof(struct seq_elem)); - memcpy(sa->elem, old, i*sizeof(struct seq_elem)); - memcpy(&sa->elem[i+1], &old[i], (sa->count-i)*sizeof(struct seq_elem)); + if (!tmp) { + gt_log("couldn't realloc!\n"); + return; + } + + sa->elem = tmp; + } + + memmove(&sa->elem[i+1], &sa->elem[i], (sa->count-i)*sizeof(struct seq_elem)); sa->elem[i].seq = seq; sa->elem[i].size = size; - - free(old); - sa->count++; } void sa_remove_elem (struct seq_array *sa, uint32_t i) { - struct seq_elem *old = sa->elem; - - sa->elem = malloc((sa->count-1)*sizeof(struct seq_elem)); - - memcpy(sa->elem, old, i*sizeof(struct seq_elem)); - memcpy(&sa->elem[i], &old[i+1], (sa->count-i-1)*sizeof(struct seq_elem)); - - free(old); + if (sa->countcount--; + + memmove(&sa->elem[i], &sa->elem[i+1], (sa->count-i)*sizeof(struct seq_elem)); } int sa_have (struct seq_array *sa, uint32_t seq, uint32_t size)