1 | /* $Id: prfcorefunction.cpp.h 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kProfiler Mark 2 - Core NewFunction Code Template.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * This file is part of kProfiler.
|
---|
10 | *
|
---|
11 | * kProfiler is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU Lesser General Public
|
---|
13 | * License as published by the Free Software Foundation; either
|
---|
14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kProfiler is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * Lesser General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU Lesser General Public
|
---|
22 | * License along with kProfiler; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Creates a new function.
|
---|
30 | *
|
---|
31 | * @returns Pointer to the new function.
|
---|
32 | * @returns NULL if we're out of space.
|
---|
33 | */
|
---|
34 | static KPRF_TYPE(P,FUNC) KPRF_NAME(NewFunction)(KPRF_TYPE(P,HDR) pHdr,KPRF_TYPE(,UPTR) uPC)
|
---|
35 | {
|
---|
36 | /*
|
---|
37 | * First find the position of the function (it might actually have been inserted by someone else by now too).
|
---|
38 | */
|
---|
39 | KPRF_FUNCS_WRITE_LOCK();
|
---|
40 |
|
---|
41 | KPRF_TYPE(P,FUNC) paFunctions = KPRF_OFF2PTR(P,FUNC, pHdr->offFunctions, pHdr);
|
---|
42 | KI32 iStart = 0;
|
---|
43 | KI32 iLast = pHdr->cFunctions - 1;
|
---|
44 | KI32 i = iLast / 2;
|
---|
45 | for (;;)
|
---|
46 | {
|
---|
47 | KU32 iFunction = pHdr->aiFunctions[i];
|
---|
48 | KPRF_TYPE(,IPTR) iDiff = uPC - paFunctions[iFunction].uEntryPtr;
|
---|
49 | if (!iDiff)
|
---|
50 | {
|
---|
51 | KPRF_FUNCS_WRITE_UNLOCK();
|
---|
52 | return &paFunctions[iFunction];
|
---|
53 | }
|
---|
54 | if (iLast == iStart)
|
---|
55 | break;
|
---|
56 | if (iDiff < 0)
|
---|
57 | iLast = i - 1;
|
---|
58 | else
|
---|
59 | iStart = i + 1;
|
---|
60 | if (iLast < iStart)
|
---|
61 | break;
|
---|
62 | i = iStart + (iLast - iStart) / 2;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Adjust the index so we're exactly in the right spot.
|
---|
67 | * (I've too much of a headache to figure out if the above loop leaves us where we should be.)
|
---|
68 | */
|
---|
69 | const KI32 iNew = pHdr->cFunctions;
|
---|
70 | if (paFunctions[pHdr->aiFunctions[i]].uEntryPtr > uPC)
|
---|
71 | {
|
---|
72 | while ( i > 0
|
---|
73 | && paFunctions[pHdr->aiFunctions[i - 1]].uEntryPtr > uPC)
|
---|
74 | i--;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | while ( i < iNew
|
---|
79 | && paFunctions[pHdr->aiFunctions[i]].uEntryPtr < uPC)
|
---|
80 | i++;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Ensure that there still is space for the function.
|
---|
85 | */
|
---|
86 | if (iNew >= (KI32)pHdr->cMaxFunctions)
|
---|
87 | {
|
---|
88 | KPRF_FUNCS_WRITE_UNLOCK();
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 | pHdr->cFunctions++;
|
---|
92 | KPRF_TYPE(P,FUNC) pNew = &paFunctions[iNew];
|
---|
93 |
|
---|
94 | /* init the new function entry */
|
---|
95 | pNew->uEntryPtr = uPC;
|
---|
96 | pNew->offModSeg = 0;
|
---|
97 | pNew->cOnStack = 0;
|
---|
98 | pNew->cCalls = 0;
|
---|
99 | pNew->OnStack.MinTicks = ~(KU64)0;
|
---|
100 | pNew->OnStack.MaxTicks = 0;
|
---|
101 | pNew->OnStack.SumTicks = 0;
|
---|
102 | pNew->OnTopOfStack.MinTicks = ~(KU64)0;
|
---|
103 | pNew->OnTopOfStack.MaxTicks = 0;
|
---|
104 | pNew->OnTopOfStack.SumTicks = 0;
|
---|
105 |
|
---|
106 | /* shift the function index array and insert the new one. */
|
---|
107 | KI32 j = iNew;
|
---|
108 | while (j > i)
|
---|
109 | {
|
---|
110 | pHdr->aiFunctions[j] = pHdr->aiFunctions[j - 1];
|
---|
111 | j--;
|
---|
112 | }
|
---|
113 | pHdr->aiFunctions[i] = iNew;
|
---|
114 | KPRF_FUNCS_WRITE_UNLOCK();
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Record the module segment (i.e. add it if it's new).
|
---|
118 | */
|
---|
119 | pNew->offModSeg = KPRF_NAME(RecordModSeg)(pHdr, uPC);
|
---|
120 |
|
---|
121 | return pNew;
|
---|
122 | }
|
---|
123 |
|
---|