示例#1
0
/** Create a huffman codebook from the input stream.
 * @param[in] fp Pointer to input stream
 * @param[out] codebook Huffman codebook
 */
void get_huffman_code(FILE *fp, struct huffcode codebook[ALPHLEN])
{
    int i;
    /* Get table of frequencies (> 0) of symbols in text */
    struct treenode ftable[ALPHLEN];
    int ftsize = create_freq_table(fp, ftable);
    /* Sort frequency table by frequency */
    qsort(ftable, ftsize, sizeof(struct treenode), node_compar);
    /* Initialize queues */
    struct queue *q1 = initqueue(), *q2 = initqueue();
    /* Enqueue leaf nodes in q1 */
    for (i=0; i<ftsize; i++) {
        enqueue(q1, &ftable[i]);
    }
    /* Form tree using both queues */
    while (q1->size > 1 || q2->size > 1) {
        struct treenode *tn[2];
        get_mintwo(q1, q2, tn);
        struct treenode *internal = malloc(sizeof(struct treenode)); 
        internal->symbol = 0;
        internal->freq = tn[0]->freq + tn[1]->freq;
        internal->l = tn[0];
        internal->is_internal = 1;
        tn[0]->p = internal;
        internal->r = tn[1];
        tn[1]->p = internal;
        enqueue(q2, internal);
    }
    /* Set root of tree */
    struct treenode *root = front(q1) ? front(q1) : front(q2);
    char code[MAX_CODELEN] = "";
    update_codes(root, codebook, code);
    freequeue(q1);
    freequeue(q2);
}
示例#2
0
void freequeue(struct tnode* root)
{
	if (root == NULL)
		return;
	freequeue(root->next);
	free(root);
}
示例#3
0
文件: queue.c 项目: jerodsanto/mgzip
int main()
{
    pthread_t threads[2];
    int i;
    int ret;
    queuetype *q;
    void *thread_ret;

    fprintf(stderr, "creating new queue...\n");
    q = newqueue();
    fprintf(stderr, "have new queue. \n");
    if (q == NULL)
        die ("bad queue\n");
    i = 0;
    ret = pthread_create(&threads[i], NULL, (void *)(void *)slam_stuff_in_queue, (void *)q);
    fprintf(stderr, "back from pthread_create 1; ret is %d, errno is %d\n", ret, errno);
    errno = 0;
    i = 1;
    ret = pthread_create(&threads[i], NULL, (void *)(void *)read_stuff_from_queue, (void *)q);
    fprintf(stderr, "back from pthread_create 2; ret is %d, errno is %d\n", ret, errno);

    fprintf(stderr, "waiting on thread 1 \n");
    pthread_join(threads[0], &thread_ret);
    fprintf(stderr, "waiting on thread 2 \n");
    pthread_join(threads[1], &thread_ret);

    fprintf(stderr, "freeing queue...\n");
    freequeue(q);
    fprintf(stderr, "done. \n");

    return 0;
}
示例#4
0
void sigint_handler(int signum)
{
	printf("got SIGINT\n");
	send_data_status(2);
	freequeue();
	close(socket_d);
	exit(0);
}
示例#5
0
static GstFlowReturn
gst_avbsrc_create (GstPushSrc * psrc, GstBuffer ** buf)
{

	if(!avb_init)
	{
		struct packet_mreq mreq;
		struct ifreq device;
		uint32_t buff_size;
		pthread_t tid;
		int ifindex;

		/* add a signal handler for interruption Ctl+c */
		signal(SIGINT, sigint_handler);

		buff_size = PAYLOAD_SIZE;

		start_feed_socket_init();

		err = pci_connect(&igb_dev);
		if (err) {
			printf("connect failed (%s) - are you running as root?\n", strerror(errno));
			return errno;
		}

		err = igb_init(&igb_dev);
		if (err) {
			printf("init failed (%s) - is the driver really loaded?\n", strerror(errno));
			return errno;
		}

		socket_d = socket(AF_PACKET, SOCK_RAW, htons(ETHER_TYPE_AVTP));
		if (socket_d == -1) {
			printf("failed to open event socket: %s \n", strerror(errno));
			return -1;
		}

		memset(&device, 0, sizeof(device));
		memcpy(device.ifr_name, interface1, IFNAMSIZ);
		err = ioctl(socket_d, SIOCGIFINDEX, &device);
		if (err == -1) {
			printf("Failed to get interface index: %s\n", strerror(errno));
			return -1;
		}

		ifindex = device.ifr_ifindex;
		memset(&ifsock_addr, 0, sizeof(ifsock_addr));
		ifsock_addr.sll_family = AF_PACKET;
		ifsock_addr.sll_ifindex = ifindex;
		ifsock_addr.sll_protocol = htons(ETHER_TYPE_AVTP);
		err = bind(socket_d, (struct sockaddr *) & ifsock_addr, sizeof(ifsock_addr));
		if (err == -1) {
			printf("Call to bind() failed: %s\n", strerror(errno));
			return -1;
		}

		memset(&mreq, 0, sizeof(mreq));
		mreq.mr_ifindex = ifindex;
		mreq.mr_type = PACKET_MR_MULTICAST;
		mreq.mr_alen = 6;
		memcpy(mreq.mr_address, DEST_ADDR, mreq.mr_alen);
		err = setsockopt(socket_d, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
		if (err == -1) {
			printf ("Unable to add PTP multicast addresses to port id: %u\n", ifindex);
			return -1;
		}

		TAILQ_INIT(&buffer_queue);
		TAILQ_INIT(&free_queue);
		pthread_mutex_init(&(buffer_queue_lock), NULL);
		pthread_mutex_init(&(free_queue_lock), NULL);

		if (initiliaze_queue(buff_size) < 0)
			return -EINVAL;

		err = pthread_create(&tid, NULL, gstreamer_main_loop, NULL);
		if (err != 0)
			printf("can't create thread :[%s]\n", strerror(err));

		memset(frame, 0, sizeof(frame));

		size = sizeof(ifsock_addr);
		frame_sequence = 0;
		start_of_input_data = 1;

		memset(&sched, 0 , sizeof (sched));
		sched.sched_priority = 25;
		sched_setscheduler(0, SCHED_RR, &sched);
	}
	
	while (1) {
		if (g_exit_app)
			break;

		err = recvfrom(socket_d, frame, FRAME_SIZE, 0, (struct sockaddr *) &ifsock_addr, (socklen_t *)&size);
		if (err > 0) {
			while (!free_queue.tqh_first) {
				usleep(1);
			}
			fprintf(stderr,"frame sequence = %lld\n", frame_sequence++);

			pthread_mutex_lock(&(free_queue_lock));
			qptr = free_queue.tqh_first;
			TAILQ_REMOVE(&free_queue, qptr, entries);
			h1722 = (seventeen22_header *)((uint8_t*)frame + sizeof(eth_header));
			qptr->payload_length = ntohs(h1722->length) - sizeof(six1883_header);
			memcpy(qptr->payload_data, (uint8_t *)((uint8_t*)frame + sizeof(eth_header) + sizeof(seventeen22_header) 
					+ sizeof(six1883_header)), qptr->payload_length);

			pthread_mutex_lock(&(buffer_queue_lock));
			TAILQ_INSERT_TAIL(&buffer_queue, qptr, entries);
			pthread_mutex_unlock(&(buffer_queue_lock));
			pthread_mutex_unlock(&(free_queue_lock));
			start_of_input_data = 1;
		} else {
 			printf("Failed to receive frame  !!!\n");
                }
	}

	usleep(100);
	pthread_exit(NULL);
	close(socket_d);
	freequeue();

	return GST_FLOW_OK;
}
示例#6
0
/*
@function main
*/
int main(int argc, char** argv)
{
	/*test pq*/
	struct tnode* p = NULL;
	struct tnode* lc, *rc;
	int NCHAR = 256; /*number of characters*/
	int i = 0;
	unsigned char c = 0;
	int usedAsciiKind = 0;

	float totalCount = 0.f;
	int count[256] = { 0 };
	float freq[256] = { 0.f };

	char INPUT_FILE[256];
	char CODE_FILE[256];
	char OUTPUT_FILE[256];

	FILE* fin = NULL;
	FILE* fout = NULL;
	FILE* fcode = NULL;

	/*zero out code*/
	memset(code, 0, sizeof(code));

	if (argc == 1) {
		printf("USAGE : %s file_to_compress", argv[0]);
		return 0;
	}
	else {
		strcpy_s(INPUT_FILE, _countof(INPUT_FILE), argv[1]);
		strcpy_s(OUTPUT_FILE, _countof(OUTPUT_FILE), argv[1]);
		strcat_s(OUTPUT_FILE, _countof(OUTPUT_FILE), ".huff");
		strcpy_s(CODE_FILE, _countof(CODE_FILE), argv[1]);
		strcat_s(CODE_FILE, _countof(CODE_FILE), ".code");
	}

	fopen_s(&fin, INPUT_FILE, "rb");
	fseek(fin, 0, SEEK_END);
	streamLength = ftell(fin);
	rewind(fin);
	//일단 빈도 통계를 내야 하므로 처음부터 끝까지 읽어가며 각 아스키 캐릭터들의 수를 센다
	//streamLength = 0;
	while (1) {
		if (feof(fin))
			break;
		fread_s(&c, sizeof(unsigned char), sizeof(unsigned char), 1, fin);
		
		totalCount += 1.;
		count[c]++;
		//streamLength++;
	}
	fclose(fin);

	qhead = NULL;
	/*initialize with freq*/
	for (i = 0; i<NCHAR; i++)
	{
		if (count[i] > 0) {
			pq_insert(talloc(i, count[i]));
			usedAsciiKind++;
		}
	}

	/*build tree*/
	for (i = 0; i < (usedAsciiKind - 1); i++)
	{
		lc = pq_pop();
		rc = pq_pop();
		/*create parent*/
		p = talloc(0, lc->appear + rc->appear);
		/*set parent link*/
		lc->parent = rc->parent = p;
		/*set child link*/
		p->right = rc; p->left = lc;
		/*make it non-leaf*/
		p->isleaf = 0;
		/*add the new node to the queue*/
		pq_insert(p);
	}

	/*get root*/
	root = pq_pop();

	/*build code*/
	generate_code(root, 0);

	/*output code*/
	fopen_s(&fout, CODE_FILE, "wb");
	//dump_code(fout);
	dump_code_bin(fout);
	fclose(fout);

	/*encode a sample string*/
	fopen_s(&fin, INPUT_FILE, "rb");
	fopen_s(&fout, OUTPUT_FILE, "wb");
	encodeBin(fin, fout);
	fclose(fin);
	fclose(fout);
	/*TODO: clear resources*/
	freetree(root);
	freequeue(qhead);

	return 0;
}