Browse Source

fixed memory manager summation bug, freed previously unfreed memory

IverMartinson 5 months ago
parent
commit
a737c2e13a

BIN
build/librasteriver.so


BIN
build/main.bin


+ 1 - 0
src/headers/custom_types.h

@@ -114,6 +114,7 @@ typedef struct {
     int reallocated_alloc;
     int freed;
     int allocated;
+    int line;
 } RI_memory_allocation;
 
 #endif

+ 1 - 1
src/headers/functions.h

@@ -7,7 +7,7 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_objects, char **filenames,
 RI_actor* RI_request_actors(int RI_number_of_requested_actors); // Load texture file(s)
 RI_material* RI_request_materials(int RI_number_of_requested_materials); // Create materials
 RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_creation_data *texture_creation_data); // Create new actor(s)
-RI_scene* RI_request_scene();
+RI_scene* RI_request_scenes(int RI_number_of_requested_scenes);
 int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title); // Initialize RasterIver
 int RI_stop(int result); // Stop RasterIver safely and free memory
 int RI_render(RI_scene *scene, RI_texture *target_texture); // Render a scene to a texture

+ 2 - 7
src/headers/rasteriver.h

@@ -23,17 +23,11 @@ typedef struct {
     SDL_Event event;
     
     // RasterIver
-    RI_vector_3f *loaded_mesh_vetex_positions; // original vertex positions from a loaded mesh
-    RI_vector_3f *normals; // original normal vectors from a loaded mesh
-    RI_vector_2f *uvs; // UV coords from a loaded mesh
-    
-    RI_vector_3f *transformed_vertex_positions; // vertex positions after trasformations (mesh & camera rotation & position changes, scale, etc)
-    RI_vector_3f *transformed_normals; // normal vectors after rotation transformations (otherwise a normal's space would constantly be in local space instead of world space)
-
     RI_mesh *loaded_meshes;
     RI_texture *loaded_textures;
     RI_material *materials;
     RI_actor *actors;
+    RI_scene *scenes;
 
     RI_texture error_texture;
     RI_texture error_bump_map;
@@ -46,6 +40,7 @@ typedef struct {
     int loaded_texture_count;
     int material_count;
     int actor_count;
+    int scene_count;
     int running;
     int frame;
     char* prefix;

+ 3 - 1
src/launch_program/main.c

@@ -19,7 +19,9 @@ int main(){
     RI_texture* textures = RI_request_textures(3, texture_creation_info);
     RI_material* materials = RI_request_materials(4);
     RI_actor* actors = RI_request_actors(4);
-    RI_scene* scene = RI_request_scene();
+    RI_scene* scenes = RI_request_scenes(1);
+
+    RI_scene* scene = &scenes[0];
 
     // meshes
     RI_mesh* unit_plane_mesh = &meshes[3];

+ 56 - 19
src/library/rasteriver.c

@@ -68,6 +68,7 @@ void* written_RI_realloc(void *__ptr, size_t __size, const char *caller, int lin
         ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 1;
         ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
         ri.allocation_table[ri.current_allocation_index].freed = 0;
+        ri.allocation_table[ri.current_allocation_index].line = line;
         ri.allocation_table[ri.current_allocation_index].pointer = pointer;        
         ri.allocation_table[ri.current_allocation_index].size = __size;
 
@@ -94,6 +95,7 @@ void* written_RI_malloc(size_t __size, const char *caller, int line){
         ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
         ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
         ri.allocation_table[ri.current_allocation_index].freed = 0;
+        ri.allocation_table[ri.current_allocation_index].line = line;
         ri.allocation_table[ri.current_allocation_index].pointer = pointer;        
         ri.allocation_table[ri.current_allocation_index].size = __size;
 
@@ -120,6 +122,7 @@ void* written_RI_calloc(size_t __nmemb, size_t __size, const char *caller, int l
         ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
         ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
         ri.allocation_table[ri.current_allocation_index].freed = 0;
+        ri.allocation_table[ri.current_allocation_index].line = line;
         ri.allocation_table[ri.current_allocation_index].pointer = pointer;        
         ri.allocation_table[ri.current_allocation_index].size = __size * __nmemb;
             
@@ -151,7 +154,7 @@ void written_RI_free(void *__ptr, const char *caller, int line){
             }
         }
         
-        debug("[Memory Manager] Freed %zu bytes  (func \"%s\":%d)", size, caller, line);
+        debug("[Memory Manager] Freed %zu bytes (func \"%s\":%d)", size, caller, line);
     }
  
     free(__ptr);
@@ -171,14 +174,23 @@ int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *ac
     return 0;
 }
 
-RI_scene* RI_request_scene(){
-    RI_scene* new_scene = RI_malloc(sizeof(RI_scene));
-
-    new_scene->actor_count = 0;
-    new_scene->actors = NULL;
-    new_scene->faces_to_render = NULL;
+RI_scene* RI_request_scenes(int RI_number_of_requested_scenes){
+    int previous_scene_count = ri.scene_count;
+    ri.scene_count += RI_number_of_requested_scenes;
     
-    return new_scene;
+    ri.scenes = RI_realloc(ri.scenes, sizeof(RI_scene) * ri.scene_count);
+
+    for (int i = 0; i < RI_number_of_requested_scenes; ++i){
+        RI_scene new_scene = {0};
+
+        new_scene.actor_count = 0;
+        new_scene.actors = NULL;
+        new_scene.faces_to_render = NULL;
+        
+        ri.scenes[i + previous_scene_count] = new_scene;
+    }
+
+    return ri.scenes;
 }
 
 RI_actor* RI_request_actors(int RI_number_of_requested_actors){
@@ -265,7 +277,7 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames,
         FILE *file = fopen(filenames[i], "r");
 
         if (!file){
-            debug("Error! File \"%s\" not found", filenames[i]);
+            debug("[Mesh Loader] Error! File \"%s\" not found", filenames[i]);
             RI_stop(1);
         }
         
@@ -401,7 +413,7 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames,
         else if (!has_normals && has_uvs) loading_mesh_notice_string = "UVs";
         else if (!has_normals && !has_uvs) loading_mesh_notice_string = "normals and UVs";
         
-        if (!has_normals || !has_uvs) debug("Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
+        if (!has_normals || !has_uvs) debug("[Mesh Loader] Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
         
         new_mesh_data_struct.has_normals = has_normals;
         new_mesh_data_struct.has_uvs = has_uvs;
@@ -411,7 +423,7 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames,
         if (!RI_return_just_mesh) {
             ri.loaded_meshes[meshes_already_loaded_count + i] = new_mesh_data_struct;   
 
-            debug("Loaded mesh \"%s\"! %d faces, %d verticies, %d normals, %d uvs", filenames[i], current_face_index, current_vertex_index, current_normal_index, current_uv_index); 
+            debug("[Mesh Loader] Loaded mesh \"%s\"! %d faces, %d verticies, %d normals, %d uvs", filenames[i], current_face_index, current_vertex_index, current_normal_index, current_uv_index); 
         }
         else {
             *mesh = new_mesh_data_struct;
@@ -990,16 +1002,40 @@ int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
 }
 
 int RI_stop(int result){
-    debug("Stopping...");
+    debug("[Notice] Stopping...");
     
+    for (int scene_index = 0; scene_index < ri.scene_count; ++scene_index){
+        RI_free(ri.scenes[scene_index].faces_to_render);
+        RI_free(ri.scenes[scene_index].actors);
+    }
+
     for (int mesh_index = 0; mesh_index < ri.loaded_mesh_count; ++mesh_index){
-        RI_free(ri.loaded_meshes[mesh_index].faces); // free face array
+        RI_free(ri.loaded_meshes[mesh_index].faces);
+        RI_free(ri.loaded_meshes[mesh_index].vertex_positions);
+        RI_free(ri.loaded_meshes[mesh_index].normals);
+        RI_free(ri.loaded_meshes[mesh_index].uvs);
     }
 
     for (int texture_index = 0; texture_index < ri.loaded_texture_count; ++texture_index){
         RI_free(ri.loaded_textures[texture_index].image_buffer);
     }
 
+    RI_free(ri.loaded_meshes);
+    RI_free(ri.loaded_textures);
+    RI_free(ri.materials);
+    RI_free(ri.actors);
+    RI_free(ri.scenes);
+
+    RI_free(ri.error_texture.image_buffer);
+    RI_free(ri.frame_buffer->image_buffer);
+    RI_free(ri.frame_buffer);
+    RI_free(ri.z_buffer);
+
+    RI_free(ri.error_mesh.faces);
+    RI_free(ri.error_mesh.vertex_positions);
+    RI_free(ri.error_mesh.normals);
+    RI_free(ri.error_mesh.uvs);
+
     if (ri.debug_memory){
         size_t total_allocated = 0;
         size_t allocated = 0;
@@ -1010,13 +1046,14 @@ int RI_stop(int result){
 
         for (int i = 1; i < ri.allocation_table_length; ++i) {
             if (ri.allocation_table[i].allocated != 1) continue;
+            else if (ri.allocation_table[i].freed)
+                freed += ri.allocation_table[i].size;
+                else debug("[Memory Manager] Memory allocated at line %d wasn't freed (%zu bytes)", ri.allocation_table[i].line, ri.allocation_table[i].size);
             
-            if (!ri.allocation_table[i].reallocated_free && !ri.allocation_table[i].freed && !ri.allocation_table[i].reallocated_alloc)
+            if (!ri.allocation_table[i].reallocated_free && !ri.allocation_table[i].reallocated_alloc)
                 allocated += ri.allocation_table[i].size;
             else if (ri.allocation_table[i].reallocated_alloc)
                 alloc_realloc += ri.allocation_table[i].size;
-            else if (ri.allocation_table[i].freed)
-                freed += ri.allocation_table[i].size;
             else if (ri.allocation_table[i].reallocated_free)
                 reallocated += ri.allocation_table[i].size;
         }
@@ -1024,11 +1061,11 @@ int RI_stop(int result){
         total_allocated = allocated + alloc_realloc;
         total_freed = freed + reallocated;
 
-        debug("[Memory Manager] [Total Bytes Allocated] M(c)alloc & Realloc(): %zu -- M(c)alloc(): %zu -- Realloc(): %zu: %zu", total_allocated, allocated, alloc_realloc);
+        debug("[Memory Manager] [Total Bytes Allocated] M(c)alloc & Realloc(): %zu -- M(c)alloc(): %zu -- Realloc(): %zu", total_allocated, allocated, alloc_realloc);
         debug("[Memory Manager] [Total Bytes Freed] Free() & Realloc(): %zu -- Free(): %zu -- Realloc(): %zu", total_freed, freed, reallocated);
 
-        if (allocated != freed){
-            debug("[Memory Manager] %zu bytes not freed", (allocated + alloc_realloc) - freed);
+        if (total_allocated != total_freed){
+            debug("[Memory Manager] %zu bytes not freed", total_allocated - total_freed);
         }
     
         debug("[Memory Manager] Freeing allocation table...");