freemyipod r716 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r715‎ | r716 | r717 >
Date:00:51, 22 May 2011
Author:user890104
Status:new
Tags:
Comment:
Beeper: use less memory
Modified paths:
  • /apps/beeper/main.c (modified) (history)

Diff [purge]

Index: apps/beeper/main.c
@@ -24,7 +24,11 @@
2525 #include "emcoreapp.h"
2626 #include "beep.h"
2727
28 -void playsong(void *buf, size_t size);
 28+struct note
 29+{
 30+ unsigned int cycles;
 31+ unsigned int length;
 32+};
2933
3034 static void main()
3135 {
@@ -36,10 +40,9 @@
3741
3842 size_t size;
3943 int fd;
40 - void *buf;
41 - unsigned char play;
 44+ struct note *buf;
 45+ unsigned int i, count;
4246
43 - play = 0;
4447 fd = file_open("/song.dat", O_RDONLY);
4548
4649 if (fd <= 0)
@@ -51,7 +54,7 @@
5255 cputs(3, "File opened\n");
5356 size = filesize(fd);
5457
55 - if (size <= 0)
 58+ if (0 == size)
5659 {
5760 close(fd);
5861 cputs(3, "Unable to get file size or file is empty!\n");
@@ -68,7 +71,7 @@
6972 }
7073
7174 buf = memalign(0x10, size);
72 -
 75+
7376 if (!buf)
7477 {
7578 close(fd);
@@ -87,37 +90,21 @@
8891 cputs(3, "Read successful\n");
8992 close(fd);
9093 cputs(3, "File closed\n");
91 - playsong(buf, size);
92 - free(buf);
93 -}
94 -
95 -void playsong(void *buf, size_t size) {
96 - int i;
97 - unsigned int *cycles, *lengths, count;
9894
9995 count = size / 8;
100 - cycles = malloc(count * sizeof(unsigned int));
101 - lengths = malloc(count * sizeof(unsigned int));
10296
10397 for (i = 0; i < count; ++i)
10498 {
105 - cycles[i] = *((unsigned int *)(buf) + i + i);
106 - lengths[i] = *((unsigned int *)(buf) + i + i + 1);
107 - }
108 -
109 - for (i = 0; i < count; ++i)
110 - {
111 - if (0 == cycles[i])
 99+ if (0 == buf[i].length)
112100 {
113 - sleep(lengths[i]);
 101+ sleep(buf[i].length);
114102 continue;
115103 }
116104
117 - singlebeep(cycles[i], lengths[i]);
 105+ singlebeep(buf[i].cycles, buf[i].length);
118106 }
119107
120 - free(lengths);
121 - free(cycles);
 108+ free(buf);
122109 }
123110
124111 EMCORE_APP_HEADER("Beeper", main, 127)