/* $Id: $ */ /** @file * * kLdr - The Dynamic Loader, Dyld module methods. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr 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. * * kLdr 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 kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include "kLdrInternal.h" #include "kLdrHlp.h" /******************************************************************************* * Defined Constants And Macros * *******************************************************************************/ /** @def KLDRDYLDMOD_ASSERT * Assert that an expression is true when KLDRDYLD_STRICT is defined. */ #ifdef KLDRDYLD_STRICT # define KLDRDYLDMOD_ASSERT(expr) kldrHlpAssert(expr) #else # define KLDRDYLDMOD_ASSERT(expr) do {} while (0) #endif void kldrDyldModUnloadPrerequisites(PKLDRDYLDMOD pMod) { } void kldrDyldModDeref(PKLDRDYLDMOD pMod) { /* validate input */ KLDRDYLDMOD_ASSERT(pMod->enmState > KLDRSTATE_INVALID && pMod->enmState < KLDRSTATE_END); KLDRDYLDMOD_ASSERT(pMod->cRefs > 0); KLDRDYLDMOD_ASSERT(pMod->cRefs >= pMod->cDepRefs + pMod->cDynRefs); /* decrement. */ if (pMod->cRefs > 0) pMod->cRefs--; /* * Drop prerequisites for stats that implies that no recursion can have taken place yet. * This ASSUMES that we're only dereferencing modules when an operation completes. * (This is *required* for the reloaded state.) */ switch (pMod->enmState) { case KLDRSTATE_MAPPED: case KLDRSTATE_LOADED_PREREQUISITES: case KLDRSTATE_FIXED_UP: case KLDRSTATE_RELOADED: kldrDyldModUnloadPrerequisites(pMod); pMod->enmState = KLDRSTATE_PENDING_GC; break; case KLDRSTATE_PENDING_INITIALIZATION: case KLDRSTATE_INITIALIZING: case KLDRSTATE_GOOD: case KLDRSTATE_PENDING_TERMINATION: case KLDRSTATE_TERMINATING: case KLDRSTATE_PENDING_GC: case KLDRSTATE_GC: case KLDRSTATE_PENDING_DESTROY: break; default: KLDRDYLDMOD_ASSERT(!"Invalid deref state"); break; } /* * Also drop prerequisites if noone is referencing the module. */ if (!pMod->cDepRefs && !pMod->cDynRefs) { switch (pMod->enmState) { case KLDRSTATE_MAPPED: case KLDRSTATE_LOADED_PREREQUISITES: case KLDRSTATE_FIXED_UP: case KLDRSTATE_RELOADED: /* already dropped. */ break; case KLDRSTATE_PENDING_INITIALIZATION: kldrDyldModUnloadPrerequisites(pMod); pMod->enmState = KLDRSTATE_PENDING_GC; break; case KLDRSTATE_GOOD: pMod->enmState = KLDRSTATE_PENDING_TERMINATION; case KLDRSTATE_PENDING_TERMINATION: case KLDRSTATE_PENDING_GC: kldrDyldModUnloadPrerequisites(pMod); break; case KLDRSTATE_TERMINATING: case KLDRSTATE_GC: case KLDRSTATE_PENDING_DESTROY: break; default: KLDRDYLDMOD_ASSERT(!"Invalid deref state (b)"); break; } } /* * If there are no references whatsoever now and the module * is pending destruction, destroy it. */ if ( !pMod->cRefs && ( pMod->enmState == KLDRSTATE_PENDING_DESTROY || pMod->enmState == KLDRSTATE_GC)) kldrDyldModDestroy(pMod); }