tokenarray.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: tokenarray.c,v 1.2 2001/11/13 12:55:59 dick Exp $
  4. */
  5. #include <malloc.h>
  6. #include "error.h"
  7. #include "lex.h"
  8. #include "tokenarray.h"
  9. #define TK_INCR 10000 /* increment of token array size */
  10. TOKEN *TokenArray; /* to be filled by malloc */
  11. static unsigned int tk_size; /* size of TokenArray[] */
  12. static unsigned int tk_free; /* next free position in TokenArray[] */
  13. void
  14. InitTokenArray(void) {
  15. tk_size = TK_INCR;
  16. TokenArray = (TOKEN *)malloc(sizeof (TOKEN) * tk_size);
  17. if (!TokenArray) fatal("out of memory");
  18. tk_free = 1; /* don't use position 0 */
  19. }
  20. void
  21. StoreToken(void) {
  22. if (tk_free == tk_size) {
  23. /* allocated array is full; try to increase its size */
  24. unsigned int new_size = tk_size + TK_INCR;
  25. register TOKEN *new_array = (TOKEN *)realloc(
  26. (char *)TokenArray,
  27. sizeof (TOKEN) * new_size
  28. );
  29. if (new_size < tk_free)
  30. fatal("internal error: TK_INCR causes numeric overflow");
  31. if (!new_array) {
  32. /* we failed */
  33. fatal("out of memory");
  34. }
  35. TokenArray = new_array, tk_size = new_size;
  36. }
  37. /* now we are sure there is room enough */
  38. TokenArray[tk_free++] = lex_token;
  39. }
  40. unsigned int
  41. TextLength(void) {
  42. return tk_free;
  43. }