Skip to content

Commit

Permalink
(cherry-pick) Put ClockFmtScnStorageHashKeyType (and some more) in re…
Browse files Browse the repository at this point in the history
…ad-only memory
  • Loading branch information
jan.nijtmans committed Jun 9, 2024
1 parent ee03574 commit aea6ca7
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 184 deletions.
47 changes: 27 additions & 20 deletions generic/tclClockFmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,28 @@ static void ClockFmtObj_DupInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr);
static void ClockFmtObj_FreeInternalRep(Tcl_Obj *objPtr);
static int ClockFmtObj_SetFromAny(Tcl_Interp *, Tcl_Obj *objPtr);
static void ClockFmtObj_UpdateString(Tcl_Obj *objPtr);
static Tcl_HashEntry * ClockFmtScnStorageAllocProc(Tcl_HashTable *, void *keyPtr);
static void ClockFmtScnStorageFreeProc(Tcl_HashEntry *hPtr);
static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss);

TCL_DECLARE_MUTEX(ClockFmtMutex); /* Serializes access to common format list. */

static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss);

#ifndef TCL_CLOCK_FULL_COMPAT
#define TCL_CLOCK_FULL_COMPAT 1
#endif

/*
* Derivation of tclStringHashKeyType with another allocEntryProc
* Derivation of tclStringHashKeyType with extra memory management trickery.
*/

static Tcl_HashKeyType ClockFmtScnStorageHashKeyType;
static const Tcl_HashKeyType ClockFmtScnStorageHashKeyType = {
TCL_HASH_KEY_TYPE_VERSION, /* version */
0, /* flags */
TclHashStringKey, /* hashKeyProc */
TclCompareStringKeys, /* compareKeysProc */
ClockFmtScnStorageAllocProc, /* allocEntryProc */
ClockFmtScnStorageFreeProc /* freeEntryProc */
};

#define IntFieldAt(info, offset) \
((int *) (((char *) (info)) + (offset)))
Expand Down Expand Up @@ -543,7 +551,7 @@ FmtScn4HashEntry(

static Tcl_HashEntry *
ClockFmtScnStorageAllocProc(
TCL_UNUSED(Tcl_HashTable *), /* Hash table. */
TCL_UNUSED(Tcl_HashTable *),/* Hash table. */
void *keyPtr) /* Key to store in the hash table entry. */
{
ClockFmtScnStorage *fss;
Expand Down Expand Up @@ -757,7 +765,7 @@ ClockFmtObj_UpdateString(
* Returns tcl object with key or format object if not localizable.
*
* Side effects:
* Converts given format object to ClockFmtObjType on demand for caching
* Converts given format object to ClockFmtObjType on demand for caching
* the key inside its internal representation.
*
*----------------------------------------------------------------------
Expand Down Expand Up @@ -803,7 +811,7 @@ ClockFrmObjGetLocFmtKey(
* Returns scan/format storage pointer to ClockFmtScnStorage.
*
* Side effects:
* Converts given format object to ClockFmtObjType on demand for caching
* Converts given format object to ClockFmtObjType on demand for caching
* the format storage reference inside its internal representation.
* Increments objRefCount of the ClockFmtScnStorage reference.
*
Expand All @@ -824,11 +832,6 @@ FindOrCreateFmtScnStorage(

/* if not yet initialized */
if (!initialized) {
/* initialize type */
memcpy(&ClockFmtScnStorageHashKeyType, &tclStringHashKeyType, sizeof(tclStringHashKeyType));
ClockFmtScnStorageHashKeyType.allocEntryProc = ClockFmtScnStorageAllocProc;
ClockFmtScnStorageHashKeyType.freeEntryProc = ClockFmtScnStorageFreeProc;

/* initialize hash table */
Tcl_InitCustomHashTable(&FmtScnHashTable, TCL_CUSTOM_TYPE_KEYS,
&ClockFmtScnStorageHashKeyType);
Expand Down Expand Up @@ -1240,9 +1243,13 @@ ObjListSearch(
/* currently unused */

static int
LocaleListSearch(ClockFmtScnCmdArgs *opts,
DateInfo *info, int mcKey, int *val,
int minLen, int maxLen)
LocaleListSearch(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
int mcKey,
int *val,
int minLen,
int maxLen)
{
Tcl_Obj **lstv;
Tcl_Size lstc;
Expand Down Expand Up @@ -1415,7 +1422,7 @@ ClockMCGetMultiListIdxTree(
* Results:
* TCL_OK - match found and the index stored in *val,
* TCL_RETURN - not matched or ambigous,
* TCL_ERROR - in error case.
* TCL_ERROR - in error case.
*
* Side effects:
* Input points to end of the found token in string.
Expand Down Expand Up @@ -1769,7 +1776,7 @@ ClockScnToken_JDN_Proc(
}
s = p;
while (p < end && isdigit(UCHAR(*p))) {
fractJDDiv *= 10;
fractJDDiv *= 10;
p++;
}
if (Clock_str2int(&fractJD, s, p, 1) != TCL_OK) {
Expand Down Expand Up @@ -2648,7 +2655,7 @@ ClockScan(
}
if (flags & (CLF_ISO8601WEEK | CLF_ISO8601YEAR)) {
if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_YEAR) {
/* for calculations expected iso year */
/* for calculations expected iso year */
info->date.iso8601Year = yyYear;
} else if (info->date.iso8601Year < 100) {
if (!(flags & CLF_ISO8601CENTURY)) {
Expand All @@ -2661,7 +2668,7 @@ ClockScan(
}
}
if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_ISO8601YEAR) {
/* for calculations expected year (e. g. CLF_ISO8601WEEK not set) */
/* for calculations expected year (e. g. CLF_ISO8601WEEK not set) */
yyYear = info->date.iso8601Year;
}
}
Expand Down Expand Up @@ -2861,7 +2868,7 @@ ClockFmtToken_JDN_Proc(
fractJD = dateFmt->date.secondOfDay
- (int)tok->map->offs; /* 0 for calendar or 43200 for astro JD */
if (fractJD < 0) {
intJD--;
intJD--;
fractJD += SECONDS_PER_DAY;
}
if (fractJD && intJD < 0) { /* avoid jump over 0, by negative JD's */
Expand Down
44 changes: 19 additions & 25 deletions generic/tclHash.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ static TCL_HASH_TYPE HashArrayKey(Tcl_HashTable *tablePtr, void *keyPtr);

static Tcl_HashEntry * AllocStringEntry(Tcl_HashTable *tablePtr,
void *keyPtr);
static int CompareStringKeys(void *keyPtr, Tcl_HashEntry *hPtr);
static TCL_HASH_TYPE HashStringKey(Tcl_HashTable *tablePtr, void *keyPtr);

/*
* Function prototypes for static functions in this file:
Expand Down Expand Up @@ -87,8 +85,8 @@ const Tcl_HashKeyType tclOneWordHashKeyType = {
const Tcl_HashKeyType tclStringHashKeyType = {
TCL_HASH_KEY_TYPE_VERSION, /* version */
0, /* flags */
HashStringKey, /* hashKeyProc */
CompareStringKeys, /* compareKeysProc */
TclHashStringKey, /* hashKeyProc */
TclCompareStringKeys, /* compareKeysProc */
AllocStringEntry, /* allocEntryProc */
NULL /* freeEntryProc */
};
Expand Down Expand Up @@ -229,7 +227,6 @@ FindHashEntry(
{
return CreateHashEntry(tablePtr, key, NULL);
}


/*
*----------------------------------------------------------------------
Expand Down Expand Up @@ -312,8 +309,7 @@ CreateHashEntry(
}
/* if keys pointers or values are equal */
if ((key == hPtr->key.oneWordValue)
|| compareKeysProc((void *) key, hPtr)
) {
|| compareKeysProc((void *) key, hPtr)) {
if (newPtr) {
*newPtr = 0;
}
Expand All @@ -328,8 +324,7 @@ CreateHashEntry(
}
/* if needle pointer equals content pointer or values equal */
if ((key == hPtr->key.string)
|| compareKeysProc((void *) key, hPtr)
) {
|| compareKeysProc((void *) key, hPtr)) {
if (newPtr) {
*newPtr = 0;
}
Expand Down Expand Up @@ -481,7 +476,7 @@ Tcl_DeleteHashTable(
{
Tcl_HashEntry *hPtr, *nextPtr;
const Tcl_HashKeyType *typePtr;
int i;
Tcl_Size i;

if (tablePtr->keyType == TCL_STRING_KEYS) {
typePtr = &tclStringHashKeyType;
Expand Down Expand Up @@ -585,8 +580,7 @@ Tcl_FirstHashEntry(

Tcl_HashEntry *
Tcl_NextHashEntry(
Tcl_HashSearch *searchPtr)
/* Place to store information about progress
Tcl_HashSearch *searchPtr) /* Place to store information about progress
* through the table. Must have been
* initialized by calling
* Tcl_FirstHashEntry. */
Expand Down Expand Up @@ -630,7 +624,7 @@ Tcl_HashStats(
Tcl_HashTable *tablePtr) /* Table for which to produce stats. */
{
#define NUM_COUNTERS 10
int i;
Tcl_Size i;
TCL_HASH_TYPE count[NUM_COUNTERS], overflow, j;
double average, tmp;
Tcl_HashEntry *hPtr;
Expand Down Expand Up @@ -700,7 +694,7 @@ Tcl_HashStats(
static Tcl_HashEntry *
AllocArrayEntry(
Tcl_HashTable *tablePtr, /* Hash table. */
void *keyPtr) /* Key to store in the hash table entry. */
void *keyPtr) /* Key to store in the hash table entry. */
{
Tcl_HashEntry *hPtr;
TCL_HASH_TYPE count = tablePtr->keyType * sizeof(int);
Expand Down Expand Up @@ -736,7 +730,7 @@ AllocArrayEntry(

static int
CompareArrayKeys(
void *keyPtr, /* New key to compare. */
void *keyPtr, /* New key to compare. */
Tcl_HashEntry *hPtr) /* Existing key to compare. */
{
size_t count = hPtr->tablePtr->keyType * sizeof(int);
Expand Down Expand Up @@ -765,7 +759,7 @@ CompareArrayKeys(
static TCL_HASH_TYPE
HashArrayKey(
Tcl_HashTable *tablePtr, /* Hash table. */
void *keyPtr) /* Key from which to compute hash value. */
void *keyPtr) /* Key from which to compute hash value. */
{
const int *array = (const int *) keyPtr;
TCL_HASH_TYPE result;
Expand Down Expand Up @@ -797,7 +791,7 @@ HashArrayKey(
static Tcl_HashEntry *
AllocStringEntry(
TCL_UNUSED(Tcl_HashTable *),
void *keyPtr) /* Key to store in the hash table entry. */
void *keyPtr) /* Key to store in the hash table entry. */
{
const char *string = (const char *) keyPtr;
Tcl_HashEntry *hPtr;
Expand All @@ -817,7 +811,7 @@ AllocStringEntry(
/*
*----------------------------------------------------------------------
*
* CompareStringKeys --
* TclCompareStringKeys --
*
* Compares two string keys.
*
Expand All @@ -831,9 +825,9 @@ AllocStringEntry(
*----------------------------------------------------------------------
*/

static int
CompareStringKeys(
void *keyPtr, /* New key to compare. */
int
TclCompareStringKeys(
void *keyPtr, /* New key to compare. */
Tcl_HashEntry *hPtr) /* Existing key to compare. */
{
return !strcmp((char *)keyPtr, hPtr->key.string);
Expand All @@ -842,7 +836,7 @@ CompareStringKeys(
/*
*----------------------------------------------------------------------
*
* HashStringKey --
* TclHashStringKey --
*
* Compute a one-word summary of a text string, which can be used to
* generate a hash index.
Expand All @@ -856,10 +850,10 @@ CompareStringKeys(
*----------------------------------------------------------------------
*/

static TCL_HASH_TYPE
HashStringKey(
TCL_HASH_TYPE
TclHashStringKey(
TCL_UNUSED(Tcl_HashTable *),
void *keyPtr) /* Key from which to compute hash value. */
void *keyPtr) /* Key from which to compute hash value. */
{
const char *string = (const char *)keyPtr;
TCL_HASH_TYPE result;
Expand Down
2 changes: 2 additions & 0 deletions generic/tclInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,8 @@ MODULE_SCOPE int TclGetLoadedLibraries(Tcl_Interp *interp,
const char *packageName);
MODULE_SCOPE int TclGetWideBitsFromObj(Tcl_Interp *, Tcl_Obj *,
Tcl_WideInt *);
MODULE_SCOPE int TclCompareStringKeys(void *keyPtr, Tcl_HashEntry *hPtr);
MODULE_SCOPE TCL_HASH_TYPE TclHashStringKey(Tcl_HashTable *tablePtr, void *keyPtr);
MODULE_SCOPE int TclIncrObj(Tcl_Interp *interp, Tcl_Obj *valuePtr,
Tcl_Obj *incrPtr);
MODULE_SCOPE Tcl_Obj * TclIncrObjVar2(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
Expand Down
Loading

0 comments on commit aea6ca7

Please sign in to comment.