MEM_SPACE.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef MEM_SPACE_HEADER
  2. #define MEM_SPACE_HEADER
  3. // this header needs
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "BASIC_TYPES.h"
  7. // first complex data type, mem_space a byte array with a byte length
  8. // and a boolean to check if the memory was manually allocated
  9. // all thanks to no check_if_ptr_malloc() function-like available >:[
  10. typedef struct
  11. {
  12. byte * ptr;
  13. umax size;
  14. } mem_space;
  15. // size of the structure
  16. #define MEM_SPACE_SIZE sizeof(mem_space)
  17. // error codes for the memory allocator
  18. typedef enum
  19. {
  20. MEM_SPACE_NO_ERR, // nothing has happened
  21. MEM_SPACE_TB_FULL, // the table is full
  22. MEM_SPACE_CALLOC_FAIL, // a call to calloc() failed
  23. } mem_space_err;
  24. // var_mem_space enum
  25. // the second variable types enum
  26. typedef enum
  27. {
  28. VAR_MEM_SPACE = VAR_BASIC_TYPES + 1,
  29. VAR_MEM_SPACE_ERR,
  30. } var_mem_space;
  31. // size of the pointer table
  32. #define MEM_SPACE_TB_LENGTH 50
  33. umax MEM_SPACE_TB_FREE_SPACE = MEM_SPACE_TB_LENGTH;
  34. // global alloc error
  35. mem_space_err MEM_SPACE_GLOB_ERR = MEM_SPACE_NO_ERR;
  36. // memory space table
  37. // table defined to keep track of all variables based on mem_space
  38. // (everything that will come after this header basically)
  39. // All memory allocated pointers will be stored in this table.
  40. // All pointers in this table will be NULL at first.
  41. // This table helps (at least helps me) to avoid memory leaks
  42. // and to avoid making so much memory allocations at once.
  43. // also the name it has sounds very interesting and advanced
  44. mem_space MEM_SPACE_TB[MEM_SPACE_TB_LENGTH] = {{NULL, 0}};
  45. // basic functions
  46. mem_space * create_mem_space(umax size);
  47. umax free_mem_space(mem_space * space);
  48. umax print_mem_space(byte * src, umax size);
  49. // other functions
  50. void print_mem_space_tb_err(void);
  51. #include "MEM_SPACE.c"
  52. #endif // MEM_SPACE_HEADER