Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Speech / Src / Internal / HGlobalSafeHandle.cs / 1 / HGlobalSafeHandle.cs
//----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// Stream Helper.
// Allocates a global memory buffer to do marshaling between a
// binary and a structured data. The global memory size increases and
// never shrinks.
//
using System;
using System.Runtime.InteropServices;
namespace System.Speech.Internal
{
///
/// Encapsulate SafeHandle for Win32 Memory Handles
///
internal sealed class HGlobalSafeHandle : SafeHandle
{
//*******************************************************************
//
// Constructors
//
//*******************************************************************
#region Constructors
internal HGlobalSafeHandle () : base (IntPtr.Zero, true)
{
}
// This destructor will run only if the Dispose method
// does not get called.
~HGlobalSafeHandle ()
{
Dispose (false);
}
protected override void Dispose (bool disposing)
{
ReleaseHandle ();
base.Dispose (disposing);
GC.SuppressFinalize (this);
}
#endregion
//********************************************************************
//
// Internal Methods
//
//*******************************************************************
#region internal Methods
///
///
///
///
///
internal IntPtr Buffer (int size)
{
if (size > _bufferSize)
{
if (_bufferSize == 0)
{
SetHandle (Marshal.AllocHGlobal (size));
}
else
{
SetHandle (Marshal.ReAllocHGlobal (handle, (IntPtr) size));
}
GC.AddMemoryPressure (size - _bufferSize);
_bufferSize = size;
}
return handle;
}
///
/// True if the no memory is allocated
///
///
public override bool IsInvalid
{
get
{
return handle == IntPtr.Zero;
}
}
#endregion
//********************************************************************
//
// Protected Methods
//
//********************************************************************
#region Protected Methods
///
/// Releases the Win32 Memory handle
///
///
protected override bool ReleaseHandle ()
{
if (handle != IntPtr.Zero)
{
// Reset the extra information given to the GC
if (_bufferSize > 0)
{
GC.RemoveMemoryPressure (_bufferSize);
_bufferSize = 0;
}
Marshal.FreeHGlobal (handle);
handle = IntPtr.Zero;
return true;
}
return false;
}
#endregion
//*******************************************************************
//
// Private Fields
//
//********************************************************************
#region Private Fields
private int _bufferSize;
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// Stream Helper.
// Allocates a global memory buffer to do marshaling between a
// binary and a structured data. The global memory size increases and
// never shrinks.
//
using System;
using System.Runtime.InteropServices;
namespace System.Speech.Internal
{
///
/// Encapsulate SafeHandle for Win32 Memory Handles
///
internal sealed class HGlobalSafeHandle : SafeHandle
{
//*******************************************************************
//
// Constructors
//
//*******************************************************************
#region Constructors
internal HGlobalSafeHandle () : base (IntPtr.Zero, true)
{
}
// This destructor will run only if the Dispose method
// does not get called.
~HGlobalSafeHandle ()
{
Dispose (false);
}
protected override void Dispose (bool disposing)
{
ReleaseHandle ();
base.Dispose (disposing);
GC.SuppressFinalize (this);
}
#endregion
//********************************************************************
//
// Internal Methods
//
//*******************************************************************
#region internal Methods
///
///
///
///
///
internal IntPtr Buffer (int size)
{
if (size > _bufferSize)
{
if (_bufferSize == 0)
{
SetHandle (Marshal.AllocHGlobal (size));
}
else
{
SetHandle (Marshal.ReAllocHGlobal (handle, (IntPtr) size));
}
GC.AddMemoryPressure (size - _bufferSize);
_bufferSize = size;
}
return handle;
}
///
/// True if the no memory is allocated
///
///
public override bool IsInvalid
{
get
{
return handle == IntPtr.Zero;
}
}
#endregion
//********************************************************************
//
// Protected Methods
//
//********************************************************************
#region Protected Methods
///
/// Releases the Win32 Memory handle
///
///
protected override bool ReleaseHandle ()
{
if (handle != IntPtr.Zero)
{
// Reset the extra information given to the GC
if (_bufferSize > 0)
{
GC.RemoveMemoryPressure (_bufferSize);
_bufferSize = 0;
}
Marshal.FreeHGlobal (handle);
handle = IntPtr.Zero;
return true;
}
return false;
}
#endregion
//*******************************************************************
//
// Private Fields
//
//********************************************************************
#region Private Fields
private int _bufferSize;
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- EventLogHandle.cs
- Compensate.cs
- CompositeDispatchFormatter.cs
- KerberosSecurityTokenAuthenticator.cs
- Win32Exception.cs
- OperatingSystem.cs
- SqlMethodAttribute.cs
- PagesChangedEventArgs.cs
- StructuredTypeEmitter.cs
- SqlTypeConverter.cs
- ArcSegment.cs
- ShapingEngine.cs
- CodeGotoStatement.cs
- TemplateBindingExtensionConverter.cs
- XPathNodeIterator.cs
- AdapterDictionary.cs
- GatewayIPAddressInformationCollection.cs
- ValidationError.cs
- SQLByteStorage.cs
- WebProxyScriptElement.cs
- WebContext.cs
- SpellerError.cs
- CallbackValidator.cs
- RewritingProcessor.cs
- EncryptedPackage.cs
- PropertyCollection.cs
- XhtmlBasicValidationSummaryAdapter.cs
- XmlElementAttribute.cs
- Gdiplus.cs
- GeneralTransform3DGroup.cs
- RenderOptions.cs
- ReaderContextStackData.cs
- ComponentDispatcherThread.cs
- SessionParameter.cs
- PersonalizableAttribute.cs
- _SSPIWrapper.cs
- PathData.cs
- ProfileProvider.cs
- ExpressionBinding.cs
- MasterPage.cs
- ModelUtilities.cs
- SqlError.cs
- KnownTypesHelper.cs
- FontStyle.cs
- Scene3D.cs
- TextEditorParagraphs.cs
- RootAction.cs
- CompressionTracing.cs
- GZipUtils.cs
- Shared.cs
- IntSecurity.cs
- RelatedCurrencyManager.cs
- TriState.cs
- Button.cs
- ValueType.cs
- OperationAbortedException.cs
- coordinator.cs
- RemoveStoryboard.cs
- ReturnType.cs
- ObjectDataSourceDisposingEventArgs.cs
- UnsignedPublishLicense.cs
- UnauthorizedAccessException.cs
- DataGridViewCellParsingEventArgs.cs
- EntityDataSourceUtil.cs
- SqlDataSourceFilteringEventArgs.cs
- Typography.cs
- DataGridRowDetailsEventArgs.cs
- ScriptingRoleServiceSection.cs
- LinkedResource.cs
- BitStream.cs
- TextEditorCopyPaste.cs
- FieldToken.cs
- StrokeSerializer.cs
- X509CertificateCollection.cs
- VisualBasicImportReference.cs
- IncrementalHitTester.cs
- SharedPersonalizationStateInfo.cs
- Triangle.cs
- peersecuritysettings.cs
- DataGridRelationshipRow.cs
- DataGridViewCellErrorTextNeededEventArgs.cs
- KeyGestureValueSerializer.cs
- ExpressionBuilderCollection.cs
- AnchoredBlock.cs
- XmlNamedNodeMap.cs
- EditableLabelControl.cs
- MaterialCollection.cs
- GlobalItem.cs
- SqlWorkflowPersistenceService.cs
- Triangle.cs
- DocumentReferenceCollection.cs
- Memoizer.cs
- TimeZone.cs
- loginstatus.cs
- Solver.cs
- DocumentGridContextMenu.cs
- TextEditorParagraphs.cs
- HtmlInputButton.cs
- TextElementEnumerator.cs
- ScrollProperties.cs