api.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdarg.h>
  19. /* WARNING: this declaration may conflict with some header files */
  20. #ifndef ssize_t
  21. typedef ptrdiff_t ssize_t;
  22. #endif
  23. /* Macros */
  24. #ifndef likely
  25. # define likely(x) __builtin_expect((!!(x)),1)
  26. #endif
  27. #ifndef unlikely
  28. # define unlikely(x) __builtin_expect((!!(x)),0)
  29. #endif
  30. #ifndef MIN
  31. #define MIN(a,b) \
  32. ({ __typeof__(a) _a = (a); \
  33. __typeof__(b) _b = (b); \
  34. _a < _b ? _a : _b; })
  35. #endif
  36. #ifndef MAX
  37. #define MAX(a,b) \
  38. ({ __typeof__(a) _a = (a); \
  39. __typeof__(b) _b = (b); \
  40. _a > _b ? _a : _b; })
  41. #endif
  42. #define ALIGN_DOWN_PTR(ptr, size) \
  43. ((__typeof__(ptr)) (((uintptr_t)(ptr)) & -(size)))
  44. #define ALIGN_UP_PTR(ptr, size) \
  45. ((__typeof__(ptr)) ALIGN_DOWN_PTR((uintptr_t)(ptr) + ((size) - 1), (size)))
  46. #ifndef container_of
  47. /**
  48. * container_of - cast a member of a structure out to the containing structure
  49. * @ptr: the pointer to the member.
  50. * @type: the type of the container struct this is embedded in.
  51. * @member: the name of the member within the struct.
  52. *
  53. */
  54. # define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
  55. #endif
  56. #define __alloca __builtin_alloca
  57. #define XSTRINGIFY(x) STRINGIFY(x)
  58. #define STRINGIFY(x) #x
  59. #define static_strlen(str) (sizeof(str) - 1)
  60. /* Turning off unused-parameter warning for keeping function signatures */
  61. #define __UNUSED(x) do { (void)(x); } while (0)
  62. /* Libc functions */
  63. /* Libc String functions string.h/stdlib.h */
  64. size_t strnlen (const char *str, size_t maxlen);
  65. size_t strlen (const char *str);
  66. long strtol (const char *s, char **endptr, int base);
  67. int atoi (const char *nptr);
  68. long int atol (const char *nptr);
  69. char * strchr (const char *s, int c_in);
  70. void * memcpy (void *dstpp, const void *srcpp, size_t len);
  71. void * memmove (void *dstpp, const void *srcpp, size_t len);
  72. void * memset (void *dstpp, int c, size_t len);
  73. int memcmp (const void *s1, const void *s2, size_t len);
  74. bool strendswith(const char* haystack, const char* needle);
  75. /* Libc memory allocation functions. stdlib.h. */
  76. void *malloc(size_t size);
  77. void free(void *ptr);
  78. void *calloc(size_t nmemb, size_t size);
  79. /* Some useful macro */
  80. /* force failure if str is not a static string */
  81. #define force_static(str) ("" str "")
  82. /* check if the var is exactly the same as the static string */
  83. #define strcmp_static(var, str) \
  84. (!memcmp((var), force_static(str), static_strlen(force_static(str)) + 1))
  85. /* check if the var starts with the static string */
  86. #define strpartcmp_static(var, str) \
  87. (!memcmp((var), force_static(str), static_strlen(force_static(str))))
  88. /* copy static string and return the address of the null end (null if the dest
  89. * is not large enough).*/
  90. #define strcpy_static(var, str, max) \
  91. (static_strlen(force_static(str)) + 1 > (max) ? NULL : \
  92. memcpy((var), force_static(str), static_strlen(force_static(str)) + 1) + \
  93. static_strlen(force_static(str)))
  94. /* Copy a fixed size array. */
  95. #define COPY_ARRAY(dst, src) \
  96. do { \
  97. /* Using pointers because otherwise the compiler would try to allocate \
  98. * memory for the fixed size arrays and complain about invalid \
  99. * initializers. \
  100. */ \
  101. __typeof__(src)* _s = &(src); \
  102. __typeof__(dst)* _d = &(dst); \
  103. \
  104. __typeof__((*_s)[0]) _s2[sizeof(*_s)/sizeof((*_s)[0])]; \
  105. __typeof__((*_d)[0]) _d2[sizeof(*_d)/sizeof((*_d)[0])]; \
  106. \
  107. /* Causes a compiler warning if the array types mismatch. */ \
  108. (void) (_s == _d); \
  109. \
  110. /* Causes a compiler warning if passed arrays are not fixed size \
  111. * arrays. \
  112. */ \
  113. (void) (_s == &_s2); \
  114. (void) (_d == &_d2); \
  115. \
  116. /* Double check sizes. */ \
  117. _Static_assert(sizeof(*_s) == sizeof(*_d), "sizes don't match"); \
  118. \
  119. memcpy(*_d, *_s, sizeof(*_d)); \
  120. } while (0)
  121. /* Libc printf functions. stdio.h/stdarg.h. */
  122. void fprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  123. const char * fmt, ...) __attribute__((format(printf, 4, 5)));
  124. void vfprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  125. const char * fmt, va_list ap) __attribute__((format(printf, 4, 0)));
  126. int snprintf (char * buf, int n, const char * fmt, ...) __attribute__((format(printf, 3, 4)));
  127. /* Miscelleneous */
  128. int inet_pton4 (const char *src, size_t len, void *dst);
  129. int inet_pton6 (const char *src, size_t len, void *dst);
  130. uint32_t __htonl (uint32_t x);
  131. uint32_t __ntohl (uint32_t x);
  132. uint16_t __htons (uint16_t x);
  133. uint16_t __ntohs (uint16_t x);
  134. extern const char * const * sys_errlist_internal;
  135. /* Graphene functions */
  136. int get_norm_path(const char* path, char* buf, size_t* size);
  137. int get_base_name(const char* path, char* buf, size_t* size);
  138. /* Loading configs / manifests */
  139. #include <list.h>
  140. struct config;
  141. DEFINE_LISTP(config);
  142. struct config_store {
  143. LISTP_TYPE(config) root;
  144. LISTP_TYPE(config) entries;
  145. void * raw_data;
  146. int raw_size;
  147. void * (*malloc) (size_t);
  148. void (*free) (void *);
  149. };
  150. int read_config (struct config_store * store, int (*filter) (const char *, int),
  151. const char ** errstring);
  152. int free_config (struct config_store * store);
  153. int copy_config (struct config_store * store, struct config_store * new_store);
  154. int write_config (void * file, int (*write) (void *, void *, int),
  155. struct config_store * store);
  156. ssize_t get_config (struct config_store * cfg, const char * key,
  157. char * val_buf, size_t buf_size);
  158. int get_config_entries (struct config_store * cfg, const char * key,
  159. char * key_buf, size_t key_bufsize);
  160. ssize_t get_config_entries_size (struct config_store * cfg, const char * key);
  161. int set_config (struct config_store * cfg, const char * key, const char * val);
  162. #define CONFIG_MAX 4096
  163. #endif /* API_H */