updataes.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 19 Feb 2001 14:49:45 +0000 (14:49 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 19 Feb 2001 14:49:45 +0000 (14:49 +0000)
lib/silccore/silcsockconn.c
lib/silccore/silcsockconn.h

index ebc2812e9a62caf1646f28ea9c1ecd809ad8ad34..3b5af11863b2af47aedecd6286bb3352a63c2469 100644 (file)
@@ -39,13 +39,17 @@ void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
   (*new_socket)->flags = 0;
   (*new_socket)->inbuf = NULL;
   (*new_socket)->outbuf = NULL;
+  (*new_socket)->users++;
 }
 
 /* Free's the Socket connection object. */
 
 void silc_socket_free(SilcSocketConnection sock)
 {
-  if (sock) {
+  sock->users--;
+  SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users + 1,
+                 sock->users));
+  if (sock->users < 1) {
     silc_buffer_free(sock->inbuf);
     silc_buffer_free(sock->outbuf);
     if (sock->hb) {
@@ -59,6 +63,16 @@ void silc_socket_free(SilcSocketConnection sock)
   }
 }
 
+/* Increase the reference counter. */
+
+SilcSocketConnection silc_socket_dup(SilcSocketConnection sock)
+{
+  sock->users++;
+  SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users - 1,
+                 sock->users));
+  return sock;
+}
+
 /* Internal timeout callback to perform heartbeat */
 
 SILC_TASK_CALLBACK(silc_socket_heartbeat)
index 9b190d1bb157cdf20bf4a4e6711546ebd647c19b..8a7967eb52f1c874c3542aa9dddc4182ca1119da 100644 (file)
@@ -86,6 +86,11 @@ typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
        indicate several different status that can affect the use of the
        socket object.
 
+   int users
+
+       Reference counter. When allocated it is set to one (1) and it won't
+       be freed until it hits zero (0).
+
    char *hostname
    char *ip
    unsigned short port
@@ -112,6 +117,7 @@ struct SilcSocketConnectionStruct {
   void *user_data;
   SilcProtocol protocol;
   unsigned int flags;
+  int users;
 
   char *hostname;
   char *ip;
@@ -160,6 +166,7 @@ struct SilcSocketConnectionHB {
 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
                       SilcSocketConnection *new_socket);
 void silc_socket_free(SilcSocketConnection sock);
+SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
                               unsigned long heartbeat,
                               void *hb_context,