util.c 2.9 KB

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