xref: /netbsd-src/external/bsd/flex/dist/tests/mem_nr.l (revision 56bd85463476f90bb489799c99292bb30d6771c0)
130da1778Schristos /*
230da1778Schristos  * This file is part of flex.
330da1778Schristos  *
430da1778Schristos  * Redistribution and use in source and binary forms, with or without
530da1778Schristos  * modification, are permitted provided that the following conditions
630da1778Schristos  * are met:
730da1778Schristos  *
830da1778Schristos  * 1. Redistributions of source code must retain the above copyright
930da1778Schristos  *    notice, this list of conditions and the following disclaimer.
1030da1778Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1130da1778Schristos  *    notice, this list of conditions and the following disclaimer in the
1230da1778Schristos  *    documentation and/or other materials provided with the distribution.
1330da1778Schristos  *
1430da1778Schristos  * Neither the name of the University nor the names of its contributors
1530da1778Schristos  * may be used to endorse or promote products derived from this software
1630da1778Schristos  * without specific prior written permission.
1730da1778Schristos  *
1830da1778Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1930da1778Schristos  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2030da1778Schristos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2130da1778Schristos  * PURPOSE.
2230da1778Schristos  */
2330da1778Schristos 
2430da1778Schristos %{
2530da1778Schristos /* A template scanner file to build "scanner.c".
2630da1778Schristos  * The whole idea is to cause memory realloc by
2730da1778Schristos  *  1. pushing a lot on the condition stack, and
2830da1778Schristos  *  2. eating input greater than YY_BUF_SIZE
2930da1778Schristos  */
3030da1778Schristos #include <stdio.h>
3130da1778Schristos #include <stdlib.h>
3230da1778Schristos #include "config.h"
3330da1778Schristos 
3430da1778Schristos /* Insanely small read buffer. This pretty much guarantees at least one realloc. */
3530da1778Schristos #ifdef YY_BUF_SIZE
3630da1778Schristos #undef YY_BUF_SIZE
3730da1778Schristos #endif
3830da1778Schristos #define YY_BUF_SIZE 8
3930da1778Schristos 
4030da1778Schristos %}
4130da1778Schristos 
4230da1778Schristos %option 8bit prefix="test"
4330da1778Schristos %option nounput nomain noyywrap noinput noyy_top_state
4430da1778Schristos %option warn stack nodefault
4530da1778Schristos %option noyyalloc noyyrealloc noyyfree
4630da1778Schristos 
4730da1778Schristos %x parens
4830da1778Schristos 
4930da1778Schristos %%
5030da1778Schristos 
5130da1778Schristos <INITIAL>{
5230da1778Schristos "("         { printf("yy_push_state(parens)\n"); yy_push_state(parens); }
5330da1778Schristos len=[0-9]+  { printf("About read token where %s\n",yytext); }
5430da1778Schristos 0+          { }
5530da1778Schristos .|\n        { }
5630da1778Schristos }
5730da1778Schristos 
5830da1778Schristos <parens>{
5930da1778Schristos "("         { printf("yy_push_state(parens)\n"); yy_push_state(parens); }
6030da1778Schristos ")"         { printf("yy_pop_state()\n");yy_pop_state();}
6130da1778Schristos [^()\n]+     { }
6230da1778Schristos .|\n        { }
6330da1778Schristos }
6430da1778Schristos 
6530da1778Schristos %%
6630da1778Schristos /* total memory allocated */
6730da1778Schristos static size_t total_mem=0;
6830da1778Schristos 
6930da1778Schristos /* track the amount of memory for ptr. */
7030da1778Schristos struct memsz {
7130da1778Schristos     void* p;
7230da1778Schristos     size_t sz;
7330da1778Schristos };
7430da1778Schristos 
7530da1778Schristos static struct memsz * ptrs=0;  /* Array of pairs. */
7630da1778Schristos static int nptrs=0; /* Number of pairs in array. */
7730da1778Schristos static int arrsz=0; /* Capacity of array. */
7830da1778Schristos 
dump_mem(FILE * fp)7930da1778Schristos static void dump_mem(FILE* fp){
8030da1778Schristos     int i;
8130da1778Schristos     fprintf(fp,"\tptrs[%d] = {", nptrs);
8230da1778Schristos     for (i=0; i < arrsz; i++)
8330da1778Schristos         fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz);
8430da1778Schristos 
8530da1778Schristos     fprintf(fp,"}\n");
8630da1778Schristos }
8730da1778Schristos 
yyalloc(yy_size_t n)8830da1778Schristos void * yyalloc(yy_size_t n)
8930da1778Schristos {
9030da1778Schristos     void * p;
9130da1778Schristos     int i;
9230da1778Schristos 
9330da1778Schristos     total_mem += n;
94*56bd8546Schristos     p = malloc(n);
9530da1778Schristos 
9630da1778Schristos     if( nptrs >= arrsz){
9730da1778Schristos         /* increase array size by 1 */
9830da1778Schristos         arrsz++;
99*56bd8546Schristos         ptrs = realloc(ptrs, (size_t) arrsz * sizeof(struct memsz));
10030da1778Schristos         ptrs[nptrs].p = 0;
10130da1778Schristos         ptrs[nptrs].sz = 0;
10230da1778Schristos     }
10330da1778Schristos 
10430da1778Schristos     /* find a null slot */
10530da1778Schristos     for(i=0; i < arrsz ; i++)
10630da1778Schristos         if (ptrs[i].p == 0) {
10730da1778Schristos             ptrs[i].p = p;
10830da1778Schristos             ptrs[i].sz = n;
10930da1778Schristos         }
11030da1778Schristos 
11130da1778Schristos     nptrs++;
11230da1778Schristos     printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p);
11330da1778Schristos     dump_mem(stdout);
11430da1778Schristos     return p;
11530da1778Schristos }
11630da1778Schristos 
yyrealloc(void * p,yy_size_t n)11730da1778Schristos void * yyrealloc(void* p, yy_size_t n)
11830da1778Schristos {
11930da1778Schristos     int i;
12030da1778Schristos     for (i=0; i < arrsz; i++)
12130da1778Schristos         if ( ptrs[i].p == p){
12230da1778Schristos             total_mem -= ptrs[i].sz;
12330da1778Schristos             total_mem += n;
124*56bd8546Schristos             ptrs[i].p = realloc(p,n);
12530da1778Schristos             ptrs[i].sz = n;
12630da1778Schristos 
12730da1778Schristos             printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n",
12830da1778Schristos                     (long)p,(long)n,(long)total_mem,(long)ptrs[i].p);
12930da1778Schristos             dump_mem(stdout);
13030da1778Schristos             return ptrs[i].p;
13130da1778Schristos         }
13230da1778Schristos 
13330da1778Schristos     fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p);
13430da1778Schristos     dump_mem(stdout);
13530da1778Schristos     exit(1);
13630da1778Schristos }
13730da1778Schristos 
yyfree(void * p)13830da1778Schristos void yyfree(void* p)
13930da1778Schristos {
14030da1778Schristos     int i;
14130da1778Schristos     for (i=0; i < arrsz; i++)
14230da1778Schristos         if ( ptrs[i].p == p){
14330da1778Schristos             total_mem -= ptrs[i].sz;
14430da1778Schristos             free(p);
14530da1778Schristos             ptrs[i].p = 0;
14630da1778Schristos             ptrs[i].sz = 0;
14730da1778Schristos             nptrs--;
14830da1778Schristos             printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem);
14930da1778Schristos             dump_mem(stdout);
15030da1778Schristos             return;
15130da1778Schristos         }
15230da1778Schristos 
15330da1778Schristos     fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p);
15430da1778Schristos     dump_mem(stdout);
15530da1778Schristos     exit(1);
15630da1778Schristos }
15730da1778Schristos 
15830da1778Schristos int main(void);
15930da1778Schristos 
16030da1778Schristos int
main(void)161*56bd8546Schristos main (void)
16230da1778Schristos {
16330da1778Schristos     arrsz = 1;
164*56bd8546Schristos     ptrs  = calloc(1, sizeof(struct memsz));
16530da1778Schristos     nptrs = 0;
16630da1778Schristos 
16730da1778Schristos     yyin = stdin;
16830da1778Schristos     yyout = stdout;
169*56bd8546Schristos     testlex();
170*56bd8546Schristos     testlex_destroy();
17130da1778Schristos     free(ptrs);
17230da1778Schristos 
17330da1778Schristos     if ( nptrs > 0 || total_mem > 0){
17430da1778Schristos         fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem);
17530da1778Schristos         exit(1);
17630da1778Schristos     }
17730da1778Schristos     printf("TEST RETURNING OK.\n");
17830da1778Schristos     return 0;
17930da1778Schristos }
180