kDefs.h: Added risc-v to the K_ARCH_XXX defines.
[kstuff-mirror.git] / kProfiler2 / prfcorefunction.cpp.h
bloba588f7e1a7304120535a1d54a5272eacca683f75
1 /* $Id$ */
2 /** @file
3 * kProfiler Mark 2 - Core NewFunction Code Template.
4 */
6 /*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
32 /**
33 * Creates a new function.
35 * @returns Pointer to the new function.
36 * @returns NULL if we're out of space.
38 static KPRF_TYPE(P,FUNC) KPRF_NAME(NewFunction)(KPRF_TYPE(P,HDR) pHdr,KPRF_TYPE(,UPTR) uPC)
41 * First find the position of the function (it might actually have been inserted by someone else by now too).
43 KPRF_FUNCS_WRITE_LOCK();
45 KPRF_TYPE(P,FUNC) paFunctions = KPRF_OFF2PTR(P,FUNC, pHdr->offFunctions, pHdr);
46 KI32 iStart = 0;
47 KI32 iLast = pHdr->cFunctions - 1;
48 KI32 i = iLast / 2;
49 for (;;)
51 KU32 iFunction = pHdr->aiFunctions[i];
52 KPRF_TYPE(,IPTR) iDiff = uPC - paFunctions[iFunction].uEntryPtr;
53 if (!iDiff)
55 KPRF_FUNCS_WRITE_UNLOCK();
56 return &paFunctions[iFunction];
58 if (iLast == iStart)
59 break;
60 if (iDiff < 0)
61 iLast = i - 1;
62 else
63 iStart = i + 1;
64 if (iLast < iStart)
65 break;
66 i = iStart + (iLast - iStart) / 2;
70 * Adjust the index so we're exactly in the right spot.
71 * (I've too much of a headache to figure out if the above loop leaves us where we should be.)
73 const KI32 iNew = pHdr->cFunctions;
74 if (paFunctions[pHdr->aiFunctions[i]].uEntryPtr > uPC)
76 while ( i > 0
77 && paFunctions[pHdr->aiFunctions[i - 1]].uEntryPtr > uPC)
78 i--;
80 else
82 while ( i < iNew
83 && paFunctions[pHdr->aiFunctions[i]].uEntryPtr < uPC)
84 i++;
88 * Ensure that there still is space for the function.
90 if (iNew >= (KI32)pHdr->cMaxFunctions)
92 KPRF_FUNCS_WRITE_UNLOCK();
93 return NULL;
95 pHdr->cFunctions++;
96 KPRF_TYPE(P,FUNC) pNew = &paFunctions[iNew];
98 /* init the new function entry */
99 pNew->uEntryPtr = uPC;
100 pNew->offModSeg = 0;
101 pNew->cOnStack = 0;
102 pNew->cCalls = 0;
103 pNew->OnStack.MinTicks = ~(KU64)0;
104 pNew->OnStack.MaxTicks = 0;
105 pNew->OnStack.SumTicks = 0;
106 pNew->OnTopOfStack.MinTicks = ~(KU64)0;
107 pNew->OnTopOfStack.MaxTicks = 0;
108 pNew->OnTopOfStack.SumTicks = 0;
110 /* shift the function index array and insert the new one. */
111 KI32 j = iNew;
112 while (j > i)
114 pHdr->aiFunctions[j] = pHdr->aiFunctions[j - 1];
115 j--;
117 pHdr->aiFunctions[i] = iNew;
118 KPRF_FUNCS_WRITE_UNLOCK();
121 * Record the module segment (i.e. add it if it's new).
123 pNew->offModSeg = KPRF_NAME(RecordModSeg)(pHdr, uPC);
125 return pNew;