defs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * defs.h
  3. *
  4. * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __MONOLITHIUM_DEFS_H__
  20. #define __MONOLITHIUM_DEFS_H__
  21. #include <limits.h>
  22. #include <stdarg.h>
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #define FALSE 0
  26. #define TRUE (!FALSE)
  27. #define VARIABLE_SIZE 0
  28. /* Monolithium-specific Type Limits */
  29. #define BYTE_MIN 0
  30. #define BYTE_MAX 0xFF
  31. #define WORD_MIN 0
  32. #define WORD_MAX 0xFFFF
  33. #define DWORD_MIN 0
  34. #define DWORD_MAX 0xFFFFFFFF
  35. #define QWORD_MIN 0ULL
  36. #define QWORD_MAX 0xFFFFFFFFFFFFFFFFULL
  37. /* Monolithium Error Codes */
  38. #define ERR_SUCCESS 0x00000000
  39. #define ERR_NOTFOUND 0xE0000001
  40. #define ERR_FORBIDDEN 0xE0000002
  41. #define ERR_INVALID 0xE0000003
  42. #define ERR_EXISTS 0xE0000004
  43. #define ERR_NOMEMORY 0xE0000005
  44. #define ERR_HARDWARE 0xE0000006
  45. #define ERR_BUSY 0xE0000007
  46. #define ERR_WRITEPROT 0xE0000008
  47. #define ERR_NOSYSCALL 0xE0000009
  48. #define ERR_TIMEOUT 0xE000000A
  49. #define ERR_BADPTR 0xE000000B
  50. #define ERR_CANCELED 0xE000000C
  51. #define ERR_ISDIR 0xE000000D
  52. #define ERR_ISNOTDIR 0xE000000E
  53. #define ERR_DISKFULL 0xE000000F
  54. #define ERR_BEYOND 0xE0000010
  55. #define ERR_SMALLBUF 0xE0000011
  56. #define ERR_NOMORE 0xE0000012
  57. #define MAX_ERR 0xE0000013
  58. #define MAX_PATH 16384
  59. #define NO_TIMEOUT 0xFFFFFFFF
  60. #define UNUSED_PARAMETER(x) (x)=(x)
  61. #define OFFSET_OF(type, field) ((uintptr_t)(&((type*)NULL)->field))
  62. #define CONTAINER_OF(ptr, type, field) ((type*)((uintptr_t)(ptr) - OFFSET_OF(type, field)))
  63. /* Monolithium-specific Types */
  64. typedef uint8_t bool_t;
  65. typedef uint8_t byte_t;
  66. typedef uint16_t word_t;
  67. typedef uint32_t dword_t;
  68. typedef uint64_t qword_t;
  69. /* System call return value */
  70. typedef dword_t sysret_t;
  71. typedef dword_t timeout_t;
  72. static inline void set_bit(dword_t *bitfield, dword_t bit)
  73. {
  74. bitfield[bit >> 5] |= 1 << (bit & 0x1F);
  75. }
  76. static inline void clear_bit(dword_t *bitfield, dword_t bit)
  77. {
  78. bitfield[bit >> 5] &= ~(1 << (bit & 0x1F));
  79. }
  80. static inline bool_t test_bit(dword_t *bitfield, dword_t bit)
  81. {
  82. return (bitfield[bit >> 5] & (1 << (bit & 0x1F))) ? TRUE : FALSE;
  83. }
  84. static inline void push_to_stack(uintptr_t *stack, uintptr_t value)
  85. {
  86. *stack -= sizeof(uintptr_t);
  87. *((uintptr_t*)(*stack)) = value;
  88. }
  89. static inline uintptr_t pop_from_stack(uintptr_t *stack)
  90. {
  91. uintptr_t value = *((uintptr_t*)(*stack));
  92. *stack += sizeof(uintptr_t);
  93. return value;
  94. }
  95. static inline const char *get_error_string(dword_t err_num)
  96. {
  97. static const char *error_strings[] = {
  98. "ERR_SUCCESS",
  99. "ERR_NOTFOUND",
  100. "ERR_FORBIDDEN",
  101. "ERR_INVALID",
  102. "ERR_EXISTS",
  103. "ERR_NOMEMORY",
  104. "ERR_HARDWARE",
  105. "ERR_BUSY",
  106. "ERR_NOMEDIA",
  107. "ERR_NOTRESP",
  108. "ERR_WRITEPROT",
  109. "ERR_NOSYSCALL",
  110. "ERR_TIMEOUT",
  111. "ERR_BADPTR",
  112. "ERR_CANCELED",
  113. "ERR_ISDIR",
  114. "ERR_ISNOTDIR",
  115. "ERR_DISKFULL",
  116. "ERR_MEDIACHG"
  117. };
  118. if (err_num == 0)
  119. {
  120. return error_strings[0];
  121. }
  122. else if (err_num >= 0xE0000000)
  123. {
  124. if (err_num < MAX_ERR) return error_strings[err_num - 0xE0000000];
  125. }
  126. return NULL;
  127. }
  128. #endif