util.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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;
  76. if (unlikely(!new_size)) {
  77. kfree(p);
  78. return ZERO_SIZE_PTR;
  79. }
  80. ks = ksize(p);
  81. if (ks >= new_size)
  82. return (void *)p;
  83. ret = kmalloc_track_caller(new_size, flags);
  84. if (ret) {
  85. memcpy(ret, p, min(new_size, ks));
  86. kfree(p);
  87. }
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(krealloc);
  91. /*
  92. * strndup_user - duplicate an existing string from user space
  93. * @s: The string to duplicate
  94. * @n: Maximum number of bytes to copy, including the trailing NUL.
  95. */
  96. char *strndup_user(const char __user *s, long n)
  97. {
  98. char *p;
  99. long length;
  100. length = strnlen_user(s, n);
  101. if (!length)
  102. return ERR_PTR(-EFAULT);
  103. if (length > n)
  104. return ERR_PTR(-EINVAL);
  105. p = kmalloc(length, GFP_KERNEL);
  106. if (!p)
  107. return ERR_PTR(-ENOMEM);
  108. if (copy_from_user(p, s, length)) {
  109. kfree(p);
  110. return ERR_PTR(-EFAULT);
  111. }
  112. p[length - 1] = '\0';
  113. return p;
  114. }
  115. EXPORT_SYMBOL(strndup_user);