common.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #include <sys/types.h>
  19. #ifdef USE_LARGEMEM
  20. #include <sys/mman.h>
  21. #endif
  22. #include <ctype.h>
  23. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  24. #include <ostream>
  25. #else
  26. #include <ostream.h>
  27. #endif
  28. #include <iostream>
  29. using namespace std;
  30. #include "common.h"
  31. /* globals */
  32. statsRecorder *stats = NULL;
  33. userOptions *opt = NULL;
  34. struct Cell_head *region = NULL;
  35. dimension_type nrows = 0, ncols = 0;
  36. size_t
  37. parse_number(const char *s) {
  38. size_t n, mult=1;
  39. int len = strlen(s);
  40. if(isalpha(s[len-1])) {
  41. switch(s[len-1]) {
  42. case 'M':
  43. mult = 1 << 20;
  44. break;
  45. case 'K':
  46. mult = 1 << 10;
  47. break;
  48. default:
  49. cerr << "bad number format: " << s << endl;
  50. exit(-1);
  51. break;
  52. }
  53. /* s[len-1] = '\0'; not needed, as it will stop at first invalid char */
  54. }
  55. n = atol(s);
  56. return n * mult;
  57. }
  58. /* ---------------------------------------------------------------------- */
  59. /* is anybody using this?? DELETE ! */
  60. #ifdef USE_LARGEMEM
  61. void *LargeMemory::ptr[LM_HIST];
  62. size_t LargeMemory::len[LM_HIST];
  63. int LargeMemory::next = 0;
  64. #ifndef MAP_ANON
  65. #define MAP_ANON 0
  66. #endif
  67. #ifdef __alpha
  68. #undef MAP_FAILED
  69. #define MAP_FAILED (caddr_t)-1L
  70. #endif
  71. void *
  72. LargeMemory::alloc(size_t leng) {
  73. assert(next < LM_HIST);
  74. void *p = mmap(0, leng, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  75. if(p == MAP_FAILED) {
  76. perror("mmap");
  77. exit(1);
  78. }
  79. len[next] = leng;
  80. ptr[next] = p;
  81. next++;
  82. if(stats) {
  83. char buf[BUFSIZ], buf2[32];
  84. sprintf(buf, "allocated large memory: %s 0x%lX",
  85. formatNumber(buf2, leng), (unsigned long)p);
  86. stats->comment(buf);
  87. }
  88. return p;
  89. }
  90. void
  91. LargeMemory::free(void *p) {
  92. int z;
  93. int i;
  94. for(i=next-1; i>=0; i--) {
  95. if(ptr[i] == p) break;
  96. }
  97. assert(i<next && i>=0); /* must have been allocated before */
  98. #if (defined sun && defined sparc)
  99. z = munmap((caddr_t)p, len[i]);
  100. #else
  101. z = munmap(p, len[i]);
  102. #endif
  103. if(z < 0) {
  104. perror("munmap");
  105. }
  106. if(stats) {
  107. char buf[BUFSIZ], buf2[32];
  108. sprintf(buf, "freed large memory: %s 0x%lX",
  109. formatNumber(buf2, len[i]), (unsigned long)p);
  110. stats->comment(buf);
  111. }
  112. next--;
  113. if(next) {
  114. ptr[i] = ptr[next];
  115. len[i] = len[next];
  116. }
  117. }
  118. #endif /* USE_LARGEMEM */