Added reference count to SilcPacketContext and added two new
authorPekka Riikonen <priikone@silcnet.org>
Tue, 21 Nov 2000 20:44:47 +0000 (20:44 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Tue, 21 Nov 2000 20:44:47 +0000 (20:44 +0000)
functions packet_context_alloc/free. packet_context_dup now
increses the reference count instead of duplicating data.

lib/silccore/silcpacket.c
lib/silccore/silcpacket.h

index 6846433a6f5ca1312ba8027ca429af4865220fdc..7054f600f6764145d6c3e6ab1ce2d2d4dcd19dfe 100644 (file)
@@ -393,7 +393,7 @@ void silc_packet_receive_process(SilcSocketConnection sock,
     }
 
     parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
-    parse_ctx->packet = silc_calloc(1, sizeof(*parse_ctx->packet));
+    parse_ctx->packet = silc_packet_context_alloc();
     parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
     parse_ctx->sock = sock;
     parse_ctx->cipher = cipher;
@@ -732,34 +732,37 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx)
   return ctx->type;
 }
 
-/* Duplicates packet context. Duplicates the entire context and its
-   contents. */
+/* Allocate packet context */
 
-SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx)
+SilcPacketContext *silc_packet_context_alloc()
 {
-  SilcPacketContext *new;
-
-  new = silc_calloc(1, sizeof(*new));
-  new->buffer = silc_buffer_copy(ctx->buffer);
-  new->type = ctx->type;
-  new->flags = ctx->flags;
-
-  new->src_id = silc_calloc(ctx->src_id_len, sizeof(*new->src_id));
-  memcpy(new->src_id, ctx->src_id, ctx->src_id_len);
-  new->src_id_len = ctx->src_id_len;
-  new->src_id_type = ctx->src_id_type;
+  SilcPacketContext *ctx = silc_calloc(1, sizeof(*ctx));
+  ctx->users++;
+  return ctx;
+}
 
-  new->dst_id = silc_calloc(ctx->dst_id_len, sizeof(*new->dst_id));
-  memcpy(new->dst_id, ctx->dst_id, ctx->dst_id_len);
-  new->dst_id_len = ctx->dst_id_len;
-  new->dst_id_type = ctx->dst_id_type;
+/* Increse the reference count of the packet context. */
 
-  new->truelen = ctx->truelen;
-  new->padlen = ctx->padlen;
+SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx)
+{
+  ctx->users++;
+  return ctx;
+}
 
-  new->rng = ctx->rng;
-  new->context = ctx->context;
-  new->sock = ctx->sock;
+/* Decrese the reference count of the packet context and free it only if
+   it is zero. */
 
-  return new;
+void silc_packet_context_free(SilcPacketContext *ctx)
+{
+  ctx->users--;
+  if (ctx->users < 1)
+    {
+      if (ctx->buffer)
+       silc_buffer_free(ctx->buffer);
+      if (ctx->src_id)
+       silc_free(ctx->src_id);
+      if (ctx->dst_id)
+       silc_free(ctx->dst_id);
+      silc_free(ctx);
+    }
 }
index 14b1c678fa07d3793a69db3617d85cf7521dbc11..86689b092da0e004a7ef73c5c6e12db0d82481cc 100644 (file)
@@ -135,6 +135,10 @@ typedef struct {
   /* Back pointers */
   void *context;
   SilcSocketConnection sock;
+
+  /* Reference count for this context. The context is free'd only
+     after the reference count is zero. */
+  int users;
 } SilcPacketContext;
 
 /* 
@@ -254,6 +258,8 @@ void silc_packet_receive_process(SilcSocketConnection sock,
                                 void *context);
 SilcPacketType silc_packet_parse(SilcPacketContext *ctx);
 SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx);
+SilcPacketContext *silc_packet_context_alloc();
 SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx);
+void silc_packet_context_free(SilcPacketContext *ctx);
 
 #endif