mem_space.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // definition of the function prototypes in mem_space.h
  2. // check pointer
  3. owl_bool owl_check_mem_space_pointer(owl_byte * src, owl_umax size)
  4. {
  5. return (src != NULL && size >= sizeof(owl_mem_space));
  6. }
  7. // check members
  8. owl_bool owl_check_mem_space_members(owl_byte * ptr, owl_umax size)
  9. {
  10. return (ptr != NULL && size != 0);
  11. }
  12. // check all
  13. owl_bool owl_check_mem_space_all(owl_mem_space * src, owl_umax size)
  14. {
  15. return owl_check_mem_space_pointer((owl_byte*) src, size)
  16. && owl_check_mem_space_members(src -> ptr, src -> size);
  17. }
  18. // allocate memory and create the structure
  19. owl_mem_space * owl_create_mem_space(owl_umax size)
  20. {
  21. // check global error
  22. owl_mem_space * space = NULL;
  23. if (owl_check_mem_space_members(NOT_NULL, size) == owl_false)
  24. goto end;
  25. // create a new owl_mem_space variable into
  26. // a free slot in the memory space table
  27. for (owl_umax i = 0; i < OWL_MEM_SPACE_TB_SIZE; i++)
  28. if (OWL_MEM_SPACE_TB[i].ptr == NULL && OWL_MEM_SPACE_TB[i].size == 0) {
  29. // allocate memory with calloc()
  30. OWL_MEM_SPACE_TB[i].ptr = calloc(size, 1);
  31. if (OWL_MEM_SPACE_TB[i].ptr == NULL)
  32. goto end;
  33. OWL_MEM_SPACE_TB[i].size = size;
  34. OWL_MEM_SPACE_TB_FREE_SLOTS--;
  35. space = &OWL_MEM_SPACE_TB[i];
  36. break;
  37. }
  38. // no space left on the table
  39. if (space == NULL)
  40. OWL_MEM_SPACE_GLOB_ERR = OWL_MEM_SPACE_TB_FULL;
  41. // return whatever happened
  42. end:
  43. return space;
  44. }
  45. // free the memory used by the structure
  46. owl_umax owl_free_mem_space(owl_mem_space * space)
  47. {
  48. // check if the pointer is valid
  49. owl_umax tmp = OWL_MEM_SPACE_TB_FREE_SLOTS;
  50. if (owl_check_mem_space_all(space, sizeof(owl_mem_space))) {
  51. // search for the equivalent structure in the space table to remove it
  52. // this function will enforce freeing a structure that was created
  53. // by the create function, it wont free anything else
  54. for (owl_umax i = 0; i < OWL_MEM_SPACE_TB_SIZE; i++)
  55. if (space -> ptr == OWL_MEM_SPACE_TB[i].ptr) {
  56. free(space -> ptr);
  57. OWL_MEM_SPACE_TB[i].ptr = NULL;
  58. OWL_MEM_SPACE_TB[i].size = 0;
  59. OWL_MEM_SPACE_TB_FREE_SLOTS++;
  60. break;
  61. }
  62. }
  63. // return free slot count
  64. if (OWL_MEM_SPACE_TB_FREE_SLOTS <= tmp && space != NULL)
  65. return 0;
  66. return OWL_MEM_SPACE_TB_FREE_SLOTS;
  67. }
  68. // print the information of the struct
  69. owl_umax owl_print_mem_space(owl_byte * src, owl_umax size)
  70. {
  71. // check params
  72. printf("OWL_MEM_SPACE info:\n");
  73. printf(" Struct info: %p, %d\n", src, size);
  74. if (!owl_check_mem_space_pointer(src, size))
  75. return 0;
  76. // otherwise print the structure info
  77. owl_mem_space * space = (void *) src;
  78. printf(" Struct info: %p, %d\n", space, sizeof(owl_mem_space));
  79. printf(" Array pointer: %p\n", space -> ptr);
  80. printf(" Array size (bytes): %llu\n", space -> size);
  81. return sizeof(owl_mem_space);
  82. }
  83. // to compare 2 mem_spaces
  84. owl_char owl_comp_mem_space(owl_byte * mem_space1, owl_byte * mem_space2)
  85. {
  86. owl_mem_space * msp1 = (void *) mem_space1,
  87. * msp2 = (void *) mem_space2;
  88. // check params
  89. if (!owl_check_mem_space_all(msp1, sizeof(owl_mem_space))
  90. || !owl_check_mem_space_all(msp2, sizeof(owl_mem_space)))
  91. return owl_comp_err;
  92. // else, do the comparison (sizes will be the way)
  93. if (msp1 -> size < msp2 -> size)
  94. return owl_comp_less;
  95. else if (msp1 -> size > msp2 -> size)
  96. return owl_comp_greater;
  97. return owl_comp_equal;
  98. }
  99. // function to print the error code stored in OWL_MEM_SPACE_GLOB_ERR
  100. void owl_print_mem_space_tb_err(void)
  101. {
  102. // evaluate the error
  103. switch(OWL_MEM_SPACE_GLOB_ERR) {
  104. case OWL_MEM_SPACE_NO_ERR:
  105. printf("OWL_MEM_SPACE_ERR: NO ERROR (%d)\n", OWL_MEM_SPACE_NO_ERR);
  106. break;
  107. case OWL_MEM_SPACE_TB_FULL:
  108. printf("OWL_MEM_SPACE_ERR: SPACE TABLE FULL (%d)\n", OWL_MEM_SPACE_TB_FULL);
  109. break;
  110. case OWL_MEM_SPACE_CALLOC_FAIL:
  111. printf("OWL_MEM_SPACE_ERR: CALLOC FAIL (%d)\n", OWL_MEM_SPACE_CALLOC_FAIL);
  112. break;
  113. }
  114. return;
  115. }