* @brief This function will change the size of previously allocated memory block. * * @param m the slab memory management object. * * @param ptr is the previously allocated memory block. * * @param size is the new size of memory block. * * @return the allocated memory. */
| 667 | * @return the allocated memory. |
| 668 | */ |
| 669 | void *rt_slab_realloc(rt_slab_t m, void *ptr, rt_size_t size) |
| 670 | { |
| 671 | void *nptr; |
| 672 | struct rt_slab_zone *z; |
| 673 | struct rt_slab_memusage *kup; |
| 674 | struct rt_slab *slab = (struct rt_slab *)m; |
| 675 | |
| 676 | if (ptr == RT_NULL) |
| 677 | return rt_slab_alloc(m, size); |
| 678 | |
| 679 | if (size == 0) |
| 680 | { |
| 681 | rt_slab_free(m, ptr); |
| 682 | return RT_NULL; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Get the original allocation's zone. If the new request winds up |
| 687 | * using the same chunk size we do not have to do anything. |
| 688 | */ |
| 689 | kup = btokup((rt_uintptr_t)ptr & ~RT_MM_PAGE_MASK); |
| 690 | if (kup->type == PAGE_TYPE_LARGE) |
| 691 | { |
| 692 | rt_size_t osize; |
| 693 | |
| 694 | osize = kup->size << RT_MM_PAGE_BITS; |
| 695 | if ((nptr = rt_slab_alloc(m, size)) == RT_NULL) |
| 696 | return RT_NULL; |
| 697 | rt_memcpy(nptr, ptr, size > osize ? osize : size); |
| 698 | rt_slab_free(m, ptr); |
| 699 | |
| 700 | return nptr; |
| 701 | } |
| 702 | else if (kup->type == PAGE_TYPE_SMALL) |
| 703 | { |
| 704 | z = (struct rt_slab_zone *)(((rt_uintptr_t)ptr & ~RT_MM_PAGE_MASK) - |
| 705 | kup->size * RT_MM_PAGE_SIZE); |
| 706 | RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC); |
| 707 | |
| 708 | zoneindex(&size); |
| 709 | if (z->z_chunksize == size) |
| 710 | return (ptr); /* same chunk */ |
| 711 | |
| 712 | /* |
| 713 | * Allocate memory for the new request size. Note that zoneindex has |
| 714 | * already adjusted the request size to the appropriate chunk size, which |
| 715 | * should optimize our bcopy(). Then copy and return the new pointer. |
| 716 | */ |
| 717 | if ((nptr = rt_slab_alloc(m, size)) == RT_NULL) |
| 718 | return RT_NULL; |
| 719 | |
| 720 | rt_memcpy(nptr, ptr, size > z->z_chunksize ? z->z_chunksize : size); |
| 721 | rt_slab_free(m, ptr); |
| 722 | |
| 723 | return nptr; |
| 724 | } |
| 725 | |
| 726 | return RT_NULL; |
no test coverage detected