-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtokenarray.c
52 lines (45 loc) · 1.32 KB
/
tokenarray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: tokenarray.c,v 1.13 2012-06-08 16:04:30 Gebruiker Exp $
*/
#include "error.h"
#include "Malloc.h"
#include "token.h"
#include "lang.h"
#include "tokenarray.h"
#define TK_START 16384 /* increment of token array size */
Token *Token_Array; /* to be filled by Malloc() */
static size_t tk_size; /* size of Token_Array[] */
static size_t tk_free; /* next free position in Token_Array[]*/
void
Init_Token_Array(void) {
if (Token_Array) Free(Token_Array);
tk_size = TK_START;
Token_Array = (Token *)Malloc(sizeof (Token) * tk_size);
tk_free = 1; /* don't use position 0 */
}
void
Store_Token(Token tk) {
if (tk_free == tk_size) {
/* allocated array is full; try to increase its size */
size_t new_size = 2 * tk_size;
if (new_size < tk_free)
fatal("internal error: TK_INCR causes numeric overflow");
fprintf (stderr,"X64: Growing Token_Array to 0x%016zx\n", new_size);
Token *new_array =
(Token *)TryRealloc(
(char *)Token_Array, sizeof (Token) * new_size
);
if (!new_array) {
/* we failed */
fatal("out of memory");
}
Token_Array = new_array, tk_size = new_size;
}
/* now we are sure there is room enough */
Token_Array[tk_free++] = tk;
}
size_t
Text_Length(void) {
return tk_free;
}