dl.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <config-util.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <sys/mman.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. void *
  27. grub_osdep_dl_memalign (grub_size_t align, grub_size_t size)
  28. {
  29. void *ret;
  30. if (align < 8192 * 16)
  31. align = 8192 * 16;
  32. size = ALIGN_UP (size, 8192 * 16);
  33. #if defined(HAVE_POSIX_MEMALIGN)
  34. if (posix_memalign (&ret, align, size) != 0)
  35. ret = 0;
  36. #elif defined(HAVE_MEMALIGN)
  37. ret = memalign (align, size);
  38. #else
  39. #error "Complete this"
  40. #endif
  41. if (!ret)
  42. {
  43. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  44. return NULL;
  45. }
  46. mprotect (ret, size, PROT_READ | PROT_WRITE | PROT_EXEC);
  47. return ret;
  48. }
  49. void
  50. grub_dl_osdep_dl_free (void *ptr)
  51. {
  52. if (ptr)
  53. free (ptr);
  54. }