From: robertc <> Date: Thu, 21 Nov 2002 19:35:52 +0000 (+0000) Subject: Refcount import X-Git-Tag: SQUID_3_0_PRE1~517 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1077c1b85bd487ab9a8b4cf01656d39a83cda6ee;p=thirdparty%2Fsquid.git Refcount import --- diff --git a/Makefile.am b/Makefile.am index d94abfd7e2..e2374f29f4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,11 +1,11 @@ ## Process this file with automake to produce Makefile.in # -# $Id: Makefile.am,v 1.15 2002/09/29 21:41:18 hno Exp $ +# $Id: Makefile.am,v 1.16 2002/11/21 12:35:52 robertc 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) diff --git a/configure.in b/configure.in index 865a1eea86..d6172216c0 100644 --- a/configure.in +++ b/configure.in @@ -3,7 +3,7 @@ dnl Configuration input file for Squid dnl dnl Duane Wessels, wessels@nlanr.net, February 1996 (autoconf v2.9) dnl -dnl $Id: configure.in,v 1.302 2002/11/10 04:12:54 hno Exp $ +dnl $Id: configure.in,v 1.303 2002/11/21 12:35:52 robertc Exp $ dnl dnl dnl @@ -11,9 +11,9 @@ AC_INIT 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.302 $)dnl +AC_REVISION($Revision: 1.303 $)dnl AC_PREFIX_DEFAULT(/usr/local/squid) AM_MAINTAINER_MODE @@ -2359,6 +2359,7 @@ AC_CONFIG_FILES([\ src/fs/diskd/Makefile \ src/fs/null/Makefile \ src/fs/ufs/Makefile \ + test-suite/Makefile \ doc/Makefile \ helpers/Makefile \ helpers/basic_auth/Makefile \ diff --git a/include/RefCount.h b/include/RefCount.h new file mode 100644 index 0000000000..8155179c47 --- /dev/null +++ b/include/RefCount.h @@ -0,0 +1,96 @@ + +/* + * $Id: RefCount.h,v 1.1 2002/11/21 12:35:53 robertc 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 RefCount { +public: + RefCount () : p_ (NULL) {} + RefCount (C const *p) : p_(p) { if (p_) p_->RefCountReference(); } + ~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 const * operator-> () const {return p_; } + C * operator-> () {return const_cast(p_); } + C const & operator * () const {return *p_; } + C & operator * () {return *const_cast(p_); } + C const * getRaw() const{return p_; } + C * getRaw() {return const_cast(p_); } + bool operator == (const RefCount& p) const + { + return p.p_ == p_; + } +private: + void dereference() + { + if (p_ && p_->RefCountDereference() == 0) p_->deleteSelf(); + } + void reference (const RefCount& p) + { + if (p.p_) p.p_->RefCountReference(); + } + C const *p_; + +}; + +struct RefCountable +{ + RefCountable():count_(0){} + virtual ~RefCountable(){} + virtual void deleteSelf() const = 0; + /* Not private, to allow class hierarchies */ + void RefCountReference() const { ++count_; } + unsigned RefCountDereference() const { return --count_; } +private: + mutable unsigned count_; +}; + +#endif /* _SQUID_REFCOUNT_H_ */ diff --git a/test-suite/Makefile b/test-suite/Makefile deleted file mode 100644 index 46016d85a7..0000000000 --- a/test-suite/Makefile +++ /dev/null @@ -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) diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am new file mode 100644 index 0000000000..2c474cc468 --- /dev/null +++ b/test-suite/Makefile.am @@ -0,0 +1,41 @@ +# +# Makefile for the Squid Object Cache server +# +# $Id: Makefile.am,v 1.1 2002/11/21 12:35:53 robertc 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) diff --git a/test-suite/refcount.cc b/test-suite/refcount.cc new file mode 100644 index 0000000000..317bfa6ce0 --- /dev/null +++ b/test-suite/refcount.cc @@ -0,0 +1,117 @@ + +/* + * $Id: refcount.cc,v 1.1 2002/11/21 12:35:53 robertc 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 { +public: + _ToRefCount () {++Instances;} + ~_ToRefCount() {--Instances;} + int someMethod() {if (!this) exit(1); + return 1;} + void deleteSelf() const {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(); + aForthObject = NULL; + } + } + /* Test creating an object, using it , and then making available as a + * refcounted one: + */ + { + _ToRefCount *aPointer = new _ToRefCount; + aPointer->someMethod(); + ToRefCount anObject(aPointer); + } + /* standalone pointers should be usable */ + { + ToRefCount anObject; + } + /* Can we check pointers for equality */ + { + ToRefCount anObject; + ToRefCount anotherObject(new _ToRefCount); + if (anObject == anotherObject) + exit (1); + anotherObject = NULL; + if (!(anObject == anotherObject)) + exit (1); + } + /* Can we get the pointer for a const object */ + { + ToRefCount anObject (new _ToRefCount); + ToRefCount const aConstObject (anObject); + _ToRefCount const *aPointer = aConstObject.getRaw(); + if (aPointer != anObject.getRaw()) + exit (2); + } + /* Can we get a refcounted pointer from a const object */ + { + _ToRefCount const * aPointer = new _ToRefCount; + ToRefCount anObject (aPointer); + } + /* Can we get a pointer to nonconst from a nonconst refcounter */ + { + ToRefCount anObject (new _ToRefCount); + _ToRefCount *aPointer = anObject.getRaw(); + aPointer = NULL; + } + return _ToRefCount::Instances == 0 ? 0 : 1; +}