2002-06-04 19:21:55 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: agc.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: reclaim unused atoms and functors *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "@(#)agc.c 1.3 3/15/90";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "absmi.h"
|
|
|
|
#include "alloc.h"
|
2002-06-05 15:23:15 +01:00
|
|
|
#include "yapio.h"
|
2005-05-27 22:44:00 +01:00
|
|
|
#include "attvar.h"
|
2002-06-04 19:21:55 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-05-30 06:26:50 +01:00
|
|
|
/* #define DEBUG_RESTORE1 1 */
|
2002-06-04 19:21:55 +01:00
|
|
|
/* #define DEBUG_RESTORE2 1 */
|
2005-05-30 06:26:50 +01:00
|
|
|
#define DEBUG_RESTORE3 1
|
2002-11-18 18:18:05 +00:00
|
|
|
#define errout Yap_stderr
|
2002-06-04 19:21:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
STATIC_PROTO(void RestoreEntries, (PropEntry *));
|
2004-02-06 10:19:49 +00:00
|
|
|
STATIC_PROTO(void CleanCode, (PredEntry *));
|
2002-06-04 19:21:55 +01:00
|
|
|
|
2002-06-05 04:59:50 +01:00
|
|
|
static int agc_calls;
|
|
|
|
|
2003-02-14 12:20:57 +00:00
|
|
|
static unsigned long int agc_collected;
|
2002-06-05 04:59:50 +01:00
|
|
|
|
|
|
|
static Int tot_agc_time = 0; /* total time spent in GC */
|
|
|
|
|
|
|
|
static Int tot_agc_recovered = 0; /* number of heap objects in all garbage collections */
|
|
|
|
|
2002-06-04 19:21:55 +01:00
|
|
|
#define AtomMarkedBit 1
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
MarkAtomEntry(AtomEntry *ae)
|
|
|
|
{
|
|
|
|
CELL c = (CELL)(ae->NextOfAE);
|
|
|
|
c |= AtomMarkedBit;
|
|
|
|
ae->NextOfAE = (Atom)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
AtomResetMark(AtomEntry *ae)
|
|
|
|
{
|
|
|
|
CELL c = (CELL)(ae->NextOfAE);
|
|
|
|
if (c & AtomMarkedBit) {
|
|
|
|
c &= ~AtomMarkedBit;
|
|
|
|
ae->NextOfAE = (Atom)c;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Atom
|
|
|
|
CleanAtomMarkedBit(Atom a)
|
|
|
|
{
|
|
|
|
CELL c = (CELL)a;
|
|
|
|
c &= ~AtomMarkedBit;
|
|
|
|
return((Atom)c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Functor
|
|
|
|
FuncAdjust(Functor f)
|
|
|
|
{
|
2005-05-30 04:26:37 +01:00
|
|
|
if (!IsExtensionFunctor(f)) {
|
|
|
|
AtomEntry *ae = RepAtom(NameOfFunctor(f));
|
|
|
|
MarkAtomEntry(ae);
|
|
|
|
}
|
2002-06-04 19:21:55 +01:00
|
|
|
return(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline Term
|
|
|
|
AtomTermAdjust(Term t)
|
|
|
|
{
|
|
|
|
AtomEntry *ae = RepAtom(AtomOfTerm(t));
|
|
|
|
MarkAtomEntry(ae);
|
|
|
|
return(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Atom
|
|
|
|
AtomAdjust(Atom a)
|
|
|
|
{
|
|
|
|
AtomEntry *ae;
|
|
|
|
if (a == NIL) return(a);
|
|
|
|
ae = RepAtom(a);
|
|
|
|
MarkAtomEntry(ae);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IsOldCode(P) FALSE
|
|
|
|
#define IsOldCodeCellPtr(P) FALSE
|
|
|
|
#define IsOldDelay(P) FALSE
|
|
|
|
#define IsOldDelayPtr(P) FALSE
|
|
|
|
#define IsOldLocalInTR(P) FALSE
|
|
|
|
#define IsOldLocalInTRPtr(P) FALSE
|
|
|
|
#define IsOldGlobal(P) FALSE
|
|
|
|
#define IsOldGlobalPtr(P) FALSE
|
|
|
|
#define IsOldTrail(P) FALSE
|
|
|
|
#define IsOldTrailPtr(P) FALSE
|
|
|
|
|
|
|
|
#define CharP(X) ((char *)(X))
|
|
|
|
|
|
|
|
#define AddrAdjust(P) (P)
|
|
|
|
#define AtomEntryAdjust(P) (P)
|
|
|
|
#define BlobTermAdjust(P) (P)
|
|
|
|
#define CellPtoHeapAdjust(P) (P)
|
|
|
|
#define CellPtoHeapCellAdjust(P) (P)
|
|
|
|
#define CellPtoTRAdjust(P) (P)
|
|
|
|
#define CodeAddrAdjust(P) (P)
|
|
|
|
#define ConsultObjAdjust(P) (P)
|
|
|
|
#define DelayAddrAdjust(P) (P)
|
|
|
|
#define DBRefAdjust(P) (P)
|
2003-01-21 16:14:52 +00:00
|
|
|
#define DBRefPAdjust(P) (P)
|
2004-02-06 02:26:23 +00:00
|
|
|
#define DBTermAdjust(P) (P)
|
|
|
|
#define LUIndexAdjust(P) (P)
|
|
|
|
#define SIndexAdjust(P) (P)
|
2002-06-04 19:21:55 +01:00
|
|
|
#define LocalAddrAdjust(P) (P)
|
|
|
|
#define GlobalAddrAdjust(P) (P)
|
2004-02-06 02:26:23 +00:00
|
|
|
#define PtoLUCAdjust(P) (P)
|
|
|
|
#define PtoStCAdjust(P) (P)
|
2002-06-04 19:21:55 +01:00
|
|
|
#define PtoArrayEAdjust(P) (P)
|
2005-10-28 18:38:50 +01:00
|
|
|
#define PtoArraySAdjust(P) (P)
|
2002-06-04 19:21:55 +01:00
|
|
|
#define PtoDelayAdjust(P) (P)
|
|
|
|
#define PtoGloAdjust(P) (P)
|
|
|
|
#define PtoLocAdjust(P) (P)
|
|
|
|
#define PtoHeapCellAdjust(P) (P)
|
|
|
|
#define PtoOpAdjust(P) (P)
|
|
|
|
#define PtoPredAdjust(P) (P)
|
|
|
|
#define PropAdjust(P) (P)
|
|
|
|
#define TrailAddrAdjust(P) (P)
|
|
|
|
#define XAdjust(P) (P)
|
|
|
|
#define YAdjust(P) (P)
|
|
|
|
|
|
|
|
static void
|
|
|
|
recompute_mask(DBRef dbr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rehash(CELL *oldcode, int NOfE, int KindOfEntries)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "rheap.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the really tough part, to restore the whole of the heap
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mark_atoms(void)
|
|
|
|
{
|
|
|
|
AtomHashEntry *HashPtr = HashChain;
|
|
|
|
register int i;
|
|
|
|
Atom atm;
|
|
|
|
AtomEntry *at;
|
|
|
|
|
|
|
|
restore_codes();
|
2003-10-28 01:16:03 +00:00
|
|
|
for (i = 0; i < AtomHashTableSize; ++i) {
|
2002-06-04 19:21:55 +01:00
|
|
|
atm = HashPtr->Entry;
|
|
|
|
if (atm) {
|
|
|
|
at = RepAtom(atm);
|
|
|
|
do {
|
2005-05-30 04:26:37 +01:00
|
|
|
#ifdef DEBUG_RESTORE1 /* useful during debug */
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(errout, "Restoring %s\n", at->StrOfAE);
|
2002-06-04 19:21:55 +01:00
|
|
|
#endif
|
|
|
|
RestoreEntries(RepProp(at->PropsOfAE));
|
|
|
|
atm = at->NextOfAE;
|
|
|
|
at = RepAtom(CleanAtomMarkedBit(atm));
|
|
|
|
} while (!EndOfPAEntr(at));
|
|
|
|
}
|
|
|
|
HashPtr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
atm = INVISIBLECHAIN.Entry;
|
|
|
|
at = RepAtom(atm);
|
|
|
|
if (EndOfPAEntr(at)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do {
|
2005-05-30 06:26:50 +01:00
|
|
|
#ifdef DEBUG_RESTORE1 /* useful during debug */
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(errout, "Restoring %s\n", at->StrOfAE);
|
2002-06-04 19:21:55 +01:00
|
|
|
#endif
|
|
|
|
RestoreEntries(RepProp(at->PropsOfAE));
|
|
|
|
atm = at->NextOfAE;
|
|
|
|
at = RepAtom(CleanAtomMarkedBit(atm));
|
|
|
|
} while (!EndOfPAEntr(at));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_trail(void)
|
|
|
|
{
|
|
|
|
register CELL *pt;
|
|
|
|
|
|
|
|
pt = (CELL *)TR;
|
|
|
|
/* moving the trail is simple */
|
2002-11-18 18:18:05 +00:00
|
|
|
while (pt != (CELL *)Yap_TrailBase) {
|
2002-06-04 19:21:55 +01:00
|
|
|
register CELL reg = pt[-1];
|
|
|
|
pt--;
|
|
|
|
if (!IsVarTerm(reg)) {
|
|
|
|
if (IsAtomTerm(reg)) {
|
|
|
|
MarkAtomEntry(RepAtom(AtomOfTerm(reg)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_local(void)
|
|
|
|
{
|
|
|
|
register CELL *pt;
|
|
|
|
|
|
|
|
/* Adjusting the local */
|
|
|
|
pt = LCL0;
|
|
|
|
/* moving the trail is simple */
|
|
|
|
while (pt > ASP) {
|
|
|
|
CELL reg = *--pt;
|
|
|
|
|
|
|
|
if (!IsVarTerm(reg)) {
|
|
|
|
if (IsAtomTerm(reg)) {
|
|
|
|
MarkAtomEntry(RepAtom(AtomOfTerm(reg)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-27 22:44:00 +01:00
|
|
|
static CELL *
|
|
|
|
mark_global_cell(CELL *pt)
|
|
|
|
{
|
|
|
|
CELL reg = *pt;
|
|
|
|
|
|
|
|
if (IsVarTerm(reg)) {
|
|
|
|
/* skip bitmaps */
|
|
|
|
switch(reg) {
|
|
|
|
case (CELL)FunctorDouble:
|
|
|
|
#if SIZEOF_DOUBLE == 2*SIZEOF_LONG_INT
|
|
|
|
return pt + 4;
|
|
|
|
#else
|
|
|
|
return pt + 3;
|
|
|
|
#endif
|
|
|
|
#if USE_GMP
|
|
|
|
case (CELL)FunctorBigInt:
|
|
|
|
{
|
|
|
|
Int sz = 1+
|
|
|
|
sizeof(MP_INT)+
|
|
|
|
(((MP_INT *)(pt+1))->_mp_alloc*sizeof(mp_limb_t));
|
|
|
|
return pt + sz+1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case (CELL)FunctorLongInt:
|
|
|
|
return pt += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (IsAtomTerm(reg)) {
|
|
|
|
MarkAtomEntry(RepAtom(AtomOfTerm(reg)));
|
|
|
|
return pt+1;
|
|
|
|
}
|
|
|
|
return pt+1;
|
|
|
|
}
|
|
|
|
|
2002-06-04 19:21:55 +01:00
|
|
|
static void
|
|
|
|
mark_global(void)
|
|
|
|
{
|
2005-05-27 22:44:00 +01:00
|
|
|
CELL *pt;
|
2002-06-04 19:21:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* to clean the global now that functors are just variables pointing to
|
|
|
|
* the code
|
|
|
|
*/
|
2005-05-27 22:44:00 +01:00
|
|
|
#if COROUTINING
|
|
|
|
CELL *ptf = (CELL *)DelayTop();
|
|
|
|
|
|
|
|
pt = (CELL *)Yap_GlobalBase;
|
|
|
|
while (pt < ptf) {
|
|
|
|
pt = mark_global_cell(pt);
|
|
|
|
}
|
2002-06-04 19:21:55 +01:00
|
|
|
#endif
|
2005-05-27 22:44:00 +01:00
|
|
|
pt = H0;
|
|
|
|
while (pt < H) {
|
|
|
|
pt = mark_global_cell(pt);
|
2002-06-04 19:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_stacks(void)
|
|
|
|
{
|
|
|
|
mark_trail();
|
|
|
|
mark_local();
|
|
|
|
mark_global();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the really tough part, to restore the whole of the heap
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clean_atoms(void)
|
|
|
|
{
|
|
|
|
AtomHashEntry *HashPtr = HashChain;
|
|
|
|
register int i;
|
|
|
|
Atom atm;
|
|
|
|
Atom *patm;
|
|
|
|
AtomEntry *at;
|
|
|
|
|
2003-10-28 01:16:03 +00:00
|
|
|
for (i = 0; i < AtomHashTableSize; ++i) {
|
2002-06-04 19:21:55 +01:00
|
|
|
atm = HashPtr->Entry;
|
|
|
|
patm = &(HashPtr->Entry);
|
|
|
|
while (atm != NIL) {
|
|
|
|
at = RepAtom(CleanAtomMarkedBit(atm));
|
|
|
|
if (AtomResetMark(at) || (AGCHook != NULL && !AGCHook(atm))) {
|
|
|
|
patm = &(at->NextOfAE);
|
|
|
|
atm = at->NextOfAE;
|
2003-10-28 01:16:03 +00:00
|
|
|
NOfAtoms--;
|
2002-06-04 19:21:55 +01:00
|
|
|
} else {
|
2005-05-30 06:26:50 +01:00
|
|
|
#ifdef DEBUG_RESTORE3
|
2005-05-31 20:42:28 +01:00
|
|
|
fprintf(stderr, "Purged %p:%s\n", at, at->StrOfAE);
|
2002-06-04 19:21:55 +01:00
|
|
|
#endif
|
|
|
|
*patm = at->NextOfAE;
|
|
|
|
atm = at->NextOfAE;
|
2004-03-05 15:26:33 +00:00
|
|
|
agc_collected += sizeof(AtomEntry)+strlen(at->StrOfAE);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeCodeSpace((char *)at);
|
2002-06-04 19:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
HashPtr++;
|
|
|
|
}
|
|
|
|
patm = &(INVISIBLECHAIN.Entry);
|
|
|
|
atm = INVISIBLECHAIN.Entry;
|
|
|
|
while (atm != NIL) {
|
|
|
|
at = RepAtom(CleanAtomMarkedBit(atm));
|
|
|
|
if (AtomResetMark(at) || (AGCHook != NULL && !AGCHook(atm))) {
|
|
|
|
patm = &(atm->NextOfAE);
|
2003-10-28 01:16:03 +00:00
|
|
|
NOfAtoms--;
|
2002-06-04 19:21:55 +01:00
|
|
|
atm = at->NextOfAE;
|
|
|
|
} else {
|
2005-05-30 06:26:50 +01:00
|
|
|
#ifdef DEBUG_RESTORE1
|
2002-06-04 19:21:55 +01:00
|
|
|
fprintf(stderr, "Purged %s\n", at->StrOfAE);
|
|
|
|
#endif
|
|
|
|
*patm = at->NextOfAE;
|
|
|
|
atm = at->NextOfAE;
|
2004-03-05 15:26:33 +00:00
|
|
|
agc_collected += sizeof(AtomEntry) + strlen(at->StrOfAE);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeCodeSpace((char *)at);
|
2002-06-04 19:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static void
|
2002-06-04 19:21:55 +01:00
|
|
|
atom_gc(void)
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
int gc_verbose = Yap_is_gc_verbose();
|
2002-06-05 04:59:50 +01:00
|
|
|
int gc_trace = 0;
|
|
|
|
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
UInt time_start, agc_time;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetValue(AtomGcTrace) != TermNil)
|
2002-06-05 04:59:50 +01:00
|
|
|
gc_trace = 1;
|
|
|
|
agc_calls++;
|
|
|
|
agc_collected = 0;
|
|
|
|
if (gc_trace) {
|
2004-06-23 18:24:20 +01:00
|
|
|
fprintf(Yap_stderr, "%% agc:\n");
|
2002-06-05 04:59:50 +01:00
|
|
|
} else if (gc_verbose) {
|
2004-06-23 18:24:20 +01:00
|
|
|
fprintf(Yap_stderr, "%% Start of atom garbage collection %d:\n", agc_calls);
|
2002-06-05 04:59:50 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
time_start = Yap_cputime();
|
2002-06-05 04:59:50 +01:00
|
|
|
/* get the number of active registers */
|
|
|
|
YAPEnterCriticalSection();
|
2002-06-04 19:21:55 +01:00
|
|
|
mark_stacks();
|
|
|
|
mark_atoms();
|
|
|
|
clean_atoms();
|
2002-06-05 04:59:50 +01:00
|
|
|
YAPLeaveCriticalSection();
|
2002-11-18 18:18:05 +00:00
|
|
|
agc_time = Yap_cputime()-time_start;
|
2002-06-05 04:59:50 +01:00
|
|
|
tot_agc_time += agc_time;
|
|
|
|
tot_agc_recovered += agc_collected;
|
|
|
|
if (gc_verbose) {
|
2004-06-23 18:24:20 +01:00
|
|
|
fprintf(Yap_stderr, "%% Collected %ld bytes.\n", agc_collected);
|
|
|
|
fprintf(Yap_stderr, "%% GC %d took %g sec, total of %g sec doing GC so far.\n", agc_calls, (double)agc_time/1000, (double)tot_agc_time/1000);
|
2002-06-05 04:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_atom_gc(void)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
atom_gc();
|
|
|
|
}
|
|
|
|
|
2002-06-05 04:59:50 +01:00
|
|
|
static Int
|
|
|
|
p_atom_gc(void)
|
|
|
|
{
|
2005-05-31 20:42:28 +01:00
|
|
|
return TRUE;
|
2002-06-05 04:59:50 +01:00
|
|
|
#ifndef FIXED_STACKS
|
|
|
|
atom_gc();
|
|
|
|
#endif /* FIXED_STACKS */
|
2005-05-31 20:42:28 +01:00
|
|
|
return TRUE;
|
2002-06-05 04:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_inform_agc(void)
|
|
|
|
{
|
|
|
|
Term tn = MkIntegerTerm(tot_agc_time);
|
|
|
|
Term tt = MkIntegerTerm(agc_calls);
|
|
|
|
Term ts = MkIntegerTerm(tot_agc_recovered);
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(tn, ARG2) && Yap_unify(tt, ARG1) && Yap_unify(ts, ARG3));
|
2002-06-05 04:59:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_init_agc(void)
|
2002-06-05 04:59:50 +01:00
|
|
|
{
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$atom_gc", 0, p_atom_gc, HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$inform_agc", 3, p_inform_agc, HiddenPredFlag);
|
2002-06-04 19:21:55 +01:00
|
|
|
}
|