api.h 7.7 KB

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