goto???

Martin Knappe kohaerenzstifter at posteo.de
Fri Jul 17 04:48:53 EDT 2015


Very easy:

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;

     void cleanup() {
         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);
         }
     }

     if (dgap_numboards >= MAXBOARDS)
         return -EPERM;

     rc = pci_enable_device(pdev);
     if (rc)
         return -EIO;

     brd = dgap_found_board(pdev, ent->driver_data, dgap_numboards);
     if (IS_ERR(brd))
         return PTR_ERR(brd);

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

     if (rc) {
         cleanup();
         return rc;
     }

     rc = dgap_alloc_flipbuf(brd);

     if (rc) {
         cleanup();
         return rc;
     }

     rc = dgap_tty_register(brd);
     cleanupState++;

     if (rc) {
         cleanup();
         return rc;
     }

     rc = dgap_request_irq(brd);
     cleanupState++;

     if (rc) {
         cleanup();
         return rc;
     }

     /*
     * Do tty device initialization.
     */

     rc = dgap_tty_init(brd);
     cleanupState++;

     if (rc) {
         cleanup();
         return rc;
     }

     rc = dgap_tty_register_ports(brd);
     cleanupState++;

     if (rc) {
         cleanup();
         return rc;
     }

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

     dgap_board[dgap_numboards++] = brd;

     return 0;
}

Am 17.07.2015 10:11 schrieb Sudip Mukherjee:
> On Fri, Jul 17, 2015 at 1:25 PM, Navy <navych at 126.com> wrote:
>> Hello,
>> Goto is recommend in linux kernel programming, but it is despised in 
>> many other situation. There are four rationable for using goto in 
>> Documentation/CodingStyle. Do you have some viewpoints about "why 
>> goto" or "why
> not goto"? I'm glad to get your point.
> Check the file  drivers/staging/dgap/dgap.c there is a function
> called dgap_init_one() which is using 6 goto statements. Please
> try to convert that file without using goto and i hope you will 
> understand
> practically why goto.
> 
> regards
> sudip
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



More information about the Kernelnewbies mailing list