3 * kProfiler Mark 2 - Core NewFunction Code Template.
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
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.
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
);
47 KI32 iLast
= pHdr
->cFunctions
- 1;
51 KU32 iFunction
= pHdr
->aiFunctions
[i
];
52 KPRF_TYPE(,IPTR
) iDiff
= uPC
- paFunctions
[iFunction
].uEntryPtr
;
55 KPRF_FUNCS_WRITE_UNLOCK();
56 return &paFunctions
[iFunction
];
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
)
77 && paFunctions
[pHdr
->aiFunctions
[i
- 1]].uEntryPtr
> uPC
)
83 && paFunctions
[pHdr
->aiFunctions
[i
]].uEntryPtr
< uPC
)
88 * Ensure that there still is space for the function.
90 if (iNew
>= (KI32
)pHdr
->cMaxFunctions
)
92 KPRF_FUNCS_WRITE_UNLOCK();
96 KPRF_TYPE(P
,FUNC
) pNew
= &paFunctions
[iNew
];
98 /* init the new function entry */
99 pNew
->uEntryPtr
= uPC
;
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. */
114 pHdr
->aiFunctions
[j
] = pHdr
->aiFunctions
[j
- 1];
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
);