File Coverage

examples/memmap.pl
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition 1 2 50.0
subroutine 4 4 100.0
pod n/a
total 26 27 96.3


line stmt bran cond sub pod time code
1             #!/usr/local/pkg/cover/20070318/sw/bin/perl -w
2             ################################################################################
3             #
4             # $Project: /Convert-Binary-C $
5             # $Author: mhx $
6             # $Date: 2004/12/24 17:15:00 +0100 $
7             # $Revision: 8 $
8             # $Source: /bin/memmap.PL $
9             #
10             ################################################################################
11             #
12             # Copyright (c) 2002-2004 Marcus Holland-Moritz. All rights reserved.
13             # This program is free software; you can redistribute it and/or modify
14             # it under the same terms as Perl itself.
15             #
16             ################################################################################
17              
18             #===============================================================================
19             #
20             # Print a simple memory map of a structure.
21             #
22             #===============================================================================
23              
24 1     1   33 use Convert::Binary::C;
  1         10  
  1         15  
25 1     1   101 use Data::Dumper;
  1         10  
  1         22  
26 1     1   19 use strict;
  1         9  
  1         15  
27              
28             #-----------------------------------------------------
29             # Create an object, configure it, and parse some code.
30             #-----------------------------------------------------
31              
32 1         1906 my $c = Convert::Binary::C->new( PointerSize => 4,
33                                              LongSize => 4,
34                                              ShortSize => 2,
35                                              Alignment => 4 )
36                                       ->parse( <<'ENDC' );
37            
38             typedef unsigned long u_32;
39            
40             typedef struct _LinkedList * LinkedList;
41             typedef struct _HashTable * HashTable;
42            
43             typedef struct {
44             enum {
45             BO_BIG_ENDIAN,
46             BO_LITTLE_ENDIAN
47             } bo;
48             } ArchSpecs;
49            
50             typedef struct {
51             char *buffer;
52             long pos, length;
53             } Buffer;
54            
55             typedef struct {
56             unsigned alignment;
57             unsigned int_size;
58             unsigned short_size;
59             unsigned long_size;
60             unsigned long_long_size;
61             int enum_size;
62             unsigned ptr_size;
63             unsigned float_size;
64             unsigned double_size;
65             unsigned long_double_size;
66             u_32 flags;
67             u_32 keywords;
68             LinkedList disabled_keywords;
69             LinkedList includes;
70             LinkedList defines;
71             LinkedList assertions;
72             HashTable keyword_map;
73             } CParseConfig;
74            
75             typedef struct {
76             LinkedList enums;
77             LinkedList structs;
78             LinkedList typedef_lists;
79             HashTable htEnumerators;
80             HashTable htEnums;
81             HashTable htStructs;
82             HashTable htTypedefs;
83             HashTable htFiles;
84             char *errstr;
85             } CParseInfo;
86            
87             typedef struct {
88             char *bufptr;
89             unsigned alignment;
90             unsigned align_base;
91             int dataTooShortFlag;
92             Buffer buf;
93             CParseConfig cfg;
94             CParseInfo cpi;
95             ArchSpecs as;
96             enum {
97             ET_INTEGER, ET_STRING, ET_BOTH
98             } enumType;
99             } CBC;
100            
101             ENDC
102              
103             #-------------------------------------------------
104             # Print the memory map for type 'CBC' with a base
105             # address of 0x01500000.
106             #-------------------------------------------------
107              
108 1         25 memmap( $c, 'CBC', 0x01500000 );
109              
110             #==========================================================
111             # SUBROUTINES
112             #==========================================================
113              
114             sub memmap
115             {
116 1     1   24   my($c, $type, $start) = @_;
117 1   50     14   $start ||= 0;
118              
119 1         34   my $afmt = '%0' . 2*$c->PointerSize . 'X';
120              
121 1         85   for my $offset ( 0 .. $c->sizeof( $type ) - 1 ) {
122 140         2319     my $m = $c->member( $type, $offset );
123 140 100       1548     rindex( $m, '+' ) < 0 or next;
124 35         518     my $t = $c->typeof( $type.$m );
125 35         533     printf "$afmt %-16s %s\n", $start+$offset, $t, $m;
126               }
127             }
128