Heterogeneous Data Structures & Alignment

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
Ithaca College 1 Machine-Level Programming VIII: Advanced Topics: alignment & Unions Comp 21000: Introduction to Computer Systems & Assembly Lang Systems.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Today’s Material Aggregate Data Types: Structures and Unions
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.
1 Array. 2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs.
CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,
Data in Memory variables have multiple attributes symbolic name
CSE 351 Section 9 3/1/12.
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Arrays CSE 351 Spring 2017 Instructor: Ruth Anderson
Structures.
C Tutorial (part 5) CS220, Spring 2012
Pointers in C.
The Hardware/Software Interface CSE351 Winter 2013
Machine-Level Programming IV: Structured Data
Structs & typedef Already seen in tirgul.
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Referencing Examples Code Does Not Do Any Bounds Checking!
Machine-Level Programming IV: Data CSCE 312
Machine-level Programming IV: Data Structures
Data Structures and Analysis (COMP 410)
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming IV: Structured Data
Structured Data II Heterogenous Data Feb. 15, 2000
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
Topic 3-b Run-Time Environment
Machine Level Representation of Programs (III)
Machine-Level Programming IV: Machine Data 9th Lecture
Machine Level Representation of Programs (IV)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine Level Representation of Programs (IV)
Machine-Level Programming 5 Structured Data
Heterogeneous Data Structures & Alignment * heterogeneous: 不同种类的
Machine-Level Programming 5 Structured Data
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C structures and Compilation to IA32
Machine-Level Programming IV: Structured Data
Instructors: Majd Sakr and Khaled Harras
Structured Data I: Homogenous Data Feb. 10, 2000
Instructor: Fatma CORUT ERGİN
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Data
Structs & typedef Already seen in tirgul.
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data
Presentation transcript:

Heterogeneous Data Structures & Alignment

Outline Struct Union Alignment Chap 3.9, 3.10

Group objects into a single object Structures Group objects into a single object struct rect { int llx; /* X coordinate of lower-left corner */ int lly; /* Y coordinate of lower-left corner */ int color; /* Coding of color */ int width; /* Width (in pixels) */ int height; /* Height (in pixels) */ };

Structure Each object is referenced by name struct rect r; r.llx = r.lly = 0; r.color = 0xFF00FF; r.width = 10; r.height = 20;

Structure int area (struct rect *rp) { return (*rp).width * (*rp).height; } void rotate_left (struct rect *rp) /* Exchange width and height */ int t = rp->height; rp->height = rp->width; rp->width = t;

Structures Memory layout All the components are stored in a contiguous region of memory A pointer to a structure is the address of its first byte

Structure struct rec { int i; int j; int a[3]; int *p; } *r; Offset 4 4 8 20 Contents i j a[0] a[1] a[2] p

References to structure elements Using offsets as displacements r->j = r->i (Copy element r->i to element r->j) r is in register %edx. 1 movl (%edx), %eax Get r->i 2 movl %eax, 4(%edx) Store in r->j Offset 4 8 20 Contents i j a[0] a[1] a[2] p

Structure &(r->a[i]) r in %eax, i in %edx: Offset 4 8 20 Contents i 1 leal 8(%eax,%edx,4),%ecx Generate &r->a[i] Offset 4 8 20 Contents i j a[0] a[1] a[2] p

r->p = &r->a[r->i + r->j]; Structure r->p = &r->a[r->i + r->j]; r in register %edx: 1 movl 4(%edx), %eax Get r->j 2 addl (%edx), %eax Add r->i 3 leal 8(%edx,%eax,4),%eax Compute &r->a[r->i + r->j] 4 movl %eax, 20(%edx) Store in r->p Offset 4 8 20 Contents i j a[0] a[1] a[2] p

Unions A single object can be referenced by using different data types The syntax of a union declaration is identical to that for structures, but its semantics are very different Rather than having the different fields reference different blocks of memory, they all reference the same block

Unions struct S3 { char c; int i[2]; double v; }; union U3 { The offsets of the fields, as well as the total size of data types S3 and U3, are: Type c i v size S3 4 12 20 U3 8

Unions struct NODE { struct NODE *left; struct NODE *right; double data; }; union NODE { struct { union NODE *left; union NODE *right; } internal;

Unions struct NODE { int is_leaf; union { struct { struct NODE *left; struct NODE *right; } internal; double data; } info; };

Unions 1 unsigned float2bit(float f) 2 { 3 union { 4 float f; 5 unsigned u; 6 } temp; 7 temp.f = f; 8 return temp.u; 9 } 1 movl 8(%ebp), %eax

Unions 1 unsigned copy (unsigned u) 2 { 3 return u; 4 } 1 movl 8(%ebp), %eax

Alignment restrictions The address for some type of object must be a multiple of some value k (typically 2, 4, or 8) Simplify the hardware design of the interface between the processor and the memory system

Alignment In IA32 hardware will work correctly regardless of the alignment of data Aligned data can improve memory system performance

Linux alignment restriction 1-byte data types are able to have any address 2-byte data types must have an address that is multiple of 2 Any larger data types must have an address that is multiple of 4

Alignment is enforced by Making sure that every data type is organized and allocated in such a way that every object within the type satisfies its alignment restrictions. malloc() Returns a generic pointer that is void * Its alignment requirement is 4

Alignment Structure data type may need to insert gaps in the field allocation may need to add padding to the end of the structure

Simple Example struct xxx { int i; char c; double d; }; struct xxx x[2]; 0x00 0x04 0x08 0x0C 0x10 0x14 &x[0].i &x[0].c &x[0].d &x[1].i

Complex Example 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C &x[0].s struct xxx { short s; char c0; int i; long l; char c1; char a[2]; double d; char c2; }; struct xxx x[2]; &x[0].c0 &x[0].i &x[0].l &x[0].c1 &x[0].a[0] &x[0].a[1] &x[0].d &x[0].c2 &x[1].s

Array struct ccc { char c1; char a[3]; char c2; }; struct ccc c[2]; 0x00 &c[0].c1 &c[0].a[0] 0x04 &c[0].c2 &c[1].c1 &c[1].a[0] 0x08 &c[1].c2 0x0C 0x10 0x14

Array struct ccc { char c1; short a[3]; char c2; }; struct sss s[2]; 0x00 &s[0].c1 &s[0].a[0] 0x04 0x08 &s[0].c2 &s[1].c1 0x0C &s[1].a[0] 0x10 &s[1].c2 0x14

Array struct iii { char c1; int a[3]; char c2; }; struct iii i[2]; 0x00 0x04 0x08 0x0C 0x10 0x14 &s[0].c1 &x[0].i &s[0].c2 &s[1].c1