diff options
Diffstat (limited to 'src/survive.c')
-rw-r--r-- | src/survive.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/survive.c b/src/survive.c index 362adbc..bb1179e 100644 --- a/src/survive.c +++ b/src/survive.c @@ -404,6 +404,31 @@ int survive_add_object(SurviveContext *ctx, SurviveObject *obj) { return 0; } +void survive_remove_object(SurviveContext *ctx, SurviveObject *obj) { + int obj_idx = 0; + for (obj_idx = 0; obj_idx < ctx->objs_ct; obj_idx++) { + if (ctx->objs[obj_idx] == obj) + break; + } + + if (obj_idx == ctx->objs_ct) { + SV_INFO("Warning: Tried to remove un-added object %p(%s)", obj, obj->codename); + return; + } + + // Swap the last item into this items slot; this assumes order doesn't matter in this list + if (obj_idx != ctx->objs_ct - 1) { + ctx->objs[obj_idx] = ctx->objs[ctx->objs_ct - 1]; + } + + ctx->objs_ct--; + + // Blank out the spot; but this is only really necessary for diagnostic reasons -- presumably no one will ever read + // past the end of the list + ctx->objs[ctx->objs_ct] = 0; + + free(obj); +} void survive_add_driver(SurviveContext *ctx, void *payload, DeviceDriverCb poll, DeviceDriverCb close, DeviceDriverMagicCb magic) { int oldct = ctx->driver_ct; |