api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef API_H
  14. #define API_H
  15. #include <assert.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <stdarg.h>
  20. /* WARNING: this declaration may conflict with some header files */
  21. #ifndef ssize_t
  22. typedef ptrdiff_t ssize_t;
  23. #endif
  24. /* Macros */
  25. #ifndef likely
  26. # define likely(x) __builtin_expect((!!(x)),1)
  27. #endif
  28. #ifndef unlikely
  29. # define unlikely(x) __builtin_expect((!!(x)),0)
  30. #endif
  31. #ifndef MIN
  32. #define MIN(a,b) \
  33. ({ __typeof__(a) _a = (a); \
  34. __typeof__(b) _b = (b); \
  35. _a < _b ? _a : _b; })
  36. #endif
  37. #ifndef MAX
  38. #define MAX(a,b) \
  39. ({ __typeof__(a) _a = (a); \
  40. __typeof__(b) _b = (b); \
  41. _a > _b ? _a : _b; })
  42. #endif
  43. #define SATURATED_ADD(a, b, limit) \
  44. ({ __typeof__(a) _a = (a); \
  45. __typeof__(b) _b = (b); \
  46. __typeof__(limit) _limit = (limit); \
  47. _b > _limit ? _limit : (_a > _limit - _b ? _limit : _a + _b); })
  48. #define SATURATED_SUB(a, b, limit) \
  49. ({ __typeof__(a) _a = (a); \
  50. __typeof__(b) _b = (b); \
  51. __typeof__(limit) _limit = (limit); \
  52. _a < _limit ? _limit : (_b > _a - _limit ? _limit : _a - _b); })
  53. #define SATURATED_P_ADD(ptr_a, b, limit) \
  54. ((__typeof__(ptr_a))SATURATED_ADD((uintptr_t)(ptr_a), (uintptr_t)(b), (uintptr_t)(limit)))
  55. #define SATURATED_P_SUB(ptr_a, b, limit) \
  56. ((__typeof__(ptr_a))SATURATED_SUB((uintptr_t)(ptr_a), (uintptr_t)(b), (uintptr_t)(limit)))
  57. #define IS_POWER_OF_2(x) \
  58. ({ assert((x) != 0); \
  59. (((x) & ((x) - 1)) == 0); })
  60. #define IS_ALIGNED(val, alignment) ((val) % (alignment) == 0)
  61. #define ALIGN_DOWN(val, alignment) ((val) - (val) % (alignment))
  62. #define ALIGN_UP(val, alignment) ALIGN_DOWN((val) + (alignment) - 1, alignment)
  63. #define IS_ALIGNED_PTR(val, alignment) IS_ALIGNED((uintptr_t)(val), alignment)
  64. #define ALIGN_DOWN_PTR(ptr, alignment) ((__typeof__(ptr))(ALIGN_DOWN((uintptr_t)(ptr), alignment)))
  65. #define ALIGN_UP_PTR(ptr, alignment) ((__typeof__(ptr))(ALIGN_UP((uintptr_t)(ptr), alignment)))
  66. /* Useful only when the alignment is a power of two, but when that's not known compile-time. */
  67. #define IS_ALIGNED_POW2(val, alignment) (((val) & ((alignment) - 1)) == 0)
  68. #define ALIGN_DOWN_POW2(val, alignment) \
  69. ((val) - ((val) & ((alignment) - 1))) // `~` doesn't work if `alignment` is of a smaller type
  70. // than `val` and unsigned.
  71. #define ALIGN_UP_POW2(val, alignment) ALIGN_DOWN_POW2((val) + (alignment) - 1, alignment)
  72. #define IS_ALIGNED_PTR_POW2(val, alignment) IS_ALIGNED_POW2((uintptr_t)(val), alignment)
  73. #define ALIGN_DOWN_PTR_POW2(ptr, alignment) ((__typeof__(ptr))(ALIGN_DOWN_POW2((uintptr_t)(ptr), \
  74. alignment)))
  75. #define ALIGN_UP_PTR_POW2(ptr, alignment) ((__typeof__(ptr))(ALIGN_UP_POW2((uintptr_t)(ptr), \
  76. alignment)))
  77. #define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
  78. #define IS_STATIC_ARRAY(a) (!SAME_TYPE(a, &*(a)))
  79. #define FORCE_STATIC_ARRAY(a) sizeof(int[IS_STATIC_ARRAY(a) - 1]) // evaluates to 0
  80. #ifndef ARRAY_SIZE
  81. #define ARRAY_SIZE(a) (FORCE_STATIC_ARRAY(a) + sizeof(a) / sizeof(a[0]))
  82. #endif
  83. #ifndef container_of
  84. /**
  85. * container_of - cast a member of a structure out to the containing structure
  86. * @ptr: the pointer to the member.
  87. * @type: the type of the container struct this is embedded in.
  88. * @member: the name of the member within the struct.
  89. *
  90. */
  91. # define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
  92. #endif
  93. #define __alloca __builtin_alloca
  94. #define XSTRINGIFY(x) STRINGIFY(x)
  95. #define STRINGIFY(x) #x
  96. #define __UNUSED(x) do { (void)(x); } while (0)
  97. #define static_strlen(str) (ARRAY_SIZE(str) - 1)
  98. /* Libc functions */
  99. /* Libc String functions string.h/stdlib.h */
  100. size_t strnlen (const char *str, size_t maxlen);
  101. size_t strlen (const char *str);
  102. int strcmp(const char* a, const char* b);
  103. long strtol (const char *s, char **endptr, int base);
  104. int atoi (const char *nptr);
  105. long int atol (const char *nptr);
  106. char * strchr (const char *s, int c_in);
  107. void * memcpy (void *dstpp, const void *srcpp, size_t len);
  108. void * memmove (void *dstpp, const void *srcpp, size_t len);
  109. void * memset (void *dstpp, int c, size_t len);
  110. int memcmp (const void *s1, const void *s2, size_t len);
  111. bool strendswith(const char* haystack, const char* needle);
  112. /* Libc memory allocation functions. stdlib.h. */
  113. void *malloc(size_t size);
  114. void free(void *ptr);
  115. void *calloc(size_t nmemb, size_t size);
  116. /* force failure if str is not a static string */
  117. #define force_literal_cstr(str) ("" str "")
  118. /* check if the var is exactly the same as the static string */
  119. #define strcmp_static(var, str) \
  120. (memcmp(var, \
  121. force_literal_cstr(str), \
  122. MIN(strlen(var) + 1, static_strlen(force_literal_cstr(str))) + 1))
  123. /* check if the var starts with the static string */
  124. #define strstartswith_static(var, str) \
  125. (!memcmp(var, force_literal_cstr(str), static_strlen(force_literal_cstr(str))))
  126. /* copy static string and return the address of the null end (null if the dest
  127. * is not large enough).*/
  128. #define strcpy_static(var, str, max) \
  129. (static_strlen(force_literal_cstr(str)) + 1 > (max) ? NULL : \
  130. memcpy((var), force_literal_cstr(str), static_strlen(force_literal_cstr(str)) + 1) + \
  131. static_strlen(force_literal_cstr(str)))
  132. /* Copy a fixed size array. */
  133. #define COPY_ARRAY(dst, src) \
  134. do { \
  135. /* Using pointers because otherwise the compiler would try to allocate \
  136. * memory for the fixed size arrays and complain about invalid \
  137. * initializers. \
  138. */ \
  139. __typeof__(src)* _s = &(src); \
  140. __typeof__(dst)* _d = &(dst); \
  141. \
  142. static_assert(SAME_TYPE((*_s)[0], (*_d)[0]), "types must match"); \
  143. static_assert(ARRAY_SIZE(*_s) == ARRAY_SIZE(*_d), "sizes must match"); \
  144. \
  145. memcpy(*_d, *_s, sizeof(*_d)); \
  146. } while (0)
  147. /* Libc printf functions. stdio.h/stdarg.h. */
  148. void fprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  149. const char * fmt, ...) __attribute__((format(printf, 4, 5)));
  150. void vfprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  151. const char * fmt, va_list ap) __attribute__((format(printf, 4, 0)));
  152. int snprintf (char * buf, size_t n, const char * fmt, ...) __attribute__((format(printf, 3, 4)));
  153. /* Miscelleneous */
  154. int inet_pton4 (const char *src, size_t len, void *dst);
  155. int inet_pton6 (const char *src, size_t len, void *dst);
  156. uint32_t __htonl (uint32_t x);
  157. uint32_t __ntohl (uint32_t x);
  158. uint16_t __htons (uint16_t x);
  159. uint16_t __ntohs (uint16_t x);
  160. extern const char * const * sys_errlist_internal;
  161. /* Graphene functions */
  162. int get_norm_path(const char* path, char* buf, size_t* size);
  163. int get_base_name(const char* path, char* buf, size_t* size);
  164. /* Loading configs / manifests */
  165. #include <list.h>
  166. struct config;
  167. DEFINE_LISTP(config);
  168. struct config_store {
  169. LISTP_TYPE(config) root;
  170. LISTP_TYPE(config) entries;
  171. void * raw_data;
  172. int raw_size;
  173. void * (*malloc) (size_t);
  174. void (*free) (void *);
  175. };
  176. int read_config (struct config_store * store, int (*filter) (const char *, int),
  177. const char ** errstring);
  178. int free_config (struct config_store * store);
  179. int copy_config (struct config_store * store, struct config_store * new_store);
  180. int write_config (void * file, int (*write) (void *, void *, int),
  181. struct config_store * store);
  182. ssize_t get_config (struct config_store * cfg, const char * key,
  183. char * val_buf, size_t buf_size);
  184. int get_config_entries (struct config_store * cfg, const char * key,
  185. char * key_buf, size_t key_bufsize);
  186. ssize_t get_config_entries_size (struct config_store * cfg, const char * key);
  187. int set_config (struct config_store * cfg, const char * key, const char * val);
  188. #define CONFIG_MAX 4096
  189. #define URI_PREFIX_SEPARATOR ":"
  190. #define URI_TYPE_DIR "dir"
  191. #define URI_TYPE_TCP "tcp"
  192. #define URI_TYPE_TCP_SRV "tcp.srv"
  193. #define URI_TYPE_UDP "udp"
  194. #define URI_TYPE_UDP_SRV "udp.srv"
  195. #define URI_TYPE_PIPE "pipe"
  196. #define URI_TYPE_PIPE_SRV "pipe.srv"
  197. #define URI_TYPE_DEV "dev"
  198. #define URI_TYPE_EVENTFD "eventfd"
  199. #define URI_TYPE_FILE "file"
  200. #define URI_PREFIX_DIR URI_TYPE_DIR URI_PREFIX_SEPARATOR
  201. #define URI_PREFIX_TCP URI_TYPE_TCP URI_PREFIX_SEPARATOR
  202. #define URI_PREFIX_TCP_SRV URI_TYPE_TCP_SRV URI_PREFIX_SEPARATOR
  203. #define URI_PREFIX_UDP URI_TYPE_UDP URI_PREFIX_SEPARATOR
  204. #define URI_PREFIX_UDP_SRV URI_TYPE_UDP_SRV URI_PREFIX_SEPARATOR
  205. #define URI_PREFIX_PIPE URI_TYPE_PIPE URI_PREFIX_SEPARATOR
  206. #define URI_PREFIX_PIPE_SRV URI_TYPE_PIPE_SRV URI_PREFIX_SEPARATOR
  207. #define URI_PREFIX_DEV URI_TYPE_DEV URI_PREFIX_SEPARATOR
  208. #define URI_PREFIX_EVENTFD URI_TYPE_EVENTFD URI_PREFIX_SEPARATOR
  209. #define URI_PREFIX_FILE URI_TYPE_FILE URI_PREFIX_SEPARATOR
  210. #define URI_PREFIX_FILE_LEN (static_strlen(URI_PREFIX_FILE))
  211. #endif /* API_H */