various: log memory events

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a69643b6d00277bb9bcfeb4cd01dc78d7cd3d
This commit is contained in:
raf 2025-09-30 20:11:06 +03:00
commit bc77b887ad
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
7 changed files with 280 additions and 47 deletions

View file

@ -1,7 +1,6 @@
#define STB_IMAGE_IMPLEMENTATION
#include "../include/stb_image.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -79,10 +78,13 @@ int chroma_image_load(chroma_image_t *image, const char *path) {
image->loaded = true;
// Calculate and log memory allocation
size_t image_size = (size_t)image->width * image->height * image->channels;
chroma_log_resource_allocation("image_data", image_size, path);
chroma_log("INFO", "Loaded image: %s (%dx%d, %d channels, %.2f MB)", path,
image->width, image->height, image->channels,
(double)(image->width * image->height * image->channels) /
(1024.0 * 1024.0));
(double)image_size / (1024.0 * 1024.0));
return CHROMA_OK;
}
@ -94,6 +96,10 @@ void chroma_image_free(chroma_image_t *image) {
}
if (image->data) {
// Log memory deallocation before freeing
size_t image_size = (size_t)image->width * image->height * image->channels;
chroma_log_resource_deallocation("image_data", image_size, image->path);
// Always use stbi_image_free since we load directly with stbi_load
stbi_image_free(image->data);
image->data = NULL;
@ -218,12 +224,15 @@ void chroma_images_cleanup(chroma_state_t *state) {
return;
}
chroma_log("DEBUG", "Cleaning up %d images", state->image_count);
for (int i = 0; i < state->image_count; i++) {
chroma_image_free(&state->images[i]);
}
state->image_count = 0;
chroma_log("INFO", "Cleaned up all images");
chroma_log_memory_stats("post-image-cleanup");
}
// Preload common image formats for validation