tcpdump mailing list archives

[PATCH] Use the first block of memory chunks.


From: Mircea Gherzan <mgherzan () gmail com>
Date: Wed, 7 Sep 2011 10:47:06 +0200

Because n_left for the first chunk was initially 0, allocations
started in the second block.
---
 gencode.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/gencode.c b/gencode.c
index 2b624df..f60c42e 100644
--- a/gencode.c
+++ b/gencode.c
@@ -192,7 +192,7 @@ struct chunk {
 };
 
 static struct chunk chunks[NCHUNKS];
-static int cur_chunk;
+static int cur_chunk = -1;
 
 static void *newchunk(u_int);
 static void freechunks(void);
@@ -298,6 +298,14 @@ newchunk(n)
        n = ALIGN(n);
 #endif
 
+       if (cur_chunk == -1) {
+               k = cur_chunk = 0;
+               chunks[k].m = malloc(CHUNK0SIZE);
+               if (chunks[k].m == NULL)
+                       bpf_error("out of memory");
+               chunks[k].n_left = CHUNK0SIZE;
+       }
+
        cp = &chunks[cur_chunk];
        if (n > cp->n_left) {
                ++cp, k = ++cur_chunk;
@@ -321,7 +329,7 @@ freechunks()
 {
        int i;
 
-       cur_chunk = 0;
+       cur_chunk = -1;
        for (i = 0; i < NCHUNKS; ++i)
                if (chunks[i].m != NULL) {
                        free(chunks[i].m);
-- 
1.7.5.4

-
This is the tcpdump-workers list.
Visit https://cod.sandelman.ca/ to unsubscribe.


Current thread: