Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | (backported from tclSE) avoid multi-threaded vulnerability of system encoding (see tcl bug [f2ff05fc8411be80]), to be thread safe increase refcount during conversion (using system encoding); atomic encoding refcount (fewer bottlenecks across threads); better handling for encoding stored in internal object representation - no extra lookup in table for cached encoding (unless epoch gets changed) |
|---|---|
| Timelines: | family | ancestors | descendants | both | sebres-encoding-perf-branch |
| Files: | files | file ages | folders |
| SHA3-256: |
a1c84c80695cce88504cde1c8fb88b6f |
| User & Date: | sebres 2024-04-26 13:47:07.305 |
References
|
2025-09-18
| ||
| 13:10 | • Ticket [893f8cc5db] Nested mutexes following TIP 509 status still Open with 3 other changes artifact: af67a51a93 user: jan.nijtmans | |
Context
|
2024-04-26
| ||
| 13:49 | more tweaks optimized encoding reusing processes (added small cache storage holding 2 last freed enc... check-in: 389529a649 user: sebres tags: sebres-encoding-perf-branch | |
| 13:47 | (backported from tclSE) avoid multi-threaded vulnerability of system encoding (see tcl bug [f2ff05fc... check-in: a1c84c8069 user: sebres tags: sebres-encoding-perf-branch | |
|
2024-04-12
| ||
| 22:55 | minor backport from my core: simple speed-up if searching for the key from hash itself (it is safe t... check-in: f472941c43 user: sebres tags: core-8-5-branch | |
Changes
Changes to generic/tclEncoding.c.
| ︙ | ︙ | |||
42 43 44 45 46 47 48 |
* type. Passed to conversion functions. */
LengthProc *lengthProc; /* Function to compute length of
* null-terminated strings in this encoding.
* If nullSize is 1, this is strlen; if
* nullSize is 2, this is a function that
* returns the number of bytes in a 0x0000
* terminated string. */
| | | 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
* type. Passed to conversion functions. */
LengthProc *lengthProc; /* Function to compute length of
* null-terminated strings in this encoding.
* If nullSize is 1, this is strlen; if
* nullSize is 2, this is a function that
* returns the number of bytes in a 0x0000
* terminated string. */
TclAtomicInt refCount; /* Number of uses of this structure. */
Tcl_HashEntry *hPtr; /* Hash table entry that owns this encoding. */
} Encoding;
/*
* The following structure is the clientData for a dynamically-loaded,
* table-driven encoding created by LoadTableEncoding(). It maps between
* Unicode and a single-byte, double-byte, or multibyte (1 or 2 bytes only)
|
| ︙ | ︙ | |||
160 161 162 163 164 165 166 |
* to each directory in this "libraryPath".
*/
static ProcessGlobalValue libraryPath = {
0, 0, NULL, NULL, TclpInitLibraryPath, NULL, NULL
};
| | | > > | | | | 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
* to each directory in this "libraryPath".
*/
static ProcessGlobalValue libraryPath = {
0, 0, NULL, NULL, TclpInitLibraryPath, NULL, NULL
};
static TclAtomicInt encodingsInitialized = 0;
/*
* Hash table that keeps track of all loaded Encodings. Keys are the string
* names that represent the encoding, values are (Encoding *).
*/
static Tcl_HashTable encodingTable;
TCL_DECLARE_MUTEX(encodingMutex);
static volatile unsigned long encodingsEpoch = 0;
/*
* The following are used to hold the default and current system encodings.
* If NULL is passed to one of the conversion routines, the current setting of
* the system encoding will be used to perform the conversion.
*/
#define defaultEncoding tclIdentityEncoding
static volatile Tcl_Encoding systemEncoding = NULL;
Tcl_Encoding tclIdentityEncoding = NULL;
/*
* The following variable is used in the sparse matrix code for a
* TableEncoding to represent a page in the table that has no entries.
*/
static unsigned short emptyPage[256];
|
| ︙ | ︙ | |||
209 210 211 212 213 214 215 | int *dstCharsPtr); static int EscapeToUtfProc(ClientData clientData, const char *src, int srcLen, int flags, Tcl_EncodingState *statePtr, char *dst, int dstLen, int *srcReadPtr, int *dstWrotePtr, int *dstCharsPtr); static void FillEncodingFileMap(void); | | | 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | int *dstCharsPtr); static int EscapeToUtfProc(ClientData clientData, const char *src, int srcLen, int flags, Tcl_EncodingState *statePtr, char *dst, int dstLen, int *srcReadPtr, int *dstWrotePtr, int *dstCharsPtr); static void FillEncodingFileMap(void); static void FreeEncoding(Encoding *encodingPtr, int lock); static void FreeEncodingIntRep(Tcl_Obj *objPtr); static Encoding * GetTableEncoding(EscapeEncodingData *dataPtr, int state); static Tcl_Encoding LoadEncodingFile(Tcl_Interp *interp, const char *name); static Tcl_Encoding LoadTableEncoding(const char *name, int type, Tcl_Channel chan); static Tcl_Encoding LoadEscapeEncoding(const char *name, Tcl_Channel chan); |
| ︙ | ︙ | |||
265 266 267 268 269 270 271 272 | int *srcReadPtr, int *dstWrotePtr, int *dstCharsPtr); static int Iso88591ToUtfProc(ClientData clientData, const char *src, int srcLen, int flags, Tcl_EncodingState *statePtr, char *dst, int dstLen, int *srcReadPtr, int *dstWrotePtr, int *dstCharsPtr); | > > > > > | > > > > > > > > > > > > > > > > > > > | | 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
int *srcReadPtr, int *dstWrotePtr,
int *dstCharsPtr);
static int Iso88591ToUtfProc(ClientData clientData,
const char *src, int srcLen, int flags,
Tcl_EncodingState *statePtr, char *dst,
int dstLen, int *srcReadPtr, int *dstWrotePtr,
int *dstCharsPtr);
static inline
Encoding *
GetSystemEncodingRef()
{
Encoding *encodingPtr;
/*
* refCount of systemEncoding gets decremented after it gets switched,
* see Tcl_SetSystemEncoding, so it is hardly possible to obtain an encoding
* with refCount of 0 (which will be freed), so simply increase it here and
* check (the improbable case) is was 1.
*/
do {
encodingPtr = (Encoding *)systemEncoding;
if (TclpAtomicIncrFetch(&encodingPtr->refCount) > 1) {
return encodingPtr;
}
/* refCount was 0 (now 1) - it going to free, so we incremented later
* than systemEncoding got switched, retry (systemEncoding already points
* to other value).
*/
} while (1);
}
/*
* A Tcl_ObjType for holding a cached Tcl_Encoding in the ptrAndLongRep.ptr field
* of the intrep. This should help the lifetime of encodings be more useful.
* See concerns raised in [Bug 1077262].
*/
static Tcl_ObjType encodingType = {
"encoding", FreeEncodingIntRep, DupEncodingIntRep, NULL, NULL
};
|
| ︙ | ︙ | |||
302 303 304 305 306 307 308 |
int
Tcl_GetEncodingFromObj(
Tcl_Interp *interp,
Tcl_Obj *objPtr,
Tcl_Encoding *encodingPtr)
{
const char *name = Tcl_GetString(objPtr);
| > > | > > | > | > > > > | > > > | > | | 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
int
Tcl_GetEncodingFromObj(
Tcl_Interp *interp,
Tcl_Obj *objPtr,
Tcl_Encoding *encodingPtr)
{
const char *name = Tcl_GetString(objPtr);
/* ensure encodings epoch is unchanged (see tests encoding-6.1, encoding-6.2) */
if (objPtr->typePtr != &encodingType
|| objPtr->internalRep.ptrAndLongRep.value != encodingsEpoch
) {
Tcl_Encoding encoding = Tcl_GetEncoding(interp, name);
if (encoding == NULL) {
return TCL_ERROR;
}
TclFreeIntRep(objPtr);
objPtr->internalRep.ptrAndLongRep.ptr = (VOID *) encoding;
objPtr->internalRep.ptrAndLongRep.value = encodingsEpoch;
objPtr->typePtr = &encodingType;
}
*encodingPtr = objPtr->internalRep.ptrAndLongRep.ptr;
/* despite the object hold the reference, related to docu, after every call
* "the caller should call Tcl_FreeEncoding on the resulting encoding token",
* so we'd increase its reference count here also: */
TclpAtomicIncrFetch(&((Encoding *)(*encodingPtr))->refCount);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* FreeEncodingIntRep --
*
* The Tcl_FreeInternalRepProc for the "encoding" Tcl_ObjType.
*
*----------------------------------------------------------------------
*/
static void
FreeEncodingIntRep(
Tcl_Obj *objPtr)
{
FreeEncoding((Encoding *)objPtr->internalRep.ptrAndLongRep.ptr, 1);
objPtr->typePtr = NULL;
}
/*
*----------------------------------------------------------------------
*
* DupEncodingIntRep --
*
* The Tcl_DupInternalRepProc for the "encoding" Tcl_ObjType.
*
*----------------------------------------------------------------------
*/
static void
DupEncodingIntRep(
Tcl_Obj *srcPtr,
Tcl_Obj *dupPtr)
{
Encoding *encodingPtr = srcPtr->internalRep.ptrAndLongRep.ptr;
TclpAtomicIncrFetch(&encodingPtr->refCount);
dupPtr->internalRep.ptrAndLongRep.ptr = encodingPtr;
dupPtr->internalRep.ptrAndLongRep.value = srcPtr->internalRep.ptrAndLongRep.value;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetEncodingSearchPath --
*
|
| ︙ | ︙ | |||
541 542 543 544 545 546 547 |
*/
void
TclInitEncodingSubsystem(void)
{
Tcl_EncodingType type;
| | | 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
*/
void
TclInitEncodingSubsystem(void)
{
Tcl_EncodingType type;
if (TclpAtomicIncrFetch(&encodingsInitialized) != 1) {
return;
}
Tcl_MutexLock(&encodingMutex);
Tcl_InitHashTable(&encodingTable, TCL_STRING_KEYS);
Tcl_MutexUnlock(&encodingMutex);
|
| ︙ | ︙ | |||
563 564 565 566 567 568 569 |
type.toUtfProc = BinaryProc;
type.fromUtfProc = BinaryProc;
type.freeProc = NULL;
type.nullSize = 1;
type.clientData = NULL;
defaultEncoding = Tcl_CreateEncoding(&type);
| | | | 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
type.toUtfProc = BinaryProc;
type.fromUtfProc = BinaryProc;
type.freeProc = NULL;
type.nullSize = 1;
type.clientData = NULL;
defaultEncoding = Tcl_CreateEncoding(&type);
TclpAtomicIncrFetch(&((Encoding *)defaultEncoding)->refCount);
systemEncoding = defaultEncoding;
type.encodingName = "utf-8";
type.toUtfProc = UtfExtToUtfIntProc;
type.fromUtfProc = UtfIntToUtfExtProc;
type.freeProc = NULL;
type.nullSize = 1;
type.clientData = NULL;
|
| ︙ | ︙ | |||
625 626 627 628 629 630 631 |
type.toUtfProc = Iso88591ToUtfProc;
type.fromUtfProc = Iso88591FromUtfProc;
type.freeProc = TableFreeProc;
type.nullSize = 1;
type.clientData = dataPtr;
Tcl_CreateEncoding(&type);
}
| < < | 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
type.toUtfProc = Iso88591ToUtfProc;
type.fromUtfProc = Iso88591FromUtfProc;
type.freeProc = TableFreeProc;
type.nullSize = 1;
type.clientData = dataPtr;
Tcl_CreateEncoding(&type);
}
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeEncodingSubsystem --
*
|
| ︙ | ︙ | |||
650 651 652 653 654 655 656 657 658 659 |
*/
void
TclFinalizeEncodingSubsystem(void)
{
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
Tcl_MutexLock(&encodingMutex);
encodingsInitialized = 0;
| > > > > > | | > | | 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
*/
void
TclFinalizeEncodingSubsystem(void)
{
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
if (!encodingsInitialized) {
return;
}
Tcl_MutexLock(&encodingMutex);
encodingsInitialized = 0;
FreeEncoding((Encoding *)systemEncoding, 0);
systemEncoding = NULL;
FreeEncoding((Encoding *)defaultEncoding, 0);
defaultEncoding = NULL;
hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
while (hPtr != NULL) {
/*
* Call FreeEncoding instead of doing it directly to handle refcounts
* like escape encodings use. [Bug 524674] Make sure to call
* Tcl_FirstHashEntry repeatedly so that all encodings are eventually
* cleaned up.
*/
FreeEncoding((Encoding *)Tcl_GetHashValue(hPtr), 0);
hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
}
Tcl_DeleteHashTable(&encodingTable);
Tcl_MutexUnlock(&encodingMutex);
}
|
| ︙ | ︙ | |||
768 769 770 771 772 773 774 |
Tcl_GetEncoding(
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */
const char *name) /* The name of the desired encoding. */
{
Tcl_HashEntry *hPtr;
Encoding *encodingPtr;
| < < < < | > | | 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 |
Tcl_GetEncoding(
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */
const char *name) /* The name of the desired encoding. */
{
Tcl_HashEntry *hPtr;
Encoding *encodingPtr;
if (name == NULL) {
return (Tcl_Encoding)GetSystemEncodingRef();
}
Tcl_MutexLock(&encodingMutex);
hPtr = Tcl_FindHashEntry(&encodingTable, name);
if (hPtr != NULL) {
encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
TclpAtomicIncrFetch(&encodingPtr->refCount);
Tcl_MutexUnlock(&encodingMutex);
return (Tcl_Encoding) encodingPtr;
}
Tcl_MutexUnlock(&encodingMutex);
return LoadEncodingFile(interp, name);
}
|
| ︙ | ︙ | |||
810 811 812 813 814 815 816 |
*---------------------------------------------------------------------------
*/
void
Tcl_FreeEncoding(
Tcl_Encoding encoding)
{
| < | < | 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 |
*---------------------------------------------------------------------------
*/
void
Tcl_FreeEncoding(
Tcl_Encoding encoding)
{
FreeEncoding((Encoding *)encoding, 1);
}
/*
*----------------------------------------------------------------------
*
* FreeEncoding --
*
|
| ︙ | ︙ | |||
835 836 837 838 839 840 841 | * the encoding may be deleted if nothing is using it anymore. * *---------------------------------------------------------------------- */ static void FreeEncoding( | < < | > | < > | | | < < > > > > > > | 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 |
* the encoding may be deleted if nothing is using it anymore.
*
*----------------------------------------------------------------------
*/
static void
FreeEncoding(
Encoding *encodingPtr,
int lock)
{
if (encodingPtr == NULL) {
return;
}
if (TclpAtomicDecrFetch(&encodingPtr->refCount) <= 0) {
if (encodingPtr->refCount < 0) {
Tcl_Panic("FreeEncoding: refcount problem !!!");
}
if (encodingPtr->freeProc != NULL) {
(*encodingPtr->freeProc)(encodingPtr->clientData);
}
if (lock) {
Tcl_MutexLock(&encodingMutex);
}
if (encodingPtr->hPtr != NULL) {
Tcl_DeleteHashEntry(encodingPtr->hPtr);
}
if (lock) {
Tcl_MutexUnlock(&encodingMutex);
}
ckfree((char *) encodingPtr->name);
ckfree((char *) encodingPtr);
}
}
/*
*-------------------------------------------------------------------------
|
| ︙ | ︙ | |||
881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
*/
const char *
Tcl_GetEncodingName(
Tcl_Encoding encoding) /* The encoding whose name to fetch. */
{
if (encoding == NULL) {
encoding = systemEncoding;
}
return ((Encoding *) encoding)->name;
}
/*
| > | 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 |
*/
const char *
Tcl_GetEncodingName(
Tcl_Encoding encoding) /* The encoding whose name to fetch. */
{
if (encoding == NULL) {
/* TODO: this may be dangerous - see tcl ticket [f2ff05fc8411be80] */
encoding = systemEncoding;
}
return ((Encoding *) encoding)->name;
}
/*
|
| ︙ | ︙ | |||
990 991 992 993 994 995 996 |
const char *name) /* The name of the desired encoding, or NULL/""
* to reset to default encoding. */
{
Tcl_Encoding encoding;
Encoding *encodingPtr;
if (!name || !*name) {
| < | < < | | | 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
const char *name) /* The name of the desired encoding, or NULL/""
* to reset to default encoding. */
{
Tcl_Encoding encoding;
Encoding *encodingPtr;
if (!name || !*name) {
encoding = defaultEncoding;
encodingPtr = (Encoding *) encoding;
TclpAtomicIncrFetch(&encodingPtr->refCount);
} else {
encoding = Tcl_GetEncoding(interp, name);
if (encoding == NULL) {
return TCL_ERROR;
}
}
encodingPtr = (Encoding *)systemEncoding;
systemEncoding = encoding;
FreeEncoding(encodingPtr, 1);
Tcl_FSMountsChanged(NULL);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
|
| ︙ | ︙ | |||
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
/*
* Remove old encoding from hash table, but don't delete it until last
* reference goes away.
*/
encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
encodingPtr->hPtr = NULL;
}
name = ckalloc((unsigned) strlen(typePtr->encodingName) + 1);
encodingPtr = (Encoding *) ckalloc(sizeof(Encoding));
encodingPtr->name = strcpy(name, typePtr->encodingName);
encodingPtr->toUtfProc = typePtr->toUtfProc;
| > > > | 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
/*
* Remove old encoding from hash table, but don't delete it until last
* reference goes away.
*/
encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
encodingPtr->hPtr = NULL;
/* increase epoch (to validate encoding stored in object repr.) */
encodingsEpoch++;
}
name = ckalloc((unsigned) strlen(typePtr->encodingName) + 1);
encodingPtr = (Encoding *) ckalloc(sizeof(Encoding));
encodingPtr->name = strcpy(name, typePtr->encodingName);
encodingPtr->toUtfProc = typePtr->toUtfProc;
|
| ︙ | ︙ | |||
1115 1116 1117 1118 1119 1120 1121 |
int srcLen, /* Source string length in bytes, or < 0 for
* encoding-specific string length. */
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the
* converted string is stored. */
{
char *dst;
Tcl_EncodingState state;
| | > | < | > > > > > > > | 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
int srcLen, /* Source string length in bytes, or < 0 for
* encoding-specific string length. */
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the
* converted string is stored. */
{
char *dst;
Tcl_EncodingState state;
Encoding *encodingPtr = (Encoding *) encoding;
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
Tcl_DStringInit(dstPtr);
dst = Tcl_DStringValue(dstPtr);
dstLen = dstPtr->spaceAvl - 1;
if (encoding == NULL) {
/* avoid [f2ff05fc8411be80], to be thread safe increase count here */
encodingPtr = GetSystemEncodingRef();
}
if (src == NULL) {
srcLen = 0;
} else if (srcLen < 0) {
srcLen = (*encodingPtr->lengthProc)(src);
}
flags = TCL_ENCODING_START | TCL_ENCODING_END;
while (1) {
result = (*encodingPtr->toUtfProc)(encodingPtr->clientData, src,
srcLen, flags, &state, dst, dstLen, &srcRead, &dstWrote,
&dstChars);
soFar = dst + dstWrote - Tcl_DStringValue(dstPtr);
if (result != TCL_CONVERT_NOSPACE) {
Tcl_DStringSetLength(dstPtr, soFar);
break;
}
flags &= ~TCL_ENCODING_START;
src += srcRead;
srcLen -= srcRead;
if (Tcl_DStringLength(dstPtr) == 0) {
Tcl_DStringSetLength(dstPtr, dstLen);
}
Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1);
dst = Tcl_DStringValue(dstPtr) + soFar;
dstLen = Tcl_DStringLength(dstPtr) - soFar - 1;
}
if (encoding == NULL) {
/* decrease count of used system encoding */
FreeEncoding(encodingPtr, 1);
}
return Tcl_DStringValue(dstPtr);
}
/*
*-------------------------------------------------------------------------
*
* Tcl_ExternalToUtf --
*
|
| ︙ | ︙ | |||
1206 1207 1208 1209 1210 1211 1212 |
int *dstWrotePtr, /* Filled with the number of bytes that were
* stored in the output buffer as a result of
* the conversion. */
int *dstCharsPtr) /* Filled with the number of characters that
* correspond to the bytes stored in the
* output buffer. */
{
| | > | < | 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 |
int *dstWrotePtr, /* Filled with the number of bytes that were
* stored in the output buffer as a result of
* the conversion. */
int *dstCharsPtr) /* Filled with the number of characters that
* correspond to the bytes stored in the
* output buffer. */
{
Encoding *encodingPtr = (Encoding *) encoding;
int result, srcRead, dstWrote, dstChars;
Tcl_EncodingState state;
if (encoding == NULL) {
/* avoid [f2ff05fc8411be80], to be thread safe increase count here */
encodingPtr = GetSystemEncodingRef();
}
if (src == NULL) {
srcLen = 0;
} else if (srcLen < 0) {
srcLen = (*encodingPtr->lengthProc)(src);
}
if (statePtr == NULL) {
|
| ︙ | ︙ | |||
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 |
dstLen--;
result = (*encodingPtr->toUtfProc)(encodingPtr->clientData, src, srcLen,
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
dstCharsPtr);
dst[*dstWrotePtr] = '\0';
return result;
}
/*
*-------------------------------------------------------------------------
*
* Tcl_UtfToExternalDString --
| > > > > | 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 |
dstLen--;
result = (*encodingPtr->toUtfProc)(encodingPtr->clientData, src, srcLen,
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
dstCharsPtr);
dst[*dstWrotePtr] = '\0';
if (encoding == NULL) {
/* decrease count of used system encoding */
FreeEncoding(encodingPtr, 1);
}
return result;
}
/*
*-------------------------------------------------------------------------
*
* Tcl_UtfToExternalDString --
|
| ︙ | ︙ | |||
1282 1283 1284 1285 1286 1287 1288 |
int srcLen, /* Source string length in bytes, or < 0 for
* strlen(). */
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the
* converted string is stored. */
{
char *dst;
Tcl_EncodingState state;
| | > | < | > > > > > > | 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 |
int srcLen, /* Source string length in bytes, or < 0 for
* strlen(). */
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the
* converted string is stored. */
{
char *dst;
Tcl_EncodingState state;
Encoding *encodingPtr = (Encoding *) encoding;
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
Tcl_DStringInit(dstPtr);
dst = Tcl_DStringValue(dstPtr);
dstLen = dstPtr->spaceAvl - 1;
if (encoding == NULL) {
/* avoid [f2ff05fc8411be80], to be thread safe increase count here */
encodingPtr = GetSystemEncodingRef();
}
if (src == NULL) {
srcLen = 0;
} else if (srcLen < 0) {
srcLen = strlen(src);
}
flags = TCL_ENCODING_START | TCL_ENCODING_END;
while (1) {
result = (*encodingPtr->fromUtfProc)(encodingPtr->clientData, src,
srcLen, flags, &state, dst, dstLen, &srcRead, &dstWrote,
&dstChars);
soFar = dst + dstWrote - Tcl_DStringValue(dstPtr);
if (result != TCL_CONVERT_NOSPACE) {
if (encodingPtr->nullSize == 2) {
Tcl_DStringSetLength(dstPtr, soFar + 1);
}
Tcl_DStringSetLength(dstPtr, soFar);
break;
}
flags &= ~TCL_ENCODING_START;
src += srcRead;
srcLen -= srcRead;
if (Tcl_DStringLength(dstPtr) == 0) {
Tcl_DStringSetLength(dstPtr, dstLen);
}
Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1);
dst = Tcl_DStringValue(dstPtr) + soFar;
dstLen = Tcl_DStringLength(dstPtr) - soFar - 1;
}
if (encoding == NULL) {
/* decrease count of used system encoding */
FreeEncoding(encodingPtr, 1);
}
return Tcl_DStringValue(dstPtr);
}
/*
*-------------------------------------------------------------------------
*
* Tcl_UtfToExternal --
*
|
| ︙ | ︙ | |||
1374 1375 1376 1377 1378 1379 1380 |
int *dstWrotePtr, /* Filled with the number of bytes that were
* stored in the output buffer as a result of
* the conversion. */
int *dstCharsPtr) /* Filled with the number of characters that
* correspond to the bytes stored in the
* output buffer. */
{
| | > | < | 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 |
int *dstWrotePtr, /* Filled with the number of bytes that were
* stored in the output buffer as a result of
* the conversion. */
int *dstCharsPtr) /* Filled with the number of characters that
* correspond to the bytes stored in the
* output buffer. */
{
Encoding *encodingPtr = (Encoding *) encoding;
int result, srcRead, dstWrote, dstChars;
Tcl_EncodingState state;
if (encoding == NULL) {
/* avoid [f2ff05fc8411be80], to be thread safe increase count here */
encodingPtr = GetSystemEncodingRef();
}
if (src == NULL) {
srcLen = 0;
} else if (srcLen < 0) {
srcLen = strlen(src);
}
if (statePtr == NULL) {
|
| ︙ | ︙ | |||
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
dstCharsPtr);
if (encodingPtr->nullSize == 2) {
dst[*dstWrotePtr + 1] = '\0';
}
dst[*dstWrotePtr] = '\0';
return result;
}
/*
*---------------------------------------------------------------------------
*
* Tcl_FindExecutable --
| > > > > | 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
dstCharsPtr);
if (encodingPtr->nullSize == 2) {
dst[*dstWrotePtr + 1] = '\0';
}
dst[*dstWrotePtr] = '\0';
if (encoding == NULL) {
/* decrease count of used system encoding */
FreeEncoding(encodingPtr, 1);
}
return result;
}
/*
*---------------------------------------------------------------------------
*
* Tcl_FindExecutable --
|
| ︙ | ︙ | |||
3404 3405 3406 3407 3408 3409 3410 |
* The encodingsInitialized flag, being reset on entry to TFES, can serve
* as a "not in finalization" test.
*/
if (encodingsInitialized)
{
subTablePtr = dataPtr->subTables;
for (i = 0; i < dataPtr->numSubTables; i++) {
| | | 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 |
* The encodingsInitialized flag, being reset on entry to TFES, can serve
* as a "not in finalization" test.
*/
if (encodingsInitialized)
{
subTablePtr = dataPtr->subTables;
for (i = 0; i < dataPtr->numSubTables; i++) {
FreeEncoding(subTablePtr->encodingPtr, 1);
subTablePtr++;
}
}
ckfree((char *) dataPtr);
}
/*
|
| ︙ | ︙ | |||
3544 3545 3546 3547 3548 3549 3550 |
Tcl_DecrRefCount(path);
}
Tcl_DecrRefCount(libPath);
Tcl_DecrRefCount(encodingObj);
*encodingPtr = libraryPath.encoding;
if (*encodingPtr) {
| | | 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 |
Tcl_DecrRefCount(path);
}
Tcl_DecrRefCount(libPath);
Tcl_DecrRefCount(encodingObj);
*encodingPtr = libraryPath.encoding;
if (*encodingPtr) {
TclpAtomicIncrFetch(&((Encoding *)(*encodingPtr))->refCount);
}
bytes = Tcl_GetStringFromObj(searchPath, &numBytes);
*lengthPtr = numBytes;
*valuePtr = ckalloc((unsigned int) numBytes + 1);
memcpy(*valuePtr, bytes, (size_t) numBytes + 1);
Tcl_DecrRefCount(searchPath);
|
| ︙ | ︙ |
Changes to generic/tclInt.h.
| ︙ | ︙ | |||
123 124 125 126 127 128 129 130 131 132 133 134 135 136 | # define PTR2UINT(p) ((unsigned int)(uintptr_t)(p)) # else # define UINT2PTR(p) ((void *)(p)) # define PTR2UINT(p) ((unsigned int)(p)) # endif #endif /* * The following procedures allow namespaces to be customized to support * special name resolution rules for commands/variables. */ struct Tcl_ResolvedVarInfo; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# define PTR2UINT(p) ((unsigned int)(uintptr_t)(p))
# else
# define UINT2PTR(p) ((void *)(p))
# define PTR2UINT(p) ((unsigned int)(p))
# endif
#endif
/*
* Interlocked (atomic fetch) primitives.
*
* Note gcc > 4.1 (also for mingw) has native support for builtin atomic operations.
* ToDo: extend with support for atomic_ops, Darwin atomic, Sun atomics if available.
*
* TclAtomicFetchAdd --
* Performs an atomic addition and returns INITIAL value (before change);
* TclAtomicFetchSub --
* Performs an atomic subtraction and returns INITIAL value (before change);
* TclpAtomicIncrFetch --
* Increments (by one) the value as an atomic operation and returns new value (after change);
* TclpAtomicDecrFetch --
* Decrements (by one) the value as an atomic operation and returns new value (after change);
*/
#if !defined(__WIN32__) || defined(__GNUC__) || defined(__MINGW32__) /* UNIX or GCC */
# define TclAtomicInt long volatile
# define TclAtomicArg long
# define TclpAtomicFetchAdd(p, add) \
__sync_fetch_and_add((p), (add))
# define TclpAtomicFetchSub(p, sub) \
__sync_fetch_and_sub((p), (sub))
# define TclpAtomicIncrFetch(p) \
(__sync_add_and_fetch((p), 1))
# define TclpAtomicDecrFetch(p) \
(__sync_sub_and_fetch((p), 1))
#else /* defined(__WIN32__) */
# define TclAtomicInt LONG volatile
# define TclAtomicArg LONG
# define TclpAtomicFetchAdd(p, add) \
InterlockedExchangeAdd((p), (add))
# define TclpAtomicFetchSub(p, sub) \
InterlockedExchangeSubtract((p), (sub))
# define TclpAtomicIncrFetch(p) \
InterlockedIncrement((p))
# define TclpAtomicDecrFetch(p) \
InterlockedDecrement((p))
#endif /* defined(__WIN32__) */
#ifdef TCL_THREADS
/* use atomic primitives */
# define TclAtomicFetchAdd(p, add) \
TclpAtomicFetchAdd((p), (add))
# define TclAtomicFetchSub(p, sub) \
TclpAtomicFetchSub((p), (sub))
# define TclAtomicIncrFetch(p) \
TclpAtomicIncrFetch((p))
# define TclAtomicDecrFetch(p) \
TclpAtomicDecrFetch((p))
#else
/* don't use atomic primitives (unsupported or unneeded) */
static inline TclAtomicInt
TclAtomicFetchAdd(TclAtomicInt *p, TclAtomicArg add) {
TclAtomicInt prev = *p; *p += add; return prev;
}
static inline TclAtomicInt
TclAtomicFetchSub(TclAtomicInt *p, TclAtomicArg sub) {
TclAtomicInt prev = *p; *p -= sub; return prev;
}
# define TclAtomicIncrFetch(p) \
(++*(p))
# define TclAtomicDecrFetch(p) \
(--*(p))
#endif
/*
* The following procedures allow namespaces to be customized to support
* special name resolution rules for commands/variables.
*/
struct Tcl_ResolvedVarInfo;
|
| ︙ | ︙ |