From 468c5a136a0c458bfe7df262027ebf8c79b19558 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sat, 15 Nov 2025 00:19:05 -0600 Subject: add removal test and const-qualify data --- main.c | 12 ++++++++++-- pipeline.c | 19 ++++++++++++------- pipeline.h | 8 ++++---- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index be91332..111424c 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ #define UNUSED(_v) ((void)(_v)) -PIPELINE_STAGE int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, void *data, size_t sz) +PIPELINE_STAGE int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) { return ptx_pipeline_ctx_next(ctx, data, sz); } @@ -31,7 +31,7 @@ PIPELINE_CLEANUP void debug_stage_cleanup(const char *name, void *puser) printf("Debug stage %s (cleanup): %p\n", name, puser); } -PIPELINE_STAGE int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, void *data, size_t sz) +PIPELINE_STAGE int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) { bool cont = !!(uintptr_t)(*ptx_pipeline_ctx_get_user(ctx)); printf("Debug stage %s:\n Data: %p\n Size: %zu\n", ptx_pipeline_ctx_get_name(ctx), data, sz); @@ -111,6 +111,14 @@ int main(void) { } printf("Handle returned: %d\n", ptx_pipeline_handle(pl, "sussy", 10)); + + if (ptx_pipeline_remove_stage(pl, "debug1") < 1) { + fputs("Failed to remove debug1 from pipeline\n", stderr); + return 1; + } + + printf("Handle returned: %d\n", ptx_pipeline_handle(pl, "sussy2", 20)); + ptx_pipeline_free(pl); return 0; diff --git a/pipeline.c b/pipeline.c index bead598..a5ddbf2 100644 --- a/pipeline.c +++ b/pipeline.c @@ -26,6 +26,10 @@ struct ptx__pipeline_tag bool handling; }; +/* returns: + * -1 : allocation failed + * 0 : nothing was allocated + * 1 : pipeline grown */ static int ptx__pipeline_reserve(ptx_pipeline_t *pl, size_t req) { assert(pl); @@ -151,7 +155,8 @@ static ssize_t ptx__pipeline_find_stage(ptx_pipeline_t *ptx, const char *name) for (size_t idx = 0; idx < ptx->stages_len; ++idx) { if (!strcmp(ptx->stages[idx].name, name)) { - return idx; + assert(idx <= SSIZE_MAX); + return (ssize_t)idx; } } @@ -181,7 +186,7 @@ static int ptx__pipeline_add_at( } /* finally, put the stage into the array */ - memmove(pipeline->stages + idx, &temp_stage, sizeof(temp_stage)); + memcpy(pipeline->stages + idx, &temp_stage, sizeof(temp_stage)); return 0; } @@ -203,7 +208,7 @@ int ptx_pipeline_add_after( ssize_t sidx = ptx__pipeline_find_stage(pipeline, after); if (sidx < 0) return 0; - idx = sidx + 1; /* after */ + idx = (size_t)(sidx + 1); /* after */ } else { idx = pipeline->stages_len; } @@ -235,7 +240,7 @@ int ptx_pipeline_add_before( ssize_t sidx = ptx__pipeline_find_stage(pipeline, before); if (sidx < 0) return 0; - idx = sidx; /* before */ + idx = (size_t)sidx; /* before */ } else { idx = 0; } @@ -259,7 +264,7 @@ int ptx_pipeline_remove_stage(ptx_pipeline_t *pipeline, const char *name) if (idx < 0) return 0; ptx__cleanup_pipeline_stage(pipeline->stages + idx); - memmove(pipeline->stages + idx, pipeline->stages + idx + 1, sizeof(pipeline->stages[0]) * (pipeline->stages_len - idx)); + memmove(pipeline->stages + idx, pipeline->stages + idx + 1, sizeof(pipeline->stages[0]) * (pipeline->stages_len - (size_t)idx - 1)); --pipeline->stages_len; return 1; @@ -277,7 +282,7 @@ const char *ptx_pipeline_ctx_get_name(const ptx_pipeline_ctx_t *ctx) return ctx->pipeline->stages[ctx->curidx].name; } -int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, void *nextdata, size_t nextsize) +int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, const void *nextdata, size_t nextsize) { assert(ctx->curidx + 1 < ctx->pipeline->stages_len); @@ -290,7 +295,7 @@ int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, void *nextdata, size_t return (*new_ctx.pipeline->stages[new_ctx.curidx].impl.handler)(&new_ctx, nextdata, nextsize); } -int ptx_pipeline_handle(ptx_pipeline_t *pipeline, void *data, size_t sz) +int ptx_pipeline_handle(ptx_pipeline_t *pipeline, const void *data, size_t sz) { assert(pipeline); assert(!pipeline->handling); diff --git a/pipeline.h b/pipeline.h index 562f1df..c0feb56 100644 --- a/pipeline.h +++ b/pipeline.h @@ -14,7 +14,7 @@ typedef struct ptx__pipeline_ctx_tag ptx_pipeline_ctx_t; typedef struct ptx__pipeline_tag ptx_pipeline_t; -typedef int (ptx_pipeline_proc_t)(const ptx_pipeline_ctx_t * /*ctx*/, void * /*data*/, size_t /*len*/) PIPELINE_STAGE; +typedef int (ptx_pipeline_proc_t)(const ptx_pipeline_ctx_t * /*ctx*/, const void * /*data*/, size_t /*len*/) PIPELINE_STAGE; typedef int (ptx_pipeline_init_proc_t)(const char * /*name*/, void ** /*ppuser*/, va_list /*init_args*/) PIPELINE_INIT; typedef void (ptx_pipeline_cleanup_proc_t)(const char * /*name*/, void * /*puser*/) PIPELINE_CLEANUP; @@ -30,7 +30,7 @@ ptx_pipeline_t *ptx_pipeline_new(size_t initial_size) ATTR_WUR ATTR_MALLOC() ATT /* frees an existing pipeline stage. note that this will call the cleanup function in the stages as well. */ void ptx_pipeline_free(ptx_pipeline_t *pl); -/* adds `stage' with `name' to `pipeline' after `after' * +/* adds `stage' with `name' to `pipeline' after `after' * returns: number of stages added to pipeline (0 or 1), or -1 if there was an allocation or initialization error. */ int ptx_pipeline_add_after(ptx_pipeline_t *pipeline, const struct ptx_pipeline_stage_funcs *stage, const char *name, const char *after, ...); /* adds `stage' with `name' to `pipeline' before `before' @@ -47,10 +47,10 @@ int ptx_pipeline_remove_stage(ptx_pipeline_t *pipeline, const char *name); void **ptx_pipeline_ctx_get_user(const ptx_pipeline_ctx_t *ctx); const char *ptx_pipeline_ctx_get_name(const ptx_pipeline_ctx_t *ctx); -int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, void *nextdata, size_t nextsize); +int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, const void *nextdata, size_t nextsize); void ptx_pipeline_ctx_diag(const ptx_pipeline_ctx_t *ctx, int lvl, const char *fmt, ...) ATTR_FORMAT((printf, 3, 4)); /* TODO: pipeline stages should be able to send diagnostic messages */ -int ptx_pipeline_handle(ptx_pipeline_t *pipeline, void *data, size_t sz); /* TODO: diagnostic */ +int ptx_pipeline_handle(ptx_pipeline_t *pipeline, const void *data, size_t sz); /* TODO: diagnostic */ #endif /* include guard */ -- cgit v1.2.3-70-g09d2