format src/systemcmds/reflect

This commit is contained in:
Daniel Agar 2015-09-05 12:21:12 -04:00
parent 01cc966b3b
commit bed3fdf952
1 changed files with 45 additions and 36 deletions

View File

@ -54,7 +54,7 @@ __EXPORT int reflect_main(int argc, char *argv[]);
#define MAX_BLOCKS 1000 #define MAX_BLOCKS 1000
static uint32_t nblocks; static uint32_t nblocks;
struct block { struct block {
uint32_t v[256]; uint32_t v[256];
}; };
static struct block *blocks[MAX_BLOCKS]; static struct block *blocks[MAX_BLOCKS];
@ -62,50 +62,59 @@ static struct block *blocks[MAX_BLOCKS];
static void allocate_blocks(void) static void allocate_blocks(void)
{ {
while (nblocks < MAX_BLOCKS) { while (nblocks < MAX_BLOCKS) {
blocks[nblocks] = calloc(1, sizeof(struct block)); blocks[nblocks] = calloc(1, sizeof(struct block));
if (blocks[nblocks] == NULL) {
break; if (blocks[nblocks] == NULL) {
} break;
for (uint32_t i=0; i<sizeof(blocks[nblocks]->v)/sizeof(uint32_t); i++) { }
blocks[nblocks]->v[i] = VALUE(i);
} for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) {
nblocks++; blocks[nblocks]->v[i] = VALUE(i);
} }
printf("Allocated %u blocks\n", nblocks);
nblocks++;
}
printf("Allocated %u blocks\n", nblocks);
} }
static void check_blocks(void) static void check_blocks(void)
{ {
for (uint32_t n=0; n<nblocks; n++) { for (uint32_t n = 0; n < nblocks; n++) {
for (uint32_t i=0; i<sizeof(blocks[nblocks]->v)/sizeof(uint32_t); i++) { for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) {
assert(blocks[n]->v[i] == VALUE(i)); assert(blocks[n]->v[i] == VALUE(i));
} }
} }
} }
int int
reflect_main(int argc, char *argv[]) reflect_main(int argc, char *argv[])
{ {
uint32_t total = 0; uint32_t total = 0;
printf("Starting reflector\n"); printf("Starting reflector\n");
allocate_blocks(); allocate_blocks();
while (true) { while (true) {
char buf[128]; char buf[128];
ssize_t n = read(0, buf, sizeof(buf)); ssize_t n = read(0, buf, sizeof(buf));
if (n < 0) {
break; if (n < 0) {
} break;
if (n > 0) { }
write(1, buf, n);
} if (n > 0) {
total += n; write(1, buf, n);
if (total > 1024000) { }
check_blocks();
total = 0; total += n;
}
} if (total > 1024000) {
return OK; check_blocks();
total = 0;
}
}
return OK;
} }