PRT_MACHINEINST_PRIV * PrtMkMachinePrivate( _Inout_ PRT_PROCESS_PRIV *process, _In_ PRT_UINT32 instanceOf, _In_ PRT_VALUE *payload ) { PRT_UINT32 packSize; PRT_UINT32 nVars; PRT_UINT8 eQSize; PRT_MACHINEINST_PRIV *context; PRT_UINT32 i; PrtLockMutex(process->processLock); nVars = process->program->machines[instanceOf].nVars; eQSize = PRT_QUEUE_LEN_DEFAULT; // // Allocate memory for state machine context // context = (PRT_MACHINEINST_PRIV*)PrtMalloc(sizeof(PRT_MACHINEINST_PRIV)); // // Add it to the array of machines in the process // PRT_UINT32 numMachines = process->numMachines; PRT_UINT32 machineCount = process->machineCount; PRT_MACHINEINST **machines = process->machines; if (machineCount == 0) { machines = (PRT_MACHINEINST **)PrtCalloc(1, sizeof(PRT_MACHINEINST *)); process->machines = machines; process->machineCount = 1; } else if (machineCount == numMachines) { PRT_MACHINEINST **newMachines = (PRT_MACHINEINST **)PrtCalloc(2 * machineCount, sizeof(PRT_MACHINEINST *)); for (PRT_UINT32 i = 0; i < machineCount; i++) { newMachines[i] = machines[i]; } PrtFree(machines); machines = newMachines; process->machines = newMachines; process->machineCount = 2 * machineCount; } machines[numMachines] = (PRT_MACHINEINST *)context; process->numMachines++; // // Initialize Machine Identity // context->process = (PRT_PROCESS *)process; context->instanceOf = instanceOf; PRT_MACHINEID id; id.machineId = process->numMachines; // index begins with 1 since 0 is reserved id.processId = process->guid; context->id = PrtMkMachineValue(id); context->extContext = NULL; context->isModel = PRT_FALSE; // // Initialize the map used in PrtDist, map from sender to the last seqnumber received // PRT_TYPE *domType = PrtMkPrimitiveType(PRT_KIND_MACHINE); PRT_TYPE *codType = PrtMkPrimitiveType(PRT_KIND_INT); PRT_TYPE *recvMapType = PrtMkMapType(domType, codType); context->recvMap = PrtMkDefaultValue(recvMapType); PrtFreeType(domType); PrtFreeType(codType); PrtFreeType(recvMapType); // Initialize Machine Internal Variables // context->currentState = process->program->machines[context->instanceOf].initStateIndex; context->isRunning = PRT_FALSE; context->isHalted = PRT_FALSE; context->lastOperation = ReturnStatement; context->currentTrigger = PrtMkEventValue(PRT_SPECIAL_EVENT_NULL); context->currentPayload = PrtCloneValue(payload); // // Allocate memory for local variables and initialize them // context->varValues = NULL; if (nVars > 0) { context->varValues = PrtCalloc(nVars, sizeof(PRT_VALUE*)); for (i = 0; i < nVars; i++) { context->varValues[i] = PrtMkDefaultValue(process->program->machines[instanceOf].vars[i].type); } } context->receive = NULL; // // Initialize various stacks // context->callStack.length = 0; context->funStack.length = 0; // // Initialize event queue // context->eventQueue.eventsSize = eQSize; context->eventQueue.events = (PRT_EVENT*)PrtCalloc(eQSize, sizeof(PRT_EVENT)); context->eventQueue.headIndex = 0; context->eventQueue.tailIndex = 0; context->eventQueue.size = 0; packSize = PrtGetPackSize(context); // // Initialize Inherited Deferred Set // context->inheritedDeferredSetCompact = (PRT_UINT32*)PrtCalloc(packSize, sizeof(PRT_UINT32)); // // Initialize the current deferred set // context->currentDeferredSetCompact = (PRT_UINT32*)PrtCalloc(packSize, sizeof(PRT_UINT32)); // // Initialize actions // context->inheritedActionSetCompact = (PRT_UINT32*)PrtCalloc(packSize, sizeof(PRT_UINT32)); context->currentActionSetCompact = (PRT_UINT32*)PrtCalloc(packSize, sizeof(PRT_UINT32)); // //Initialize state machine lock // context->stateMachineLock = PrtCreateMutex(); // //Log // PrtLog(PRT_STEP_CREATE, context); // // Allocate external context Structure // process->program->machines[context->instanceOf].extCtorFun((PRT_MACHINEINST *)context, payload); PrtUnlockMutex(process->processLock); // // Run the state machine // PrtLockMutex(context->stateMachineLock); PrtRunStateMachine(context, PRT_FALSE); return context; }
void PRT_CALL_CONV PrtFreeType(_Inout_ PRT_TYPE *type) { PRT_TYPE_KIND kind = type->typeKind; switch (kind) { case PRT_KIND_ANY: case PRT_KIND_BOOL: case PRT_KIND_EVENT: case PRT_KIND_MACHINE: case PRT_KIND_INT: case PRT_KIND_FORGN: case PRT_KIND_NULL: type->typeKind = PRT_TYPE_KIND_CANARY; PrtFree(type); break; case PRT_KIND_MAP: { PRT_MAPTYPE *mtype = (PRT_MAPTYPE *)type->typeUnion.map; PrtFreeType(mtype->domType); PrtFreeType(mtype->codType); type->typeKind = PRT_TYPE_KIND_CANARY; PrtFree(mtype); PrtFree(type); break; } case PRT_KIND_NMDTUP: { PRT_UINT32 i; PRT_NMDTUPTYPE *ntype = type->typeUnion.nmTuple; for (i = 0; i < ntype->arity; ++i) { PrtFree(ntype->fieldNames[i]); PrtFreeType(ntype->fieldTypes[i]); } PrtFree(ntype->fieldNames); PrtFree(ntype->fieldTypes); type->typeKind = PRT_TYPE_KIND_CANARY; PrtFree(ntype); PrtFree(type); break; } case PRT_KIND_SEQ: { PRT_SEQTYPE *stype = type->typeUnion.seq; PrtFreeType(stype->innerType); type->typeKind = PRT_TYPE_KIND_CANARY; PrtFree(stype); PrtFree(type); break; } case PRT_KIND_TUPLE: { PRT_UINT32 i; PRT_TUPTYPE *ttype = type->typeUnion.tuple; for (i = 0; i < ttype->arity; ++i) { PrtFreeType(ttype->fieldTypes[i]); } PrtFree(ttype->fieldTypes); type->typeKind = PRT_TYPE_KIND_CANARY; PrtFree(ttype); PrtFree(type); break; } default: PrtAssert(PRT_FALSE, "PrtFreeType: Invalid type"); break; } }