Esempio n. 1
0
File: dict.c Progetto: elechak/blue
static void destroy(Link self){
    Dict dict = self->value.vptr;
    if (dict){
        dictionary_free(dict->dictionary);
        mutex_free(dict->mutex);
        free(self->value.vptr);
    }
    object_destroy(self);   
}
Esempio n. 2
0
/**
  Czyszczenie pamięci słownika.
  @param[in,out] dict Słownik.
 */
static void dictionary_free(struct dictionary *dict)
{
	for(int i = 0; i < dict->children_size; i++)
	{
		dictionary_free(*(dict->children + i));
	}
	free(dict->children);
	free(dict);
}
Esempio n. 3
0
/**
 * @brief Loads a dictionary file from a zlib file handle
 *
 * @param gzf the zlib file handle to be used to read the Dictionary
 * @return the readed Dictionary object
 */
Dictionary *dictionary_load(FILE* gzf) 
{
    Dictionary *dic;
    nat_uint32_t size;
    if (gzread(gzf, &size, sizeof(nat_uint32_t)) != sizeof(nat_uint32_t)) return NULL;
    dic = dictionary_new(size);
    if (!dic) return NULL;
    if (gzread(gzf, dic->pairs, sizeof(DicPair)*MAXENTRY*(dic->size+1))	!=
	sizeof(DicPair)*MAXENTRY*(dic->size+1)) {
	dictionary_free(dic);
	return NULL;
    }
    if (gzread(gzf, dic->occurs, sizeof(nat_uint32_t)*(dic->size+1)) !=
	sizeof(nat_uint32_t)*(dic->size+1)) {
	dictionary_free(dic);
	return NULL;
    }
    return dic;
}
Esempio n. 4
0
void dictionary_remove_key(Dictionary* dict, char* key) {
	DictValue* next;
	DictValue* toRelease;
	next = dict->values;

	while (next != NULL) {
		if (strcmp(next->key, key) == 0) {
			toRelease = next;
			if (toRelease->prev) {
				toRelease->prev->next = toRelease->next;

			} else {
				dict->values = toRelease->next;
			}

			if (toRelease->next) {
				toRelease->next->prev = toRelease->prev;
			}

			switch(toRelease->type) {
				case DictionaryType:
					dictionary_free((Dictionary*) toRelease);
					break;
				case ArrayType:
					array_free((ArrayValue*) toRelease);
					break;
				case StringType:
					free(((StringValue*)(toRelease))->value);
					free(toRelease->key);
					free(toRelease);
					break;
				case DataType:
					free(((DataValue*)(toRelease))->value);
					free(toRelease->key);
					free(toRelease);
					break;	
				case IntegerType:
				case BoolType:
					free(toRelease->key);
					free(toRelease);
					break;
			}
			return;
		}
		next = next->next;
	}

	return;
}
Esempio n. 5
0
void dictionary_free(Dictionary* myself) {
	DictValue* next;
	DictValue* toRelease;
	
	free(myself->dValue.key);
	next = myself->values;
	while(next != NULL) {
		toRelease = next;
		next = next->next;

		switch(toRelease->type) {
		case DictionaryType:
			dictionary_free((Dictionary*) toRelease);
			break;

		case ArrayType:
			array_free((ArrayValue*) toRelease);
			break;

		case StringType:
			free(((StringValue*) (toRelease))->value);
			free(toRelease->key);
			free(toRelease);
			break;

		case DataType:
			free(((DataValue*) (toRelease))->value);
			free(toRelease->key);
			free(toRelease);
			break;

		case IntegerType:
		case BoolType:
			free(toRelease->key);
			free(toRelease);
			break;
		}
	}
	free(myself);
}
Esempio n. 6
0
void array_free(ArrayValue* myself) {
	int i;
	
	free(myself->dValue.key);
	for (i = 0; i < myself->size; i++) {
		switch (myself->values[i]->type) {
		case DictionaryType:
			dictionary_free((Dictionary*) myself->values[i]);
			break;

		case ArrayType:
			array_free((ArrayValue*) myself->values[i]);
			break;

		case StringType:
			free(((StringValue*) (myself->values[i]))->value);
			free(myself->values[i]->key);
			free(myself->values[i]);
			break;

		case DataType:
			free(((DataValue*) (myself->values[i]))->value);
			free(myself->values[i]->key);
			free(myself->values[i]);
			break;

		case BoolType:
		case IntegerType:
			free(myself->values[i]->key);
			free(myself->values[i]);
			break;
		}
	}
	free(myself->values);
	free(myself);
}
Esempio n. 7
0
static int save(char *outfile,
		char *lang1, char *lang2,
		char *dicfile1, nat_uint32_t *tab1,
		char *dicfile2, nat_uint32_t *tab2,
		wchar_t *string1, nat_uint32_t ptr1, NATCell *cells1, nat_uint32_t size1,
		wchar_t *string2, nat_uint32_t ptr2, NATCell *cells2, nat_uint32_t size2) {
    FILE *out = NULL;
    nat_int_t s;

    Dictionary *dic;

    out = gzopen(outfile, "wb");
    if (!out) return 0;

    // Say this is a NATDict
    gzprintf(out,"!NATDict");

    s = strlen(lang1)+1;
    gzwrite(out, &s, sizeof(nat_int_t));
    gzwrite(out, lang1, s);

    s = strlen(lang2)+1;
    gzwrite(out, &s, sizeof(nat_int_t));
    gzwrite(out, lang2, s);

    // Save first lexicon
    g_message("** Saving source Lexicon **");
    gzwrite(out, &ptr1, sizeof(nat_uint32_t));
    gzwrite(out, string1, ptr1);
    gzwrite(out, &size1, sizeof(nat_uint32_t));
    g_message("\tSize: %u", size1);
    gzwrite(out, cells1, sizeof(NATCell) * size1);

    // Save second lexicon
    g_message("** Saving target Lexicon **");
    gzwrite(out, &ptr2, sizeof(nat_uint32_t));
    gzwrite(out, string2, ptr2);
    gzwrite(out, &size2, sizeof(nat_uint32_t));
    g_message("\tSize: %u", size2);
    gzwrite(out, cells2, sizeof(NATCell)* size2);

    // Load first dictionary
    g_message("** Source -> Target dictionary **");
    g_message("\tLoading...");
    dic = dictionary_open(dicfile1);

    dictionary_remap(tab1, tab2, dic);

    g_message("\tSaving...");
    gzwrite(out, &dic->size, sizeof(nat_uint32_t));
    gzwrite(out, dic->pairs, sizeof(DicPair)*MAXENTRY*(dic->size+1));
    gzwrite(out, dic->occurs, sizeof(nat_uint32_t)*(dic->size+1));
    dictionary_free(dic);

    // Load second dictionary
    g_message("** Target -> Source dictionary **");
    g_message("\tLoading...");
    dic = dictionary_open(dicfile2);

    dictionary_remap(tab2, tab1, dic);

    g_message("\tSaving...");
    gzwrite(out, &dic->size, sizeof(nat_uint32_t));
    gzwrite(out, dic->pairs, sizeof(DicPair)*MAXENTRY*(dic->size+1));
    gzwrite(out, dic->occurs, sizeof(nat_uint32_t)*(dic->size+1));
    dictionary_free(dic);

    // Close the file
    g_message("** DONE **");
    gzclose(out);

    return 1;
}
Esempio n. 8
0
/* =============================================================================
 * main
 * =============================================================================
 */
MAIN(argc, argv)
{
    GOTO_REAL();

    /*
     * Initialization
     */

    parseArgs(argc, (char** const)argv);
    long numThread = global_params[PARAM_THREAD];
    SIM_GET_NUM_CPU(numThread);
    TM_STARTUP(numThread);
    P_MEMORY_STARTUP(numThread);
    thread_startup(numThread);

    long percentAttack = global_params[PARAM_ATTACK];
    long maxDataLength = global_params[PARAM_LENGTH];
    long numFlow       = global_params[PARAM_NUM];
    long randomSeed    = global_params[PARAM_SEED];
    printf("Percent attack  = %li\n", percentAttack);
    printf("Max data length = %li\n", maxDataLength);
    printf("Num flow        = %li\n", numFlow);
    printf("Random seed     = %li\n", randomSeed);

    dictionary_t* dictionaryPtr = dictionary_alloc();
    assert(dictionaryPtr);
    stream_t* streamPtr = stream_alloc(percentAttack);
    assert(streamPtr);
    long numAttack = stream_generate(streamPtr,
                                     dictionaryPtr,
                                     numFlow,
                                     randomSeed,
                                     maxDataLength);
    printf("Num attack      = %li\n", numAttack);

    decoder_t* decoderPtr = decoder_alloc();
    assert(decoderPtr);

    vector_t** errorVectors = (vector_t**)malloc(numThread * sizeof(vector_t*));
    assert(errorVectors);
    long i;
    for (i = 0; i < numThread; i++) {
        vector_t* errorVectorPtr = vector_alloc(numFlow);
        assert(errorVectorPtr);
        errorVectors[i] = errorVectorPtr;
    }

    arg_t arg;
    arg.streamPtr    = streamPtr;
    arg.decoderPtr   = decoderPtr;
    arg.errorVectors = errorVectors;

    /*
     * Run transactions
     */

    TIMER_T startTime;
    TIMER_READ(startTime);
    GOTO_SIM();
#ifdef OTM
#pragma omp parallel
    {
        processPackets((void*)&arg);
    }
    
#else
    thread_start(processPackets, (void*)&arg);
#endif
    GOTO_REAL();
    TIMER_T stopTime;
    TIMER_READ(stopTime);
    printf("\nTime = %lf\n", TIMER_DIFF_SECONDS(startTime, stopTime));

    /*
     * Check solution
     */

    /*long numFound = 0;
    for (i = 0; i < numThread; i++) {
        vector_t* errorVectorPtr = errorVectors[i];
        long e;
        long numError = vector_getSize(errorVectorPtr);
        numFound += numError;
        for (e = 0; e < numError; e++) {
            long flowId = (long)vector_at(errorVectorPtr, e);
            bool_t status = stream_isAttack(streamPtr, flowId);
            assert(status);
        }
    }*/
    /*printf("Num found       = %li\n", numFound);
    assert(numFound == numAttack);*/

    /*
     * Clean up
     */

    for (i = 0; i < numThread; i++) {
        vector_free(errorVectors[i]);
    }
    free(errorVectors);
    decoder_free(decoderPtr);
    stream_free(streamPtr);
    dictionary_free(dictionaryPtr);

    TM_SHUTDOWN();
    P_MEMORY_SHUTDOWN();

    GOTO_SIM();

    thread_shutdown();

    MAIN_RETURN(0);
}
Esempio n. 9
0
/* =============================================================================
 * main
 * =============================================================================
 */
MAIN(argc, argv)
{
    GOTO_REAL();

    /*
     * Initialization
     */
    SETUP_NUMBER_TASKS(3);

    parseArgs(argc, (char** const)argv);
    long numThread = global_params[PARAM_THREAD];
    SETUP_NUMBER_THREADS(numThread);
    SIM_GET_NUM_CPU(numThread);
    TM_STARTUP(numThread, 0);
    P_MEMORY_STARTUP(numThread);
    thread_startup(numThread);

    long percentAttack = global_params[PARAM_ATTACK];
    long maxDataLength = global_params[PARAM_LENGTH];
    long numFlow       = global_params[PARAM_NUM];
    long randomSeed    = global_params[PARAM_SEED];
/*    printf("Percent attack  = %li\n", percentAttack);
    printf("Max data length = %li\n", maxDataLength);
    printf("Num flow        = %li\n", numFlow);
    printf("Random seed     = %li\n", randomSeed); */

double time_total = 0.0;
int repeats = global_params[PARAM_REPEAT];
for (; repeats > 0; --repeats) {

    dictionary_t* dictionaryPtr = dictionary_alloc();
    assert(dictionaryPtr);
    stream_t* streamPtr = stream_alloc(percentAttack);
    assert(streamPtr);
    long numAttack = stream_generate(streamPtr,
                                     dictionaryPtr,
                                     numFlow,
                                     randomSeed,
                                     maxDataLength);
//    printf("Num attack      = %li\n", numAttack);

    decoder_t* decoderPtr = decoder_alloc();
    assert(decoderPtr);

    vector_t** errorVectors = (vector_t**)malloc(numThread * sizeof(vector_t*));
    assert(errorVectors);
    long i;
    for (i = 0; i < numThread; i++) {
        vector_t* errorVectorPtr = vector_alloc(numFlow);
        assert(errorVectorPtr);
        errorVectors[i] = errorVectorPtr;
    }

    arg_t arg;
    arg.streamPtr    = streamPtr;
    arg.decoderPtr   = decoderPtr;
    arg.errorVectors = errorVectors;

    /*
     * Run transactions
     */

    TIMER_T startTime;
    TIMER_READ(startTime);
    tm_time_t start_clock=TM_TIMER_READ();
    GOTO_SIM();
    thread_start(processPackets, (void*)&arg);
    GOTO_REAL();
    TIMER_T stopTime;
    tm_time_t end_clock=TM_TIMER_READ();
    TIMER_READ(stopTime);
    double time_tmp = TIMER_DIFF_SECONDS(startTime, stopTime);
	time_total += time_tmp;
    PRINT_STATS();
    PRINT_CLOCK_THROUGHPUT(end_clock-start_clock);

    /*
     * Check solution
     */

    long numFound = 0;
    for (i = 0; i < numThread; i++) {
        vector_t* errorVectorPtr = errorVectors[i];
        long e;
        long numError = vector_getSize(errorVectorPtr);
        numFound += numError;
        for (e = 0; e < numError; e++) {
            long flowId = (long)vector_at(errorVectorPtr, e);
            bool_t status = stream_isAttack(streamPtr, flowId);
            assert(status);
        }
    }
//    printf("Num found       = %li\n", numFound);
    assert(numFound == numAttack);

    /*
     * Clean up
     */

    for (i = 0; i < numThread; i++) {
        vector_free(errorVectors[i]);
    }
    free(errorVectors);
    decoder_free(decoderPtr);
    stream_free(streamPtr);
    dictionary_free(dictionaryPtr);

}

printf("Time = %f\n", time_total);

    TM_SHUTDOWN();
    P_MEMORY_SHUTDOWN();

    GOTO_SIM();

    thread_shutdown();

    MAIN_RETURN(0);
}
Esempio n. 10
0
void test_copy(dfixture *df, gconstpointer ignored){
    dictionary *cp = dictionary_copy(df->dd);
    check_keys(cp);
    dictionary_free(cp);
}
Esempio n. 11
0
void dict_teardown(dfixture *df, gconstpointer test_data){
    dictionary_free(df->dd);
}
Esempio n. 12
0
void dictionary_done(struct dictionary *dict)
{
	dictionary_free(dict);
}