pcr_interface.c 4.6 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. * Note that POSIX PCR requires an ANSI C compiler. Hence we are allowed
  17. * to make the same assumption here.
  18. * We wrap all of the allocator functions to avoid questions of
  19. * compatibility between the prototyped and nonprototyped versions of the f
  20. */
  21. # include "config/PCR_StdTypes.h"
  22. # include "mm/PCR_MM.h"
  23. # include <errno.h>
  24. # define MY_MAGIC 17L
  25. # define MY_DEBUGMAGIC 42L
  26. void * GC_AllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
  27. {
  28. if (ptrFree) {
  29. void * result = (void *)GC_malloc_atomic(size);
  30. if (clear && result != 0) BZERO(result, size);
  31. return(result);
  32. } else {
  33. return((void *)GC_malloc(size));
  34. }
  35. }
  36. void * GC_DebugAllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
  37. {
  38. if (ptrFree) {
  39. void * result = (void *)GC_debug_malloc_atomic(size, __FILE__,
  40. __LINE__);
  41. if (clear && result != 0) BZERO(result, size);
  42. return(result);
  43. } else {
  44. return((void *)GC_debug_malloc(size, __FILE__, __LINE__));
  45. }
  46. }
  47. # define GC_ReallocProc GC_realloc
  48. void * GC_DebugReallocProc(void * old_object, size_t new_size_in_bytes)
  49. {
  50. return(GC_debug_realloc(old_object, new_size_in_bytes, __FILE__, __LINE__));
  51. }
  52. # define GC_FreeProc GC_free
  53. # define GC_DebugFreeProc GC_debug_free
  54. typedef struct {
  55. PCR_ERes (*ed_proc)(void *p, size_t size, PCR_Any data);
  56. GC_bool ed_pointerfree;
  57. PCR_ERes ed_fail_code;
  58. PCR_Any ed_client_data;
  59. } enumerate_data;
  60. void GC_enumerate_block(h, ed)
  61. register struct hblk *h;
  62. enumerate_data * ed;
  63. {
  64. register hdr * hhdr;
  65. register int sz;
  66. word *p;
  67. word * lim;
  68. hhdr = HDR(h);
  69. sz = hhdr -> hb_sz;
  70. if (sz >= 0 && ed -> ed_pointerfree
  71. || sz <= 0 && !(ed -> ed_pointerfree)) return;
  72. if (sz < 0) sz = -sz;
  73. lim = (word *)(h+1) - sz;
  74. p = (word *)h;
  75. do {
  76. if (PCR_ERes_IsErr(ed -> ed_fail_code)) return;
  77. ed -> ed_fail_code =
  78. (*(ed -> ed_proc))(p, WORDS_TO_BYTES(sz), ed -> ed_client_data);
  79. p+= sz;
  80. } while (p <= lim);
  81. }
  82. struct PCR_MM_ProcsRep * GC_old_allocator = 0;
  83. PCR_ERes GC_EnumerateProc(
  84. PCR_Bool ptrFree,
  85. PCR_ERes (*proc)(void *p, size_t size, PCR_Any data),
  86. PCR_Any data
  87. )
  88. {
  89. enumerate_data ed;
  90. ed.ed_proc = proc;
  91. ed.ed_pointerfree = ptrFree;
  92. ed.ed_fail_code = PCR_ERes_okay;
  93. ed.ed_client_data = data;
  94. GC_apply_to_all_blocks(GC_enumerate_block, &ed);
  95. if (ed.ed_fail_code != PCR_ERes_okay) {
  96. return(ed.ed_fail_code);
  97. } else {
  98. /* Also enumerate objects allocated by my predecessors */
  99. return((*(GC_old_allocator->mmp_enumerate))(ptrFree, proc, data));
  100. }
  101. }
  102. void GC_DummyFreeProc(void *p) {}
  103. void GC_DummyShutdownProc(void) {}
  104. struct PCR_MM_ProcsRep GC_Rep = {
  105. MY_MAGIC,
  106. GC_AllocProc,
  107. GC_ReallocProc,
  108. GC_DummyFreeProc, /* mmp_free */
  109. GC_FreeProc, /* mmp_unsafeFree */
  110. GC_EnumerateProc,
  111. GC_DummyShutdownProc /* mmp_shutdown */
  112. };
  113. struct PCR_MM_ProcsRep GC_DebugRep = {
  114. MY_DEBUGMAGIC,
  115. GC_DebugAllocProc,
  116. GC_DebugReallocProc,
  117. GC_DummyFreeProc, /* mmp_free */
  118. GC_DebugFreeProc, /* mmp_unsafeFree */
  119. GC_EnumerateProc,
  120. GC_DummyShutdownProc /* mmp_shutdown */
  121. };
  122. GC_bool GC_use_debug = 0;
  123. void GC_pcr_install()
  124. {
  125. PCR_MM_Install((GC_use_debug? &GC_DebugRep : &GC_Rep), &GC_old_allocator);
  126. }
  127. PCR_ERes
  128. PCR_GC_Setup(void)
  129. {
  130. return PCR_ERes_okay;
  131. }
  132. PCR_ERes
  133. PCR_GC_Run(void)
  134. {
  135. if( !PCR_Base_TestPCRArg("-nogc") ) {
  136. GC_quiet = ( PCR_Base_TestPCRArg("-gctrace") ? 0 : 1 );
  137. GC_use_debug = (GC_bool)PCR_Base_TestPCRArg("-debug_alloc");
  138. GC_init();
  139. if( !PCR_Base_TestPCRArg("-nogc_incremental") ) {
  140. /*
  141. * awful hack to test whether VD is implemented ...
  142. */
  143. if( PCR_VD_Start( 0, NIL, 0) != PCR_ERes_FromErr(ENOSYS) ) {
  144. GC_enable_incremental();
  145. }
  146. }
  147. }
  148. return PCR_ERes_okay;
  149. }
  150. void GC_push_thread_structures(void)
  151. {
  152. /* PCR doesn't work unless static roots are pushed. Can't get here. */
  153. ABORT("In GC_push_thread_structures()");
  154. }
  155. # endif