Skip to content

Commit 072e876

Browse files
authored
Improve System.Speech trimmability (#61566)
by removing obvious warnings
1 parent f9d05bd commit 072e876

File tree

11 files changed

+60
-62
lines changed

11 files changed

+60
-62
lines changed

src/libraries/System.Speech/src/AudioFormat/AudioFormatConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ internal static class AudioFormatConverter
1414

1515
internal static SpeechAudioFormatInfo ToSpeechAudioFormatInfo(IntPtr waveFormatPtr)
1616
{
17-
WaveFormatEx waveFormatEx = (WaveFormatEx)Marshal.PtrToStructure(waveFormatPtr, typeof(WaveFormatEx));
17+
WaveFormatEx waveFormatEx = Marshal.PtrToStructure<WaveFormatEx>(waveFormatPtr);
1818

1919
byte[] extraData = new byte[waveFormatEx.cbSize];
20-
IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf(waveFormatEx));
20+
IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf<WaveFormatEx>());
2121
for (int i = 0; i < waveFormatEx.cbSize; i++)
2222
{
2323
extraData[i] = Marshal.ReadByte(extraDataPtr, i);

src/libraries/System.Speech/src/Internal/SapiInterop/SapiRecoInterop.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words,
450450
SPPHRASEELEMENT[] elements = new SPPHRASEELEMENT[words.Length];
451451

452452
// build the unmanaged interop layer
453-
int size = Marshal.SizeOf(typeof(SPPHRASEELEMENT));
453+
int size = Marshal.SizeOf<SPPHRASEELEMENT>();
454454
List<GCHandle> handles = new();
455455

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

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

507507
SPPHRASE spPhrase = new();
508-
spPhrase.cbSize = (uint)Marshal.SizeOf(spPhrase.GetType());
508+
spPhrase.cbSize = (uint)Marshal.SizeOf<SPPHRASE>();
509509
spPhrase.LangID = (ushort)culture.LCID;
510510
spPhrase.Rule = new SPPHRASERULE
511511
{

src/libraries/System.Speech/src/Internal/SapiInterop/SpeechEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private SpeechEvent(SPEVENTENUM eEventId, SPEVENTLPARAMTYPE elParamType,
2626
// Let the GC know if we have a unmanaged object with a given size
2727
if (_paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_POINTER || _paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_STRING)
2828
{
29-
GC.AddMemoryPressure(_sizeMemoryPressure = Marshal.SizeOf(_lParam));
29+
GC.AddMemoryPressure(_sizeMemoryPressure = sizeof(ulong));
3030
}
3131
}
3232

src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
10051005
}
10061006

10071007
CfgGrammar.CfgSerializedHeader header = new();
1008-
uint ulOffset = (uint)Marshal.SizeOf(typeof(CfgGrammar.CfgSerializedHeader));
1008+
uint ulOffset = (uint)Marshal.SizeOf<CfgGrammar.CfgSerializedHeader>();
10091009

10101010
header.FormatId = CfgGrammar._SPGDF_ContextFree;
10111011
_guid = Guid.NewGuid();
@@ -1032,16 +1032,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
10321032
ulOffset += (uint)_symbols.SerializeSize() * Helpers._sizeOfChar;
10331033
header.cRules = _rules.Count;
10341034
header.pRules = ulOffset;
1035-
ulOffset += (uint)(_rules.Count * Marshal.SizeOf(typeof(CfgRule)));
1035+
ulOffset += (uint)(_rules.Count * Marshal.SizeOf<CfgRule>());
10361036
header.cBasePath = cBasePath > 0 ? ulOffset : 0; //If there is no base path offset is set to zero
10371037
ulOffset += (uint)((cBasePath * Helpers._sizeOfChar + 3) & ~3);
10381038
header.cArcs = cArcs;
10391039
header.pArcs = ulOffset;
1040-
ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(CfgArc)));
1040+
ulOffset += (uint)(cArcs * Marshal.SizeOf<CfgArc>());
10411041
if (_fNeedWeightTable)
10421042
{
10431043
header.pWeights = ulOffset;
1044-
ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(float)));
1044+
ulOffset += (uint)(cArcs * sizeof(float));
10451045
pWeights = new float[cArcs];
10461046
pWeights[0] = 0.0f;
10471047
}
@@ -1067,16 +1067,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List<State> sortedStates, int
10671067
header.GrammarMode = (uint)_grammarMode;
10681068
header.cTags = cSemanticTags;
10691069
header.tags = ulOffset;
1070-
ulOffset += (uint)(cSemanticTags * Marshal.SizeOf(typeof(CfgSemanticTag)));
1070+
ulOffset += (uint)(cSemanticTags * Marshal.SizeOf<CfgSemanticTag>());
10711071
header.cScripts = _scriptRefs.Count;
10721072
header.pScripts = header.cScripts > 0 ? ulOffset : 0;
1073-
ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf(typeof(CfgScriptRef)));
1073+
ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf<CfgScriptRef>());
10741074
header.cIL = 0;
10751075
header.pIL = 0;
1076-
ulOffset += (uint)(header.cIL * Marshal.SizeOf(typeof(byte)));
1076+
ulOffset += (uint)(header.cIL * sizeof(byte));
10771077
header.cPDB = 0;
10781078
header.pPDB = 0;
1079-
ulOffset += (uint)(header.cPDB * Marshal.SizeOf(typeof(byte)));
1079+
ulOffset += (uint)(header.cPDB * sizeof(byte));
10801080
header.ulTotalSerializedSize = ulOffset;
10811081
return header;
10821082
}

src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool in
272272
}
273273

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

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

418418
//Check the arc offset
419419
if (pFH.cArcs > 0)
420420
{
421-
CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf(typeof(CfgArc)), ref ullStartOffset, pFH.ulTotalSerializedSize);
421+
CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf<CfgArc>(), ref ullStartOffset, pFH.ulTotalSerializedSize);
422422
}
423423

424424
//Check the weight offset
425425
if (pFH.pWeights > 0)
426426
{
427-
CheckSetOffsets(pFH.pWeights, pFH.cArcs * Marshal.SizeOf(typeof(float)), ref ullStartOffset, pFH.ulTotalSerializedSize);
427+
CheckSetOffsets(pFH.pWeights, pFH.cArcs * sizeof(float), ref ullStartOffset, pFH.ulTotalSerializedSize);
428428
}
429429

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

435435
if (includeAllGrammarData)
436436
{
@@ -465,17 +465,17 @@ private static void CheckValidCfgFormat(CfgSerializedHeader pFH, CfgHeader heade
465465
//Check the offset for the scripts
466466
if (pFH.cScripts > 0)
467467
{
468-
CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf(typeof(CfgScriptRef)), ref ullStartOffset, pFH.ulTotalSerializedSize);
468+
CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf<CfgScriptRef>(), ref ullStartOffset, pFH.ulTotalSerializedSize);
469469
}
470470

471471
if (pFH.cIL > 0)
472472
{
473-
CheckSetOffsets(pFH.pIL, pFH.cIL * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize);
473+
CheckSetOffsets(pFH.pIL, pFH.cIL * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize);
474474
}
475475

476476
if (pFH.cPDB > 0)
477477
{
478-
CheckSetOffsets(pFH.pPDB, pFH.cPDB * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize);
478+
CheckSetOffsets(pFH.pPDB, pFH.cPDB * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize);
479479
}
480480
}
481481

src/libraries/System.Speech/src/Internal/StreamMarshaler.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public void Dispose()
3030
#region internal Methods
3131
internal void ReadArray<T>(T[] ao, int c)
3232
{
33-
Type type = typeof(T);
34-
int sizeOfOne = Marshal.SizeOf(type);
33+
int sizeOfOne = Marshal.SizeOf<T>();
3534
int sizeObject = sizeOfOne * c;
3635
byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject);
3736

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

4746
internal void WriteArray<T>(T[] ao, int c)
4847
{
49-
Type type = typeof(T);
50-
int sizeOfOne = Marshal.SizeOf(type);
48+
int sizeOfOne = Marshal.SizeOf<T>();
5149
int sizeObject = sizeOfOne * c;
5250
byte[] ab = new byte[sizeObject];
5351
IntPtr buffer = _safeHMem.Buffer(sizeObject);
5452

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

6058
Marshal.Copy(buffer, ab, 0, sizeObject);
@@ -116,24 +114,24 @@ internal void WriteArrayChar(char[] ach, int c)
116114
}
117115
}
118116

119-
internal void ReadStream(object o)
117+
internal void ReadStream<T>(T o)
120118
{
121-
int sizeObject = Marshal.SizeOf(o.GetType());
119+
int sizeObject = Marshal.SizeOf<T>();
122120
byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject);
123121

124122
IntPtr buffer = _safeHMem.Buffer(sizeObject);
125123

126124
Marshal.Copy(ab, 0, buffer, sizeObject);
127-
Marshal.PtrToStructure(buffer, o);
125+
Marshal.PtrToStructure<T>(buffer, o);
128126
}
129127

130-
internal void WriteStream(object o)
128+
internal void WriteStream<T>(T o)
131129
{
132-
int sizeObject = Marshal.SizeOf(o.GetType());
130+
int sizeObject = Marshal.SizeOf<T>();
133131
byte[] ab = new byte[sizeObject];
134132
IntPtr buffer = _safeHMem.Buffer(sizeObject);
135133

136-
Marshal.StructureToPtr(o, buffer, false);
134+
Marshal.StructureToPtr<T>(o, buffer, false);
137135
Marshal.Copy(buffer, ab, 0, sizeObject);
138136

139137
// Read the Header

src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long po
234234
BLOCKHDR block = new(0);
235235
DATAHDR dataHdr = new(0);
236236

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

242242
int total = cRiff + cBlock + cWaveEx + cDataHdr;
243243

src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ internal static MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LP
313313
prodName = string.Empty;
314314
SafeNativeMethods.WAVEOUTCAPS caps = new();
315315

316-
MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf(caps));
316+
MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf<SafeNativeMethods.WAVEOUTCAPS>());
317317
if (result != MMSYSERR.NOERROR)
318318
{
319319
return result;

src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal int SizeHDR
8686
{
8787
get
8888
{
89-
return Marshal.SizeOf(_waveHdr);
89+
return Marshal.SizeOf<WAVEHDR>();
9090
}
9191
}
9292

src/libraries/System.Speech/src/Result/RecognitionResult.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public RecognizedAudio Audio
151151
{
152152
IntPtr audioBuffer = gc.AddrOfPinnedObject();
153153

154-
SPWAVEFORMATEX audioHeader = (SPWAVEFORMATEX)Marshal.PtrToStructure(audioBuffer, typeof(SPWAVEFORMATEX));
154+
SPWAVEFORMATEX audioHeader = Marshal.PtrToStructure<SPWAVEFORMATEX>(audioBuffer);
155155

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

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

299-
if (headerSize == Marshal.SizeOf(typeof(SPRESULTHEADER_Sapi51))) // SAPI 5.1 size
299+
if (headerSize == Marshal.SizeOf<SPRESULTHEADER_Sapi51>()) // SAPI 5.1 size
300300
{
301-
SPRESULTHEADER_Sapi51 legacyHeader = (SPRESULTHEADER_Sapi51)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER_Sapi51));
301+
SPRESULTHEADER_Sapi51 legacyHeader = Marshal.PtrToStructure<SPRESULTHEADER_Sapi51>(buffer);
302302
_header = new SPRESULTHEADER(legacyHeader);
303303
_isSapi53Header = false;
304304
}
305305
else
306306
{
307-
_header = (SPRESULTHEADER)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER));
307+
_header = Marshal.PtrToStructure<SPRESULTHEADER>(buffer);
308308
_isSapi53Header = true;
309309
}
310310

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

358-
int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf(typeof(SPSERIALIZEDPHRASEALT));
358+
int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf<SPSERIALIZEDPHRASEALT>();
359359
int offset = 0;
360360
for (int i = 0; i < numberOfAlternates; i++)
361361
{
362362
IntPtr altBuffer = new((long)buffer + offset);
363-
SPSERIALIZEDPHRASEALT alt = (SPSERIALIZEDPHRASEALT)Marshal.PtrToStructure(altBuffer, typeof(SPSERIALIZEDPHRASEALT));
363+
SPSERIALIZEDPHRASEALT alt = Marshal.PtrToStructure<SPSERIALIZEDPHRASEALT>(altBuffer);
364364

365365
offset += sizeOfSpSerializedPhraseAlt; // advance over SPSERIALIZEDPHRASEALT
366366
if (isSapi53Header)

0 commit comments

Comments
 (0)