Ignore:
Timestamp:
Jan 5, 2021, 5:37:31 AM (5 years ago)
Author:
Paul Smedley
Message:

Update krealloc implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GPL/branches/uniaud32-next/lib32/memory.c

    r631 r632  
    708708//******************************************************************************
    709709//******************************************************************************
    710 /* krealloc() wrapper */
    711 //from linux 2.6
     710static inline void *__do_krealloc(const void *p, size_t new_size,
     711                                           gfp_t flags)
     712{
     713        void *ret;
     714        size_t ks = 0;
     715
     716        if (p)
     717                ks = ksize(p);
     718
     719        if (ks >= new_size)
     720                return (void *)p;
     721
     722        ret = __kmalloc(new_size, flags);
     723        if (ret && p)
     724                memcpy(ret, p, ks);
     725
     726        return ret;
     727}
     728//******************************************************************************
     729//******************************************************************************
     730/**
     731 * krealloc - reallocate memory. The contents will remain unchanged.
     732 * @p: object to reallocate memory for.
     733 * @new_size: how many bytes of memory are required.
     734 * @flags: the type of memory to allocate.
     735 *
     736 * The contents of the object pointed to are preserved up to the
     737 * lesser of the new and old sizes.  If @p is %NULL, krealloc()
     738 * behaves exactly like kmalloc().  If @new_size is 0 and @p is not a
     739 * %NULL pointer, the object pointed to is freed.
     740 */
    712741void *krealloc(const void *p, size_t new_size, gfp_t flags)
    713742{
    714743        void *ret;
    715 
    716         if (!p)
    717                 return __kmalloc(new_size, flags);
    718744
    719745        if (!new_size) {
    720746                kfree(p);
    721                 return NULL;
    722         }
    723 
    724         ret = __kmalloc(new_size, flags);
    725         if (!ret)
    726                 return NULL;
    727 
    728         memcpy(ret, p, min(new_size, ksize(p)));
    729         kfree(p);
     747                return ZERO_SIZE_PTR;
     748        }
     749
     750        ret = __do_krealloc(p, new_size, flags);
     751        if (ret && p != ret)
     752                kfree(p);
     753
    730754        return ret;
    731755}
Note: See TracChangeset for help on using the changeset viewer.