Announcement

Collapse
No announcement yet.

Interlocked API and PB9 array elements

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    Ask PB Support and you shall receive...
    Hi Michael,

    You could use a s String, Strings are always DWORD aligned.

    Sincerely,
    Jeff Daniels
    PowerBASIC Staff
    MCM

    Leave a comment:


  • Michael Mattias
    replied
    This is interesting..... it suggests that any block of memory allocated from the heap is always DWORD-aligned.
    However, I cannot find that claim in a statement from Microsoft anywhere.

    Source: http://blogs.msdn.com/oldnewthing/ar...7/6873648.aspx

    Code:
    struct misaligned_members {
     WORD w;
     DWORD dw;
     BYTE b;
    };
    Given this structure, you cannot pass the address of the dw member to a function that expects a pointer to a DWORD, since the ground rules for programming specify that all pointers must be aligned unless unaligned pointers are explicitly permitted.
    Code:
    void ExpectsAlignedPointer(DWORD *pdw);
    void UnalignedPointerOkay(UNALIGNED DWORD *pdw);
    
    misaligned_members s;
    ExpectsAlignedPointer(&s.dw); // wrong
    UnalignedPointerOkay(&s.dw);  // okay
    What about the member w? Is it aligned or not? Well, it depends.

    If you allocate a single structure on the heap, then the w member is aligned, since heap allocations are always aligned in a manner suitable for any fundamental data type. [bold mine MCM]. (I vaguely recall some possible weirdness with 10-byte floating point values, but that's not relevant to the topic at hand.)

    Code:
    misaligned_members *p = (misaligned_members)
        HeapAllocate(hheap, 0, sizeof(misaligned_members));
    Given this code fragment, the member p->w is aligned since the entire structure is suitably aligned, and therefore so too is w.
    MCM

    Leave a comment:


  • Michael Mattias
    replied
    Is it safe to assume in PB9 that the dword elements of an array of dwords will always be 32 bit aligned in memory? Would I have any potential issues using these functions with elements of a dword array?
    That's really a question for PB, since "how" array memory is allocated is proprietary.

    Frankly I would be surprised if it were NOT 32-bit aligned.

    You can take out insurance by allocating your own memory using one of the functions which guarantees this alignment ( e.g., VirtualAlloc).

    Or, you can allocate one additional element for your array, and shift the bits until VARPTR (Element 0) MOD 4 = 0

    MCM

    Leave a comment:


  • jcfuller
    replied
    Paul,
    As I understand it you should have no problems with dword arrays.

    #ALIGN is useful in a situation where you have raw data and want to make sure it is aligned.
    #ALIGN 32
    DataStart:
    !db 1,2,3,4,5,6

    James

    Leave a comment:


  • Rodney Hicks
    replied
    Check the
    #ALIGN metastatement
    in PBWin 9.1

    Leave a comment:


  • Paul Johnson
    started a topic Interlocked API and PB9 array elements

    Interlocked API and PB9 array elements

    I am wanting to use the Interlocked api functions to read/write to some dwords from multiple threads. The dwords are part of a Powerbasic array of type dword. One requirement for the Interlocked api functions is that the dword being read from / written to, is that it be aligned on 32 bit boundaries in order to ensure the reads/writes are atomic.

    Is it safe to assume in PB9 that the dword elements of an array of dwords will always be 32 bit aligned in memory? Would I have any potential issues using these functions with elements of a dword array?

    The functions are:

    InterlockedIncrement
    InterlockedDecrement
    and InterlockedExchange I believe
Working...
X