/* * 重置事件处理器已追踪的最大文件描述符 * * eventLoop 事件处理器指针 * setsize 已追踪的最大文件描述符 * */ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) { // 循环计数器 int i; // 设置的最大文件描述符与当前事件处理器已追踪的最大文件描述符一致,返回成功状态 if (setsize == eventLoop->setsize) return AE_OK; // 已注册的最大文件描述符大于或等于 设置的最大文件描述符,返回失败状态 if (eventLoop->maxfd >= setsize) return AE_ERR; // 使用对应平台模块中的aeApiResize方法 if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR; // 为已注册的文件事件重新分配内存 eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize); // 为已就绪的事件重新分配内存 eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize); // 设置当前已追踪的最大文件描述符 eventLoop->setsize = setsize; /* Make sure that if we created new slots, they are initialized with * an AE_NONE mask. */ // 初始化事件状态类型为未设置 for (i = eventLoop->maxfd+1; i < setsize; i++) eventLoop->events[i].mask = AE_NONE; // 返回成功状态 return AE_OK; }
/**** * 修改事件循环处理器的setsize值 * @eventLoop[IN]: 事件循环处理器 * @setsize[IN]: IO文件描述符上限(maxfd最大为setsize-1) * @return: 成功返回AE_OK, 失败返回AE_ERR */ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) { int i; /* 如果要设置的setsize值与eventLoop当前的setsize值相等, 则不执行任何动作 */ if (setsize == eventLoop->setsize) return AE_OK; /* 如果要设置的setsize值不大于当前已注册的事件中最大的文件描述符, 则返回失败 * 这个很容易理解, 因为这样修改后, 描述符值大于等于setsize的那部分事件无法保存 * 在已注册事件数组中, 所以这种修改是不允许进行的 */ if (eventLoop->maxfd >= setsize) return AE_ERR; /* 操作系统特定IO复用设施设置setsize值 */ if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR; /* 重新分配已注册IO事件数组空间, zrealloc与c语言的realloc函数功能一样 */ eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize); /* 重新分配已就绪IO事件数组空间 */ eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize); /* 更新setsize值 */ eventLoop->setsize = setsize; /* 如果扩大了数组空间, 则需要确保新增的槽位中事件的mask初始化为AE_NONE */ for (i = eventLoop->maxfd+1; i < setsize; i++) eventLoop->events[i].mask = AE_NONE; return AE_OK; }
/* Resize the maximum set size of the event loop. * If the requested set size is smaller than the current set size, but * there is already a file descriptor in use that is >= the requested * set size minus one, AE_ERR is returned and the operation is not * performed at all. * * Otherwise AE_OK is returned and the operation is successful. */ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) { int i; if (setsize == eventLoop->setsize) return AE_OK; if (eventLoop->maxfd >= setsize) return AE_ERR; if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR; eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize); eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize); eventLoop->setsize = setsize; /* Make sure that if we created new slots, they are initialized with * an AE_NONE mask. */ for (i = eventLoop->maxfd+1; i < setsize; i++) eventLoop->events[i].mask = AE_NONE; return AE_OK; }