void PrtPopState( _Inout_ PRT_MACHINEINST_PRIV *context, _In_ PRT_BOOLEAN isPopStatement ) { PRT_UINT16 i; PRT_UINT16 packSize; PRT_UINT16 length; PRT_STATESTACK_INFO poppedState; i = 0; packSize = PrtGetPackSize(context); length = context->callStack.length; if (length == 0) { if (PrtPrimGetEvent(PrtGetCurrentTrigger(context)) == PRT_SPECIAL_EVENT_HALT) { PrtHaltMachine(context); } else { PrtHandleError(PRT_STATUS_EVENT_UNHANDLED, context); } return; } context->callStack.length = length - 1; poppedState = context->callStack.stateStack[length - 1]; context->currentState = poppedState.stateIndex; for (i = 0; i < packSize; i++) { context->inheritedDeferredSetCompact[i] = poppedState.inheritedDeferredSetCompact[i]; context->inheritedActionSetCompact[i] = poppedState.inheritedActionSetCompact[i]; } PrtFree(poppedState.inheritedDeferredSetCompact); PrtFree(poppedState.inheritedActionSetCompact); PrtUpdateCurrentActionsSet(context); PrtUpdateCurrentDeferredSet(context); if (isPopStatement) { PrtLog(PRT_STEP_POP, context); } else { // unhandled event PrtLog(PRT_STEP_UNHANDLED, context); } }
PRT_VALUE* PrtFormatPrintf(_In_ PRT_CSTRING msg, ...) { PrtPrintf(msg); va_list argp; va_start(argp, msg); PRT_UINT32 numArgs, numSegs; numArgs = va_arg(argp, PRT_UINT32); PRT_VALUE **args = (PRT_VALUE **)PrtCalloc(numArgs, sizeof(PRT_VALUE *)); for (PRT_UINT32 i = 0; i < numArgs; i++) { // skip over arg status PRT_FUN_PARAM_STATUS argStatus = va_arg(argp, PRT_FUN_PARAM_STATUS); args[i] = va_arg(argp, PRT_VALUE *); } numSegs = va_arg(argp, PRT_UINT32); for (PRT_UINT32 i = 0; i < numSegs; i++) { PRT_UINT32 argIndex = va_arg(argp, PRT_UINT32); PrtPrintValue(args[argIndex]); PRT_CSTRING seg = va_arg(argp, PRT_CSTRING); PrtPrintf(seg); } va_end(argp); PrtFree(args); return NULL; }
void PrtFreeLocals( _In_ PRT_MACHINEINST_PRIV *context, _Inout_ PRT_FUNSTACK_INFO *frame ) { if (!frame->freeLocals) { return; } if (frame->locals == NULL) { return; } PRT_FUNDECL *funDecl = &context->process->program->machines[context->instanceOf].funs[frame->funIndex]; for (PRT_UINT32 i = 0; i < funDecl->maxNumLocals; i++) { if (frame->locals[i] != NULL) { PrtFreeValue(frame->locals[i]); } } PrtFree(frame->locals); }
void P_DTOR_Timer_IMPL(PRT_MACHINEINST *context) { TimerContext *timerContext; timerContext = (TimerContext *)context->extContext; PrtFreeValue(timerContext->client); PrtFree(timerContext); }
void P_DTOR_Timer_IMPL(PRT_MACHINEINST *context) { printf("Entering P_DTOR_Timer_IMPL\n"); TimerContext *timerContext; timerContext = (TimerContext *)context->extContext; PrtFreeValue(timerContext->client); CloseHandle(timerContext->timer); PrtFree(timerContext); }
void UserFreeForeignValue(_In_ PRT_UINT16 typeTag, _Inout_ PRT_UINT64 frgnVal) { switch (typeTag) { case P_FORGN_TYPE_StringType: PrtFree((PRT_STRING)frgnVal);; default: break; }; }
void PRT_CALL_CONV PrtPrintType(_In_ PRT_TYPE *type) { char *buffer = NULL; PRT_UINT32 bufferSize = 0; PRT_UINT32 nChars = 0; PrtUserPrintType(type, &buffer, &bufferSize, &nChars); PRT_DBG_ASSERT(buffer[nChars] == '\0', "Expected null terminated result"); PrtPrintf(buffer); PrtFree(buffer); }
void PRT_CALL_CONV PrtPrintStep(_In_ PRT_STEP step, _In_ PRT_MACHINEINST *sender, _In_ PRT_MACHINEINST *receiver, _In_ PRT_VALUE* event, _In_ PRT_VALUE* payload) { char *buffer = NULL; PRT_UINT32 bufferSize = 0; PRT_UINT32 nChars = 0; PrtUserPrintStep(step, sender, receiver, event, payload, &buffer, &bufferSize, &nChars); PRT_DBG_ASSERT(buffer[nChars] == '\0', "Expected null terminated result"); PrtPrintf(buffer); PrtFree(buffer); }
void PrtResizeEventQueue( _Inout_ PRT_MACHINEINST_PRIV *context ) { PRT_UINT32 maxEventQueueSize = context->process->program->machines[context->instanceOf].maxQueueSize; PRT_UINT32 currEventQueueSize = context->eventQueue.eventsSize; PRT_UINT32 newQueueSize = (maxEventQueueSize != 0xffffffff && currEventQueueSize * 2 > maxEventQueueSize) ? maxEventQueueSize : currEventQueueSize * 2; PRT_EVENT* oldQueue = context->eventQueue.events; PRT_UINT32 oldHead = context->eventQueue.headIndex; PRT_UINT32 oldTail = context->eventQueue.tailIndex; PRT_EVENT *newQueue = (PRT_EVENT*)PrtCalloc(newQueueSize, sizeof(PRT_EVENT)); PRT_UINT32 newHead = 0; PRT_UINT32 newTail = 0; // // Check from head to end of Array // while (oldHead < currEventQueueSize) { newQueue[newTail] = oldQueue[oldHead]; newTail++; oldHead++; } // // Reset Head to the start of Array oldHead = 0; // // Check from start of Array till head // while (oldHead < oldTail) { newQueue[newTail] = oldQueue[oldHead]; newTail++; oldHead++; } //Update the Queue context->eventQueue.events = newQueue; context->eventQueue.headIndex = newHead; context->eventQueue.size = newTail - newHead; context->eventQueue.tailIndex = newTail; context->eventQueue.eventsSize = newQueueSize; //Release the older Queue PrtFree(oldQueue); }
static void ResizeBuffer(_Inout_ char **buffer, _Inout_ PRT_UINT32 *bufferSize, _Inout_ PRT_UINT32 numCharsWritten, PRT_UINT32 resizeNum) { PRT_UINT32 padding = 100; if (*buffer == NULL) { *bufferSize = resizeNum + 1 + padding; *buffer = (char *)PrtCalloc(*bufferSize, sizeof(char)); } else if (*bufferSize < numCharsWritten + resizeNum + 1) { PRT_UINT32 newBufferSize = numCharsWritten + resizeNum + 1 + padding; char *newBuffer = (char *)PrtCalloc(newBufferSize, sizeof(char)); strcpy_s(newBuffer, newBufferSize, *buffer); PrtFree(*buffer); *buffer = newBuffer; *bufferSize = newBufferSize; } }
void P_DTOR_Client_IMPL(PRT_MACHINEINST *context) { ClientContext *clientContext = (ClientContext *)context->extContext; PrtFreeValue(clientContext->client); PrtFree(clientContext); }
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 PrtCleanupMachine( _Inout_ PRT_MACHINEINST_PRIV *context ) { if (context->isHalted) return; if (context->eventQueue.events != NULL) { PRT_EVENT *queue = context->eventQueue.events; PRT_UINT32 head = context->eventQueue.headIndex; PRT_UINT32 tail = context->eventQueue.tailIndex; PRT_UINT32 count = 0; while (count < context->eventQueue.size && head < context->eventQueue.eventsSize) { if (queue[head].payload != NULL) { PrtFreeValue(queue[head].payload); } if (queue[head].trigger != NULL) { PrtFreeValue(queue[head].trigger); } head++; count++; } head = 0; while (count < context->eventQueue.size) { if (queue[head].payload != NULL) { PrtFreeValue(queue[head].payload); } if (queue[head].trigger != NULL) { PrtFreeValue(queue[head].trigger); } head++; count++; } PrtFree(context->eventQueue.events); } for (int i = 0; i < context->callStack.length; i++) { PRT_STATESTACK_INFO *info = &context->callStack.stateStack[i]; if (info->inheritedActionSetCompact != NULL) { PrtFree(info->inheritedActionSetCompact); } if (info->inheritedDeferredSetCompact != NULL) { PrtFree(info->inheritedDeferredSetCompact); } } for (int i = 0; i < context->funStack.length; i++) { PRT_FUNSTACK_INFO *info = &context->funStack.funs[i]; PrtFreeLocals(context, info); } if (context->currentActionSetCompact != NULL) { PrtFree(context->currentActionSetCompact); } if (context->currentDeferredSetCompact != NULL) { PrtFree(context->currentDeferredSetCompact); } if (context->inheritedActionSetCompact != NULL) { PrtFree(context->inheritedActionSetCompact); } if (context->inheritedDeferredSetCompact != NULL) { PrtFree(context->inheritedDeferredSetCompact); } if (context->varValues != NULL) { UINT i; PRT_MACHINEDECL *mdecl = &(context->process->program->machines[context->instanceOf]); for (i = 0; i < mdecl->nVars; i++) { PrtFreeValue(context->varValues[i]); } PrtFree(context->varValues); } if (context->stateMachineLock != NULL) { PrtDestroyMutex(context->stateMachineLock); } if (context->extContext != NULL) context->process->program->machines[context->instanceOf].extDtorFun((PRT_MACHINEINST *)context); PrtFreeValue(context->id); PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); if (context->recvMap != NULL) { PrtFreeValue(context->recvMap); } }
int main(int argc, char *argv[]) { #ifndef PRT_PLAT_WINUSER init_ros("test_motion_planner", &argc, argv); #endif if (!ParseCommandLine(argc, argv)) { PrintUsage(); return 1; } const char* motion_planner_delta_s = getenv("MOTION_PLANNER_DELTA"); if(motion_planner_delta_s) { Delta = atoi(motion_planner_delta_s); printf("Using MOTION_PLANNER_DELTA = %d\n", Delta); } printf("Press any key to start simulation\n"); getchar(); PRT_DBG_START_MEM_BALANCED_REGION { PRT_PROCESS *process; PRT_GUID processGuid; PRT_VALUE *payload; //Initialize the workspace WORKSPACE_INFO = ParseWorkspaceConfig(workspaceConfig); #ifdef USE_DIJKSTRA_PRECOMPUTATION WS_LocationsList ends; ends.size = WORKSPACE_INFO->starts.size + WORKSPACE_INFO->ends.size; ends.locations = malloc(sizeof(WS_Coord) * ends.size); int count = 0; for(int i=0; i < WORKSPACE_INFO->starts.size; i++) { ends.locations[count++] = WORKSPACE_INFO->starts.locations[i]; } for(int i=0; i < WORKSPACE_INFO->ends.size; i++) { ends.locations[count++] = WORKSPACE_INFO->ends.locations[i]; } PreComputeObstacleDistanceH(WORKSPACE_INFO->dimension, WORKSPACE_INFO->obstacles, ends); #endif processGuid.data1 = 1; processGuid.data2 = 0; processGuid.data3 = 0; processGuid.data4 = 0; process = PrtStartProcess(processGuid, &P_GEND_PROGRAM, ErrorHandler, Log); if (cooperative) { PrtSetSchedulingPolicy(process, PRT_SCHEDULINGPOLICY_COOPERATIVE); } if (parg == NULL) { payload = PrtMkNullValue(); } else { int i = atoi(parg); payload = PrtMkIntValue(i); } PrtUpdateAssertFn(MyAssert); PrtMkMachine(process, P_MACHINE_Main, payload); if (cooperative) { // test some multithreading across state machines. #if defined(PRT_PLAT_WINUSER) HANDLE* threadsArr = (HANDLE*)PrtMalloc(threads*sizeof(HANDLE)); for (int i = 0; i < threads; i++) { DWORD threadId; threadsArr[i] = CreateThread(NULL, 16000, (LPTHREAD_START_ROUTINE)RunToIdle, process, 0, &threadId); } WaitForMultipleObjects(threads, threadsArr, TRUE, INFINITE); PrtFree(threadsArr); #elif defined(PRT_PLAT_LINUXUSER) typedef void *(*start_routine) (void *); pthread_t tid[threads]; for (int i = 0; i < threads; i++) { pthread_create(&tid[i], NULL, (start_routine)RunToIdle, (void*)process); } for (int i = 0; i < threads; i++) { pthread_join(tid[i], NULL); } #else #error Invalid Platform #endif } PrtFreeValue(payload); PrtStopProcess(process); } PRT_DBG_END_MEM_BALANCED_REGION //_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); //_CrtDumpMemoryLeaks(); }
void PRT_FORGN_FREE_StringType_IMPL(PRT_UINT64 frgnVal) { PrtFree((PRT_STRING)frgnVal); }
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; } }
static void PrtUserPrintValue(_In_ PRT_VALUE *value, _Inout_ char **buffer, _Inout_ PRT_UINT32 *bufferSize, _Inout_ PRT_UINT32 *numCharsWritten) { PRT_STRING frgnStr; PRT_VALUE_KIND kind = value->discriminator; switch (kind) { case PRT_VALUE_KIND_NULL: PrtUserPrintString("null", buffer, bufferSize, numCharsWritten); break; case PRT_VALUE_KIND_BOOL: PrtUserPrintString(PrtPrimGetBool(value) == PRT_TRUE ? "true" : "false", buffer, bufferSize, numCharsWritten); break; case PRT_VALUE_KIND_INT: PrtUserPrintInt32(PrtPrimGetInt(value), buffer, bufferSize, numCharsWritten); break; case PRT_VALUE_KIND_EVENT: PrtUserPrintString("<", buffer, bufferSize, numCharsWritten); PrtUserPrintUint32(PrtPrimGetEvent(value), buffer, bufferSize, numCharsWritten); PrtUserPrintString(">", buffer, bufferSize, numCharsWritten); break; case PRT_VALUE_KIND_MID: PrtUserPrintMachineId(PrtPrimGetMachine(value), buffer, bufferSize, numCharsWritten); break; case PRT_VALUE_KIND_FORGN: frgnStr = prtForeignTypeDecls[value->valueUnion.frgn->typeTag].toStringFun(value->valueUnion.frgn->value); PrtUserPrintString(frgnStr, buffer, bufferSize, numCharsWritten); PrtFree(frgnStr); break; case PRT_VALUE_KIND_MAP: { PRT_MAPVALUE *mval = value->valueUnion.map; PRT_MAPNODE *next = mval->first; PrtUserPrintString("{", buffer, bufferSize, numCharsWritten); while (next != NULL) { PrtUserPrintValue(next->key, buffer, bufferSize, numCharsWritten); PrtUserPrintString(" --> ", buffer, bufferSize, numCharsWritten); PrtUserPrintValue(next->value, buffer, bufferSize, numCharsWritten); if (next->bucketNext != NULL) { PrtUserPrintString("*", buffer, bufferSize, numCharsWritten); } if (next->insertNext != NULL) { PrtUserPrintString(", ", buffer, bufferSize, numCharsWritten); } next = next->insertNext; } PrtUserPrintString("} (", buffer, bufferSize, numCharsWritten); PrtUserPrintUint32(mval->size, buffer, bufferSize, numCharsWritten); PrtUserPrintString(" / ", buffer, bufferSize, numCharsWritten); PrtUserPrintUint32(PrtMapCapacity(value), buffer, bufferSize, numCharsWritten); PrtUserPrintString(")", buffer, bufferSize, numCharsWritten); break; } case PRT_VALUE_KIND_SEQ: { PRT_UINT32 i; PRT_SEQVALUE *sVal = value->valueUnion.seq; PrtUserPrintString("[", buffer, bufferSize, numCharsWritten); for (i = 0; i < sVal->size; ++i) { PrtUserPrintValue(sVal->values[i], buffer, bufferSize, numCharsWritten); if (i < sVal->size - 1) { PrtUserPrintString(", ", buffer, bufferSize, numCharsWritten); } } PrtUserPrintString("]", buffer, bufferSize, numCharsWritten); break; } case PRT_VALUE_KIND_TUPLE: { PRT_UINT32 i; PRT_TUPVALUE *tval = value->valueUnion.tuple; PrtUserPrintString("(", buffer, bufferSize, numCharsWritten); if (tval->size == 1) { PrtUserPrintValue(tval->values[0], buffer, bufferSize, numCharsWritten); PrtUserPrintString(",)", buffer, bufferSize, numCharsWritten); } else { for (i = 0; i < tval->size; ++i) { PrtUserPrintValue(tval->values[i], buffer, bufferSize, numCharsWritten); if (i < tval->size - 1) { PrtUserPrintString(", ", buffer, bufferSize, numCharsWritten); } else { PrtUserPrintString(")", buffer, bufferSize, numCharsWritten); } } } break; } default: PrtAssert(PRT_FALSE, "PrtUserPrintValue: Invalid value"); break; } }