gc.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;;; gc.test --- test guile's garbage collection -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2004, 2006 Free Software Foundation, Inc.
  3. ;;;;
  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, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program 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 General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;;; As a special exception, the Free Software Foundation gives permission
  20. ;;;; for additional uses of the text contained in its release of GUILE.
  21. ;;;;
  22. ;;;; The exception is that, if you link the GUILE library with other files
  23. ;;;; to produce an executable, this does not by itself cause the
  24. ;;;; resulting executable to be covered by the GNU General Public License.
  25. ;;;; Your use of that executable is in no way restricted on account of
  26. ;;;; linking the GUILE library code into it.
  27. ;;;;
  28. ;;;; This exception does not however invalidate any other reasons why
  29. ;;;; the executable file might be covered by the GNU General Public License.
  30. ;;;;
  31. ;;;; This exception applies only to the code released by the
  32. ;;;; Free Software Foundation under the name GUILE. If you copy
  33. ;;;; code from other Free Software Foundation releases into a copy of
  34. ;;;; GUILE, as the General Public License permits, the exception does
  35. ;;;; not apply to the code that you add in this way. To avoid misleading
  36. ;;;; anyone as to the status of such modified files, you must delete
  37. ;;;; this exception notice from them.
  38. ;;;;
  39. ;;;; If you write modifications of your own for GUILE, it is your choice
  40. ;;;; whether to permit this exception to apply to your modifications.
  41. ;;;; If you do not wish that, delete this exception notice.
  42. (use-modules (ice-9 documentation))
  43. ;;;
  44. ;;; miscellaneous
  45. ;;;
  46. (define (documented? object)
  47. (not (not (object-documentation object))))
  48. ;; In guile 1.6.4 this test bombed, due to the record in h being collected
  49. ;; by the gc, but not removed from h, leaving "x" as a freed cell.
  50. ;; The usual correct result here is for x to be #f, but there's always a
  51. ;; chance gc will mark something used when it isn't, so we allow x to be a
  52. ;; record too.
  53. (pass-if "weak-values versus records"
  54. (let ((rec-type (make-record-type "foo" '()))
  55. (h (make-weak-value-hash-table 61)))
  56. (hash-set! h "foo" ((record-constructor rec-type)))
  57. (gc)
  58. (let ((x (hash-ref h "foo")))
  59. (or (not x)
  60. ((record-predicate rec-type) x)))))
  61. ;;;
  62. ;;;
  63. ;;;
  64. (with-test-prefix "gc"
  65. (pass-if "after-gc-hook gets called"
  66. (let* ((foo #f)
  67. (thunk (lambda () (set! foo #t))))
  68. (add-hook! after-gc-hook thunk)
  69. (gc)
  70. (remove-hook! after-gc-hook thunk)
  71. foo)))
  72. (with-test-prefix "scm_must_realloc"
  73. (define (malloced-steady thunk)
  74. (define old-malloced -1)
  75. (define new-malloced -1)
  76. (let more ((attempt 0))
  77. (if (> attempt 30)
  78. (begin
  79. (format #t "bytes-malloced kept changing: ~a ~a\n"
  80. old-malloced new-malloced)
  81. #f)
  82. (begin
  83. (set! old-malloced new-malloced)
  84. (gc)
  85. (thunk)
  86. (thunk)
  87. (thunk)
  88. (gc)
  89. (gc)
  90. (set! new-malloced (assoc-ref (gc-stats) 'bytes-malloced))
  91. (if (= old-malloced new-malloced)
  92. #t
  93. (more (1+ attempt)))))))
  94. ;; In guile 1.6.7 and earlier, scm_must_realloc didn't adjust
  95. ;; scm_mallocated when reducing the size of a block, so when high zeros on
  96. ;; a bignum were trimmed by scm_i_adjbig the mallocated count ended up too
  97. ;; high after gc.
  98. ;;
  99. (with-test-prefix "bignum string->number trim"
  100. (do ((i 64 (1+ i)))
  101. ((> i 80))
  102. (pass-if i
  103. (malloced-steady
  104. (lambda ()
  105. (string->number (string-append "1" (make-string i #\0)) 16)))))))