goto???

Martin Knappe kohaerenzstifter at posteo.de
Fri Jul 17 05:40:00 EDT 2015


Like so:

static int dgap_init_one(struct pci_dev *pdev, const struct 
pci_device_id *ent)
{
     int rc = 0;
     int cleanupState = 0;
     struct board_t *brd;

     if (dgap_numboards >= MAXBOARDS) {
         rc = -EPERM;
         goto finish;
     }

     rc = pci_enable_device(pdev);
     if (rc) {
         rc = -EIO;
         goto finish;
     }

     brd = dgap_found_board(pdev, ent->driver_data, dgap_numboards);
     if (IS_ERR(brd)) {
         rc = PTR_ERR(brd);
         goto finish;
     }

     rc = dgap_firmware_load(pdev, ent->driver_data, brd);
     cleanupState++;

     if (rc) {
         goto finish;
     }

     rc = dgap_alloc_flipbuf(brd);

     if (rc) {
         goto finish;
     }

     rc = dgap_tty_register(brd);
     cleanupState++;

     if (rc) {
         goto finish;
     }

     rc = dgap_request_irq(brd);
     cleanupState++;

     if (rc) {
         goto finish;
     }

     /*
     * Do tty device initialization.
     */

     rc = dgap_tty_init(brd);
     cleanupState++;

     if (rc) {
         goto finish;
     }

     rc = dgap_tty_register_ports(brd);
     cleanupState++;

     if (rc) {
         goto finish;
     }

     brd->state = BOARD_READY;
     brd->dpastatus = BD_RUNNING;

     dgap_board[dgap_numboards++] = brd;

finish:
     if (cleanupState > 4) {
         dgap_tty_free(brd);
     }
     if (cleanupState > 3) {
         dgap_free_irq(brd);
         dgap_tty_unregister(brd);
     }
     if (cleanupState > 2) {
         dgap_tty_unregister(brd);
     }
     if (cleanupState > 1) {
         dgap_free_flipbuf(brd);
     }
     if (cleanupState > 0) {
         dgap_cleanup_nodes();
         dgap_unmap(brd);
         kfree(brd);
     }
     return rc;
}

Am 17.07.2015 11:31 schrieb Sudip Mukherjee:
> On Fri, Jul 17, 2015 at 2:52 PM, Martin Knappe
> <kohaerenzstifter at posteo.de> wrote:
>> I'm just messing ...
>> I guess I felt a bit challenged by your "try to write that without 
>> using
>> goto"
> Hey, it was not a challenge. main thing is the readability.
> But going by your general rules how will you modify this
> same function of dgap? I think I can get rid of multiple return
> only if i modify the code into a series of if - else .
> or anything simple?
> 
> regards
> sudip



More information about the Kernelnewbies mailing list