Exemplo n.º 1
0
/*
 * Pak_ReadPakfile
 *
 * Return a populated Pakfile from the specified path, with entries
 * hashed by name for fast finds.
 */
pak_t *Pak_ReadPakfile(const char *pakfile) {
	pak_header_t header;
	int i;
	pak_t *pak;

	pak = (pak_t *) Z_Malloc(sizeof(*pak));

	pak->handle = fopen(pakfile, "rb");
	if (!pak->handle) {
		Com_Warn("Pak_ReadPakfile: Couldn't open %s.\n", pakfile);
		Z_Free(pak);
		return NULL;
	}

	strcpy(pak->file_name, pakfile);

	Fs_Read(&header, 1, sizeof(pak_header_t), pak->handle);
	if (LittleLong(header.ident) != PAK_HEADER) {
		Com_Warn("Pak_ReadPakfile: %s is not a pak file.\n", pakfile);
		Fs_CloseFile(pak->handle);
		Z_Free(pak);
		return NULL;
	}

	header.dir_ofs = LittleLong(header.dir_ofs);
	header.dir_len = LittleLong(header.dir_len);

	pak->num_entries = header.dir_len / sizeof(pak_entry_t);
	if (pak->num_entries > MAX_PAK_ENTRIES) {
		Com_Warn("Pak_ReadPakfile: %s has %i files.\n", pakfile,
				pak->num_entries);
		Fs_CloseFile(pak->handle);
		Z_Free(pak);
		return NULL;
	}

	pak->entries = (pak_entry_t *) Z_Malloc(
			pak->num_entries * sizeof(pak_entry_t));

	fseek(pak->handle, header.dir_ofs, SEEK_SET);
	Fs_Read(pak->entries, 1, header.dir_len, pak->handle);

	Hash_Init(&pak->hash_table);

	// parse the directory
	for (i = 0; i < pak->num_entries; ++i) {
		pak->entries[i].file_ofs = LittleLong(pak->entries[i].file_ofs);
		pak->entries[i].file_len = LittleLong(pak->entries[i].file_len);

		Hash_Put(&pak->hash_table, pak->entries[i].name, &pak->entries[i]);
	}

	return pak;
}
Exemplo n.º 2
0
void
InitDebugMem()
{
#ifdef TESTING
    static int	initialized = 0;

    MASTER_LOCK(&_debugMemMutex);
    if (initialized == 0) {
	initialized = 1;
        MASTER_UNLOCK(&_debugMemMutex);
        Hash_Init(&_debugMemTable, 1000, 1);
    } else {
	MASTER_UNLOCK(&_debugMemMutex);
    }
#endif TESTING
}
Exemplo n.º 3
0
int main()
{
  printf("\n   Start testing, please wait...\n");
  int i;
  for(i = 0; i < NUM_OF_LOOPS; i++) {
    // Use a constant seed so that the test uses the same number of threads
    // and operations for everybody.
    srand(i);
    Hash_Init(&h, rand() % ADDITIONAL_NUM_OF_BUCKETS + MIN_NUM_OF_BUCKETS);
    start_threads(i);
  }
  // If the program got here without failing and quitting, 
  // then the test passed.
  printf("\n  Test succeeded \n");
  printf("    Congrats! you are an expert! ... \n");
  return 0;
}
Exemplo n.º 4
0
int main()
{
    pthread_t thread[3];
    counter_t counter;

    printf("Testing Counter...\n");

    printf("Begin single thread test\n");

    printf("Counter Init\n");
    Counter_Init(&counter, 0);

    printf("Counter_GetValue\n");
    int counter_val = Counter_GetValue(&counter);
    printf("expected 0 got %d\n", counter_val);

    printf("Counter_Increment\n");
    Counter_Increment(&counter);
    counter_val = counter_val = Counter_GetValue(&counter);
    printf("expected 1 got %d\n", counter_val);

    printf("Counter_Decrement\n");
    Counter_Decrement(&counter);
    counter_val = Counter_GetValue(&counter);
    printf("expected 0 got %d\n", counter_val);

    printf("Begin multithreaded test\n");

    printf("Counter Init\n");
    Counter_Init(&counter, 0);

    pthread_create(&thread[0], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 1 increment call\n", Counter_GetValue(&counter));
    pthread_create(&thread[1], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 2 increment calls\n", Counter_GetValue(&counter));
    pthread_create(&thread[2], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 3 increment calls\n", Counter_GetValue(&counter));

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Count after 3 threads = %d\n", Counter_GetValue(&counter));

    printf("Done Testing Counter\n\n");

    list_insert_arg insert_arg[3];
    list_lookup_arg lookup_arg[3];
    list_t list;

    printf("Testing list...\n");

    printf("Begin single-thread test\n");

    printf("List_Init()\n");
    List_Init(&list);

    printf("List_Insert()\n");
    List_Insert(&list, (void *)0x1, 1);
    List_Insert(&list, (void *)0x2, 2);
    List_Insert(&list, (void *)0x3, 3);

    printf("Expected %p got %p\n", (void *)0x1, List_Lookup(&list, 1));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));

    printf("List_Delete()\n");
    List_Delete(&list, 1);

    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1));

    printf("Single-thread test completed\n");

    printf("Begin multi-thread test\n");

    printf("List_Init()\n");
    List_Init(&list);

    printf("Parallel List_Insert()\n");

    insert_arg[0].list = &list;
    insert_arg[0].element = (void *)0x1;
    insert_arg[0].key = 1;

    pthread_create(&thread[0], NULL, pthread_list_insert, &insert_arg[0]);

    insert_arg[1].list = &list;
    insert_arg[1].element = (void *)0x2;
    insert_arg[1].key = 2;

    pthread_create(&thread[1], NULL, pthread_list_insert, &insert_arg[1]);

    insert_arg[2].list = &list;
    insert_arg[2].element = (void *)0x3;
    insert_arg[2].key = 3;

    pthread_create(&thread[2], NULL, pthread_list_insert, &insert_arg[2]);

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Parallel List_Insert() completed\n");

    printf("Parallel List_Lookup()\n");

    lookup_arg[0].list = &list;
    lookup_arg[0].key = 1;

    pthread_create(&thread[0], NULL, pthread_list_lookup, &lookup_arg[0]);

    lookup_arg[1].list = &list;
    lookup_arg[1].key = 2;

    pthread_create(&thread[1], NULL, pthread_list_lookup, &lookup_arg[1]);

    lookup_arg[2].list = &list;
    lookup_arg[2].key = 3;

    pthread_create(&thread[2], NULL, pthread_list_lookup, &lookup_arg[2]);

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Parallel List_Lookup() completed\n");

    printf("List_Delete()\n");
    List_Delete(&list, 1);

    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1));

    printf("Multi-thread test completed\n");

    printf("Done testing list\n\n");

    hash_t hash;

    printf("Testing hash...\n");
    printf("Begin single-thread test\n");

    printf("Hash_Init()\n");
    Hash_Init(&hash, 2);

    printf("Hash_Insert()\n");
    Hash_Insert(&hash, (void *)0x1, 1);
    Hash_Insert(&hash, (void *)0x2, 2);
    Hash_Insert(&hash, (void *)0x3, 3);

    printf("Hash_Lookup()\n");
    printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2));
    printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3));
    printf("Expected %p got %p\n", (void *)0x1, Hash_Lookup(&hash, 1));

    printf("Hash_Delete()\n");
    Hash_Delete(&hash, 1);

    printf("Hash_Lookup()\n");
    printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2));
    printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3));
    printf("Expected %p got %p\n", NULL, Hash_Lookup(&hash, 1));

    printf("Single-thread test completed\n");
    printf("Done testing hash...\n");

    return 0;
}