4 * This file is part of flex.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 /* A template scanner file to build "scanner.c".
28 * The whole idea is to cause memory realloc by
29 * 1. pushing a lot on the condition stack, and
30 * 2. eating input greater than YY_BUF_SIZE
36 /* Insanely small read buffer. This pretty much guarantees at least one realloc. */
44 %option 8bit outfile="scanner.c" prefix="test"
45 %option nounput nomain noyywrap
46 %option warn stack nodefault
47 %option noyyalloc noyyrealloc noyyfree
54 "(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); }
55 len=[0-9]+ { printf("About read token where %s\n",yytext); }
61 "(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); }
62 ")" { printf("yy_pop_state()\n");yy_pop_state();}
68 /* total memory allocated */
69 static size_t total_mem=0;
71 /* track the amount of memory for ptr. */
77 static struct memsz * ptrs=0; /* Array of pairs. */
78 static int nptrs=0; /* Number of pairs in array. */
79 static int arrsz=0; /* Capacity of array. */
81 static void dump_mem(FILE* fp){
83 fprintf(fp,"\tptrs[%d] = {", nptrs);
84 for (i=0; i < arrsz; i++)
85 fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz);
90 void * yyalloc(yy_size_t n)
99 /* increase array size by 1 */
101 ptrs = (struct memsz*)realloc( ptrs, arrsz * sizeof(struct memsz));
106 /* find a null slot */
107 for(i=0; i < arrsz ; i++)
108 if (ptrs[i].p == 0) {
114 printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p);
119 void * yyrealloc(void* p, yy_size_t n)
122 for (i=0; i < arrsz; i++)
123 if ( ptrs[i].p == p){
124 total_mem -= ptrs[i].sz;
126 ptrs[i].p = (void*)realloc(p,n);
129 printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n",
130 (long)p,(long)n,(long)total_mem,(long)ptrs[i].p);
135 fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p);
143 for (i=0; i < arrsz; i++)
144 if ( ptrs[i].p == p){
145 total_mem -= ptrs[i].sz;
150 printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem);
155 fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p);
166 ptrs = (struct memsz*)calloc(1,sizeof(struct memsz));
175 if ( nptrs > 0 || total_mem > 0){
176 fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem);
179 printf("TEST RETURNING OK.\n");