---------------------
PatchSet 130 
Date: 2002/11/06 06:57:34
Author: rbcollins
Branch: refcount
Tag: (none) 
Log:
Refcounted template class for squid 3

Members: 
	Makefile.am:1.1->1.1.8.1 
	configure.in:1.6->1.6.2.1 
	include/RefCount.h:1.1->1.1.2.1 
	test-suite/Makefile:1.1->1.1.8.1(DEAD) 
	test-suite/Makefile.am:1.1->1.1.6.1 
	test-suite/refcount.cc:1.1->1.1.2.1 

Index: squid3/Makefile.am
===================================================================
RCS file: /cvsroot/squid-sf//squid3/Makefile.am,v
retrieving revision 1.1
retrieving revision 1.1.8.1
diff -u -r1.1 -r1.1.8.1
--- squid3/Makefile.am	14 Oct 2002 00:20:06 -0000	1.1
+++ squid3/Makefile.am	6 Nov 2002 06:57:34 -0000	1.1.8.1
@@ -1,11 +1,11 @@
 ## Process this file with automake to produce Makefile.in
 #
-# $Id: Makefile.am,v 1.1 2002/10/14 00:20:06 squidadm Exp $
+# $Id: Makefile.am,v 1.1.8.1 2002/11/06 06:57:34 rbcollins Exp $
 #
 
 AUTOMAKE_OPTIONS = dist-bzip2 subdir-objects 1.5
-DIST_SUBDIRS	= lib snmplib scripts src icons errors contrib doc helpers
-SUBDIRS		= lib @makesnmplib@ scripts src icons errors doc helpers
+DIST_SUBDIRS	= lib snmplib scripts src icons errors contrib doc helpers test-suite 
+SUBDIRS		= lib @makesnmplib@ scripts src icons errors doc helpers test-suite
 
 DISTCLEANFILES = include/stamp-h include/stamp-h[0-9]*
 DEFAULT_PINGER		= $(libexecdir)/pinger$(EXEEXT)
Index: squid3/configure.in
===================================================================
RCS file: /cvsroot/squid-sf//squid3/configure.in,v
retrieving revision 1.6
retrieving revision 1.6.2.1
diff -u -r1.6 -r1.6.2.1
--- squid3/configure.in	1 Nov 2002 09:13:44 -0000	1.6
+++ squid3/configure.in	6 Nov 2002 06:57:35 -0000	1.6.2.1
@@ -3,7 +3,7 @@
 dnl
 dnl  Duane Wessels, wessels@nlanr.net, February 1996 (autoconf v2.9)
 dnl
-dnl  $Id: configure.in,v 1.6 2002/11/01 09:13:44 squidadm Exp $
+dnl  $Id: configure.in,v 1.6.2.1 2002/11/06 06:57:35 rbcollins Exp $
 dnl
 dnl
 dnl
@@ -11,9 +11,9 @@
 AC_PREREQ(2.52)
 AC_CONFIG_SRCDIR([src/main.cc])
 AC_CONFIG_AUX_DIR(cfgaux)
-AM_INIT_AUTOMAKE(squid, 3.0-DEVEL)
+AM_INIT_AUTOMAKE(squid, 3.0-DEVEL-refcount)
 AM_CONFIG_HEADER(include/autoconf.h)
-AC_REVISION($Revision: 1.6 $)dnl
+AC_REVISION($Revision: 1.6.2.1 $)dnl
 AC_PREFIX_DEFAULT(/usr/local/squid)
 AM_MAINTAINER_MODE
 
@@ -2345,6 +2345,7 @@
 	src/fs/diskd/Makefile \
 	src/fs/null/Makefile \
 	src/fs/ufs/Makefile \
+	test-suite/Makefile \
 	doc/Makefile \
 	helpers/Makefile \
 	helpers/basic_auth/Makefile \
--- /dev/null	Wed Feb 14 12:13:05 2007
+++ squid3/include/RefCount.h	Wed Feb 14 12:13:53 2007
@@ -0,0 +1,86 @@
+
+/*
+ * $Id: RefCount.h,v 1.1.2.1 2002/11/06 06:57:35 rbcollins Exp $
+ *
+ * DEBUG: section xx    Refcount allocator
+ * AUTHOR:  Robert Collins
+ *
+ * SQUID Web Proxy Cache          http://www.squid-cache.org/
+ * ----------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from
+ *  the Internet community; see the CONTRIBUTORS file for full
+ *  details.   Many organizations have provided support for Squid's
+ *  development; see the SPONSORS file for full details.  Squid is
+ *  Copyrighted (C) 2001 by the Regents of the University of
+ *  California; see the COPYRIGHT file for full details.  Squid
+ *  incorporates software developed and/or copyrighted by other
+ *  sources; see the CREDITS file for full details.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ *
+ */
+
+#ifndef _SQUID_REFCOUNT_H_
+#define _SQUID_REFCOUNT_H_
+
+template <class C>
+class RefCount {
+public:
+  RefCount (C *p) : p_(p) { if (p_) ++p_->count_; }
+  ~RefCount()
+    {
+      dereference();
+    }
+  RefCount (const RefCount &p) : p_(p.p_) 
+    {
+      reference (p);
+    }
+  RefCount& operator = (const RefCount& p)
+    {
+      // DO NOT CHANGE THE ORDER HERE!!!
+      // This preserves semantics on self assignment
+      reference(p);
+      dereference();
+      p_ = p.p_;
+      return *this;
+    }
+  C * operator-> () {return p_; }
+  C & operator * () {return *p_; }
+  C * getRaw()      {return p_; }
+private:
+  void dereference()
+    {
+      if (p_ && --p_->count_ == 0) p_->deleteSelf();
+    }
+  void reference (const RefCount& p)
+    {
+      if (p.p_) ++p.p_->count_;
+    }
+  C *p_;
+
+};
+
+template<class C> class RefCountable
+{
+public:
+    virtual ~RefCountable(){}
+    virtual void deleteSelf() = 0;
+private:
+    friend class RefCount<C>;
+    unsigned count_;
+};
+
+#endif /* _SQUID_REFCOUNT_H_ */
--- squid3/test-suite/Makefile	Wed Feb 14 12:13:53 2007
+++ /dev/null	Wed Feb 14 12:13:05 2007
@@ -1,29 +0,0 @@
-CC	= gcc
-CFLAGS	= -g -Wall -I../include -I../src
-OBJS	= membanger.o hash.o SizeToPool.o
-LIB	= -L. -lMem
-TARGLIB = libMem.a
-LIBOBJS = Mem.o \
-          Stack.o
-AR_R      = /usr/bin/ar r
-RM      = rm
-XTRA_LIBS = -lm  -lmalloc
-
-all:  membanger
-
-membanger: $(OBJS) $(TARGLIB)
-	$(CC) -o membanger $(OBJS) $(LIB)
-
-tcp-banger2: tcp-banger2.o
-	$(CC) -g -o $@ tcp-banger2.o
-
-tcp-banger2.o: tcp-banger2.c
-	$(CC) -c $(CFLAGS) tcp-banger2.c
-
-$(OBJS): Makefile
-
-$(TARGLIB): $(LIBOBJS)
-	$(AR_R) $(TARGLIB) $(LIBOBJS)
-
-clean: 
-	rm $(OBJS) $(TARGLIB) $(LIBOBJS)
--- /dev/null	Wed Feb 14 12:13:05 2007
+++ squid3/test-suite/Makefile.am	Wed Feb 14 12:13:53 2007
@@ -0,0 +1,41 @@
+#
+#  Makefile for the Squid Object Cache server
+#
+#  $Id: Makefile.am,v 1.1.6.1 2002/11/06 06:57:35 rbcollins Exp $
+#
+
+AM_CFLAGS = -Werror -Wall
+AM_CXXFLAGS = -Werror -Wall
+
+INCLUDES        = -I. -I$(srcdir) -I$(top_builddir)/include -I$(top_srcdir)/include -I$(top_srcdir)/src
+
+EXTRA_PROGRAMS = membanger tcp-banger2
+
+TESTS = refcount
+
+LDADD = -L$(top_builddir)/lib -lmiscutil
+
+check_PROGRAMS= refcount
+
+refcount_SOURCES = refcount.cc
+
+
+## membanger won't link today. Bitrot..
+##CC	= gcc
+##CFLAGS	= -g -Wall -I../include -I../src
+##OBJS	= membanger.o hash.o SizeToPool.o
+##LIB	= -L. -lMem
+##TARGLIB = libMem.a
+##LIBOBJS = Mem.o \
+##          Stack.o
+##AR_R      = /usr/bin/ar r
+##RM      = rm
+##XTRA_LIBS = -lm  -lmalloc
+##
+##all:  membanger
+##
+##membanger: $(OBJS) $(TARGLIB)
+##	$(CC) -o membanger $(OBJS) $(LIB)
+##
+##$(TARGLIB): $(LIBOBJS)
+##	$(AR_R) $(TARGLIB) $(LIBOBJS)
--- /dev/null	Wed Feb 14 12:13:05 2007
+++ squid3/test-suite/refcount.cc	Wed Feb 14 12:13:53 2007
@@ -0,0 +1,75 @@
+
+/*
+ * $Id: refcount.cc,v 1.1.2.1 2002/11/06 06:57:35 rbcollins Exp $
+ *
+ * DEBUG: section xx    Refcount allocator
+ * AUTHOR:  Robert Collins
+ *
+ * SQUID Web Proxy Cache          http://www.squid-cache.org/
+ * ----------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from
+ *  the Internet community; see the CONTRIBUTORS file for full
+ *  details.   Many organizations have provided support for Squid's
+ *  development; see the SPONSORS file for full details.  Squid is
+ *  Copyrighted (C) 2001 by the Regents of the University of
+ *  California; see the COPYRIGHT file for full details.  Squid
+ *  incorporates software developed and/or copyrighted by other
+ *  sources; see the CREDITS file for full details.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ *
+ */
+
+#include "squid.h"
+#include "RefCount.h"
+
+class _ToRefCount :public RefCountable<_ToRefCount> {
+public:
+    _ToRefCount () {++Instances;}
+    ~_ToRefCount() {--Instances;}
+    int someMethod() {if (!this) exit(1);
+	return 1;}
+    void deleteSelf() {delete this;}
+    static int Instances;
+private:
+};
+
+typedef RefCount<_ToRefCount> ToRefCount;
+
+/* Must be zero at the end for the test to pass. */
+int _ToRefCount::Instances = 0;
+
+int
+main (int argc, char **argv)
+{
+    {
+      ToRefCount anObject(new _ToRefCount);
+      anObject->someMethod();
+      anObject = anObject;
+      ToRefCount objectTwo (anObject);
+      anObject = objectTwo;
+	{
+	  ToRefCount anotherObject(new _ToRefCount);
+	  anObject = anotherObject;
+	}
+	{
+	  ToRefCount aForthObject (anObject);
+	  anObject = ToRefCount(NULL);
+	  aForthObject->someMethod();
+	}
+    }
+    return _ToRefCount::Instances == 0 ? 0 : 1;
+}
