Lhogho  0.0.027
Defines | Functions | Variables
options.c File Reference

Defines

#define CHECK(PARAM1, PARAM2, BODY)
 check parameter and execute code if it matches

Functions

void init_options (int argc, char *argv[])
void dump_statistics ()
 dump statistics
void finit_options ()
 finalizes the Options module
void output_compiler_name (int show_help)
 outputs the name of the compier
void set_options (int argc, char *argv[])
 sets options from command-line parameters
voidalloc (int size)
 allocates dynamic memory
voidreallocate (void *mem, int size)
voidxalloc (int size)
void dealloc (void *ptr)
 deallocates dynamic memory

Variables

int option_turned_on [OPTIONS_COUNT]
 Values for options.
atom_t option_texts [4][OPTIONS_COUNT]
 Texts for short options [0]-short [1]-long [2]-alternative [3]-description.
char * option_source_filename
 ASCII name of current source file.
chars_t option_source_filename_chars
 name of current source file
char * option_compiler_filename
 ASCII name of current compiler.
chars_t option_compiler_filename_chars
 name of current compiler
int standalone
 indicate using of standalone compiler (i.e. -x option)
atom_t standalone_arguments
 list of command-line arguments
char * id_names [MAX_ID]
 array of names for each atom type
int stats_free
 statistics for free atoms (of any type)
int stats_allocs
 statistics for usage of ALLOC and DEALLOC
stats_t stats [MAX_ID]
 statistics for each type of atoms

Define Documentation

#define CHECK (   PARAM1,
  PARAM2,
  BODY 
)
Value:
if( strcmp(*argv,PARAM1)==0 || strcmp(*argv,PARAM2)==0 ) \
  BODY;

Function Documentation

void init_options ( int  argc,
char *  argv[] 
)
  {
    option_turned_on  [i] = 0;
    int j;
    for( j=0; j<4; j++ )
    {
      #ifdef SAFEMODE
      assert( IS_NOT_EMPTY(t) );            // too few words in TR_OPTIONS
      #endif
      option_texts[j][i] = USE(CAR(t));
      t=CDR(t);
    }
  }

  #ifdef SAFEMODE
  assert( IS_EMPTY(t) ); // too many words in TR_OPTIONS
  #endif

  DEUSE( names );
  DEUSE( tokens );
}


Dumps statistics of memory usage

  {
    stats_used += stats[i].allocs-stats[i].deallocs;
    if( stats[i].allocs )
    {
      PRINT("   %10s %8d %8d %8d %8d\n",
      id_names[i],
      stats[i].max,
      stats[i].allocs,
      stats[i].deallocs,
      stats[i].allocs-stats[i].deallocs );
    }
  }
  PRINT(" %39s\n","----------------");
  PRINT(" %30s %8d\n","Used",stats_used);
  PRINT(" %30s %8d\n","Free",stats_free);
  PRINT(" %39s\n","----------------");
  PRINT(" %30s %8d\n","OS alloc balance",stats_allocs);
}
#endif

Dumps statistics if -Zm option was switched on

  {
    int j;
    for( j = 0; j<4; j++ )
    {
      DEUSE(option_texts[j][i]);
    }
  }
  DEUSE( standalone_arguments );

  DEALLOC( option_compiler_filename );
  DEALLOC( option_compiler_filename_chars );

  if( option_source_filename_chars )
  {
    DEALLOC( option_source_filename_chars );
  }

#ifdef ADVANCED
  int stats_used=0;
  if( OPTION_DETAILED_MEMORY_STATISTICS )
  {
    dump_statistics();
  }

  if( OPTION_MEMORY_STATISTICS )
  {
    for( i=MIN_ID; i<MAX_ID; i++ )
    stats_used += stats[i].allocs-stats[i].deallocs;
    PRINT("{MEM#%d:%d}\n",stats_used,stats_allocs);
  }
#endif //ADVANCED
}


void output_compiler_name ( int  show_help)
Parameters:
show_helpif set also output commandline options

Outputs the name of the compiler, its language, its platform and build date. If show_help is not zero, output a list of command-line options

  {
    dump( CAR(t) ); t = CDR(t);       // Usage
    
    int i;
    for( i=0; i<OPTIONS_COUNT; i++ )
    {
      int len;

      if( i==FIRST_ADVANCED_OPTION )
      {
        #ifndef ADVANCED
          t = CDR(t);
        #endif
        dumpln( CAR(t) ); t = CDR(t);       // options
      }
      
      // show short version
      len = LENGTH(option_texts[0][i]);
      outter( TEXT("  "), -1 );
      dump( option_texts[0][i] );
      for( ; len<6; len++ ) outter( TEXT(" "), -1 );

      // show long version
      len = LENGTH(option_texts[2][i]);
      if( len )
      {
        dump( option_texts[2][i] );
      }
      else
      {
        len = LENGTH(option_texts[1][i]);
        dump( option_texts[1][i] );
      }
      for( ; len<30; len++ ) outter( TEXT(" "), -1 );

      // show description      
      dumpln( option_texts[3][i] );
    }
  }
  
  DEUSE( texts );
  DEUSE( help_texts );
}


void set_options ( int  argc,
char *  argv[] 
)
Parameters:
argcargc parameter from the main function
argvargv parameter from the main function

Reads options from the argv array and sets appropriate parameters.

  { // standalone mode -- collect arguments in a list
    for( argc--,argv++; argc--; argv++ )
    {
      atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
      append( word, &standalone_arguments, &last );
    }
  }
  else
  { // compiler mode -- process options till file name and then collect arguments
    for( argc--,argv++; argc--; argv++ )
    {
      if( option_source_filename_chars )
      {
        atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
        append ( word, &standalone_arguments, &last );
      }
      else
      {
        int found = 0;
        chars_t option = UNFILENAME(*argv);
        int len = STRLEN(option);
        int i;
        for( i=0; i<OPTIONS_SUPPORTED_COUNT; i++ )
        {
          int j;
          for( j=0; j<4; j++ )
          {
            if( len==LENGTH(option_texts[j][i]) )
            {
              if( same_strings(1,option,STRING(option_texts[j][i]),len) )
              {
                found = 1;
                option_turned_on[i] = 1;
                break;
              }
            }
          }
        }
        DEALLOC( option );

        if( OPTION_HELP )
        {
          output_compiler_name( 1 );
          exit(0);
        }

        if( found ) continue;
        
        // check for invalid option or second file name
        if( **argv=='-'  )
        {
          chars_t name = UNFILENAME(*argv); // has ALLOC inside
          atom_t word = new_word( name, UNKNOWN );
          atom_t err = new_error( ERROR_UNKNOWN_OPTION, word );
          dumpln( err );
          DEUSE( word );
          DEALLOC( name );
          continue;
        }

        // set file name
        option_source_filename = *argv;
        option_source_filename_chars = UNFILENAME(*argv);
      }
    } // for
  }
}


void * alloc ( int  size)
Parameters:
sizesize of memory (in bytes) to allocate
Returns:
pointer to allocated memory

This function is used to allocate memory. Depending on the existence of DEBUG_HEAP symbol it either just allocates memory or allocates and dump on the screen allocation data.

                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
  #ifdef ADVANCED
  stats_allocs++;
  #endif
  return buffer;
}

void* reallocate(void * mem, int size)
void* reallocate ( void mem,
int  size 
)
                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
  return buffer;
}

void* xalloc( int size )
void* xalloc ( int  size)
                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }

  #ifdef ADVANCED
  stats_allocs++;
  #endif
  return buffer;
}
void dealloc( void* ptr )
{
void dealloc ( void ptr)
Parameters:
ptrpointer to the memory to deallocate

This function is used to deallocate memory. Depending on the existence of DEBUG_HEAP symbol it either just deallocates memory or deallocates and dumps on the screen deallocation data.


Variable Documentation

char* id_names[MAX_ID]
Initial value:
 { 
  "Integers",
  "Floats",
  "Lists",
  "Words",
  "Subwords",
  "Errors",
  "Var",
  "Memory" }

[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Tue Feb 7 2012