Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve System.Speech trimmability #61566

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ internal static class AudioFormatConverter

internal static SpeechAudioFormatInfo ToSpeechAudioFormatInfo(IntPtr waveFormatPtr)
{
WaveFormatEx waveFormatEx = (WaveFormatEx)Marshal.PtrToStructure(waveFormatPtr, typeof(WaveFormatEx));
WaveFormatEx waveFormatEx = Marshal.PtrToStructure<WaveFormatEx>(waveFormatPtr);

byte[] extraData = new byte[waveFormatEx.cbSize];
IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf(waveFormatEx));
IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf<WaveFormatEx>());
for (int i = 0; i < waveFormatEx.cbSize; i++)
{
extraData[i] = Marshal.ReadByte(extraDataPtr, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words,
SPPHRASEELEMENT[] elements = new SPPHRASEELEMENT[words.Length];

// build the unmanaged interop layer
int size = Marshal.SizeOf(typeof(SPPHRASEELEMENT));
int size = Marshal.SizeOf<SPPHRASEELEMENT>();
List<GCHandle> handles = new();

coMem = Marshal.AllocCoTaskMem(size * elements.Length);
Expand Down Expand Up @@ -496,7 +496,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words,
elements[i].pszPronunciation = handle.AddrOfPinnedObject();
}

Marshal.StructureToPtr(elements[i], new IntPtr((long)coMem + size * i), false);
Marshal.StructureToPtr<SPPHRASEELEMENT>(elements[i], new IntPtr((long)coMem + size * i), false);
}
}
finally
Expand All @@ -505,7 +505,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words,
}

SPPHRASE spPhrase = new();
spPhrase.cbSize = (uint)Marshal.SizeOf(spPhrase.GetType());
spPhrase.cbSize = (uint)Marshal.SizeOf<SPPHRASE>();
spPhrase.LangID = (ushort)culture.LCID;
spPhrase.Rule = new SPPHRASERULE
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private SpeechEvent(SPEVENTENUM eEventId, SPEVENTLPARAMTYPE elParamType,
// Let the GC know if we have a unmanaged object with a given size
if (_paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_POINTER || _paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_STRING)
{
GC.AddMemoryPressure(_sizeMemoryPressure = Marshal.SizeOf(_lParam));
GC.AddMemoryPressure(_sizeMemoryPressure = sizeof(ulong));
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
}

CfgGrammar.CfgSerializedHeader header = new();
uint ulOffset = (uint)Marshal.SizeOf(typeof(CfgGrammar.CfgSerializedHeader));
uint ulOffset = (uint)Marshal.SizeOf<CfgGrammar.CfgSerializedHeader>();

header.FormatId = CfgGrammar._SPGDF_ContextFree;
_guid = Guid.NewGuid();
Expand All @@ -1032,16 +1032,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
ulOffset += (uint)_symbols.SerializeSize() * Helpers._sizeOfChar;
header.cRules = _rules.Count;
header.pRules = ulOffset;
ulOffset += (uint)(_rules.Count * Marshal.SizeOf(typeof(CfgRule)));
ulOffset += (uint)(_rules.Count * Marshal.SizeOf<CfgRule>());
header.cBasePath = cBasePath > 0 ? ulOffset : 0; //If there is no base path offset is set to zero
ulOffset += (uint)((cBasePath * Helpers._sizeOfChar + 3) & ~3);
header.cArcs = cArcs;
header.pArcs = ulOffset;
ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(CfgArc)));
ulOffset += (uint)(cArcs * Marshal.SizeOf<CfgArc>());
if (_fNeedWeightTable)
{
header.pWeights = ulOffset;
ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(float)));
ulOffset += (uint)(cArcs * sizeof(float));
pWeights = new float[cArcs];
pWeights[0] = 0.0f;
}
Expand All @@ -1067,16 +1067,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
header.GrammarMode = (uint)_grammarMode;
header.cTags = cSemanticTags;
header.tags = ulOffset;
ulOffset += (uint)(cSemanticTags * Marshal.SizeOf(typeof(CfgSemanticTag)));
ulOffset += (uint)(cSemanticTags * Marshal.SizeOf<CfgSemanticTag>());
header.cScripts = _scriptRefs.Count;
header.pScripts = header.cScripts > 0 ? ulOffset : 0;
ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf(typeof(CfgScriptRef)));
ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf<CfgScriptRef>());
header.cIL = 0;
header.pIL = 0;
ulOffset += (uint)(header.cIL * Marshal.SizeOf(typeof(byte)));
ulOffset += (uint)(header.cIL * sizeof(byte));
header.cPDB = 0;
header.pPDB = 0;
ulOffset += (uint)(header.cPDB * Marshal.SizeOf(typeof(byte)));
ulOffset += (uint)(header.cPDB * sizeof(byte));
header.ulTotalSerializedSize = ulOffset;
return header;
}
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool in
}

//We know that in SAPI 5.0 grammar format pszWords follows header immediately.
if (cfgSerializedHeader.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader)))
if (cfgSerializedHeader.pszWords < Marshal.SizeOf<CfgSerializedHeader>())
{
//This is SAPI 5.0 and SAPI 5.1 grammar format
header.ulRootRuleIndex = 0xFFFFFFFF;
Expand All @@ -294,7 +294,7 @@ internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool in
// Get the chars and build the string
if (cfgSerializedHeader.cBasePath > 0)
{
streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf(typeof(CfgRule)));
streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf<CfgRule>());
header.BasePath = streamHelper.ReadNullTerminatedString();
}
}
Expand All @@ -319,7 +319,7 @@ internal static ScriptRef[] LoadScriptRefs(StreamMarshaler streamHelper, CfgSeri
}

//We know that in SAPI 5.0 grammar format pszWords follows header immediately.
if (pFH.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader)))
if (pFH.pszWords < Marshal.SizeOf<CfgSerializedHeader>())
{
// Must be SAPI 6.0 or above to hold a .NET script
return null;
Expand Down Expand Up @@ -412,25 +412,25 @@ private static void CheckValidCfgFormat(CfgSerializedHeader pFH, CfgHeader heade
//Check the rule offset
if (pFH.cRules > 0)
{
CheckSetOffsets(pFH.pRules, pFH.cRules * Marshal.SizeOf(typeof(CfgRule)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pRules, pFH.cRules * Marshal.SizeOf<CfgRule>(), ref ullStartOffset, pFH.ulTotalSerializedSize);
}

//Check the arc offset
if (pFH.cArcs > 0)
{
CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf(typeof(CfgArc)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf<CfgArc>(), ref ullStartOffset, pFH.ulTotalSerializedSize);
}

//Check the weight offset
if (pFH.pWeights > 0)
{
CheckSetOffsets(pFH.pWeights, pFH.cArcs * Marshal.SizeOf(typeof(float)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pWeights, pFH.cArcs * sizeof(float), ref ullStartOffset, pFH.ulTotalSerializedSize);
}

//Check the semantic tag offset
if (pFH.cTags > 0)
{
CheckSetOffsets(pFH.tags, pFH.cTags * Marshal.SizeOf(typeof(CfgSemanticTag)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.tags, pFH.cTags * Marshal.SizeOf<CfgSemanticTag>(), ref ullStartOffset, pFH.ulTotalSerializedSize);

if (includeAllGrammarData)
{
Expand Down Expand Up @@ -465,17 +465,17 @@ private static void CheckValidCfgFormat(CfgSerializedHeader pFH, CfgHeader heade
//Check the offset for the scripts
if (pFH.cScripts > 0)
{
CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf(typeof(CfgScriptRef)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf<CfgScriptRef>(), ref ullStartOffset, pFH.ulTotalSerializedSize);
}

if (pFH.cIL > 0)
{
CheckSetOffsets(pFH.pIL, pFH.cIL * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pIL, pFH.cIL * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize);
}

if (pFH.cPDB > 0)
{
CheckSetOffsets(pFH.pPDB, pFH.cPDB * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize);
CheckSetOffsets(pFH.pPDB, pFH.cPDB * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize);
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/libraries/System.Speech/src/Internal/StreamMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public void Dispose()
#region internal Methods
internal void ReadArray<T>(T[] ao, int c)
{
Type type = typeof(T);
int sizeOfOne = Marshal.SizeOf(type);
int sizeOfOne = Marshal.SizeOf<T>();
int sizeObject = sizeOfOne * c;
byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject);

Expand All @@ -40,21 +39,20 @@ internal void ReadArray<T>(T[] ao, int c)
Marshal.Copy(ab, 0, buffer, sizeObject);
for (int i = 0; i < c; i++)
{
ao[i] = (T)Marshal.PtrToStructure((IntPtr)((long)buffer + i * sizeOfOne), type);
ao[i] = Marshal.PtrToStructure<T>((IntPtr)((long)buffer + i * sizeOfOne));
}
}

internal void WriteArray<T>(T[] ao, int c)
{
Type type = typeof(T);
int sizeOfOne = Marshal.SizeOf(type);
int sizeOfOne = Marshal.SizeOf<T>();
int sizeObject = sizeOfOne * c;
byte[] ab = new byte[sizeObject];
IntPtr buffer = _safeHMem.Buffer(sizeObject);

for (int i = 0; i < c; i++)
{
Marshal.StructureToPtr(ao[i], (IntPtr)((long)buffer + i * sizeOfOne), false);
Marshal.StructureToPtr<T>(ao[i], (IntPtr)((long)buffer + i * sizeOfOne), false);
}

Marshal.Copy(buffer, ab, 0, sizeObject);
Expand Down Expand Up @@ -116,24 +114,24 @@ internal void WriteArrayChar(char[] ach, int c)
}
}

internal void ReadStream(object o)
internal void ReadStream<T>(T o)
{
int sizeObject = Marshal.SizeOf(o.GetType());
int sizeObject = Marshal.SizeOf<T>();
byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject);

IntPtr buffer = _safeHMem.Buffer(sizeObject);

Marshal.Copy(ab, 0, buffer, sizeObject);
Marshal.PtrToStructure(buffer, o);
Marshal.PtrToStructure<T>(buffer, o);
}

internal void WriteStream(object o)
internal void WriteStream<T>(T o)
{
int sizeObject = Marshal.SizeOf(o.GetType());
int sizeObject = Marshal.SizeOf<T>();
byte[] ab = new byte[sizeObject];
IntPtr buffer = _safeHMem.Buffer(sizeObject);

Marshal.StructureToPtr(o, buffer, false);
Marshal.StructureToPtr<T>(o, buffer, false);
Marshal.Copy(buffer, ab, 0, sizeObject);

// Read the Header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long po
BLOCKHDR block = new(0);
DATAHDR dataHdr = new(0);

int cRiff = Marshal.SizeOf(riff);
int cBlock = Marshal.SizeOf(block);
int cRiff = Marshal.SizeOf<RIFFHDR>();
int cBlock = Marshal.SizeOf<BLOCKHDR>();
int cWaveEx = waveEx.Length;// Marshal.SizeOf (waveEx); // The CLR automatically pad the waveEx structure to dword boundary. Force 16.
int cDataHdr = Marshal.SizeOf(dataHdr);
int cDataHdr = Marshal.SizeOf<DATAHDR>();

int total = cRiff + cBlock + cWaveEx + cDataHdr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ internal static MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LP
prodName = string.Empty;
SafeNativeMethods.WAVEOUTCAPS caps = new();

MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf(caps));
MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf<SafeNativeMethods.WAVEOUTCAPS>());
if (result != MMSYSERR.NOERROR)
{
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal int SizeHDR
{
get
{
return Marshal.SizeOf(_waveHdr);
return Marshal.SizeOf<WAVEHDR>();
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libraries/System.Speech/src/Result/RecognitionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public RecognizedAudio Audio
{
IntPtr audioBuffer = gc.AddrOfPinnedObject();

SPWAVEFORMATEX audioHeader = (SPWAVEFORMATEX)Marshal.PtrToStructure(audioBuffer, typeof(SPWAVEFORMATEX));
SPWAVEFORMATEX audioHeader = Marshal.PtrToStructure<SPWAVEFORMATEX>(audioBuffer);

IntPtr rawDataBuffer = new((long)audioBuffer + audioHeader.cbUsed);
byte[] rawAudioData = new byte[audioLength - audioHeader.cbUsed];
Expand Down Expand Up @@ -296,15 +296,15 @@ private void Initialize(IRecognizerInternal recognizer, ISpRecoResult recoResult

int headerSize = Marshal.ReadInt32(buffer, 4); // Read header size directly from buffer - 4 is the offset of cbHeaderSize.

if (headerSize == Marshal.SizeOf(typeof(SPRESULTHEADER_Sapi51))) // SAPI 5.1 size
if (headerSize == Marshal.SizeOf<SPRESULTHEADER_Sapi51>()) // SAPI 5.1 size
{
SPRESULTHEADER_Sapi51 legacyHeader = (SPRESULTHEADER_Sapi51)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER_Sapi51));
SPRESULTHEADER_Sapi51 legacyHeader = Marshal.PtrToStructure<SPRESULTHEADER_Sapi51>(buffer);
_header = new SPRESULTHEADER(legacyHeader);
_isSapi53Header = false;
}
else
{
_header = (SPRESULTHEADER)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER));
_header = Marshal.PtrToStructure<SPRESULTHEADER>(buffer);
_isSapi53Header = true;
}

Expand Down Expand Up @@ -355,12 +355,12 @@ private Collection<RecognizedPhrase> ExtractAlternates(int numberOfAlternates, b
{
IntPtr buffer = gc.AddrOfPinnedObject();

int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf(typeof(SPSERIALIZEDPHRASEALT));
int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf<SPSERIALIZEDPHRASEALT>();
int offset = 0;
for (int i = 0; i < numberOfAlternates; i++)
{
IntPtr altBuffer = new((long)buffer + offset);
SPSERIALIZEDPHRASEALT alt = (SPSERIALIZEDPHRASEALT)Marshal.PtrToStructure(altBuffer, typeof(SPSERIALIZEDPHRASEALT));
SPSERIALIZEDPHRASEALT alt = Marshal.PtrToStructure<SPSERIALIZEDPHRASEALT>(altBuffer);

offset += sizeOfSpSerializedPhraseAlt; // advance over SPSERIALIZEDPHRASEALT
if (isSapi53Header)
Expand Down
Loading