mem_space.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // var_mem_space enum
  21. // the second variable types enum
  22. typedef enum
  23. {
  24. OWL_TYPE_MEM_SPACE = OWL_TYPE_BOOL + 1,
  25. OWL_TYPE_MEM_SPACE_ERR,
  26. } owl_type_mem_space_h;
  27. // size of the pointer table
  28. #define OWL_MEM_SPACE_TB_SIZE 50
  29. owl_umax OWL_MEM_SPACE_TB_FREE_SLOTS = OWL_MEM_SPACE_TB_SIZE;
  30. // global alloc error
  31. owl_mem_space_err OWL_MEM_SPACE_GLOB_ERR = OWL_MEM_SPACE_NO_ERR;
  32. // memory space table
  33. // table defined to keep track of all variables based on mem_space
  34. // (everything that will come after this header basically)
  35. // All memory allocated pointers will be stored in this table.
  36. // All pointers in this table will be NULL at first.
  37. // This table helps (at least helps me) to avoid memory leaks
  38. // and to avoid making so much memory allocations at once.
  39. // also the name it has sounds very interesting and advanced
  40. owl_mem_space OWL_MEM_SPACE_TB[OWL_MEM_SPACE_TB_SIZE] = {{NULL, 0}};
  41. // basic functions
  42. owl_bool owl_check_mem_space_pointer(owl_byte * src, owl_umax size);
  43. owl_bool owl_check_mem_space_members(owl_byte * ptr, owl_umax size);
  44. owl_bool owl_check_mem_space_all(owl_mem_space * src, owl_umax size);
  45. owl_mem_space * owl_create_mem_space(owl_umax size);
  46. owl_umax owl_free_mem_space(owl_mem_space * space);
  47. owl_umax owl_print_mem_space(owl_byte * src, owl_umax size);
  48. owl_bool owl_comp_mem_space(owl_byte * mem_space1, owl_byte * mem_space2);
  49. // other functions
  50. void owl_print_mem_space_tb_err(void);
  51. #include "mem_space.c"
  52. #endif // mem_space_h