util.c 771 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <linux/slab.h>
  2. #include <linux/string.h>
  3. #include <linux/module.h>
  4. /**
  5. * kzalloc - allocate memory. The memory is set to zero.
  6. * @size: how many bytes of memory are required.
  7. * @flags: the type of memory to allocate.
  8. */
  9. void *kzalloc(size_t size, gfp_t flags)
  10. {
  11. void *ret = kmalloc(size, flags);
  12. if (ret)
  13. memset(ret, 0, size);
  14. return ret;
  15. }
  16. EXPORT_SYMBOL(kzalloc);
  17. /*
  18. * kstrdup - allocate space for and copy an existing string
  19. *
  20. * @s: the string to duplicate
  21. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  22. */
  23. char *kstrdup(const char *s, gfp_t gfp)
  24. {
  25. size_t len;
  26. char *buf;
  27. if (!s)
  28. return NULL;
  29. len = strlen(s) + 1;
  30. buf = kmalloc(len, gfp);
  31. if (buf)
  32. memcpy(buf, s, len);
  33. return buf;
  34. }
  35. EXPORT_SYMBOL(kstrdup);