Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
atoms.h File Reference

Go to the source code of this file.

Data Structures

struct  node
 core atom structure More...
 
struct  node_ex
 

Typedefs

typedef struct node atomrec_t
 core structure of an atom More...
 
typedef atomrec_tatom_t
 global atom type More...
 
typedef struct node_ex atomrec_ex_t
 core structure of an atom More...
 
typedef atomrec_ex_tatom_ex_t
 global atom type More...
 

Functions

void init_atoms ()
 initializes the Atoms module More...
 
void finit_atoms ()
 finalizes the Atoms module More...
 
atom_t use (atom_t a)
 adds link to atom More...
 
void __attribute__ ((used, noinline, regparm(0), stdcall)) deuse(atom_t a)
 
void outter (chars_t string, int len)
 prints text More...
 
char_t inner ()
 
int inner_eof ()
 returns eof status More...
 
void dump (atom_t a)
 dumps atom contents More...
 
void dumpln (atom_t a)
 dumps atom contents More...
 
void dump_atom (atom_t a, int level)
 dumps atom contents More...
 
void init_output (outter_t new_outter)
 initializes text output More...
 
void init_input (inner_t new_inner, inner_eof_t new_inner_eof)
 initializes text input More...
 

Variables

int stats_free
 statistics for free atoms (of any type) More...
 
FILE * input_stream
 current input stream More...
 
FILE * output_stream
 current input stream More...
 
int outter_size
 counts outted characters More...
 
FILE * dribble_handle
 Handle of the dribble file. More...
 
struct lconv * locale_info
 locale information More...
 

Atom IDs

These constants represent atom ID. MIN_ID and MAX_ID are not real atom IDs.

#define MIN_ID   INTEGER_ID
 
#define INTEGER_ID   0
 
#define FLOAT_ID   1
 
#define LIST_ID   2
 
#define WORD_ID   3
 
#define SUBWORD_ID   4
 
#define ERROR_ID   5
 
#define VAR_ID   6
 
#define MEM_ID   7
 
#define MAX_ID   (MEM_ID+1)
 

Common atoms

#define OFFSET_ID   4
 
#define OFFSET_INT   8
 
#define OFFSET_REPCOUNT   8
 
#define OFFSET_REPLIMIT   12
 
#define REF(x)   ((x)->unode.u[0])
 
#define ID(x)   ((x)->unode.b[OFFSET_ID])
 
#define FLAGS(x)   ((x)->unode.s[3])
 
#define GET_FLAGS(a, bitmask)   (FLAGS(a) & (bitmask))
 
#define SET_FLAGS(a, bitmask)   FLAGS(a) |= (bitmask)
 
#define CLR_FLAGS(a, bitmask)   FLAGS(a) &= ~(bitmask)
 
#define DEUSE(x)   deuse(x)
 
#define USE(x)   use(x)
 

Sizes

#define ATOM_SIZE   sizeof(atomrec_t)
 atom size (in bytes) More...
 
#define ATOM_SIZE_EX   (ATOM_SIZE+1*sizeof(int_t))
 size of extended atom More...
 
#define CHAR_SIZE   sizeof(char_t)
 char size (in bytes) More...
 
#define UNKNOWN   -1
 

Macro Definition Documentation

#define MIN_ID   INTEGER_ID

Definition at line 112 of file atoms.h.

#define INTEGER_ID   0

Definition at line 113 of file atoms.h.

#define FLOAT_ID   1

Definition at line 114 of file atoms.h.

#define LIST_ID   2

Definition at line 115 of file atoms.h.

#define WORD_ID   3

Definition at line 116 of file atoms.h.

#define SUBWORD_ID   4

Definition at line 117 of file atoms.h.

#define ERROR_ID   5

Definition at line 118 of file atoms.h.

#define VAR_ID   6

Definition at line 119 of file atoms.h.

#define MEM_ID   7

Definition at line 120 of file atoms.h.

#define MAX_ID   (MEM_ID+1)

Definition at line 121 of file atoms.h.

#define OFFSET_ID   4

Definition at line 129 of file atoms.h.

#define OFFSET_INT   8

Definition at line 130 of file atoms.h.

#define OFFSET_REPCOUNT   8

Definition at line 131 of file atoms.h.

#define OFFSET_REPLIMIT   12

Definition at line 132 of file atoms.h.

#define REF (   x)    ((x)->unode.u[0])

Definition at line 134 of file atoms.h.

#define ID (   x)    ((x)->unode.b[OFFSET_ID])

Definition at line 135 of file atoms.h.

#define FLAGS (   x)    ((x)->unode.s[3])

Definition at line 136 of file atoms.h.

#define GET_FLAGS (   a,
  bitmask 
)    (FLAGS(a) & (bitmask))

Definition at line 138 of file atoms.h.

#define SET_FLAGS (   a,
  bitmask 
)    FLAGS(a) |= (bitmask)

Definition at line 139 of file atoms.h.

#define CLR_FLAGS (   a,
  bitmask 
)    FLAGS(a) &= ~(bitmask)

Definition at line 140 of file atoms.h.

#define DEUSE (   x)    deuse(x)

Definition at line 144 of file atoms.h.

#define USE (   x)    use(x)

Definition at line 145 of file atoms.h.

#define ATOM_SIZE   sizeof(atomrec_t)

Definition at line 152 of file atoms.h.

#define ATOM_SIZE_EX   (ATOM_SIZE+1*sizeof(int_t))

Definition at line 153 of file atoms.h.

#define CHAR_SIZE   sizeof(char_t)

Definition at line 154 of file atoms.h.

#define UNKNOWN   -1

Definition at line 155 of file atoms.h.

Typedef Documentation

typedef struct node atomrec_t
typedef atomrec_t* atom_t

Definition at line 93 of file atoms.h.

typedef struct node_ex atomrec_ex_t

Definition at line 101 of file atoms.h.

Function Documentation

void init_atoms ( )

Creates a data pool for all atoms and creates one special atom representing the empty list.

Definition at line 130 of file atoms.c.

131 {
132  #ifdef DEBUG_ATOM
133  printf("<ATOM> Atoms initialized\n");
134  #endif //DEBUG_ATOM
135 
136 //#ifdef __LP64__
137 //#warning Definitely 64 bit
138 //#endif
139 //#ifndef __LP64__
140 //#warning Maybe 32 bit
141 //#endif
142 
143  #ifdef ADVANCED
144  int i;
145  stats_free = 0;
146  stats_allocs = 0;
147  for( i=MIN_ID; i<MAX_ID; i++ )
148  {
149  stats[i].max = 0;
150  stats[i].allocs = 0;
151  stats[i].deallocs = 0;
152  }
153  #endif //ADVANCED
154 
155  #ifdef SAFEMODE
156  assert( sizeof(atomrec_t)==16 );
157  #endif
158 
159  //setlocale( LC_ALL, "" );
160  locale_info = localeconv();
161 
164 
166  REF(empty_list) = 1;
167  ID(empty_list) = LIST_ID;
170  FLAGS(empty_list) = 0;
171 }
void finit_atoms ( )

Returns the empty list back to the data pool.

Definition at line 182 of file atoms.c.

183 {
185 
186  #ifdef DEBUG_MEMORY_LEAKS
187  dump_pool();
188  #endif
189 
190  #ifdef DEBUG_ATOM
191  printf("<ATOM> Atoms finalized\n");
192  #endif //DEBUG_ATOM
193 }
atom_t use ( atom_t  a)
Parameters
aatom to use

Adds a link to an atom by increasing atom's reference count.

Definition at line 206 of file atoms.c.

207 {
208  if( IS_UNBOUND(a) || IS_EMPTY(a) || IS_STOPPED(a) )
209  return a;
210 
211  //if( a==0x49f3b8)
212  //{
213  //printf("USE.BUG[%08x] ref %d->%d\n", (int)a, REF(a), REF(a)+1 );
214  //}
215 
216 
217  #ifdef DEBUG_RUNTIME_ATOMS
219  {
220  outter( TEXT("<RUNTIME> use "), -1 );
221  dump_atom_address( a );
222  dump_atom( a, 1 );
223  outter( TEXT("\n"), -1 );
224  }
225  #endif
226  #ifdef DEBUG_COMPILETIME_ATOMS
227  if( compiling_code )
228  {
229  outter( TEXT("<COMPILETIME> use "), -1 );
230  dump_atom_address( a );
231  dump_atom( a, 1 );
232  outter( TEXT("\n"), -1 );
233  }
234  #endif
235 
236  //if( IS_ERROR(a) )
237  // {
238  // printf("TO BE USEIT hex(a)=%x id=%d REF=%d a=",(int)a,ID(a),REF(a)); dumpln(a);
239  // }
240 
241  #ifdef SAFEMODE
242  assert( a );
243  assert( ID(a)<MAX_ID );
244  assert( REF(a)>=0 ); // 2009 was >0
245  #endif //SAFEMODE
246 
247  //if( IS_INTEGER(a) || IS_FLOAT(a))
248  //{
249  //printf("deuse[addr=%x ref=%d]\n",a,REF(a));
250  //printf("use atom="); dumpln(a);
251  //}
252 
253  REF(a)++;
254 
255  #ifdef DEBUG_ATOM
256  printf("<ATOM> [%08x] ref+1\n",(int)a);
257  #endif //DEBUG_ATOM
258  return a;
259 }
atom_t __attribute__ ( (used, noinline, regparm(0), stdcall)  )
void outter ( chars_t  string,
int  len 
)
Parameters
stringtext to print
lenlength of text

This function prints text to either the hooked output or to a file.

Definition at line 496 of file atoms.c.

497 {
498  if( output_stream==NULL )
499  {
500  std_outter( string, len );
501  }
502  else
503  {
504  if( len==-1 ) len = STRLEN( string );
505  for( ; len>0; len--, string++ )
506  {
507  int crlf = (DEBAR(*string)==0x0D) && (DEBAR(*(string+1))==0x0A);
508  if( !crlf )
509  {
510  char_t wc[2];
511  wc[0] = DEBAR(*string);
512  wc[1] = 0;
513 
514  char* buf =(char*) UTF16_to_UTF8(wc);
515  DEALLOC( buf );
516  fprintf( output_stream, "%S", wc );
517  }
518  }
519  }
520 
521  if( dribble_handle )
522  {
523  if( len==-1 ) len = STRLEN( string );
524  for( ; len>0; len--, string++ )
525  {
526  int crlf = (DEBAR(*string)==0x0D) && (DEBAR(*(string+1))==0x0A);
527  if( !crlf )
528  {
529  char_t wc[2];
530  wc[0] = DEBAR(*string);
531  wc[1] = 0;
532 
533  char* buf =(char*) UTF16_to_UTF8(wc);
534  DEALLOC( buf );
535  fprintf( dribble_handle, "%S", wc );
536  }
537  }
538  }
539 
540 }
char_t inner ( )

Definition at line 569 of file atoms.c.

570 {
571  if( input_stream==NULL )
572  {
573  char_t ch = std_inner();
574 
575  if( dribble_handle )
576  {
577  fprintf( dribble_handle, "%C", (wint_t)ch );
578  }
579 
580  return ch;
581  }
582  else
583  {
584  char_t ch;
585  //ch = GETCHAR( input_stream );
586  //while( ch=='\r' ) ch = GETCHAR( input_stream ); // remove ^M from input, keep ^J
587 
588  ch = (char_t)getc( input_stream );
589  while( ch=='\r' ) ch = (char_t)getc( input_stream ); // remove ^M from input, keep ^J
590  return ch;
591  }
592 }
int inner_eof ( )

Return EOF status of a hooked or file stream.

Definition at line 549 of file atoms.c.

550 {
551  if( input_stream==NULL )
552  {
553  return std_inner_eof();
554  }
555  else
556  {
557  return feof(input_stream)?1:0;
558  }
559 }
void dump ( atom_t  a)
Parameters
aatom to dump

Dumps atom's contents throught the current text output function (e.g. use_stdout).

Definition at line 427 of file atoms.c.

428 {
429  dump_atom( a, 0 );
430 }
void dumpln ( atom_t  a)
Parameters
aatom to dump

Dumps atom's contents throught the current text output function (e.g. use_stdout) and moves cursor to the next line.

Definition at line 444 of file atoms.c.

445 {
446  dump_atom( a, 0 );
447  outter( TEXT("\n"), 1 );
448 }
void dump_atom ( atom_t  a,
int  level 
)
Parameters
aatom to dump
levellevel of nesting

Dumps atom's contents using a call-back outter function. Texts are automatically indented according to the level. Typically this function is used to print an atom when the call-back function forwards contents to standard output. The call-back function is set by init_output().

< array of deleter functions for each atom type

Definition at line 375 of file atoms.c.

376 {
377  typedef void(*dumper_t)(atom_t,int);
378 
379  static dumper_t dumpers[MAX_ID] = {
380  dump_integer, // INTEGER_ID
381  dump_float, // FLOAT_ID
382  dump_list, // LIST_ID
383  dump_word, // WORD_ID
384  dump_word, // SUBWORD_ID
385  dump_error, // ERROR_ID
386  dump_var, // VAR_ID
387  dump_mem, // MEM_ID
388  };
389 
390 
391  #ifdef SAFEMODE
392  assert( a );
393  //assert( outter );
394  if( IS_NOT_EMPTY(a) ) assert( ID(a)<MAX_ID );
395  #endif //SAFEMODE
396 
397  #ifdef DEBUG_REF_COUNT
398  if( IS_EMPTY(a) )
399  {
400  outter( TEXT("##"), 2 );
401  }
402  else
403  {
404  int n;
405  #define DUMP_BUF_SIZE 64
406  char_t buf[DUMP_BUF_SIZE];
407  n = SPRINTF( buf, DUMP_BUF_SIZE, TEXT(" %d#"), REF(a) );
408  outter( buf, n );
409  }
410  #endif
411 
412  dumper_t dumper = dumpers[ID(a)];
413  dumper(a,level);
414 }
void init_output ( outter_t  new_outter)
Parameters
new_outternew outter function to use by dump and dumpln

Initializes the text output system by setting the std_outter function to output text throught it.

Definition at line 461 of file atoms.c.

462 {
463  std_outter = new_outter;
464 }
void init_input ( inner_t  new_inner,
inner_eof_t  new_inner_eof 
)
Parameters
new_innernew inner function
new_inner_eofnew inner_eof function

Initializes the text input system by setting the std_inner and :std_inner_eof functions to input text throught it.

Definition at line 479 of file atoms.c.

480 {
481  std_inner = new_inner;
482  std_inner_eof = new_inner_eof;
483 }

Variable Documentation

int stats_free

Definition at line 108 of file options.c.

FILE* input_stream

Definition at line 117 of file atoms.c.

FILE* output_stream

Definition at line 118 of file atoms.c.

int outter_size

Definition at line 116 of file atoms.c.

FILE* dribble_handle

Definition at line 119 of file atoms.c.

struct lconv* locale_info

Definition at line 120 of file atoms.c.


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013