kmemdup

来源:互联网 发布:js触发事件 自动 编辑:程序博客网 时间:2024/05/17 23:47
在kernel中可以通过kmemdup将一种类型的数据赋值给另外同一个类型的数据。例如下例:就是要将thermal_zone_device_ops的数据赋值给ops。这个属于浅拷贝.struct thermal_zone_device_ops *ops;ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);static struct thermal_zone_device_ops of_thermal_ops = {.get_mode = of_thermal_get_mode,.set_mode = of_thermal_set_mode,.get_trip_type = of_thermal_get_trip_type,.get_trip_temp = of_thermal_get_trip_temp,.set_trip_temp = of_thermal_set_trip_temp,.get_trip_hyst = of_thermal_get_trip_hyst,.set_trip_hyst = of_thermal_set_trip_hyst,.get_crit_temp = of_thermal_get_crit_temp,.bind = of_thermal_bind,.unbind = of_thermal_unbind,};我们看看kmemdup的源码void *kmemdup(const void *src, size_t len, gfp_t gfp){void *p;//先申请memoryp = kmalloc_track_caller(len, gfp);if (p) //如果memory申请成功,则通过memcpy赋值,所以这里很明确看出是浅拷贝。同时可以知道以后结构体复制可以通过memcpy直接进行memcpy(p, src, len);return p;}