pcr_interface.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. # include "private/gc_priv.h"
  14. # ifdef PCR
  15. /*
  16. * We wrap all of the allocator functions to avoid questions of
  17. * compatibility between the prototyped and nonprototyped versions of the f
  18. */
  19. # include "config/PCR_StdTypes.h"
  20. # include "mm/PCR_MM.h"
  21. # include <errno.h>
  22. # define MY_MAGIC 17L
  23. # define MY_DEBUGMAGIC 42L
  24. void * GC_AllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
  25. {
  26. if (ptrFree) {
  27. void * result = (void *)GC_malloc_atomic(size);
  28. if (clear && result != 0) BZERO(result, size);
  29. return(result);
  30. } else {
  31. return((void *)GC_malloc(size));
  32. }
  33. }
  34. void * GC_DebugAllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
  35. {
  36. if (ptrFree) {
  37. void * result = (void *)GC_debug_malloc_atomic(size, __FILE__,
  38. __LINE__);
  39. if (clear && result != 0) BZERO(result, size);
  40. return(result);
  41. } else {
  42. return((void *)GC_debug_malloc(size, __FILE__, __LINE__));
  43. }
  44. }
  45. # define GC_ReallocProc GC_realloc
  46. void * GC_DebugReallocProc(void * old_object, size_t new_size_in_bytes)
  47. {
  48. return(GC_debug_realloc(old_object, new_size_in_bytes, __FILE__, __LINE__));
  49. }
  50. # define GC_FreeProc GC_free
  51. # define GC_DebugFreeProc GC_debug_free
  52. typedef struct {
  53. PCR_ERes (*ed_proc)(void *p, size_t size, PCR_Any data);
  54. GC_bool ed_pointerfree;
  55. PCR_ERes ed_fail_code;
  56. PCR_Any ed_client_data;
  57. } enumerate_data;
  58. void GC_enumerate_block(struct hblk *h, enumerate_data * ed)
  59. {
  60. hdr * hhdr;
  61. size_t sz;
  62. ptr_t p;
  63. ptr_t lim;
  64. word descr;
  65. # if !defined(CPPCHECK)
  66. # error This code was updated without testing.
  67. # error and its precursor was clearly broken.
  68. # endif
  69. hhdr = HDR(h);
  70. descr = hhdr -> hb_descr;
  71. sz = (size_t)hhdr->hb_sz;
  72. if (descr != 0 && ed -> ed_pointerfree
  73. || descr == 0 && !(ed -> ed_pointerfree)) return;
  74. lim = (ptr_t)(h+1) - sz;
  75. p = (ptr_t)h;
  76. do {
  77. if (PCR_ERes_IsErr(ed -> ed_fail_code)) return;
  78. ed -> ed_fail_code =
  79. (*(ed -> ed_proc))(p, sz, ed -> ed_client_data);
  80. p+= sz;
  81. } while ((word)p <= (word)lim);
  82. }
  83. struct PCR_MM_ProcsRep * GC_old_allocator = 0;
  84. PCR_ERes GC_EnumerateProc(
  85. PCR_Bool ptrFree,
  86. PCR_ERes (*proc)(void *p, size_t size, PCR_Any data),
  87. PCR_Any data
  88. )
  89. {
  90. enumerate_data ed;
  91. ed.ed_proc = proc;
  92. ed.ed_pointerfree = ptrFree;
  93. ed.ed_fail_code = PCR_ERes_okay;
  94. ed.ed_client_data = data;
  95. GC_apply_to_all_blocks(GC_enumerate_block, &ed);
  96. if (ed.ed_fail_code != PCR_ERes_okay) {
  97. return(ed.ed_fail_code);
  98. } else {
  99. /* Also enumerate objects allocated by my predecessors */
  100. return((*(GC_old_allocator->mmp_enumerate))(ptrFree, proc, data));
  101. }
  102. }
  103. void GC_DummyFreeProc(void *p) {}
  104. void GC_DummyShutdownProc(void) {}
  105. struct PCR_MM_ProcsRep GC_Rep = {
  106. MY_MAGIC,
  107. GC_AllocProc,
  108. GC_ReallocProc,
  109. GC_DummyFreeProc, /* mmp_free */
  110. GC_FreeProc, /* mmp_unsafeFree */
  111. GC_EnumerateProc,
  112. GC_DummyShutdownProc /* mmp_shutdown */
  113. };
  114. struct PCR_MM_ProcsRep GC_DebugRep = {
  115. MY_DEBUGMAGIC,
  116. GC_DebugAllocProc,
  117. GC_DebugReallocProc,
  118. GC_DummyFreeProc, /* mmp_free */
  119. GC_DebugFreeProc, /* mmp_unsafeFree */
  120. GC_EnumerateProc,
  121. GC_DummyShutdownProc /* mmp_shutdown */
  122. };
  123. GC_bool GC_use_debug = 0;
  124. void GC_pcr_install()
  125. {
  126. PCR_MM_Install((GC_use_debug? &GC_DebugRep : &GC_Rep), &GC_old_allocator);
  127. }
  128. PCR_ERes
  129. PCR_GC_Setup(void)
  130. {
  131. return PCR_ERes_okay;
  132. }
  133. PCR_ERes
  134. PCR_GC_Run(void)
  135. {
  136. if( !PCR_Base_TestPCRArg("-nogc") ) {
  137. GC_quiet = ( PCR_Base_TestPCRArg("-gctrace") ? 0 : 1 );
  138. GC_use_debug = (GC_bool)PCR_Base_TestPCRArg("-debug_alloc");
  139. GC_init();
  140. if( !PCR_Base_TestPCRArg("-nogc_incremental") ) {
  141. /*
  142. * awful hack to test whether VD is implemented ...
  143. */
  144. if( PCR_VD_Start( 0, NIL, 0) != PCR_ERes_FromErr(ENOSYS) ) {
  145. GC_enable_incremental();
  146. }
  147. }
  148. }
  149. return PCR_ERes_okay;
  150. }
  151. void GC_push_thread_structures(void)
  152. {
  153. /* PCR doesn't work unless static roots are pushed. Can't get here. */
  154. ABORT("In GC_push_thread_structures()");
  155. }
  156. # endif