Skip to content

Commit a51819a

Browse files
committed
Backed out multi-interpreter solution. Moved to with checkin [86490c608c] to branch [ecf13be4c9-multi-interpreter] of ticket [ecf13be4c9]
1 parent 4e5bc76 commit a51819a

File tree

1 file changed

+36
-106
lines changed

1 file changed

+36
-106
lines changed

generic/tclsample.c

Lines changed: 36 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@
2828
static const unsigned char itoa64f[] =
2929
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_,";
3030

31-
#define DIGESTSIZE 20
31+
static int numcontexts = 0;
32+
static SHA1_CTX *sha1Contexts = NULL;
33+
static Tcl_Size *ctxtotalRead = NULL;
3234

33-
/*
34-
* The procedure needs interpreter local state. This is called
35-
* "Command client data" in TCL. Typically, a struct is allocated and
36-
* the pointer to it is made available on each operation by TCL.
37-
* Here is the struct for the sha1 procedure.
38-
*/
35+
static int Sha1_Cmd(void *clientData, Tcl_Interp *interp,
36+
int objc, Tcl_Obj *const objv[]);
3937

40-
struct Sha1ClientData {
41-
int numcontexts;
42-
SHA1_CTX *sha1Contexts;
43-
Tcl_Size *ctxtotalRead;
44-
};
38+
#define DIGESTSIZE 20
4539

4640
/*
4741
* The DLL needs interpreter local storage to get the command tolkens to the
@@ -81,18 +75,12 @@ struct DllAssocData {
8175

8276
static int
8377
Sha1_Cmd(
84-
ClientData clientData, /* Client data with thread local state */
78+
void *dummy, /* Not used. */
8579
Tcl_Interp *interp, /* Current interpreter */
8680
int objc, /* Number of arguments */
8781
Tcl_Obj *const objv[] /* Argument strings */
8882
)
8983
{
90-
/*
91-
* Get my thread local memory
92-
*/
93-
94-
struct Sha1ClientData *sha1ClientDataPtr = clientData;
95-
9684
/*
9785
* The default base is hex
9886
*/
@@ -104,13 +92,15 @@ Sha1_Cmd(
10492
Tcl_Channel copychan = NULL;
10593
int mode;
10694
int contextnum = 0;
95+
#define sha1Context (sha1Contexts[contextnum])
10796
char *bufPtr;
10897
Tcl_WideInt maxbytes = 0;
10998
int doinit = 1;
11099
int dofinal = 1;
111100
Tcl_Obj *descriptorObj = NULL;
112101
Tcl_Size totalRead = 0, n;
113102
int i, j, mask, bits, offset;
103+
(void)dummy;
114104

115105
/*
116106
* For binary representation + null char
@@ -143,26 +133,24 @@ Sha1_Cmd(
143133
}
144134
switch ((enum ShaOpts) index) {
145135
case SHAOPT_INIT:
146-
for (contextnum = 1; contextnum < sha1ClientDataPtr->numcontexts; contextnum++) {
147-
if (sha1ClientDataPtr->ctxtotalRead[contextnum] == -1) {
136+
for (contextnum = 1; contextnum < numcontexts; contextnum++) {
137+
if (ctxtotalRead[contextnum] == -1) {
148138
break;
149139
}
150140
}
151-
if (contextnum == sha1ClientDataPtr->numcontexts) {
141+
if (contextnum == numcontexts) {
152142
/*
153143
* Allocate a new context.
154144
*/
155145

156-
sha1ClientDataPtr->numcontexts++;
157-
sha1ClientDataPtr->sha1Contexts = (SHA1_CTX *) ckrealloc(
158-
(void *) sha1ClientDataPtr->sha1Contexts,
159-
sha1ClientDataPtr->numcontexts * sizeof(SHA1_CTX));
160-
sha1ClientDataPtr->ctxtotalRead = (Tcl_Size *)ckrealloc(
161-
sha1ClientDataPtr->ctxtotalRead,
162-
sha1ClientDataPtr->numcontexts * sizeof(Tcl_Size));
146+
numcontexts++;
147+
sha1Contexts = (SHA1_CTX *) ckrealloc((void *) sha1Contexts,
148+
numcontexts * sizeof(SHA1_CTX));
149+
ctxtotalRead = (Tcl_Size *)ckrealloc(ctxtotalRead,
150+
numcontexts * sizeof(Tcl_Size));
163151
}
164-
sha1ClientDataPtr->ctxtotalRead[contextnum] = 0;
165-
SHA1Init(&sha1ClientDataPtr->sha1Contexts[contextnum]);
152+
ctxtotalRead[contextnum] = 0;
153+
SHA1Init(&sha1Context);
166154
snprintf(buf, sizeof(buf), "sha1%d", contextnum);
167155
Tcl_AppendResult(interp, buf, NULL);
168156
return TCL_OK;
@@ -220,16 +208,16 @@ Sha1_Cmd(
220208

221209
if (descriptorObj != NULL) {
222210
if ((sscanf(Tcl_GetString(descriptorObj), "sha1%d",
223-
&contextnum) != 1) || (contextnum >= sha1ClientDataPtr->numcontexts) ||
224-
(sha1ClientDataPtr->ctxtotalRead[contextnum] == -1)) {
211+
&contextnum) != 1) || (contextnum >= numcontexts) ||
212+
(ctxtotalRead[contextnum] == -1)) {
225213
Tcl_AppendResult(interp, "invalid sha1 descriptor \"",
226214
Tcl_GetString(descriptorObj), "\"", NULL);
227215
return TCL_ERROR;
228216
}
229217
}
230218

231219
if (doinit) {
232-
SHA1Init(&sha1ClientDataPtr->sha1Contexts[contextnum]);
220+
SHA1Init(&sha1Context);
233221
}
234222

235223
if (stringObj != NULL) {
@@ -238,17 +226,10 @@ Sha1_Cmd(
238226
goto wrongArgs;
239227
}
240228
string = Tcl_GetStringFromObj(stringObj, &totalRead);
241-
SHA1Update(&sha1ClientDataPtr->sha1Contexts[contextnum],
242-
(unsigned char *) string, totalRead);
229+
SHA1Update(&sha1Context, (unsigned char *) string, totalRead);
243230
} else if (chan != NULL) {
244231
bufPtr = (char *)ckalloc(TCL_READ_CHUNK_SIZE);
245232
totalRead = 0;
246-
/*
247-
* FIXME: MS-VC 2015 gives the following warning in the next line I
248-
* was not able to fix (translated from German):
249-
* warning C4244: "Function": Conversion of "Tcl_WideInt" to "int",
250-
* possible data loss
251-
*/
252233
while ((n = Tcl_Read(chan, bufPtr,
253234
maxbytes == 0
254235
? TCL_READ_CHUNK_SIZE
@@ -265,8 +246,7 @@ Sha1_Cmd(
265246

266247
totalRead += n;
267248

268-
SHA1Update(&sha1ClientDataPtr->sha1Contexts[contextnum],
269-
(unsigned char *) bufPtr, n);
249+
SHA1Update(&sha1Context, (unsigned char *) bufPtr, n);
270250

271251
if (copychan != NULL) {
272252
n = Tcl_Write(copychan, bufPtr, n);
@@ -290,17 +270,17 @@ Sha1_Cmd(
290270
}
291271

292272
if (!dofinal) {
293-
sha1ClientDataPtr->ctxtotalRead[contextnum] += totalRead;
273+
ctxtotalRead[contextnum] += totalRead;
294274
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(totalRead));
295275
return TCL_OK;
296276
}
297277

298278
if (stringObj == NULL) {
299-
totalRead += sha1ClientDataPtr->ctxtotalRead[contextnum];
279+
totalRead += ctxtotalRead[contextnum];
300280
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(totalRead));
301281
}
302282

303-
SHA1Final(&sha1ClientDataPtr->sha1Contexts[contextnum], digest);
283+
SHA1Final(&sha1Context, digest);
304284

305285
/*
306286
* Take the 20 byte array and print it in the requested base
@@ -337,7 +317,7 @@ Sha1_Cmd(
337317
buf[j++] = '\0';
338318
Tcl_AppendResult(interp, buf, NULL);
339319
if (contextnum > 0) {
340-
sha1ClientDataPtr->ctxtotalRead[contextnum] = -1;
320+
ctxtotalRead[contextnum] = -1;
341321
}
342322
return TCL_OK;
343323

@@ -366,42 +346,6 @@ Sha1_Cmd(
366346
return TCL_ERROR;
367347
}
368348

369-
370-
/*
371-
*----------------------------------------------------------------------
372-
*
373-
* Sha1_CmdDeleteProc --
374-
*
375-
* Clear all thread data of the Sha1 command.
376-
*
377-
* Results:
378-
* No result
379-
*
380-
* Side effects:
381-
* None.
382-
*
383-
*----------------------------------------------------------------------
384-
*/
385-
386-
static void
387-
Sha1_CmdDeleteProc(ClientData clientData)
388-
{
389-
struct Sha1ClientData *sha1ClientDataPtr = clientData;
390-
391-
/*
392-
* Release the sha1 contextes
393-
*/
394-
395-
ckfree(sha1ClientDataPtr->sha1Contexts);
396-
ckfree(sha1ClientDataPtr->ctxtotalRead);
397-
398-
/*
399-
* Release the procedure client data
400-
*/
401-
402-
ckfree(sha1ClientDataPtr);
403-
}
404-
405349

406350
/*
407351
*----------------------------------------------------------------------
@@ -496,7 +440,6 @@ Sample_Init(
496440
{
497441
Tcl_CmdInfo info;
498442
struct DllAssocData *dllAssocDataPtr;
499-
struct Sha1ClientData *sha1ClientDataPtr;
500443

501444
/*
502445
* Require compatible TCL version.
@@ -519,26 +462,6 @@ Sample_Init(
519462
dllAssocDataPtr = ckalloc(sizeof(struct DllAssocData));
520463
Tcl_SetAssocData(interp, ASSOC_DATA_KEY, pkgInterpDeleted, dllAssocDataPtr);
521464

522-
/*
523-
* Init the sha1 context queues
524-
*/
525-
526-
sha1ClientDataPtr = ckalloc(sizeof(struct Sha1ClientData));
527-
sha1ClientDataPtr->numcontexts = 1;
528-
sha1ClientDataPtr->sha1Contexts = (SHA1_CTX *) ckalloc(sizeof(SHA1_CTX));
529-
sha1ClientDataPtr->ctxtotalRead = (Tcl_Size *) ckalloc(sizeof(Tcl_Size));
530-
531-
/*
532-
* Create the sha1 command.
533-
* Pass the client data pointer to the procedure, so the queue data is
534-
* available.
535-
* Also, register a delete proc to clear the sha1 queue on deletion.
536-
*/
537-
538-
dllAssocDataPtr->sha1CmdTolken = Tcl_CreateObjCommand(
539-
interp, "sha1", (Tcl_ObjCmdProc *)Sha1_Cmd,
540-
sha1ClientDataPtr, Sha1_CmdDeleteProc);
541-
542465
/*
543466
* Create the buildinfo command if tcl supports it
544467
*/
@@ -608,12 +531,19 @@ Sample_Init(
608531

609532
dllAssocDataPtr->buildInfoCmdTolken = NULL;
610533
}
611-
534+
612535
/* Provide the current package */
613536

614537
if (Tcl_PkgProvideEx(interp, PACKAGE_NAME, PACKAGE_VERSION, NULL) != TCL_OK) {
615538
return TCL_ERROR;
616539
}
540+
dllAssocDataPtr->sha1CmdTolken = Tcl_CreateObjCommand(interp, "sha1", (Tcl_ObjCmdProc *)Sha1_Cmd,
541+
NULL, NULL);
542+
543+
numcontexts = 1;
544+
sha1Contexts = (SHA1_CTX *) ckalloc(sizeof(SHA1_CTX));
545+
ctxtotalRead = (Tcl_Size *) ckalloc(sizeof(Tcl_Size));
546+
ctxtotalRead[0] = 0;
617547

618548
return TCL_OK;
619549
}

0 commit comments

Comments
 (0)