@@ -144,6 +144,7 @@ struct hid_device_ {
144
144
HANDLE device_handle ;
145
145
BOOL blocking ;
146
146
USHORT output_report_length ;
147
+ unsigned char * write_buf ;
147
148
size_t input_report_length ;
148
149
USHORT feature_report_length ;
149
150
unsigned char * feature_buf ;
@@ -161,6 +162,7 @@ static hid_device *new_hid_device()
161
162
dev -> device_handle = INVALID_HANDLE_VALUE ;
162
163
dev -> blocking = TRUE;
163
164
dev -> output_report_length = 0 ;
165
+ dev -> write_buf = NULL ;
164
166
dev -> input_report_length = 0 ;
165
167
dev -> feature_report_length = 0 ;
166
168
dev -> feature_buf = NULL ;
@@ -182,6 +184,7 @@ static void free_hid_device(hid_device *dev)
182
184
CloseHandle (dev -> write_ol .hEvent );
183
185
CloseHandle (dev -> device_handle );
184
186
LocalFree (dev -> last_error_str );
187
+ free (dev -> write_buf );
185
188
free (dev -> feature_buf );
186
189
free (dev -> read_buf );
187
190
free (dev );
@@ -661,14 +664,14 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *
661
664
one for the report number) bytes even if the data is a report
662
665
which is shorter than that. Windows gives us this value in
663
666
caps.OutputReportByteLength. If a user passes in fewer bytes than this,
664
- create a temporary buffer which is the proper size. */
667
+ use cached temporary buffer which is the proper size. */
665
668
if (length >= dev -> output_report_length ) {
666
669
/* The user passed the right number of bytes. Use the buffer as-is. */
667
670
buf = (unsigned char * ) data ;
668
671
} else {
669
- /* Create a temporary buffer and copy the user's data
670
- into it, padding the rest with zeros. */
671
- buf = ( unsigned char * ) malloc ( dev -> output_report_length ) ;
672
+ if ( dev -> write_buf == NULL )
673
+ dev -> write_buf = ( unsigned char * ) malloc ( dev -> output_report_length );
674
+ buf = dev -> write_buf ;
672
675
memcpy (buf , data , length );
673
676
memset (buf + length , 0 , dev -> output_report_length - length );
674
677
length = dev -> output_report_length ;
@@ -708,9 +711,6 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *
708
711
}
709
712
710
713
end_of_function :
711
- if (buf != data )
712
- free (buf );
713
-
714
714
return function_result ;
715
715
}
716
716
0 commit comments