[phc-internals] [phc commit] r1786 - trunk/src/lib
codesite-noreply at google.com
codesite-noreply at google.com
Tue Oct 14 22:32:24 IST 2008
Author: paul.biggar
Date: Tue Oct 14 14:32:12 2008
New Revision: 1786
Modified:
trunk/src/lib/Object.cpp
trunk/src/lib/Object.h
Log:
Currently, the largest file in the phc testsuite is 2000 lines, and this
requires 150MB of RAM. This is unacceptable, since we should be able to
compile an entire web-app, of, say, 50000 lines, without hitting a problem.
Obviously, we need to fix the GC problems, and enable it by default.
Having studied the libgc documentation, the most likely cause of the
segfaults we experience is that memory is still pointed to by objects not
traced by the collector. I've added a plan for fixing this. One of the
biggest problems was a simple one: Object required clone() to be
implemented. So far, we've only needed deep copies on AST/HIR/MIR objects
and Lists. So we need a root object which doesnt require clone(). This is
now GC_obj. Object still requires clone(), and it will still be the root
for maketea generated sources. All other classes we define will be migrated
to GC_obj.
Modified: trunk/src/lib/Object.cpp
==============================================================================
--- trunk/src/lib/Object.cpp (original)
+++ trunk/src/lib/Object.cpp Tue Oct 14 14:32:12 2008
@@ -7,3 +7,9 @@
#include "lib/Object.h"
+// TODO:
+//
+// Remove uses of 'use namespace std'
+// Replace std containers with GC ones in the phc namespace.
+// Figure out what to do with strings and stringstreams.
+// Replace all '#include <map>' with '#include "lib/Map.h" (same for
stack, set etc).
Modified: trunk/src/lib/Object.h
==============================================================================
--- trunk/src/lib/Object.h (original)
+++ trunk/src/lib/Object.h Tue Oct 14 14:32:12 2008
@@ -2,8 +2,8 @@
* phc -- the open source PHP compiler
* See doc/license/README.license for licensing information
*
- * The sole purpose of Object is to guarantee a polymorpic base
- * (i.e., a base that supports RTTI)
+ * GC_Obj provides a polymorpic base (i.e., a base that supports RTTI), and
+ * garbage collection. Object additionally provides a clone() method.
*/
#ifndef PHC_OBJECT_H
@@ -25,17 +25,50 @@
#endif
#endif
+/*
+ * Garbage collection:
+ *
+ * We use the Boehm garbage collector for C++. This creates a very small
number
+ * of rules that we need to follow. Failure to follow these rules will
+ * typically lead to the garbage allocator prematurely collecting objects,
+ * leading to segfaults. (Note: object on the stack are NOt excepted).
+ *
+ * 1. _ALL_ (let me repeat, ALL) classes which we define need to inherit
from GC_Obj.
+ * 2. Never use STL objects without wrapping them. The naming scheme is as
follows:
+ * - phc::map is identical to std::map, except that it uses it uses the
GC allocator.
+ * - phc::Map is a phc::map with extra interface components such as the
clone() method.
+ * 3. When using embedded PHP, do garbage collection manually. If garbage
+ * collected objects need to be put in PHP structures (which they
currently
+ * dont), then the memory must be allocated using libgc's traceable
+ * allocators, which traces, but doesn't deallocate memory.
+ * 4. For exceptional objects allocated with new, use the placement new
form:
+ * int* a = new (GC) int;
+ * If they need to be traceable but not collectable, use the other
placement new form:
+ * int* a = new (NoGC) int;
+ * 5. Object which dont contain other objects and are allocated on the
stack
+ * are OK: ie string, stringstream.
+ *
+ *
+ * TODO: it may be necessary to include libgc within phc to ensure that it
is
+ * compiled correctly for our use.
+ */
+
+
+
#ifdef USE_GC
#include "gc/gc_cpp.h"
-class Object : public gc
+class GC_Obj : public gc
#else
-class Object
+class GC_Obj
#endif
{
-// Make Object a virtual base (required for RTTI and dynamic casts)
+// Make Obj a virtual base (required for RTTI and dynamic casts)
public:
- virtual ~Object() {}
+ virtual ~GC_Obj() {}
+};
+class Object : public GC_Obj
+{
// Objects should support cloning
public:
virtual Object* clone() = 0;
@@ -43,12 +76,12 @@
/* These are copies of ideas from LLVM. All of our uses of casts can be
* characterized as one of these. */
-template <class T> bool isa(Object* in)
+template <class T> bool isa(GC_Obj* in)
{
return dynamic_cast<T*> (in) != NULL;
}
-template <class T> T* dyc(Object* in)
+template <class T> T* dyc(GC_Obj* in)
{
if (in == NULL) return NULL;
More information about the phc-internals
mailing list