1
0

memLib.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * memLib.c
  3. *
  4. * We undefine the public functions
  5. * (just in case they've been MACRO overriden
  6. */
  7. #undef calloc
  8. #undef cfree
  9. #undef memFindMax
  10. #undef memInit
  11. #undef memLibInit
  12. #undef memOptionsSet
  13. #undef memPartFindMax
  14. #undef memPartOptionsSet
  15. #undef memPartRealloc
  16. #undef memalign
  17. #undef realloc
  18. #undef valloc
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <memPartLib.h>
  22. #include <memLib.h>
  23. #include <private/funcBindP.h>
  24. #include "private.h"
  25. #define VX_PAGE_SIZE 4096
  26. /*-------------------------------------------------------- */
  27. void *
  28. calloc(size_t elemNum,
  29. size_t elemSize)
  30. {
  31. size_t nBytes = elemNum * elemSize;
  32. char *ptr = malloc(nBytes);
  33. memset(ptr,0,nBytes);
  34. return ptr;
  35. }
  36. /*-------------------------------------------------------- */
  37. STATUS
  38. cfree(char *pBlock)
  39. {
  40. return memPartFree(memSysPartId,pBlock);
  41. }
  42. /*-------------------------------------------------------- */
  43. int
  44. memFindMax(void)
  45. {
  46. return memPartFindMax(memSysPartId);
  47. }
  48. /*-------------------------------------------------------- */
  49. STATUS
  50. memLibInit(char *pPool,
  51. unsigned poolSize)
  52. {
  53. memPartLibInit(pPool,poolSize);
  54. return OK;
  55. }
  56. /*-------------------------------------------------------- */
  57. STATUS
  58. memInit(char *pPool,
  59. unsigned poolSize)
  60. {
  61. /* set up the system pointers */
  62. _func_valloc = (FUNCPTR) valloc;
  63. _func_memalign = (FUNCPTR) memalign;
  64. return memLibInit(pPool,poolSize);
  65. }
  66. /*-------------------------------------------------------- */
  67. void
  68. memOptionsSet (unsigned options)
  69. {
  70. memPartOptionsSet(memSysPartId,options);
  71. }
  72. /*-------------------------------------------------------- */
  73. int
  74. memPartFindMax(PART_ID partId)
  75. {
  76. partition_t *this = (partition_t *)partId;
  77. size_t result;
  78. semTake(&this->sem,WAIT_FOREVER);
  79. result = lub_heap__get_max_free(this->heap);
  80. semGive(&this->sem);
  81. return result;
  82. }
  83. /*-------------------------------------------------------- */
  84. STATUS
  85. memPartOptionsSet(PART_ID partId,
  86. unsigned options)
  87. {
  88. partition_t *this = (partition_t*)partId;
  89. this->options = options;
  90. return OK;
  91. }
  92. /*-------------------------------------------------------- */
  93. void *
  94. memPartRealloc(PART_ID partId,
  95. char *pBlock,
  96. unsigned nBytes)
  97. {
  98. partition_t *this = (partition_t *)partId;
  99. lub_heap_status_t status;
  100. semTake(&this->sem,WAIT_FOREVER);
  101. status = lub_heap_realloc(this->heap,&pBlock,nBytes,LUB_HEAP_ALIGN_NATIVE);
  102. semGive(&this->sem);
  103. lubheap_vxworks_check_status(this,status,"memPartRealloc",pBlock,nBytes);
  104. return (LUB_HEAP_OK == status) ? pBlock : NULL;
  105. }
  106. /*-------------------------------------------------------- */
  107. void *
  108. memalign(unsigned alignment,
  109. unsigned size)
  110. {
  111. return memPartAlignedAlloc(memSysPartId,size,alignment);
  112. }
  113. /*-------------------------------------------------------- */
  114. void *
  115. realloc(void *ptr, size_t nBytes)
  116. {
  117. return memPartRealloc(memSysPartId,ptr,nBytes);
  118. }
  119. /*-------------------------------------------------------- */
  120. void *
  121. valloc(unsigned size)
  122. {
  123. return memalign(VX_PAGE_SIZE,size);
  124. }
  125. /*-------------------------------------------------------- */