api.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef API_H
  16. #define API_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) ((a) < (b) ? (a) : (b))
  33. #endif
  34. #ifndef MAX
  35. # define MAX(a, b) ((a) > (b) ? (a) : (b))
  36. #endif
  37. #define ALIGN_DOWN_PTR(ptr, size) \
  38. ((__typeof__(ptr)) (((uintptr_t)(ptr)) & -(size)))
  39. #define ALIGN_UP_PTR(ptr, size) \
  40. ((__typeof__(ptr)) ALIGN_DOWN_PTR((uintptr_t)(ptr) + ((size) - 1), (size)))
  41. #ifndef container_of
  42. /**
  43. * container_of - cast a member of a structure out to the containing structure
  44. * @ptr: the pointer to the member.
  45. * @type: the type of the container struct this is embedded in.
  46. * @member: the name of the member within the struct.
  47. *
  48. */
  49. # define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
  50. #endif
  51. #define __alloca __builtin_alloca
  52. #define XSTRINGIFY(x) STRINGIFY(x)
  53. #define STRINGIFY(x) #x
  54. #define static_strlen(str) (sizeof(str) - 1)
  55. /* Turning off unused-parameter warning for keeping function signatures */
  56. #define __UNUSED(x) do { (void)(x); } while (0)
  57. /* Libc functions */
  58. /* Libc String functions string.h/stdlib.h */
  59. size_t strnlen (const char *str, size_t maxlen);
  60. size_t strlen (const char *str);
  61. long strtol (const char *s, char **endptr, int base);
  62. int atoi (const char *nptr);
  63. long int atol (const char *nptr);
  64. char * strchr (const char *s, int c_in);
  65. void * memcpy (void *dstpp, const void *srcpp, size_t len);
  66. void * memmove (void *dstpp, const void *srcpp, size_t len);
  67. void * memset (void *dstpp, int c, size_t len);
  68. int memcmp (const void *s1, const void *s2, size_t len);
  69. /* Libc memory allocation functions. stdlib.h. */
  70. void *malloc(size_t size);
  71. void free(void *ptr);
  72. void *calloc(size_t nmemb, size_t size);
  73. /* Some useful macro */
  74. /* force failure if str is not a static string */
  75. #define force_static(str) ("" str "")
  76. /* check if the var is exactly the same as the static string */
  77. #define strcmp_static(var, str) \
  78. (!memcmp((var), force_static(str), static_strlen(force_static(str)) + 1))
  79. /* check if the var starts with the static string */
  80. #define strpartcmp_static(var, str) \
  81. (!memcmp((var), force_static(str), static_strlen(force_static(str))))
  82. /* copy static string and return the address of the null end (null if the dest
  83. * is not large enough).*/
  84. #define strcpy_static(var, str, max) \
  85. (static_strlen(force_static(str)) + 1 > (max) ? NULL : \
  86. memcpy((var), force_static(str), static_strlen(force_static(str)) + 1) + \
  87. static_strlen(force_static(str)))
  88. /* Copy a fixed size array. */
  89. #define COPY_ARRAY(dst, src) \
  90. do { \
  91. /* Using pointers because otherwise the compiler would try to allocate \
  92. * memory for the fixed size arrays and complain about invalid \
  93. * initializers. \
  94. */ \
  95. __typeof__(src)* _s = &(src); \
  96. __typeof__(dst)* _d = &(dst); \
  97. \
  98. __typeof__((*_s)[0]) _s2[sizeof(*_s)/sizeof((*_s)[0])]; \
  99. __typeof__((*_d)[0]) _d2[sizeof(*_d)/sizeof((*_d)[0])]; \
  100. \
  101. /* Causes a compiler warning if the array types mismatch. */ \
  102. (void) (_s == _d); \
  103. \
  104. /* Causes a compiler warning if passed arrays are not fixed size \
  105. * arrays. \
  106. */ \
  107. (void) (_s == &_s2); \
  108. (void) (_d == &_d2); \
  109. \
  110. /* Double check sizes. */ \
  111. _Static_assert(sizeof(*_s) == sizeof(*_d), "sizes don't match"); \
  112. \
  113. memcpy(*_d, *_s, sizeof(*_d)); \
  114. } while (0)
  115. /* Libc printf functions. stdio.h/stdarg.h. */
  116. void fprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  117. const char * fmt, ...);
  118. void vfprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  119. const char * fmt, va_list *ap);
  120. int snprintf (char * buf, int n, const char * fmt, ...);
  121. /* Miscelleneous */
  122. int inet_pton4 (const char *src, int len, void *dst);
  123. int inet_pton6 (const char *src, int len, void *dst);
  124. uint32_t __htonl (uint32_t x);
  125. uint32_t __ntohl (uint32_t x);
  126. uint16_t __htons (uint16_t x);
  127. uint16_t __ntohs (uint16_t x);
  128. extern const char * const * sys_errlist_internal;
  129. /* Graphene functions */
  130. int get_norm_path (const char * path, char * buf, int offset, int size);
  131. int get_base_name (const char * path, char * buf, int size);
  132. /* Loading configs / manifests */
  133. #include <list.h>
  134. struct config;
  135. DEFINE_LISTP(config);
  136. struct config_store {
  137. LISTP_TYPE(config) root;
  138. LISTP_TYPE(config) entries;
  139. void * raw_data;
  140. int raw_size;
  141. void * (*malloc) (size_t);
  142. void (*free) (void *);
  143. };
  144. int read_config (struct config_store * store, int (*filter) (const char *, int),
  145. const char ** errstring);
  146. int free_config (struct config_store * store);
  147. int copy_config (struct config_store * store, struct config_store * new_store);
  148. int write_config (void * file, int (*write) (void *, void *, int),
  149. struct config_store * store);
  150. ssize_t get_config (struct config_store * cfg, const char * key,
  151. char * val_buf, size_t buf_size);
  152. int get_config_entries (struct config_store * cfg, const char * key,
  153. char * key_buf, size_t key_bufsize);
  154. ssize_t get_config_entries_size (struct config_store * cfg, const char * key);
  155. int set_config (struct config_store * cfg, const char * key, const char * val);
  156. #define CONFIG_MAX 4096
  157. #endif /* API_H */