mem_space.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef mem_space_h
  2. #define mem_space_h
  3. // all dependencies
  4. #include "basic_types.h"
  5. #include <stdlib.h>
  6. // first complex data type, mem_space,
  7. // a byte array with a length
  8. typedef struct
  9. {
  10. owl_byte * ptr;
  11. owl_umax size;
  12. } owl_mem_space;
  13. // error codes for the memory allocator
  14. typedef enum
  15. {
  16. OWL_MEM_SPACE_NO_ERR, // nothing has happened
  17. OWL_MEM_SPACE_TB_FULL, // the table is full
  18. OWL_MEM_SPACE_CALLOC_FAIL, // a call to calloc() failed
  19. } owl_mem_space_err;
  20. typedef enum
  21. {
  22. OWL_TYPE_MEM_SPACE = OWL_TYPE_BOOL + 1,
  23. OWL_TYPE_MEM_SPACE_ERR,
  24. } owl_type_mem_space_h;
  25. // size of the pointer table
  26. #define OWL_MEM_SPACE_TB_SIZE 50
  27. owl_umax OWL_MEM_SPACE_TB_FREE_SLOTS = OWL_MEM_SPACE_TB_SIZE;
  28. // global alloc error
  29. owl_mem_space_err OWL_MEM_SPACE_GLOB_ERR = OWL_MEM_SPACE_NO_ERR;
  30. // memory space table
  31. // table defined to keep track of all variables based on mem_space
  32. // (everything that will come after this header basically)
  33. // All memory allocated pointers will be stored in this table.
  34. // All pointers in this table will be NULL at first.
  35. // This table helps (at least helps me) to avoid memory leaks
  36. // and to avoid making so much memory allocations at once.
  37. // also the name it has sounds very interesting and advanced
  38. owl_mem_space OWL_MEM_SPACE_TB[OWL_MEM_SPACE_TB_SIZE] = {{NULL, 0}};
  39. // basic functions
  40. owl_bool owl_check_mem_space_pointer(owl_byte * src, owl_umax size);
  41. owl_bool owl_check_mem_space_members(owl_byte * ptr, owl_umax size);
  42. owl_bool owl_check_mem_space_all(owl_mem_space * src, owl_umax size);
  43. owl_mem_space * owl_create_mem_space(owl_umax size);
  44. owl_umax owl_free_mem_space(owl_mem_space * space);
  45. owl_umax owl_print_mem_space(owl_byte * src, owl_umax size);
  46. owl_char owl_comp_mem_space(owl_byte * mem_space1, owl_byte * mem_space2);
  47. // other functions
  48. void owl_print_mem_space_tb_err(void);
  49. #include "mem_space.c"
  50. #endif // mem_space_h