mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-22 04:50:06 +00:00
lmem.h: new laikaM_*Vector macros
- these will slowly replace laikaM_*array - lpeer.[ch] has been migrated
This commit is contained in:
parent
dbbe5f5a2a
commit
af09e74263
@ -455,7 +455,7 @@ bool laikaC_iterPeersNext(struct sLaika_cnc *cnc, size_t *i, struct sLaika_peer
|
|||||||
{
|
{
|
||||||
tCNC_PeerHashElem *elem;
|
tCNC_PeerHashElem *elem;
|
||||||
|
|
||||||
if (hashmap_iter(cnc->peers, i, (void *)&elem)) {
|
if (hashmap_iter(cnc->peers, i, (void **)&elem)) {
|
||||||
*peer = elem->peer;
|
*peer = elem->peer;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,44 @@
|
|||||||
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
|
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
|
||||||
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
||||||
|
|
||||||
|
/* ========================================[[ Vectors ]]======================================== */
|
||||||
|
|
||||||
|
#define laikaM_countVector(name) name##_COUNT
|
||||||
|
#define laikaM_capVector(name) name##_CAP
|
||||||
|
|
||||||
|
#define laikaM_newVector(type, name) \
|
||||||
|
type *name; \
|
||||||
|
int name##_COUNT; \
|
||||||
|
int name##_CAP;
|
||||||
|
#define laikaM_initVector(name, startCap) \
|
||||||
|
name = NULL; \
|
||||||
|
name##_COUNT = 0; \
|
||||||
|
name##_CAP = startCap;
|
||||||
|
|
||||||
|
#define laikaM_growVector(type, name, needed) \
|
||||||
|
if (name##_COUNT + needed >= name##_CAP || name == NULL) { \
|
||||||
|
name##_CAP = (name##_CAP + needed) * GROW_FACTOR; \
|
||||||
|
name = (type *)laikaM_realloc(name, sizeof(type) * name##_CAP); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* moves vector elements above indx down by numElem, removing numElem elements at indx */
|
||||||
|
#define laikaM_rmvVector(name, indx, numElem) \
|
||||||
|
do { \
|
||||||
|
int _i, _sz = ((name##_COUNT - indx) - numElem); \
|
||||||
|
for (_i = 0; _i < _sz; _i++) \
|
||||||
|
name[indx + _i] = name[indx + numElem + _i]; \
|
||||||
|
name##_COUNT -= numElem; \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
/* moves vector elements above indx up by numElem, inserting numElem elements at indx */
|
||||||
|
#define laikaM_insertVector(name, indx, numElem) \
|
||||||
|
do { \
|
||||||
|
int _i; \
|
||||||
|
for (_i = name##_COUNT; _i > indx; _i--) \
|
||||||
|
name[_i] = name[_i - 1]; \
|
||||||
|
name##_COUNT += numElem; \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
#define laikaM_growarray(type, buf, needed, count, capacity) \
|
#define laikaM_growarray(type, buf, needed, count, capacity) \
|
||||||
if (count + needed >= capacity || buf == NULL) { \
|
if (count + needed >= capacity || buf == NULL) { \
|
||||||
capacity = (capacity + needed) * GROW_FACTOR; \
|
capacity = (capacity + needed) * GROW_FACTOR; \
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "hashmap.h"
|
#include "hashmap.h"
|
||||||
#include "laika.h"
|
#include "laika.h"
|
||||||
#include "lsocket.h"
|
#include "lsocket.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
@ -20,22 +21,16 @@ struct sLaika_pollEvent
|
|||||||
struct sLaika_pollList
|
struct sLaika_pollList
|
||||||
{
|
{
|
||||||
struct hashmap *sockets;
|
struct hashmap *sockets;
|
||||||
struct sLaika_socket **outQueue; /* holds sockets which have data needed to be sent */
|
laikaM_newVector(struct sLaika_socket *, outQueue); /* holds sockets which have data needed to be sent */
|
||||||
struct sLaika_pollEvent *revents;
|
laikaM_newVector(struct sLaika_pollEvent, revents);
|
||||||
#ifdef LAIKA_USE_EPOLL
|
#ifdef LAIKA_USE_EPOLL
|
||||||
/* epoll */
|
/* epoll */
|
||||||
struct epoll_event ev, ep_events[MAX_EPOLL_EVENTS];
|
struct epoll_event ev, ep_events[MAX_EPOLL_EVENTS];
|
||||||
SOCKET epollfd;
|
SOCKET epollfd;
|
||||||
#else
|
#else
|
||||||
/* raw poll descriptor */
|
/* raw poll descriptor */
|
||||||
PollFD *fds;
|
laikaM_newVector(PollFD, fds);
|
||||||
int fdCapacity;
|
|
||||||
int fdCount;
|
|
||||||
#endif
|
#endif
|
||||||
int reventCap;
|
|
||||||
int reventCount;
|
|
||||||
int outCap;
|
|
||||||
int outCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void laikaP_initPList(struct sLaika_pollList *pList);
|
void laikaP_initPList(struct sLaika_pollList *pList);
|
||||||
|
@ -34,12 +34,10 @@ void laikaP_initPList(struct sLaika_pollList *pList)
|
|||||||
/* setup hashmap */
|
/* setup hashmap */
|
||||||
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash,
|
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash,
|
||||||
elem_compare, NULL, NULL);
|
elem_compare, NULL, NULL);
|
||||||
pList->revents = NULL; /* laikaP_pollList() will allocate the buffer */
|
|
||||||
pList->reventCap = POLLSTARTCAP / GROW_FACTOR;
|
/* laikaP_pollList() will allocate these buffer */
|
||||||
pList->reventCount = 0;
|
laikaM_initVector(pList->revents, POLLSTARTCAP / GROW_FACTOR);
|
||||||
pList->outQueue = NULL;
|
laikaM_initVector(pList->outQueue, POLLSTARTCAP / GROW_FACTOR);
|
||||||
pList->outCap = POLLSTARTCAP / GROW_FACTOR;
|
|
||||||
pList->outCount = 0;
|
|
||||||
|
|
||||||
#ifdef LAIKA_USE_EPOLL
|
#ifdef LAIKA_USE_EPOLL
|
||||||
/* setup our epoll */
|
/* setup our epoll */
|
||||||
@ -48,11 +46,8 @@ void laikaP_initPList(struct sLaika_pollList *pList)
|
|||||||
LAIKA_ERROR("epoll_create() failed!\n");
|
LAIKA_ERROR("epoll_create() failed!\n");
|
||||||
|
|
||||||
#else
|
#else
|
||||||
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */
|
/* laikaP_addSock will allocate this buffer */
|
||||||
pList->fdCapacity =
|
laikaM_initVector(pList->fds, POLLSTARTCAP / GROW_FACTOR);
|
||||||
POLLSTARTCAP /
|
|
||||||
GROW_FACTOR; /* div by GROW_FACTOR since laikaM_growarray multiplies by GROW_FACTOR */
|
|
||||||
pList->fdCount = 0;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +80,8 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
/* allocate space in array & add PollFD */
|
/* allocate space in array & add PollFD */
|
||||||
laikaM_growarray(PollFD, pList->fds, 1, pList->fdCount, pList->fdCapacity);
|
laikaM_growVector(PollFD, pList->fds, 1);
|
||||||
pList->fds[pList->fdCount++] = (PollFD){sock->sock, POLLIN};
|
pList->fds[laikaM_countVector(pList->fds)++] = (PollFD){sock->sock, POLLIN};
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,9 +93,9 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
|||||||
hashmap_delete(pList->sockets, &(tLaika_hashMapElem){.fd = sock->sock, .sock = sock});
|
hashmap_delete(pList->sockets, &(tLaika_hashMapElem){.fd = sock->sock, .sock = sock});
|
||||||
|
|
||||||
/* make sure peer isn't in outQueue */
|
/* make sure peer isn't in outQueue */
|
||||||
for (i = 0; i < pList->outCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
|
||||||
if ((void *)pList->outQueue[i] == (void *)sock) {
|
if ((void *)pList->outQueue[i] == (void *)sock) {
|
||||||
laikaM_rmvarray(pList->outQueue, pList->outCount, i, 1);
|
laikaM_rmvVector(pList->outQueue, i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,10 +109,10 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
/* search fds for socket, remove it and shrink array */
|
/* search fds for socket, remove it and shrink array */
|
||||||
for (i = 0; i < pList->fdCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->fds); i++) {
|
||||||
if (pList->fds[i].fd == sock->sock) {
|
if (pList->fds[i].fd == sock->sock) {
|
||||||
/* remove from array */
|
/* remove from array */
|
||||||
laikaM_rmvarray(pList->fds, pList->fdCount, i, 1);
|
laikaM_rmvVector(pList->fds, i, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,7 +135,7 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* search fds for socket, add POLLOUT flag */
|
/* search fds for socket, add POLLOUT flag */
|
||||||
for (i = 0; i < pList->fdCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->fds); i++) {
|
||||||
if (pList->fds[i].fd == sock->sock) {
|
if (pList->fds[i].fd == sock->sock) {
|
||||||
pList->fds[i].events = POLLIN | POLLOUT;
|
pList->fds[i].events = POLLIN | POLLOUT;
|
||||||
break;
|
break;
|
||||||
@ -167,7 +162,7 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* search fds for socket, remove POLLOUT flag */
|
/* search fds for socket, remove POLLOUT flag */
|
||||||
for (i = 0; i < pList->fdCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->fds); i++) {
|
||||||
if (pList->fds[i].fd == sock->sock) {
|
if (pList->fds[i].fd == sock->sock) {
|
||||||
pList->fds[i].events = POLLIN;
|
pList->fds[i].events = POLLIN;
|
||||||
break;
|
break;
|
||||||
@ -183,18 +178,18 @@ void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *so
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* first, check that we don't have this peer in the queue already */
|
/* first, check that we don't have this peer in the queue already */
|
||||||
for (i = 0; i < pList->outCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
|
||||||
if (pList->outQueue[i] == sock)
|
if (pList->outQueue[i] == sock)
|
||||||
return; /* found it :) */
|
return; /* found it :) */
|
||||||
}
|
}
|
||||||
|
|
||||||
laikaM_growarray(struct sLaika_socket *, pList->outQueue, 1, pList->outCount, pList->outCap);
|
laikaM_growVector(struct sLaika_socket *, pList->outQueue, 1);
|
||||||
pList->outQueue[pList->outCount++] = sock;
|
pList->outQueue[laikaM_countVector(pList->outQueue)++] = sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList)
|
void laikaP_resetOutQueue(struct sLaika_pollList *pList)
|
||||||
{
|
{
|
||||||
pList->outCount = 0; /* ez lol */
|
laikaM_countVector(pList->outQueue) = 0; /* ez lol */
|
||||||
}
|
}
|
||||||
|
|
||||||
void laikaP_flushOutQueue(struct sLaika_pollList *pList)
|
void laikaP_flushOutQueue(struct sLaika_pollList *pList)
|
||||||
@ -203,7 +198,7 @@ void laikaP_flushOutQueue(struct sLaika_pollList *pList)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* flush pList's outQueue */
|
/* flush pList's outQueue */
|
||||||
for (i = 0; i < pList->outCount; i++) {
|
for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
|
||||||
sock = pList->outQueue[i];
|
sock = pList->outQueue[i];
|
||||||
LAIKA_DEBUG("sending OUT to %p\n", sock);
|
LAIKA_DEBUG("sending OUT to %p\n", sock);
|
||||||
if (sock->onPollOut && !sock->onPollOut(sock) && sock->onPollFail)
|
if (sock->onPollOut && !sock->onPollOut(sock) && sock->onPollFail)
|
||||||
@ -216,7 +211,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
{
|
{
|
||||||
int nEvents, i;
|
int nEvents, i;
|
||||||
|
|
||||||
pList->reventCount = 0; /* reset revent array */
|
laikaM_countVector(pList->revents) = 0; /* reset revent array */
|
||||||
#ifdef LAIKA_USE_EPOLL
|
#ifdef LAIKA_USE_EPOLL
|
||||||
/* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a
|
/* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a
|
||||||
lookup into our socket hashmap not to mention the various improvements epoll() has over
|
lookup into our socket hashmap not to mention the various improvements epoll() has over
|
||||||
@ -229,9 +224,8 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
|
|
||||||
for (i = 0; i < nEvents; i++) {
|
for (i = 0; i < nEvents; i++) {
|
||||||
/* add event to revent array */
|
/* add event to revent array */
|
||||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount,
|
laikaM_growVector(struct sLaika_pollEvent, pList->revents, 1);
|
||||||
pList->reventCap);
|
pList->revents[laikaM_countVector(pList->revents)++] =
|
||||||
pList->revents[pList->reventCount++] =
|
|
||||||
(struct sLaika_pollEvent){.sock = pList->ep_events[i].data.ptr,
|
(struct sLaika_pollEvent){.sock = pList->ep_events[i].data.ptr,
|
||||||
.pollIn = pList->ep_events[i].events & EPOLLIN,
|
.pollIn = pList->ep_events[i].events & EPOLLIN,
|
||||||
.pollOut = pList->ep_events[i].events & EPOLLOUT};
|
.pollOut = pList->ep_events[i].events & EPOLLOUT};
|
||||||
@ -252,9 +246,8 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
|
pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
|
||||||
|
|
||||||
/* insert event into revents array */
|
/* insert event into revents array */
|
||||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount,
|
laikaM_growVector(struct sLaika_pollEvent, pList->revents, 1);
|
||||||
pList->reventCap);
|
pList->revents[laikaM_countVector(pList->revents)++] =
|
||||||
pList->revents[pList->reventCount++] =
|
|
||||||
(struct sLaika_pollEvent){.sock = elem->sock,
|
(struct sLaika_pollEvent){.sock = elem->sock,
|
||||||
.pollIn = pfd.revents & POLLIN,
|
.pollIn = pfd.revents & POLLIN,
|
||||||
.pollOut = pfd.revents & POLLOUT};
|
.pollOut = pfd.revents & POLLOUT};
|
||||||
@ -264,7 +257,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
*_nevents = pList->reventCount;
|
*_nevents = laikaM_countVector(pList->revents);
|
||||||
|
|
||||||
/* return revents array */
|
/* return revents array */
|
||||||
return pList->revents;
|
return pList->revents;
|
||||||
|
Loading…
Reference in New Issue
Block a user