* @brief This function will allocate an object from object system. * * @param type Type of object. The type of the allocated object can only be of * type rt_object_class_type other than RT_Object_Class_Static. * In addition, the type of object allocated through this interface * is dynamic, not static. * * @param name Name of the object. In system, the obj
| 475 | * @return object handle allocated successfully, or RT_NULL if no memory can be allocated. |
| 476 | */ |
| 477 | rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) |
| 478 | { |
| 479 | struct rt_object *object; |
| 480 | rt_base_t level; |
| 481 | rt_size_t obj_name_len; |
| 482 | struct rt_object_information *information; |
| 483 | #ifdef RT_USING_MODULE |
| 484 | struct rt_dlmodule *module = dlmodule_self(); |
| 485 | #endif /* RT_USING_MODULE */ |
| 486 | |
| 487 | RT_DEBUG_NOT_IN_INTERRUPT; |
| 488 | |
| 489 | /* get object information */ |
| 490 | information = rt_object_get_information(type); |
| 491 | RT_ASSERT(information != RT_NULL); |
| 492 | |
| 493 | object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size); |
| 494 | if (object == RT_NULL) |
| 495 | { |
| 496 | /* no memory can be allocated */ |
| 497 | return RT_NULL; |
| 498 | } |
| 499 | |
| 500 | /* clean memory data of object */ |
| 501 | rt_memset(object, 0x0, information->object_size); |
| 502 | |
| 503 | /* initialize object's parameters */ |
| 504 | |
| 505 | /* set object type */ |
| 506 | object->type = type; |
| 507 | |
| 508 | /* set object flag */ |
| 509 | object->flag = 0; |
| 510 | |
| 511 | #if RT_NAME_MAX > 0 |
| 512 | if (name) |
| 513 | { |
| 514 | obj_name_len = rt_strlen(name); |
| 515 | if(obj_name_len > RT_NAME_MAX - 1) |
| 516 | { |
| 517 | LOG_E("Object name %s exceeds RT_NAME_MAX=%d, consider increasing RT_NAME_MAX.", name, RT_NAME_MAX); |
| 518 | RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1); |
| 519 | } |
| 520 | rt_memcpy(object->name, name, obj_name_len); |
| 521 | object->name[obj_name_len] = '\0'; |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | object->name[0] = '\0'; |
| 526 | } |
| 527 | #else |
| 528 | object->name = name; |
| 529 | #endif |
| 530 | |
| 531 | RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object)); |
| 532 | |
| 533 | level = rt_spin_lock_irqsave(&(information->spinlock)); |
| 534 |