File indexing completed on 2024-04-28 09:36:47

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 #ifndef FIXCOST_H
0010 #define FIXCOST_H
0011 
0012 /**
0013  * Setting USE_FIXCOST to 1 enables a memory space hack:
0014  * For some data, build up internal data model lazy by using
0015  * the Fix*Cost classes, which are simple copies from input data.
0016  */
0017 #define USE_FIXCOST 1
0018 
0019 #include "tracedata.h"
0020 #include "pool.h"
0021 
0022 class PositionSpec
0023 {
0024 public:
0025     PositionSpec()
0026     { fromLine = 0, toLine = 0, fromAddr = 0, toAddr = 0; }
0027     PositionSpec(uint l1, uint l2, Addr a1, Addr a2)
0028     { fromLine = l1, toLine = l2, fromAddr = a1, toAddr = a2; }
0029 
0030     bool isLineRegion() const { return (fromLine != toLine); }
0031     bool isAddrRegion() const { return (fromAddr != toAddr); }
0032 
0033     uint fromLine, toLine;
0034     Addr fromAddr, toAddr;
0035 };
0036 
0037 /**
0038  * A class holding an unchangable cost item of an input file.
0039  *
0040  * As there can be a lot of such cost items, we use our own
0041  * allocator which uses FixPool
0042  */
0043 class FixCost
0044 {
0045 
0046 public:
0047     FixCost(TracePart*, FixPool*,
0048             TraceFunctionSource*,
0049             PositionSpec&,
0050             TracePartFunction*,
0051             FixString&);
0052 
0053     void *operator new(size_t size, FixPool*);
0054 
0055     void addTo(ProfileCostArray*);
0056 
0057     TracePart* part() const { return _part; }
0058     bool isLineRegion() const { return _pos.isLineRegion(); }
0059     bool isAddrRegion() const { return _pos.isAddrRegion(); }
0060     uint fromLine() const { return _pos.fromLine; }
0061     uint line() const { return _pos.fromLine; }
0062     uint toLine() const { return _pos.toLine; }
0063     Addr fromAddr() const { return _pos.fromAddr; }
0064     Addr addr() const { return _pos.fromAddr; }
0065     Addr toAddr() const { return _pos.toAddr; }
0066     TraceFunctionSource* functionSource() const { return _functionSource; }
0067 
0068     FixCost* nextCostOfPartFunction() const
0069     { return _nextCostOfPartFunction; }
0070 
0071 private:
0072     int _count;
0073     SubCost*  _cost;
0074     PositionSpec _pos;
0075 
0076     TracePart* _part;
0077     TraceFunctionSource* _functionSource;
0078     FixCost *_nextCostOfPartFunction;
0079 };
0080 
0081 /**
0082  * A FixCallCost will be inserted into a
0083  * - TracePartCall to keep source/target function info
0084  * - TraceFunctionSourceFile to keep file info of call source
0085  */
0086 class FixCallCost
0087 {
0088 
0089 public:
0090     FixCallCost(TracePart*, FixPool*,
0091                 TraceFunctionSource*,
0092                 unsigned int line,
0093                 Addr addr,
0094                 TracePartCall*,
0095                 SubCost, FixString&);
0096 
0097     void *operator new(size_t size, FixPool*);
0098 
0099     void addTo(TraceCallCost*);
0100     void setMax(ProfileCostArray*);
0101 
0102     TracePart* part() const { return _part; }
0103     unsigned int line() const { return _line; }
0104     Addr addr() const { return _addr; }
0105     SubCost callCount() const { return _cost[_count]; }
0106     TraceFunctionSource* functionSource() const { return _functionSource; }
0107     FixCallCost* nextCostOfPartCall() const
0108     { return _nextCostOfPartCall; }
0109 
0110 private:
0111     // we use 1 SubCost more than _count: _cost[_count] is the call count
0112     int _count;
0113     SubCost*  _cost;
0114     unsigned int _line;
0115     Addr _addr;
0116 
0117     TracePart* _part;
0118     TraceFunctionSource* _functionSource;
0119     FixCallCost* _nextCostOfPartCall;
0120 };
0121 
0122 /**
0123  * A class holding a jump (mostly) inside of a function
0124  */
0125 class FixJump
0126 {
0127 
0128 public:
0129     FixJump(TracePart*, FixPool*,
0130             /* source position */
0131             unsigned int line, Addr addr,
0132             TracePartFunction*, TraceFunctionSource*,
0133             /* target position */
0134             unsigned int targetLine, Addr targetAddr,
0135             TraceFunction*, TraceFunctionSource*,
0136             bool isCondJump,
0137             SubCost, SubCost);
0138 
0139     void *operator new(size_t size, FixPool*);
0140 
0141     void addTo(TraceJumpCost*);
0142 
0143     TracePart* part() const { return _part; }
0144     unsigned int line() const { return _line; }
0145     Addr addr() const { return _addr; }
0146     TraceFunctionSource* source() const { return _source; }
0147     TraceFunction* targetFunction() const { return _targetFunction; }
0148     unsigned int targetLine() const { return _targetLine; }
0149     Addr targetAddr() const { return _targetAddr; }
0150     TraceFunctionSource* targetSource() const { return _targetSource; }
0151     bool isCondJump() const { return _isCondJump; }
0152     SubCost executedCount() const { return _cost[0]; }
0153     SubCost followedCount() const
0154     { return _isCondJump ? _cost[1] : SubCost(0); }
0155 
0156     FixJump* nextJumpOfPartFunction() const
0157     { return _nextJumpOfPartFunction; }
0158 
0159 private:
0160     bool _isCondJump;
0161     SubCost* _cost;
0162     unsigned int _line, _targetLine;
0163     Addr _addr, _targetAddr;
0164     
0165     TracePart* _part;
0166     TraceFunctionSource *_source, *_targetSource;
0167     TraceFunction* _targetFunction;
0168     FixJump *_nextJumpOfPartFunction;
0169 };
0170 
0171 #endif
0172 
0173