/// <summary> /// Given module handle, locate the metadata reader. Return null when not found. /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> /// <returns>Reader for the embedded metadata blob in the module, null when not found</returns> public MetadataReader GetMetadataReaderForModule(IntPtr moduleHandle) { ModuleMap moduleMap = _loadedModuleMap; int moduleIndex; if (moduleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out moduleIndex)) { return moduleMap.Modules[moduleIndex].MetadataReader; } return null; }
/// <summary> /// Try to Locate module info for a given module. Returns false when not found. /// gets initialized. /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> public bool TryGetModuleInfoByHandle(IntPtr moduleHandle, out ModuleInfo moduleInfo) { ModuleMap moduleMap = _loadedModuleMap; int moduleIndex; if (moduleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out moduleIndex)) { moduleInfo = moduleMap.Modules[moduleIndex]; return true; } moduleInfo = null; return false; }
/// <summary> /// Initialize the module enumerator state machine and locate the preferred module index. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal NativeFormatModuleInfoEnumerator(ModuleMap moduleMap, TypeManagerHandle preferredModuleHandle) { _modules = moduleMap.Modules; _preferredIndex = -1; _iterationIndex = -1; _currentModule = null; if (!preferredModuleHandle.IsNull && !moduleMap.HandleToModuleIndex.TryGetValue(preferredModuleHandle, out _preferredIndex)) { Environment.FailFast("Invalid module requested in enumeration: " + preferredModuleHandle.LowLevelToString()); } }
/// <summary> /// Try to Locate module info for a given module. Returns false when not found. /// gets initialized. /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> /// <param name="moduleInfo">Found module info</param> public bool TryGetModuleInfoByHandle(TypeManagerHandle moduleHandle, out ModuleInfo moduleInfo) { ModuleMap moduleMap = _loadedModuleMap; int moduleIndex; if (moduleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out moduleIndex)) { moduleInfo = moduleMap.Modules[moduleIndex]; return(true); } moduleInfo = null; return(false); }
/// <summary> /// Initialize the module enumerator state machine and locate the preferred module index. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal ModuleInfoEnumerator(ModuleMap moduleMap, IntPtr preferredModuleHandle) { _modules = moduleMap.Modules; _preferredIndex = -1; _iterationIndex = -1; _currentModule = null; if (preferredModuleHandle != default(IntPtr) && !moduleMap.HandleToModuleIndex.TryGetValue(preferredModuleHandle, out _preferredIndex)) { Environment.FailFast("Invalid module requested in enumeration: " + preferredModuleHandle.LowLevelToString()); } }
/// <summary> /// Register all modules which were added (Registered) to the runtime and are not already registered with the TypeLoader. /// </summary> /// <param name="moduleType">Type to assign to all new modules.</param> public void RegisterNewModules(ModuleType moduleType) { // prevent multiple threads from registering modules concurrently using (LockHolder.Hold(_moduleRegistrationLock)) { // Fetch modules that have already been registered with the runtime int loadedModuleCount = RuntimeAugments.GetLoadedModules(null); IntPtr[] loadedModuleHandles = new IntPtr[loadedModuleCount]; int loadedModuleCountUpdated = RuntimeAugments.GetLoadedModules(loadedModuleHandles); Debug.Assert(loadedModuleCount == loadedModuleCountUpdated); LowLevelList <IntPtr> newModuleHandles = new LowLevelList <IntPtr>(loadedModuleHandles.Length); foreach (IntPtr moduleHandle in loadedModuleHandles) { // Skip already registered modules. int oldModuleIndex; if (_loadedModuleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out oldModuleIndex)) { continue; } newModuleHandles.Add(moduleHandle); } // Copy existing modules to new dictionary int oldModuleCount = _loadedModuleMap.Modules.Length; ModuleInfo[] updatedModules = new ModuleInfo[oldModuleCount + newModuleHandles.Count]; if (oldModuleCount > 0) { Array.Copy(_loadedModuleMap.Modules, 0, updatedModules, 0, oldModuleCount); } for (int newModuleIndex = 0; newModuleIndex < newModuleHandles.Count; newModuleIndex++) { ModuleInfo newModuleInfo = new ModuleInfo(newModuleHandles[newModuleIndex], moduleType); updatedModules[oldModuleCount + newModuleIndex] = newModuleInfo; if (_moduleRegistrationCallbacks != null) { _moduleRegistrationCallbacks(newModuleInfo); } } // Atomically update the module map _loadedModuleMap = new ModuleMap(updatedModules); } }
/// <summary> /// Register initially (eagerly) loaded modules. /// </summary> internal ModuleList() { _loadedModuleMap = new ModuleMap(new ModuleInfo[0]); _moduleRegistrationCallbacks = default(Action<ModuleInfo>); _moduleRegistrationLock = new Lock(); RegisterNewModules(ModuleType.Eager); IntPtr systemObjectModule = RuntimeAugments.GetModuleFromTypeHandle(RuntimeAugments.RuntimeTypeHandleOf<object>()); foreach (ModuleInfo m in _loadedModuleMap.Modules) { if (m.Handle == systemObjectModule) { _systemModule = m; break; } } }
public void RegisterModule(ModuleInfo newModuleInfo) { // prevent multiple threads from registering modules concurrently using (LockHolder.Hold(_moduleRegistrationLock)) { // Copy existing modules to new dictionary int oldModuleCount = _loadedModuleMap.Modules.Length; ModuleInfo[] updatedModules = new ModuleInfo[oldModuleCount + 1]; if (oldModuleCount > 0) { Array.Copy(_loadedModuleMap.Modules, 0, updatedModules, 0, oldModuleCount); } updatedModules[oldModuleCount] = newModuleInfo; _moduleRegistrationCallbacks?.Invoke(newModuleInfo); // Atomically update the module map _loadedModuleMap = new ModuleMap(updatedModules); } }
/// <summary> /// Register initially (eagerly) loaded modules. /// </summary> internal ModuleList() { _loadedModuleMap = new ModuleMap(new ModuleInfo[0]); _moduleRegistrationCallbacks = default(Action <ModuleInfo>); _moduleRegistrationLock = new Lock(); // Fetch modules that have already been registered with the runtime int loadedModuleCount = RuntimeAugments.GetLoadedModules(null); IntPtr[] loadedModuleHandles = new IntPtr[loadedModuleCount]; int loadedModuleCountUpdated = RuntimeAugments.GetLoadedModules(loadedModuleHandles); Debug.Assert(loadedModuleCount == loadedModuleCountUpdated); foreach (IntPtr moduleHandle in loadedModuleHandles) { RegisterModule(moduleHandle); } }
/// <summary> /// Given module handle, locate the metadata reader. Return null when not found. /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> /// <returns>Reader for the embedded metadata blob in the module, null when not found</returns> public MetadataReader GetMetadataReaderForModule(TypeManagerHandle moduleHandle) { ModuleMap moduleMap = _loadedModuleMap; int moduleIndex; if (moduleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out moduleIndex)) { NativeFormatModuleInfo moduleInfo = moduleMap.Modules[moduleIndex] as NativeFormatModuleInfo; if (moduleInfo != null) { return(moduleInfo.MetadataReader); } else { return(null); } } return(null); }
/// <summary> /// Construct the underlying module info enumerator used to iterate the module map /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal MetadataReaderEnumerator(ModuleMap moduleMap, IntPtr preferredModuleHandle) { _moduleInfoEnumerator = new ModuleInfoEnumerator(moduleMap, preferredModuleHandle); }
/// <summary> /// Locate module info for a given module. Fail if not found or before the module registry /// gets initialized. Must only be called for modules described as native format (not the mrt module, or an ECMA module) /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> public NativeFormatModuleInfo GetModuleInfoByHandle(TypeManagerHandle moduleHandle) { ModuleMap moduleMap = _loadedModuleMap; return((NativeFormatModuleInfo)moduleMap.Modules[moduleMap.HandleToModuleIndex[moduleHandle]]); }
/// <summary> /// Store module map and preferred module to pass to the enumerator upon construction. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal NativeFormatModuleInfoEnumerable(ModuleMap moduleMap, IntPtr preferredModuleHandle) { _moduleMap = moduleMap; _preferredModuleHandle = preferredModuleHandle; }
/// <summary> /// Construct the underlying module info enumerator used to iterate the module map /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal MetadataReaderEnumerator(ModuleMap moduleMap, TypeManagerHandle preferredModuleHandle) { _moduleInfoEnumerator = new NativeFormatModuleInfoEnumerator(moduleMap, preferredModuleHandle); }
/// <summary> /// Register all modules which were added (Registered) to the runtime and are not already registered with the TypeLoader. /// </summary> /// <param name="moduleType">Type to assign to all new modules.</param> public void RegisterNewModules(ModuleType moduleType) { // prevent multiple threads from registering modules concurrently using (LockHolder.Hold(_moduleRegistrationLock)) { // Fetch modules that have already been registered with the runtime int loadedModuleCount = RuntimeAugments.GetLoadedModules(null); TypeManagerHandle[] loadedModuleHandles = new TypeManagerHandle[loadedModuleCount]; int loadedModuleCountUpdated = RuntimeAugments.GetLoadedModules(loadedModuleHandles); Debug.Assert(loadedModuleCount == loadedModuleCountUpdated); LowLevelList <TypeManagerHandle> newModuleHandles = new LowLevelList <TypeManagerHandle>(loadedModuleHandles.Length); foreach (TypeManagerHandle moduleHandle in loadedModuleHandles) { // Skip already registered modules. int oldModuleIndex; if (_loadedModuleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out oldModuleIndex)) { continue; } newModuleHandles.Add(moduleHandle); } // Copy existing modules to new dictionary int oldModuleCount = _loadedModuleMap.Modules.Length; ModuleInfo[] updatedModules = new ModuleInfo[oldModuleCount + newModuleHandles.Count]; if (oldModuleCount > 0) { Array.Copy(_loadedModuleMap.Modules, 0, updatedModules, 0, oldModuleCount); } for (int newModuleIndex = 0; newModuleIndex < newModuleHandles.Count; newModuleIndex++) { ModuleInfo newModuleInfo; unsafe { byte *pBlob; uint cbBlob; if (RuntimeAugments.FindBlob(newModuleHandles[newModuleIndex], (int)ReflectionMapBlob.EmbeddedMetadata, new IntPtr(&pBlob), new IntPtr(&cbBlob))) { newModuleInfo = new NativeFormatModuleInfo(newModuleHandles[newModuleIndex], moduleType, (IntPtr)pBlob, (int)cbBlob); } else { newModuleInfo = new ModuleInfo(newModuleHandles[newModuleIndex], moduleType); } } updatedModules[oldModuleCount + newModuleIndex] = newModuleInfo; if (_moduleRegistrationCallbacks != null) { _moduleRegistrationCallbacks(newModuleInfo); } } // Atomically update the module map _loadedModuleMap = new ModuleMap(updatedModules); } }
/// <summary> /// Construct the underlying module info enumerator used to iterate the module map /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal ModuleHandleEnumerator(ModuleMap moduleMap, TypeManagerHandle preferredModuleHandle) { _moduleInfoEnumerator = new ModuleInfoEnumerator(moduleMap, preferredModuleHandle); }
/// <summary> /// Store module map and preferred module to pass to the enumerator upon construction. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal MetadataReaderEnumerable(ModuleMap moduleMap, TypeManagerHandle preferredModuleHandle) { _moduleMap = moduleMap; _preferredModuleHandle = preferredModuleHandle; }
/// <summary> /// Register a new module. Call all module registration callbacks. /// </summary> /// <param name="moduleHandle">Module handle to register</param> public void RegisterModule(IntPtr newModuleHandle) { // prevent multiple threads from registering modules concurrently using (LockHolder.Hold(_moduleRegistrationLock)) { // Don't allow double module registration int oldModuleIndex; if (_loadedModuleMap.HandleToModuleIndex.TryGetValue(newModuleHandle, out oldModuleIndex)) { Environment.FailFast("Module " + newModuleHandle.LowLevelToString() + " is being registered for the second time"); } ModuleInfo newModuleInfo = new ModuleInfo(newModuleHandle); // Copy existing modules to new dictionary int oldModuleCount = _loadedModuleMap.Modules.Length; ModuleInfo[] updatedModules = new ModuleInfo[oldModuleCount + 1]; if (oldModuleCount > 0) { Array.Copy(_loadedModuleMap.Modules, 0, updatedModules, 0, oldModuleCount); } updatedModules[oldModuleCount] = newModuleInfo; // Atomically update the module map _loadedModuleMap = new ModuleMap(updatedModules); if (_moduleRegistrationCallbacks != null) { _moduleRegistrationCallbacks(newModuleInfo); } } }
/// <summary> /// Register initially (eagerly) loaded modules. /// </summary> internal ModuleList() { _loadedModuleMap = new ModuleMap(new ModuleInfo[0]); _moduleRegistrationCallbacks = default(Action<ModuleInfo>); _moduleRegistrationLock = new Lock(); // Fetch modules that have already been registered with the runtime int loadedModuleCount = RuntimeAugments.GetLoadedModules(null); IntPtr[] loadedModuleHandles = new IntPtr[loadedModuleCount]; int loadedModuleCountUpdated = RuntimeAugments.GetLoadedModules(loadedModuleHandles); Debug.Assert(loadedModuleCount == loadedModuleCountUpdated); foreach (IntPtr moduleHandle in loadedModuleHandles) { RegisterModule(moduleHandle); } }
/// <summary> /// Register initially (eagerly) loaded modules. /// </summary> internal ModuleList() { _loadedModuleMap = new ModuleMap(new ModuleInfo[0]); _moduleRegistrationCallbacks = default(Action<ModuleInfo>); _moduleRegistrationLock = new Lock(); RegisterNewModules(ModuleType.Eager); }
/// <summary> /// Store module map and preferred module to pass to the enumerator upon construction. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal MetadataReaderEnumerable(ModuleMap moduleMap, IntPtr preferredModuleHandle) { _moduleMap = moduleMap; _preferredModuleHandle = preferredModuleHandle; }
/// <summary> /// Store module map and preferred module to pass to the enumerator upon construction. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal ModuleHandleEnumerable(ModuleMap moduleMap, IntPtr preferredModuleHandle) { _moduleMap = moduleMap; _preferredModuleHandle = preferredModuleHandle; }
/// <summary> /// Locate module info for a given module. Fail if not found or before the module registry /// gets initialized. /// </summary> /// <param name="moduleHandle">Handle of module to look up</param> public ModuleInfo GetModuleInfoByHandle(IntPtr moduleHandle) { ModuleMap moduleMap = _loadedModuleMap; return(moduleMap.Modules[moduleMap.HandleToModuleIndex[moduleHandle]]); }
/// <summary> /// Store module map and preferred module to pass to the enumerator upon construction. /// </summary> /// <param name="moduleMap">Module map to enumerate</param> /// <param name="preferredModuleHandle">Optional module handle to enumerate first</param> internal NativeFormatModuleInfoEnumerable(ModuleMap moduleMap, TypeManagerHandle preferredModuleHandle) { _moduleMap = moduleMap; _preferredModuleHandle = preferredModuleHandle; }
/// <summary> /// Register all modules which were added (Registered) to the runtime and are not already registered with the TypeLoader. /// </summary> /// <param name="moduleType">Type to assign to all new modules.</param> public void RegisterNewModules(ModuleType moduleType) { // prevent multiple threads from registering modules concurrently using (LockHolder.Hold(_moduleRegistrationLock)) { // Fetch modules that have already been registered with the runtime int loadedModuleCount = RuntimeAugments.GetLoadedModules(null); IntPtr[] loadedModuleHandles = new IntPtr[loadedModuleCount]; int loadedModuleCountUpdated = RuntimeAugments.GetLoadedModules(loadedModuleHandles); Debug.Assert(loadedModuleCount == loadedModuleCountUpdated); LowLevelList<IntPtr> newModuleHandles = new LowLevelList<IntPtr>(loadedModuleHandles.Length); foreach (IntPtr moduleHandle in loadedModuleHandles) { // Skip already registered modules. int oldModuleIndex; if (_loadedModuleMap.HandleToModuleIndex.TryGetValue(moduleHandle, out oldModuleIndex)) { continue; } newModuleHandles.Add(moduleHandle); } // Copy existing modules to new dictionary int oldModuleCount = _loadedModuleMap.Modules.Length; ModuleInfo[] updatedModules = new ModuleInfo[oldModuleCount + newModuleHandles.Count]; if (oldModuleCount > 0) { Array.Copy(_loadedModuleMap.Modules, 0, updatedModules, 0, oldModuleCount); } for (int newModuleIndex = 0; newModuleIndex < newModuleHandles.Count; newModuleIndex++) { ModuleInfo newModuleInfo = new ModuleInfo(newModuleHandles[newModuleIndex], moduleType); updatedModules[oldModuleCount + newModuleIndex] = newModuleInfo; if (_moduleRegistrationCallbacks != null) { _moduleRegistrationCallbacks(newModuleInfo); } } // Atomically update the module map _loadedModuleMap = new ModuleMap(updatedModules); } }