util.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 - reallocate memory. The contents will remain unchanged.
  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. * The contents of the object pointed to are preserved up to the
  68. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  69. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  70. * %NULL pointer, the object pointed to is freed.
  71. */
  72. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  73. {
  74. void *ret;
  75. size_t ks = 0;
  76. if (unlikely(!new_size)) {
  77. kfree(p);
  78. return ZERO_SIZE_PTR;
  79. }
  80. if (p)
  81. ks = ksize(p);
  82. if (ks >= new_size)
  83. return (void *)p;
  84. ret = kmalloc_track_caller(new_size, flags);
  85. if (ret && p) {
  86. memcpy(ret, p, ks);
  87. kfree(p);
  88. }
  89. return ret;
  90. }
  91. EXPORT_SYMBOL(krealloc);
  92. /*
  93. * strndup_user - duplicate an existing string from user space
  94. * @s: The string to duplicate
  95. * @n: Maximum number of bytes to copy, including the trailing NUL.
  96. */
  97. char *strndup_user(const char __user *s, long n)
  98. {
  99. char *p;
  100. long length;
  101. length = strnlen_user(s, n);
  102. if (!length)
  103. return ERR_PTR(-EFAULT);
  104. if (length > n)
  105. return ERR_PTR(-EINVAL);
  106. p = kmalloc(length, GFP_KERNEL);
  107. if (!p)
  108. return ERR_PTR(-ENOMEM);
  109. if (copy_from_user(p, s, length)) {
  110. kfree(p);
  111. return ERR_PTR(-EFAULT);
  112. }
  113. p[length - 1] = '\0';
  114. return p;
  115. }
  116. EXPORT_SYMBOL(strndup_user);