Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2647

Show
Ignore:
Timestamp:
10/12/07 15:59:51 (1 year ago)
Author:
sean
Message:

Added gc_addrOf(p) to find the base address of the block containing p. Related to ticket #557.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/common/tango/core/Memory.d

    r2404 r2647  
    2929    extern (C) void   gc_free( void* p ); 
    3030 
     31    extern (C) void*  gc_addrOf( void* p ); 
    3132    extern (C) size_t gc_sizeOf( void* p ); 
    3233 
     
    277278 
    278279    /** 
     280     * Returns the base address of the memory block containing p.  This value 
     281     * is useful to determine whether p is an interior pointer, and the result 
     282     * may be passed to routines such as sizeOf which may otherwise fail.  If p 
     283     * references memory not originally allocated by this garbage collector, if 
     284     * p is null, or if the garbage collector does not support this operation, 
     285     * null will be returned. 
     286     * 
     287     * Params: 
     288     *  p = A pointer to the root or the interior of a valid memory block or to 
     289     *      null. 
     290     * 
     291     * Returns: 
     292     *  The base address of the memory block referenced by p or null on error. 
     293     */ 
     294    static void* addrOf( void* p ) 
     295    { 
     296        return gc_addrOf( p ); 
     297    } 
     298 
     299 
     300    /** 
    279301     * Returns the true size of the memory block referenced by p.  This value 
    280302     * represents the maximum number of bytes for which a call to realloc may 
  • trunk/lib/gc/basic/gc.d

    r2464 r2647  
    127127} 
    128128 
     129extern (C) void* gc_addrOf( void* p ) 
     130{ 
     131    return _gc.addrOf( p ); 
     132} 
     133 
    129134extern (C) size_t gc_sizeOf( void* p ) 
    130135{ 
  • trunk/lib/gc/basic/gcx.d

    r2463 r2647  
    866866 
    867867    /** 
     868     * Determine the base address of the block containing p.  If p is not a gc 
     869     * allocated pointer, return null. 
     870     */ 
     871    void* addrOf(void *p) 
     872    { 
     873        if (!p) 
     874        { 
     875            return null; 
     876        } 
     877 
     878        if (!thread_needLock()) 
     879        { 
     880            return addrOfNoSync(p); 
     881        } 
     882        else synchronized (gcLock) 
     883        { 
     884            return addrOfNoSync(p); 
     885        } 
     886    } 
     887 
     888 
     889    // 
     890    // 
     891    // 
     892    void* addrOfNoSync(void *p) 
     893    { 
     894        if (!p) 
     895        { 
     896            return null; 
     897        } 
     898 
     899        return gcx.findBase(p); 
     900    } 
     901 
     902 
     903    /** 
    868904     * Determine the allocated size of pointer p.  If p is an interior pointer 
    869905     * or not a gc allocated pointer, return 0. 
     
    15271563                    break; 
    15281564                } 
     1565            } 
     1566        } 
     1567        return null; 
     1568    } 
     1569 
     1570 
     1571    /** 
     1572     * Find base address of block containing pointer p. 
     1573     * Returns null if not a gc'd pointer 
     1574     */ 
     1575    void* findBase(void *p) 
     1576    { 
     1577        Pool *pool; 
     1578 
     1579        pool = findPool(p); 
     1580        if (pool) 
     1581        { 
     1582            size_t offset = cast(size_t)(p - pool.baseAddr); 
     1583            uint pn = offset / PAGESIZE; 
     1584            Bins bin = cast(Bins)pool.pagetable[pn]; 
     1585 
     1586            // Adjust bit to be at start of allocated memory block 
     1587            if (bin <= B_PAGE) 
     1588            { 
     1589                return pool.baseAddr + (offset & notbinsize[bin]); 
     1590            } 
     1591            else if (bin == B_PAGEPLUS) 
     1592            { 
     1593                do 
     1594                {   --pn, offset -= PAGESIZE; 
     1595                } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); 
     1596 
     1597                return pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1))); 
     1598            } 
     1599            else 
     1600            { 
     1601                // we are in a B_FREE or B_UNCOMMITTED page 
     1602                return null; 
    15291603            } 
    15301604        } 
     
    18361910                if (pool) 
    18371911                { 
    1838                     size_t offset = cast(uint)(p - pool.baseAddr); 
     1912                    size_t offset = cast(size_t)(p - pool.baseAddr); 
    18391913                    uint biti; 
    18401914                    uint pn = offset / PAGESIZE; 
     
    24052479                pool = findPool(p); 
    24062480                assert(pool); 
    2407                 size_t offset = cast(uint)(p - pool.baseAddr); 
     2481                size_t offset = cast(size_t)(p - pool.baseAddr); 
    24082482                uint biti; 
    24092483                uint pn = offset / PAGESIZE; 
  • trunk/lib/gc/stub/gc.d

    r2494 r2647  
    114114} 
    115115 
     116extern (C) void* gc_addrOf( void* p ) 
     117{ 
     118    return null; 
     119} 
     120 
    116121extern (C) size_t gc_sizeOf( void* p ) 
    117122{