| 1 | /* $Id: prfcoreinit.cpp.h 29 2009-07-01 20:30:29Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kProfiler Mark 2 - Core Initialization 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 | * Calculates the size of the profiler data set.
|
|---|
| 34 | *
|
|---|
| 35 | * @returns The size of the data set in bytes.
|
|---|
| 36 | *
|
|---|
| 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.
|
|---|
| 42 | *
|
|---|
| 43 | * @remark This function does not input checks, it only aligns it. The caller is
|
|---|
| 44 | * responsible for the input to make some sense.
|
|---|
| 45 | */
|
|---|
| 46 | KPRF_DECL_FUNC(KU32, CalcSize)(KU32 cMaxFunctions, KU32 cbMaxModSegs, KU32 cMaxThreads, KU32 cMaxStacks, KU32 cMaxStackFrames)
|
|---|
| 47 | {
|
|---|
| 48 | /*
|
|---|
| 49 | * Normalize input.
|
|---|
| 50 | */
|
|---|
| 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);
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * Calc the size from the input.
|
|---|
| 59 | * We do not take overflows into account, stupid user means stupid result.
|
|---|
| 60 | */
|
|---|
| 61 | KU32 cb = KPRF_OFFSETOF(HDR, aiFunctions[cMaxFunctions]);
|
|---|
| 62 | KU32 cbTotal = KPRF_ALIGN(cb, 32);
|
|---|
| 63 |
|
|---|
| 64 | cb = cMaxFunctions * KPRF_SIZEOF(FUNC);
|
|---|
| 65 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 66 |
|
|---|
| 67 | cbTotal += cbMaxModSegs;
|
|---|
| 68 |
|
|---|
| 69 | cb = cMaxThreads * KPRF_SIZEOF(THREAD);
|
|---|
| 70 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 71 |
|
|---|
| 72 | cb = cMaxStacks * KPRF_SIZEOF(STACK);
|
|---|
| 73 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 74 |
|
|---|
| 75 | cb = cMaxStackFrames * cMaxStacks * KPRF_SIZEOF(FRAME);
|
|---|
| 76 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 77 |
|
|---|
| 78 | return cbTotal;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Initializes the profiler data set.
|
|---|
| 84 | *
|
|---|
| 85 | * @returns Pointer to the initialized profiler header on success.
|
|---|
| 86 | * @returns NULL if the input doesn't add up.
|
|---|
| 87 | *
|
|---|
| 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.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| 97 | KPRF_DECL_FUNC(KPRF_TYPE(P,HDR), Init)(void *pvData, KU32 cbData, KU32 cMaxFunctions, KU32 cbMaxModSegs,
|
|---|
| 98 | KU32 cMaxThreads, KU32 cMaxStacks, KU32 cMaxStackFrames)
|
|---|
| 99 | {
|
|---|
| 100 | /*
|
|---|
| 101 | * Normalize the input.
|
|---|
| 102 | */
|
|---|
| 103 | if (!pvData)
|
|---|
| 104 | return NULL;
|
|---|
| 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);
|
|---|
| 110 |
|
|---|
| 111 | /*
|
|---|
| 112 | * The header.
|
|---|
| 113 | */
|
|---|
| 114 | KU32 off = 0;
|
|---|
| 115 | KU32 cb = KPRF_OFFSETOF(HDR, aiFunctions[cMaxFunctions]);
|
|---|
| 116 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 117 | if (cbData < off + cb || off > off + cb)
|
|---|
| 118 | return NULL;
|
|---|
| 119 | KPRF_TYPE(P,HDR) pHdr = (KPRF_TYPE(P,HDR))pvData;
|
|---|
| 120 |
|
|---|
| 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. */
|
|---|
| 125 | #if KPRF_BITS <= 16
|
|---|
| 126 | pHdr->u16Reserved = 0;
|
|---|
| 127 | #endif
|
|---|
| 128 | #if KPRF_BITS <= 32
|
|---|
| 129 | pHdr->u32Reserved = 0;
|
|---|
| 130 | #endif
|
|---|
| 131 | pHdr->cb = cbData;
|
|---|
| 132 | pHdr->cbAllocated = cbData;
|
|---|
| 133 |
|
|---|
| 134 | /* functions */
|
|---|
| 135 | off += cb;
|
|---|
| 136 | cb = cMaxFunctions * KPRF_SIZEOF(FUNC);
|
|---|
| 137 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 138 | if (cbData < off + cb || off > off + cb)
|
|---|
| 139 | return NULL;
|
|---|
| 140 | pHdr->cMaxFunctions = cMaxFunctions;
|
|---|
| 141 | pHdr->cFunctions = 0;
|
|---|
| 142 | pHdr->offFunctions = off;
|
|---|
| 143 | pHdr->cbFunction = KPRF_SIZEOF(FUNC);
|
|---|
| 144 |
|
|---|
| 145 | /* modsegs */
|
|---|
| 146 | off += cb;
|
|---|
| 147 | cb = KPRF_ALIGN(cbMaxModSegs, 32);
|
|---|
| 148 | if (cbData < off + cb || off > off + cb)
|
|---|
| 149 | return NULL;
|
|---|
| 150 | pHdr->cbMaxModSegs = cbMaxModSegs;
|
|---|
| 151 | pHdr->cbModSegs = 0;
|
|---|
| 152 | pHdr->offModSegs = off;
|
|---|
| 153 |
|
|---|
| 154 | /* threads */
|
|---|
| 155 | off += cb;
|
|---|
| 156 | cb = cMaxThreads * KPRF_SIZEOF(THREAD);
|
|---|
| 157 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 158 | if (cbData < off + cb || off > off + cb)
|
|---|
| 159 | return NULL;
|
|---|
| 160 | pHdr->cMaxThreads = cMaxThreads;
|
|---|
| 161 | pHdr->cThreads = 0;
|
|---|
| 162 | pHdr->offThreads = off;
|
|---|
| 163 | pHdr->cbThread = KPRF_SIZEOF(THREAD);
|
|---|
| 164 |
|
|---|
| 165 | /* stacks */
|
|---|
| 166 | off += cb;
|
|---|
| 167 | cb = cMaxStacks * KPRF_OFFSETOF(STACK, aFrames[cMaxStackFrames]);
|
|---|
| 168 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 169 | if (cbData < off + cb || off > off + cb)
|
|---|
| 170 | return NULL;
|
|---|
| 171 | pHdr->cMaxStacks = cMaxStacks;
|
|---|
| 172 | pHdr->cStacks = 0;
|
|---|
| 173 | pHdr->offStacks = off;
|
|---|
| 174 | pHdr->cbStack = KPRF_OFFSETOF(STACK, aFrames[cMaxStackFrames]);
|
|---|
| 175 | pHdr->cMaxStackFrames = cMaxStackFrames;
|
|---|
| 176 |
|
|---|
| 177 | /* commandline */
|
|---|
| 178 | pHdr->offCommandLine = 0;
|
|---|
| 179 | pHdr->cchCommandLine = 0;
|
|---|
| 180 |
|
|---|
| 181 | /* the final size */
|
|---|
| 182 | pHdr->cb = off + cb;
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | /*
|
|---|
| 186 | * Done.
|
|---|
| 187 | */
|
|---|
| 188 | pHdr->u32Magic = KPRF_TYPE(,HDR_MAGIC);
|
|---|
| 189 | return pHdr;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|