| 2277 | } |
| 2278 | |
| 2279 | void ggml_free(struct ggml_context * ctx) { |
| 2280 | // make this function thread safe |
| 2281 | ggml_critical_section_start(); |
| 2282 | |
| 2283 | bool found = false; |
| 2284 | |
| 2285 | for (int i = 0; i < GGML_MAX_CONTEXTS; i++) { |
| 2286 | if (&g_state.contexts[i].context == ctx) { |
| 2287 | g_state.contexts[i].used = false; |
| 2288 | |
| 2289 | GGML_PRINT_DEBUG("%s: context %d has been freed. memory used = %zu\n", |
| 2290 | __func__, i, ggml_used_mem(ctx)); |
| 2291 | |
| 2292 | if (ctx->mem_buffer_owned) { |
| 2293 | GGML_ALIGNED_FREE(ctx->mem_buffer); |
| 2294 | } |
| 2295 | |
| 2296 | found = true; |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | if (!found) { |
| 2302 | GGML_PRINT_DEBUG("%s: context not found\n", __func__); |
| 2303 | } |
| 2304 | |
| 2305 | ggml_critical_section_end(); |
| 2306 | } |
| 2307 | |
| 2308 | size_t ggml_used_mem(const struct ggml_context * ctx) { |
| 2309 | return ctx->objects_end == NULL ? 0 : ctx->objects_end->offs + ctx->objects_end->size; |