| 1 | /* $Id: prfcorefunction.cpp.h 29 2009-07-01 20:30:29Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kProfiler Mark 2 - Core NewFunction Code Template.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
|---|
| 8 | *
|
|---|
| 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:
|
|---|
| 17 | *
|
|---|
| 18 | * The above copyright notice and this permission notice shall be
|
|---|
| 19 | * included in all copies or substantial portions of the Software.
|
|---|
| 20 | *
|
|---|
| 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.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Creates a new function.
|
|---|
| 34 | *
|
|---|
| 35 | * @returns Pointer to the new function.
|
|---|
| 36 | * @returns NULL if we're out of space.
|
|---|
| 37 | */
|
|---|
| 38 | static KPRF_TYPE(P,FUNC) KPRF_NAME(NewFunction)(KPRF_TYPE(P,HDR) pHdr,KPRF_TYPE(,UPTR) uPC)
|
|---|
| 39 | {
|
|---|
| 40 | /*
|
|---|
| 41 | * First find the position of the function (it might actually have been inserted by someone else by now too).
|
|---|
| 42 | */
|
|---|
| 43 | KPRF_FUNCS_WRITE_LOCK();
|
|---|
| 44 |
|
|---|
| 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 (;;)
|
|---|
| 50 | {
|
|---|
| 51 | KU32 iFunction = pHdr->aiFunctions[i];
|
|---|
| 52 | KPRF_TYPE(,IPTR) iDiff = uPC - paFunctions[iFunction].uEntryPtr;
|
|---|
| 53 | if (!iDiff)
|
|---|
| 54 | {
|
|---|
| 55 | KPRF_FUNCS_WRITE_UNLOCK();
|
|---|
| 56 | return &paFunctions[iFunction];
|
|---|
| 57 | }
|
|---|
| 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;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 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.)
|
|---|
| 72 | */
|
|---|
| 73 | const KI32 iNew = pHdr->cFunctions;
|
|---|
| 74 | if (paFunctions[pHdr->aiFunctions[i]].uEntryPtr > uPC)
|
|---|
| 75 | {
|
|---|
| 76 | while ( i > 0
|
|---|
| 77 | && paFunctions[pHdr->aiFunctions[i - 1]].uEntryPtr > uPC)
|
|---|
| 78 | i--;
|
|---|
| 79 | }
|
|---|
| 80 | else
|
|---|
| 81 | {
|
|---|
| 82 | while ( i < iNew
|
|---|
| 83 | && paFunctions[pHdr->aiFunctions[i]].uEntryPtr < uPC)
|
|---|
| 84 | i++;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | * Ensure that there still is space for the function.
|
|---|
| 89 | */
|
|---|
| 90 | if (iNew >= (KI32)pHdr->cMaxFunctions)
|
|---|
| 91 | {
|
|---|
| 92 | KPRF_FUNCS_WRITE_UNLOCK();
|
|---|
| 93 | return NULL;
|
|---|
| 94 | }
|
|---|
| 95 | pHdr->cFunctions++;
|
|---|
| 96 | KPRF_TYPE(P,FUNC) pNew = &paFunctions[iNew];
|
|---|
| 97 |
|
|---|
| 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;
|
|---|
| 109 |
|
|---|
| 110 | /* shift the function index array and insert the new one. */
|
|---|
| 111 | KI32 j = iNew;
|
|---|
| 112 | while (j > i)
|
|---|
| 113 | {
|
|---|
| 114 | pHdr->aiFunctions[j] = pHdr->aiFunctions[j - 1];
|
|---|
| 115 | j--;
|
|---|
| 116 | }
|
|---|
| 117 | pHdr->aiFunctions[i] = iNew;
|
|---|
| 118 | KPRF_FUNCS_WRITE_UNLOCK();
|
|---|
| 119 |
|
|---|
| 120 | /*
|
|---|
| 121 | * Record the module segment (i.e. add it if it's new).
|
|---|
| 122 | */
|
|---|
| 123 | pNew->offModSeg = KPRF_NAME(RecordModSeg)(pHdr, uPC);
|
|---|
| 124 |
|
|---|
| 125 | return pNew;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|