Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Memory.

Similar presentations


Presentation on theme: "CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Memory."— Presentation transcript:

1 CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Memory

2 Memory Representation We typically draw diagrams representing the memory of the computer, the memory of our particular program or both as rectangles. Our convention will be that "high-memory" will be on the bottom and "low- memory" on top. Typically these drawings are not to scale Low Memory High Memory x0000 xFFFF

3 Typical Arrangement Normally the actual program code (executable instructions) is placed in low memory Code x0000 xFFFF

4 Typical Arrangement Next we have an area for storage of constant data Code Constant Data x0000 xFFFF

5 Typical Arrangement Data that may be changed follows Code Constant Data Alterable Data x0000 xFFFF

6 Typical Arrangement These three items comprise what is considered the static area of memory. The static area details (size, what is where, etc.) are known at translation or compile time. Code Constant Data Alterable Data Static x0000 xFFFF

7 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Variables (I) Local variable outside a function (value persists throughout the life of the program) static keyword declared inside a function Located in static area Storage Class: STATIC

8 // File Demo.c int main() { } int buttonState(int which) { static int state; }

9 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Variables (II) Global variable visible to all functions within a file static keyword declared outside of any function Located in static area Storage Class: STATIC

10 // File Demo.c static int pressure; int main() { pressure = 7; } int buttonState(int which) { static int state; x = pressure + 3; }

11 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Variables (III) Global variable visible in more than one file Declared outside of any function in one file Declared extern in other files (or functions/blocks) Located in static area Storage Class: STATIC

12 // File Demo.c static int pressure; int cycles = 60; int main() { } int buttonState(int which) { static int state; } // File Auxilary.c extern int cycles; int foo() { } int bar() { }

13 Typical Arrangement Immediately below the static area the heap is located. The heap can expand downward as the program dynamically requests additional storage space In most cases, the runtime environment manages the heap for the user We will return to this later Code Constant Data Alterable Data Static Heap x0000 xFFFF

14 Typical Arrangement Finally, the activation stack starts in high memory and can grow up as space is needed. Items maintained in the stack include –auto variables –Function parameters –Return values Stack Code Constant Data Alterable Data Static Heap x0000 xFFFF

15 Typical Arrangement These items in the lower portion of the diagram change* during execution of the program. Thus they are called dynamic Dynamic *Not just their value Stack Code Constant Data Alterable Data Static Heap x0000 xFFFF

16 CS1372: HELPING TO PUT THE COMPUTING IN ECE auto auto, short for automatic variables are those that exist on the stack. The auto keyword is not normally used. Automatic means that space is allocated and deallocated on the stack automatically without the programmer having to do any special operations. Dynamic Stack Code Constant Data Alterable Data Static Heap x0000 xFFFF

17 // File Demo.c static int pressure; int cycles = 60; int main() { } int buttonState(int which) { static int state; } // File Auxilary.c extern int cycles; int foo() { } int bar() { }

18 CS1372: HELPING TO PUT THE COMPUTING IN ECE Automatic Variables Local variable inside a function (lasts only while the function is running) auto keyword (never used!) Located on stack Storage Class: AUTO

19 // File Demo.c static int pressure; int cycles = 60; int main() { int p; } int buttonState(int which) { static int state; int y; } // File Auxilary.c extern int cycles; int foo() { int y; } int bar() { int i; int y; int p; }

20 CS1372: HELPING TO PUT THE COMPUTING IN ECE Confused? int x; static int y; int main() { static int z; int w;... static? auto?

21 CS1372: HELPING TO PUT THE COMPUTING IN ECE What if......parts of memory could be set to read-only? (After your program was loaded in) What could that be useful for?

22 Typical Arrangement The constant data area is where items such as the "Hello" in a statement such as printf("Hello"); would be stored. Dynamic Stack Code Constant Data Alterable Data Static Heap x0000 xFFFF

23 Here's the funny part! This in NOT where variables marked const are stored! const int x; Dynamic Stack Code Constant Data Alterable Data Static Heap x0000 xFFFF

24 CS1372: HELPING TO PUT THE COMPUTING IN ECE Want Proof? Will this work? int test(int n) { const int q = n + 7;

25 CS1372: HELPING TO PUT THE COMPUTING IN ECE const The const keyword is a type qualifier not a storage class The const keyword has NOTHING to do with the constant area of memory!!! (sort of!) It tells the compiler to not allow the programmer to change the value of a variable once set. Warning: It is relatively easy to get around this! int foo(int n) { const int k = 8; const int i = n + 7;...

26 CS1372: HELPING TO PUT THE COMPUTING IN ECE Now that we've said that Sometimes const can mean put something in read only memory! Can you think of an example?

27 CS1372: HELPING TO PUT THE COMPUTING IN ECE static Variables –A global variable is stored in the static area of memory –A variable that is global just to the functions in one file must be designated using static –A variable that is local to a function AND retains its value (i.e. persistence) must be labeled static Functions –A function labeled as static is visible only within the file where it is defined

28 CS1372: HELPING TO PUT THE COMPUTING IN ECE Multiple Files foo.cbar.cbaz.c Linker a.out Advantage: For maintenance only have to recompile affected files Advantage: For maintenance only have to recompile affected files

29 CS1372: HELPING TO PUT THE COMPUTING IN ECE Multiple File Scope Scope can be local, just inside a function Scope can be global within a file Scope can be global across all files Scope can be defined illegally! Actually called Linkage in C standards –None –Internal –External Note: global here means visible in more than one function

30 Scope can be global within a file static int i; int foo(...) {... } int bar(...) {... } static int i; int main() {... } int baz(...) {... } These are different variables with Global scope within their respective files These are different variables with Global scope within their respective files

31 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope can be global across all files int i=0;extern int i; void f() { extern int i;... } These are the same variable!

32 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope can be defined poorly! int i; extern int i;

33 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope can be defined poorly! int i;int i= 42;extern int i;

34 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope can be defined illegally! int i = 42; extern int i;

35 CS1372: HELPING TO PUT THE COMPUTING IN ECE This is correct! int i=3;extern int i; void f() { extern int i;... }

36 CS1372: HELPING TO PUT THE COMPUTING IN ECE According to the C89 Standard... There are 4 types of scope –Prototype Scope Just applies to prototypes Means this is illegal void func(int i, int i); –File Scope Declared outside any function –Function Scope only applies to labels!!! –Block Scope includes function parameters

37 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope vs. Lifetime Scope File Scope Block Scope Lifetime Life of program Life of block

38 CS1372: HELPING TO PUT THE COMPUTING IN ECE Scope vs. Lifetime Scope File Scope Block Scope Lifetime Life of program Life of block Can be overridden with “ static ”

39 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Many languages have fixed mappings between scopes and lifetimes In C, we have the option to decide... void foo(void) { int x; static int a; x = 42; When the function goes away, x and its value go away since they were on stack. Setting a value into a static variable (e.g. a ) means that the value is stored in a static area and will persist throughout the entire execution of the program

40 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Initialization void foo(void) { int x = 10; static int y = 20; Both variables are initialized as allocated x is initialized to 10 every time function foo runs y is initialized when program starts

41 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Initialization? void foo(void) { int x = 10; static int y; y = 20; This would defeat purpose of static variable having persistence!

42 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static Initialization void foo(void) { int x = 10; static int y = 20; printf(“x = %d y = %d\n”, x, y); x += 40; y += 30; } What prints the first time foo is called? What prints the second time?

43 CS1372: HELPING TO PUT THE COMPUTING IN ECE Static also applies to functions Static has different meanings depending on where used #include... #define int x; static int y; /* What does this mean? */ void foo(void) { static int z; /* What does this mean? */... } static void bar(void) /* What does this mean?*/

44 CS1372: HELPING TO PUT THE COMPUTING IN ECE Questions?

45 Other goodies register –Suggests to compiler that this variable should be kept in a register –Cannot get address of variable declared register –Not as important as it once was with modern compilers volatile –Type qualifier –Tells compiler that value in this variable may change on its own! –Used in shared memory applications Memory mapped I/O

46 CS1372: HELPING TO PUT THE COMPUTING IN ECE volatile Used to indicate possibility that this variable might be changed by someone else

47 CS1372: HELPING TO PUT THE COMPUTING IN ECE Conversely What if a particular variable needs to be accessed very frequently? Usually the compiler will recognize this and keep the variable in a register int i; for(i=0; i<100000; i++) { /* loop */ }

48 CS1372: HELPING TO PUT THE COMPUTING IN ECE Conversely To suggest to the compiler that this is a good idea: register register int i; for(i=0; i<100000; i++) { /* loop */ } The register keyword suggests to the compiler that a variable should be maintained in a register as opposed to memory.

49 CS1372: HELPING TO PUT THE COMPUTING IN ECE register Since putting one variable in a register can speed up execution putting more variables in registers can only make things better!?!?!? Fact: Overuse of “register” can adversely affect the compiler’s register allocation algorithm Don't try and outwit the compiler! In some isolated cases it is possible to improve performance: i.e. dumb compiler

50 CS1372: HELPING TO PUT THE COMPUTING IN ECE register Also, cannot get the address of a variable declared to be in the register storage class: int i, *ip; ip = &i;/* (more later) */ This will generate a warning and force the variable to be put in memory. In most cases use of register is discouraged (often compilers ignore it!)

51 const volatile Type Qualifiers Storage Class Specifiers register auto static extern

52 CS1372: HELPING TO PUT THE COMPUTING IN ECE Questions?

53 CS1372: HELPING TO PUT THE COMPUTING IN ECE Recall 0x1 = 1 0x10 = 16 0x100 = 256 0x1 000 = 4,096 0x10 000 = 65,536 0x100 000 = 1,048,576 (1 M) 0x1 000 000 = 16 M 0x10 000 000 = 256 M 0x100 000 000 = 4 G

54 CS1372: HELPING TO PUT THE COMPUTING IN ECE Types of GBA Memory Volatile Persistent –Backup SRAM –Backup EEPROM –Backup Flash ROM –Backup DACS –Flashcards

55 CS1372: HELPING TO PUT THE COMPUTING IN ECE Types of Memory RAM –Random Access Memory ROM –Read Only Memory ROM is RAM but not all RAM is ROM

56 System ROM You can't get to it! 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

57 WRAM Internal Working RAM Sometimes called Working RAM On the ARM chip Very fast Costly Globals, static, stack all go here by default! Force functions? 0x03 000 000 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

58 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Tradeoff COST Speed

59 Video&Hardware Hardware Registers –Sound –DMA –etc. Palette RAM Video RAM Oject Attribute Memory 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

60 Game Pak ROM Cartridge Memory Making an array const will put it here. Code goes here –main –functions 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

61 Game Pak Flash Cartridge Memory Can write to this block Could store or use sprintf, etc. 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

62 EXRAM External RAM Heap (malloc) Can force variables here 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

63 Typical C Program Dynamic Allocation plus forced vars Static/Global Stack Buttons Audio Video Code and Constant data Save Game Area 00000000-00003FFFF System ROM 16KB 02000000-0203FFFF EXRAM 256KB 03000000-03007FFF IWRAM 32KB 04000000 Hardware Registers 05000000-050003FFF Palette Memory 1KB 06000000-06017FFF Video RAM 96KB 07000000-070003FF Object Attribute Memory 1 KB 0800000 Game Pak ROM Wait State 0 255 Mbits (~32Mb) 0A00000 Game Pak ROM Wait State 1 255 Mbits (~32Mb) 09FE0000 Game Pak Flash Wait State 0 1Mbit(128KB)

64 CS1372: HELPING TO PUT THE COMPUTING IN ECE Implications Make arrays constant if they are. Other arrays can be put into EXRAM Code that needs to be fast can be put into IWRAM Can use dynamically allocated memory

65

66 CS1372: HELPING TO PUT THE COMPUTING IN ECE Dynamically Allocated Memory? Memory that can be allocated during execution of the program with knowing ahead of time exact amount required Examples –List of inventory items –Dynamic structures: Lists, Trees, etc. –Music composition –Animations –etc.

67 CS1372: HELPING TO PUT THE COMPUTING IN ECE Dynamic Allocation Functions void *malloc(size_t n); void *realloc(void *, size_t n); void free(void *);

68 CS1372: HELPING TO PUT THE COMPUTING IN ECE void *

69 CS1372: HELPING TO PUT THE COMPUTING IN ECE Example /* Create an array of n ints */ int *intarr; intarr = malloc(n*sizeof(int)); if(intarr == NULL) /* Handle error */ for(i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/4258019/14/slides/slide_68.jpg", "name": "CS1372: HELPING TO PUT THE COMPUTING IN ECE Example /* Create an array of n ints */ int *intarr; intarr = malloc(n*sizeof(int)); if(intarr == NULL) /* Handle error */ for(i=0; i

70 CS1372: HELPING TO PUT THE COMPUTING IN ECE Freeing Up Memory Once dynamically allocated memory is no longer needed it is returned using free free(intarr); NOTE: –Freed memory is not cleared –The pointer is not set to NULL

71 CS1372: HELPING TO PUT THE COMPUTING IN ECE Keeping Track The runtime system just knows that at a certain address a certain amount of memory has been allocated!!! 0x20001000x400 0x20005000x100 0x20008000x050 etc.

72 CS1372: HELPING TO PUT THE COMPUTING IN ECE Bad Things Freeing up memory at hasn't been allocated Losing track of dynamically allocated memory Freeing up memory and continuing to use it Forgetting that dynamically allocated memory is not initialized Failing to check for error conditions

73 CS1372: HELPING TO PUT THE COMPUTING IN ECE Realloc int *pi; pi = malloc(n*sizeof(*pi)); /* error checking */ /* Need to change size!!! */ pi = realloc(pi, newsize); /* error checking */

74 CS1372: HELPING TO PUT THE COMPUTING IN ECE Realloc (the right way) int *pi; int *temp; pi = malloc(n*sizeof(*pi)); /* error checking */ /* Need to change size!!! */ temp = realloc(pi, newsize); /* error checking */ pi = temp; /* Should we free up temp now? */

75 CS1372: HELPING TO PUT THE COMPUTING IN ECE Additional Information realloc(NULL, n)  malloc(n); realloc(cp, 0)  free(cp);

76 CS1372: HELPING TO PUT THE COMPUTING IN ECE Realloc (the right way) int *pi; int *temp; pi = malloc(NULL, n*sizeof(*pi)); /* error checking */ /* Need to change size!!! */ temp = realloc(pi, newsize); /* error checking */ pi = temp; /* Should we free up temp now? */

77 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Management Ok? int *ip, *ip2; ip = malloc(...); ip2 = ip; free(ip); *ip2 = 42; 1.Yes 2.No 3.Maybe

78 CS1372: HELPING TO PUT THE COMPUTING IN ECE More... int *ip, *ip2;/* Line 1 */ ip = malloc(40);/* Line 2 */ /* Error checking here */ ip2 = ip;/* Line 3 */ ip += 4;/* Line 4 */ free(ip);/* Line 5 */ free(ip2);/* Line 6 */ Problem line?

79 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Leaks Memory leaks occur when the programmer loses track of memory allocated by malloc or other functions that call malloc void foo(void) { char *ca = malloc(...); /* no free */ return; } Bad

80 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Leaks Obviously a program that runs for a fraction of a second and then terminates will not cause a problem if it leaks a few bytes of memory. However, for real world programs memory leaks are not acceptable. Imagine a game that runs out of memory just as you are about to beat the world record

81 CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory Management Some functions that call malloc –calloc –strdup –etc. C doesn’t do automatic memory management for efficiency reasons –If you want to manage memory...do it yourself!

82 CS1372: HELPING TO PUT THE COMPUTING IN ECE Questions?

83 CS1372: HELPING TO PUT THE COMPUTING IN ECE Big-Endian vs Little-Endian

84 CS1372: HELPING TO PUT THE COMPUTING IN ECE


Download ppt "CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Memory."

Similar presentations


Ads by Google