Mark an object as freed in the auto release queue, so that users can still * free things manually if they want. * * The function returns 1 if the object was actually found in the auto memory * pool, otherwise 0 is returned. */
| 1059 | * The function returns 1 if the object was actually found in the auto memory |
| 1060 | * pool, otherwise 0 is returned. */ |
| 1061 | int autoMemoryFreed(RedisModuleCtx *ctx, int type, void *ptr) { |
| 1062 | if (!(ctx->flags & REDISMODULE_CTX_AUTO_MEMORY)) return 0; |
| 1063 | |
| 1064 | int count = (ctx->amqueue_used+1)/2; |
| 1065 | for (int j = 0; j < count; j++) { |
| 1066 | for (int side = 0; side < 2; side++) { |
| 1067 | /* For side = 0 check right side of the array, for |
| 1068 | * side = 1 check the left side instead (zig-zag scanning). */ |
| 1069 | int i = (side == 0) ? (ctx->amqueue_used - 1 - j) : j; |
| 1070 | if (ctx->amqueue[i].type == type && |
| 1071 | ctx->amqueue[i].ptr == ptr) |
| 1072 | { |
| 1073 | ctx->amqueue[i].type = REDISMODULE_AM_FREED; |
| 1074 | |
| 1075 | /* Switch the freed element and the last element, to avoid growing |
| 1076 | * the queue unnecessarily if we allocate/free in a loop */ |
| 1077 | if (i != ctx->amqueue_used-1) { |
| 1078 | ctx->amqueue[i] = ctx->amqueue[ctx->amqueue_used-1]; |
| 1079 | } |
| 1080 | |
| 1081 | /* Reduce the size of the queue because we either moved the top |
| 1082 | * element elsewhere or freed it */ |
| 1083 | ctx->amqueue_used--; |
| 1084 | return 1; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | /* Release all the objects in queue. */ |
| 1092 | void autoMemoryCollect(RedisModuleCtx *ctx) { |
no outgoing calls
no test coverage detected