307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
|
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
-
-
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
|
proc ::xvfs::staticIncludeHeader {pathToHeaderFile} {
set fd [open $pathToHeaderFile]
::xvfs::staticIncludeHeaderData [read $fd]
close $fd
}
proc ::xvfs::generatePerfectHashFunctionCall {cVarName cVarLength invalidValue nameList} {
proc ::xvfs::generatePerfectHashFunctionCall {cVarName cVarLength invalidValue nameList args} {
array set config {
preferMinimalHashSize 8
switchToNonMinimalHash 1048576
triesAtHashSize 1024
maxIntermediateMultiple 8
}
foreach {configKey configVal} $args {
if {![info exists config($configKey)]} {
error "Invalid option: $configKey"
}
}
array set config $args
set minVal 0
set maxVal [llength $nameList]
set testExpr {([zlib adler32 $nameItem $alpha] + $beta) % $gamma}
set testExprC {((Tcl_ZlibAdler32($alpha, (unsigned char *) $cVarName, $cVarLength) + $beta) % $gamma)}
set testExpr(0) {([zlib adler32 $nameItem $alpha] + $beta) % $gamma}
set testExpr(1) {([zlib crc32 $nameItem $alpha] + $beta) % $gamma}
set testExpr(2) {([zlib adler32 $nameItem [zlib crc32 $nameItem $alpha]] + $beta) % $gamma}
set testExprC(0) {((Tcl_ZlibAdler32(${alpha}LU, (unsigned char *) $cVarName, $cVarLength) + ${beta}LU) % ${gamma}LU)}
set testExprC(1) {((Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) $cVarName, $cVarLength) + ${beta}LU) % ${gamma}LU)}
set testExprC(2) {((Tcl_ZlibAdler32(Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) $cVarName, $cVarLength), (unsigned char *) $cVarName, $cVarLength) + ${beta}LU) % ${gamma}LU)}
set minimal false
if {$maxVal < $config(preferMinimalHashSize)} {
set minimal true
}
set round -1
set beta 0
set gamma $maxVal
set gammaRoundMod [expr {$maxVal * ($config(maxIntermediateMultiple) - 1)}]
while true {
if {$minimal && $round > $config(switchToNonMinimalHash)} {
set minimal false
set round -1
}
incr round
if {$minimal} {
set gamma [expr {$maxVal + ($round % ($maxVal * 4))}]
} else {
set gamma [expr {$maxVal + ($round % $gammaRoundMod)}]
}
for {set try 0} {$try < $config(triesAtHashSize)} {incr try} {
set alpha $round
set gamma [expr {($round % ($maxVal + 1)) + $maxVal}]
set alpha [expr {entier(rand() * (2**31))}]
set beta [expr {entier(rand() * (2**31))}]
foreach {testExprID testExprContents} [array get testExpr] {
set idx -1
set seenIndexes [list]
set failed false
foreach nameItem $nameList {
incr idx
set idx -1
set seenIndexes [list]
set failed false
foreach nameItem $nameList {
set testExprVal [expr $testExprContents]
if {$minimal} {
incr idx
set testExprVal [expr $testExpr]
if {$testExprVal in $seenIndexes} {
if {$testExprVal != $idx} {
set failed true
break
}
} else {
if {$testExprVal in $seenIndexes} {
incr alpha
set failed true
break
}
set failed true
break
}
lappend seenIndexes $testExprVal
lappend seenIndexes $testExprVal
}
}
if {!$failed} {
break
}
}
if {!$failed} {
break
}
}
if {!$failed} {
break
}
}
if {$minimal} {
set phfCall [subst $testExprC($testExprID)]
} else {
unset -nocomplain mapArray
for {set idx 0} {$idx < $gamma} {incr idx} {
set mapArray($idx) $invalidValue
}
unset -nocomplain mapArray
for {set idx 0} {$idx < $gamma} {incr idx} {
set mapArray($idx) $invalidValue
}
set idx -1
foreach nameItem $nameList {
incr idx
set idx -1
foreach nameItem $nameList {
incr idx
set mapArray([expr $testExpr]) $idx
}
set mapArray([expr $testExpr($testExprID)]) $idx
}
set map "(long\[\])\{"
for {set idx 0} {$idx < $gamma} {incr idx} {
append map "$mapArray($idx), "
}
set map [string range $map 0 end-2]
append map "\}\[[subst $testExprC]\]"
set phfCall $map
set map "(long\[\])\{"
for {set idx 0} {$idx < $gamma} {incr idx} {
append map "$mapArray($idx), "
}
set map [string range $map 0 end-2]
append map "\}\[[subst $testExprC($testExprID)]\]"
set phfCall $map
}
return $phfCall
}
package provide xvfs 1
|