/* ============================================================================= * sequencer_free * ============================================================================= */ void sequencer_free (sequencer_t* sequencerPtr) { long i; table_free(sequencerPtr->hashToConstructEntryTable); SEQ_FREE(sequencerPtr->constructEntries); for (i = 1; i < sequencerPtr->segmentLength; i++) { table_free(sequencerPtr->startHashToConstructEntryTables[i]); } SEQ_FREE(sequencerPtr->startHashToConstructEntryTables); SEQ_FREE(sequencerPtr->endInfoEntries); #if 0 /* TODO: fix mixed sequential/parallel allocation */ hashtable_free(sequencerPtr->uniqueSegmentsPtr); if (sequencerPtr->sequence != NULL) { SEQ_FREE(sequencerPtr->sequence); } #endif SEQ_FREE(sequencerPtr); }
/* ============================================================================= * net_free * ============================================================================= */ void net_free (net_t* netPtr) { long i; vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; long numNode = vector_getSize(nodeVectorPtr); for (i = 0; i < numNode; i++) { net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, i); freeNode(nodePtr); } vector_free(netPtr->nodeVectorPtr); SEQ_FREE(netPtr); }
/* ============================================================================= * allocNode * ============================================================================= */ static net_node_t* allocNode (long id) { net_node_t* nodePtr; nodePtr = (net_node_t*)SEQ_MALLOC(sizeof(net_node_t)); if (nodePtr) { nodePtr->parentIdListPtr = Plist_alloc(&net_compareid); if (nodePtr->parentIdListPtr == NULL) { SEQ_FREE(nodePtr); return NULL; } nodePtr->childIdListPtr = Plist_alloc(&net_compareid); if (nodePtr->childIdListPtr == NULL) { list_free(nodePtr->parentIdListPtr); SEQ_FREE(nodePtr); return NULL; } nodePtr->id = id; } return nodePtr; }
/* ============================================================================= * table_free * ============================================================================= */ void table_free (table_t* tablePtr) { #if 0 /* TODO: fix mixed sequential/parallel allocation */ long i; for (i = 0; i < tablePtr->numBucket; i++) { list_free(tablePtr->buckets[i]); } #endif SEQ_FREE(tablePtr); }
void StartTrader (void) { TradedContextMod_clp tcmod; Security_Tag systag; ContextMod_clp cmod; IDCOffer_clp trader_offer; TradedContext_clp trader; Security_CertSeq *null_seq; Context_clp sys; int i; TRC(printf("Trader: starting:\n")); cmod = NAME_FIND ("modules>ContextMod", ContextMod_clp); tcmod = NAME_FIND ("modules>TradedContextMod", TradedContextMod_clp); systag = NAME_FIND ("sys>SystemTag", Security_Tag); null_seq=SEQ_CLEAR(SEQ_NEW(Security_CertSeq,0,Pvs(heap))); /* Create the trader context */ TRC(printf("Trader: creating main trader context\n")); trader_offer = TradedContextMod$New(tcmod, cmod, Pvs(heap), Pvs(types), Pvs(entry), systag); TRC(printf("Trader: binding to main trader context\n")); trader = IDC_BIND(trader_offer, TradedContext_clp); for (i=0; i<NUM_TRADED_CONTEXTS; i++) { IDCOffer_clp tmp; TRC(printf("Trader: create >%s\n", traded_contexts[i])); tmp = TradedContextMod$New(tcmod, cmod, Pvs(heap), Pvs(types), Pvs(entry), systag); TradedContext$AddTradedContext (trader, traded_contexts[i], tmp, systag, null_seq); } /* Export an offer for the trader at "sys>TraderOffer" in root */ TRC(printf ("Trader_Init: exporting... ")); sys=NAME_FIND("sys>StdRoot>sys",Context_clp); CX_ADD_IN_CX(sys, "TraderOffer", trader_offer, IDCOffer_clp); SEQ_FREE(null_seq); TRC(printf("StartTrader: done\n")); }
/* ============================================================================= * decoder_getComplete * -- If none, returns NULL * ============================================================================= */ char* decoder_getComplete (decoder_t* decoderPtr, long* decodedFlowIdPtr) { char* data; decoded_t* decodedPtr = (decoded_t*)queue_pop(decoderPtr->decodedQueuePtr); if (decodedPtr) { *decodedFlowIdPtr = decodedPtr->flowId; data = decodedPtr->data; SEQ_FREE(decodedPtr); } else { *decodedFlowIdPtr = -1; data = NULL; } return data; }
/* ============================================================================= * data_copy * -- Returns false on failure * ============================================================================= */ bool data_copy (data_t* dstPtr, data_t* srcPtr) { long numDstDatum = dstPtr->numVar * dstPtr->numRecord; long numSrcDatum = srcPtr->numVar * srcPtr->numRecord; if (numDstDatum != numSrcDatum) { SEQ_FREE(dstPtr->records); dstPtr->records = (char*)calloc(numSrcDatum, sizeof(char)); if (dstPtr->records == NULL) { return false; } } dstPtr->numVar = srcPtr->numVar; dstPtr->numRecord = srcPtr->numRecord; memcpy(dstPtr->records, srcPtr->records, (numSrcDatum * sizeof(char))); return true; }
/* ============================================================================= * allocNode * ============================================================================= */ adtree_node_t* allocNode (long index) { adtree_node_t* nodePtr; nodePtr = (adtree_node_t*)SEQ_MALLOC(sizeof(adtree_node_t)); if (nodePtr) { nodePtr->varyVectorPtr = vector_alloc(1); if (nodePtr->varyVectorPtr == NULL) { SEQ_FREE(nodePtr); return NULL; } nodePtr->index = index; nodePtr->value = -1; nodePtr->count = -1; } return nodePtr; }
/* ============================================================================= * data_alloc * ============================================================================= */ data_t* data_alloc (long numVar, long numRecord, random_t* randomPtr) { data_t* dataPtr; dataPtr = (data_t*)SEQ_MALLOC(sizeof(data_t)); if (dataPtr) { long numDatum = numVar * numRecord; dataPtr->records = (char*)SEQ_MALLOC(numDatum * sizeof(char)); if (dataPtr->records == NULL) { SEQ_FREE(dataPtr); return NULL; } memset(dataPtr->records, DATA_INIT, (numDatum * sizeof(char))); dataPtr->numVar = numVar; dataPtr->numRecord = numRecord; dataPtr->randomPtr = randomPtr; } return dataPtr; }
hashtable_t* hashtable_alloc (long initNumBucket, ulong_t (*hash)(const void*), comparator_t* comparePairs, long resizeRatio, long growthFactor) { if(comparePairs==NULL){ comparePairs = &default_hashtable_comparator; } hashtable_t* hashtablePtr; resizeRatio = -1; growthFactor = -1; hashtablePtr = (hashtable_t*)SEQ_MALLOC(sizeof(hashtable_t)); if (hashtablePtr == NULL) { return NULL; } if(hash==NULL){ hash = &func_hash; } hashtablePtr->buckets = allocBuckets(initNumBucket, comparePairs); if (hashtablePtr->buckets == NULL) { SEQ_FREE(hashtablePtr); return NULL; } hashtablePtr->numBucket = initNumBucket; #ifdef HASHTABLE_SIZE_FIELD hashtablePtr->size = 0; #endif hashtablePtr->hash = hash; hashtablePtr->comparePairs = comparePairs; hashtablePtr->resizeRatio = ((resizeRatio < 0) ? HASHTABLE_DEFAULT_RESIZE_RATIO : resizeRatio); hashtablePtr->growthFactor = ((growthFactor < 0) ? HASHTABLE_DEFAULT_GROWTH_FACTOR : growthFactor); return hashtablePtr; }
/* ============================================================================= * freeNode * ============================================================================= */ void freeNode (adtree_node_t* nodePtr) { vector_free(nodePtr->varyVectorPtr); SEQ_FREE(nodePtr); }
/* ============================================================================= * freeVary * ============================================================================= */ void freeVary (adtree_vary_t* varyPtr) { SEQ_FREE(varyPtr); }
/* ============================================================================= * adtree_free * ============================================================================= */ void adtree_free (adtree_t* adtreePtr) { freeNodes(adtreePtr->rootNodePtr); SEQ_FREE(adtreePtr); }
int main () { decoder_t* decoderPtr; puts("Starting..."); decoderPtr = decoder_alloc(); assert(decoderPtr); long numDataByte = 3; long numPacketByte = PACKET_HEADER_LENGTH + numDataByte; char* abcBytes = (char*)SEQ_MALLOC(numPacketByte); assert(abcBytes); packet_t* abcPacketPtr; abcPacketPtr = (packet_t*)abcBytes; abcPacketPtr->flowId = 1; abcPacketPtr->fragmentId = 0; abcPacketPtr->numFragment = 2; abcPacketPtr->length = numDataByte; abcPacketPtr->data[0] = 'a'; abcPacketPtr->data[1] = 'b'; abcPacketPtr->data[2] = 'c'; char* defBytes = (char*)SEQ_MALLOC(numPacketByte); assert(defBytes); packet_t* defPacketPtr; defPacketPtr = (packet_t*)defBytes; defPacketPtr->flowId = 1; defPacketPtr->fragmentId = 1; defPacketPtr->numFragment = 2; defPacketPtr->length = numDataByte; defPacketPtr->data[0] = 'd'; defPacketPtr->data[1] = 'e'; defPacketPtr->data[2] = 'f'; assert(decoder_process(decoderPtr, abcBytes, numDataByte) == ERROR_SHORT); abcPacketPtr->flowId = -1; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_FLOWID); abcPacketPtr->flowId = 1; abcPacketPtr->fragmentId = -1; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_FRAGMENTID); abcPacketPtr->fragmentId = 0; abcPacketPtr->fragmentId = 2; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_FRAGMENTID); abcPacketPtr->fragmentId = 0; abcPacketPtr->fragmentId = 2; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_FRAGMENTID); abcPacketPtr->fragmentId = 0; abcPacketPtr->length = -1; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_LENGTH); abcPacketPtr->length = numDataByte; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_NONE); defPacketPtr->numFragment = 3; assert(decoder_process(decoderPtr, defBytes, numPacketByte) == ERROR_NUMFRAGMENT); defPacketPtr->numFragment = 2; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_NONE); defPacketPtr->fragmentId = 0; assert(decoder_process(decoderPtr, defBytes, numPacketByte) == ERROR_INCOMPLETE); defPacketPtr->fragmentId = 1; long flowId; assert(decoder_process(decoderPtr, defBytes, numPacketByte) == ERROR_NONE); assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_NONE); char* str = decoder_getComplete(decoderPtr, &flowId); assert(strcmp(str, "abcdef") == 0); SEQ_FREE(str); assert(flowId == 1); abcPacketPtr->numFragment = 1; assert(decoder_process(decoderPtr, abcBytes, numPacketByte) == ERROR_NONE); str = decoder_getComplete(decoderPtr, &flowId); assert(strcmp(str, "abc") == 0); SEQ_FREE(str); abcPacketPtr->numFragment = 2; assert(flowId == 1); str = decoder_getComplete(decoderPtr, &flowId); assert(str == NULL); assert(flowId == -1); decoder_free(decoderPtr); SEQ_FREE(abcBytes); SEQ_FREE(defBytes); puts("All tests passed."); return 0; }
/* ============================================================================= * main * ============================================================================= */ MAIN(argc, argv) { int max_nclusters = 13; int min_nclusters = 4; char* filename = 0; float* buf; float** attributes; float** cluster_centres = NULL; int i; int j; int best_nclusters; int* cluster_assign; int numAttributes; int numObjects; int use_zscore_transform = 1; char* line; int isBinaryFile = 0; int nloops; int len; int nthreads; float threshold = 0.001; int opt; nthreads = 1; while ((opt = getopt(argc,(char**)argv,"p:i:m:n:t:bz")) != EOF) { switch (opt) { case 'i': filename = optarg; break; case 'b': isBinaryFile = 1; break; case 't': threshold = atof(optarg); break; case 'm': max_nclusters = atoi(optarg); break; case 'n': min_nclusters = atoi(optarg); break; case 'z': use_zscore_transform = 0; break; case 'p': nthreads = atoi(optarg); break; case '?': usage((char*)argv[0]); break; default: usage((char*)argv[0]); break; } } // [RSTM] moved this allocation so that we only allocate after // an MMPolicy has been created line = (char*)SEQ_MALLOC(MAX_LINE_LENGTH); /* reserve memory line */ if (filename == 0) { usage((char*)argv[0]); } if (max_nclusters < min_nclusters) { fprintf(stderr, "Error: max_clusters must be >= min_clusters\n"); usage((char*)argv[0]); } SIM_GET_NUM_CPU(nthreads); numAttributes = 0; numObjects = 0; /* * From the input file, get the numAttributes and numObjects */ if (isBinaryFile) { int infile; if ((infile = open(filename, O_RDONLY, "0600")) == -1) { fprintf(stderr, "Error: no such file (%s)\n", filename); exit(1); } read(infile, &numObjects, sizeof(int)); read(infile, &numAttributes, sizeof(int)); /* Allocate space for attributes[] and read attributes of all objects */ buf = (float*)SEQ_MALLOC(numObjects * numAttributes * sizeof(float)); assert(buf); attributes = (float**)SEQ_MALLOC(numObjects * sizeof(float*)); assert(attributes); attributes[0] = (float*)SEQ_MALLOC(numObjects * numAttributes * sizeof(float)); assert(attributes[0]); for (i = 1; i < numObjects; i++) { attributes[i] = attributes[i-1] + numAttributes; } read(infile, buf, (numObjects * numAttributes * sizeof(float))); close(infile); } else { FILE *infile; if ((infile = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error: no such file (%s)\n", filename); exit(1); } while (fgets(line, MAX_LINE_LENGTH, infile) != NULL) { if (strtok(line, " \t\n") != 0) { numObjects++; } } rewind(infile); while (fgets(line, MAX_LINE_LENGTH, infile) != NULL) { if (strtok(line, " \t\n") != 0) { /* Ignore the id (first attribute): numAttributes = 1; */ while (strtok(NULL, " ,\t\n") != NULL) { numAttributes++; } break; } } /* Allocate space for attributes[] and read attributes of all objects */ buf = (float*)SEQ_MALLOC(numObjects * numAttributes * sizeof(float)); assert(buf); attributes = (float**)SEQ_MALLOC(numObjects * sizeof(float*)); assert(attributes); attributes[0] = (float*)SEQ_MALLOC(numObjects * numAttributes * sizeof(float)); assert(attributes[0]); for (i = 1; i < numObjects; i++) { attributes[i] = attributes[i-1] + numAttributes; } rewind(infile); i = 0; while (fgets(line, MAX_LINE_LENGTH, infile) != NULL) { if (strtok(line, " \t\n") == NULL) { continue; } for (j = 0; j < numAttributes; j++) { buf[i] = atof(strtok(NULL, " ,\t\n")); i++; } } fclose(infile); } TM_STARTUP(nthreads); thread_startup(nthreads); /* * The core of the clustering */ cluster_assign = (int*)SEQ_MALLOC(numObjects * sizeof(int)); assert(cluster_assign); nloops = 1; len = max_nclusters - min_nclusters + 1; for (i = 0; i < nloops; i++) { /* * Since zscore transform may perform in cluster() which modifies the * contents of attributes[][], we need to re-store the originals */ memcpy(attributes[0], buf, (numObjects * numAttributes * sizeof(float))); cluster_centres = NULL; cluster_exec(nthreads, numObjects, numAttributes, attributes, /* [numObjects][numAttributes] */ use_zscore_transform, /* 0 or 1 */ min_nclusters, /* pre-define range from min to max */ max_nclusters, threshold, &best_nclusters, /* return: number between min and max */ &cluster_centres, /* return: [best_nclusters][numAttributes] */ cluster_assign); /* return: [numObjects] cluster id for each object */ } #ifdef GNUPLOT_OUTPUT { FILE** fptr; char outFileName[1024]; fptr = (FILE**)SEQ_MALLOC(best_nclusters * sizeof(FILE*)); for (i = 0; i < best_nclusters; i++) { sprintf(outFileName, "group.%d", i); fptr[i] = fopen(outFileName, "w"); } for (i = 0; i < numObjects; i++) { fprintf(fptr[cluster_assign[i]], "%6.4f %6.4f\n", attributes[i][0], attributes[i][1]); } for (i = 0; i < best_nclusters; i++) { fclose(fptr[i]); } SEQ_FREE(fptr); } #endif /* GNUPLOT_OUTPUT */ #ifdef OUTPUT_TO_FILE { /* Output: the coordinates of the cluster centres */ FILE* cluster_centre_file; FILE* clustering_file; char outFileName[1024]; sprintf(outFileName, "%s.cluster_centres", filename); cluster_centre_file = fopen(outFileName, "w"); for (i = 0; i < best_nclusters; i++) { fprintf(cluster_centre_file, "%d ", i); for (j = 0; j < numAttributes; j++) { fprintf(cluster_centre_file, "%f ", cluster_centres[i][j]); } fprintf(cluster_centre_file, "\n"); } fclose(cluster_centre_file); /* Output: the closest cluster centre to each of the data points */ sprintf(outFileName, "%s.cluster_assign", filename); clustering_file = fopen(outFileName, "w"); for (i = 0; i < numObjects; i++) { fprintf(clustering_file, "%d %d\n", i, cluster_assign[i]); } fclose(clustering_file); } #endif /* OUTPUT TO_FILE */ #ifdef OUTPUT_TO_STDOUT { /* Output: the coordinates of the cluster centres */ for (i = 0; i < best_nclusters; i++) { printf("%d ", i); for (j = 0; j < numAttributes; j++) { printf("%f ", cluster_centres[i][j]); } printf("\n"); } } #endif /* OUTPUT TO_STDOUT */ printf("Time: %lg seconds\n", global_time); SEQ_FREE(cluster_assign); SEQ_FREE(attributes); SEQ_FREE(cluster_centres[0]); SEQ_FREE(cluster_centres); SEQ_FREE(buf); TM_SHUTDOWN(); thread_shutdown(); MAIN_RETURN(0); }
/* ============================================================================= * list_free * ============================================================================= */ void list_free (list_t* listPtr) { freeList(listPtr->head.nextPtr); SEQ_FREE(listPtr); }
/* ============================================================================= * initializeManager * ============================================================================= */ static manager_t* initializeManager () { manager_t* managerPtr; long i; long numRelation; random_t* randomPtr; long* ids; bool (*manager_add[])( manager_t*, long, long, long) = { &manager_addCar_seq, &manager_addFlight_seq, &manager_addRoom_seq, &addCustomer }; long t; long numTable = sizeof(manager_add) / sizeof(manager_add[0]); printf("Initializing manager... "); fflush(stdout); randomPtr = random_alloc(); assert(randomPtr != NULL); managerPtr = manager_alloc(); assert(managerPtr != NULL); numRelation = (long)global_params[PARAM_RELATIONS]; ids = (long*)SEQ_MALLOC(numRelation * sizeof(long)); for (i = 0; i < numRelation; i++) { ids[i] = i + 1; } for (t = 0; t < numTable; t++) { /* Shuffle ids */ for (i = 0; i < numRelation; i++) { long x = random_generate(randomPtr) % numRelation; long y = random_generate(randomPtr) % numRelation; long tmp = ids[x]; ids[x] = ids[y]; ids[y] = tmp; } /* Populate table */ for (i = 0; i < numRelation; i++) { bool status; long id = ids[i]; long num = ((random_generate(randomPtr) % 5) + 1) * 100; long price = ((random_generate(randomPtr) % 5) * 10) + 50; status = manager_add[t](managerPtr, id, num, price); assert(status); } } /* for t */ puts("done."); fflush(stdout); random_free(randomPtr); SEQ_FREE(ids); return managerPtr; }
/* ============================================================================= * hashtable_free * ============================================================================= */ void hashtable_free (hashtable_t* hashtablePtr) { freeBuckets(hashtablePtr->buckets, hashtablePtr->numBucket); SEQ_FREE(hashtablePtr); }
/* ============================================================================= * coordinate_free * ============================================================================= */ void coordinate_free (coordinate_t* coordinatePtr) { SEQ_FREE(coordinatePtr); }
/* ============================================================================= * data_free * ============================================================================= */ void data_free (data_t* dataPtr) { SEQ_FREE(dataPtr->records); SEQ_FREE(dataPtr); }
/* ** The Nemesis domain is special - it gets passed ** only half a comment ** and its Pvs are special. */ void Go(Activation_cl *self, VP_clp vp /* IN */, Activation_Reason ar /* IN */ ) { kernel_st *kst = (kernel_st *)self->st; dcb_rw_t *rwp = vp->st; dcb_ro_t *rop = DCB_RW2RO(rwp); ActivationF_cl *actf; TRY { #ifdef __IX86__ ntsc_entkern(); /* There is an NTSC hack that prevents this from being undone. */ #endif /* __IX86__ */ /* Direct all output from Nemesis domain through NTSC (or, on Alpha, direct to the hardware) */ Pvs(err)=Pvs(out)=Pvs(console)=&triv_wr_cl; TRC(printf("Nemesis domain entered.\n")); TRC(printf(" + DCBRO at %p\n", rop)); TRC(printf(" psmask=%qx\n", rop->psmask)); #ifdef __ALPHA__ TRC(printf(" ps=%qx\n", ntsc_rdps())); #endif TRC(printf(" + DCBRW at %p\n", rwp)); TRC(printf(" + Activation clp at %p\n", self)); TRC(printf(" + VP clp at %p\n", vp)); TRC(printf(" + activationsOn=%d\n", VP$ActivationMode(vp))); TRC(printf(" + Activation Reason %d\n", ar)); TRC(printf(" + PVS = %p\n", PVS())); TRC(DUMP_PVS()); { StretchAllocator_clp salloc; StretchAllocator_SizeSeq *sizes; StretchAllocator_StretchSeq *stretches; ThreadsPackage_Stack protoStack; Stretch_clp userStretch; BootDomain_Info *nem_info; ThreadsPackage_clp tp; ThreadF_clp thdf; #ifdef CONFIG_MEMSYS_EXPT SDriverMod_cl *sdmod; StretchTbl_cl *strtab; #define MAX_DESCS 5 Mem_PMemDesc pmem[MAX_DESCS + 1]; Type_Any fany; flink_t *lk; flist_t *cur; int i = 0; /* Create a new stretch driver (physical one for now) */ TRC(printf("Creating initial stretch driver and stretch tab.\n")); sdmod = NAME_FIND("modules>SDriverMod", SDriverMod_clp); strtab = NAME_FIND("sys>StretchTable", StretchTbl_clp); /* Grab the pmem which has been allocated for us by dmgr */ for(lk = rop->finfo.next; lk != &rop->finfo; lk = lk->next) { cur = (flist_t *)lk; pmem[i].start_addr = cur->base; pmem[i].frame_width = cur->fwidth; pmem[i].nframes = cur->npf >> (cur->fwidth - FRAME_WIDTH); pmem[i].attr = 0; if(++i == MAX_DESCS) break; } pmem[i].nframes = 0; /* terminate array */ ANY_INIT(&fany, Frames_clp, rop->frames); Pvs(sdriver) = SDriverMod$NewPhysical(sdmod, vp, Pvs(heap), strtab, pmem, &fany); #endif /* Add our personal frames closure into the sys context */ /* XXX SDE: I'm not convinced that this is a good idea; the Nemesis domain (and all other domains) should have a bit of private namespace for this. I was bitten once by a domain using the Nemesis domain's Frames closure directly because its own hadn't overridden it in the namespace. */ CX_ADD("sys>Frames", rop->frames, Frames_clp); TRC(eprintf (" + Finding Nemesis StretchAllocator\n")); salloc = NAME_FIND("sys>StretchAllocator", StretchAllocator_clp); TRC(eprintf (" + StretchAlloc = %p\n", salloc)); TRC(eprintf (" + Finding parameters\n")); nem_info= NAME_FIND("progs>Nemesis", BootDomain_InfoP); sizes = SEQ_NEW(StretchAllocator_SizeSeq, 3, Pvs(heap)); SEQ_ELEM(sizes, 0) = FRAME_SIZE; /* for guard page */ SEQ_ELEM(sizes, 1) = nem_info->stackWords*sizeof(word_t); SEQ_ELEM(sizes, 2) = nem_info->pHeapWords*sizeof(word_t); TRC(eprintf (" + Allocating Stretches\n")); stretches = STR_NEWLIST_SALLOC(salloc, sizes); protoStack.guard = SEQ_ELEM(stretches, 0); protoStack.stretch = SEQ_ELEM(stretches, 1); userStretch = SEQ_ELEM(stretches, 2); TRC(eprintf (" + Freeing SEQs\n")); SEQ_FREE(sizes); SEQ_FREE(stretches); #ifdef CONFIG_MEMSYS_EXPT /* Bind and map the stack */ { Stretch_Size sz; addr_t stackva; int npages; TRC(printf("+ Binding and mapping stack.\n")); stackva = STR_RANGE(protoStack.stretch, &sz); npages = sz >> PAGE_WIDTH; while(npages--) { StretchDriver$Map(Pvs(sdriver), protoStack.stretch, stackva); stackva += PAGE_SIZE; } } #endif TRC(printf(" + Setting protection\n")); SALLOC_SETGLOBAL(salloc, protoStack.guard, 0); /* XXX PRB Global write is a bit dodgy to say the least! */ SALLOC_SETGLOBAL(salloc, protoStack.stretch, SET_ELEM(Stretch_Right_Read) | SET_ELEM(Stretch_Right_Write) ); SALLOC_SETGLOBAL(salloc, userStretch, SET_ELEM(Stretch_Right_Read) | SET_ELEM(Stretch_Right_Write) ); TRC(printf(" + Finding ThreadsPackage\n")); tp = NAME_FIND("modules>NonPreemptiveThreads",ThreadsPackage_clp); TRC(printf(" + ThreadsPackage = %p\n", tp)); if(!(thdf = ThreadsPackage$New(tp, (addr_t)&NemesisMainThread, (addr_t)kst, &protoStack, userStretch, nem_info->stackWords*sizeof(word_t), (Pervasives_Init *)PVS(), &actf))) { TRC(printf("Nemesis: can't get threads.\n")); ntsc_halt(); } /* Set ourselves up to handle memory faults */ /* first get/set an event channel for fault notification */ rwp->mm_ep = VP$AllocChannel(Pvs(vp)); #if defined(CONFIG_MEMSYS_STD) || defined(CONFIG_MEMSYS_EXPT) { MMNotifyMod_cl *mmnmod; MMNotify_cl *mmnfy; StretchTbl_clp strtable; #ifdef CONFIG_MEMSYS_EXPT /* get the stretch table */ TRC(eprintf (" + Finding StretchTable\n")); strtable = NAME_FIND("sys>StretchTable", StretchTbl_clp); #else strtable = (StretchTbl_cl *)NULL; #endif mmnmod = NAME_FIND("modules>MMNotifyMod", MMNotifyMod_clp); mmnfy = MMNotifyMod$New(mmnmod, vp, Pvs(heap), strtable); CX_ADD("sys>MMNotify", mmnfy, MMNotify_clp); ActivationF$Attach(actf, (ChannelNotify_cl *)mmnfy, rwp->mm_ep); } #endif /* for non expt, we do all this with a mmentry in main thd */ } TRC(printf(" + yielding to main thread.\n")); VP$ActivationsOn(Pvs(vp)); VP$Yield(Pvs(vp)); } CATCH_Context$NotFound(name) { printf("Caught execption Context$NotFound(%s)\n", name); } CATCH_ALL { printf("Caught exception %s\n", exn_ctx.cur_exception); } ENDTRY; /* XXX we are not happy here; for now, just halt. */ printf("\nNemesis: Much bogosity!!! halting.\n"); ntsc_dbgstop(); }
/* ============================================================================= * router_free * ============================================================================= */ void router_free (router_t* routerPtr) { SEQ_FREE(routerPtr); }
/* ============================================================================= * rbtree_free * ============================================================================= */ void rbtree_free (rbtree_t* r) { freeNode(r->root); SEQ_FREE(r); }
/* ============================================================================= * grid_free * ============================================================================= */ void grid_free (grid_t* gridPtr) { SEQ_FREE(gridPtr->points_unaligned); SEQ_FREE(gridPtr); }
/* ============================================================================= * random_free * ============================================================================= */ void random_free (random_t* randomPtr) { SEQ_FREE(randomPtr); }
/* ============================================================================= * stream_generate * -- Returns number of attacks generated * ============================================================================= */ long stream_generate (stream_t* streamPtr, dictionary_t* dictionaryPtr, long numFlow, long seed, long maxLength) { long numAttack = 0; long percentAttack = streamPtr->percentAttack; random_t* randomPtr = streamPtr->randomPtr; vector_t* allocVectorPtr = streamPtr->allocVectorPtr; queue_t* packetQueuePtr = streamPtr->packetQueuePtr; MAP_T* attackMapPtr = streamPtr->attackMapPtr; detector_t* detectorPtr = detector_alloc(); assert(detectorPtr); detector_addPreprocessor(detectorPtr, &preprocessor_toLower); random_seed(randomPtr, seed); queue_clear(packetQueuePtr); long range = '~' - ' ' + 1; assert(range > 0); printf("stream generate\n"); long f; for (f = 1; f <= numFlow; f++) { char* str; if ((long)(random_generate(randomPtr) % 100) < percentAttack) { long s = random_generate(randomPtr) % global_numDefaultSignature; str = dictionary_get(dictionaryPtr, s); bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); assert(status); numAttack++; } else { /* * Create random string */ long length = (random_generate(randomPtr) % maxLength) + 1; str = (char*)SEQ_MALLOC((length + 1) * sizeof(char)); bool_t status = vector_pushBack(allocVectorPtr, (void*)str); assert(status); long l; for (l = 0; l < length; l++) { str[l] = ' ' + (char)(random_generate(randomPtr) % range); } const char *s; str[l] = '\0'; char* str2 = (char*)SEQ_MALLOC((length + 1) * sizeof(char)); assert(str2); strcpy(str2, str); int_error_t error = detector_process(detectorPtr, str2); /* updates in-place */ if (error == ERROR_SIGNATURE) { bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); assert(status); numAttack++; } SEQ_FREE(str2); } splitIntoPackets(str, f, randomPtr, allocVectorPtr, packetQueuePtr); } queue_shuffle(packetQueuePtr, randomPtr); detector_free(detectorPtr); return numAttack; }
/* ============================================================================= * data_generate * -- Binary variables of random PDFs * -- If seed is <0, do not reseed * -- Returns random network * ============================================================================= */ net_t* data_generate (data_t* dataPtr, long seed, long maxNumParent, long percentParent) { random_t* randomPtr = dataPtr->randomPtr; if (seed >= 0) { random_seed(randomPtr, seed); } /* * Generate random Bayesian network */ long numVar = dataPtr->numVar; net_t* netPtr = net_alloc(numVar); assert(netPtr); net_generateRandomEdges(netPtr, maxNumParent, percentParent, randomPtr); /* * Create a threshold for each of the possible permutation of variable * value instances */ long** thresholdsTable = (long**)SEQ_MALLOC(numVar * sizeof(long*)); assert(thresholdsTable); long v; for (v = 0; v < numVar; v++) { list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long numThreshold = 1 << list_getSize(parentIdListPtr); long* thresholds = (long*)SEQ_MALLOC(numThreshold * sizeof(long)); assert(thresholds); long t; for (t = 0; t < numThreshold; t++) { long threshold = random_generate(randomPtr) % (DATA_PRECISION + 1); thresholds[t] = threshold; } thresholdsTable[v] = thresholds; } /* * Create variable dependency ordering for record generation */ long* order = (long*)SEQ_MALLOC(numVar * sizeof(long)); assert(order); long numOrder = 0; queue_t* workQueuePtr = queue_alloc(-1); assert(workQueuePtr); vector_t* dependencyVectorPtr = vector_alloc(1); assert(dependencyVectorPtr); bitmap_t* orderedBitmapPtr = bitmap_alloc(numVar); assert(orderedBitmapPtr); bitmap_clearAll(orderedBitmapPtr); bitmap_t* doneBitmapPtr = bitmap_alloc(numVar); assert(doneBitmapPtr); bitmap_clearAll(doneBitmapPtr); v = -1; while ((v = bitmap_findClear(doneBitmapPtr, (v + 1))) >= 0) { list_t* childIdListPtr = net_getChildIdListPtr(netPtr, v); long numChild = list_getSize(childIdListPtr); if (numChild == 0) { bool status; /* * Use breadth-first search to find net connected to this leaf */ queue_clear(workQueuePtr); status = queue_push(workQueuePtr, (void*)v); assert(status); while (!queue_isEmpty(workQueuePtr)) { long id = (long)queue_pop(workQueuePtr); status = bitmap_set(doneBitmapPtr, id); assert(status); status = vector_pushBack(dependencyVectorPtr, (void*)id); assert(status); list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id); list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); status = queue_push(workQueuePtr, (void*)parentId); assert(status); } } /* * Create ordering */ long i; long n = vector_getSize(dependencyVectorPtr); for (i = 0; i < n; i++) { long id = (long)vector_popBack(dependencyVectorPtr); if (!bitmap_isSet(orderedBitmapPtr, id)) { bitmap_set(orderedBitmapPtr, id); order[numOrder++] = id; } } } } assert(numOrder == numVar); /* * Create records */ char* record = dataPtr->records; long r; long numRecord = dataPtr->numRecord; for (r = 0; r < numRecord; r++) { long o; for (o = 0; o < numOrder; o++) { long v = order[o]; list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long index = 0; list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); long value = record[parentId]; assert(value != DATA_INIT); index = (index << 1) + value; } long rnd = random_generate(randomPtr) % DATA_PRECISION; long threshold = thresholdsTable[v][index]; record[v] = ((rnd < threshold) ? 1 : 0); } record += numVar; assert(record <= (dataPtr->records + numRecord * numVar)); } /* * Clean up */ bitmap_free(doneBitmapPtr); bitmap_free(orderedBitmapPtr); vector_free(dependencyVectorPtr); queue_free(workQueuePtr); SEQ_FREE(order); for (v = 0; v < numVar; v++) { SEQ_FREE(thresholdsTable[v]); } SEQ_FREE(thresholdsTable); return netPtr; }
/* ============================================================================= * element_free * ============================================================================= */ void element_free (element_t* elementPtr) { list_free(elementPtr->neighborListPtr); SEQ_FREE(elementPtr); }
/* ============================================================================= * freeNode * ============================================================================= */ static void freeNode (list_node_t* nodePtr) { SEQ_FREE(nodePtr); }
/* ============================================================================= * heap_free * ============================================================================= */ void heap_free (heap_t* heapPtr) { SEQ_FREE(heapPtr->elements); SEQ_FREE(heapPtr); }