C language, the function inside the structure

Source: Internet
Author: User

Take linux-3.2 kernel code for example, the use of functions inside a struct:

example, in a driver file, a platform device driver is defined:

static struct Platform_driver S3c24xx_led_driver = {    . Probe        = s3c24xx_led_probe,    . Remove        = S3c24xx_led_remove,    . Driver        = {        . Name        = "S3c24xx_led",        . Owner        =  This_module,},};     

For struct platform_driver, the function required to register the probe platform device, remove is the function required when the platform device is removed.
For different hardware, the registration and removal of the time, there are different parts of the operation, such as the hardware pull down, special register CFG configuration.
These different operations are reflected in the example of S3c24xx_led_probe, S3c24xx_led_remove.
static int s3c24xx_led_remove (struct platform_device *dev) {struct s3c24xx_gpio_led *led = Pdev_to_gpio (dev); led_ Classdev_unregister (&led->cdev); Kfree (LED); return 0;} static int s3c24xx_led_probe (struct platform_device *dev) {struct S3c24xx_led_platdata *pdata = dev->dev.platform_ Data;struct s3c24xx_gpio_led *led;int ret;led = kzalloc (sizeof (struct s3c24xx_gpio_led), gfp_kernel); if (led = = NULL) {de V_err (&dev->dev, "No Memory for Device\n"); return-enomem;} Platform_set_drvdata (Dev, led); led->cdev.brightness_set = S3c24xx_led_set;led->cdev.default_trigger = pdata- >def_trigger;led->cdev.name = pdata->name;led->cdev.flags |= led_core_suspendresume;led->pdata = pdata;/* no point in have a pull-up if we is always driving */if (Pdata->flags & s3c24xx_ledf_tristate) {s3c2410 _gpio_setpin (Pdata->gpio, 0); S3c2410_gpio_cfgpin (Pdata->gpio, s3c2410_gpio_input);} else {s3c2410_gpio_pullup (pdata->gpio, 0); S3c2410_gpio_setpin (Pdata->gpio, 0); s3c2410_gpio_cfgpin (Pdata->gpio, s3c2410_gpio_output);} /* Register our new LED device */ret = Led_classdev_register (&dev->dev, &led->cdev); if (Ret < 0) {Dev_err (&dev->dev, "Led_classdev_register failed\n"); Kfree (LED); return ret;} return 0;}

  


Using the struct, we implement an object-oriented idea, which is an instantiated structure object that describes the behavior of the object (function), a variable (variable, struct variable, etc.) that describes the object's eigenvalues or objects.

The following is a device structure definition for reference:

struct platform_driver {    int (*probe) (struct platform_device *);    Int (*remove) (struct platform_device *);    void (*shutdown) (struct platform_device *);    Int (*suspend) (struct Platform_device *, pm_message_t state);    Int (*resume) (struct Platform_device *), struct device_driver driver; const struct PLATFORM_DEVICE_ID *ID _table;};       

/** struct Device_driver-the Basic device driver structure * @name: Name of the device driver. * @bus: The bus which the device of this driver belongs to. * @owner: the module owner. * @mod_name: Used for built-in modules. * @suppress_bind_attrs: Disables Bind/unbind via SYSFS. * @of_match_table: The Open Firmware table. * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind T He driver * to a specific device. * @remove: Called when the device was removed from the system to * unbind a device from this driver. * @shutdown: Called at shut-down time to quiesce the device. * @suspend: Called to put the device to sleep mode. Usually to a * low power state. * @resume: Called to bring a device from sleep mode. * @groups: Default attributes that get created by the driver core * automatically. * @pm: Power management operations of the device which matched * this Driver. * @p:driver core s private data, no one other than the Driver * core can touch this. * * The device Driver-model tracks all of the drivers known to the system. * The main reason for this tracking are to enable the driver core to match * up drivers with new devices. Once drivers is known objects within the * system, however, a number of other things become possible.  Device drivers * Can export information and configuration variables that is independent * of any specific device.*/structDevice_driver {Const Char*name; structBus_type *bus; structModule *owner; Const Char*mod_name;/*used for built-in modules*/    BOOLSuppress_bind_attrs;/*disables Bind/unbind via Sysfs*/    Const structOF_DEVICE_ID *of_match_table; int(*probe) (structDevice *Dev); int(*remove) (structDevice *Dev); void(*shutdown) (structDevice *Dev); int(*suspend) (structDevice *Dev, pm_message_t State); int(*resume) (structDevice *Dev); Const structAttribute_group * *groups; Const structDev_pm_ops *pm; structDriver_private *p;};

...

C language, the function inside the structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.