utils.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * utils - misc libubox utility functions
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "utils.h"
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
  23. for (_addr = (_first_addr), _len = (_first_len); \
  24. _addr; \
  25. _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
  26. void *__calloc_a(size_t len, ...)
  27. {
  28. va_list ap, ap1;
  29. void *ret;
  30. void **cur_addr;
  31. size_t cur_len;
  32. int alloc_len = 0;
  33. char *ptr;
  34. va_start(ap, len);
  35. va_copy(ap1, ap);
  36. foreach_arg(ap1, cur_addr, cur_len, &ret, len)
  37. alloc_len += cur_len;
  38. va_end(ap1);
  39. ptr = calloc(1, alloc_len);
  40. if (!ptr)
  41. return NULL;
  42. alloc_len = 0;
  43. foreach_arg(ap, cur_addr, cur_len, &ret, len) {
  44. *cur_addr = &ptr[alloc_len];
  45. alloc_len += cur_len;
  46. }
  47. va_end(ap);
  48. return ret;
  49. }
  50. #ifdef __APPLE__
  51. #include <mach/mach_host.h> /* host_get_clock_service() */
  52. #include <mach/mach_port.h> /* mach_port_deallocate() */
  53. #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
  54. #include <mach/clock.h> /* clock_get_time() */
  55. static clock_serv_t clock_realtime;
  56. static clock_serv_t clock_monotonic;
  57. static void __constructor clock_name_init(void)
  58. {
  59. mach_port_t host_self = mach_host_self();
  60. host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
  61. host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
  62. }
  63. static void __destructor clock_name_dealloc(void)
  64. {
  65. mach_port_t self = mach_task_self();
  66. mach_port_deallocate(self, clock_realtime);
  67. mach_port_deallocate(self, clock_monotonic);
  68. }
  69. int clock_gettime(int type, struct timespec *tv)
  70. {
  71. int retval = -1;
  72. mach_timespec_t mts;
  73. switch (type) {
  74. case CLOCK_REALTIME:
  75. retval = clock_get_time(clock_realtime, &mts);
  76. break;
  77. case CLOCK_MONOTONIC:
  78. retval = clock_get_time(clock_monotonic, &mts);
  79. break;
  80. default:
  81. goto out;
  82. }
  83. tv->tv_sec = mts.tv_sec;
  84. tv->tv_nsec = mts.tv_nsec;
  85. out:
  86. return retval;
  87. }
  88. #endif