📄 queue.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>
#include "queue.h"
bool IsFull( QUE_handle h ) {
int nexttail = h->tail+1;
if ( nexttail == h->size ) {
nexttail = 0;
}
return(nexttail == h->head);
} // end IsFull
bool IsEmpty( QUE_handle h )
{
return(h->head == h->tail);
}
QUE_handle queue_init(int que_size, int one_buf_len)
{
int i;
QUE_handle que_handle;
que_handle = (QUE_handle)malloc(sizeof(queue));
que_handle->size = que_size;
que_handle->head = 0;
que_handle->tail = 0;
//que_handle->mMutex = PTHREAD_MUTEX_INITIALIZER;
que_handle->block = (struct one_block*)malloc(sizeof(struct one_block)*que_size);
printf("que_handleeeeeeeeeeeee\n");
for(i=0; i<que_handle->size; i++)
{
if((que_handle->block[i].buf=(char *)malloc(one_buf_len)) == NULL)
{
printf("no memory for alloc buffer\n");
}
printf("buf addr=%d",que_handle->block[i].buf);
memset(que_handle->block[i].buf, 0x0, one_buf_len);
}
return que_handle;
}
bool queue_deinit(QUE_handle h)
{
int i;
for(i=0; i<h->size; i++)
{
free(h->block[i].buf);
}
free(h->block);
free(h);
return true;
}
bool enqueue(QUE_handle h, struct one_block * block )
{
bool result = false;
if(block->buf == NULL)
return result;
LOCK(h->mMutex);
if ( ! IsFull(h) ) {
// store at end of queue
h->block[h->tail].len = block->len;
memcpy(h->block[h->tail].buf, block->buf, block->len);
h->tail++;
// wrap around if needed
if ( h->tail == h->size ) {
h->tail = 0;
}
result = true;
}
UNLOCK(h->mMutex);
return result;
}
bool dequeue( QUE_handle h, struct one_block * block)
{
bool result =false;
if(block == NULL)
return result;
LOCK(h->mMutex);
if ( ! IsEmpty(h) ) {
// fetch front of queue
block->len = h->block[h->head].len;
memcpy(block->buf,h->block[h->head].buf, block->len);
h->head++;
// wrap around if needed
if ( h->head == h->size ) {
h->head = 0;
}
result = true;
}
UNLOCK(h->mMutex);
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -