1
0
mirror of https://github.com/CPunch/Laika.git synced 2026-02-03 15:10:03 +00:00

lmem.h: new laikaM_*Vector macros

- these will slowly replace laikaM_*array
- lpeer.[ch] has been migrated
This commit is contained in:
2022-09-01 18:47:29 -05:00
parent dbbe5f5a2a
commit af09e74263
4 changed files with 68 additions and 42 deletions

View File

@@ -17,6 +17,44 @@
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
#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) \
if (count + needed >= capacity || buf == NULL) { \
capacity = (capacity + needed) * GROW_FACTOR; \

View File

@@ -4,6 +4,7 @@
#include "hashmap.h"
#include "laika.h"
#include "lsocket.h"
#include "lmem.h"
#include <stdbool.h>
@@ -20,22 +21,16 @@ struct sLaika_pollEvent
struct sLaika_pollList
{
struct hashmap *sockets;
struct sLaika_socket **outQueue; /* holds sockets which have data needed to be sent */
struct sLaika_pollEvent *revents;
laikaM_newVector(struct sLaika_socket *, outQueue); /* holds sockets which have data needed to be sent */
laikaM_newVector(struct sLaika_pollEvent, revents);
#ifdef LAIKA_USE_EPOLL
/* epoll */
struct epoll_event ev, ep_events[MAX_EPOLL_EVENTS];
SOCKET epollfd;
#else
/* raw poll descriptor */
PollFD *fds;
int fdCapacity;
int fdCount;
laikaM_newVector(PollFD, fds);
#endif
int reventCap;
int reventCount;
int outCap;
int outCount;
};
void laikaP_initPList(struct sLaika_pollList *pList);