util.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <linux/slab.h>
  2. #include <linux/string.h>
  3. #include <linux/module.h>
  4. #include <linux/err.h>
  5. #include <asm/uaccess.h>
  6. /**
  7. * kstrdup - allocate space for and copy an existing string
  8. * @s: the string to duplicate
  9. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  10. */
  11. char *kstrdup(const char *s, gfp_t gfp)
  12. {
  13. size_t len;
  14. char *buf;
  15. if (!s)
  16. return NULL;
  17. len = strlen(s) + 1;
  18. buf = kmalloc_track_caller(len, gfp);
  19. if (buf)
  20. memcpy(buf, s, len);
  21. return buf;
  22. }
  23. EXPORT_SYMBOL(kstrdup);
  24. /**
  25. * kstrndup - allocate space for and copy an existing string
  26. * @s: the string to duplicate
  27. * @max: read at most @max chars from @s
  28. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  29. */
  30. char *kstrndup(const char *s, size_t max, gfp_t gfp)
  31. {
  32. size_t len;
  33. char *buf;
  34. if (!s)
  35. return NULL;
  36. len = strnlen(s, max);
  37. buf = kmalloc_track_caller(len+1, gfp);
  38. if (buf) {
  39. memcpy(buf, s, len);
  40. buf[len] = '\0';
  41. }
  42. return buf;
  43. }
  44. EXPORT_SYMBOL(kstrndup);
  45. /**
  46. * kmemdup - duplicate region of memory
  47. *
  48. * @src: memory region to duplicate
  49. * @len: memory region length
  50. * @gfp: GFP mask to use
  51. */
  52. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  53. {
  54. void *p;
  55. p = kmalloc_track_caller(len, gfp);
  56. if (p)
  57. memcpy(p, src, len);
  58. return p;
  59. }
  60. EXPORT_SYMBOL(kmemdup);
  61. /**
  62. * __krealloc - like krealloc() but don't free @p.
  63. * @p: object to reallocate memory for.
  64. * @new_size: how many bytes of memory are required.
  65. * @flags: the type of memory to allocate.
  66. *
  67. * This function is like krealloc() except it never frees the originally
  68. * allocated buffer. Use this if you don't want to free the buffer immediately
  69. * like, for example, with RCU.
  70. */
  71. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  72. {
  73. void *ret;
  74. size_t ks = 0;
  75. if (unlikely(!new_size))
  76. return ZERO_SIZE_PTR;
  77. if (p)
  78. ks = ksize(p);
  79. if (ks >= new_size)
  80. return (void *)p;
  81. ret = kmalloc_track_caller(new_size, flags);
  82. if (ret && p)
  83. memcpy(ret, p, ks);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(__krealloc);
  87. /**
  88. * krealloc - reallocate memory. The contents will remain unchanged.
  89. * @p: object to reallocate memory for.
  90. * @new_size: how many bytes of memory are required.
  91. * @flags: the type of memory to allocate.
  92. *
  93. * The contents of the object pointed to are preserved up to the
  94. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  95. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  96. * %NULL pointer, the object pointed to is freed.
  97. */
  98. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  99. {
  100. void *ret;
  101. if (unlikely(!new_size)) {
  102. kfree(p);
  103. return ZERO_SIZE_PTR;
  104. }
  105. ret = __krealloc(p, new_size, flags);
  106. if (ret && p != ret)
  107. kfree(p);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(krealloc);
  111. /*
  112. * strndup_user - duplicate an existing string from user space
  113. * @s: The string to duplicate
  114. * @n: Maximum number of bytes to copy, including the trailing NUL.
  115. */
  116. char *strndup_user(const char __user *s, long n)
  117. {
  118. char *p;
  119. long length;
  120. length = strnlen_user(s, n);
  121. if (!length)
  122. return ERR_PTR(-EFAULT);
  123. if (length > n)
  124. return ERR_PTR(-EINVAL);
  125. p = kmalloc(length, GFP_KERNEL);
  126. if (!p)
  127. return ERR_PTR(-ENOMEM);
  128. if (copy_from_user(p, s, length)) {
  129. kfree(p);
  130. return ERR_PTR(-EFAULT);
  131. }
  132. p[length - 1] = '\0';
  133. return p;
  134. }
  135. EXPORT_SYMBOL(strndup_user);