ggc-page.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. /* "Bag-of-pages" garbage collector for the GNU compiler.
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "rtl.h"
  30. #include "tm_p.h"
  31. #include "diagnostic-core.h"
  32. #include "flags.h"
  33. #include "ggc.h"
  34. #include "ggc-internal.h"
  35. #include "timevar.h"
  36. #include "params.h"
  37. #include "hash-map.h"
  38. #include "is-a.h"
  39. #include "plugin-api.h"
  40. #include "vec.h"
  41. #include "hashtab.h"
  42. #include "hash-set.h"
  43. #include "machmode.h"
  44. #include "hard-reg-set.h"
  45. #include "input.h"
  46. #include "function.h"
  47. #include "ipa-ref.h"
  48. #include "cgraph.h"
  49. #include "cfgloop.h"
  50. #include "plugin.h"
  51. #include "basic-block.h"
  52. /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
  53. file open. Prefer either to valloc. */
  54. #ifdef HAVE_MMAP_ANON
  55. # undef HAVE_MMAP_DEV_ZERO
  56. # define USING_MMAP
  57. #endif
  58. #ifdef HAVE_MMAP_DEV_ZERO
  59. # define USING_MMAP
  60. #endif
  61. #ifndef USING_MMAP
  62. #define USING_MALLOC_PAGE_GROUPS
  63. #endif
  64. #if defined(HAVE_MADVISE) && HAVE_DECL_MADVISE && defined(MADV_DONTNEED) \
  65. && defined(USING_MMAP)
  66. # define USING_MADVISE
  67. #endif
  68. /* Strategy:
  69. This garbage-collecting allocator allocates objects on one of a set
  70. of pages. Each page can allocate objects of a single size only;
  71. available sizes are powers of two starting at four bytes. The size
  72. of an allocation request is rounded up to the next power of two
  73. (`order'), and satisfied from the appropriate page.
  74. Each page is recorded in a page-entry, which also maintains an
  75. in-use bitmap of object positions on the page. This allows the
  76. allocation state of a particular object to be flipped without
  77. touching the page itself.
  78. Each page-entry also has a context depth, which is used to track
  79. pushing and popping of allocation contexts. Only objects allocated
  80. in the current (highest-numbered) context may be collected.
  81. Page entries are arranged in an array of singly-linked lists. The
  82. array is indexed by the allocation size, in bits, of the pages on
  83. it; i.e. all pages on a list allocate objects of the same size.
  84. Pages are ordered on the list such that all non-full pages precede
  85. all full pages, with non-full pages arranged in order of decreasing
  86. context depth.
  87. Empty pages (of all orders) are kept on a single page cache list,
  88. and are considered first when new pages are required; they are
  89. deallocated at the start of the next collection if they haven't
  90. been recycled by then. */
  91. /* Define GGC_DEBUG_LEVEL to print debugging information.
  92. 0: No debugging output.
  93. 1: GC statistics only.
  94. 2: Page-entry allocations/deallocations as well.
  95. 3: Object allocations as well.
  96. 4: Object marks as well. */
  97. #define GGC_DEBUG_LEVEL (0)
  98. #ifndef HOST_BITS_PER_PTR
  99. #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
  100. #endif
  101. /* A two-level tree is used to look up the page-entry for a given
  102. pointer. Two chunks of the pointer's bits are extracted to index
  103. the first and second levels of the tree, as follows:
  104. HOST_PAGE_SIZE_BITS
  105. 32 | |
  106. msb +----------------+----+------+------+ lsb
  107. | | |
  108. PAGE_L1_BITS |
  109. | |
  110. PAGE_L2_BITS
  111. The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
  112. pages are aligned on system page boundaries. The next most
  113. significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
  114. index values in the lookup table, respectively.
  115. For 32-bit architectures and the settings below, there are no
  116. leftover bits. For architectures with wider pointers, the lookup
  117. tree points to a list of pages, which must be scanned to find the
  118. correct one. */
  119. #define PAGE_L1_BITS (8)
  120. #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
  121. #define PAGE_L1_SIZE ((uintptr_t) 1 << PAGE_L1_BITS)
  122. #define PAGE_L2_SIZE ((uintptr_t) 1 << PAGE_L2_BITS)
  123. #define LOOKUP_L1(p) \
  124. (((uintptr_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
  125. #define LOOKUP_L2(p) \
  126. (((uintptr_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
  127. /* The number of objects per allocation page, for objects on a page of
  128. the indicated ORDER. */
  129. #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
  130. /* The number of objects in P. */
  131. #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
  132. /* The size of an object on a page of the indicated ORDER. */
  133. #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
  134. /* For speed, we avoid doing a general integer divide to locate the
  135. offset in the allocation bitmap, by precalculating numbers M, S
  136. such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
  137. within the page which is evenly divisible by the object size Z. */
  138. #define DIV_MULT(ORDER) inverse_table[ORDER].mult
  139. #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
  140. #define OFFSET_TO_BIT(OFFSET, ORDER) \
  141. (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
  142. /* We use this structure to determine the alignment required for
  143. allocations. For power-of-two sized allocations, that's not a
  144. problem, but it does matter for odd-sized allocations.
  145. We do not care about alignment for floating-point types. */
  146. struct max_alignment {
  147. char c;
  148. union {
  149. int64_t i;
  150. void *p;
  151. } u;
  152. };
  153. /* The biggest alignment required. */
  154. #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
  155. /* The number of extra orders, not corresponding to power-of-two sized
  156. objects. */
  157. #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
  158. #define RTL_SIZE(NSLOTS) \
  159. (RTX_HDR_SIZE + (NSLOTS) * sizeof (rtunion))
  160. #define TREE_EXP_SIZE(OPS) \
  161. (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
  162. /* The Ith entry is the maximum size of an object to be stored in the
  163. Ith extra order. Adding a new entry to this array is the *only*
  164. thing you need to do to add a new special allocation size. */
  165. static const size_t extra_order_size_table[] = {
  166. /* Extra orders for small non-power-of-two multiples of MAX_ALIGNMENT.
  167. There are a lot of structures with these sizes and explicitly
  168. listing them risks orders being dropped because they changed size. */
  169. MAX_ALIGNMENT * 3,
  170. MAX_ALIGNMENT * 5,
  171. MAX_ALIGNMENT * 6,
  172. MAX_ALIGNMENT * 7,
  173. MAX_ALIGNMENT * 9,
  174. MAX_ALIGNMENT * 10,
  175. MAX_ALIGNMENT * 11,
  176. MAX_ALIGNMENT * 12,
  177. MAX_ALIGNMENT * 13,
  178. MAX_ALIGNMENT * 14,
  179. MAX_ALIGNMENT * 15,
  180. sizeof (struct tree_decl_non_common),
  181. sizeof (struct tree_field_decl),
  182. sizeof (struct tree_parm_decl),
  183. sizeof (struct tree_var_decl),
  184. sizeof (struct tree_type_non_common),
  185. sizeof (struct function),
  186. sizeof (struct basic_block_def),
  187. sizeof (struct cgraph_node),
  188. sizeof (struct loop),
  189. };
  190. /* The total number of orders. */
  191. #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
  192. /* Compute the smallest nonnegative number which when added to X gives
  193. a multiple of F. */
  194. #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
  195. /* Compute the smallest multiple of F that is >= X. */
  196. #define ROUND_UP(x, f) (CEIL (x, f) * (f))
  197. /* Round X to next multiple of the page size */
  198. #define PAGE_ALIGN(x) (((x) + G.pagesize - 1) & ~(G.pagesize - 1))
  199. /* The Ith entry is the number of objects on a page or order I. */
  200. static unsigned objects_per_page_table[NUM_ORDERS];
  201. /* The Ith entry is the size of an object on a page of order I. */
  202. static size_t object_size_table[NUM_ORDERS];
  203. /* The Ith entry is a pair of numbers (mult, shift) such that
  204. ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
  205. for all k evenly divisible by OBJECT_SIZE(I). */
  206. static struct
  207. {
  208. size_t mult;
  209. unsigned int shift;
  210. }
  211. inverse_table[NUM_ORDERS];
  212. /* A page_entry records the status of an allocation page. This
  213. structure is dynamically sized to fit the bitmap in_use_p. */
  214. typedef struct page_entry
  215. {
  216. /* The next page-entry with objects of the same size, or NULL if
  217. this is the last page-entry. */
  218. struct page_entry *next;
  219. /* The previous page-entry with objects of the same size, or NULL if
  220. this is the first page-entry. The PREV pointer exists solely to
  221. keep the cost of ggc_free manageable. */
  222. struct page_entry *prev;
  223. /* The number of bytes allocated. (This will always be a multiple
  224. of the host system page size.) */
  225. size_t bytes;
  226. /* The address at which the memory is allocated. */
  227. char *page;
  228. #ifdef USING_MALLOC_PAGE_GROUPS
  229. /* Back pointer to the page group this page came from. */
  230. struct page_group *group;
  231. #endif
  232. /* This is the index in the by_depth varray where this page table
  233. can be found. */
  234. unsigned long index_by_depth;
  235. /* Context depth of this page. */
  236. unsigned short context_depth;
  237. /* The number of free objects remaining on this page. */
  238. unsigned short num_free_objects;
  239. /* A likely candidate for the bit position of a free object for the
  240. next allocation from this page. */
  241. unsigned short next_bit_hint;
  242. /* The lg of size of objects allocated from this page. */
  243. unsigned char order;
  244. /* Discarded page? */
  245. bool discarded;
  246. /* A bit vector indicating whether or not objects are in use. The
  247. Nth bit is one if the Nth object on this page is allocated. This
  248. array is dynamically sized. */
  249. unsigned long in_use_p[1];
  250. } page_entry;
  251. #ifdef USING_MALLOC_PAGE_GROUPS
  252. /* A page_group describes a large allocation from malloc, from which
  253. we parcel out aligned pages. */
  254. typedef struct page_group
  255. {
  256. /* A linked list of all extant page groups. */
  257. struct page_group *next;
  258. /* The address we received from malloc. */
  259. char *allocation;
  260. /* The size of the block. */
  261. size_t alloc_size;
  262. /* A bitmask of pages in use. */
  263. unsigned int in_use;
  264. } page_group;
  265. #endif
  266. #if HOST_BITS_PER_PTR <= 32
  267. /* On 32-bit hosts, we use a two level page table, as pictured above. */
  268. typedef page_entry **page_table[PAGE_L1_SIZE];
  269. #else
  270. /* On 64-bit hosts, we use the same two level page tables plus a linked
  271. list that disambiguates the top 32-bits. There will almost always be
  272. exactly one entry in the list. */
  273. typedef struct page_table_chain
  274. {
  275. struct page_table_chain *next;
  276. size_t high_bits;
  277. page_entry **table[PAGE_L1_SIZE];
  278. } *page_table;
  279. #endif
  280. class finalizer
  281. {
  282. public:
  283. finalizer (void *addr, void (*f)(void *)) : m_addr (addr), m_function (f) {}
  284. void *addr () const { return m_addr; }
  285. void call () const { m_function (m_addr); }
  286. private:
  287. void *m_addr;
  288. void (*m_function)(void *);
  289. };
  290. class vec_finalizer
  291. {
  292. public:
  293. vec_finalizer (uintptr_t addr, void (*f)(void *), size_t s, size_t n) :
  294. m_addr (addr), m_function (f), m_object_size (s), m_n_objects (n) {}
  295. void call () const
  296. {
  297. for (size_t i = 0; i < m_n_objects; i++)
  298. m_function (reinterpret_cast<void *> (m_addr + (i * m_object_size)));
  299. }
  300. void *addr () const { return reinterpret_cast<void *> (m_addr); }
  301. private:
  302. uintptr_t m_addr;
  303. void (*m_function)(void *);
  304. size_t m_object_size;
  305. size_t m_n_objects;
  306. };
  307. #ifdef ENABLE_GC_ALWAYS_COLLECT
  308. /* List of free objects to be verified as actually free on the
  309. next collection. */
  310. struct free_object
  311. {
  312. void *object;
  313. struct free_object *next;
  314. };
  315. #endif
  316. /* The rest of the global variables. */
  317. static struct ggc_globals
  318. {
  319. /* The Nth element in this array is a page with objects of size 2^N.
  320. If there are any pages with free objects, they will be at the
  321. head of the list. NULL if there are no page-entries for this
  322. object size. */
  323. page_entry *pages[NUM_ORDERS];
  324. /* The Nth element in this array is the last page with objects of
  325. size 2^N. NULL if there are no page-entries for this object
  326. size. */
  327. page_entry *page_tails[NUM_ORDERS];
  328. /* Lookup table for associating allocation pages with object addresses. */
  329. page_table lookup;
  330. /* The system's page size. */
  331. size_t pagesize;
  332. size_t lg_pagesize;
  333. /* Bytes currently allocated. */
  334. size_t allocated;
  335. /* Bytes currently allocated at the end of the last collection. */
  336. size_t allocated_last_gc;
  337. /* Total amount of memory mapped. */
  338. size_t bytes_mapped;
  339. /* Bit N set if any allocations have been done at context depth N. */
  340. unsigned long context_depth_allocations;
  341. /* Bit N set if any collections have been done at context depth N. */
  342. unsigned long context_depth_collections;
  343. /* The current depth in the context stack. */
  344. unsigned short context_depth;
  345. /* A file descriptor open to /dev/zero for reading. */
  346. #if defined (HAVE_MMAP_DEV_ZERO)
  347. int dev_zero_fd;
  348. #endif
  349. /* A cache of free system pages. */
  350. page_entry *free_pages;
  351. #ifdef USING_MALLOC_PAGE_GROUPS
  352. page_group *page_groups;
  353. #endif
  354. /* The file descriptor for debugging output. */
  355. FILE *debug_file;
  356. /* Current number of elements in use in depth below. */
  357. unsigned int depth_in_use;
  358. /* Maximum number of elements that can be used before resizing. */
  359. unsigned int depth_max;
  360. /* Each element of this array is an index in by_depth where the given
  361. depth starts. This structure is indexed by that given depth we
  362. are interested in. */
  363. unsigned int *depth;
  364. /* Current number of elements in use in by_depth below. */
  365. unsigned int by_depth_in_use;
  366. /* Maximum number of elements that can be used before resizing. */
  367. unsigned int by_depth_max;
  368. /* Each element of this array is a pointer to a page_entry, all
  369. page_entries can be found in here by increasing depth.
  370. index_by_depth in the page_entry is the index into this data
  371. structure where that page_entry can be found. This is used to
  372. speed up finding all page_entries at a particular depth. */
  373. page_entry **by_depth;
  374. /* Each element is a pointer to the saved in_use_p bits, if any,
  375. zero otherwise. We allocate them all together, to enable a
  376. better runtime data access pattern. */
  377. unsigned long **save_in_use;
  378. /* Finalizers for single objects. */
  379. vec<finalizer> finalizers;
  380. /* Finalizers for vectors of objects. */
  381. vec<vec_finalizer> vec_finalizers;
  382. #ifdef ENABLE_GC_ALWAYS_COLLECT
  383. /* List of free objects to be verified as actually free on the
  384. next collection. */
  385. struct free_object *free_object_list;
  386. #endif
  387. struct
  388. {
  389. /* Total GC-allocated memory. */
  390. unsigned long long total_allocated;
  391. /* Total overhead for GC-allocated memory. */
  392. unsigned long long total_overhead;
  393. /* Total allocations and overhead for sizes less than 32, 64 and 128.
  394. These sizes are interesting because they are typical cache line
  395. sizes. */
  396. unsigned long long total_allocated_under32;
  397. unsigned long long total_overhead_under32;
  398. unsigned long long total_allocated_under64;
  399. unsigned long long total_overhead_under64;
  400. unsigned long long total_allocated_under128;
  401. unsigned long long total_overhead_under128;
  402. /* The allocations for each of the allocation orders. */
  403. unsigned long long total_allocated_per_order[NUM_ORDERS];
  404. /* The overhead for each of the allocation orders. */
  405. unsigned long long total_overhead_per_order[NUM_ORDERS];
  406. } stats;
  407. } G;
  408. /* True if a gc is currently taking place. */
  409. static bool in_gc = false;
  410. /* The size in bytes required to maintain a bitmap for the objects
  411. on a page-entry. */
  412. #define BITMAP_SIZE(Num_objects) \
  413. (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof (long))
  414. /* Allocate pages in chunks of this size, to throttle calls to memory
  415. allocation routines. The first page is used, the rest go onto the
  416. free list. This cannot be larger than HOST_BITS_PER_INT for the
  417. in_use bitmask for page_group. Hosts that need a different value
  418. can override this by defining GGC_QUIRE_SIZE explicitly. */
  419. #ifndef GGC_QUIRE_SIZE
  420. # ifdef USING_MMAP
  421. # define GGC_QUIRE_SIZE 512 /* 2MB for 4K pages */
  422. # else
  423. # define GGC_QUIRE_SIZE 16
  424. # endif
  425. #endif
  426. /* Initial guess as to how many page table entries we might need. */
  427. #define INITIAL_PTE_COUNT 128
  428. static int ggc_allocated_p (const void *);
  429. static page_entry *lookup_page_table_entry (const void *);
  430. static void set_page_table_entry (void *, page_entry *);
  431. #ifdef USING_MMAP
  432. static char *alloc_anon (char *, size_t, bool check);
  433. #endif
  434. #ifdef USING_MALLOC_PAGE_GROUPS
  435. static size_t page_group_index (char *, char *);
  436. static void set_page_group_in_use (page_group *, char *);
  437. static void clear_page_group_in_use (page_group *, char *);
  438. #endif
  439. static struct page_entry * alloc_page (unsigned);
  440. static void free_page (struct page_entry *);
  441. static void release_pages (void);
  442. static void clear_marks (void);
  443. static void sweep_pages (void);
  444. static void ggc_recalculate_in_use_p (page_entry *);
  445. static void compute_inverse (unsigned);
  446. static inline void adjust_depth (void);
  447. static void move_ptes_to_front (int, int);
  448. void debug_print_page_list (int);
  449. static void push_depth (unsigned int);
  450. static void push_by_depth (page_entry *, unsigned long *);
  451. /* Push an entry onto G.depth. */
  452. inline static void
  453. push_depth (unsigned int i)
  454. {
  455. if (G.depth_in_use >= G.depth_max)
  456. {
  457. G.depth_max *= 2;
  458. G.depth = XRESIZEVEC (unsigned int, G.depth, G.depth_max);
  459. }
  460. G.depth[G.depth_in_use++] = i;
  461. }
  462. /* Push an entry onto G.by_depth and G.save_in_use. */
  463. inline static void
  464. push_by_depth (page_entry *p, unsigned long *s)
  465. {
  466. if (G.by_depth_in_use >= G.by_depth_max)
  467. {
  468. G.by_depth_max *= 2;
  469. G.by_depth = XRESIZEVEC (page_entry *, G.by_depth, G.by_depth_max);
  470. G.save_in_use = XRESIZEVEC (unsigned long *, G.save_in_use,
  471. G.by_depth_max);
  472. }
  473. G.by_depth[G.by_depth_in_use] = p;
  474. G.save_in_use[G.by_depth_in_use++] = s;
  475. }
  476. #if (GCC_VERSION < 3001)
  477. #define prefetch(X) ((void) X)
  478. #else
  479. #define prefetch(X) __builtin_prefetch (X)
  480. #endif
  481. #define save_in_use_p_i(__i) \
  482. (G.save_in_use[__i])
  483. #define save_in_use_p(__p) \
  484. (save_in_use_p_i (__p->index_by_depth))
  485. /* Returns nonzero if P was allocated in GC'able memory. */
  486. static inline int
  487. ggc_allocated_p (const void *p)
  488. {
  489. page_entry ***base;
  490. size_t L1, L2;
  491. #if HOST_BITS_PER_PTR <= 32
  492. base = &G.lookup[0];
  493. #else
  494. page_table table = G.lookup;
  495. uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
  496. while (1)
  497. {
  498. if (table == NULL)
  499. return 0;
  500. if (table->high_bits == high_bits)
  501. break;
  502. table = table->next;
  503. }
  504. base = &table->table[0];
  505. #endif
  506. /* Extract the level 1 and 2 indices. */
  507. L1 = LOOKUP_L1 (p);
  508. L2 = LOOKUP_L2 (p);
  509. return base[L1] && base[L1][L2];
  510. }
  511. /* Traverse the page table and find the entry for a page.
  512. Die (probably) if the object wasn't allocated via GC. */
  513. static inline page_entry *
  514. lookup_page_table_entry (const void *p)
  515. {
  516. page_entry ***base;
  517. size_t L1, L2;
  518. #if HOST_BITS_PER_PTR <= 32
  519. base = &G.lookup[0];
  520. #else
  521. page_table table = G.lookup;
  522. uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
  523. while (table->high_bits != high_bits)
  524. table = table->next;
  525. base = &table->table[0];
  526. #endif
  527. /* Extract the level 1 and 2 indices. */
  528. L1 = LOOKUP_L1 (p);
  529. L2 = LOOKUP_L2 (p);
  530. return base[L1][L2];
  531. }
  532. /* Set the page table entry for a page. */
  533. static void
  534. set_page_table_entry (void *p, page_entry *entry)
  535. {
  536. page_entry ***base;
  537. size_t L1, L2;
  538. #if HOST_BITS_PER_PTR <= 32
  539. base = &G.lookup[0];
  540. #else
  541. page_table table;
  542. uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
  543. for (table = G.lookup; table; table = table->next)
  544. if (table->high_bits == high_bits)
  545. goto found;
  546. /* Not found -- allocate a new table. */
  547. table = XCNEW (struct page_table_chain);
  548. table->next = G.lookup;
  549. table->high_bits = high_bits;
  550. G.lookup = table;
  551. found:
  552. base = &table->table[0];
  553. #endif
  554. /* Extract the level 1 and 2 indices. */
  555. L1 = LOOKUP_L1 (p);
  556. L2 = LOOKUP_L2 (p);
  557. if (base[L1] == NULL)
  558. base[L1] = XCNEWVEC (page_entry *, PAGE_L2_SIZE);
  559. base[L1][L2] = entry;
  560. }
  561. /* Prints the page-entry for object size ORDER, for debugging. */
  562. DEBUG_FUNCTION void
  563. debug_print_page_list (int order)
  564. {
  565. page_entry *p;
  566. printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
  567. (void *) G.page_tails[order]);
  568. p = G.pages[order];
  569. while (p != NULL)
  570. {
  571. printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
  572. p->num_free_objects);
  573. p = p->next;
  574. }
  575. printf ("NULL\n");
  576. fflush (stdout);
  577. }
  578. #ifdef USING_MMAP
  579. /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
  580. (if non-null). The ifdef structure here is intended to cause a
  581. compile error unless exactly one of the HAVE_* is defined. */
  582. static inline char *
  583. alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, bool check)
  584. {
  585. #ifdef HAVE_MMAP_ANON
  586. char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
  587. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  588. #endif
  589. #ifdef HAVE_MMAP_DEV_ZERO
  590. char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
  591. MAP_PRIVATE, G.dev_zero_fd, 0);
  592. #endif
  593. if (page == (char *) MAP_FAILED)
  594. {
  595. if (!check)
  596. return NULL;
  597. perror ("virtual memory exhausted");
  598. exit (FATAL_EXIT_CODE);
  599. }
  600. /* Remember that we allocated this memory. */
  601. G.bytes_mapped += size;
  602. /* Pretend we don't have access to the allocated pages. We'll enable
  603. access to smaller pieces of the area in ggc_internal_alloc. Discard the
  604. handle to avoid handle leak. */
  605. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
  606. return page;
  607. }
  608. #endif
  609. #ifdef USING_MALLOC_PAGE_GROUPS
  610. /* Compute the index for this page into the page group. */
  611. static inline size_t
  612. page_group_index (char *allocation, char *page)
  613. {
  614. return (size_t) (page - allocation) >> G.lg_pagesize;
  615. }
  616. /* Set and clear the in_use bit for this page in the page group. */
  617. static inline void
  618. set_page_group_in_use (page_group *group, char *page)
  619. {
  620. group->in_use |= 1 << page_group_index (group->allocation, page);
  621. }
  622. static inline void
  623. clear_page_group_in_use (page_group *group, char *page)
  624. {
  625. group->in_use &= ~(1 << page_group_index (group->allocation, page));
  626. }
  627. #endif
  628. /* Allocate a new page for allocating objects of size 2^ORDER,
  629. and return an entry for it. The entry is not added to the
  630. appropriate page_table list. */
  631. static inline struct page_entry *
  632. alloc_page (unsigned order)
  633. {
  634. struct page_entry *entry, *p, **pp;
  635. char *page;
  636. size_t num_objects;
  637. size_t bitmap_size;
  638. size_t page_entry_size;
  639. size_t entry_size;
  640. #ifdef USING_MALLOC_PAGE_GROUPS
  641. page_group *group;
  642. #endif
  643. num_objects = OBJECTS_PER_PAGE (order);
  644. bitmap_size = BITMAP_SIZE (num_objects + 1);
  645. page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
  646. entry_size = num_objects * OBJECT_SIZE (order);
  647. if (entry_size < G.pagesize)
  648. entry_size = G.pagesize;
  649. entry_size = PAGE_ALIGN (entry_size);
  650. entry = NULL;
  651. page = NULL;
  652. /* Check the list of free pages for one we can use. */
  653. for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
  654. if (p->bytes == entry_size)
  655. break;
  656. if (p != NULL)
  657. {
  658. if (p->discarded)
  659. G.bytes_mapped += p->bytes;
  660. p->discarded = false;
  661. /* Recycle the allocated memory from this page ... */
  662. *pp = p->next;
  663. page = p->page;
  664. #ifdef USING_MALLOC_PAGE_GROUPS
  665. group = p->group;
  666. #endif
  667. /* ... and, if possible, the page entry itself. */
  668. if (p->order == order)
  669. {
  670. entry = p;
  671. memset (entry, 0, page_entry_size);
  672. }
  673. else
  674. free (p);
  675. }
  676. #ifdef USING_MMAP
  677. else if (entry_size == G.pagesize)
  678. {
  679. /* We want just one page. Allocate a bunch of them and put the
  680. extras on the freelist. (Can only do this optimization with
  681. mmap for backing store.) */
  682. struct page_entry *e, *f = G.free_pages;
  683. int i, entries = GGC_QUIRE_SIZE;
  684. page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE, false);
  685. if (page == NULL)
  686. {
  687. page = alloc_anon (NULL, G.pagesize, true);
  688. entries = 1;
  689. }
  690. /* This loop counts down so that the chain will be in ascending
  691. memory order. */
  692. for (i = entries - 1; i >= 1; i--)
  693. {
  694. e = XCNEWVAR (struct page_entry, page_entry_size);
  695. e->order = order;
  696. e->bytes = G.pagesize;
  697. e->page = page + (i << G.lg_pagesize);
  698. e->next = f;
  699. f = e;
  700. }
  701. G.free_pages = f;
  702. }
  703. else
  704. page = alloc_anon (NULL, entry_size, true);
  705. #endif
  706. #ifdef USING_MALLOC_PAGE_GROUPS
  707. else
  708. {
  709. /* Allocate a large block of memory and serve out the aligned
  710. pages therein. This results in much less memory wastage
  711. than the traditional implementation of valloc. */
  712. char *allocation, *a, *enda;
  713. size_t alloc_size, head_slop, tail_slop;
  714. int multiple_pages = (entry_size == G.pagesize);
  715. if (multiple_pages)
  716. alloc_size = GGC_QUIRE_SIZE * G.pagesize;
  717. else
  718. alloc_size = entry_size + G.pagesize - 1;
  719. allocation = XNEWVEC (char, alloc_size);
  720. page = (char *) (((uintptr_t) allocation + G.pagesize - 1) & -G.pagesize);
  721. head_slop = page - allocation;
  722. if (multiple_pages)
  723. tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
  724. else
  725. tail_slop = alloc_size - entry_size - head_slop;
  726. enda = allocation + alloc_size - tail_slop;
  727. /* We allocated N pages, which are likely not aligned, leaving
  728. us with N-1 usable pages. We plan to place the page_group
  729. structure somewhere in the slop. */
  730. if (head_slop >= sizeof (page_group))
  731. group = (page_group *)page - 1;
  732. else
  733. {
  734. /* We magically got an aligned allocation. Too bad, we have
  735. to waste a page anyway. */
  736. if (tail_slop == 0)
  737. {
  738. enda -= G.pagesize;
  739. tail_slop += G.pagesize;
  740. }
  741. gcc_assert (tail_slop >= sizeof (page_group));
  742. group = (page_group *)enda;
  743. tail_slop -= sizeof (page_group);
  744. }
  745. /* Remember that we allocated this memory. */
  746. group->next = G.page_groups;
  747. group->allocation = allocation;
  748. group->alloc_size = alloc_size;
  749. group->in_use = 0;
  750. G.page_groups = group;
  751. G.bytes_mapped += alloc_size;
  752. /* If we allocated multiple pages, put the rest on the free list. */
  753. if (multiple_pages)
  754. {
  755. struct page_entry *e, *f = G.free_pages;
  756. for (a = enda - G.pagesize; a != page; a -= G.pagesize)
  757. {
  758. e = XCNEWVAR (struct page_entry, page_entry_size);
  759. e->order = order;
  760. e->bytes = G.pagesize;
  761. e->page = a;
  762. e->group = group;
  763. e->next = f;
  764. f = e;
  765. }
  766. G.free_pages = f;
  767. }
  768. }
  769. #endif
  770. if (entry == NULL)
  771. entry = XCNEWVAR (struct page_entry, page_entry_size);
  772. entry->bytes = entry_size;
  773. entry->page = page;
  774. entry->context_depth = G.context_depth;
  775. entry->order = order;
  776. entry->num_free_objects = num_objects;
  777. entry->next_bit_hint = 1;
  778. G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
  779. #ifdef USING_MALLOC_PAGE_GROUPS
  780. entry->group = group;
  781. set_page_group_in_use (group, page);
  782. #endif
  783. /* Set the one-past-the-end in-use bit. This acts as a sentry as we
  784. increment the hint. */
  785. entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
  786. = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
  787. set_page_table_entry (page, entry);
  788. if (GGC_DEBUG_LEVEL >= 2)
  789. fprintf (G.debug_file,
  790. "Allocating page at %p, object size=%lu, data %p-%p\n",
  791. (void *) entry, (unsigned long) OBJECT_SIZE (order), page,
  792. page + entry_size - 1);
  793. return entry;
  794. }
  795. /* Adjust the size of G.depth so that no index greater than the one
  796. used by the top of the G.by_depth is used. */
  797. static inline void
  798. adjust_depth (void)
  799. {
  800. page_entry *top;
  801. if (G.by_depth_in_use)
  802. {
  803. top = G.by_depth[G.by_depth_in_use-1];
  804. /* Peel back indices in depth that index into by_depth, so that
  805. as new elements are added to by_depth, we note the indices
  806. of those elements, if they are for new context depths. */
  807. while (G.depth_in_use > (size_t)top->context_depth+1)
  808. --G.depth_in_use;
  809. }
  810. }
  811. /* For a page that is no longer needed, put it on the free page list. */
  812. static void
  813. free_page (page_entry *entry)
  814. {
  815. if (GGC_DEBUG_LEVEL >= 2)
  816. fprintf (G.debug_file,
  817. "Deallocating page at %p, data %p-%p\n", (void *) entry,
  818. entry->page, entry->page + entry->bytes - 1);
  819. /* Mark the page as inaccessible. Discard the handle to avoid handle
  820. leak. */
  821. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->page, entry->bytes));
  822. set_page_table_entry (entry->page, NULL);
  823. #ifdef USING_MALLOC_PAGE_GROUPS
  824. clear_page_group_in_use (entry->group, entry->page);
  825. #endif
  826. if (G.by_depth_in_use > 1)
  827. {
  828. page_entry *top = G.by_depth[G.by_depth_in_use-1];
  829. int i = entry->index_by_depth;
  830. /* We cannot free a page from a context deeper than the current
  831. one. */
  832. gcc_assert (entry->context_depth == top->context_depth);
  833. /* Put top element into freed slot. */
  834. G.by_depth[i] = top;
  835. G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
  836. top->index_by_depth = i;
  837. }
  838. --G.by_depth_in_use;
  839. adjust_depth ();
  840. entry->next = G.free_pages;
  841. G.free_pages = entry;
  842. }
  843. /* Release the free page cache to the system. */
  844. static void
  845. release_pages (void)
  846. {
  847. #ifdef USING_MADVISE
  848. page_entry *p, *start_p;
  849. char *start;
  850. size_t len;
  851. size_t mapped_len;
  852. page_entry *next, *prev, *newprev;
  853. size_t free_unit = (GGC_QUIRE_SIZE/2) * G.pagesize;
  854. /* First free larger continuous areas to the OS.
  855. This allows other allocators to grab these areas if needed.
  856. This is only done on larger chunks to avoid fragmentation.
  857. This does not always work because the free_pages list is only
  858. approximately sorted. */
  859. p = G.free_pages;
  860. prev = NULL;
  861. while (p)
  862. {
  863. start = p->page;
  864. start_p = p;
  865. len = 0;
  866. mapped_len = 0;
  867. newprev = prev;
  868. while (p && p->page == start + len)
  869. {
  870. len += p->bytes;
  871. if (!p->discarded)
  872. mapped_len += p->bytes;
  873. newprev = p;
  874. p = p->next;
  875. }
  876. if (len >= free_unit)
  877. {
  878. while (start_p != p)
  879. {
  880. next = start_p->next;
  881. free (start_p);
  882. start_p = next;
  883. }
  884. munmap (start, len);
  885. if (prev)
  886. prev->next = p;
  887. else
  888. G.free_pages = p;
  889. G.bytes_mapped -= mapped_len;
  890. continue;
  891. }
  892. prev = newprev;
  893. }
  894. /* Now give back the fragmented pages to the OS, but keep the address
  895. space to reuse it next time. */
  896. for (p = G.free_pages; p; )
  897. {
  898. if (p->discarded)
  899. {
  900. p = p->next;
  901. continue;
  902. }
  903. start = p->page;
  904. len = p->bytes;
  905. start_p = p;
  906. p = p->next;
  907. while (p && p->page == start + len)
  908. {
  909. len += p->bytes;
  910. p = p->next;
  911. }
  912. /* Give the page back to the kernel, but don't free the mapping.
  913. This avoids fragmentation in the virtual memory map of the
  914. process. Next time we can reuse it by just touching it. */
  915. madvise (start, len, MADV_DONTNEED);
  916. /* Don't count those pages as mapped to not touch the garbage collector
  917. unnecessarily. */
  918. G.bytes_mapped -= len;
  919. while (start_p != p)
  920. {
  921. start_p->discarded = true;
  922. start_p = start_p->next;
  923. }
  924. }
  925. #endif
  926. #if defined(USING_MMAP) && !defined(USING_MADVISE)
  927. page_entry *p, *next;
  928. char *start;
  929. size_t len;
  930. /* Gather up adjacent pages so they are unmapped together. */
  931. p = G.free_pages;
  932. while (p)
  933. {
  934. start = p->page;
  935. next = p->next;
  936. len = p->bytes;
  937. free (p);
  938. p = next;
  939. while (p && p->page == start + len)
  940. {
  941. next = p->next;
  942. len += p->bytes;
  943. free (p);
  944. p = next;
  945. }
  946. munmap (start, len);
  947. G.bytes_mapped -= len;
  948. }
  949. G.free_pages = NULL;
  950. #endif
  951. #ifdef USING_MALLOC_PAGE_GROUPS
  952. page_entry **pp, *p;
  953. page_group **gp, *g;
  954. /* Remove all pages from free page groups from the list. */
  955. pp = &G.free_pages;
  956. while ((p = *pp) != NULL)
  957. if (p->group->in_use == 0)
  958. {
  959. *pp = p->next;
  960. free (p);
  961. }
  962. else
  963. pp = &p->next;
  964. /* Remove all free page groups, and release the storage. */
  965. gp = &G.page_groups;
  966. while ((g = *gp) != NULL)
  967. if (g->in_use == 0)
  968. {
  969. *gp = g->next;
  970. G.bytes_mapped -= g->alloc_size;
  971. free (g->allocation);
  972. }
  973. else
  974. gp = &g->next;
  975. #endif
  976. }
  977. /* This table provides a fast way to determine ceil(log_2(size)) for
  978. allocation requests. The minimum allocation size is eight bytes. */
  979. #define NUM_SIZE_LOOKUP 512
  980. static unsigned char size_lookup[NUM_SIZE_LOOKUP] =
  981. {
  982. 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
  983. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  984. 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  985. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  986. 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  987. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  988. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  989. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  990. 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  991. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  992. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  993. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  994. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  995. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  996. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  997. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  998. 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  999. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1000. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1001. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1002. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1003. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1004. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1005. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1006. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1007. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1008. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1009. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1010. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1011. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1012. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  1013. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
  1014. };
  1015. /* For a given size of memory requested for allocation, return the
  1016. actual size that is going to be allocated, as well as the size
  1017. order. */
  1018. static void
  1019. ggc_round_alloc_size_1 (size_t requested_size,
  1020. size_t *size_order,
  1021. size_t *alloced_size)
  1022. {
  1023. size_t order, object_size;
  1024. if (requested_size < NUM_SIZE_LOOKUP)
  1025. {
  1026. order = size_lookup[requested_size];
  1027. object_size = OBJECT_SIZE (order);
  1028. }
  1029. else
  1030. {
  1031. order = 10;
  1032. while (requested_size > (object_size = OBJECT_SIZE (order)))
  1033. order++;
  1034. }
  1035. if (size_order)
  1036. *size_order = order;
  1037. if (alloced_size)
  1038. *alloced_size = object_size;
  1039. }
  1040. /* For a given size of memory requested for allocation, return the
  1041. actual size that is going to be allocated. */
  1042. size_t
  1043. ggc_round_alloc_size (size_t requested_size)
  1044. {
  1045. size_t size = 0;
  1046. ggc_round_alloc_size_1 (requested_size, NULL, &size);
  1047. return size;
  1048. }
  1049. /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
  1050. void *
  1051. ggc_internal_alloc (size_t size, void (*f)(void *), size_t s, size_t n
  1052. MEM_STAT_DECL)
  1053. {
  1054. size_t order, word, bit, object_offset, object_size;
  1055. struct page_entry *entry;
  1056. void *result;
  1057. ggc_round_alloc_size_1 (size, &order, &object_size);
  1058. /* If there are non-full pages for this size allocation, they are at
  1059. the head of the list. */
  1060. entry = G.pages[order];
  1061. /* If there is no page for this object size, or all pages in this
  1062. context are full, allocate a new page. */
  1063. if (entry == NULL || entry->num_free_objects == 0)
  1064. {
  1065. struct page_entry *new_entry;
  1066. new_entry = alloc_page (order);
  1067. new_entry->index_by_depth = G.by_depth_in_use;
  1068. push_by_depth (new_entry, 0);
  1069. /* We can skip context depths, if we do, make sure we go all the
  1070. way to the new depth. */
  1071. while (new_entry->context_depth >= G.depth_in_use)
  1072. push_depth (G.by_depth_in_use-1);
  1073. /* If this is the only entry, it's also the tail. If it is not
  1074. the only entry, then we must update the PREV pointer of the
  1075. ENTRY (G.pages[order]) to point to our new page entry. */
  1076. if (entry == NULL)
  1077. G.page_tails[order] = new_entry;
  1078. else
  1079. entry->prev = new_entry;
  1080. /* Put new pages at the head of the page list. By definition the
  1081. entry at the head of the list always has a NULL pointer. */
  1082. new_entry->next = entry;
  1083. new_entry->prev = NULL;
  1084. entry = new_entry;
  1085. G.pages[order] = new_entry;
  1086. /* For a new page, we know the word and bit positions (in the
  1087. in_use bitmap) of the first available object -- they're zero. */
  1088. new_entry->next_bit_hint = 1;
  1089. word = 0;
  1090. bit = 0;
  1091. object_offset = 0;
  1092. }
  1093. else
  1094. {
  1095. /* First try to use the hint left from the previous allocation
  1096. to locate a clear bit in the in-use bitmap. We've made sure
  1097. that the one-past-the-end bit is always set, so if the hint
  1098. has run over, this test will fail. */
  1099. unsigned hint = entry->next_bit_hint;
  1100. word = hint / HOST_BITS_PER_LONG;
  1101. bit = hint % HOST_BITS_PER_LONG;
  1102. /* If the hint didn't work, scan the bitmap from the beginning. */
  1103. if ((entry->in_use_p[word] >> bit) & 1)
  1104. {
  1105. word = bit = 0;
  1106. while (~entry->in_use_p[word] == 0)
  1107. ++word;
  1108. #if GCC_VERSION >= 3004
  1109. bit = __builtin_ctzl (~entry->in_use_p[word]);
  1110. #else
  1111. while ((entry->in_use_p[word] >> bit) & 1)
  1112. ++bit;
  1113. #endif
  1114. hint = word * HOST_BITS_PER_LONG + bit;
  1115. }
  1116. /* Next time, try the next bit. */
  1117. entry->next_bit_hint = hint + 1;
  1118. object_offset = hint * object_size;
  1119. }
  1120. /* Set the in-use bit. */
  1121. entry->in_use_p[word] |= ((unsigned long) 1 << bit);
  1122. /* Keep a running total of the number of free objects. If this page
  1123. fills up, we may have to move it to the end of the list if the
  1124. next page isn't full. If the next page is full, all subsequent
  1125. pages are full, so there's no need to move it. */
  1126. if (--entry->num_free_objects == 0
  1127. && entry->next != NULL
  1128. && entry->next->num_free_objects > 0)
  1129. {
  1130. /* We have a new head for the list. */
  1131. G.pages[order] = entry->next;
  1132. /* We are moving ENTRY to the end of the page table list.
  1133. The new page at the head of the list will have NULL in
  1134. its PREV field and ENTRY will have NULL in its NEXT field. */
  1135. entry->next->prev = NULL;
  1136. entry->next = NULL;
  1137. /* Append ENTRY to the tail of the list. */
  1138. entry->prev = G.page_tails[order];
  1139. G.page_tails[order]->next = entry;
  1140. G.page_tails[order] = entry;
  1141. }
  1142. /* Calculate the object's address. */
  1143. result = entry->page + object_offset;
  1144. if (GATHER_STATISTICS)
  1145. ggc_record_overhead (OBJECT_SIZE (order), OBJECT_SIZE (order) - size,
  1146. result FINAL_PASS_MEM_STAT);
  1147. #ifdef ENABLE_GC_CHECKING
  1148. /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
  1149. exact same semantics in presence of memory bugs, regardless of
  1150. ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
  1151. handle to avoid handle leak. */
  1152. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, object_size));
  1153. /* `Poison' the entire allocated object, including any padding at
  1154. the end. */
  1155. memset (result, 0xaf, object_size);
  1156. /* Make the bytes after the end of the object unaccessible. Discard the
  1157. handle to avoid handle leak. */
  1158. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) result + size,
  1159. object_size - size));
  1160. #endif
  1161. /* Tell Valgrind that the memory is there, but its content isn't
  1162. defined. The bytes at the end of the object are still marked
  1163. unaccessible. */
  1164. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
  1165. /* Keep track of how many bytes are being allocated. This
  1166. information is used in deciding when to collect. */
  1167. G.allocated += object_size;
  1168. /* For timevar statistics. */
  1169. timevar_ggc_mem_total += object_size;
  1170. if (f && n == 1)
  1171. G.finalizers.safe_push (finalizer (result, f));
  1172. else if (f)
  1173. G.vec_finalizers.safe_push
  1174. (vec_finalizer (reinterpret_cast<uintptr_t> (result), f, s, n));
  1175. if (GATHER_STATISTICS)
  1176. {
  1177. size_t overhead = object_size - size;
  1178. G.stats.total_overhead += overhead;
  1179. G.stats.total_allocated += object_size;
  1180. G.stats.total_overhead_per_order[order] += overhead;
  1181. G.stats.total_allocated_per_order[order] += object_size;
  1182. if (size <= 32)
  1183. {
  1184. G.stats.total_overhead_under32 += overhead;
  1185. G.stats.total_allocated_under32 += object_size;
  1186. }
  1187. if (size <= 64)
  1188. {
  1189. G.stats.total_overhead_under64 += overhead;
  1190. G.stats.total_allocated_under64 += object_size;
  1191. }
  1192. if (size <= 128)
  1193. {
  1194. G.stats.total_overhead_under128 += overhead;
  1195. G.stats.total_allocated_under128 += object_size;
  1196. }
  1197. }
  1198. if (GGC_DEBUG_LEVEL >= 3)
  1199. fprintf (G.debug_file,
  1200. "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
  1201. (unsigned long) size, (unsigned long) object_size, result,
  1202. (void *) entry);
  1203. return result;
  1204. }
  1205. /* Mark function for strings. */
  1206. void
  1207. gt_ggc_m_S (const void *p)
  1208. {
  1209. page_entry *entry;
  1210. unsigned bit, word;
  1211. unsigned long mask;
  1212. unsigned long offset;
  1213. if (!p || !ggc_allocated_p (p))
  1214. return;
  1215. /* Look up the page on which the object is alloced. . */
  1216. entry = lookup_page_table_entry (p);
  1217. gcc_assert (entry);
  1218. /* Calculate the index of the object on the page; this is its bit
  1219. position in the in_use_p bitmap. Note that because a char* might
  1220. point to the middle of an object, we need special code here to
  1221. make sure P points to the start of an object. */
  1222. offset = ((const char *) p - entry->page) % object_size_table[entry->order];
  1223. if (offset)
  1224. {
  1225. /* Here we've seen a char* which does not point to the beginning
  1226. of an allocated object. We assume it points to the middle of
  1227. a STRING_CST. */
  1228. gcc_assert (offset == offsetof (struct tree_string, str));
  1229. p = ((const char *) p) - offset;
  1230. gt_ggc_mx_lang_tree_node (CONST_CAST (void *, p));
  1231. return;
  1232. }
  1233. bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
  1234. word = bit / HOST_BITS_PER_LONG;
  1235. mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
  1236. /* If the bit was previously set, skip it. */
  1237. if (entry->in_use_p[word] & mask)
  1238. return;
  1239. /* Otherwise set it, and decrement the free object count. */
  1240. entry->in_use_p[word] |= mask;
  1241. entry->num_free_objects -= 1;
  1242. if (GGC_DEBUG_LEVEL >= 4)
  1243. fprintf (G.debug_file, "Marking %p\n", p);
  1244. return;
  1245. }
  1246. /* User-callable entry points for marking string X. */
  1247. void
  1248. gt_ggc_mx (const char *& x)
  1249. {
  1250. gt_ggc_m_S (x);
  1251. }
  1252. void
  1253. gt_ggc_mx (unsigned char *& x)
  1254. {
  1255. gt_ggc_m_S (x);
  1256. }
  1257. void
  1258. gt_ggc_mx (unsigned char& x ATTRIBUTE_UNUSED)
  1259. {
  1260. }
  1261. /* If P is not marked, marks it and return false. Otherwise return true.
  1262. P must have been allocated by the GC allocator; it mustn't point to
  1263. static objects, stack variables, or memory allocated with malloc. */
  1264. int
  1265. ggc_set_mark (const void *p)
  1266. {
  1267. page_entry *entry;
  1268. unsigned bit, word;
  1269. unsigned long mask;
  1270. /* Look up the page on which the object is alloced. If the object
  1271. wasn't allocated by the collector, we'll probably die. */
  1272. entry = lookup_page_table_entry (p);
  1273. gcc_assert (entry);
  1274. /* Calculate the index of the object on the page; this is its bit
  1275. position in the in_use_p bitmap. */
  1276. bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
  1277. word = bit / HOST_BITS_PER_LONG;
  1278. mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
  1279. /* If the bit was previously set, skip it. */
  1280. if (entry->in_use_p[word] & mask)
  1281. return 1;
  1282. /* Otherwise set it, and decrement the free object count. */
  1283. entry->in_use_p[word] |= mask;
  1284. entry->num_free_objects -= 1;
  1285. if (GGC_DEBUG_LEVEL >= 4)
  1286. fprintf (G.debug_file, "Marking %p\n", p);
  1287. return 0;
  1288. }
  1289. /* Return 1 if P has been marked, zero otherwise.
  1290. P must have been allocated by the GC allocator; it mustn't point to
  1291. static objects, stack variables, or memory allocated with malloc. */
  1292. int
  1293. ggc_marked_p (const void *p)
  1294. {
  1295. page_entry *entry;
  1296. unsigned bit, word;
  1297. unsigned long mask;
  1298. /* Look up the page on which the object is alloced. If the object
  1299. wasn't allocated by the collector, we'll probably die. */
  1300. entry = lookup_page_table_entry (p);
  1301. gcc_assert (entry);
  1302. /* Calculate the index of the object on the page; this is its bit
  1303. position in the in_use_p bitmap. */
  1304. bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
  1305. word = bit / HOST_BITS_PER_LONG;
  1306. mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
  1307. return (entry->in_use_p[word] & mask) != 0;
  1308. }
  1309. /* Return the size of the gc-able object P. */
  1310. size_t
  1311. ggc_get_size (const void *p)
  1312. {
  1313. page_entry *pe = lookup_page_table_entry (p);
  1314. return OBJECT_SIZE (pe->order);
  1315. }
  1316. /* Release the memory for object P. */
  1317. void
  1318. ggc_free (void *p)
  1319. {
  1320. if (in_gc)
  1321. return;
  1322. page_entry *pe = lookup_page_table_entry (p);
  1323. size_t order = pe->order;
  1324. size_t size = OBJECT_SIZE (order);
  1325. if (GATHER_STATISTICS)
  1326. ggc_free_overhead (p);
  1327. if (GGC_DEBUG_LEVEL >= 3)
  1328. fprintf (G.debug_file,
  1329. "Freeing object, actual size=%lu, at %p on %p\n",
  1330. (unsigned long) size, p, (void *) pe);
  1331. #ifdef ENABLE_GC_CHECKING
  1332. /* Poison the data, to indicate the data is garbage. */
  1333. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
  1334. memset (p, 0xa5, size);
  1335. #endif
  1336. /* Let valgrind know the object is free. */
  1337. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
  1338. #ifdef ENABLE_GC_ALWAYS_COLLECT
  1339. /* In the completely-anal-checking mode, we do *not* immediately free
  1340. the data, but instead verify that the data is *actually* not
  1341. reachable the next time we collect. */
  1342. {
  1343. struct free_object *fo = XNEW (struct free_object);
  1344. fo->object = p;
  1345. fo->next = G.free_object_list;
  1346. G.free_object_list = fo;
  1347. }
  1348. #else
  1349. {
  1350. unsigned int bit_offset, word, bit;
  1351. G.allocated -= size;
  1352. /* Mark the object not-in-use. */
  1353. bit_offset = OFFSET_TO_BIT (((const char *) p) - pe->page, order);
  1354. word = bit_offset / HOST_BITS_PER_LONG;
  1355. bit = bit_offset % HOST_BITS_PER_LONG;
  1356. pe->in_use_p[word] &= ~(1UL << bit);
  1357. if (pe->num_free_objects++ == 0)
  1358. {
  1359. page_entry *p, *q;
  1360. /* If the page is completely full, then it's supposed to
  1361. be after all pages that aren't. Since we've freed one
  1362. object from a page that was full, we need to move the
  1363. page to the head of the list.
  1364. PE is the node we want to move. Q is the previous node
  1365. and P is the next node in the list. */
  1366. q = pe->prev;
  1367. if (q && q->num_free_objects == 0)
  1368. {
  1369. p = pe->next;
  1370. q->next = p;
  1371. /* If PE was at the end of the list, then Q becomes the
  1372. new end of the list. If PE was not the end of the
  1373. list, then we need to update the PREV field for P. */
  1374. if (!p)
  1375. G.page_tails[order] = q;
  1376. else
  1377. p->prev = q;
  1378. /* Move PE to the head of the list. */
  1379. pe->next = G.pages[order];
  1380. pe->prev = NULL;
  1381. G.pages[order]->prev = pe;
  1382. G.pages[order] = pe;
  1383. }
  1384. /* Reset the hint bit to point to the only free object. */
  1385. pe->next_bit_hint = bit_offset;
  1386. }
  1387. }
  1388. #endif
  1389. }
  1390. /* Subroutine of init_ggc which computes the pair of numbers used to
  1391. perform division by OBJECT_SIZE (order) and fills in inverse_table[].
  1392. This algorithm is taken from Granlund and Montgomery's paper
  1393. "Division by Invariant Integers using Multiplication"
  1394. (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
  1395. constants). */
  1396. static void
  1397. compute_inverse (unsigned order)
  1398. {
  1399. size_t size, inv;
  1400. unsigned int e;
  1401. size = OBJECT_SIZE (order);
  1402. e = 0;
  1403. while (size % 2 == 0)
  1404. {
  1405. e++;
  1406. size >>= 1;
  1407. }
  1408. inv = size;
  1409. while (inv * size != 1)
  1410. inv = inv * (2 - inv*size);
  1411. DIV_MULT (order) = inv;
  1412. DIV_SHIFT (order) = e;
  1413. }
  1414. /* Initialize the ggc-mmap allocator. */
  1415. void
  1416. init_ggc (void)
  1417. {
  1418. static bool init_p = false;
  1419. unsigned order;
  1420. if (init_p)
  1421. return;
  1422. init_p = true;
  1423. G.pagesize = getpagesize ();
  1424. G.lg_pagesize = exact_log2 (G.pagesize);
  1425. #ifdef HAVE_MMAP_DEV_ZERO
  1426. G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
  1427. if (G.dev_zero_fd == -1)
  1428. internal_error ("open /dev/zero: %m");
  1429. #endif
  1430. #if 0
  1431. G.debug_file = fopen ("ggc-mmap.debug", "w");
  1432. #else
  1433. G.debug_file = stdout;
  1434. #endif
  1435. #ifdef USING_MMAP
  1436. /* StunOS has an amazing off-by-one error for the first mmap allocation
  1437. after fiddling with RLIMIT_STACK. The result, as hard as it is to
  1438. believe, is an unaligned page allocation, which would cause us to
  1439. hork badly if we tried to use it. */
  1440. {
  1441. char *p = alloc_anon (NULL, G.pagesize, true);
  1442. struct page_entry *e;
  1443. if ((uintptr_t)p & (G.pagesize - 1))
  1444. {
  1445. /* How losing. Discard this one and try another. If we still
  1446. can't get something useful, give up. */
  1447. p = alloc_anon (NULL, G.pagesize, true);
  1448. gcc_assert (!((uintptr_t)p & (G.pagesize - 1)));
  1449. }
  1450. /* We have a good page, might as well hold onto it... */
  1451. e = XCNEW (struct page_entry);
  1452. e->bytes = G.pagesize;
  1453. e->page = p;
  1454. e->next = G.free_pages;
  1455. G.free_pages = e;
  1456. }
  1457. #endif
  1458. /* Initialize the object size table. */
  1459. for (order = 0; order < HOST_BITS_PER_PTR; ++order)
  1460. object_size_table[order] = (size_t) 1 << order;
  1461. for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
  1462. {
  1463. size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
  1464. /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
  1465. so that we're sure of getting aligned memory. */
  1466. s = ROUND_UP (s, MAX_ALIGNMENT);
  1467. object_size_table[order] = s;
  1468. }
  1469. /* Initialize the objects-per-page and inverse tables. */
  1470. for (order = 0; order < NUM_ORDERS; ++order)
  1471. {
  1472. objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
  1473. if (objects_per_page_table[order] == 0)
  1474. objects_per_page_table[order] = 1;
  1475. compute_inverse (order);
  1476. }
  1477. /* Reset the size_lookup array to put appropriately sized objects in
  1478. the special orders. All objects bigger than the previous power
  1479. of two, but no greater than the special size, should go in the
  1480. new order. */
  1481. for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
  1482. {
  1483. int o;
  1484. int i;
  1485. i = OBJECT_SIZE (order);
  1486. if (i >= NUM_SIZE_LOOKUP)
  1487. continue;
  1488. for (o = size_lookup[i]; o == size_lookup [i]; --i)
  1489. size_lookup[i] = order;
  1490. }
  1491. G.depth_in_use = 0;
  1492. G.depth_max = 10;
  1493. G.depth = XNEWVEC (unsigned int, G.depth_max);
  1494. G.by_depth_in_use = 0;
  1495. G.by_depth_max = INITIAL_PTE_COUNT;
  1496. G.by_depth = XNEWVEC (page_entry *, G.by_depth_max);
  1497. G.save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
  1498. }
  1499. /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
  1500. reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
  1501. static void
  1502. ggc_recalculate_in_use_p (page_entry *p)
  1503. {
  1504. unsigned int i;
  1505. size_t num_objects;
  1506. /* Because the past-the-end bit in in_use_p is always set, we
  1507. pretend there is one additional object. */
  1508. num_objects = OBJECTS_IN_PAGE (p) + 1;
  1509. /* Reset the free object count. */
  1510. p->num_free_objects = num_objects;
  1511. /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
  1512. for (i = 0;
  1513. i < CEIL (BITMAP_SIZE (num_objects),
  1514. sizeof (*p->in_use_p));
  1515. ++i)
  1516. {
  1517. unsigned long j;
  1518. /* Something is in use if it is marked, or if it was in use in a
  1519. context further down the context stack. */
  1520. p->in_use_p[i] |= save_in_use_p (p)[i];
  1521. /* Decrement the free object count for every object allocated. */
  1522. for (j = p->in_use_p[i]; j; j >>= 1)
  1523. p->num_free_objects -= (j & 1);
  1524. }
  1525. gcc_assert (p->num_free_objects < num_objects);
  1526. }
  1527. /* Unmark all objects. */
  1528. static void
  1529. clear_marks (void)
  1530. {
  1531. unsigned order;
  1532. for (order = 2; order < NUM_ORDERS; order++)
  1533. {
  1534. page_entry *p;
  1535. for (p = G.pages[order]; p != NULL; p = p->next)
  1536. {
  1537. size_t num_objects = OBJECTS_IN_PAGE (p);
  1538. size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
  1539. /* The data should be page-aligned. */
  1540. gcc_assert (!((uintptr_t) p->page & (G.pagesize - 1)));
  1541. /* Pages that aren't in the topmost context are not collected;
  1542. nevertheless, we need their in-use bit vectors to store GC
  1543. marks. So, back them up first. */
  1544. if (p->context_depth < G.context_depth)
  1545. {
  1546. if (! save_in_use_p (p))
  1547. save_in_use_p (p) = XNEWVAR (unsigned long, bitmap_size);
  1548. memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
  1549. }
  1550. /* Reset reset the number of free objects and clear the
  1551. in-use bits. These will be adjusted by mark_obj. */
  1552. p->num_free_objects = num_objects;
  1553. memset (p->in_use_p, 0, bitmap_size);
  1554. /* Make sure the one-past-the-end bit is always set. */
  1555. p->in_use_p[num_objects / HOST_BITS_PER_LONG]
  1556. = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
  1557. }
  1558. }
  1559. }
  1560. /* Check if any blocks with a registered finalizer have become unmarked. If so
  1561. run the finalizer and unregister it because the block is about to be freed.
  1562. Note that no garantee is made about what order finalizers will run in so
  1563. touching other objects in gc memory is extremely unwise. */
  1564. static void
  1565. ggc_handle_finalizers ()
  1566. {
  1567. if (G.context_depth != 0)
  1568. return;
  1569. unsigned length = G.finalizers.length ();
  1570. for (unsigned int i = 0; i < length;)
  1571. {
  1572. finalizer &f = G.finalizers[i];
  1573. if (!ggc_marked_p (f.addr ()))
  1574. {
  1575. f.call ();
  1576. G.finalizers.unordered_remove (i);
  1577. length--;
  1578. }
  1579. else
  1580. i++;
  1581. }
  1582. length = G.vec_finalizers.length ();
  1583. for (unsigned int i = 0; i < length;)
  1584. {
  1585. vec_finalizer &f = G.vec_finalizers[i];
  1586. if (!ggc_marked_p (f.addr ()))
  1587. {
  1588. f.call ();
  1589. G.vec_finalizers.unordered_remove (i);
  1590. length--;
  1591. }
  1592. else
  1593. i++;
  1594. }
  1595. }
  1596. /* Free all empty pages. Partially empty pages need no attention
  1597. because the `mark' bit doubles as an `unused' bit. */
  1598. static void
  1599. sweep_pages (void)
  1600. {
  1601. unsigned order;
  1602. for (order = 2; order < NUM_ORDERS; order++)
  1603. {
  1604. /* The last page-entry to consider, regardless of entries
  1605. placed at the end of the list. */
  1606. page_entry * const last = G.page_tails[order];
  1607. size_t num_objects;
  1608. size_t live_objects;
  1609. page_entry *p, *previous;
  1610. int done;
  1611. p = G.pages[order];
  1612. if (p == NULL)
  1613. continue;
  1614. previous = NULL;
  1615. do
  1616. {
  1617. page_entry *next = p->next;
  1618. /* Loop until all entries have been examined. */
  1619. done = (p == last);
  1620. num_objects = OBJECTS_IN_PAGE (p);
  1621. /* Add all live objects on this page to the count of
  1622. allocated memory. */
  1623. live_objects = num_objects - p->num_free_objects;
  1624. G.allocated += OBJECT_SIZE (order) * live_objects;
  1625. /* Only objects on pages in the topmost context should get
  1626. collected. */
  1627. if (p->context_depth < G.context_depth)
  1628. ;
  1629. /* Remove the page if it's empty. */
  1630. else if (live_objects == 0)
  1631. {
  1632. /* If P was the first page in the list, then NEXT
  1633. becomes the new first page in the list, otherwise
  1634. splice P out of the forward pointers. */
  1635. if (! previous)
  1636. G.pages[order] = next;
  1637. else
  1638. previous->next = next;
  1639. /* Splice P out of the back pointers too. */
  1640. if (next)
  1641. next->prev = previous;
  1642. /* Are we removing the last element? */
  1643. if (p == G.page_tails[order])
  1644. G.page_tails[order] = previous;
  1645. free_page (p);
  1646. p = previous;
  1647. }
  1648. /* If the page is full, move it to the end. */
  1649. else if (p->num_free_objects == 0)
  1650. {
  1651. /* Don't move it if it's already at the end. */
  1652. if (p != G.page_tails[order])
  1653. {
  1654. /* Move p to the end of the list. */
  1655. p->next = NULL;
  1656. p->prev = G.page_tails[order];
  1657. G.page_tails[order]->next = p;
  1658. /* Update the tail pointer... */
  1659. G.page_tails[order] = p;
  1660. /* ... and the head pointer, if necessary. */
  1661. if (! previous)
  1662. G.pages[order] = next;
  1663. else
  1664. previous->next = next;
  1665. /* And update the backpointer in NEXT if necessary. */
  1666. if (next)
  1667. next->prev = previous;
  1668. p = previous;
  1669. }
  1670. }
  1671. /* If we've fallen through to here, it's a page in the
  1672. topmost context that is neither full nor empty. Such a
  1673. page must precede pages at lesser context depth in the
  1674. list, so move it to the head. */
  1675. else if (p != G.pages[order])
  1676. {
  1677. previous->next = p->next;
  1678. /* Update the backchain in the next node if it exists. */
  1679. if (p->next)
  1680. p->next->prev = previous;
  1681. /* Move P to the head of the list. */
  1682. p->next = G.pages[order];
  1683. p->prev = NULL;
  1684. G.pages[order]->prev = p;
  1685. /* Update the head pointer. */
  1686. G.pages[order] = p;
  1687. /* Are we moving the last element? */
  1688. if (G.page_tails[order] == p)
  1689. G.page_tails[order] = previous;
  1690. p = previous;
  1691. }
  1692. previous = p;
  1693. p = next;
  1694. }
  1695. while (! done);
  1696. /* Now, restore the in_use_p vectors for any pages from contexts
  1697. other than the current one. */
  1698. for (p = G.pages[order]; p; p = p->next)
  1699. if (p->context_depth != G.context_depth)
  1700. ggc_recalculate_in_use_p (p);
  1701. }
  1702. }
  1703. #ifdef ENABLE_GC_CHECKING
  1704. /* Clobber all free objects. */
  1705. static void
  1706. poison_pages (void)
  1707. {
  1708. unsigned order;
  1709. for (order = 2; order < NUM_ORDERS; order++)
  1710. {
  1711. size_t size = OBJECT_SIZE (order);
  1712. page_entry *p;
  1713. for (p = G.pages[order]; p != NULL; p = p->next)
  1714. {
  1715. size_t num_objects;
  1716. size_t i;
  1717. if (p->context_depth != G.context_depth)
  1718. /* Since we don't do any collection for pages in pushed
  1719. contexts, there's no need to do any poisoning. And
  1720. besides, the IN_USE_P array isn't valid until we pop
  1721. contexts. */
  1722. continue;
  1723. num_objects = OBJECTS_IN_PAGE (p);
  1724. for (i = 0; i < num_objects; i++)
  1725. {
  1726. size_t word, bit;
  1727. word = i / HOST_BITS_PER_LONG;
  1728. bit = i % HOST_BITS_PER_LONG;
  1729. if (((p->in_use_p[word] >> bit) & 1) == 0)
  1730. {
  1731. char *object = p->page + i * size;
  1732. /* Keep poison-by-write when we expect to use Valgrind,
  1733. so the exact same memory semantics is kept, in case
  1734. there are memory errors. We override this request
  1735. below. */
  1736. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (object,
  1737. size));
  1738. memset (object, 0xa5, size);
  1739. /* Drop the handle to avoid handle leak. */
  1740. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object, size));
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. #else
  1747. #define poison_pages()
  1748. #endif
  1749. #ifdef ENABLE_GC_ALWAYS_COLLECT
  1750. /* Validate that the reportedly free objects actually are. */
  1751. static void
  1752. validate_free_objects (void)
  1753. {
  1754. struct free_object *f, *next, *still_free = NULL;
  1755. for (f = G.free_object_list; f ; f = next)
  1756. {
  1757. page_entry *pe = lookup_page_table_entry (f->object);
  1758. size_t bit, word;
  1759. bit = OFFSET_TO_BIT ((char *)f->object - pe->page, pe->order);
  1760. word = bit / HOST_BITS_PER_LONG;
  1761. bit = bit % HOST_BITS_PER_LONG;
  1762. next = f->next;
  1763. /* Make certain it isn't visible from any root. Notice that we
  1764. do this check before sweep_pages merges save_in_use_p. */
  1765. gcc_assert (!(pe->in_use_p[word] & (1UL << bit)));
  1766. /* If the object comes from an outer context, then retain the
  1767. free_object entry, so that we can verify that the address
  1768. isn't live on the stack in some outer context. */
  1769. if (pe->context_depth != G.context_depth)
  1770. {
  1771. f->next = still_free;
  1772. still_free = f;
  1773. }
  1774. else
  1775. free (f);
  1776. }
  1777. G.free_object_list = still_free;
  1778. }
  1779. #else
  1780. #define validate_free_objects()
  1781. #endif
  1782. /* Top level mark-and-sweep routine. */
  1783. void
  1784. ggc_collect (void)
  1785. {
  1786. /* Avoid frequent unnecessary work by skipping collection if the
  1787. total allocations haven't expanded much since the last
  1788. collection. */
  1789. float allocated_last_gc =
  1790. MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
  1791. float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
  1792. if (G.allocated < allocated_last_gc + min_expand && !ggc_force_collect)
  1793. return;
  1794. timevar_push (TV_GC);
  1795. if (!quiet_flag)
  1796. fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
  1797. if (GGC_DEBUG_LEVEL >= 2)
  1798. fprintf (G.debug_file, "BEGIN COLLECTING\n");
  1799. /* Zero the total allocated bytes. This will be recalculated in the
  1800. sweep phase. */
  1801. G.allocated = 0;
  1802. /* Release the pages we freed the last time we collected, but didn't
  1803. reuse in the interim. */
  1804. release_pages ();
  1805. /* Indicate that we've seen collections at this context depth. */
  1806. G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
  1807. invoke_plugin_callbacks (PLUGIN_GGC_START, NULL);
  1808. in_gc = true;
  1809. clear_marks ();
  1810. ggc_mark_roots ();
  1811. ggc_handle_finalizers ();
  1812. if (GATHER_STATISTICS)
  1813. ggc_prune_overhead_list ();
  1814. poison_pages ();
  1815. validate_free_objects ();
  1816. sweep_pages ();
  1817. in_gc = false;
  1818. G.allocated_last_gc = G.allocated;
  1819. invoke_plugin_callbacks (PLUGIN_GGC_END, NULL);
  1820. timevar_pop (TV_GC);
  1821. if (!quiet_flag)
  1822. fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
  1823. if (GGC_DEBUG_LEVEL >= 2)
  1824. fprintf (G.debug_file, "END COLLECTING\n");
  1825. }
  1826. /* Assume that all GGC memory is reachable and grow the limits for next collection.
  1827. With checking, trigger GGC so -Q compilation outputs how much of memory really is
  1828. reachable. */
  1829. void
  1830. ggc_grow (void)
  1831. {
  1832. #ifndef ENABLE_CHECKING
  1833. G.allocated_last_gc = MAX (G.allocated_last_gc,
  1834. G.allocated);
  1835. #else
  1836. ggc_collect ();
  1837. #endif
  1838. if (!quiet_flag)
  1839. fprintf (stderr, " {GC start %luk} ", (unsigned long) G.allocated / 1024);
  1840. }
  1841. /* Print allocation statistics. */
  1842. #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
  1843. ? (x) \
  1844. : ((x) < 1024*1024*10 \
  1845. ? (x) / 1024 \
  1846. : (x) / (1024*1024))))
  1847. #define STAT_LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
  1848. void
  1849. ggc_print_statistics (void)
  1850. {
  1851. struct ggc_statistics stats;
  1852. unsigned int i;
  1853. size_t total_overhead = 0;
  1854. /* Clear the statistics. */
  1855. memset (&stats, 0, sizeof (stats));
  1856. /* Make sure collection will really occur. */
  1857. G.allocated_last_gc = 0;
  1858. /* Collect and print the statistics common across collectors. */
  1859. ggc_print_common_statistics (stderr, &stats);
  1860. /* Release free pages so that we will not count the bytes allocated
  1861. there as part of the total allocated memory. */
  1862. release_pages ();
  1863. /* Collect some information about the various sizes of
  1864. allocation. */
  1865. fprintf (stderr,
  1866. "Memory still allocated at the end of the compilation process\n");
  1867. fprintf (stderr, "%-5s %10s %10s %10s\n",
  1868. "Size", "Allocated", "Used", "Overhead");
  1869. for (i = 0; i < NUM_ORDERS; ++i)
  1870. {
  1871. page_entry *p;
  1872. size_t allocated;
  1873. size_t in_use;
  1874. size_t overhead;
  1875. /* Skip empty entries. */
  1876. if (!G.pages[i])
  1877. continue;
  1878. overhead = allocated = in_use = 0;
  1879. /* Figure out the total number of bytes allocated for objects of
  1880. this size, and how many of them are actually in use. Also figure
  1881. out how much memory the page table is using. */
  1882. for (p = G.pages[i]; p; p = p->next)
  1883. {
  1884. allocated += p->bytes;
  1885. in_use +=
  1886. (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
  1887. overhead += (sizeof (page_entry) - sizeof (long)
  1888. + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
  1889. }
  1890. fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
  1891. (unsigned long) OBJECT_SIZE (i),
  1892. SCALE (allocated), STAT_LABEL (allocated),
  1893. SCALE (in_use), STAT_LABEL (in_use),
  1894. SCALE (overhead), STAT_LABEL (overhead));
  1895. total_overhead += overhead;
  1896. }
  1897. fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
  1898. SCALE (G.bytes_mapped), STAT_LABEL (G.bytes_mapped),
  1899. SCALE (G.allocated), STAT_LABEL (G.allocated),
  1900. SCALE (total_overhead), STAT_LABEL (total_overhead));
  1901. if (GATHER_STATISTICS)
  1902. {
  1903. fprintf (stderr, "\nTotal allocations and overheads during the compilation process\n");
  1904. fprintf (stderr, "Total Overhead: %10" HOST_LONG_LONG_FORMAT "d\n",
  1905. G.stats.total_overhead);
  1906. fprintf (stderr, "Total Allocated: %10" HOST_LONG_LONG_FORMAT "d\n",
  1907. G.stats.total_allocated);
  1908. fprintf (stderr, "Total Overhead under 32B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1909. G.stats.total_overhead_under32);
  1910. fprintf (stderr, "Total Allocated under 32B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1911. G.stats.total_allocated_under32);
  1912. fprintf (stderr, "Total Overhead under 64B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1913. G.stats.total_overhead_under64);
  1914. fprintf (stderr, "Total Allocated under 64B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1915. G.stats.total_allocated_under64);
  1916. fprintf (stderr, "Total Overhead under 128B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1917. G.stats.total_overhead_under128);
  1918. fprintf (stderr, "Total Allocated under 128B: %10" HOST_LONG_LONG_FORMAT "d\n",
  1919. G.stats.total_allocated_under128);
  1920. for (i = 0; i < NUM_ORDERS; i++)
  1921. if (G.stats.total_allocated_per_order[i])
  1922. {
  1923. fprintf (stderr, "Total Overhead page size %7lu: %10" HOST_LONG_LONG_FORMAT "d\n",
  1924. (unsigned long) OBJECT_SIZE (i),
  1925. G.stats.total_overhead_per_order[i]);
  1926. fprintf (stderr, "Total Allocated page size %7lu: %10" HOST_LONG_LONG_FORMAT "d\n",
  1927. (unsigned long) OBJECT_SIZE (i),
  1928. G.stats.total_allocated_per_order[i]);
  1929. }
  1930. }
  1931. }
  1932. struct ggc_pch_ondisk
  1933. {
  1934. unsigned totals[NUM_ORDERS];
  1935. };
  1936. struct ggc_pch_data
  1937. {
  1938. struct ggc_pch_ondisk d;
  1939. uintptr_t base[NUM_ORDERS];
  1940. size_t written[NUM_ORDERS];
  1941. };
  1942. struct ggc_pch_data *
  1943. init_ggc_pch (void)
  1944. {
  1945. return XCNEW (struct ggc_pch_data);
  1946. }
  1947. void
  1948. ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
  1949. size_t size, bool is_string ATTRIBUTE_UNUSED)
  1950. {
  1951. unsigned order;
  1952. if (size < NUM_SIZE_LOOKUP)
  1953. order = size_lookup[size];
  1954. else
  1955. {
  1956. order = 10;
  1957. while (size > OBJECT_SIZE (order))
  1958. order++;
  1959. }
  1960. d->d.totals[order]++;
  1961. }
  1962. size_t
  1963. ggc_pch_total_size (struct ggc_pch_data *d)
  1964. {
  1965. size_t a = 0;
  1966. unsigned i;
  1967. for (i = 0; i < NUM_ORDERS; i++)
  1968. a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
  1969. return a;
  1970. }
  1971. void
  1972. ggc_pch_this_base (struct ggc_pch_data *d, void *base)
  1973. {
  1974. uintptr_t a = (uintptr_t) base;
  1975. unsigned i;
  1976. for (i = 0; i < NUM_ORDERS; i++)
  1977. {
  1978. d->base[i] = a;
  1979. a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
  1980. }
  1981. }
  1982. char *
  1983. ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
  1984. size_t size, bool is_string ATTRIBUTE_UNUSED)
  1985. {
  1986. unsigned order;
  1987. char *result;
  1988. if (size < NUM_SIZE_LOOKUP)
  1989. order = size_lookup[size];
  1990. else
  1991. {
  1992. order = 10;
  1993. while (size > OBJECT_SIZE (order))
  1994. order++;
  1995. }
  1996. result = (char *) d->base[order];
  1997. d->base[order] += OBJECT_SIZE (order);
  1998. return result;
  1999. }
  2000. void
  2001. ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
  2002. FILE *f ATTRIBUTE_UNUSED)
  2003. {
  2004. /* Nothing to do. */
  2005. }
  2006. void
  2007. ggc_pch_write_object (struct ggc_pch_data *d,
  2008. FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
  2009. size_t size, bool is_string ATTRIBUTE_UNUSED)
  2010. {
  2011. unsigned order;
  2012. static const char emptyBytes[256] = { 0 };
  2013. if (size < NUM_SIZE_LOOKUP)
  2014. order = size_lookup[size];
  2015. else
  2016. {
  2017. order = 10;
  2018. while (size > OBJECT_SIZE (order))
  2019. order++;
  2020. }
  2021. if (fwrite (x, size, 1, f) != 1)
  2022. fatal_error (input_location, "can%'t write PCH file: %m");
  2023. /* If SIZE is not the same as OBJECT_SIZE(order), then we need to pad the
  2024. object out to OBJECT_SIZE(order). This happens for strings. */
  2025. if (size != OBJECT_SIZE (order))
  2026. {
  2027. unsigned padding = OBJECT_SIZE (order) - size;
  2028. /* To speed small writes, we use a nulled-out array that's larger
  2029. than most padding requests as the source for our null bytes. This
  2030. permits us to do the padding with fwrite() rather than fseek(), and
  2031. limits the chance the OS may try to flush any outstanding writes. */
  2032. if (padding <= sizeof (emptyBytes))
  2033. {
  2034. if (fwrite (emptyBytes, 1, padding, f) != padding)
  2035. fatal_error (input_location, "can%'t write PCH file");
  2036. }
  2037. else
  2038. {
  2039. /* Larger than our buffer? Just default to fseek. */
  2040. if (fseek (f, padding, SEEK_CUR) != 0)
  2041. fatal_error (input_location, "can%'t write PCH file");
  2042. }
  2043. }
  2044. d->written[order]++;
  2045. if (d->written[order] == d->d.totals[order]
  2046. && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
  2047. G.pagesize),
  2048. SEEK_CUR) != 0)
  2049. fatal_error (input_location, "can%'t write PCH file: %m");
  2050. }
  2051. void
  2052. ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
  2053. {
  2054. if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
  2055. fatal_error (input_location, "can%'t write PCH file: %m");
  2056. free (d);
  2057. }
  2058. /* Move the PCH PTE entries just added to the end of by_depth, to the
  2059. front. */
  2060. static void
  2061. move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
  2062. {
  2063. unsigned i;
  2064. /* First, we swap the new entries to the front of the varrays. */
  2065. page_entry **new_by_depth;
  2066. unsigned long **new_save_in_use;
  2067. new_by_depth = XNEWVEC (page_entry *, G.by_depth_max);
  2068. new_save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
  2069. memcpy (&new_by_depth[0],
  2070. &G.by_depth[count_old_page_tables],
  2071. count_new_page_tables * sizeof (void *));
  2072. memcpy (&new_by_depth[count_new_page_tables],
  2073. &G.by_depth[0],
  2074. count_old_page_tables * sizeof (void *));
  2075. memcpy (&new_save_in_use[0],
  2076. &G.save_in_use[count_old_page_tables],
  2077. count_new_page_tables * sizeof (void *));
  2078. memcpy (&new_save_in_use[count_new_page_tables],
  2079. &G.save_in_use[0],
  2080. count_old_page_tables * sizeof (void *));
  2081. free (G.by_depth);
  2082. free (G.save_in_use);
  2083. G.by_depth = new_by_depth;
  2084. G.save_in_use = new_save_in_use;
  2085. /* Now update all the index_by_depth fields. */
  2086. for (i = G.by_depth_in_use; i > 0; --i)
  2087. {
  2088. page_entry *p = G.by_depth[i-1];
  2089. p->index_by_depth = i-1;
  2090. }
  2091. /* And last, we update the depth pointers in G.depth. The first
  2092. entry is already 0, and context 0 entries always start at index
  2093. 0, so there is nothing to update in the first slot. We need a
  2094. second slot, only if we have old ptes, and if we do, they start
  2095. at index count_new_page_tables. */
  2096. if (count_old_page_tables)
  2097. push_depth (count_new_page_tables);
  2098. }
  2099. void
  2100. ggc_pch_read (FILE *f, void *addr)
  2101. {
  2102. struct ggc_pch_ondisk d;
  2103. unsigned i;
  2104. char *offs = (char *) addr;
  2105. unsigned long count_old_page_tables;
  2106. unsigned long count_new_page_tables;
  2107. count_old_page_tables = G.by_depth_in_use;
  2108. /* We've just read in a PCH file. So, every object that used to be
  2109. allocated is now free. */
  2110. clear_marks ();
  2111. #ifdef ENABLE_GC_CHECKING
  2112. poison_pages ();
  2113. #endif
  2114. /* Since we free all the allocated objects, the free list becomes
  2115. useless. Validate it now, which will also clear it. */
  2116. validate_free_objects ();
  2117. /* No object read from a PCH file should ever be freed. So, set the
  2118. context depth to 1, and set the depth of all the currently-allocated
  2119. pages to be 1 too. PCH pages will have depth 0. */
  2120. gcc_assert (!G.context_depth);
  2121. G.context_depth = 1;
  2122. for (i = 0; i < NUM_ORDERS; i++)
  2123. {
  2124. page_entry *p;
  2125. for (p = G.pages[i]; p != NULL; p = p->next)
  2126. p->context_depth = G.context_depth;
  2127. }
  2128. /* Allocate the appropriate page-table entries for the pages read from
  2129. the PCH file. */
  2130. if (fread (&d, sizeof (d), 1, f) != 1)
  2131. fatal_error (input_location, "can%'t read PCH file: %m");
  2132. for (i = 0; i < NUM_ORDERS; i++)
  2133. {
  2134. struct page_entry *entry;
  2135. char *pte;
  2136. size_t bytes;
  2137. size_t num_objs;
  2138. size_t j;
  2139. if (d.totals[i] == 0)
  2140. continue;
  2141. bytes = PAGE_ALIGN (d.totals[i] * OBJECT_SIZE (i));
  2142. num_objs = bytes / OBJECT_SIZE (i);
  2143. entry = XCNEWVAR (struct page_entry, (sizeof (struct page_entry)
  2144. - sizeof (long)
  2145. + BITMAP_SIZE (num_objs + 1)));
  2146. entry->bytes = bytes;
  2147. entry->page = offs;
  2148. entry->context_depth = 0;
  2149. offs += bytes;
  2150. entry->num_free_objects = 0;
  2151. entry->order = i;
  2152. for (j = 0;
  2153. j + HOST_BITS_PER_LONG <= num_objs + 1;
  2154. j += HOST_BITS_PER_LONG)
  2155. entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
  2156. for (; j < num_objs + 1; j++)
  2157. entry->in_use_p[j / HOST_BITS_PER_LONG]
  2158. |= 1L << (j % HOST_BITS_PER_LONG);
  2159. for (pte = entry->page;
  2160. pte < entry->page + entry->bytes;
  2161. pte += G.pagesize)
  2162. set_page_table_entry (pte, entry);
  2163. if (G.page_tails[i] != NULL)
  2164. G.page_tails[i]->next = entry;
  2165. else
  2166. G.pages[i] = entry;
  2167. G.page_tails[i] = entry;
  2168. /* We start off by just adding all the new information to the
  2169. end of the varrays, later, we will move the new information
  2170. to the front of the varrays, as the PCH page tables are at
  2171. context 0. */
  2172. push_by_depth (entry, 0);
  2173. }
  2174. /* Now, we update the various data structures that speed page table
  2175. handling. */
  2176. count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
  2177. move_ptes_to_front (count_old_page_tables, count_new_page_tables);
  2178. /* Update the statistics. */
  2179. G.allocated = G.allocated_last_gc = offs - (char *)addr;
  2180. }