static bool SplitB2Fix(const IMI::FixB2 *fixB2, const IMI::FixB *fixFull, IMI::FixB *fix1, IMI::FixB *fix2) { if (fixB2->id != IMIFIX_ID_B2_RECORD || fixFull->id != IMIFIX_ID_B_RECORD) return false; fix1->id = IMIFIX_ID_B_RECORD; fix1->fv = fixFull->fv; fix1->time = fixFull->time + fixB2->time1 + 1; fix1->enl = fixB2->enl1; fix1->alt = (long)fixFull->alt + fixB2->alt1; fix1->gpsalt = (long)fixFull->gpsalt + fixB2->gpsalt1; fix1->lat = AngleAdd((IMI::IMIDWORD)fixFull->lat, (long)fixB2->lat1); fix1->lon = AngleAdd((IMI::IMIDWORD)fixFull->lon, (long)fixB2->lon1); fix1->fxa = (long)fixFull->fxa + (long)fixB2->fxa1; fix1->checksum = IMI::FixChecksum(fix1, __builtin_offsetof(IMI::Fix, checksum)); fix2->id = IMIFIX_ID_B_RECORD; fix2->fv = fixFull->fv; fix2->time = fix1->time + fixB2->time2 + 1; fix2->enl = fixB2->enl2; fix2->alt = (long)fix1->alt + fixB2->alt2; fix2->gpsalt = (long)fix1->gpsalt + fixB2->gpsalt2; fix2->lat = AngleAdd((IMI::IMIDWORD)fix1->lat, (long)fixB2->lat2); fix2->lon = AngleAdd((IMI::IMIDWORD)fix1->lon, (long)fixB2->lon2); fix2->fxa = (long)fix1->fxa + (long)fixB2->fxa2; fix2->checksum = IMI::FixChecksum(fix2, __builtin_offsetof(IMI::Fix, checksum)); return true; }
void type_alignment(const T&) { struct S { char c; T t; } s; SA(__builtin_offsetof (S,t) - __builtin_offsetof (S,c) == 1); }
void f0 () { __builtin_offsetof(A<char>, p); // OK __builtin_offsetof(A<char>, p[1]); // { dg-error "non constant address" } __builtin_offsetof(B, a.p); // OK __builtin_offsetof(B, a.p[1]); // { dg-error "non constant address" } }
namespace test1 { template <typename T> struct A { int a; T b[0]; } __attribute__((packed)); typedef A<unsigned long> type; int test0[sizeof(type) == 4 ? 1 : -1]; int test1[__builtin_offsetof(type, a) == 0 ? 1 : -1]; int test2[__builtin_offsetof(type, b) == 4 ? 1 : -1]; }
static int fontcmp(const GdiFont *font, FONT_DESC *fd) { if(font->font_desc.hash != fd->hash) return 1; if(memcmp(&font->font_desc.lf, &fd->lf, __builtin_offsetof (LOGFONTW, lfFaceName))) return 1; if(!font->font_desc.can_use_bitmap != !fd->can_use_bitmap) return 1; return strcmpiW(font->font_desc.lf.lfFaceName, fd->lf.lfFaceName); }
static void dump_offsets (void) { printf ("#define QNX_UC_MCONTEXT_CPU_OFF\t0x%02x\n", __builtin_offsetof (ucontext_t, uc_mcontext) + __builtin_offsetof (mcontext_t, cpu)); printf ("#define QNX_UC_MCONTEXT_FPU_OFF\t0x%02x\n", __builtin_offsetof (ucontext_t, uc_mcontext) + __builtin_offsetof (mcontext_t, fpu)); PRINT_REG_OFFSET (EDI, edi); PRINT_REG_OFFSET (ESI, esi); PRINT_REG_OFFSET (EBP, ebp); PRINT_REG_OFFSET (EXX, exx); PRINT_REG_OFFSET (EBX, ebx); PRINT_REG_OFFSET (EDX, edx); PRINT_REG_OFFSET (ECX, ecx); PRINT_REG_OFFSET (EAX, eax); PRINT_REG_OFFSET (EIP, eip); PRINT_REG_OFFSET (CS, cs); PRINT_REG_OFFSET (EFL, efl); PRINT_REG_OFFSET (ESP, esp); PRINT_REG_OFFSET (SS, ss); }
void X() { do { char* b; Header c; if (a) c.fork_flags |= 1; __builtin_memcpy(b, &c, __builtin_offsetof(Header, data_length)); b += foo(); } while (1); }
void bar (char *d, int *off) { typedef struct { char c; V16 char vc; } s_t; s_t s = { 1,{ 0,11,22,33,44,55,66,77,88,99,101,111,121,131,141,151 }}; *off = __builtin_offsetof(s_t, vc); __builtin_memcpy(d, &s.vc, 16); }
int main(int argc, char** argv, char** envp) { // this is how you would use such a union... LARGE_INTEGER li; li.LowPart=15; li.HighPart=16; printf("li.LowPart is %u\n", li.LowPart); printf("li.HighPart is %u\n", li.HighPart); printf("li.QuadPart is %lli\n", li.QuadPart); // trampling the quad part tramples both low and high parts li.QuadPart=17; printf("li.LowPart is %u\n", li.LowPart); printf("li.HighPart is %u\n", li.HighPart); printf("li.QuadPart is %lli\n", li.QuadPart); // this is the offsets of it's relevant parts... printf("offset of LowPart is %zd\n", OffsetOf(LARGE_INTEGER, LowPart)); printf("offset of HighPart is %zd\n", OffsetOf(LARGE_INTEGER, HighPart)); printf("offset of QuadPart is %zd\n", OffsetOf(LARGE_INTEGER, QuadPart)); // or we could use the built in... printf("offset of LowPart is %zd\n", __builtin_offsetof(LARGE_INTEGER, LowPart)); printf("offset of HighPart is %zd\n", __builtin_offsetof(LARGE_INTEGER, HighPart)); printf("offset of QuadPart is %zd\n", __builtin_offsetof(LARGE_INTEGER, QuadPart)); return EXIT_SUCCESS; }
static gomp_barrier_t * generation_to_barrier (int *addr) { return (gomp_barrier_t *) ((char *) addr - __builtin_offsetof (gomp_barrier_t, generation)); }
typedef struct nitem { Dtlink_t link; int val; node_t *cnode; box bb; } nitem; typedef int (*distfn) (box *, box *); typedef int (*intersectfn) (nitem *, nitem *); static int cmpitem (Dt_t * d, int *p1, int *p2, Dtdisc_t * disc) { } static Dtdisc_t constr = { __builtin_offsetof (nitem, val), sizeof (int), __builtin_offsetof (nitem, link), ((Dtmake_f) 0), ((Dtfree_f) 0), (Dtcompar_f) cmpitem, ((Dthash_f) 0), ((Dtmemory_f) 0), ((Dtevent_f) 0) }; static int distX (box * b1, box * b2) { } static int intersectY0 (nitem * p, nitem * q) { } static int intersectY (nitem * p, nitem * q)
* to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <cassert.h> #include <cpu_data.h> #include <platform_def.h> /* verify assembler offsets match data structures */ CASSERT(CPU_DATA_CRASH_STACK_OFFSET == __builtin_offsetof (cpu_data_t, crash_stack), assert_cpu_data_crash_stack_offset_mismatch); CASSERT((1 << CPU_DATA_LOG2SIZE) == sizeof(cpu_data_t), assert_cpu_data_log2size_mismatch); /* The per_cpu_ptr_cache_t space allocation */ cpu_data_t percpu_data[PLATFORM_CORE_COUNT];
typedef struct len_and_sockaddr { union { int i; } u; } len_and_sockaddr; enum { LSA_LEN_SIZE = __builtin_offsetof (len_and_sockaddr, u) }; void main(){}
int main(void) { if(__builtin_offsetof(token_t, v.symbol) != sizeof(int)) return 1; return 0; }
int main() { return __builtin_offsetof(B, a[0]); /* { dg-error "cannot apply.*offsetof" } */ }
void ataInit(AHCIController *ctrl, int portno) { ATADevice *dev = NEW(ATADevice); ctrl->ataDevices[ctrl->numAtaDevices++] = dev; mutexInit(&dev->lock); dev->ctrl = ctrl; dev->port = &ctrl->regs->ports[portno]; dev->sd = NULL; // stop the command engine while setting up the commands and stuff ahciStopCmd(dev->port); // create the operations area if (dmaCreateBuffer(&dev->dmabuf, sizeof(AHCIOpArea), 0) != 0) { // it didn't work kprintf("sdahci: failed to allocate operations area\n"); kfree(dev); ctrl->numAtaDevices--; return; }; // set up the operations area AHCIOpArea *opArea = (AHCIOpArea*) dmaGetPtr(&dev->dmabuf); memset(opArea, 0, sizeof(AHCIOpArea)); // set the command list and FIS area dev->port->clb = dmaGetPhys(&dev->dmabuf) + __builtin_offsetof(AHCIOpArea, cmdlist); dev->port->fb = dmaGetPhys(&dev->dmabuf) + __builtin_offsetof(AHCIOpArea, fisArea); // we only use the first command header, so initialize it to point to the table opArea->cmdlist[0].ctba = dmaGetPhys(&dev->dmabuf) + __builtin_offsetof(AHCIOpArea, cmdtab); // start the command engine ahciStartCmd(dev->port); dev->port->serr = dev->port->serr; dev->port->is = dev->port->is; // send the IDENTIFY command. opArea->cmdlist[0].cfl = sizeof(FIS_REG_H2D)/4; // FIS length in dwords opArea->cmdlist[0].w = 0; // read data opArea->cmdlist[0].prdtl = 1; // only one PRDT entry opArea->cmdlist[0].p = 1; opArea->cmdtab.prdt[0].dba = dmaGetPhys(&dev->dmabuf) + __builtin_offsetof(AHCIOpArea, id); opArea->cmdtab.prdt[0].dbc = 511; // length-1 opArea->cmdtab.prdt[0].i = 0; // do not interrupt // set up command FIS FIS_REG_H2D *cmdfis = (FIS_REG_H2D*) opArea->cmdtab.cfis; cmdfis->fis_type = FIS_TYPE_REG_H2D; cmdfis->c = 1; cmdfis->command = ATA_CMD_IDENTIFY; // issue the command and await completion __sync_synchronize(); if (ahciIssueCmd(dev->port) != 0) { kprintf("sdahci: error during identification\n"); return; }; uint32_t *cmdsetptr = (uint32_t*) &opArea->id[ATA_IDENT_COMMANDSETS]; uint32_t cmdset = *cmdsetptr; uint64_t size; if (cmdset & (1 << 26)) { uint64_t *szptr = (uint64_t*) &opArea->id[ATA_IDENT_MAX_LBA_EXT]; size = (*szptr) & 0xFFFFFFFFFFFF; } else { uint32_t *szptr = (uint32_t*) &opArea->id[ATA_IDENT_MAX_LBA]; size = (uint64_t) (*szptr); }; char model[41]; int k; for (k=0; k<40; k+=2) { model[k] = opArea->id[ATA_IDENT_MODEL + k + 1]; model[k+1] = opArea->id[ATA_IDENT_MODEL + k]; }; model[40] = 0; char *check = &model[39]; while (*check == ' ') { if (check == model) break; *check-- = 0; }; kprintf("sdahci: size in MB: %lu, model: %s\n", size / 1024 / 2, model); SDParams sdpars; sdpars.flags = 0; sdpars.blockSize = 512; sdpars.totalSize = size * 512; // do a cache flush opArea->cmdlist[0].w = 0; opArea->cmdlist[0].p = 0; opArea->cmdlist[0].c = 0; opArea->cmdlist[0].prdtl = 0; cmdfis->fis_type = FIS_TYPE_REG_H2D; cmdfis->c = 1; cmdfis->command = ATA_CMD_CACHE_FLUSH_EXT; cmdfis->lba0 = 0; cmdfis->lba1 = 0; cmdfis->lba2 = 0; cmdfis->device = 1<<6; // LBA mode cmdfis->lba3 = 0; cmdfis->lba4 = 0; cmdfis->lba5 = 0; cmdfis->countl = 0; cmdfis->counth = 0; // issue the flush command int status = ahciIssueCmd(dev->port); kprintf("sdahci: cache flush status: %d\n", status); dev->sd = sdCreate(&sdpars, model, &ataOps, dev); if (dev->sd == NULL) { kprintf("sdahci: SD creation failed\n"); // NOTE: do not free anything; this is done upon removing the driver }; };
void IMI::WriteFix(const Fix &fix, bool fromB2, int no_enl, FILE *file) { bool append_line_break = false; unsigned offset = __builtin_offsetof(Fix, checksum); if (fix.checksum != FixChecksum(&fix, offset)) { fixBLastFull.id = 0; return; } if (fix.id == IMIFIX_ID_B_RECORD) { const FixB *fix_b = (const FixB *)&fix; AngleConverter angle; fputc('B', file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); angle.value = (IMIDWORD)fix_b->lat; fprintf(file, "%02d%05d", angle.degrees, angle.milliminutes); fputc((angle.sign) ? 'S' : 'N', file); angle.value = (IMIDWORD)fix_b->lon; fprintf(file, "%03d%05d", angle.degrees, angle.milliminutes); fputc((angle.sign) ? 'W' : 'E', file); fputc("VA??"[fix_b->fv], file); int alt = (int)fix_b->alt - 1000; fprintf(file, alt < 0 ? "%04d" : "%05d", alt); int gpsalt = (int)fix_b->gpsalt - 1000; fprintf(file, alt < 0 ? "%04d" : "%05d", gpsalt); fprintf(file, "%03d", fix_b->fxa); fprintf(file, "%02d", siu); if (!no_enl) fprintf(file, "%03d", fix_b->enl); append_line_break = true; if (!fromB2) memcpy(&fixBLastFull, &fix, sizeof(fixBLastFull)); } else if (fix.id == IMIFIX_ID_B2_RECORD) { const FixB2 *fix_b2 = (const FixB2 *)&fix; if (SplitB2Fix(fix_b2, &fixBLastFull, &fixB1, &fixB2)) { WriteFix(*(Fix*)&fixB1, true, no_enl, file); WriteFix(*(Fix*)&fixB2, true, no_enl, file); } } else if (fix.id == IMIFIX_ID_K_RECORD) { const FixK *fix_k = (const FixK *)&fix; fputc('K', file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); fprintf(file, "%03d", fix_k->hdt); fprintf(file, "%03d", fix_k->gsp); append_line_break = true; } else if (fix.id == IMIFIX_ID_E_RECORD) { const FixE *fix_e = (const FixE *)&fix; if (fix_e->type == IMIFIX_E_TYPE_SATELLITES) { siu = 0; fputc('F', file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); for (unsigned i = 0; i < sizeof(fix_e->text); i++) { if (fix_e->text[i] > 0) { fprintf(file, "%02d", fix_e->text[i]); siu++; } } append_line_break = true; } else if (fix_e->type == IMIFIX_E_TYPE_COMMENT) { fputs("LIMI", file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); WriteString((const char *)fix_e->text, sizeof(fix_e->text), file); append_line_break = true; } else if (fix_e->type == IMIFIX_E_TYPE_PEV) { fputc('E', file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); WriteString((const char *)fix_e->text, sizeof(fix_e->text), file); append_line_break = true; } else if (fix_e->type == IMIFIX_E_TYPE_TASK) { fputc('E', file); BrokenTime time = ConvertToDateTime(fix.time); fprintf(file, "%02d%02d%02d", time.hour, time.minute, time.second); if (fix_e->text[0] == 1) { fputs("STA", file); WriteString((const char *)fix_e->text + 2, sizeof(fix_e->text) - 2, file); } else { if (fix_e->text[0] == 2) fputs("ONT", file); else if (fix_e->text[0] == 3) fputs("FIN", file); else fputs("TPC", file); fprintf(file, "%02d", fix_e->text[1]); WriteString((const char *)fix_e->text + 2, sizeof(fix_e->text) - 2, file); } append_line_break = true; } } if (append_line_break) fputs("\r\n", file); }
// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-apple-darwin9 -std=c++11 %s // expected-no-diagnostics #pragma ms_struct on struct A { unsigned long a:4; unsigned char b; A(); }; struct B : public A { unsigned long c:16; int d; B(); }; static_assert(__builtin_offsetof(B, d) == 12, "We can't allocate the bitfield into the padding under ms_struct");
// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -std=c++11 -verify %s -Winvalid-offsetof struct NonPOD { virtual void f(); int m; }; struct P { NonPOD fieldThatPointsToANonPODType; }; void f() { int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-standard-layout type 'P'}} } struct StandardLayout { int x; StandardLayout() {} }; int o = __builtin_offsetof(StandardLayout, x); // no-warning
void f() { int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-standard-layout type 'P'}} }
int main(int argc, char *argv[]) { int i; struct a_struct mystruct; size_t size; size_t padding; size_t size_acc = 0; size_t padding_acc = 0; uint32_t foo = 0xdeadbeef; uint8_t *ptr8 = (uint8_t *)&foo; uint16_t *ptr16 = (uint16_t *)&foo; printf("(uint32_t *): %p->%#8.8x\n", &foo, foo); for (i = 0; i < sizeof(foo) / sizeof(*ptr16); i++, ptr16++) printf("(uint16_t *): %p->%#2.2x\n", ptr16, *ptr16); for (i = 0; i < sizeof(foo) / sizeof(*ptr8); i++, ptr8++) printf("(uint8_t *): %p->%#2.2x\n", ptr8, *ptr8); printf("\n"); printf("mystruct@%p\n", &mystruct); size = sizeof(mystruct.uint8); size_acc += size; printf("mystruct.uint8@%p, size:%lu\n", &mystruct.uint8, size); padding = __builtin_offsetof(typeof(mystruct), uint16) - size_acc - padding_acc; if (padding) { printf("padding:%lu\n", padding); padding_acc += padding; } size = sizeof(mystruct.uint16); size_acc += size; printf("mystruct.uint16@%p, size:%lu\n", &mystruct.uint16, size); padding = __builtin_offsetof(typeof(mystruct), uint8_2) - size_acc - padding_acc; if (padding) { printf("padding:%lu\n", padding); padding_acc += padding; } size = sizeof(mystruct.uint8_2); size_acc += size; printf("mystruct.uint8_2@%p, size:%lu\n", &mystruct.uint8_2, size); padding = __builtin_offsetof(typeof(mystruct), uint32) - size_acc - padding_acc; if (padding) { printf("padding:%lu\n", padding); padding_acc += padding; } size = sizeof(mystruct.uint32); size_acc += size; printf("mystruct.uint32@%p, size:%lu\n", &mystruct.uint32, size); padding = __builtin_offsetof(typeof(mystruct), ptr32) - size_acc - padding_acc; if (padding) { printf("padding:%lu\n", padding); padding_acc += padding; } size = sizeof(mystruct.ptr32); size_acc += size; printf("mystruct.ptr32@%p, size:%lu\n", &mystruct.ptr32, size); printf("mystruct: size:%lu, size payload:%lu, size padding:%lu\n", sizeof(mystruct), size_acc, padding_acc); return 0; }
// PR c++/27601 // Origin: Patrik Hägglund <*****@*****.**> // { dg-do compile } struct bar { static int foo; static int baz(); }; int a = __builtin_offsetof(bar, foo); // { dg-error "static data member" } int b = __builtin_offsetof(bar, baz); // { dg-error "member function" } int b0 = __builtin_offsetof(bar, baz[0]); // { dg-error "function" } int c = __builtin_offsetof(bar, ~bar); // { dg-error "member function" } typedef int I; enum E { }; int d = __builtin_offsetof(I, ~I); // { dg-error "destructor" } int e = __builtin_offsetof(E, ~E); // { dg-error "destructor" }