@@ -831,7 +831,7 @@ g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
831
831
}
832
832
833
833
static gunichar2 *
834
- eg_utf8_to_utf16_general (const gchar * str , glong len , glong * items_read , glong * items_written , gboolean include_nuls , gboolean replace_invalid_codepoints , GError * * err )
834
+ eg_utf8_to_utf16_general (const gchar * str , glong len , glong * items_read , glong * items_written , gboolean include_nuls , gboolean replace_invalid_codepoints , GCustomAllocator custom_alloc_func , gpointer custom_alloc_data , GError * * err )
835
835
{
836
836
gunichar2 * outbuf , * outptr ;
837
837
size_t outlen = 0 ;
@@ -881,7 +881,16 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
881
881
if (items_written )
882
882
* items_written = outlen ;
883
883
884
- outptr = outbuf = g_malloc ((outlen + 1 ) * sizeof (gunichar2 ));
884
+ if (G_LIKELY (!custom_alloc_func ))
885
+ outptr = outbuf = g_malloc ((outlen + 1 ) * sizeof (gunichar2 ));
886
+ else
887
+ outptr = outbuf = (gunichar2 * )custom_alloc_func ((outlen + 1 ) * sizeof (gunichar2 ), custom_alloc_data );
888
+
889
+ if (G_UNLIKELY (custom_alloc_func && !outbuf )) {
890
+ mono_set_errno (ENOMEM );
891
+ goto error ;
892
+ }
893
+
885
894
inptr = (char * ) str ;
886
895
inleft = len ;
887
896
@@ -908,8 +917,11 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
908
917
909
918
return outbuf ;
910
919
911
- error :
912
- if (errno == EILSEQ ) {
920
+ error :
921
+ if (errno == ENOMEM ) {
922
+ g_set_error (err , G_CONVERT_ERROR , G_CONVERT_ERROR_NO_MEMORY ,
923
+ "Allocation failed." );
924
+ } else if (errno == EILSEQ ) {
913
925
g_set_error (err , G_CONVERT_ERROR , G_CONVERT_ERROR_ILLEGAL_SEQUENCE ,
914
926
"Illegal byte sequence encounted in the input." );
915
927
} else if (items_read ) {
@@ -931,19 +943,25 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
931
943
gunichar2 *
932
944
g_utf8_to_utf16 (const gchar * str , glong len , glong * items_read , glong * items_written , GError * * err )
933
945
{
934
- return eg_utf8_to_utf16_general (str , len , items_read , items_written , FALSE, FALSE, err );
946
+ return eg_utf8_to_utf16_general (str , len , items_read , items_written , FALSE, FALSE, NULL , NULL , err );
947
+ }
948
+
949
+ gunichar2 *
950
+ g_utf8_to_utf16_custom_alloc (const gchar * str , glong len , glong * items_read , glong * items_written , GCustomAllocator custom_alloc_func , gpointer custom_alloc_data , GError * * err )
951
+ {
952
+ return eg_utf8_to_utf16_general (str , len , items_read , items_written , FALSE, FALSE, custom_alloc_func , custom_alloc_data , err );
935
953
}
936
954
937
955
gunichar2 *
938
956
eg_utf8_to_utf16_with_nuls (const gchar * str , glong len , glong * items_read , glong * items_written , GError * * err )
939
957
{
940
- return eg_utf8_to_utf16_general (str , len , items_read , items_written , TRUE, FALSE, err );
958
+ return eg_utf8_to_utf16_general (str , len , items_read , items_written , TRUE, FALSE, NULL , NULL , err );
941
959
}
942
960
943
961
gunichar2 *
944
962
eg_wtf8_to_utf16 (const gchar * str , glong len , glong * items_read , glong * items_written , GError * * err )
945
963
{
946
- return eg_utf8_to_utf16_general (str , len , items_read , items_written , TRUE, TRUE, err );
964
+ return eg_utf8_to_utf16_general (str , len , items_read , items_written , TRUE, TRUE, NULL , NULL , err );
947
965
}
948
966
949
967
gunichar *
@@ -1018,8 +1036,9 @@ g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_wri
1018
1036
return outbuf ;
1019
1037
}
1020
1038
1039
+ static
1021
1040
gchar *
1022
- g_utf16_to_utf8 (const gunichar2 * str , glong len , glong * items_read , glong * items_written , GError * * err )
1041
+ eg_utf16_to_utf8_general (const gunichar2 * str , glong len , glong * items_read , glong * items_written , GCustomAllocator custom_alloc_func , gpointer custom_alloc_data , GError * * err )
1023
1042
{
1024
1043
char * inptr , * outbuf , * outptr ;
1025
1044
size_t outlen = 0 ;
@@ -1077,8 +1096,19 @@ g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *item
1077
1096
1078
1097
if (items_written )
1079
1098
* items_written = outlen ;
1099
+
1100
+ if (G_LIKELY (!custom_alloc_func ))
1101
+ outptr = outbuf = g_malloc (outlen + 1 );
1102
+ else
1103
+ outptr = outbuf = (char * )custom_alloc_func (outlen + 1 , custom_alloc_data );
1104
+
1105
+ if (G_UNLIKELY (custom_alloc_func && !outbuf )) {
1106
+ g_set_error (err , G_CONVERT_ERROR , G_CONVERT_ERROR_NO_MEMORY , "Allocation failed." );
1107
+ if (items_written )
1108
+ * items_written = 0 ;
1109
+ return NULL ;
1110
+ }
1080
1111
1081
- outptr = outbuf = g_malloc (outlen + 1 );
1082
1112
inptr = (char * ) str ;
1083
1113
inleft = len * 2 ;
1084
1114
@@ -1098,6 +1128,18 @@ g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *item
1098
1128
return outbuf ;
1099
1129
}
1100
1130
1131
+ gchar *
1132
+ g_utf16_to_utf8 (const gunichar2 * str , glong len , glong * items_read , glong * items_written , GError * * err )
1133
+ {
1134
+ return eg_utf16_to_utf8_general (str , len , items_read , items_written , NULL , NULL , err );
1135
+ }
1136
+
1137
+ gchar *
1138
+ g_utf16_to_utf8_custom_alloc (const gunichar2 * str , glong len , glong * items_read , glong * items_written , GCustomAllocator custom_alloc_func , gpointer custom_alloc_data , GError * * err )
1139
+ {
1140
+ return eg_utf16_to_utf8_general (str , len , items_read , items_written , custom_alloc_func , custom_alloc_data , err );
1141
+ }
1142
+
1101
1143
gunichar *
1102
1144
g_utf16_to_ucs4 (const gunichar2 * str , glong len , glong * items_read , glong * items_written , GError * * err )
1103
1145
{
@@ -1302,3 +1344,17 @@ g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items
1302
1344
1303
1345
return outbuf ;
1304
1346
}
1347
+
1348
+ gpointer
1349
+ g_fixed_buffer_custom_allocator (gsize req_size , gpointer custom_alloc_data )
1350
+ {
1351
+ GFixedBufferCustomAllocatorData * fixed_buffer_custom_alloc_data = (GFixedBufferCustomAllocatorData * )custom_alloc_data ;
1352
+ if (!fixed_buffer_custom_alloc_data )
1353
+ return NULL ;
1354
+
1355
+ fixed_buffer_custom_alloc_data -> req_buffer_size = req_size ;
1356
+ if (req_size > fixed_buffer_custom_alloc_data -> buffer_size )
1357
+ return NULL ;
1358
+
1359
+ return fixed_buffer_custom_alloc_data -> buffer ;
1360
+ }
0 commit comments