Ignore:
Timestamp:
Aug 27, 2007, 6:06:17 AM (18 years ago)
Author:
bird
Message:

Use kStuff.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/kProfile/kProfileR3.cpp

    r3537 r3565  
     1/* $Id: $ */
     2/** @file
     3 * kProfiler Mark 2 - The Ring-3 Implementation.
     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 */
    126
    227
     
    833# include <psapi.h>
    934# include <malloc.h>
    10 typedef unsigned char uint8_t;
    11 typedef signed char int8_t;
    12 typedef unsigned short uint16_t;
    13 typedef signed short int16_t;
    14 typedef unsigned int uint32_t;
    15 typedef signed int int32_t;
    16 typedef unsigned _int64 uint64_t;
    17 typedef signed _int64 int64_t;
    18 typedef size_t uintptr_t;
    1935# if _MSC_VER >= 1400
    2036#  include <intrin.h>
     
    4561#endif
    4662
     63#include <k/kDefs.h>
     64#include <k/kTypes.h>
    4765
    4866/*
     
    100118/** Update the state. */
    101119#define KPRF_RWLOCK_SETSTATE(pRWLock, enmNewState) \
    102     kPrfAtomicSet32((volatile uint32_t *)&(pRWLock)->enmState, (uint32_t)(enmNewState))
     120    kPrfAtomicSet32((volatile KU32 *)&(pRWLock)->enmState, (KU32)(enmNewState))
    103121
    104122/** Read/Write lock type. */
     
    109127    KPRF_TYPE(,MUTEX)       Mutex;
    110128    /** The current number of readers. */
    111     uint32_t                cReaders;
     129    KU32                    cReaders;
    112130    /** The number of readers waiting. */
    113     uint32_t                cReadersWaiting;
     131    KU32                    cReadersWaiting;
    114132    /** The current number of waiting writers. */
    115     uint32_t                cWritersWaiting;
     133    KU32                    cWritersWaiting;
    116134# if defined(KPRF_OS_WINDOWS)
    117135    /** The handle of the event object on which the waiting readers block. (manual reset). */
     
    218236 * @returns The thread id.
    219237 */
    220 static inline uintptr_t kPrfGetThreadId(void)
     238static inline KUPTR kPrfGetThreadId(void)
    221239{
    222240/* Win32/64 */
    223241#if defined(KPRF_OS_WINDOWS)
    224     uintptr_t ThreadId = (uintptr_t)GetCurrentThreadId();
     242    KUPTR ThreadId = (KUPTR)GetCurrentThreadId();
    225243
    226244/* Posix Threads */
    227245#elif defined(KPRF_USE_PTHREAD)
    228     uintptr_t ThreadId = (uintptr_t)pthread_self();
     246    KUPTR ThreadId = (KUPTR)pthread_self();
    229247
    230248#elif defined(KPRF_OS_OS2)
     
    248266 * @returns The process id.
    249267 */
    250 static inline uintptr_t kPrfGetProcessId(void)
     268static inline KUPTR kPrfGetProcessId(void)
    251269{
    252270/* Win32/64 */
    253271#if defined(KPRF_OS_WINDOWS)
    254     uintptr_t ThreadId = (uintptr_t)GetProcessId(GetCurrentProcess());
     272    KUPTR ThreadId = (KUPTR)GetProcessId(GetCurrentProcess());
    255273
    256274#elif defined(KPRF_OS_OS2)
     
    261279
    262280#else
    263     uintptr_t ThreadId = (uintptr_t)getpid();
     281    KUPTR ThreadId = (KUPTR)getpid();
    264282#endif
    265283
     
    302320 * This must correspond to what the assembly code are doing.
    303321 */
    304 static inline uint64_t kPrfNow(void)
     322static inline KU64 kPrfNow(void)
    305323{
    306324#if defined(HAVE_INTRIN)
     
    309327    union
    310328    {
    311         uint64_t u64;
     329        KU64 u64;
    312330        struct
    313331        {
    314             uint32_t u32Lo;
    315             uint32_t u32Hi;
     332            KU32 u32Lo;
     333            KU32 u32Hi;
    316334        } s;
    317335    } u;
     
    336354 * Atomically set a 32-bit value.
    337355 */
    338 static inline void kPrfAtomicSet32(volatile uint32_t *pu32, const uint32_t u32)
     356static inline void kPrfAtomicSet32(volatile KU32 *pu32, const KU32 u32)
    339357{
    340358#if defined(HAVE_INTRIN)
     
    365383 * Atomically set a 64-bit value.
    366384 */
    367 static inline void kPrfAtomicSet64(volatile uint64_t *pu64, uint64_t u64)
     385static inline void kPrfAtomicSet64(volatile KU64 *pu64, KU64 u64)
    368386{
    369387#if defined(HAVE_INTRIN) && KPRF_BITS == 64
    370     _InterlockedExchange64((int64_t *)pu64, (const int64_t)u64);
     388    _InterlockedExchange64((KI64 *)pu64, (const KI64)u64);
    371389
    372390#elif defined(__GNUC__) && KPRF_BITS == 64
     
    382400                           "=m" (*pu64)
    383401                         : "0" (*pu64),
    384                            "b" ( (uint32_t)u64 ),
    385                            "c" ( (uint32_t)(u64 >> 32) ));
     402                           "b" ( (KU32)u64 ),
     403                           "c" ( (KU32)(u64 >> 32) ));
    386404
    387405#elif _MSC_VER
     
    407425 * Atomically add a 32-bit integer to another.
    408426 */
    409 static inline void kPrfAtomicAdd32(volatile uint32_t *pu32, const uint32_t u32)
     427static inline void kPrfAtomicAdd32(volatile KU32 *pu32, const KU32 u32)
    410428{
    411429#if defined(HAVE_INTRIN)
     
    431449#define KPRF_ATOMIC_ADD32(a,b)          kPrfAtomicAdd32(a, b)
    432450#define KPRF_ATOMIC_INC32(a)            kPrfAtomicAdd32(a, 1);
    433 #define KPRF_ATOMIC_DEC32(a)            kPrfAtomicAdd32(a, (uint32_t)-1);
     451#define KPRF_ATOMIC_DEC32(a)            kPrfAtomicAdd32(a, (KU32)-1);
    434452
    435453
     
    438456 * Atomically isn't quite required, just a non-corruptive manner, assuming all updates are adds.
    439457 */
    440 static inline void kPrfAtomicAdd64(volatile uint64_t *pu64, const uint64_t u64)
     458static inline void kPrfAtomicAdd64(volatile KU64 *pu64, const KU64 u64)
    441459{
    442460#if defined(HAVE_INTRIN) && KPRF_BITS == 64
    443     _InterlockedExchangeAdd64((volatile int64_t *)pu64, (const int64_t)u64);
     461    _InterlockedExchangeAdd64((volatile KI64 *)pu64, (const KI64)u64);
    444462
    445463#elif defined(__GNUC__) && KPRF_BITS == 64
     
    451469    __asm__ __volatile__("lock; addl %0, %2\n\t"
    452470                         "lock; adcl %1, %3\n\t"
    453                          : "=m" (*(volatile uint32_t *)pu64),
    454                            "=m" (*((volatile uint32_t *)pu64 + 1))
    455                          : "r" ((uint32_t)u64),
    456                            "r" ((uint32_t)(u64 >> 32)));
     471                         : "=m" (*(volatile KU32 *)pu64),
     472                           "=m" (*((volatile KU32 *)pu64 + 1))
     473                         : "r" ((KU32)u64),
     474                           "r" ((KU32)(u64 >> 32)));
    457475
    458476#elif _MSC_VER
     
    922940 *
    923941 */
    924 static int kPrfGetModSeg(KPRF_TYPE(,UPTR) uAddress, char *pszPath, uint32_t cchPath, uint32_t *piSegment,
     942static int kPrfGetModSeg(KPRF_TYPE(,UPTR) uAddress, char *pszPath, KU32 cchPath, KU32 *piSegment,
    925943                         KPRF_TYPE(P,UPTR) puBasePtr, KPRF_TYPE(P,UPTR) pcbSegmentMinusOne)
    926944{
     
    945963            __try
    946964            {
    947                 const uintptr_t uImageBase = (uintptr_t)pahModules[i];
     965                const KUPTR uImageBase = (KUPTR)pahModules[i];
    948966                union
    949967                {
    950                     uint8_t            *pu8;
     968                    KU8                *pu8;
    951969                    PIMAGE_DOS_HEADER   pDos;
    952970                    PIMAGE_NT_HEADERS   pNt;
    953971                    PIMAGE_NT_HEADERS32 pNt32;
    954972                    PIMAGE_NT_HEADERS64 pNt64;
    955                     uintptr_t           u;
     973                    KUPTR               u;
    956974                } u;
    957975                u.u = uImageBase;
     
    970988
    971989                /* Extract necessary info from the optional header (comes in 32-bit and 64-bit variations, we simplify a bit). */
    972                 uint32_t                cbImage;
     990                KU32                    cbImage;
    973991                PIMAGE_SECTION_HEADER   paSHs;
    974992                if (u.pNt->FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32))
     
    9861004
    9871005                /* Is our address within the image size */
    988                 uintptr_t uRVA = uAddress - (uintptr_t)pahModules[i];
     1006                KUPTR uRVA = uAddress - (KUPTR)pahModules[i];
    9891007                if (uRVA >= cbImage)
    9901008                    continue;
     
    9941012                 * (segment == section + 1)
    9951013                 */
    996                 const uint32_t cSHs = u.pNt->FileHeader.NumberOfSections;
     1014                const KU32 cSHs = u.pNt->FileHeader.NumberOfSections;
    9971015                if (uRVA < paSHs[0].VirtualAddress)
    9981016                {
     
    10041022                else
    10051023                {
    1006                     uint32_t iSH = 0;
     1024                    KU32 iSH = 0;
    10071025                    for (;;)
    10081026                    {
     
    10631081        ULONG cb     = ~0UL;
    10641082        ULONG fFlags = ~0UL;
    1065         uAddress &= ~(uintptr_t)0xfff;
     1083        uAddress &= ~(KUPTR)0xfff;
    10661084        rc = DosQueryMem((PVOID)(uAddress, &cb, &fFlags);
    10671085        if (!rc)
    10681086        {
    1069             *pcbSegmentMinusOne = (offObj & ~(uintptr_t)0xfff) + KPRF_ALIGN(cb, 0x1000) - 1;
     1087            *pcbSegmentMinusOne = (offObj & ~(KUPTR)0xfff) + KPRF_ALIGN(cb, 0x1000) - 1;
    10701088            if ((fFlags & PAG_BASE) && cb <= 0x1000) /* don't quite remember if PAG_BASE returns one page or not */
    10711089            {
     
    11181136static KPRF_TYPE(P,THREAD) kPrfGetThreadAutoReg(void)
    11191137{
    1120     uintptr_t uStackBasePtr;
     1138    KUPTR uStackBasePtr;
    11211139
    11221140#if 0
     
    11291147    DosGetInfoBlocks(&pTib, &pPib); /* never fails except if you give it bad input, thus 'Get' not 'Query'. */
    11301148    /* I never recall which of these is the right one... */
    1131     uStackBasePtr = (uintptr_t)pTib->tib_pstack < (uintptr_t)pTib->tib_pstack_limit
    1132         ? (uintptr_t)pTib->tib_pstack
    1133         : (uintptr_t)pTib->tib_pstack_limit;
     1149    uStackBasePtr = (KUPTR)pTib->tib_pstack < (KUPTR)pTib->tib_pstack_limit
     1150        ? (KUPTR)pTib->tib_pstack
     1151        : (KUPTR)pTib->tib_pstack_limit;
    11341152
    11351153#else
    11361154    /* the default is top of the current stack page (assuming a page to be 4KB) */
    1137     uStackBasePtr = (uintptr_t)&uStackBasePtr;
    1138     uStackBasePtr = (uStackBasePtr + 0xfff) & ~(uintptr_t)0xfff;
     1155    uStackBasePtr = (KUPTR)&uStackBasePtr;
     1156    uStackBasePtr = (uStackBasePtr + 0xfff) & ~(KUPTR)0xfff;
    11391157#endif
    11401158
     
    11521170 * @param   pszDefault  The default value.
    11531171 */
    1154 static char *kPrfGetEnvString(const char *pszVar, char *pszValue, uint32_t cchValue, const char *pszDefault)
     1172static char *kPrfGetEnvString(const char *pszVar, char *pszValue, KU32 cchValue, const char *pszDefault)
    11551173{
    11561174#if defined(KPRF_OS_WINDOWS)
     
    11911209 * @param   uDefault    The default value.
    11921210 */
    1193 static uint32_t kPrfGetEnvValue(const char *pszVar, uint32_t uDefault)
     1211static KU32 kPrfGetEnvValue(const char *pszVar, KU32 uDefault)
    11941212{
    11951213#if defined(KPRF_OS_WINDOWS)
     
    12231241     */
    12241242    unsigned    uBase = 10;
    1225     uint32_t    uValue = 0;
     1243    KU32        uValue = 0;
    12261244    const char *psz = pszValue;
    12271245
     
    12791297 * @param   cb      The amount of memory (in bytes) to allocate.
    12801298 */
    1281 static void *kPrfAllocMem(uint32_t cb)
     1299static void *kPrfAllocMem(KU32 cb)
    12821300{
    12831301#if defined(KPRF_OS_WINDOWS)
     
    13421360 * @param   cbData      The amount of data to write.
    13431361 */
    1344 static int kPrfWriteFile(const char *pszName, const void *pvData, uint32_t cbData)
     1362static int kPrfWriteFile(const char *pszName, const void *pvData, KU32 cbData)
    13451363{
    13461364#if defined(KPRF_OS_WINDOWS)
     
    14121430     * Initial suggestions.
    14131431     */
    1414     uint32_t    cbModSegs  = kPrfGetEnvValue("KPRF2_CBMODSEGS",  128*1024);
    1415     uint32_t    cFunctions = kPrfGetEnvValue("KPRF2_CFUNCTIONS", 8192);
    1416     uint32_t    cThreads   = kPrfGetEnvValue("KPRF2_CTHREADS",   256);
    1417     uint32_t    cStacks    = kPrfGetEnvValue("KPRF2_CSTACKS",    48);
    1418     uint32_t    cFrames    = kPrfGetEnvValue("KPRF2_CFRAMES",    448);
    1419 
    1420     uint32_t cb = KPRF_NAME(CalcSize)(cFunctions, cbModSegs, cThreads, cStacks, cFrames);
     1432    KU32    cbModSegs  = kPrfGetEnvValue("KPRF2_CBMODSEGS",  128*1024);
     1433    KU32    cFunctions = kPrfGetEnvValue("KPRF2_CFUNCTIONS", 8192);
     1434    KU32    cThreads   = kPrfGetEnvValue("KPRF2_CTHREADS",   256);
     1435    KU32    cStacks    = kPrfGetEnvValue("KPRF2_CSTACKS",    48);
     1436    KU32    cFrames    = kPrfGetEnvValue("KPRF2_CFRAMES",    448);
     1437
     1438    KU32    cb = KPRF_NAME(CalcSize)(cFunctions, cbModSegs, cThreads, cStacks, cFrames);
    14211439
    14221440    /*
     
    15481566
    15491567    /* append the process id */
    1550     uintptr_t pid = kPrfGetProcessId();
     1568    KUPTR pid = kPrfGetProcessId();
    15511569    char *psz = szName;
    15521570    while (*psz)
     
    15541572
    15551573    static char s_szDigits[0x11] = "0123456789abcdef";
    1556     uint32_t uShift = KPRF_BITS - 4;
     1574    KU32 uShift = KPRF_BITS - 4;
    15571575    while (     uShift > 0
    15581576           &&   !(pid & (0xf << uShift)))
Note: See TracChangeset for help on using the changeset viewer.