core/NativeOwner.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jan 22 23:18:27 2013 -0500 (2013-01-22)
changeset 3749 4dc3d723c490
parent 2226 d10b38524be3
permissions -rw-r--r--
Triggering build against new mac libprotobuf.
     1 #include "NativeOwner.h"
     2 #include "ShareSys.h"
     3 #include "PluginSys.h"
     4 
     5 CNativeOwner::CNativeOwner() : m_nMarkSerial(0)
     6 {
     7 }
     8 
     9 void CNativeOwner::SetMarkSerial(unsigned int serial)
    10 {
    11 	m_nMarkSerial = serial;
    12 }
    13 
    14 unsigned int CNativeOwner::GetMarkSerial()
    15 {
    16 	return m_nMarkSerial;
    17 }
    18 
    19 void CNativeOwner::AddDependent(CPlugin *pPlugin)
    20 {
    21 	m_Dependents.push_back(pPlugin);
    22 }
    23 
    24 void CNativeOwner::AddWeakRef(const WeakNative & ref)
    25 {
    26 	m_WeakRefs.push_back(ref);
    27 }
    28 
    29 void CNativeOwner::AddNatives(const sp_nativeinfo_t *natives)
    30 {
    31 	NativeEntry *pEntry;
    32 
    33 	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
    34 	{
    35 		if ((pEntry = g_ShareSys.AddNativeToCache(this, &natives[i])) == NULL)
    36 		{
    37 			continue;
    38 		}
    39 
    40 		m_Natives.push_back(pEntry);
    41 	}
    42 }
    43 
    44 void CNativeOwner::PropogateMarkSerial(unsigned int serial)
    45 {
    46 	CNativeOwner *pOwner;
    47 	List<CPlugin *>::iterator iter;
    48 
    49 	for (iter = m_Dependents.begin();
    50 		 iter != m_Dependents.end();
    51 		 iter++)
    52 	{
    53 		pOwner = (*iter);
    54 		pOwner->SetMarkSerial(serial);
    55 	}
    56 }
    57 
    58 void CNativeOwner::UnbindWeakRef(const WeakNative & ref)
    59 {
    60 	sp_native_t *native;
    61 	IPluginContext *pContext;
    62 
    63 	pContext = ref.pl->GetBaseContext();
    64 	if ((pContext->GetNativeByIndex(ref.idx, &native)) == SP_ERROR_NONE)
    65 	{
    66 		/* If there is no reference, the native must be unbound */
    67 		if (ref.entry == NULL)
    68 		{
    69 			native->status = SP_NATIVE_UNBOUND;
    70 			native->pfn = NULL;
    71 		}
    72 		/* If we've cached a reference, it's a core native we can 
    73 		 * rebind back to its original (this was a replacement).
    74 		 */
    75 		else
    76 		{
    77 			native->pfn = ref.entry->func;
    78 		}
    79 	}
    80 }
    81 
    82 void CNativeOwner::DropEverything()
    83 {
    84 	NativeEntry *pEntry;
    85 	List<WeakNative>::iterator iter;
    86 	List<NativeEntry *>::iterator ntv_iter;
    87 
    88 	/* Unbind and remove all weak references to us */
    89 	iter = m_WeakRefs.begin();
    90 	while (iter != m_WeakRefs.end())
    91 	{
    92 		UnbindWeakRef((*iter));
    93 		iter = m_WeakRefs.erase(iter);
    94 	}
    95 
    96 	/* Unmark our replacement natives */
    97 	ntv_iter = m_ReplacedNatives.begin();
    98 	while (ntv_iter != m_ReplacedNatives.end())
    99 	{
   100 		pEntry = (*ntv_iter);
   101 		pEntry->replacement.func = NULL;
   102 		pEntry->replacement.owner = NULL;
   103 		ntv_iter = m_ReplacedNatives.erase(ntv_iter);
   104 	}
   105 
   106 	/* Strip all of our natives from the cache */
   107 	ntv_iter = m_Natives.begin();
   108 	while (ntv_iter != m_Natives.end())
   109 	{
   110 		g_ShareSys.ClearNativeFromCache(this, (*ntv_iter)->name);
   111 		ntv_iter = m_Natives.erase(ntv_iter);
   112 	}
   113 }
   114 
   115 void CNativeOwner::DropWeakRefsTo(CPlugin *pPlugin)
   116 {
   117 	List<WeakNative>::iterator iter;
   118 
   119 	iter = m_WeakRefs.begin();
   120 	while (iter != m_WeakRefs.end())
   121 	{
   122 		WeakNative & ref = (*iter);
   123 
   124 		if (ref.pl == pPlugin)
   125 		{
   126 			iter = m_WeakRefs.erase(iter);
   127 		}
   128 		else
   129 		{
   130 			iter++;
   131 		}
   132 	}
   133 }
   134 
   135 void CNativeOwner::DropRefsTo(CPlugin *pPlugin)
   136 {
   137 	m_Dependents.remove(pPlugin);
   138 	DropWeakRefsTo(pPlugin);
   139 }
   140 
   141 void CNativeOwner::AddReplacedNative(NativeEntry *pEntry)
   142 {
   143 	m_ReplacedNatives.push_back(pEntry);
   144 }