/* ---------------------------------------------------------------------
   (c) ED 2003
   Project      : CLIB
   Function     : Resource Allocator (debug helpers)
   Module       : RA
   File         : ra_dbg.c
   Created      : 25-09-2003
   Modified     : 25-09-2003
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   25-09-2003 Created
   25-09-2003 Initial version

   --------------------------------------------------------------------- */

#ifdef __cplusplus
#error This is not C++. Please use a C compiler.
#endif

#include "ed/inc/ra_dbg.h"

/* macros ============================================================== */
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private data ======================================================== */
/* private functions =================================================== */
/* internal public data ================================================ */
/* internal public functions =========================================== */
/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   ra_dbg_serr ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *ra_dbg_serr (ra_err_e err)
{
   char const *s = NULL;
   static char const *const as[] =
   {
      "OK",
#define ITEM(a, b) \
   b,
#include "ed/inc/ra_err.itm"
#undef ITEM

   };

   if (err < RA_ERR_NB)
   {
      s = as[err];
   }
   return s;
}

/* ---------------------------------------------------------------------
   ra_dbg_print_stat ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void ra_dbg_print_stat (ra_s const *this)
{
   printf ("\n");
   printf ("'%s' allocator status\n", this->sid ? this->sid : "");
   printf (" nb_res = %3u\n", (uint) this->nb_res);
   printf ("  count = %3u\n", (uint) this->count);
   printf ("\n");

   if (this->err.alloc)
   {
      printf ("err.alloc = %8lu\n", (ulong) this->err.alloc);
   }

   if (this->err.free)
   {
      printf ("err.free  = %8lu\n", (ulong) this->err.free);
   }

   {
      uint unfreed = this->nb_res - this->count;

      if (unfreed == 0)
      {
         printf ("OK\n");
      }
      else
      {
         printf ("unfreed = %3u\n", (uint) unfreed);
         {
            size_t i;

            printf ("Index Ressource Data\n");
            for (i = 0; i < this->nb_res; i++)
            {
               ra_usr_s const *const p = this->p_res + i;

               if (p->used)
               {
                  printf ("[%3u]       %3u  %3u\n"
                          ,(uint) i
                          ,(uint) p->res
                          ,(uint) p->data);
               }
            }
         }
         printf ("*ERROR*\n");
      }
   }
   printf ("\n");
}

/* ---------------------------------------------------------------------
   ra_dbg_print_usage ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void ra_dbg_print_usage (ra_s * this)
{
   printf ("'%s' allocator usage report = %u/%u\n"
           ,this->sid ? this->sid : ""
           ,(uint) (this->nb_res - this->min_count)
           ,(uint) this->nb_res
      );
}

/* public data ========================================================= */
