common.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "common.h"
  30. /* globals */
  31. statsRecorder *stats = NULL;
  32. userOptions *opt = NULL;
  33. struct Cell_head *region = NULL;
  34. dimension_type nrows = 0, ncols = 0;
  35. size_t
  36. parse_number(const char *s) {
  37. size_t n, mult=1;
  38. int len = strlen(s);
  39. if(isalpha(s[len-1])) {
  40. switch(s[len-1]) {
  41. case 'M':
  42. mult = 1 << 20;
  43. break;
  44. case 'K':
  45. mult = 1 << 10;
  46. break;
  47. default:
  48. cerr << "bad number format: " << s << endl;
  49. exit(-1);
  50. break;
  51. }
  52. /* s[len-1] = '\0'; not needed, as it will stop at first invalid char */
  53. }
  54. n = atol(s);
  55. return n * mult;
  56. }
  57. /* ---------------------------------------------------------------------- */
  58. /* is anybody using this?? DELETE ! */
  59. #ifdef USE_LARGEMEM
  60. void *LargeMemory::ptr[LM_HIST];
  61. size_t LargeMemory::len[LM_HIST];
  62. int LargeMemory::next = 0;
  63. #ifndef MAP_ANON
  64. #define MAP_ANON 0
  65. #endif
  66. #ifdef __alpha
  67. #undef MAP_FAILED
  68. #define MAP_FAILED (caddr_t)-1L
  69. #endif
  70. void *
  71. LargeMemory::alloc(size_t leng) {
  72. assert(next < LM_HIST);
  73. void *p = mmap(0, leng, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  74. if(p == MAP_FAILED) {
  75. perror("mmap");
  76. exit(1);
  77. }
  78. len[next] = leng;
  79. ptr[next] = p;
  80. next++;
  81. if(stats) {
  82. char buf[BUFSIZ], buf2[32];
  83. sprintf(buf, "allocated large memory: %s 0x%lX",
  84. formatNumber(buf2, leng), (unsigned long)p);
  85. stats->comment(buf);
  86. }
  87. return p;
  88. }
  89. void
  90. LargeMemory::free(void *p) {
  91. int z;
  92. int i;
  93. for(i=next-1; i>=0; i--) {
  94. if(ptr[i] == p) break;
  95. }
  96. assert(i<next && i>=0); /* must have been allocated before */
  97. #if (defined sun && defined sparc)
  98. z = munmap((caddr_t)p, len[i]);
  99. #else
  100. z = munmap(p, len[i]);
  101. #endif
  102. if(z < 0) {
  103. perror("munmap");
  104. }
  105. if(stats) {
  106. char buf[BUFSIZ], buf2[32];
  107. sprintf(buf, "freed large memory: %s 0x%lX",
  108. formatNumber(buf2, len[i]), (unsigned long)p);
  109. stats->comment(buf);
  110. }
  111. next--;
  112. if(next) {
  113. ptr[i] = ptr[next];
  114. len[i] = len[next];
  115. }
  116. }
  117. #endif /* USE_LARGEMEM */