| HeapWalk | Function (ROM Call 0x12C)

AMS 2.00 or higher | alloc.h

short HeapWalk (short function);

Verifies and dumps the contents of the heap.

HeapWall looks through the heap to verify it is valid. Then it prints the status of the heap, prints the size of each heap block and its handle, or prints the symbol table, according to the value of function. HeapWalk uses LIO_SendData to send the output through the link port.
The valid values for function, defined in the enum HeapWalkCmds, are:

H_WALK_VERIFY | The function just verifies the heap, and outputs nothing to the link port.
H_WALK_STATUS | The function outputs the total free space, maximum size of a free block, number of used and free blocks, and the number of locked blocks.
H_WALK_DUMP | The function outputs the heap status and the size of the heap block for each handle.
H_WALK_SYM | AMS 2.04 or higher: The function outputs the entire VAT.

HeapWalk called with function as H_WALK_VERIFY, H_WALK_STATUS or H_WALK_DUMP, returns TRUE if the heap is valid, FALSE otherwise.
HeapWalk(H_WALK_SYM) executes this block of code after verifying the heap. The example "List variables and folders" works on any AMS version, unlike HeapWalk(H_WALK_SYM):

    // Sends the list of all variables and folders through the link port.
    // This program does what HeapWalk(H_WALK_SYM); does on AMS 2.04 and
    // later, but also works on any AMS version.

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define SAVE_SCREEN           // Save/Restore LCD Contents
    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define NO_CALC_DETECT
    #define ENABLE_ERROR_RETURN
    #define MIN_AMS 100           // Compile for AMS 1.00 or higher

    #include <tigcclib.h>         // Include All Header Files

    void _main(void)
    {
    SYM_ENTRY *symptr;
    unsigned char buffer[256];
    #ifdef SAVE_SYMPG // Saving the SymPG isn't necessary in _main, nobody else does it.
    SymPG_S save;
    TRY
      memcpy(&save, pSymPG, sizeof(SymPG_S));
    #endif
      if ((symptr = SymFindFirst(NULL,2)) != NULL)
      {
        strcpy(buffer, "\r\nName/Flags/hVal (dec)\r\n");
        LIO_SendData(buffer, strlen((char*)buffer));
        do
        {
          short flags = symptr->flags.flags_n;
          if ((flags&SF_FOLDER))
            sprintf((char *)buffer, "FOLDER: %-8s   %04X  %hd\r\n", symptr->name,
            flags, symptr->handle);
          else
            sprintf((char *)buffer, "%8s\\%-8s %04X  %hd\r\n", SymFindFolderName(),
            symptr->name, flags, symptr->handle);
          LIO_SendData(buffer, strlen((char *)buffer));
          symptr = SymFindNext();
        } while (symptr != NULL);
      }
    #ifdef SAVE_SYMPG // See above.
    FINALLY
      memcpy(pSymPG, &save, sizeof(SymPG_S));
    ENDFINAL
    #endif
    }

Uses: ER_catch, LIO_SendData, memcpy, sprintf, strlen, pSymPG, SymFindFirst, SymFindFolderName, SymFindNext

See also: HeapShuffle, vat.h, SymFindFolderName, pSymPG, SymPG_S