public static int Main() { Console.WriteLine("Beginning phase 1"); GCUtil.AllocWithGaps(); Console.WriteLine("phase 1 complete"); // losing all live references to the unpinned byte arrays // this will fragment the heap with ~50 byte holes GCUtil.FreeNonPins(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Console.WriteLine("Beginning phase 2"); bList = new List <byte[]>(); for (int i = 0; i < 1024 * 1024; i++) { byte[] unpinned = new byte[50]; bList.Add(unpinned); } Console.WriteLine("phase 2 complete"); GC.KeepAlive(gchList); GC.KeepAlive(bList); return(100); }
public static int Main() { int[] arr = new int[1000]; GCHandle[] arrhandle = new GCHandle[10000]; // array of handles to the same object IntPtr[] oldaddress = new IntPtr[10000]; // store old addresses IntPtr[] newaddress = new IntPtr[10000]; // store new addresses for (int i = 0; i < 10000; i++) { arrhandle[i] = GCUtil.Alloc(arr, GCHandleType.Pinned); oldaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]); } GC.Collect(); GC.WaitForPendingFinalizers(); for (int i = 0; i < 10000; i++) { newaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]); } for (int i = 0; i < 10000; i++) { if (oldaddress[i] != newaddress[i]) { Console.WriteLine("Test failed"); return 1; } } Console.WriteLine("Test passed"); return 100; }
private void ShowProp(int index) { MacroModule mod = GApp.MacroManager.GetModule(index); Keys key = Keys.None; IDictionaryEnumerator ie = _keyToModule.GetEnumerator(); while (ie.MoveNext()) { if (ie.Value == mod) { key = (Keys)(ie.Key); break; } } ModuleProperty dlg = new ModuleProperty(this, mod, key); if (GCUtil.ShowModalDialog(this, dlg) == DialogResult.OK) { GApp.MacroManager.ReplaceModule(GApp.MacroManager.GetModule(index), dlg.Module); GApp.Options.Commands.ModifyMacroKey(dlg.Module.Index, dlg.ShortCut & Keys.Modifiers, dlg.ShortCut & Keys.KeyCode); ListViewItem li = _list.Items[index]; li.Text = dlg.Module.Title; li.SubItems[1].Text = dlg.Module.Path; li.SubItems[2].Text = UILibUtil.KeyString(dlg.ShortCut); li.SubItems[3].Text = GetInfoString(dlg.Module); _keyToModule.Remove(key); if (dlg.ShortCut != Keys.None) { _keyToModule.Add(dlg.ShortCut, dlg.Module); } AdjustUI(); } }
public Task WriteCloseFragmentAsync(WebSocketCloseStatus closeStatus, string statusDescription) { TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); // Callback will always be called (since it is responsible for cleanup), even if completed synchronously CompletionCallback callback = (hrError, cbIO, fUtf8Encoded, fFinalFragment, fClose) => { try { ThrowExceptionForHR(hrError); tcs.TrySetResult(null); // regular completion } catch (Exception ex) { tcs.TrySetException(ex); // exceptional completion } }; IntPtr completionContext = GCUtil.RootObject(callback); // Call the underlying implementation; SendConnectionClose should never throw an exception bool completionExpected; int hr = _context.SendConnectionClose( fAsync: true, uStatusCode: (ushort)closeStatus, szReason: statusDescription, // don't need to pin string: CLR marshaler handles managed to unmanaged conversion, and IIS makes local copy for duration of async operation pfnCompletion: _asyncThunkAddress, pvCompletionContext: completionContext, pfCompletionExpected: out completionExpected); if (!completionExpected) { // Completed synchronously or error; the thunk and callback together handle cleanup AsyncCallbackThunk(hr, completionContext, cbIO: 0, fUtf8Encoded: true, fFinalFragment: true, fClose: false); } return(tcs.Task); }
public static int Main() { int i = 10; Object temp1, temp2; GCHandle handle = GCUtil.Alloc(i, GCHandleType.Pinned); temp1 = GCUtil.GetTarget(handle); Console.WriteLine(temp1); GC.Collect(); GC.WaitForPendingFinalizers(); temp2 = GCUtil.GetTarget(handle); Console.WriteLine(temp2); if (temp1 == temp2) { Console.WriteLine("Test passed"); return(100); } else { Console.WriteLine("Test failed"); return(1); } }
// This thunk dispatches to the appropriate instance continuation when an asynchronous event completes private static void AsyncCallbackThunk(int hrError, IntPtr pvCompletionContext, int cbIO, bool fUtf8Encoded, bool fFinalFragment, bool fClose) { // Calling UnrootObject also makes the callback and everything it references eligible for garbage collection CompletionCallback callback = (CompletionCallback)GCUtil.UnrootObject(pvCompletionContext); callback(hrError, cbIO, fUtf8Encoded, fFinalFragment, fClose); }
private void OnOpenPrivateKey(object sender, System.EventArgs e) { string fn = GCUtil.SelectPrivateKeyFileByDialog(this); if (fn != null) { _privateKeyBox.Text = fn; } }
private void OnSelectLogFile(object sender, EventArgs args) { string fn = GCUtil.SelectLogFileByDialog(this); if (fn != null) { _fileNameBox.Text = fn; } }
private void SelectLog(object sender, System.EventArgs e) { string fn = GCUtil.SelectLogFileByDialog(this); if (fn != null) { _logFileBox.Text = fn; } }
private void OpenKeyFile(object sender, EventArgs args) { string fn = GCUtil.SelectPrivateKeyFileByDialog(this); if (fn != null) { _tKeyFile.Text = fn; } }
private void OnSelectBackgroundImage(object sender, EventArgs args) { string t = GCUtil.SelectPictureFileByDialog(this); if (t != null) { _backgroundImageBox.Text = t; _profile.BackgroundImageFileName = t; } }
private void OnSelectBackgroundImage(object sender, EventArgs args) { string t = GCUtil.SelectPictureFileByDialog(FindForm()); if (t != null) { _backgroundImageBox.Text = t; _defaultFileDir = GUtil.FileToDir(t); } }
private void OnOpenPrivateKey(object sender, System.EventArgs e) { string fn = GCUtil.SelectPrivateKeyFileByDialog(this); if (fn != null) { _privateKeyFile.Text = fn; } _privateKeySelect.Focus(); //どっちにしても次のフォーカスは鍵選択ボタンへ }
public static int Main() { float[] arr = new float[100]; GCHandle handle1 = GCUtil.Alloc(arr, GCHandleType.Pinned); GCHandle handle2 = GCUtil.Alloc(arr, GCHandleType.Normal); IntPtr oldaddr, newaddr; oldaddr = GCUtil.AddrOfPinnedObject(handle1); Console.WriteLine("Address of obj: {0}", oldaddr); GC.Collect(); GC.WaitForPendingFinalizers(); // handle1.Free(); // arr should only have normal handle now GCUtil.Free(ref handle1); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); try { Console.WriteLine("Address of obj: {0}", handle1.AddrOfPinnedObject()); } catch (Exception e) { Console.WriteLine("Caught: " + e); } arr = null; GC.Collect(); // Pinning the arr again..it should have moved GCHandle handle3 = GCUtil.Alloc(arr, GCHandleType.Pinned); newaddr = GCUtil.AddrOfPinnedObject(handle3); Console.WriteLine("Address of obj: {0}", newaddr); if (oldaddr == newaddr) { Console.WriteLine("Test failed!"); return(1); } else { Console.WriteLine("Test passed!"); return(100); } }
private void OnAddButtonClicked(object sender, EventArgs args) { EditEnvVariable d = new EditEnvVariable(this); d.IsNewVariable = true; if (GCUtil.ShowModalDialog(this, d) == DialogResult.OK) { ListViewItem li = new ListViewItem(d.VarName); li = _list.Items.Add(li); li.SubItems.Add(d.VarValue); li.Selected = true; } }
private void OnEditButtonClicked(object sender, EventArgs args) { ListViewItem li = _list.SelectedItems[0]; EditEnvVariable d = new EditEnvVariable(this); d.VarName = li.Text; d.VarValue = li.SubItems[1].Text; d.IsNewVariable = false; if (GCUtil.ShowModalDialog(this, d) == DialogResult.OK) { li.Text = d.VarName; li.SubItems[1].Text = d.VarValue; } }
public static int Main() { CreateAndReleaseFinalizable(); TestFramework.LogInformation("First Alloc"); GCUtil.Alloc(1024 * 1024, 50); GC.Collect(); GC.WaitForPendingFinalizers(); TestFramework.LogInformation("Second Alloc"); GCUtil.Alloc(1024 * 1024, 50); GCUtil.FreePins(); TestFramework.LogInformation("Test passed"); return(100); }
// Called from native code to get the managed principal for a given request // If the return value is non-zero, the caller must free the returned GCHandle internal static IntPtr GetManagedPrincipalHandler(IntPtr pRootedObjects, int requestingAppDomainId) { // DevDiv 375079: Server.TransferRequest can be used to transfer requests to different applications, // which means that we might be trying to pass a GCHandle to the IPrincipal object to a different // AppDomain, which is disallowed. If this happens, we just tell our caller that we can't give him // a managed IPrincipal object. if (requestingAppDomainId != AppDomain.CurrentDomain.Id) { return(IntPtr.Zero); } IPrincipal principal = RootedObjects.FromPointer(pRootedObjects).Principal; return(GCUtil.RootObject(principal)); }
private void OnAddButtonClicked(object sender, EventArgs args) { ModuleProperty dlg = new ModuleProperty(this, null, Keys.None); if (GCUtil.ShowModalDialog(this, dlg) == DialogResult.OK) { AddListItem(dlg.Module, dlg.ShortCut); GApp.MacroManager.AddModule(dlg.Module); if (dlg.ShortCut != Keys.None) { _keyToModule.Add(dlg.ShortCut, dlg.Module); } AdjustUI(); } }
public static int Main() { Test t = new Test(); TestFramework.LogInformation("First Alloc"); GCUtil.Alloc(1024 * 1024, 50); t = null; GC.Collect(); GC.WaitForPendingFinalizers(); TestFramework.LogInformation("Second Alloc"); GCUtil.Alloc(1024 * 1024, 50); GCUtil.FreePins(); TestFramework.LogInformation("Test passed"); return(100); }
public static int Main() { Console.WriteLine("First Alloc"); GCUtil.Alloc(1024 * 1024 * 4, 30); GCUtil.FreeNonPins(); GC.Collect(); Console.WriteLine("Second Alloc"); GCUtil.Alloc(1024 * 1024 * 4, 50); GCUtil.FreeNonPins(); GC.Collect(); GCUtil.FreePins(); Console.WriteLine("Test passed"); return(100); }
private void OnEditColorEscapeSequence(object sender, EventArgs args) { EditEscapeSequenceColor dlg = new EditEscapeSequenceColor(_fontSample.BackColor, _fontSample.ForeColor, _ESColorSet); if (GCUtil.ShowModalDialog(FindForm(), dlg) == DialogResult.OK) { _ESColorSet = dlg.Result; _bgColorBox.SelectedColor = dlg.GBackColor; _textColorBox.SelectedColor = dlg.GForeColor; _fontSample.ForeColor = dlg.GForeColor; _fontSample.BackColor = dlg.GBackColor; _fontSample.Invalidate(); _bgColorBox.Invalidate(); _textColorBox.Invalidate(); } }
public Task WriteFragmentAsync(ArraySegment <byte> buffer, bool isUtf8Encoded, bool isFinalFragment) { TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); // The buffer will be read from asynchronously by unmanaged code, so we require that it remain pinned PinnedArraySegment <byte> pinnedBuffer = new PinnedArraySegment <byte>(buffer); // Callback will always be called (since it is responsible for cleanup), even if completed synchronously CompletionCallback callback = (hrError, cbIO, fUtf8Encoded, fFinalFragment, fClose) => { try { ThrowExceptionForHR(hrError); tcs.TrySetResult(null); // regular completion } catch (Exception ex) { tcs.TrySetException(ex); // exceptional completion } finally { // Always free the buffer to prevent a memory leak pinnedBuffer.Dispose(); } }; IntPtr completionContext = GCUtil.RootObject(callback); // update perf counter with count of data written to wire _perfCounters.IncrementCounter(AppPerfCounter.REQUEST_BYTES_OUT_WEBSOCKETS, pinnedBuffer.Count); // Call the underlying implementation; WriteFragment should never throw an exception int bytesSent = pinnedBuffer.Count; bool completionExpected; int hr = _context.WriteFragment( pData: pinnedBuffer.Pointer, pcbSent: ref bytesSent, fAsync: true, fUtf8Encoded: isUtf8Encoded, fFinalFragment: isFinalFragment, pfnCompletion: _asyncThunkAddress, pvCompletionContext: completionContext, pfCompletionExpected: out completionExpected); if (!completionExpected) { // Completed synchronously or error; the thunk and callback together handle cleanup AsyncCallbackThunk(hr, completionContext, bytesSent, isUtf8Encoded, isFinalFragment, fClose: false); } return(tcs.Task); }
private void OnOK(object sender, EventArgs args) { this.DialogResult = DialogResult.None; if (_homeDirectoryBox.Text.Length == 0) { GUtil.Warning(this, GApp.Strings.GetString("Message.CygwinLoginDialog.EmptyHomeDirectory")); } else if (_shellBox.Text.Length == 0) { GUtil.Warning(this, GApp.Strings.GetString("Message.CygwinLoginDialog.EmptyShell")); } _param.LogType = (LogType)EnumDescAttributeT.For(typeof(LogType)).FromDescription(_logTypeBox.Text, LogType.None); if (_param.LogType != LogType.None) { _param.LogPath = _logFileBox.Text; LogFileCheckResult r = GCUtil.CheckLogFileName(_param.LogPath, this); if (r == LogFileCheckResult.Cancel || r == LogFileCheckResult.Error) { return; } _param.LogAppend = (r == LogFileCheckResult.Append); } _param.Home = _homeDirectoryBox.Text; _param.Shell = _shellBox.Text; _okButton.Enabled = false; _cancelButton.Enabled = false; this.Cursor = Cursors.WaitCursor; _savedHWND = this.Handle; if (_param is CygwinTerminalParam) { this.Text = GApp.Strings.GetString("Caption.CygwinLoginDialog.ConnectingCygwin"); } else { this.Text = GApp.Strings.GetString("Caption.CygwinLoginDialog.ConnectingSFU"); } _connector = CygwinUtil.AsyncPrepareSocket(this, _param); if (_connector == null) { ClearConnectingState(); } }
private SSHTerminalParam ValidateContent() { SSHTerminalParam p = _terminalParam; string msg = null; try { p.LogType = (LogType)EnumDescAttributeT.For(typeof(LogType)).FromDescription(_logTypeBox.Text, LogType.None); if (p.LogType != LogType.None) { p.LogPath = _logFileBox.Text; LogFileCheckResult r = GCUtil.CheckLogFileName(p.LogPath, this); if (r == LogFileCheckResult.Cancel || r == LogFileCheckResult.Error) { return(null); } p.LogAppend = (r == LogFileCheckResult.Append); } if (p.AuthType == AuthType.PublicKey) { if (!File.Exists(_privateKeyBox.Text)) { msg = GApp.Strings.GetString("Message.SSHShortcutLoginDialog.KeyFileNotExist"); } else { p.IdentityFile = _privateKeyBox.Text; } } if (msg != null) { GUtil.Warning(this, msg); return(null); } else { return(p); } } catch (Exception ex) { GUtil.Warning(this, ex.Message); return(null); } }
private void OnFontSelect(object sender, EventArgs args) { GFontDialog gd = new GFontDialog(); gd.SetFont(_useClearType, _font, _japaneseFont); DialogResult r = GCUtil.ShowModalDialog(FindForm(), gd); if (r == DialogResult.OK) { Font f = gd.ASCIIFont; _font = f; _japaneseFont = gd.JapaneseFont; _useClearType = gd.UseClearType; _fontSample.Font = f; AdjustFontDescription(f, gd.JapaneseFont); } }
public static int Main() { TestFramework.LogInformation("First Alloc"); GCUtil.Alloc(1024 * 1024, 50); GCUtil.FreeNonPins(); GC.Collect(); TestFramework.LogInformation("Second Alloc"); GCUtil.Alloc(1024 * 1024, 50); GCUtil.FreeNonPins(); GC.Collect(); GCUtil.FreePins(); TestFramework.LogInformation("Test passed"); return(100); }
private void OnFontSelect(object sender, EventArgs args) { GFontDialog gd = new GFontDialog(); Font nf = GUtil.CreateFont(_profile.FontName, _profile.FontSize); Font jf = GUtil.CreateFont(_profile.JapaneseFontName, _profile.FontSize); gd.SetFont(_profile.UseClearType, nf, jf); if (GCUtil.ShowModalDialog(this, gd) == DialogResult.OK) { Font f = gd.ASCIIFont; _profile.FontName = f.Name; _profile.JapaneseFontName = gd.JapaneseFont.Name; _profile.FontSize = f.Size; _profile.UseClearType = gd.UseClearType; _fontSample.Font = f; AdjustFontDescription(f.Name, gd.JapaneseFont.Name, f.Size); } }
public static int Main() { Object[] arr = new Object[100]; try { GCHandle handle1 = GCUtil.Alloc(arr, GCHandleType.Pinned); } catch (Exception e) { Console.WriteLine("Caught: {0}", e); Console.WriteLine("Test passed"); return(100); } Console.WriteLine("Test failed"); return(1); }
private void OnSaveOpenSSHPublicKey(object sender, EventArgs args) { SaveFileDialog dlg = new SaveFileDialog(); dlg.InitialDirectory = GApp.Options.DefaultKeyDir; dlg.Title = GApp.Strings.GetString("Caption.KeyGenWizard.SavePublicInOpenSSH"); dlg.DefaultExt = "pub"; dlg.Filter = "SSH Public Key(*.pub)|*.pub|All Files(*.*)|*.*"; if (GCUtil.ShowModalDialog(this, dlg) == DialogResult.OK) { GApp.Options.DefaultKeyDir = GUtil.FileToDir(dlg.FileName); try { _resultKey.WritePublicPartInOpenSSHStyle(new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write)); } catch (Exception ex) { GUtil.Warning(this, String.Format(GApp.Strings.GetString("Message.KeyGenWizard.KeySaveError"), ex.Message)); } } }