alloc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * For MMU hosts we need to track the size of the allocations otherwise
  3. * munmap will fail to free the memory (EINVAL).
  4. */
  5. #include <features.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/mman.h>
  12. #ifdef L_calloc_dbg
  13. void *calloc_dbg(size_t num, size_t size, char *function, char *file,
  14. int line)
  15. {
  16. void *ptr;
  17. fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", (int) (num * size),
  18. function, file, line);
  19. ptr = calloc(num, size);
  20. fprintf(stderr, "%p\n", ptr);
  21. return ptr;
  22. }
  23. #endif
  24. #ifdef L_malloc_dbg
  25. void *malloc_dbg(size_t size, char *function, char *file, int line)
  26. {
  27. void *result;
  28. fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", (int) size, function,
  29. file, line);
  30. result = malloc(size);
  31. fprintf(stderr, "%p\n", result);
  32. return result;
  33. }
  34. #endif
  35. #ifdef L_free_dbg
  36. void free_dbg(void *ptr, char *function, char *file, int line)
  37. {
  38. fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file,
  39. line);
  40. free(ptr);
  41. }
  42. #endif
  43. #ifdef L_calloc
  44. void *calloc(size_t num, size_t size)
  45. {
  46. void *ptr = malloc(num * size);
  47. if (ptr)
  48. memset(ptr, 0, num * size);
  49. return ptr;
  50. }
  51. #endif
  52. #ifdef L_malloc
  53. void *malloc(size_t size)
  54. {
  55. void *result;
  56. /* Some programs will call malloc (0). Lets be strict and return NULL */
  57. if (size == 0)
  58. return NULL;
  59. #ifdef __UCLIBC_HAS_MMU__
  60. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  61. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  62. #else
  63. result = mmap((void *) 0, size, PROT_READ | PROT_WRITE,
  64. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  65. #endif
  66. if (result == MAP_FAILED)
  67. return 0;
  68. #ifdef __UCLIBC_HAS_MMU__
  69. * (size_t *) result = size;
  70. return(result + sizeof(size_t));
  71. #else
  72. return(result);
  73. #endif
  74. }
  75. #endif
  76. #ifdef L_free
  77. void free(void *ptr)
  78. {
  79. #ifdef __UCLIBC_HAS_MMU__
  80. if (ptr) {
  81. ptr -= sizeof(size_t);
  82. munmap(ptr, * (size_t *) ptr);
  83. }
  84. #else
  85. munmap(ptr, 0);
  86. #endif
  87. }
  88. #endif
  89. #ifdef L_realloc
  90. void *realloc(void *ptr, size_t size)
  91. {
  92. void *newptr = NULL;
  93. if (size > 0) {
  94. newptr = malloc(size);
  95. if (newptr && ptr) {
  96. #ifdef __UCLIBC_HAS_MMU__
  97. memcpy(newptr, ptr, * ((size_t *) (ptr - sizeof(size_t))));
  98. #else
  99. memcpy(newptr, ptr, size);
  100. #endif
  101. free(ptr);
  102. }
  103. }
  104. else
  105. free(ptr);
  106. return newptr;
  107. }
  108. #endif