util.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * __kzalloc - allocate memory. The memory is set to zero.
  8. * @size: how many bytes of memory are required.
  9. * @flags: the type of memory to allocate.
  10. */
  11. void *__kzalloc(size_t size, gfp_t flags)
  12. {
  13. void *ret = kmalloc_track_caller(size, flags);
  14. if (ret)
  15. memset(ret, 0, size);
  16. return ret;
  17. }
  18. EXPORT_SYMBOL(__kzalloc);
  19. /*
  20. * kstrdup - allocate space for and copy an existing string
  21. *
  22. * @s: the string to duplicate
  23. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  24. */
  25. char *kstrdup(const char *s, gfp_t gfp)
  26. {
  27. size_t len;
  28. char *buf;
  29. if (!s)
  30. return NULL;
  31. len = strlen(s) + 1;
  32. buf = kmalloc_track_caller(len, gfp);
  33. if (buf)
  34. memcpy(buf, s, len);
  35. return buf;
  36. }
  37. EXPORT_SYMBOL(kstrdup);
  38. /**
  39. * kmemdup - duplicate region of memory
  40. *
  41. * @src: memory region to duplicate
  42. * @len: memory region length
  43. * @gfp: GFP mask to use
  44. */
  45. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  46. {
  47. void *p;
  48. p = kmalloc_track_caller(len, gfp);
  49. if (p)
  50. memcpy(p, src, len);
  51. return p;
  52. }
  53. EXPORT_SYMBOL(kmemdup);
  54. /**
  55. * krealloc - reallocate memory. The contents will remain unchanged.
  56. * @p: object to reallocate memory for.
  57. * @new_size: how many bytes of memory are required.
  58. * @flags: the type of memory to allocate.
  59. *
  60. * The contents of the object pointed to are preserved up to the
  61. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  62. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  63. * %NULL pointer, the object pointed to is freed.
  64. */
  65. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  66. {
  67. void *ret;
  68. size_t ks;
  69. if (unlikely(!new_size)) {
  70. kfree(p);
  71. return ZERO_SIZE_PTR;
  72. }
  73. ks = ksize(p);
  74. if (ks >= new_size)
  75. return (void *)p;
  76. ret = kmalloc_track_caller(new_size, flags);
  77. if (ret) {
  78. memcpy(ret, p, min(new_size, ks));
  79. kfree(p);
  80. }
  81. return ret;
  82. }
  83. EXPORT_SYMBOL(krealloc);
  84. /*
  85. * strndup_user - duplicate an existing string from user space
  86. *
  87. * @s: The string to duplicate
  88. * @n: Maximum number of bytes to copy, including the trailing NUL.
  89. */
  90. char *strndup_user(const char __user *s, long n)
  91. {
  92. char *p;
  93. long length;
  94. length = strnlen_user(s, n);
  95. if (!length)
  96. return ERR_PTR(-EFAULT);
  97. if (length > n)
  98. return ERR_PTR(-EINVAL);
  99. p = kmalloc(length, GFP_KERNEL);
  100. if (!p)
  101. return ERR_PTR(-ENOMEM);
  102. if (copy_from_user(p, s, length)) {
  103. kfree(p);
  104. return ERR_PTR(-EFAULT);
  105. }
  106. p[length - 1] = '\0';
  107. return p;
  108. }
  109. EXPORT_SYMBOL(strndup_user);