utils.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Catalyst PCIBX32 PCI Extender control utility
  3. Copyright (c) 2006,2007 Michael Buesch <mb@bu3sch.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "utils.h"
  18. #include "pcibx.h"
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <time.h>
  24. #include <sys/time.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. void udelay(unsigned int usecs)
  28. {
  29. int err;
  30. struct timeval time, deadline;
  31. err = gettimeofday(&deadline, NULL);
  32. if (err)
  33. goto error;
  34. deadline.tv_usec += usecs;
  35. if (deadline.tv_usec >= 1000000) {
  36. deadline.tv_sec++;
  37. deadline.tv_usec -= 1000000;
  38. }
  39. while (1) {
  40. err = gettimeofday(&time, NULL);
  41. if (err)
  42. goto error;
  43. if (time.tv_sec < deadline.tv_sec)
  44. continue;
  45. if (time.tv_usec >= deadline.tv_usec)
  46. break;
  47. }
  48. return;
  49. error:
  50. prerror("gettimeofday() failed with: %s\n",
  51. strerror(errno));
  52. }
  53. void msleep(unsigned int msecs)
  54. {
  55. int err;
  56. struct timespec time;
  57. time.tv_sec = 0;
  58. while (msecs >= 1000) {
  59. time.tv_sec++;
  60. msecs -= 1000;
  61. }
  62. time.tv_nsec = msecs;
  63. time.tv_nsec *= 1000000;
  64. do {
  65. err = nanosleep(&time, &time);
  66. } while (err && errno == EINTR);
  67. if (err) {
  68. prerror("nanosleep() failed with: %s\n",
  69. strerror(errno));
  70. }
  71. }
  72. int prinfo(const char *fmt, ...)
  73. {
  74. int ret;
  75. va_list va;
  76. va_start(va, fmt);
  77. ret = vfprintf(stdout, fmt, va);
  78. va_end(va);
  79. return ret;
  80. }
  81. int prerror(const char *fmt, ...)
  82. {
  83. int ret;
  84. va_list va;
  85. va_start(va, fmt);
  86. ret = vfprintf(stderr, fmt, va);
  87. va_end(va);
  88. return ret;
  89. }
  90. void internal_error(const char *message)
  91. {
  92. prerror("Internal programming error: %s\n", message);
  93. exit(1);
  94. }
  95. static void oom(void)
  96. {
  97. prerror("ERROR: Out of memory!\n"
  98. "Virtual memory exhausted. "
  99. "Please close some applications, "
  100. "add more RAM or SWAP space.\n");
  101. exit(1);
  102. }
  103. void * malloce(size_t size)
  104. {
  105. void *ret;
  106. ret = malloc(size);
  107. if (!ret)
  108. oom();
  109. return ret;
  110. }
  111. void * realloce(void *ptr, size_t newsize)
  112. {
  113. void *ret;
  114. ret = realloc(ptr, newsize);
  115. if (!ret)
  116. oom();
  117. return ret;
  118. }