Hi, This patch makes: -Tranforms "while" and "for" loops to "do {} while()" loops, when it made cleanes code. -Cleaned handling of error in sys_execve(). -Code size was reduces by 112 bytes. Greetings, Juan
diff -Nur elks.orig/arch/i86/drivers/block/doshd.c elks/arch/i86/drivers/block/doshd.c --- elks.orig/arch/i86/drivers/block/doshd.c 2014-04-26 22:12:31.000000000 -0500 +++ elks/arch/i86/drivers/block/doshd.c 2014-10-14 12:45:13.000000000 -0500 @@ -403,12 +403,13 @@ #ifndef CONFIG_HW_USE_INT13_FOR_DISKPARMS drivep->cylinders = 0; - for (count = 0; count < 2; count++) { + count = 0; + do { if (seek_sector(hd_drive_map[target], track_probe[count], 1)) { break; } drivep->cylinders = track_probe[count]; - } + } while(++count < 2); /* Next, probe for sector number. We probe on track 0 (40-40 in * seek_sector), which is safe for all formats, and if we get a @@ -417,12 +418,13 @@ */ drivep->sectors = 0; - for (count = 0; count < 5; count++) { + count = 0; + do { if (seek_sector(hd_drive_map[target], 40, sector_probe[count])) { break; } drivep->sectors = sector_probe[count]; - } + } while(++count < 5); #else @@ -526,8 +528,10 @@ hdcount = bioshd_gethdinfo(); printk("doshd: found %d hard drive%c\n", hdcount, hdcount == 1 ? ' ' : 's'); + bioshd_gendisk.nr_real = hdcount; #endif + if (!(count + hdcount)) return 0; @@ -556,10 +560,6 @@ } #endif -#ifdef CONFIG_BLK_DEV_BHD - bioshd_gendisk.nr_real = hdcount; -#endif - i = register_blkdev(MAJOR_NR, DEVICE_NAME, &bioshd_fops); if (i == 0) { diff -Nur elks.orig/arch/i86/drivers/char/dircon.c elks/arch/i86/drivers/char/dircon.c --- elks.orig/arch/i86/drivers/char/dircon.c 2014-04-26 22:12:31.000000000 -0500 +++ elks/arch/i86/drivers/char/dircon.c 2014-10-14 18:29:29.000000000 -0500 @@ -518,14 +518,11 @@ static void Console_gotoxy(register Console * C, int x, int y) { - { - register char *xp = (char *)x; - C->cx = ((((int) xp) >= MaxCol) ? MaxCol : ((((int)xp) < 0) ? 0 : (int)x)); - } - { - register char *yp = (char *)y; - C->cy = ((((int) yp) >= MaxRow) ? MaxRow : ((((int)yp) < 0) ? 0 : (int)y)); - } + register char *xp = (char *)x; + + C->cx = ((((int) xp) >= MaxCol) ? MaxCol : ((((int)xp) < 0) ? 0 : (int)xp)); + xp = (char *)y; + C->cy = ((((int) xp) >= MaxRow) ? MaxRow : ((((int)xp) < 0) ? 0 : (int)xp)); } #endif @@ -619,8 +616,8 @@ { register char *pi; + C = Con; for (pi = 0; ((unsigned int)pi) < NumConsoles; pi++) { - C = &Con[(unsigned int)pi]; C->cx = C->cy = 0; C->state = ST_NORMAL; C->vseg = VideoSeg + (PageSize >> 4) * ((unsigned int)pi); @@ -635,10 +632,11 @@ if (pi) ClearRange(C, 0, 0, Width, Height); + C++; } } - C = &Con[0]; + C = Con; C->cx = peekb(0x40, 0x50); C->cy = peekb(0x40, 0x51); Visible = C; diff -Nur elks.orig/arch/i86/drivers/char/meta.c elks/arch/i86/drivers/char/meta.c --- elks.orig/arch/i86/drivers/char/meta.c 2014-04-26 22:12:31.000000000 -0500 +++ elks/arch/i86/drivers/char/meta.c 2014-10-14 12:33:07.000000000 -0500 @@ -29,23 +29,26 @@ static struct ud_driver *get_driver(int major) { - int i; + register struct ud_driver *d = drivers; - for (i = 0; i < MAX_UDD; i++) - if (drivers[i].udd_major == major) - return &drivers[i]; + do { + if(d->udd_major == major) + return d; + } while(++d < &drivers[MAX_UDD]); return NULL; } struct ud_request *new_request(void) { int i; + register struct ud_request *r = requests; - for (i = 0; i < MAX_UDR; i++) - if (requests[i].udr_status == 0) { - requests[i].udr_status = 1; - return &requests[i]; + do { + if(r->udr_status == 0) { + r->udr_status = 1; + return r; } + } while(++r < &requests[MAX_UDR]); panic("Out of requests\n"); return NULL; } diff -Nur elks.orig/arch/i86/drivers/char/ntty.c elks/arch/i86/drivers/char/ntty.c --- elks.orig/arch/i86/drivers/char/ntty.c 2014-10-15 13:12:08.000000000 -0500 +++ elks/arch/i86/drivers/char/ntty.c 2014-10-15 14:18:12.000000000 -0500 @@ -338,15 +338,16 @@ /* unsigned short int i; */ register char *pi; - for (pi = 0 ; ((int)pi) < NUM_TTYS ; pi++) { - ttyp = &ttys[(int)pi]; + ttyp = &ttys[0]; + while(ttyp < &ttys[NUM_TTYS]) { ttyp->minor -= (ttyp->minor + 1); /* set unsigned to -1 */ memcpy(&ttyp->termios, &def_vals, sizeof(struct termios)); + ttyp++; } #ifdef CONFIG_CONSOLE_BIOS - ttyp = &ttys[0]; + ttyp = ttys; ttyp->ops = &bioscon_ops; ttyp->minor = 0; @@ -354,33 +355,31 @@ #if defined(CONFIG_CONSOLE_DIRECT) || defined(CONFIG_SIBO_CONSOLE_DIRECT) + chq_init(ttys[0].inq, ttys[0].inq_buf, INQ_SIZE); + ttyp = ttys; for (pi = 0 ; ((int)pi) < NUM_TTYS ; pi++) { - ttyp = &ttys[(int)pi]; - if (!pi) { - chq_init(&ttyp->inq, ttyp->inq_buf, INQ_SIZE); - } ttyp->ops = &dircon_ops; - ttyp->minor = (int)pi; + (ttyp++)->minor = (int)pi; } #endif #ifdef CONFIG_CHAR_DEV_RS + ttyp = &ttys[4]; for (pi = (char *)4; ((int)pi) < 8; pi++) { - ttyp = &ttys[(int)pi]; ttyp->ops = &rs_ops; - ttyp->minor = ((int)pi) + 60; + (ttyp++)->minor = ((int)pi) + 60; } #endif #ifdef CONFIG_PSEUDO_TTY + ttyp = &ttys[8]; for (pi = 8; ((int)pi) < 8 + NR_PTYS; pi++) { - ttyp = &ttys[(int)pi]; ttyp->ops = &ttyp_ops; - ttyp->minor = (int)pi; + (ttyp++)->minor = (int)pi; } pty_init(); diff -Nur elks.orig/arch/i86/drivers/char/serial.c elks/arch/i86/drivers/char/serial.c --- elks.orig/arch/i86/drivers/char/serial.c 2014-04-26 22:12:31.000000000 -0500 +++ elks/arch/i86/drivers/char/serial.c 2014-10-14 14:28:13.000000000 -0500 @@ -211,8 +211,8 @@ i = 0; while (chq_getch(&tty->outq, &ch, 0) != -1) { - while (!(inb_p(port->io + UART_LSR) & UART_LSR_TEMT)) - /* Do nothing */ ; + do { /* Do nothing */ + } while(!(inb_p(port->io + UART_LSR) & UART_LSR_TEMT)); outb(ch, port->io + UART_TX); i++; } @@ -375,37 +375,32 @@ int rs_init(void) { register struct serial_info *sp = ports; - register char *pi; - int ttyno = 4; + int ttyno = 0; printk("Serial driver version 0.02\n"); - pi = (char *) 4; do { - --pi; - if (sp->tty != NULL) { /* * if rs_init is called twice, because of serial console */ - printk("ttyS%d at 0x%x (irq = %d)", ttyno - 4, sp->io, sp->irq); + printk("ttyS%d at 0x%x (irq = %d)", ttyno, sp->io, sp->irq); print_serial_type(sp->flags & SERF_TYPE); printk(", fetched\n"); ttyno++; } else { if ((rs_probe(sp) == 0) && (!request_irq(sp->irq, rs_irq, NULL))) { printk("ttyS%d at 0x%x (irq = %d)", - ttyno - 4, sp->io, sp->irq); + ttyno, sp->io, sp->irq); print_serial_type(sp->flags & SERF_TYPE); printk("\n"); - sp->tty = &ttys[ttyno++]; + sp->tty = &ttys[4 + ttyno++]; update_port(sp); #if 0 outb_p(? ? ? ?, sp->io + UART_MCR); #endif } } - sp++; - } while (pi); + } while(++sp < &ports[4]); return 0; } diff -Nur elks.orig/fs/buffer.c elks/fs/buffer.c --- elks.orig/fs/buffer.c 2014-04-26 22:12:31.000000000 -0500 +++ elks/fs/buffer.c 2014-10-14 13:54:20.000000000 -0500 @@ -13,20 +13,20 @@ #include <arch/io.h> #include <arch/irq.h> +static struct buffer_head buffers[NR_BUFFERS]; +static char bufmem[NR_MAPBUFS][BLOCK_SIZE]; /* L1 buffer area */ + /* * STUBS for the buffer cache when we put it in */ -static struct buffer_head *bh_chain = NULL; -static struct buffer_head *bh_lru = NULL; +static struct buffer_head *bh_chain = buffers; +static struct buffer_head *bh_lru = buffers; static struct buffer_head *bh_llru = NULL; #if 0 struct wait_queue bufwait; /* Wait for a free buffer */ #endif -static struct buffer_head buffers[NR_BUFFERS]; -static char bufmem[NR_MAPBUFS][BLOCK_SIZE]; /* L1 buffer area */ - #ifdef CONFIG_FS_EXTERNAL_BUFFER static struct wait_queue bufmapwait; /* Wait for a free L1 buffer area */ static struct buffer_head *bufmem_map[NR_MAPBUFS]; /* Array of bufmem's allocation */ @@ -45,13 +45,12 @@ wait_set(&bh->b_wait); - for (;;) { - current->state = TASK_UNINTERRUPTIBLE; - if (!buffer_locked(bh)) { - break; - } + goto chk_buf; + do { schedule(); - } + chk_buf: + current->state = TASK_UNINTERRUPTIBLE; + } while(buffer_locked(bh)); wait_clear(&bh->b_wait); bh->b_count--; @@ -238,7 +237,7 @@ bh = get_hash_table(dev, block); if (bh != NULL) { if (buffer_clean(bh) && buffer_uptodate(bh)) - put_last_lru(bh); + put_last_lru(bh); return bh; } @@ -388,8 +387,8 @@ static int lastumap; -/* map_buffer forces a buffer into L1 buffer space. It will freeze forever - * before failing, so it can return void. This is mostly 8086 dependant, +/* map_buffer forces a buffer into L1 buffer space. It will freeze forever + * before failing, so it can return void. This is mostly 8086 dependant, * although the interface is not. */ void map_buffer(register struct buffer_head *bh) @@ -432,7 +431,7 @@ } } - /* Now, we check for a mapped buffer with no count and then + /* Now, we check for a mapped buffer with no count and then * hopefully find one to send back to L2 */ for (i = (lastumap + 1) % NR_MAPBUFS; i != lastumap; i = ((i + 1) % NR_MAPBUFS)) { @@ -524,9 +523,10 @@ _buf_ds = mm_alloc(NR_BUFFERS * 0x40); lastumap = 0; for (i = 0; i < NR_MAPBUFS; i++) - bufmem_map[i] = 0; + bufmem_map[i] = NULL; #endif + buffers[0].b_prev_lru = NULL; for (i = 0; i < NR_BUFFERS; i++) { #ifdef CONFIG_FS_EXTERNAL_BUFFER bh->b_data = 0; /* L1 buffer cache is reserved! */ @@ -535,11 +535,7 @@ #else bh->b_data = bufmem[i]; #endif - if (i == 0) { - bh_chain = bh; - bh_lru = bh; - bh->b_prev_lru = NULL; - } else { + if (i > 0) { bh->b_prev_lru = bh - 1; } if (i == NR_BUFFERS - 1) { diff -Nur elks.orig/fs/exec.c elks/fs/exec.c --- elks.orig/fs/exec.c 2014-10-14 10:39:02.000000000 -0500 +++ elks/fs/exec.c 2014-10-14 19:12:13.000000000 -0500 @@ -65,7 +65,7 @@ uid_t effuid; gid_t effgid; lsize_t len; - size_t count, result; + size_t result; char load_code = 0; /* @@ -78,14 +78,19 @@ debug1("EXEC: open returned %d\n", -retval); if (retval) - goto end_readexec; + goto error_exec1; debug("EXEC: start building a file handle\n"); /* * Get a reading file handle */ + retval = -ENFILE; filp = get_empty_filp(O_RDONLY); + if(!filp) { + debug("\nNo filps\n"); + goto error_exec1; + } filp->f_inode = inode; #ifdef BLOAT_FS @@ -97,7 +102,7 @@ if ((!filp->f_op) || ((filp->f_op->open) && (filp->f_op->open(inode, filp))) || (!filp->f_op->read)) - goto close_readexec; + goto error_exec2; debug1("EXEC: Inode dev = 0x%x opened OK.\n", inode->i_dev); @@ -129,8 +134,7 @@ if (result != sizeof(mh) || (mh.type != MINIX_SPLITID) || mh.chmem < 1024 || mh.tseg == 0) { debug1("EXEC: bad header, result %u\n", result); - retval = -ENOEXEC; - goto close_readexec; + goto error_exec2; } #ifdef CONFIG_EXEC_ELKS @@ -141,20 +145,17 @@ tregs->ds = ds; if (result != sizeof(msuph)) { debug1("EXEC: Bad secondary header, result %u\n", result); - retval = -ENOEXEC; - goto close_readexec; + goto error_exec2; } stack_top = msuph.msh_dbase; if(stack_top & 0xf){ - retval = -ENOEXEC; - goto close_readexec; + goto error_exec2; } debug1("EXEC: New type executable stack = %x\n", stack_top); } #else if((unsigned int) mh.hlen != 0x20){ - retval = -ENOEXEC; - goto close_readexec; + goto error_exec2; } #endif @@ -176,11 +177,11 @@ } while (++p < &task[MAX_TASKS]); } + retval = -ENOMEM; if (!cseg) { cseg = mm_alloc((segext_t) ((mh.tseg + 15) >> 4)); if (!cseg) { - retval = -ENOMEM; - goto close_readexec; + goto error_exec2; } load_code = 1; } @@ -196,22 +197,19 @@ } len = (len + 15) & ~15L; if (len > (lsize_t) 0x10000L) { - retval = -ENOMEM; - mm_free(cseg); - goto close_readexec; + goto error_exec3; } debug1("EXEC: Allocating %ld bytes for data segment\n", len); dseg = mm_alloc((segext_t) (len >> 4)); if (!dseg) { - retval = -ENOMEM; - mm_free(cseg); - goto close_readexec; + goto error_exec3; } debug2("EXEC: Malloc succeeded - cs=%x ds=%x\n", cseg, dseg); + retval = -ENOEXEC; if(load_code){ tregs->ds = cseg; result = filp->f_op->read(inode, filp, 0, mh.tseg); @@ -219,10 +217,7 @@ if (result != mh.tseg) { debug2("EXEC(tseg read): bad result %u, expected %u\n", result, mh.tseg); - retval = -ENOEXEC; - mm_free(cseg); - mm_free(dseg); - goto close_readexec; + goto error_exec4; } } else { filp->f_pos += mh.tseg; @@ -234,10 +229,7 @@ if (result != mh.dseg) { debug2("EXEC(dseg read): bad result %d, expected %d\n", result, mh.dseg); - retval = -ENOEXEC; - mm_free(cseg); - mm_free(dseg); - goto close_readexec; + goto error_exec4; } /* @@ -251,8 +243,7 @@ ptr = (stack_top) ? (char *) (stack_top - slen) : (char *) (len - slen); - count = slen; - fmemcpy(dseg, (__u16) ptr, current->mm.dseg, (__u16) sptr, (__u16) count); + fmemcpy(dseg, (__u16) ptr, current->mm.dseg, (__u16) sptr, (__u16) slen); /* argv and envp are two NULL-terminated arrays of pointers, located * right after argc. This fixes them up so that the loaded program @@ -335,7 +326,6 @@ if (sgidfile) currentp->egid = effgid; - retval = 0; wake_up(¤tp->p_parent->child_wait); } @@ -343,19 +333,27 @@ * Done */ - close_readexec: - if (filp->f_op->release) - filp->f_op->release(inode, filp); - filp->f_count--; - - end_readexec: - /* * This will return onto the new user stack and to cs:0 of * the user process. */ + retval = 0; + goto normal_out; + + error_exec4: + mm_free(dseg); + + error_exec3: + mm_free(cseg); + + normal_out: + error_exec2: + if(filp->f_op->release) + filp->f_op->release(inode, filp); + filp->f_count--; + + error_exec1: debug1("EXEC: Returning %d\n", retval); return retval; - } diff -Nur elks.orig/fs/inode.c elks/fs/inode.c --- elks.orig/fs/inode.c 2014-10-14 10:49:03.000000000 -0500 +++ elks/fs/inode.c 2014-10-14 14:05:50.000000000 -0500 @@ -24,7 +24,7 @@ static struct inode inode_block[NR_INODE]; static struct inode *first_inode; static struct wait_queue inode_wait; -static int nr_free_inodes; +static int nr_free_inodes = NR_INODE; static void insert_inode_free(register struct inode *inode) { @@ -53,7 +53,6 @@ { register struct inode *inode = inode_block; - nr_free_inodes = NR_INODE; first_inode = inode->i_next = inode->i_prev = inode; do { insert_inode_free(++inode); @@ -73,12 +72,12 @@ return; wait_set(&inode->i_wait); - repeat: - current->state = TASK_UNINTERRUPTIBLE; - if (inode->i_lock) { + goto lwi; + do { schedule(); - goto repeat; - } + lwi: + current->state = TASK_UNINTERRUPTIBLE; + } while(inode->i_lock); wait_clear(&inode->i_wait); current->state = TASK_RUNNING; } diff -Nur elks.orig/kernel/fork.c elks/kernel/fork.c --- elks.orig/kernel/fork.c 2014-10-14 11:28:39.000000000 -0500 +++ elks/kernel/fork.c 2014-10-14 18:19:30.000000000 -0500 @@ -75,10 +75,10 @@ /* * We do shared text. */ - (void) mm_realloc(currentp->mm.cseg); + mm_realloc(currentp->mm.cseg); if (virtual) { - (void) mm_realloc(currentp->mm.dseg); + mm_realloc(currentp->mm.dseg); } else { t->mm.dseg = mm_dup(currentp->mm.dseg); @@ -101,9 +101,11 @@ /* Increase the reference count to all open files */ - for (j = 0; j < NR_OPEN; j++) - if ((filp = currentp->files.fd[j])) + j = 0; + do { + if ((filp = t->files.fd[j])) filp->f_count++; + } while(++j < NR_OPEN); /* Increase the reference count for program text inode - tgm */ t->t_inode->i_count++; diff -Nur elks.orig/kernel/sched.c elks/kernel/sched.c --- elks.orig/kernel/sched.c 2014-10-14 11:28:39.000000000 -0500 +++ elks/kernel/sched.c 2014-10-14 12:33:07.000000000 -0500 @@ -99,24 +99,24 @@ case TASK_INTERRUPTIBLE: if (prev->signal /* & ~prev->blocked */ ) goto makerunnable; - + timeout = prev->timeout; - + if (prev->timeout && (prev->timeout <= jiffies)) { prev->timeout = timeout = 0UL; makerunnable: prev->state = TASK_RUNNING; break; } - + default: del_from_runqueue(prev); /*break; */ case TASK_RUNNING: - break; + ; } set_irq(); - + if(next == &init_task) next = next->next_run;