LTP GCOV extension - code coverage report
Current view: directory - csrc/cl - spine_stack.c
Test: app.info
Date: 2007-12-10 Instrumented lines: 68
Code covered: 97.1 % Executed lines: 66

       1                 : /*
       2                 :         Copyright (C) 2007, Bruce Ediger
       3                 : 
       4                 :     This file is part of cl.
       5                 : 
       6                 :     cl is free software; you can redistribute it and/or modify
       7                 :     it under the terms of the GNU General Public License as published by
       8                 :     the Free Software Foundation; either version 2 of the License, or
       9                 :     (at your option) any later version.
      10                 : 
      11                 :     cl is distributed in the hope that it will be useful,
      12                 :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :     GNU General Public License for more details.
      15                 : 
      16                 :     You should have received a copy of the GNU General Public License
      17                 :     along with cl; if not, write to the Free Software
      18                 :     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      19                 : 
      20                 : */
      21                 : #include <stdlib.h>
      22                 : #include <stdio.h>
      23                 : 
      24                 : #include <spine_stack.h>
      25                 : 
      26                 : static struct spine_stack *spine_stack_free_list = NULL;
      27                 : 
      28                 : static int stack_malloc_cnt = 0;
      29                 : static int stack_reused_cnt = 0;
      30                 : static int new_stack_cnt   = 0;
      31                 : 
      32                 : extern int interpreter_interrupted;
      33                 : 
      34                 : struct spine_stack *
      35                 : new_spine_stack(int sz)
      36            2264 : {
      37                 :         struct spine_stack *r;
      38                 : 
      39            2264 :         ++new_stack_cnt;
      40                 : 
      41            2264 :         if (spine_stack_free_list)
      42                 :         {
      43                 :                 /* dual use of prev field: linked list of "free" spine stacks,
      44                 :                  * and FIFO stack of actually in-use spine stacks. */
      45            1641 :                 r = spine_stack_free_list;
      46            1641 :                 spine_stack_free_list = r->prev;
      47            1641 :                 r->prev = NULL;
      48            1641 :                 ++stack_reused_cnt;
      49                 :         } else {
      50             623 :                 r = malloc(sizeof(*r));
      51             623 :                 r->stack = malloc(sz * sizeof(r->stack[0]));
      52             623 :                 r->top   = 0;
      53             623 :                 r->maxdepth = 0;
      54             623 :                 r->size = sz;
      55             623 :                 r->prev = NULL;
      56             623 :                 r->sn = ++stack_malloc_cnt;
      57                 :         }
      58                 : 
      59            2264 :         return r;
      60                 : }
      61                 : 
      62                 : void
      63                 : delete_spine_stack(struct spine_stack *ss)
      64            2253 : {
      65                 :         /* dual use of prev field: linked list of "free" spine stacks,
      66                 :          * and FIFO stack of actually in-use spine stacks. */
      67            2253 :         ss->prev = spine_stack_free_list;
      68            2253 :         spine_stack_free_list = ss;
      69            2253 : }
      70                 : 
      71                 : void
      72                 : push_spine_stack(struct spine_stack **ss)
      73            2264 : {
      74                 :         /* dual use of prev field: linked list of "free" spine stacks,
      75                 :          * and FIFO stack of actually in-use spine stacks.
      76                 :          * This constitutes the FIFO stack of in-use spine stacks usage. */
      77            2264 :         struct spine_stack *p = new_spine_stack(32);
      78            2264 :         p->prev = *ss;
      79            2264 :         *ss = p;
      80            2264 : }
      81                 : 
      82                 : void
      83                 : pop_spine_stack(struct spine_stack **ss)
      84            2253 : {
      85                 :         /* FIFO-stack-of-in-use-stacks use of prev field */
      86            2253 :         struct spine_stack *tmp = (*ss)->prev;
      87            2253 :         delete_spine_stack(*ss);
      88            2253 :         *ss = tmp;
      89            2253 : }
      90                 : 
      91                 : void
      92                 : free_all_spine_stacks(int memory_info_flag)
      93              69 : {
      94              69 :         int maxdepth = 0;
      95              69 :         int maxsize  = 0;
      96              69 :         int depth_sum = 0;
      97              69 :         int stack_freed_cnt = 0;
      98             750 :         while (spine_stack_free_list)
      99                 :         {
     100             612 :                 struct spine_stack *tmp = spine_stack_free_list->prev;
     101             612 :                 if (spine_stack_free_list->maxdepth > maxdepth)
     102              87 :                         maxdepth = spine_stack_free_list->maxdepth;
     103             612 :                 if (spine_stack_free_list->size > maxsize)
     104              65 :                         maxsize = spine_stack_free_list->size; 
     105             612 :                 depth_sum += spine_stack_free_list->maxdepth;
     106             612 :                 free(spine_stack_free_list->stack);
     107             612 :                 spine_stack_free_list->stack = NULL;
     108             612 :                 free(spine_stack_free_list);
     109             612 :                 spine_stack_free_list = tmp;
     110             612 :                 ++stack_freed_cnt;
     111                 :         }
     112                 : 
     113              69 :         if (!interpreter_interrupted && stack_freed_cnt != stack_malloc_cnt)
     114               0 :                 fprintf(stderr, "Allocated %d spine stacks, freed %d\n",
     115                 :                         stack_malloc_cnt, stack_freed_cnt);
     116                 : 
     117              69 :         if (memory_info_flag)
     118                 :         {
     119               2 :                 float mean_depth = stack_freed_cnt? (float)depth_sum/(float)stack_freed_cnt: 0.0;
     120               2 :                 fprintf(stderr, "Gave out %d spine stacks\n", new_stack_cnt);
     121               2 :                 fprintf(stderr, "Allocated %d spine stacks\n", stack_malloc_cnt);
     122               2 :                 fprintf(stderr, "Reused %d spine stacks\n", stack_reused_cnt);
     123               2 :                 fprintf(stderr, "Maximum spine stack depth: %d\n", maxdepth);
     124               2 :                 fprintf(stderr, "Maximum spine stack size:  %d\n", maxsize);
     125               2 :                 fprintf(stderr, "Mean spine stack depth:    %.02f\n", mean_depth);
     126                 :         }
     127              69 : }
     128                 : 
     129                 : void
     130                 : pushnode(struct spine_stack *ss, struct node *n)
     131       150130917 : {
     132       150130917 :         ss->stack[ss->top] = n;
     133                 : 
     134       150130917 :         ++ss->top;
     135                 : 
     136       150130917 :         if (ss->top >= ss->size)
     137                 :         {
     138                 :                 /* resize the allocation pointed to by stack */
     139             119 :                 struct node **old_stack = ss->stack;
     140             119 :                 size_t new_size = ss->size * 2;  /* XXX !!! */
     141             119 :                 ss->stack = realloc(old_stack, sizeof(struct node *)*new_size);
     142             119 :                 if (!ss->stack)
     143               0 :                         ss->stack = old_stack;  /* realloc failed */
     144                 :                 else
     145             119 :                         ss->size = new_size;
     146                 :         }
     147       150130917 : }

Generated by: LTP GCOV extension version 1.6