This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 3e371e8cb57c669cd0adff8acfedaaa16e0f2308
Author: Matteo Golin <[email protected]>
AuthorDate: Sat Oct 17 06:53:52 2026 -0400

    games/NXDoom: add debug option menu
    
    Allow debug options to be enabled/disabled for NXDoom to aid in the
    optimization process. Currently adds memory allocator profiling.
    
    Signed-off-by: Matteo Golin <[email protected]>
---
 games/NXDoom/Kconfig               |   2 +
 games/NXDoom/Kconfig.debug         |  23 ++++++++
 games/NXDoom/Kconfig.optimizations |   7 ---
 games/NXDoom/src/z_native.c        |  88 +++++++++++++++---------------
 games/NXDoom/src/z_zone.c          | 109 +++++++++++++++++++++++++++++--------
 5 files changed, 154 insertions(+), 75 deletions(-)

diff --git a/games/NXDoom/Kconfig b/games/NXDoom/Kconfig
index fcf3cdc69..f13b0d243 100644
--- a/games/NXDoom/Kconfig
+++ b/games/NXDoom/Kconfig
@@ -68,4 +68,6 @@ source "$APPSDIR/games/NXDoom/Kconfig.input"
 
 source "$APPSDIR/games/NXDoom/Kconfig.optimizations"
 
+source "$APPSDIR/games/NXDoom/Kconfig.debug"
+
 endif # GAMES_NXDOOM
diff --git a/games/NXDoom/Kconfig.debug b/games/NXDoom/Kconfig.debug
new file mode 100644
index 000000000..f4025adbe
--- /dev/null
+++ b/games/NXDoom/Kconfig.debug
@@ -0,0 +1,23 @@
+menuconfig GAMES_NXDOOM_DEBUG
+       bool "Debug options"
+       default n
+       ---help---
+               Enables debugging options for the NXDoom port.
+       
+if GAMES_NXDOOM_DEBUG
+
+config GAMES_NXDOOM_RANGECHECK
+       bool "Perform range checks"
+       default y
+       ---help---
+               The ChocolateDOOM source includes the ability to optionally 
exclude
+               range checking on some array operations.
+
+config GAMES_NXDOOM_ALLOCPROF
+       bool "Profile allocator"
+       default n
+       ---help---
+               Print the running tally of allocated memory for NXDoom to the 
console as
+               allocations are made.
+
+endif # GAMES_NXDOOM_DEBUG
diff --git a/games/NXDoom/Kconfig.optimizations 
b/games/NXDoom/Kconfig.optimizations
index ccace20b5..ee1c89f1d 100644
--- a/games/NXDoom/Kconfig.optimizations
+++ b/games/NXDoom/Kconfig.optimizations
@@ -45,13 +45,6 @@ config GAMES_NXDOOM_STATDUMP_MAX_CAPTURES
        ---help---
                Number of diagnostic playtime-statistics capture slots.
 
-config GAMES_NXDOOM_RANGECHECK
-       bool "Perform range checks"
-       default y
-       ---help---
-               The ChocolateDOOM source includes the ability to optionally 
exclude
-               range checking on some array operations.
-
 config GAMES_NXDOOM_FB_CMAP
        bool "Draw through the frame buffer colour map"
        default n
diff --git a/games/NXDoom/src/z_native.c b/games/NXDoom/src/z_native.c
index 95ef76531..8ee4d2925 100644
--- a/games/NXDoom/src/z_native.c
+++ b/games/NXDoom/src/z_native.c
@@ -70,8 +70,8 @@ struct memblock_s
 
 static memblock_t *allocated_blocks[PU_NUM_TAGS];
 
-#ifdef TESTING
-static int test_malloced = 0;
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+static size_t g_allocated;
 #endif
 
 /****************************************************************************
@@ -210,43 +210,15 @@ static boolean clear_cache(int size)
  * Public Functions
  ****************************************************************************/
 
-#ifdef TESTING
-void *test_malloc(size_t size)
-{
-  int *result;
-
-  if (test_malloced + size > 2 * 1024 * 1024)
-    {
-      return NULL;
-    }
-
-  test_malloced += size;
-
-  result = malloc(size + sizeof(int));
-
-  *result = size;
-
-  return result + 1;
-}
-
-void test_free(void *data)
-{
-  int *i;
-
-  i = ((int *)data) - 1;
-
-  test_malloced -= *i;
-
-  free(i);
-}
-#endif /* #ifdef TESTING */
-
 /****************************************************************************
  * Name: z_init
  ****************************************************************************/
 
 void z_init(void)
 {
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated = 0;
+#endif
   memset(allocated_blocks, 0, sizeof(allocated_blocks));
   printf("zone memory: Using native C allocator.\n");
 }
@@ -273,6 +245,9 @@ void z_free(void *ptr)
       *block->user = NULL;
     }
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated -= block->size;
+#endif
   z_remove_block(block);
 
   /* Free back to system */
@@ -344,6 +319,10 @@ void *z_malloc(int size, int tag, void *user)
       *newblock->user = result;
     }
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated += size;
+  printf("Currently allocated: %zu bytes\n", g_allocated);
+#endif
   return result;
 }
 
@@ -404,8 +383,10 @@ void z_dump_heap(int lowtag, int hightag)
   for (block = mainzone->blocklist.next; ; block = block->next)
     {
       if (block->tag >= lowtag && block->tag <= hightag)
-        printf("block:%p    size:%7i    user:%p    tag:%3i\n", block,
-               block->size, block->user, block->tag);
+        {
+          printf("block:%p    size:%7i    user:%p    tag:%3i\n", block,
+                 block->size, block->user, block->tag);
+        }
 
       if (block->next == &mainzone->blocklist)
         {
@@ -415,13 +396,19 @@ void z_dump_heap(int lowtag, int hightag)
         }
 
       if ((byte *)block + block->size != (byte *)block->next)
-        printf("ERROR: block size does not touch the next block\n");
+        {
+          printf("ERROR: block size does not touch the next block\n");
+        }
 
       if (block->next->prev != block)
-        printf("ERROR: next block doesn't have proper back link\n");
+        {
+          printf("ERROR: next block doesn't have proper back link\n");
+        }
 
       if (block->tag == PU_FREE && block->next->tag == PU_FREE)
-        printf("ERROR: two consecutive free blocks\n");
+        {
+          printf("ERROR: two consecutive free blocks\n");
+        }
     }
 #endif
 }
@@ -451,13 +438,19 @@ void z_file_dump_heap(FILE *f)
         }
 
       if ((byte *)block + block->size != (byte *)block->next)
-        fprintf(f, "ERROR: block size does not touch the next block\n");
+        {
+          fprintf(f, "ERROR: block size does not touch the next block\n");
+        }
 
       if (block->next->prev != block)
-        fprintf(f, "ERROR: next block doesn't have proper back link\n");
+        {
+          fprintf(f, "ERROR: next block doesn't have proper back link\n");
+        }
 
       if (block->tag == PU_FREE && block->next->tag == PU_FREE)
-        fprintf(f, "ERROR: two consecutive free blocks\n");
+        {
+          fprintf(f, "ERROR: two consecutive free blocks\n");
+        }
     }
 #endif
 }
@@ -506,12 +499,17 @@ void z_change_tag2(void *ptr, int tag, const char *file, 
int line)
   block = (memblock_t *)((byte *)ptr - sizeof(memblock_t));
 
   if (block->id != ZONEID)
-    i_error("%s:%i: z_change_tag: block without a ZONEID!", file, line);
+    {
+      i_error("%s:%i: z_change_tag: block without a ZONEID!",
+              file, line);
+    }
 
   if (tag >= PU_PURGELEVEL && block->user == NULL)
-    i_error("%s:%i: z_change_tag: an owner is required "
-            "for purgeable blocks",
-            file, line);
+    {
+      i_error("%s:%i: z_change_tag: an owner is required "
+              "for purgeable blocks",
+              file, line);
+    }
 
   /* Remove the block from its current list, and rehook it into
    * its new list.
diff --git a/games/NXDoom/src/z_zone.c b/games/NXDoom/src/z_zone.c
index f5274e334..9727a4d2c 100644
--- a/games/NXDoom/src/z_zone.c
+++ b/games/NXDoom/src/z_zone.c
@@ -87,6 +87,10 @@ static memzone_t *mainzone;
 static boolean zero_on_free;
 static boolean scan_on_free;
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+static size_t g_allocated;
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -152,6 +156,10 @@ void z_init(void)
   memblock_t *block;
   int size;
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated = 0;
+#endif
+
   mainzone = (memzone_t *)i_zone_base(&size);
   mainzone->size = size;
 
@@ -196,7 +204,10 @@ void z_free(void *ptr)
 
   block = (memblock_t *)((byte *)ptr - sizeof(memblock_t));
 
-  if (block->id != ZONEID) i_error("z_free: freed a pointer without ZONEID");
+  if (block->id != ZONEID)
+    {
+      i_error("z_free: freed a pointer without ZONEID");
+    }
 
   if (block->tag != PU_FREE && block->user != NULL)
     {
@@ -209,6 +220,10 @@ void z_free(void *ptr)
   block->user = NULL;
   block->id = 0;
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated -= block->size;
+#endif
+
   /* If the -zonezero flag is provided, we zero out the block on free
    * to break code that depends on reading freed memory.
    */
@@ -233,7 +248,10 @@ void z_free(void *ptr)
       other->next = block->next;
       other->next->prev = other;
 
-      if (block == mainzone->rover) mainzone->rover = other;
+      if (block == mainzone->rover)
+        {
+          mainzone->rover = other;
+        }
 
       block = other;
     }
@@ -247,7 +265,10 @@ void z_free(void *ptr)
       block->next = other->next;
       block->next->prev = block;
 
-      if (other == mainzone->rover) mainzone->rover = block;
+      if (other == mainzone->rover)
+        {
+          mainzone->rover = block;
+        }
     }
 }
 
@@ -282,7 +303,10 @@ void *z_malloc(int size, int tag, void *user)
 
   base = mainzone->rover;
 
-  if (base->prev->tag == PU_FREE) base = base->prev;
+  if (base->prev->tag == PU_FREE)
+    {
+      base = base->prev;
+    }
 
   rover = base;
   start = base->prev;
@@ -345,7 +369,9 @@ void *z_malloc(int size, int tag, void *user)
     }
 
   if (user == NULL && tag >= PU_PURGELEVEL)
-    i_error("z_malloc: an owner is required for purgeable blocks");
+    {
+      i_error("z_malloc: an owner is required for purgeable blocks");
+    }
 
   base->user = user;
   base->tag = tag;
@@ -363,6 +389,10 @@ void *z_malloc(int size, int tag, void *user)
 
   base->id = ZONEID;
 
+#ifdef CONFIG_GAMES_NXDOOM_ALLOCPROF
+  g_allocated += size;
+  printf("Currently allocated: %zu bytes\n", g_allocated);
+#endif
   return result;
 }
 
@@ -384,10 +414,15 @@ void z_free_tags(int lowtag, int hightag)
 
       /* free block? */
 
-      if (block->tag == PU_FREE) continue;
+      if (block->tag == PU_FREE)
+        {
+          continue;
+        }
 
       if (block->tag >= lowtag && block->tag <= hightag)
-        z_free((byte *)block + sizeof(memblock_t));
+        {
+          z_free((byte *)block + sizeof(memblock_t));
+        }
     }
 }
 
@@ -410,8 +445,10 @@ void z_dump_heap(int lowtag, int hightag)
   for (block = mainzone->blocklist.next; ; block = block->next)
     {
       if (block->tag >= lowtag && block->tag <= hightag)
-        printf("block:%p    size:%7i    user:%p    tag:%3i\n", block,
-               block->size, block->user, block->tag);
+        {
+          printf("block:%p    size:%7i    user:%p    tag:%3i\n", block,
+                 block->size, block->user, block->tag);
+        }
 
       if (block->next == &mainzone->blocklist)
         {
@@ -419,13 +456,19 @@ void z_dump_heap(int lowtag, int hightag)
         }
 
       if ((byte *)block + block->size != (byte *)block->next)
-        printf("ERROR: block size does not touch the next block\n");
+        {
+          printf("ERROR: block size does not touch the next block\n");
+        }
 
       if (block->next->prev != block)
-        printf("ERROR: next block doesn't have proper back link\n");
+        {
+          printf("ERROR: next block doesn't have proper back link\n");
+        }
 
       if (block->tag == PU_FREE && block->next->tag == PU_FREE)
-        printf("ERROR: two consecutive free blocks\n");
+        {
+          printf("ERROR: two consecutive free blocks\n");
+        }
     }
 }
 
@@ -450,13 +493,19 @@ void z_file_dump_heap(FILE *f)
         }
 
       if ((byte *)block + block->size != (byte *)block->next)
-        fprintf(f, "ERROR: block size does not touch the next block\n");
+        {
+          fprintf(f, "ERROR: block size does not touch the next block\n");
+        }
 
       if (block->next->prev != block)
-        fprintf(f, "ERROR: next block doesn't have proper back link\n");
+        {
+          fprintf(f, "ERROR: next block doesn't have proper back link\n");
+        }
 
       if (block->tag == PU_FREE && block->next->tag == PU_FREE)
-        fprintf(f, "ERROR: two consecutive free blocks\n");
+        {
+          fprintf(f, "ERROR: two consecutive free blocks\n");
+        }
     }
 }
 
@@ -476,13 +525,21 @@ void z_check_heap(void)
         }
 
       if ((byte *)block + block->size != (byte *)block->next)
-        i_error("z_check_heap: block size does not touch the next block\n");
+        {
+          i_error(
+            "z_check_heap: block size does not touch the next block\n");
+        }
 
       if (block->next->prev != block)
-        i_error("z_check_heap: next block doesn't have proper back link\n");
+        {
+          i_error(
+            "z_check_heap: next block doesn't have proper back link\n");
+        }
 
       if (block->tag == PU_FREE && block->next->tag == PU_FREE)
-        i_error("z_check_heap: two consecutive free blocks\n");
+        {
+          i_error("z_check_heap: two consecutive free blocks\n");
+        }
     }
 }
 
@@ -497,12 +554,16 @@ void z_change_tag2(void *ptr, int tag, const char *file, 
int line)
   block = (memblock_t *)((byte *)ptr - sizeof(memblock_t));
 
   if (block->id != ZONEID)
-    i_error("%s:%i: z_change_tag: block without a ZONEID!", file, line);
+    {
+      i_error("%s:%i: z_change_tag: block without a ZONEID!", file, line);
+    }
 
   if (tag >= PU_PURGELEVEL && block->user == NULL)
-    i_error("%s:%i: z_change_tag: an owner is required "
-            "for purgeable blocks",
-            file, line);
+    {
+      i_error("%s:%i: z_change_tag: an owner is required "
+              "for purgeable blocks",
+              file, line);
+    }
 
   block->tag = tag;
 }
@@ -541,7 +602,9 @@ int z_free_memory(void)
        block = block->next)
     {
       if (block->tag == PU_FREE || block->tag >= PU_PURGELEVEL)
-        free += block->size;
+        {
+          free += block->size;
+        }
     }
 
   return free;

Reply via email to