swDataBuffer_trunk * swDataBuffer_newTrunk(swDataBuffer *data_buffer, swDataBuffer_item *item) { swDataBuffer_trunk *trunk = sw_malloc(sizeof(swDataBuffer_trunk)); if (trunk == NULL) { swWarn("malloc for trunk failed. Error: %s[%d]", strerror(errno), errno); return NULL; } char *buf = sw_malloc(data_buffer->trunk_size); if (buf == NULL) { swWarn("malloc for data failed. Error: %s[%d]", strerror(errno), errno); sw_free(trunk); return NULL; } bzero(trunk, sizeof(swDataBuffer_trunk)); trunk->data = buf; item->trunk_num++; if(item->head == NULL) { item->tail = item->head = trunk; } else { item->tail->next = trunk; item->tail = trunk; } return trunk; }
swRbtree* swRbtree_new() { swRbtree *rbtree = sw_malloc(sizeof(swRbtree)); swRbtree_node *sentinel = sw_malloc(sizeof(swRbtree_node)); sentinel->color = 0; rbtree->root = sentinel; rbtree->sentinel = sentinel; return rbtree; }
int swAio_dispatch(swAio_event *_event) { if (SwooleAIO.init == 0) { swAio_init(); } _event->task_id = SwooleAIO.current_id++; swAio_event *event = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (event == NULL) { swWarn("malloc failed"); return SW_ERR; } memcpy(event, _event, sizeof(swAio_event)); if (swThreadPool_dispatch(&pool, event, sizeof(event)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return _event->task_id; } }
static void swTable_compress_list(swTable *table) { table->lock.lock(&table->lock); swTableRow **tmp = sw_malloc(sizeof(swTableRow *) * table->size); if (!tmp) { swWarn("malloc() failed, cannot compress the jump table."); goto unlock; } int i, tmp_i = 0; for (i = 0; i < table->list_n; i++) { if (table->rows_list[i] != NULL) { tmp[tmp_i] = table->rows_list[i]; tmp[tmp_i]->list_index = tmp_i; tmp_i++; } } memcpy(table->rows_list, tmp, sizeof(swTableRow *) * tmp_i); sw_free(tmp); table->list_n = tmp_i; unlock: table->lock.unlock(&table->lock); }
swMemoryPool *swRingBuffer_new(uint32_t size, uint8_t shared) { void *mem = (shared == 1) ? sw_shm_malloc(size) : sw_malloc(size); if (mem == NULL) { swWarn("malloc(%d) failed.", size); return NULL; } swRingBuffer *object = mem; mem = (char *) mem + sizeof(swRingBuffer); bzero(object, sizeof(swRingBuffer)); object->size = (size - sizeof(swRingBuffer) - sizeof(swMemoryPool)); object->shared = shared; swMemoryPool *pool = mem; mem = (char *) mem + sizeof(swMemoryPool); pool->object = object; pool->destroy = swRingBuffer_destory; pool->free = swRingBuffer_free; pool->alloc = swRingBuffer_alloc; object->memory = mem; swDebug("memory: ptr=%p", mem); return pool; }
static int swAioBase_read(int fd, void *inbuf, size_t size, off_t offset) { swAio_event *aio_ev = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (aio_ev == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(aio_ev, sizeof(swAio_event)); aio_ev->fd = fd; aio_ev->buf = inbuf; aio_ev->type = SW_AIO_READ; aio_ev->nbytes = size; aio_ev->offset = offset; aio_ev->task_id = SwooleAIO.current_id++; if (swThreadPool_dispatch(&swAioBase_thread_pool, aio_ev, sizeof(aio_ev)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return aio_ev->task_id; } }
int swReactorEpoll_create(swReactor *reactor, int max_event_num) { //create reactor object swReactorEpoll *reactor_object = sw_malloc(sizeof(swReactorEpoll)); if (reactor_object == NULL) { swWarn("malloc[0] fail\n"); return SW_ERR; } reactor->object = reactor_object; reactor_object->events = sw_calloc(max_event_num, sizeof(struct epoll_event)); if (reactor_object->events == NULL) { swWarn("malloc[1] fail\n"); return SW_ERR; } //epoll create reactor_object->event_max = 0; reactor_object->epfd = epoll_create(512); if (reactor_object->epfd < 0) { swWarn("epoll_create fail.Error: %s[%d]", strerror(errno), errno); return SW_ERR; } //binding method reactor->add = swReactorEpoll_add; reactor->set = swReactorEpoll_set; reactor->del = swReactorEpoll_del; reactor->wait = swReactorEpoll_wait; reactor->free = swReactorEpoll_free; reactor->setHandle = swReactor_setHandle; return SW_OK; }
long swTimer_addtimeout(swTimer *timer, int timeout_ms, void *data) { int new_interval = swoole_common_divisor(timeout_ms, timer->interval); if (new_interval < timer->interval) { swTimer_set(timer, new_interval); timer->interval = new_interval; } struct timeval now; if (gettimeofday(&now, NULL) < 0) { swWarn("gettimeofday() failed. Error: %s[%d]", strerror(errno), errno); return SW_ERR; } uint32_t now_ms = now.tv_sec * 1000 + now.tv_usec / 1000; swTimer_node *node = sw_malloc(sizeof(swTimer_node)); if (node == NULL) { swWarn("malloc(%d) failed. Error: %s[%d]", (int ) sizeof(swTimer_node), strerror(errno), errno); return SW_ERR; } bzero(node, sizeof(swTimer_node)); node->data = data; node->exec_msec = now_ms + timeout_ms; node->id = timer->_next_id++; swTimer_node_insert(&timer->root, node); return node->id; }
int swPipeBase_create(swPipe *p, int blocking) { int ret; swPipeBase *object = sw_malloc(sizeof(swPipeBase)); if (object == NULL) { return -1; } p->blocking = blocking; ret = pipe(object->pipes); if (ret < 0) { swWarn("pipe create fail. Error: %s[%d]", strerror(errno), errno); return -1; } else { //Nonblock if(blocking == 0) { swSetNonBlock(object->pipes[0]); swSetNonBlock(object->pipes[1]); } p->object = object; p->read = swPipeBase_read; p->write = swPipeBase_write; p->getFd = swPipeBase_getFd; p->close = swPipeBase_close; } return 0; }
static int swServer_poll_onReceive2(swReactor *reactor, swEvent *event) { int ret; swServer *serv = reactor->ptr; swFactory *factory = &(serv->factory); swEventData *buf = sw_malloc(sizeof(swEventData)); if(buf==NULL) { swTrace("Malloc fail\n"); return SW_ERR; } bzero(buf->data, sizeof(buf->data)); ret = swRead(event->fd, buf->data, SW_BUFFER_SIZE); if (ret < 0) { swTrace("Receive Error.Fd=%d.From=%d\n", event->fd, event->from_id); return SW_ERR; } else if (ret == 0) { swTrace("Close Event.FD=%d|From=%d\n", event->fd, event->from_id); sw_free(buf); return swServer_close(serv, event); } else { buf->fd = event->fd; buf->len = ret; buf->from_id = event->from_id; //swTrace("recv: %s|fd=%d|ret=%d|errno=%d\n", buf->data, event->fd, ret, errno); factory->dispatch(factory, buf); } return SW_OK; }
static long swTimer_add(swTimer *timer, int msec, int interval, void *data) { if (interval == 0) { return swTimer_addtimeout(timer, msec, data); } swTimer_node *node = sw_malloc(sizeof(swTimer_node)); if (node == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(node, sizeof(swTimer_node)); node->interval = msec; if (gettimeofday(&node->lasttime, NULL) < 0) { swSysError("gettimeofday() failed."); return SW_ERR; } if (msec < timer->interval) { int new_interval = swoole_common_divisor(msec, timer->interval); timer->interval = new_interval; swTimer_set(timer, new_interval); } swHashMap_add_int(timer->list, msec, node, NULL); timer->num++; return SW_OK; }
int swTimer_add(swTimer *timer, int ms) { swTimer_node *node = sw_malloc(sizeof(swTimer_node)); if (node == NULL) { swWarn("swTimer_add malloc fail"); return SW_ERR; } bzero(node, sizeof(swTimer_node)); node->lasttime = swTimer_get_ms(); node->interval = ms; if(ms < timer->interval) { int new_interval = swoole_common_divisor(ms, timer->interval); timer->interval = new_interval; #if defined(HAVE_TIMERFD) && SW_WORKER_IPC_MODE == 1 swTimer_timerfd_set(timer, new_interval); #else swTimer_signal_set(timer, new_interval); #endif } swHashMap_add_int(&timer->list, ms, node); timer->num++; return SW_OK; }
swTimeWheel* swTimeWheel_new(uint16_t size) { swTimeWheel *tw = sw_malloc(sizeof(swTimeWheel)); if (!tw) { swWarn("malloc(%ld) failed.", sizeof(swTimeWheel)); return NULL; } tw->size = size; tw->current = 0; tw->wheel = sw_calloc(size, sizeof(void*)); if (tw->wheel == NULL) { swWarn("malloc(%ld) failed.", sizeof(void*) * size); sw_free(tw); return NULL; } int i; for (i = 0; i < size; i++) { tw->wheel[i] = swHashMap_new(16, NULL); if (tw->wheel[i] == NULL) { swTimeWheel_free(tw); return NULL; } } return tw; }
int swReactorPoll_create(swReactor *reactor, int max_fd_num) { //create reactor object swReactorPoll *object = sw_malloc(sizeof(swReactorPoll)); if (object == NULL) { swError("malloc[0] failed"); return SW_ERR; } bzero(object, sizeof(swReactorPoll)); object->fds = sw_calloc(max_fd_num, sizeof(swPollFdInfo)); if (object->fds == NULL) { swError("malloc[1] failed"); return SW_ERR; } object->events = sw_calloc(max_fd_num, sizeof(struct pollfd)); if (object->events == NULL) { swError("malloc[2] failed"); return SW_ERR; } object->max_fd_num = max_fd_num; bzero(reactor->handle, sizeof(reactor->handle)); reactor->object = object; //binding method reactor->add = swReactorPoll_add; reactor->del = swReactorPoll_del; reactor->set = swReactorPoll_set; reactor->wait = swReactorPoll_wait; reactor->free = swReactorPoll_free; reactor->setHandle = swReactor_setHandle; return SW_OK; }
int swPipeUnsock_create(swPipe *p, int blocking, int protocol) { int ret; swPipeUnsock *object = sw_malloc(sizeof(swPipeUnsock)); if (object == NULL) { return -1; } p->blocking = blocking; ret = socketpair(PF_LOCAL, protocol, 0, object->socks); if (ret < 0) { return -1; } else { //Nonblock if (blocking == 0) { swSetNonBlock(object->socks[0]); swSetNonBlock(object->socks[1]); } p->object = object; p->read = swPipeUnsock_read; p->write = swPipeUnsock_write; p->getFd = swPipeUnsock_getFd; p->close = swPipeUnsock_close; } return 0; }
static int swAioLinux_write(int fd, void *inbuf, size_t size, off_t offset) { struct iocb *iocbps[1]; struct iocb *iocbp = sw_malloc(sizeof(struct iocb)); if (iocbp == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(iocbp, sizeof(struct iocb)); iocbp->aio_fildes = fd; iocbp->aio_lio_opcode = IOCB_CMD_PWRITE; iocbp->aio_buf = (__u64 ) inbuf; iocbp->aio_offset = offset; iocbp->aio_nbytes = size; iocbp->aio_flags = IOCB_FLAG_RESFD; iocbp->aio_resfd = swoole_aio_eventfd; iocbp->aio_reqprio = SwooleAIO.current_id++; //iocbp->aio_data = (__u64) aio_callback; iocbps[0] = iocbp; if (io_submit(swoole_aio_context, 1, iocbps) == 1) { SwooleAIO.task_num++; return iocbp->aio_reqprio; } swWarn("io_submit failed. Error: %s[%d]", strerror(errno), errno); return SW_ERR; }
int swAio_dns_lookup(void *hostname, void *ip_addr, size_t size) { swAio_event *aio_ev = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (aio_ev == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(aio_ev, sizeof(swAio_event)); aio_ev->buf = ip_addr; aio_ev->req = hostname; aio_ev->type = SW_AIO_DNS_LOOKUP; aio_ev->nbytes = size; aio_ev->task_id = SwooleAIO.current_id++; if (swThreadPool_dispatch(&swAioBase_thread_pool, aio_ev, sizeof(aio_ev)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return aio_ev->task_id; } }
/** * @brief * Function to assign a file descriptor and necessary information to a file * structure inorder to pursue subsequent operations * @param finfo * Pointer to a file structure where the file information is stored * @param entry * Pointer to the directory entry of the file/directory * @param mode * Mode in which the file is requested to be opened * @return * File descriptor of the file */ int retrieve_file_info(file_info *finfo,dir_entry *entry,u8 mode, u8 *dir_offset,const char *path) { file_info *temp; if(file_head == NULL) file_head = finfo; else{ temp = file_head; while(temp->next != NULL) temp = temp->next; temp->next = finfo; } finfo->fd = fdes = fdes + 1; finfo->file_path = sw_malloc(sizeof(char) * (sw_strlen(path) + 1)); sw_strcpy(finfo->file_path,path); finfo->entry = entry; finfo->strt_cluster = (entry->strt_clus_hword)<<16 | (entry->strt_clus_lword); finfo->cur_cluster = finfo->strt_cluster; finfo->cur_offset = 0; finfo->file_size = entry->size; finfo->mode = mode; finfo->bytes_traversed = 0; finfo->dir_entry_offset = dir_offset; finfo->next = NULL; return(finfo->fd); }
static int swEventTimer_add(swTimer *timer, int _msec, int interval, void *data) { swTimer_node *node = sw_malloc(sizeof(swTimer_node)); if (!node) { swSysError("malloc(%d) failed.", (int )sizeof(swTimer_node)); return SW_ERR; } int now_msec = swEventTimer_get_relative_msec(); if (now_msec < 0) { return SW_ERR; } node->data = data; node->exec_msec = now_msec + _msec; node->interval = interval ? _msec : 0; if (SwooleG.main_reactor->timeout_msec > _msec) { SwooleG.main_reactor->timeout_msec = _msec; } swTimer_node_insert(&timer->root, node); return SW_OK; }
void* swHeap_push(swHeap *heap, uint64_t priority, void *data) { void *tmp; uint32_t i; uint32_t newsize; if (heap->num >= heap->size) { newsize = heap->size * 2; if (!(tmp = sw_realloc(heap->nodes, sizeof(void *) * newsize))) { return NULL; } heap->nodes = tmp; heap->size = newsize; } swHeap_node *node = sw_malloc(sizeof(swHeap_node)); if (!node) { return NULL; } node->priority = priority; node->data = data; i = heap->num++; heap->nodes[i] = node; swHeap_bubble_up(heap, i); return node; }
int swReactorKqueue_create(swReactor *reactor, int max_event_num) { //create reactor object swReactorKqueue *reactor_object = sw_malloc(sizeof(swReactorKqueue)); if (reactor_object == NULL) { swTrace("[swReactorKqueueCreate] malloc[0] fail\n"); return SW_ERR; } reactor->object = reactor_object; reactor_object->events = sw_calloc(max_event_num, sizeof(struct kevent)); if (reactor_object->events == NULL) { swTrace("[swReactorKqueueCreate] malloc[1] fail\n"); return SW_ERR; } //kqueue create reactor_object->event_max = max_event_num; reactor_object->epfd = kqueue(); if (reactor_object->epfd < 0) { swTrace("[swReactorKqueueCreate] kqueue_create[0] fail\n"); return SW_ERR; } //binding method reactor->add = swReactorKqueue_add; reactor->set = swReactorKqueue_set; reactor->del = swReactorKqueue_del; reactor->wait = swReactorKqueue_wait; reactor->free = swReactorKqueue_free; reactor->setHandle = swReactor_setHandle; return SW_OK; }
int swPipeMsg_create(swPipe *p, int blocking, int msg_key, long type) { int msg_id; swPipeMsg *object = sw_malloc(sizeof(swPipeMsg)); if (object == NULL) { return -1; } if (blocking == 0) { object->ipc_wait = IPC_NOWAIT; } else { object->ipc_wait = 0; } p->blocking = blocking; msg_id = msgget(msg_key, IPC_CREAT | 0666); if (msg_id < 0) { return -1; } else { object->msg_id = msg_id; object->type = type; p->object = object; p->read = swPipeMsg_read; p->write = swPipeMsg_write; p->getFd = swPipeMsg_getFd; p->close = swPipeMsg_close; } return 0; }
static int swFactoryProcess_writer_start(swFactory *factory) { swFactoryProcess *this = factory->object; swThreadParam *param; int i; pthread_t pidt; for (i = 0; i < this->writer_num; i++) { param = sw_malloc(sizeof(swThreadParam)); if (param == NULL) { swTrace("malloc fail\n"); return SW_ERR; } param->object = factory; param->pti = i; if (pthread_create(&pidt, NULL, (void * (*)(void *)) swFactoryProcess_writer_loop, (void *) param) < 0) { swTrace("pthread_create fail\n"); return SW_ERR; } this->writers[i].ptid = pidt; SW_START_SLEEP; } return SW_OK; }
int swReactorEpoll_create(swReactor *reactor, int max_event_num) { //create reactor object swReactorEpoll *reactor_object = sw_malloc(sizeof(swReactorEpoll)); if (reactor_object == NULL) { swTrace("[swReactorEpollCreate] malloc[0] fail\n"); return SW_ERR; } reactor->object = reactor_object; reactor_object->events = sw_calloc(max_event_num, sizeof(struct epoll_event)); if (reactor_object->events == NULL) { swTrace("[swReactorEpollCreate] malloc[1] fail\n"); return SW_ERR; } //epoll create reactor_object->event_max = 0; reactor_object->epfd = epoll_create(512); if (reactor_object->epfd < 0) { swTrace("[swReactorEpollCreate] epoll_create[0] fail\n"); return SW_ERR; } //binding method reactor->add = swReactorEpoll_add; reactor->del = swReactorEpoll_del; reactor->wait = swReactorEpoll_wait; reactor->free = swReactorEpoll_free; reactor->setHandle = swReactor_setHandle; return SW_OK; }
int swTimer_add(swTimer *timer, int ms) { swTimer_node *node = sw_malloc(sizeof(swTimer_node)); if (node == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(node, sizeof(swTimer_node)); node->lasttime = swTimer_get_ms(); node->interval = ms; if (ms < timer->interval) { int new_interval = swoole_common_divisor(ms, timer->interval); timer->interval = new_interval; if (SwooleG.use_timerfd) { swTimer_timerfd_set(timer, new_interval); } else { swTimer_signal_set(timer, new_interval); } } swHashMap_add_int(timer->list, ms, node); timer->num++; return SW_OK; }
swMemoryPool *swRingBuffer_new(size_t size, uint8_t shared) { size_t malloc_size = size + sizeof(swRingBuffer) + sizeof(swMemoryPool); void *mem = (shared == 1) ? sw_shm_malloc(malloc_size) : sw_malloc(malloc_size); if (mem == NULL) { swWarn("malloc(%ld) failed.", size); return NULL; } swRingBuffer *object = mem; mem += sizeof(swRingBuffer); bzero(object, sizeof(swRingBuffer)); object->size = size; object->shared = shared; swMemoryPool *pool = mem; mem += sizeof(swMemoryPool); pool->object = object; pool->destroy = swRingBuffer_destory; pool->free = swRingBuffer_free; pool->alloc = swRingBuffer_alloc; object->memory = mem; return pool; }
static int swFactoryProcess_writer_start(swFactory *factory) { swFactoryProcess *object = factory->object; swThreadParam *param; int i; pthread_t pidt; swThreadStartFunc thread_main; #if SW_WORKER_IPC_MODE == 2 thread_main = (swThreadStartFunc) swFactoryProcess_writer_loop_queue; #else thread_main = (swThreadStartFunc) swFactoryProcess_writer_loop_unsock; #endif for (i = 0; i < object->writer_num; i++) { param = sw_malloc(sizeof(swPipe)); if (param == NULL) { swError("malloc fail\n"); return SW_ERR; } param->object = factory; param->pti = i; if (pthread_create(&pidt, NULL, thread_main, (void *) param) < 0) { swTrace("pthread_create fail\n"); return SW_ERR; } pthread_detach(pidt); object->writers[i].ptid = pidt; SW_START_SLEEP; } return SW_OK; }
static int swAioGcc_write(int fd, void *inbuf, size_t size, off_t offset) { swAio_gcc_t *aiocb = sw_malloc(sizeof(swAio_gcc_t)); if (aiocb == NULL) { swWarn("malloc failed."); return SW_ERR; } aiocb->next = NULL; if (swAioGcc_request == NULL) { swAioGcc_request = aiocb; } else { swAioGcc_request->next = aiocb; } bzero(aiocb, sizeof(swAio_gcc_t)); aiocb->aiocb.aio_fildes = fd; aiocb->aiocb.aio_buf = inbuf; aiocb->aiocb.aio_nbytes = size; aiocb->aiocb.aio_lio_opcode = LIO_WRITE; aiocb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; aiocb->aiocb.aio_sigevent.sigev_signo = SIGIO; if (aio_write(&aiocb->aiocb) == -1) { swWarn("aio_write failed. Error: %s[%d]", strerror(errno), errno); return SW_ERR; } return SW_OK; }
int swWorker_create(swWorker *worker) { /** * Create shared memory storage */ void *store = sw_shm_malloc(SwooleG.serv->buffer_output_size); if (store == NULL) { swWarn("malloc for worker->store failed."); return SW_ERR; } swPipe *worker_notify = sw_malloc(sizeof(swPipe)); if (worker_notify == NULL) { swWarn("malloc for worker->notify failed."); sw_shm_free(store); return SW_ERR; } /** * Create notify pipe */ if (swPipeNotify_auto(worker_notify, 1, 0)) { sw_shm_free(store); sw_free(worker_notify); return SW_ERR; } worker->notify = worker_notify; worker->store.ptr = store; worker->store.lock = 0; return SW_OK; }
swDataBuffer_item* swDataBuffer_newItem(swDataBuffer *data_buffer, int fd, int trunk_size) { swDataBuffer_item *newItem = sw_malloc(sizeof(swDataBuffer_item)); //内存分配失败 if (newItem == NULL) { swWarn("malloc for newItem failed. Error: %s[%d]", strerror(errno), errno); return NULL; } bzero(newItem, sizeof(swDataBuffer_item)); //创建item时,自动建立一个trunk swDataBuffer_trunk *newTrunk = swDataBuffer_newTrunk(data_buffer, newItem); if (newTrunk == NULL) { sw_free(newItem); swWarn("malloc for newTrunk failed. Error: %s[%d]", strerror(errno), errno); return NULL; } newItem->fd = fd; swHashMap_add_int(&data_buffer->map, fd, newItem); return newItem; }