3 * kProfiler Mark 2 - Core Initialization 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 * Calculates the size of the profiler data set.
35 * @returns The size of the data set in bytes.
37 * @param cMaxFunctions The max number of functions.
38 * @param cbMaxModSeg The max bytes for module segments.
39 * @param cMaxThreads The max number of threads.
40 * @param cMaxStacks The max number of stacks. (should be less or equal to the max number of threads)
41 * @param cMaxStackFrames The max number of frames on each of the stacks.
43 * @remark This function does not input checks, it only aligns it. The caller is
44 * responsible for the input to make some sense.
46 KPRF_DECL_FUNC(KU32
, CalcSize
)(KU32 cMaxFunctions
, KU32 cbMaxModSegs
, KU32 cMaxThreads
, KU32 cMaxStacks
, KU32 cMaxStackFrames
)
51 KPRF_SETMIN_ALIGN(cMaxFunctions
, 16, 16);
52 KPRF_SETMIN_ALIGN(cbMaxModSegs
, KPRF_SIZEOF(MODSEG
), 32);
53 KPRF_SETMIN_ALIGN(cMaxThreads
, 1, 1);
54 KPRF_SETMIN_ALIGN(cMaxStacks
, 1, 1);
55 KPRF_SETMIN_ALIGN(cMaxStackFrames
, 32, 32);
58 * Calc the size from the input.
59 * We do not take overflows into account, stupid user means stupid result.
61 KU32 cb
= KPRF_OFFSETOF(HDR
, aiFunctions
[cMaxFunctions
]);
62 KU32 cbTotal
= KPRF_ALIGN(cb
, 32);
64 cb
= cMaxFunctions
* KPRF_SIZEOF(FUNC
);
65 cbTotal
+= KPRF_ALIGN(cb
, 32);
67 cbTotal
+= cbMaxModSegs
;
69 cb
= cMaxThreads
* KPRF_SIZEOF(THREAD
);
70 cbTotal
+= KPRF_ALIGN(cb
, 32);
72 cb
= cMaxStacks
* KPRF_SIZEOF(STACK
);
73 cbTotal
+= KPRF_ALIGN(cb
, 32);
75 cb
= cMaxStackFrames
* cMaxStacks
* KPRF_SIZEOF(FRAME
);
76 cbTotal
+= KPRF_ALIGN(cb
, 32);
83 * Initializes the profiler data set.
85 * @returns Pointer to the initialized profiler header on success.
86 * @returns NULL if the input doesn't add up.
88 * @param pvData Where to initialize the profiler data set.
89 * @param cbData The size of the available data.
90 * @param cMaxFunctions The max number of functions.
91 * @param cbMaxModSeg The max bytes for module segments.
92 * @param cMaxThreads The max number of threads.
93 * @param cMaxStacks The max number of stacks. (should be less or equal to the max number of threads)
94 * @param cMaxStackFrames The max number of frames on each of the stacks.
97 KPRF_DECL_FUNC(KPRF_TYPE(P
,HDR
), Init
)(void *pvData
, KU32 cbData
, KU32 cMaxFunctions
, KU32 cbMaxModSegs
,
98 KU32 cMaxThreads
, KU32 cMaxStacks
, KU32 cMaxStackFrames
)
101 * Normalize the input.
105 KPRF_SETMIN_ALIGN(cMaxFunctions
, 16, 16);
106 KPRF_SETMIN_ALIGN(cbMaxModSegs
, KPRF_SIZEOF(MODSEG
), 32);
107 KPRF_SETMIN_ALIGN(cMaxThreads
, 1, 1);
108 KPRF_SETMIN_ALIGN(cMaxStacks
, 1, 1);
109 KPRF_SETMIN_ALIGN(cMaxStackFrames
, 32, 32);
115 KU32 cb
= KPRF_OFFSETOF(HDR
, aiFunctions
[cMaxFunctions
]);
116 cb
= KPRF_ALIGN(cb
, 32);
117 if (cbData
< off
+ cb
|| off
> off
+ cb
)
119 KPRF_TYPE(P
,HDR
) pHdr
= (KPRF_TYPE(P
,HDR
))pvData
;
121 /* the core header */
122 pHdr
->u32Magic
= 0; /* Set at the very end */
123 pHdr
->cFormatBits
= KPRF_BITS
;
124 pHdr
->uBasePtr
= 0; /* Can be set afterwards using SetBasePtr. */
126 pHdr
->u16Reserved
= 0;
129 pHdr
->u32Reserved
= 0;
132 pHdr
->cbAllocated
= cbData
;
136 cb
= cMaxFunctions
* KPRF_SIZEOF(FUNC
);
137 cb
= KPRF_ALIGN(cb
, 32);
138 if (cbData
< off
+ cb
|| off
> off
+ cb
)
140 pHdr
->cMaxFunctions
= cMaxFunctions
;
141 pHdr
->cFunctions
= 0;
142 pHdr
->offFunctions
= off
;
143 pHdr
->cbFunction
= KPRF_SIZEOF(FUNC
);
147 cb
= KPRF_ALIGN(cbMaxModSegs
, 32);
148 if (cbData
< off
+ cb
|| off
> off
+ cb
)
150 pHdr
->cbMaxModSegs
= cbMaxModSegs
;
152 pHdr
->offModSegs
= off
;
156 cb
= cMaxThreads
* KPRF_SIZEOF(THREAD
);
157 cb
= KPRF_ALIGN(cb
, 32);
158 if (cbData
< off
+ cb
|| off
> off
+ cb
)
160 pHdr
->cMaxThreads
= cMaxThreads
;
162 pHdr
->offThreads
= off
;
163 pHdr
->cbThread
= KPRF_SIZEOF(THREAD
);
167 cb
= cMaxStacks
* KPRF_OFFSETOF(STACK
, aFrames
[cMaxStackFrames
]);
168 cb
= KPRF_ALIGN(cb
, 32);
169 if (cbData
< off
+ cb
|| off
> off
+ cb
)
171 pHdr
->cMaxStacks
= cMaxStacks
;
173 pHdr
->offStacks
= off
;
174 pHdr
->cbStack
= KPRF_OFFSETOF(STACK
, aFrames
[cMaxStackFrames
]);
175 pHdr
->cMaxStackFrames
= cMaxStackFrames
;
178 pHdr
->offCommandLine
= 0;
179 pHdr
->cchCommandLine
= 0;
188 pHdr
->u32Magic
= KPRF_TYPE(,HDR_MAGIC
);