| 1 | /* $Id: prfcoreinit.cpp.h 2 2007-11-16 16:07:14Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kProfiler Mark 2 - Core Initialization Code Template.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kProfiler.
|
|---|
| 10 | *
|
|---|
| 11 | * kProfiler is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kProfiler is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kProfiler; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Calculates the size of the profiler data set.
|
|---|
| 30 | *
|
|---|
| 31 | * @returns The size of the data set in bytes.
|
|---|
| 32 | *
|
|---|
| 33 | * @param cMaxFunctions The max number of functions.
|
|---|
| 34 | * @param cbMaxModSeg The max bytes for module segments.
|
|---|
| 35 | * @param cMaxThreads The max number of threads.
|
|---|
| 36 | * @param cMaxStacks The max number of stacks. (should be less or equal to the max number of threads)
|
|---|
| 37 | * @param cMaxStackFrames The max number of frames on each of the stacks.
|
|---|
| 38 | *
|
|---|
| 39 | * @remark This function does not input checks, it only aligns it. The caller is
|
|---|
| 40 | * responsible for the input to make some sense.
|
|---|
| 41 | */
|
|---|
| 42 | KPRF_DECL_FUNC(KU32, CalcSize)(KU32 cMaxFunctions, KU32 cbMaxModSegs, KU32 cMaxThreads, KU32 cMaxStacks, KU32 cMaxStackFrames)
|
|---|
| 43 | {
|
|---|
| 44 | /*
|
|---|
| 45 | * Normalize input.
|
|---|
| 46 | */
|
|---|
| 47 | KPRF_SETMIN_ALIGN(cMaxFunctions, 16, 16);
|
|---|
| 48 | KPRF_SETMIN_ALIGN(cbMaxModSegs, KPRF_SIZEOF(MODSEG), 32);
|
|---|
| 49 | KPRF_SETMIN_ALIGN(cMaxThreads, 1, 1);
|
|---|
| 50 | KPRF_SETMIN_ALIGN(cMaxStacks, 1, 1);
|
|---|
| 51 | KPRF_SETMIN_ALIGN(cMaxStackFrames, 32, 32);
|
|---|
| 52 |
|
|---|
| 53 | /*
|
|---|
| 54 | * Calc the size from the input.
|
|---|
| 55 | * We do not take overflows into account, stupid user means stupid result.
|
|---|
| 56 | */
|
|---|
| 57 | KU32 cb = KPRF_OFFSETOF(HDR, aiFunctions[cMaxFunctions]);
|
|---|
| 58 | KU32 cbTotal = KPRF_ALIGN(cb, 32);
|
|---|
| 59 |
|
|---|
| 60 | cb = cMaxFunctions * KPRF_SIZEOF(FUNC);
|
|---|
| 61 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 62 |
|
|---|
| 63 | cbTotal += cbMaxModSegs;
|
|---|
| 64 |
|
|---|
| 65 | cb = cMaxThreads * KPRF_SIZEOF(THREAD);
|
|---|
| 66 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 67 |
|
|---|
| 68 | cb = cMaxStacks * KPRF_SIZEOF(STACK);
|
|---|
| 69 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 70 |
|
|---|
| 71 | cb = cMaxStackFrames * cMaxStacks * KPRF_SIZEOF(FRAME);
|
|---|
| 72 | cbTotal += KPRF_ALIGN(cb, 32);
|
|---|
| 73 |
|
|---|
| 74 | return cbTotal;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Initializes the profiler data set.
|
|---|
| 80 | *
|
|---|
| 81 | * @returns Pointer to the initialized profiler header on success.
|
|---|
| 82 | * @returns NULL if the input doesn't add up.
|
|---|
| 83 | *
|
|---|
| 84 | * @param pvData Where to initialize the profiler data set.
|
|---|
| 85 | * @param cbData The size of the available data.
|
|---|
| 86 | * @param cMaxFunctions The max number of functions.
|
|---|
| 87 | * @param cbMaxModSeg The max bytes for module segments.
|
|---|
| 88 | * @param cMaxThreads The max number of threads.
|
|---|
| 89 | * @param cMaxStacks The max number of stacks. (should be less or equal to the max number of threads)
|
|---|
| 90 | * @param cMaxStackFrames The max number of frames on each of the stacks.
|
|---|
| 91 | *
|
|---|
| 92 | */
|
|---|
| 93 | KPRF_DECL_FUNC(KPRF_TYPE(P,HDR), Init)(void *pvData, KU32 cbData, KU32 cMaxFunctions, KU32 cbMaxModSegs,
|
|---|
| 94 | KU32 cMaxThreads, KU32 cMaxStacks, KU32 cMaxStackFrames)
|
|---|
| 95 | {
|
|---|
| 96 | /*
|
|---|
| 97 | * Normalize the input.
|
|---|
| 98 | */
|
|---|
| 99 | if (!pvData)
|
|---|
| 100 | return NULL;
|
|---|
| 101 | KPRF_SETMIN_ALIGN(cMaxFunctions, 16, 16);
|
|---|
| 102 | KPRF_SETMIN_ALIGN(cbMaxModSegs, KPRF_SIZEOF(MODSEG), 32);
|
|---|
| 103 | KPRF_SETMIN_ALIGN(cMaxThreads, 1, 1);
|
|---|
| 104 | KPRF_SETMIN_ALIGN(cMaxStacks, 1, 1);
|
|---|
| 105 | KPRF_SETMIN_ALIGN(cMaxStackFrames, 32, 32);
|
|---|
| 106 |
|
|---|
| 107 | /*
|
|---|
| 108 | * The header.
|
|---|
| 109 | */
|
|---|
| 110 | KU32 off = 0;
|
|---|
| 111 | KU32 cb = KPRF_OFFSETOF(HDR, aiFunctions[cMaxFunctions]);
|
|---|
| 112 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 113 | if (cbData < off + cb || off > off + cb)
|
|---|
| 114 | return NULL;
|
|---|
| 115 | KPRF_TYPE(P,HDR) pHdr = (KPRF_TYPE(P,HDR))pvData;
|
|---|
| 116 |
|
|---|
| 117 | /* the core header */
|
|---|
| 118 | pHdr->u32Magic = 0; /* Set at the very end */
|
|---|
| 119 | pHdr->cFormatBits = KPRF_BITS;
|
|---|
| 120 | pHdr->uBasePtr = 0; /* Can be set afterwards using SetBasePtr. */
|
|---|
| 121 | #if KPRF_BITS <= 16
|
|---|
| 122 | pHdr->u16Reserved = 0;
|
|---|
| 123 | #endif
|
|---|
| 124 | #if KPRF_BITS <= 32
|
|---|
| 125 | pHdr->u32Reserved = 0;
|
|---|
| 126 | #endif
|
|---|
| 127 | pHdr->cb = cbData;
|
|---|
| 128 | pHdr->cbAllocated = cbData;
|
|---|
| 129 |
|
|---|
| 130 | /* functions */
|
|---|
| 131 | off += cb;
|
|---|
| 132 | cb = cMaxFunctions * KPRF_SIZEOF(FUNC);
|
|---|
| 133 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 134 | if (cbData < off + cb || off > off + cb)
|
|---|
| 135 | return NULL;
|
|---|
| 136 | pHdr->cMaxFunctions = cMaxFunctions;
|
|---|
| 137 | pHdr->cFunctions = 0;
|
|---|
| 138 | pHdr->offFunctions = off;
|
|---|
| 139 | pHdr->cbFunction = KPRF_SIZEOF(FUNC);
|
|---|
| 140 |
|
|---|
| 141 | /* modsegs */
|
|---|
| 142 | off += cb;
|
|---|
| 143 | cb = KPRF_ALIGN(cbMaxModSegs, 32);
|
|---|
| 144 | if (cbData < off + cb || off > off + cb)
|
|---|
| 145 | return NULL;
|
|---|
| 146 | pHdr->cbMaxModSegs = cbMaxModSegs;
|
|---|
| 147 | pHdr->cbModSegs = 0;
|
|---|
| 148 | pHdr->offModSegs = off;
|
|---|
| 149 |
|
|---|
| 150 | /* threads */
|
|---|
| 151 | off += cb;
|
|---|
| 152 | cb = cMaxThreads * KPRF_SIZEOF(THREAD);
|
|---|
| 153 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 154 | if (cbData < off + cb || off > off + cb)
|
|---|
| 155 | return NULL;
|
|---|
| 156 | pHdr->cMaxThreads = cMaxThreads;
|
|---|
| 157 | pHdr->cThreads = 0;
|
|---|
| 158 | pHdr->offThreads = off;
|
|---|
| 159 | pHdr->cbThread = KPRF_SIZEOF(THREAD);
|
|---|
| 160 |
|
|---|
| 161 | /* stacks */
|
|---|
| 162 | off += cb;
|
|---|
| 163 | cb = cMaxStacks * KPRF_OFFSETOF(STACK, aFrames[cMaxStackFrames]);
|
|---|
| 164 | cb = KPRF_ALIGN(cb, 32);
|
|---|
| 165 | if (cbData < off + cb || off > off + cb)
|
|---|
| 166 | return NULL;
|
|---|
| 167 | pHdr->cMaxStacks = cMaxStacks;
|
|---|
| 168 | pHdr->cStacks = 0;
|
|---|
| 169 | pHdr->offStacks = off;
|
|---|
| 170 | pHdr->cbStack = KPRF_OFFSETOF(STACK, aFrames[cMaxStackFrames]);
|
|---|
| 171 | pHdr->cMaxStackFrames = cMaxStackFrames;
|
|---|
| 172 |
|
|---|
| 173 | /* commandline */
|
|---|
| 174 | pHdr->offCommandLine = 0;
|
|---|
| 175 | pHdr->cchCommandLine = 0;
|
|---|
| 176 |
|
|---|
| 177 | /* the final size */
|
|---|
| 178 | pHdr->cb = off + cb;
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 | /*
|
|---|
| 182 | * Done.
|
|---|
| 183 | */
|
|---|
| 184 | pHdr->u32Magic = KPRF_TYPE(,HDR_MAGIC);
|
|---|
| 185 | return pHdr;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|