diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2024-11-01 09:33:00 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2024-11-01 09:33:00 -0400 |
| commit | 2f4944595da6011e15ba2a65637c916665bc95e0 (patch) | |
| tree | b062b7a9aac717f76f33dd9c8a40aef03e1bae41 | |
| parent | e4d71f20517488dd48cec43a870af496b0f90c5b (diff) | |
pkev: first working version
This is a 'grug' implementation.
Use the following defines to control size:
PK_EV_MAX_COUNT = 16
PK_EV_MAX_CB_COUNT = 8
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | pkev.h | 54 |
2 files changed, 48 insertions, 17 deletions
@@ -19,6 +19,8 @@ SRC = \ test/pkmem.cpp \ test/pkstr.c \ test/pkstr.cpp \ + test/pkev.c \ + test/pkev.cpp \ OBJ = $(SRC:%.c=.o) PPOBJ = $(SRC:%.cpp=.so) @@ -112,11 +114,18 @@ test-pkstr: test/pkstr.o test-pkstr-cpp: test/pkstr.so $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test-pkev: test/pkev.o + $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) + +test-pkev-cpp: test/pkev.so + $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) + test: pkmacros pkmem-types pkmem pkstr pkev test: test-pkmacros test-pkmacros-cpp test: test-pkmem-types test-pkmem-types-cpp test: test-pkmem test-pkmem-cpp test: test-pkstr test-pkstr-cpp +test: test-pkev test-pkev-cpp test: @echo "" ./test/test-pkmacros ; echo Result: $$? "\n" @@ -127,6 +136,8 @@ test: ./test/test-pkmem-cpp ; echo Result: $$? "\n" ./test/test-pkstr ; echo Result: $$? "\n" ./test/test-pkstr-cpp ; echo Result: $$? "\n" + ./test/test-pkev ; echo Result: $$? "\n" + ./test/test-pkev-cpp ; echo Result: $$? "\n" clean: rm -f *.plist *.gch *.gchpp *.o *.so test/*.o test/*.so test/test-* @@ -9,16 +9,17 @@ typedef uint64_t pk_ev_id_T; struct pk_ev_mgr *pk_ev_create(); void pk_ev_destroy(struct pk_ev_mgr *evmgr); -typedef void (*pk_ev_cb)(); +typedef void (pk_ev_cb)(); -void pk_ev_register_ev(struct pk_ev_mgr *evmgr, pk_ev_id_T evid); -void pk_ev_register_cb(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, pk_ev_cb *cb); -void pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, ...); +bool pk_ev_register_ev(struct pk_ev_mgr *evmgr, pk_ev_id_T evid); +bool pk_ev_register_cb(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, pk_ev_cb *cb); +void pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid); #endif /* PK_EV_H */ #ifdef PK_IMPL_EV +#include <stdio.h> #include <assert.h> #include <stdlib.h> #include <string.h> @@ -27,14 +28,18 @@ void pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, ...); # define PK_EV_MAX_COUNT 16 #endif +#ifndef PK_EV_MAX_CB_COUNT +# define PK_EV_MAX_CB_COUNT 8 +#endif + struct pk_ev { pk_ev_id_T evid; - pk_ev_cb **cb; + pk_ev_cb *cb[PK_EV_MAX_CB_COUNT]; uint8_t n_cb; }; struct pk_ev_mgr { - struct pk_ev *ev; + struct pk_ev ev[PK_EV_MAX_COUNT]; uint8_t n_ev; }; @@ -44,36 +49,51 @@ pk_ev_create() struct pk_ev_mgr *mgr = (struct pk_ev_mgr*)malloc(sizeof(struct pk_ev_mgr)); if (mgr == NULL) return NULL; memset(mgr, 0, sizeof(struct pk_ev_mgr)); - mgr->ev = (struct pk_ev*)malloc(sizeof(struct pk_ev) * PK_EV_MAX_COUNT); - if (mgr->ev == NULL) return NULL; - memset(mgr->ev, 0, sizeof(struct pk_ev) * PK_EV_MAX_COUNT); - mgr->n_ev = 0; return mgr; } inline void pk_ev_destroy(struct pk_ev_mgr *evmgr) { - memset(evmgr->ev, 0, sizeof(struct pk_ev) * PK_EV_MAX_COUNT); - free(evmgr->ev); + assert(evmgr != NULL); memset(evmgr, 0, sizeof(struct pk_ev_mgr)); free(evmgr); } -inline void +inline bool pk_ev_register_ev(struct pk_ev_mgr *evmgr, pk_ev_id_T evid) { - // TODO + assert(evmgr != NULL); + int i; + for (i = 0; i < evmgr->n_ev; ++i) { + if (evmgr->ev[i].evid == evid) { + return false; + } + } + evmgr->ev[evmgr->n_ev].evid = evid; + evmgr->n_ev++; + return true; } -inline void +inline bool pk_ev_register_cb(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, pk_ev_cb *cb) { - // TODO + assert(evmgr != NULL); + struct pk_ev *ev = {0}; + int i; + for (i = 0; i < evmgr->n_ev; ++i) { + if (evmgr->ev[i].evid == evid) { + ev = &evmgr->ev[i]; + } + } + if (ev == NULL) return false; + ev->cb[ev->n_cb] = cb; + ev->n_cb++; + return true; } inline void -pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, ...) +pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid) { assert(evmgr != NULL); uint8_t i; |
