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

Go to the source code of this file.

Macros

#define void   void __attribute__ ((used,noinline,regparm(0),stdcall))
 

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 deuse (atom_t a)
 removes link to atom More...
 
void dump_atom (atom_t a, int level)
 dumps atom contents More...
 
void dump (atom_t a)
 dumps atom contents More...
 
void dumpln (atom_t a)
 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...
 
void outter (chars_t string, int len)
 prints text More...
 
int inner_eof ()
 returns eof status More...
 
char_t inner ()
 

Variables

inner_t std_inner
 hook variable for inputting text More...
 
inner_eof_t std_inner_eof
 hook variable for inputting text More...
 
outter_t std_outter
 hook variable for outputting text More...
 
int outter_size =0
 counts outted characters More...
 
FILE * input_stream = NULL
 current input stream More...
 
FILE * output_stream = NULL
 current input stream More...
 
FILE * dribble_handle = NULL
 Handle of the dribble file. More...
 
struct lconv * locale_info
 locale information More...
 

Macro Definition Documentation

#define void   void __attribute__ ((used,noinline,regparm(0),stdcall))

Definition at line 275 of file atoms.c.

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 }
void deuse ( atom_t  a)
Parameters
aatom to unlink

Decrements the reference count for the atom and if it reaches 0 then frees the atom using one of the functions delete_numeric, delete_list, delete_word, delete_subword, delete_error, delete_var.

Definition at line 276 of file atoms.c.

278 {
279  __asm__ volatile ( ASM_STORE_RESULT:::ASM_CLOBBER_REGISTERS );
280 
281  typedef void(*deleter_t)(atom_t);
282 
283  static deleter_t deleters[MAX_ID] = {
284  delete_numeric, // INTEGER_ID
285  delete_numeric, // FLOAT_ID
286  delete_list, // LIST_ID
287  delete_word, // WORD_ID
288  delete_subword, // SUBWORD_ID
289  delete_error, // ERROR_ID
290  delete_var, // VAR_ID
291  delete_mem, // MEM_ID
292  }; // array of deleter functions for each atom type
293 
294  //if( a==0x49f3b8)
295  //{
296  //printf("DEUSE.BUG[%08x] ref %d->%d\n", (int)a, REF(a), REF(a)-1 );
297  //}
298 
299  //if( IS_INTEGER(a) || IS_FLOAT(a))
300  // {
301  // //printf("deuse[addr=%x ref=%d]\n",(int)a,REF(a));
302  // printf("deuse atom="); dumpln(a);
303  // }
304 
305 
306  if( !IS_EMPTY(a) && !IS_UNBOUND(a) && !IS_STOPPED(a))
307  {
308  //printf("deuse "); dumpln(a);
309  #ifdef DEBUG_RUNTIME_ATOMS
311  {
312  outter( TEXT("<RUNTIME> deuse"), -1 );
313  dump_atom_address( a );
314  dump_atom( a, 1 );
315  outter( TEXT("\n"), -1 );
316  }
317  #endif
318  #ifdef DEBUG_COMPILETIME_ATOMS
319  if( compiling_code )
320  {
321  outter( TEXT("<COMPILETIME> deuse"), -1 );
322  dump_atom_address( a );
323  dump_atom( a, 1 );
324  outter( TEXT("\n"), -1 );
325  }
326  #endif
327 
328  //if( IS_ERROR(a) )
329  //{
330  //printf("TO BE DEUSE hex(a)=%x id=%d REF=%d a=",(int)a,ID(a),REF(a)); dumpln(a);
331  //}
332 
333  #ifdef SAFEMODE
334  assert( a );
335  //if(ID(a)>=MAX_ID) {printf("ASSERT[%x]\n",a);}
336  assert( (ID(a)<MAX_ID) );
337  assert( REF(a)>0 );
338  #endif // SAFEMODE
339  //printf("GOODY hex(a)=%x id=%d REF=%d\n\n",(int)a,ID(a),REF(a));
340 
341  if( !--REF(a) )
342  {
343  #ifdef DEBUG_ATOM
344  printf("<ATOM> [%08x] ref-1\n",(int)a);
345  #endif //DEBUG_ATOM
346 
347  #ifdef ADVANCED
348  stats[ID(a)].deallocs++;
349  stats_free++;
350  #endif //ADVANCED
351 
352  deleter_t deleter = deleters[ID(a)];
353  deleter(a);
354  }
355  }
356  __asm__ volatile ( ASM_RESTORE_RESULT:::ASM_CLOBBER_REGISTERS );
357 }
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 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 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 }
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 }
void 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 }
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 }

Variable Documentation

inner_t std_inner

Definition at line 113 of file atoms.c.

inner_eof_t std_inner_eof

Definition at line 114 of file atoms.c.

outter_t std_outter

Definition at line 115 of file atoms.c.

int outter_size =0

Definition at line 116 of file atoms.c.

FILE* input_stream = NULL

Definition at line 117 of file atoms.c.

FILE* output_stream = NULL

Definition at line 118 of file atoms.c.

FILE* dribble_handle = NULL

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