FastMalloc.cpp 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208
  1. // Copyright (c) 2005, 2007, Google Inc.
  2. // All rights reserved.
  3. // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat <opensource@google.com>
  32. //
  33. // A malloc that uses a per-thread cache to satisfy small malloc requests.
  34. // (The time for malloc/free of a small object drops from 300 ns to 50 ns.)
  35. //
  36. // See doc/tcmalloc.html for a high-level
  37. // description of how this malloc works.
  38. //
  39. // SYNCHRONIZATION
  40. // 1. The thread-specific lists are accessed without acquiring any locks.
  41. // This is safe because each such list is only accessed by one thread.
  42. // 2. We have a lock per central free-list, and hold it while manipulating
  43. // the central free list for a particular size.
  44. // 3. The central page allocator is protected by "pageheap_lock".
  45. // 4. The pagemap (which maps from page-number to descriptor),
  46. // can be read without holding any locks, and written while holding
  47. // the "pageheap_lock".
  48. // 5. To improve performance, a subset of the information one can get
  49. // from the pagemap is cached in a data structure, pagemap_cache_,
  50. // that atomically reads and writes its entries. This cache can be
  51. // read and written without locking.
  52. //
  53. // This multi-threaded access to the pagemap is safe for fairly
  54. // subtle reasons. We basically assume that when an object X is
  55. // allocated by thread A and deallocated by thread B, there must
  56. // have been appropriate synchronization in the handoff of object
  57. // X from thread A to thread B. The same logic applies to pagemap_cache_.
  58. //
  59. // THE PAGEID-TO-SIZECLASS CACHE
  60. // Hot PageID-to-sizeclass mappings are held by pagemap_cache_. If this cache
  61. // returns 0 for a particular PageID then that means "no information," not that
  62. // the sizeclass is 0. The cache may have stale information for pages that do
  63. // not hold the beginning of any free()'able object. Staleness is eliminated
  64. // in Populate() for pages with sizeclass > 0 objects, and in do_malloc() and
  65. // do_memalign() for all other relevant pages.
  66. //
  67. // TODO: Bias reclamation to larger addresses
  68. // TODO: implement mallinfo/mallopt
  69. // TODO: Better testing
  70. //
  71. // 9/28/2003 (new page-level allocator replaces ptmalloc2):
  72. // * malloc/free of small objects goes from ~300 ns to ~50 ns.
  73. // * allocation of a reasonably complicated struct
  74. // goes from about 1100 ns to about 300 ns.
  75. #include "config.h"
  76. #include "FastMalloc.h"
  77. #include "Assertions.h"
  78. #include "CurrentTime.h"
  79. #include <limits>
  80. #if OS(WINDOWS)
  81. #include <windows.h>
  82. #else
  83. #include <pthread.h>
  84. #endif
  85. #include <string.h>
  86. #include <wtf/StdLibExtras.h>
  87. #if OS(DARWIN)
  88. #include <malloc/malloc.h>
  89. #endif
  90. #ifndef NO_TCMALLOC_SAMPLES
  91. #ifdef WTF_CHANGES
  92. #define NO_TCMALLOC_SAMPLES
  93. #endif
  94. #endif
  95. #if !(defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC) && defined(NDEBUG)
  96. #define FORCE_SYSTEM_MALLOC 0
  97. #else
  98. #define FORCE_SYSTEM_MALLOC 1
  99. #endif
  100. // Harden the pointers stored in the TCMalloc linked lists
  101. #if COMPILER(GCC) && !PLATFORM(QT)
  102. #define ENABLE_TCMALLOC_HARDENING 1
  103. #endif
  104. // Use a background thread to periodically scavenge memory to release back to the system
  105. #if PLATFORM(IOS) || OS(ORBIS)
  106. #define USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY 0
  107. #else
  108. #define USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY 1
  109. #endif
  110. #ifndef NDEBUG
  111. namespace WTF {
  112. #if OS(WINDOWS)
  113. // TLS_OUT_OF_INDEXES is not defined on WinCE.
  114. #ifndef TLS_OUT_OF_INDEXES
  115. #define TLS_OUT_OF_INDEXES 0xffffffff
  116. #endif
  117. static DWORD isForibiddenTlsIndex = TLS_OUT_OF_INDEXES;
  118. static const LPVOID kTlsAllowValue = reinterpret_cast<LPVOID>(0); // Must be zero.
  119. static const LPVOID kTlsForbiddenValue = reinterpret_cast<LPVOID>(1);
  120. #if !ASSERT_DISABLED
  121. static bool isForbidden()
  122. {
  123. // By default, fastMalloc is allowed so we don't allocate the
  124. // tls index unless we're asked to make it forbidden. If TlsSetValue
  125. // has not been called on a thread, the value returned by TlsGetValue is 0.
  126. return (isForibiddenTlsIndex != TLS_OUT_OF_INDEXES) && (TlsGetValue(isForibiddenTlsIndex) == kTlsForbiddenValue);
  127. }
  128. #endif
  129. void fastMallocForbid()
  130. {
  131. if (isForibiddenTlsIndex == TLS_OUT_OF_INDEXES)
  132. isForibiddenTlsIndex = TlsAlloc(); // a little racey, but close enough for debug only
  133. TlsSetValue(isForibiddenTlsIndex, kTlsForbiddenValue);
  134. }
  135. void fastMallocAllow()
  136. {
  137. if (isForibiddenTlsIndex == TLS_OUT_OF_INDEXES)
  138. return;
  139. TlsSetValue(isForibiddenTlsIndex, kTlsAllowValue);
  140. }
  141. #else // !OS(WINDOWS)
  142. static pthread_key_t isForbiddenKey;
  143. static pthread_once_t isForbiddenKeyOnce = PTHREAD_ONCE_INIT;
  144. static void initializeIsForbiddenKey()
  145. {
  146. pthread_key_create(&isForbiddenKey, 0);
  147. }
  148. #if !ASSERT_DISABLED
  149. static bool isForbidden()
  150. {
  151. pthread_once(&isForbiddenKeyOnce, initializeIsForbiddenKey);
  152. return !!pthread_getspecific(isForbiddenKey);
  153. }
  154. #endif
  155. void fastMallocForbid()
  156. {
  157. pthread_once(&isForbiddenKeyOnce, initializeIsForbiddenKey);
  158. pthread_setspecific(isForbiddenKey, &isForbiddenKey);
  159. }
  160. void fastMallocAllow()
  161. {
  162. pthread_once(&isForbiddenKeyOnce, initializeIsForbiddenKey);
  163. pthread_setspecific(isForbiddenKey, 0);
  164. }
  165. #endif // OS(WINDOWS)
  166. } // namespace WTF
  167. #endif // NDEBUG
  168. namespace WTF {
  169. namespace Internal {
  170. #if !ENABLE(WTF_MALLOC_VALIDATION)
  171. WTF_EXPORT_PRIVATE void fastMallocMatchFailed(void*);
  172. #else
  173. COMPILE_ASSERT(((sizeof(ValidationHeader) % sizeof(AllocAlignmentInteger)) == 0), ValidationHeader_must_produce_correct_alignment);
  174. #endif
  175. NO_RETURN_DUE_TO_CRASH void fastMallocMatchFailed(void*)
  176. {
  177. CRASH();
  178. }
  179. } // namespace Internal
  180. void* fastZeroedMalloc(size_t n)
  181. {
  182. void* result = fastMalloc(n);
  183. memset(result, 0, n);
  184. return result;
  185. }
  186. char* fastStrDup(const char* src)
  187. {
  188. size_t len = strlen(src) + 1;
  189. char* dup = static_cast<char*>(fastMalloc(len));
  190. memcpy(dup, src, len);
  191. return dup;
  192. }
  193. TryMallocReturnValue tryFastZeroedMalloc(size_t n)
  194. {
  195. void* result;
  196. if (!tryFastMalloc(n).getValue(result))
  197. return 0;
  198. memset(result, 0, n);
  199. return result;
  200. }
  201. } // namespace WTF
  202. #if JITBRIDGE_ENABLE_DEBUG_ARENA
  203. namespace WTF {
  204. void fastFree(void * p)
  205. {
  206. ASSERT(!isForbidden());
  207. #if BUILDING_DETACHED_JIT
  208. JITDebugCompilerMemory::shared_free(p);
  209. #else
  210. JITDebugVMMemory::shared_free(p);
  211. #endif
  212. }
  213. void * fastMalloc(size_t size)
  214. {
  215. ASSERT(!isForbidden());
  216. #if BUILDING_DETACHED_JIT
  217. void * memory(JITDebugCompilerMemory::shared_malloc(size));
  218. return memory;
  219. #else
  220. void * memory(JITDebugVMMemory::shared_malloc(size));
  221. return memory;
  222. #endif
  223. }
  224. void * fastCalloc(size_t n_elements, size_t element_size)
  225. {
  226. ASSERT(!isForbidden());
  227. #if BUILDING_DETACHED_JIT
  228. void * memory(JITDebugCompilerMemory::shared_calloc(n_elements, element_size));
  229. return memory;
  230. #else
  231. void * memory(JITDebugVMMemory::shared_calloc(n_elements, element_size));
  232. return memory;
  233. #endif
  234. }
  235. void * fastRealloc(void * p, size_t size)
  236. {
  237. ASSERT(!isForbidden());
  238. #if BUILDING_DETACHED_JIT
  239. void * memory(JITDebugCompilerMemory::shared_realloc(p, size));
  240. return memory;
  241. #else
  242. void * memory(JITDebugVMMemory::shared_realloc(p, size));
  243. return memory;
  244. #endif
  245. }
  246. TryMallocReturnValue tryFastMalloc(size_t size)
  247. {
  248. ASSERT(!isForbidden());
  249. #if BUILDING_DETACHED_JIT
  250. void * memory(JITDebugCompilerMemory::shared_malloc(size));
  251. return memory;
  252. #else
  253. void * memory(JITDebugVMMemory::shared_malloc(size));
  254. return memory;
  255. #endif
  256. }
  257. TryMallocReturnValue tryFastRealloc(void* p, size_t size)
  258. {
  259. ASSERT(!isForbidden());
  260. #if BUILDING_DETACHED_JIT
  261. void * memory(JITDebugCompilerMemory::shared_realloc(p, size));
  262. return memory;
  263. #else
  264. void * memory(JITDebugVMMemory::shared_realloc(p, size));
  265. return memory;
  266. #endif
  267. }
  268. TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size)
  269. {
  270. ASSERT(!isForbidden());
  271. #if BUILDING_DETACHED_JIT
  272. void * memory(JITDebugCompilerMemory::shared_calloc(n_elements, element_size));
  273. return memory;
  274. #else
  275. void * memory(JITDebugVMMemory::shared_calloc(n_elements, element_size));
  276. return memory;
  277. #endif
  278. }
  279. size_t fastMallocSize(const void* p)
  280. {
  281. #if BUILDING_DETACHED_JIT
  282. return JITDebugCompilerMemory::shared_malloc_usable_size(const_cast<void*>(p));
  283. #else
  284. return JITDebugVMMemory::shared_malloc_usable_size(const_cast<void*>(p));
  285. #endif
  286. }
  287. FastMallocStatistics fastMallocStatistics()
  288. {
  289. FastMallocStatistics statistics = { 0, 0, 0 };
  290. return statistics;
  291. }
  292. void fastMallocShutdown() {}
  293. size_t fastMallocGoodSize(size_t bytes)
  294. {
  295. return bytes;
  296. }
  297. } // namespace WTF
  298. #else // #if JITBRIDGE_ENABLE_DEBUG_ARENA
  299. #if FORCE_SYSTEM_MALLOC
  300. #if OS(WINDOWS)
  301. #include <malloc.h>
  302. #endif
  303. namespace WTF {
  304. size_t fastMallocGoodSize(size_t bytes)
  305. {
  306. #if OS(DARWIN)
  307. return malloc_good_size(bytes);
  308. #else
  309. return bytes;
  310. #endif
  311. }
  312. TryMallocReturnValue tryFastMalloc(size_t n)
  313. {
  314. ASSERT(!isForbidden());
  315. #if ENABLE(WTF_MALLOC_VALIDATION)
  316. if (std::numeric_limits<size_t>::max() - Internal::ValidationBufferSize <= n) // If overflow would occur...
  317. return 0;
  318. void* result = malloc(n + Internal::ValidationBufferSize);
  319. if (!result)
  320. return 0;
  321. Internal::ValidationHeader* header = static_cast<Internal::ValidationHeader*>(result);
  322. header->m_size = n;
  323. header->m_type = Internal::AllocTypeMalloc;
  324. header->m_prefix = static_cast<unsigned>(Internal::ValidationPrefix);
  325. result = header + 1;
  326. *Internal::fastMallocValidationSuffix(result) = Internal::ValidationSuffix;
  327. fastMallocValidate(result);
  328. return result;
  329. #else
  330. return malloc(n);
  331. #endif
  332. }
  333. void* fastMalloc(size_t n)
  334. {
  335. ASSERT(!isForbidden());
  336. #if ENABLE(WTF_MALLOC_VALIDATION)
  337. TryMallocReturnValue returnValue = tryFastMalloc(n);
  338. void* result;
  339. if (!returnValue.getValue(result))
  340. CRASH();
  341. #else
  342. void* result = malloc(n);
  343. #endif
  344. if (!result)
  345. CRASH();
  346. return result;
  347. }
  348. TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size)
  349. {
  350. ASSERT(!isForbidden());
  351. #if ENABLE(WTF_MALLOC_VALIDATION)
  352. size_t totalBytes = n_elements * element_size;
  353. if (n_elements > 1 && element_size && (totalBytes / element_size) != n_elements)
  354. return 0;
  355. TryMallocReturnValue returnValue = tryFastMalloc(totalBytes);
  356. void* result;
  357. if (!returnValue.getValue(result))
  358. return 0;
  359. memset(result, 0, totalBytes);
  360. fastMallocValidate(result);
  361. return result;
  362. #else
  363. return calloc(n_elements, element_size);
  364. #endif
  365. }
  366. void* fastCalloc(size_t n_elements, size_t element_size)
  367. {
  368. ASSERT(!isForbidden());
  369. #if ENABLE(WTF_MALLOC_VALIDATION)
  370. TryMallocReturnValue returnValue = tryFastCalloc(n_elements, element_size);
  371. void* result;
  372. if (!returnValue.getValue(result))
  373. CRASH();
  374. #else
  375. void* result = calloc(n_elements, element_size);
  376. #endif
  377. if (!result)
  378. CRASH();
  379. return result;
  380. }
  381. void fastFree(void* p)
  382. {
  383. ASSERT(!isForbidden());
  384. #if ENABLE(WTF_MALLOC_VALIDATION)
  385. if (!p)
  386. return;
  387. fastMallocMatchValidateFree(p, Internal::AllocTypeMalloc);
  388. Internal::ValidationHeader* header = Internal::fastMallocValidationHeader(p);
  389. memset(p, 0xCC, header->m_size);
  390. free(header);
  391. #else
  392. free(p);
  393. #endif
  394. }
  395. TryMallocReturnValue tryFastRealloc(void* p, size_t n)
  396. {
  397. ASSERT(!isForbidden());
  398. #if ENABLE(WTF_MALLOC_VALIDATION)
  399. if (p) {
  400. if (std::numeric_limits<size_t>::max() - Internal::ValidationBufferSize <= n) // If overflow would occur...
  401. return 0;
  402. fastMallocValidate(p);
  403. Internal::ValidationHeader* result = static_cast<Internal::ValidationHeader*>(realloc(Internal::fastMallocValidationHeader(p), n + Internal::ValidationBufferSize));
  404. if (!result)
  405. return 0;
  406. result->m_size = n;
  407. result = result + 1;
  408. *fastMallocValidationSuffix(result) = Internal::ValidationSuffix;
  409. fastMallocValidate(result);
  410. return result;
  411. } else {
  412. return fastMalloc(n);
  413. }
  414. #else
  415. return realloc(p, n);
  416. #endif
  417. }
  418. void* fastRealloc(void* p, size_t n)
  419. {
  420. ASSERT(!isForbidden());
  421. #if ENABLE(WTF_MALLOC_VALIDATION)
  422. TryMallocReturnValue returnValue = tryFastRealloc(p, n);
  423. void* result;
  424. if (!returnValue.getValue(result))
  425. CRASH();
  426. #else
  427. void* result = realloc(p, n);
  428. #endif
  429. if (!result)
  430. CRASH();
  431. return result;
  432. }
  433. void releaseFastMallocFreeMemory() { }
  434. FastMallocStatistics fastMallocStatistics()
  435. {
  436. FastMallocStatistics statistics = { 0, 0, 0 };
  437. return statistics;
  438. }
  439. size_t fastMallocSize(const void* p)
  440. {
  441. #if ENABLE(WTF_MALLOC_VALIDATION)
  442. return Internal::fastMallocValidationHeader(const_cast<void*>(p))->m_size;
  443. #elif OS(DARWIN)
  444. return malloc_size(p);
  445. #elif OS(WINDOWS)
  446. return _msize(const_cast<void*>(p));
  447. #else
  448. UNUSED_PARAM(p);
  449. return 1;
  450. #endif
  451. }
  452. } // namespace WTF
  453. #if OS(DARWIN)
  454. // This symbol is present in the JavaScriptCore exports file even when FastMalloc is disabled.
  455. // It will never be used in this case, so it's type and value are less interesting than its presence.
  456. extern "C" WTF_EXPORT_PRIVATE const int jscore_fastmalloc_introspection = 0;
  457. #endif
  458. #else // FORCE_SYSTEM_MALLOC
  459. #include "TCPackedCache.h"
  460. #include "TCPageMap.h"
  461. #include "TCSpinLock.h"
  462. #include "TCSystemAlloc.h"
  463. #include "ThreadSpecific.h"
  464. #include <algorithm>
  465. #if USE(PTHREADS)
  466. #include <pthread.h>
  467. #endif
  468. #include <stdarg.h>
  469. #include <stddef.h>
  470. #include <stdint.h>
  471. #include <stdio.h>
  472. #if HAVE(ERRNO_H)
  473. #include <errno.h>
  474. #endif
  475. #if OS(UNIX)
  476. #include <unistd.h>
  477. #endif
  478. #if OS(WINDOWS)
  479. #ifndef WIN32_LEAN_AND_MEAN
  480. #define WIN32_LEAN_AND_MEAN
  481. #endif
  482. #include <windows.h>
  483. #endif
  484. #ifdef WTF_CHANGES
  485. #if OS(DARWIN)
  486. #include <wtf/HashSet.h>
  487. #include <wtf/Vector.h>
  488. #endif
  489. #if HAVE(DISPATCH_H)
  490. #include <dispatch/dispatch.h>
  491. #endif
  492. #ifdef __has_include
  493. #if __has_include(<System/pthread_machdep.h>)
  494. #include <System/pthread_machdep.h>
  495. #if defined(__PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0)
  496. #define WTF_USE_PTHREAD_GETSPECIFIC_DIRECT 1
  497. #endif
  498. #endif
  499. #endif
  500. #ifndef PRIuS
  501. #define PRIuS "zu"
  502. #endif
  503. // Calling pthread_getspecific through a global function pointer is faster than a normal
  504. // call to the function on Mac OS X, and it's used in performance-critical code. So we
  505. // use a function pointer. But that's not necessarily faster on other platforms, and we had
  506. // problems with this technique on Windows, so we'll do this only on Mac OS X.
  507. #if OS(DARWIN)
  508. #if !USE(PTHREAD_GETSPECIFIC_DIRECT)
  509. static void* (*pthread_getspecific_function_pointer)(pthread_key_t) = pthread_getspecific;
  510. #define pthread_getspecific(key) pthread_getspecific_function_pointer(key)
  511. #else
  512. #define pthread_getspecific(key) _pthread_getspecific_direct(key)
  513. #define pthread_setspecific(key, val) _pthread_setspecific_direct(key, (val))
  514. #endif
  515. #endif
  516. #define DEFINE_VARIABLE(type, name, value, meaning) \
  517. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \
  518. type FLAGS_##name(value); \
  519. char FLAGS_no##name; \
  520. } \
  521. using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
  522. #define DEFINE_int64(name, value, meaning) \
  523. DEFINE_VARIABLE(int64_t, name, value, meaning)
  524. #define DEFINE_double(name, value, meaning) \
  525. DEFINE_VARIABLE(double, name, value, meaning)
  526. namespace WTF {
  527. #define malloc fastMalloc
  528. #define calloc fastCalloc
  529. #define free fastFree
  530. #define realloc fastRealloc
  531. #define MESSAGE LOG_ERROR
  532. #define CHECK_CONDITION ASSERT
  533. static const char kLLHardeningMask = 0;
  534. template <unsigned> struct EntropySource;
  535. template <> struct EntropySource<4> {
  536. static uint32_t value()
  537. {
  538. #if OS(DARWIN)
  539. return arc4random();
  540. #else
  541. return static_cast<uint32_t>(static_cast<uintptr_t>(currentTime() * 10000) ^ reinterpret_cast<uintptr_t>(&kLLHardeningMask));
  542. #endif
  543. }
  544. };
  545. template <> struct EntropySource<8> {
  546. static uint64_t value()
  547. {
  548. return EntropySource<4>::value() | (static_cast<uint64_t>(EntropySource<4>::value()) << 32);
  549. }
  550. };
  551. #if ENABLE(TCMALLOC_HARDENING)
  552. /*
  553. * To make it harder to exploit use-after free style exploits
  554. * we mask the addresses we put into our linked lists with the
  555. * address of kLLHardeningMask. Due to ASLR the address of
  556. * kLLHardeningMask should be sufficiently randomized to make direct
  557. * freelist manipulation much more difficult.
  558. */
  559. enum {
  560. MaskKeyShift = 13
  561. };
  562. static ALWAYS_INLINE uintptr_t internalEntropyValue()
  563. {
  564. static uintptr_t value = EntropySource<sizeof(uintptr_t)>::value() | 1;
  565. ASSERT(value);
  566. return value;
  567. }
  568. #define HARDENING_ENTROPY internalEntropyValue()
  569. #define ROTATE_VALUE(value, amount) (((value) >> (amount)) | ((value) << (sizeof(value) * 8 - (amount))))
  570. #define XOR_MASK_PTR_WITH_KEY(ptr, key, entropy) (reinterpret_cast<__typeof__(ptr)>(reinterpret_cast<uintptr_t>(ptr)^(ROTATE_VALUE(reinterpret_cast<uintptr_t>(key), MaskKeyShift)^entropy)))
  571. static ALWAYS_INLINE uint32_t freedObjectStartPoison()
  572. {
  573. static uint32_t value = EntropySource<sizeof(uint32_t)>::value() | 1;
  574. ASSERT(value);
  575. return value;
  576. }
  577. static ALWAYS_INLINE uint32_t freedObjectEndPoison()
  578. {
  579. static uint32_t value = EntropySource<sizeof(uint32_t)>::value() | 1;
  580. ASSERT(value);
  581. return value;
  582. }
  583. #define PTR_TO_UINT32(ptr) static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr))
  584. #define END_POISON_INDEX(allocationSize) (((allocationSize) - sizeof(uint32_t)) / sizeof(uint32_t))
  585. #define POISON_ALLOCATION(allocation, allocationSize) do { \
  586. ASSERT((allocationSize) >= 2 * sizeof(uint32_t)); \
  587. reinterpret_cast<uint32_t*>(allocation)[0] = 0xbadbeef1; \
  588. reinterpret_cast<uint32_t*>(allocation)[1] = 0xbadbeef3; \
  589. if ((allocationSize) < 4 * sizeof(uint32_t)) \
  590. break; \
  591. reinterpret_cast<uint32_t*>(allocation)[2] = 0xbadbeef5; \
  592. reinterpret_cast<uint32_t*>(allocation)[END_POISON_INDEX(allocationSize)] = 0xbadbeef7; \
  593. } while (false);
  594. #define POISON_DEALLOCATION_EXPLICIT(allocation, allocationSize, startPoison, endPoison) do { \
  595. ASSERT((allocationSize) >= 2 * sizeof(uint32_t)); \
  596. reinterpret_cast<uint32_t*>(allocation)[0] = 0xbadbeef9; \
  597. reinterpret_cast<uint32_t*>(allocation)[1] = 0xbadbeefb; \
  598. if ((allocationSize) < 4 * sizeof(uint32_t)) \
  599. break; \
  600. reinterpret_cast<uint32_t*>(allocation)[2] = (startPoison) ^ PTR_TO_UINT32(allocation); \
  601. reinterpret_cast<uint32_t*>(allocation)[END_POISON_INDEX(allocationSize)] = (endPoison) ^ PTR_TO_UINT32(allocation); \
  602. } while (false)
  603. #define POISON_DEALLOCATION(allocation, allocationSize) \
  604. POISON_DEALLOCATION_EXPLICIT(allocation, (allocationSize), freedObjectStartPoison(), freedObjectEndPoison())
  605. #define MAY_BE_POISONED(allocation, allocationSize) (((allocationSize) >= 4 * sizeof(uint32_t)) && ( \
  606. (reinterpret_cast<uint32_t*>(allocation)[2] == (freedObjectStartPoison() ^ PTR_TO_UINT32(allocation))) || \
  607. (reinterpret_cast<uint32_t*>(allocation)[END_POISON_INDEX(allocationSize)] == (freedObjectEndPoison() ^ PTR_TO_UINT32(allocation))) \
  608. ))
  609. #define IS_DEFINITELY_POISONED(allocation, allocationSize) (((allocationSize) < 4 * sizeof(uint32_t)) || ( \
  610. (reinterpret_cast<uint32_t*>(allocation)[2] == (freedObjectStartPoison() ^ PTR_TO_UINT32(allocation))) && \
  611. (reinterpret_cast<uint32_t*>(allocation)[END_POISON_INDEX(allocationSize)] == (freedObjectEndPoison() ^ PTR_TO_UINT32(allocation))) \
  612. ))
  613. #else
  614. #define POISON_ALLOCATION(allocation, allocationSize)
  615. #define POISON_DEALLOCATION(allocation, allocationSize)
  616. #define POISON_DEALLOCATION_EXPLICIT(allocation, allocationSize, startPoison, endPoison)
  617. #define MAY_BE_POISONED(allocation, allocationSize) (false)
  618. #define IS_DEFINITELY_POISONED(allocation, allocationSize) (true)
  619. #define XOR_MASK_PTR_WITH_KEY(ptr, key, entropy) (((void)entropy), ((void)key), ptr)
  620. #define HARDENING_ENTROPY 0
  621. #endif
  622. //-------------------------------------------------------------------
  623. // Configuration
  624. //-------------------------------------------------------------------
  625. // Not all possible combinations of the following parameters make
  626. // sense. In particular, if kMaxSize increases, you may have to
  627. // increase kNumClasses as well.
  628. #if OS(DARWIN)
  629. # define K_PAGE_SHIFT PAGE_SHIFT
  630. # if (K_PAGE_SHIFT == 12)
  631. # define K_NUM_CLASSES 68
  632. # elif (K_PAGE_SHIFT == 14)
  633. # define K_NUM_CLASSES 77
  634. # else
  635. # error "Unsupported PAGE_SHIFT amount"
  636. # endif
  637. #else
  638. # define K_PAGE_SHIFT 12
  639. # define K_NUM_CLASSES 68
  640. #endif
  641. static const size_t kPageShift = K_PAGE_SHIFT;
  642. static const size_t kPageSize = 1 << kPageShift;
  643. static const size_t kMaxSize = 32u * 1024;
  644. static const size_t kAlignShift = 3;
  645. static const size_t kAlignment = 1 << kAlignShift;
  646. static const size_t kNumClasses = K_NUM_CLASSES;
  647. // Allocates a big block of memory for the pagemap once we reach more than
  648. // 128MB
  649. static const size_t kPageMapBigAllocationThreshold = 128 << 20;
  650. // Minimum number of pages to fetch from system at a time. Must be
  651. // significantly bigger than kPageSize to amortize system-call
  652. // overhead, and also to reduce external fragementation. Also, we
  653. // should keep this value big because various incarnations of Linux
  654. // have small limits on the number of mmap() regions per
  655. // address-space.
  656. static const size_t kMinSystemAlloc = 1 << (20 - kPageShift);
  657. // Number of objects to move between a per-thread list and a central
  658. // list in one shot. We want this to be not too small so we can
  659. // amortize the lock overhead for accessing the central list. Making
  660. // it too big may temporarily cause unnecessary memory wastage in the
  661. // per-thread free list until the scavenger cleans up the list.
  662. static int num_objects_to_move[kNumClasses];
  663. // Maximum length we allow a per-thread free-list to have before we
  664. // move objects from it into the corresponding central free-list. We
  665. // want this big to avoid locking the central free-list too often. It
  666. // should not hurt to make this list somewhat big because the
  667. // scavenging code will shrink it down when its contents are not in use.
  668. static const int kMaxFreeListLength = 256;
  669. // Lower and upper bounds on the per-thread cache sizes
  670. static const size_t kMinThreadCacheSize = kMaxSize * 2;
  671. #if PLATFORM(IOS)
  672. static const size_t kMaxThreadCacheSize = 512 * 1024;
  673. #else
  674. static const size_t kMaxThreadCacheSize = 2 << 20;
  675. #endif
  676. // Default bound on the total amount of thread caches
  677. static const size_t kDefaultOverallThreadCacheSize = 16 << 20;
  678. // For all span-lengths < kMaxPages we keep an exact-size list.
  679. // REQUIRED: kMaxPages >= kMinSystemAlloc;
  680. static const size_t kMaxPages = kMinSystemAlloc;
  681. /* The smallest prime > 2^n */
  682. static int primes_list[] = {
  683. // Small values might cause high rates of sampling
  684. // and hence commented out.
  685. // 2, 5, 11, 17, 37, 67, 131, 257,
  686. // 521, 1031, 2053, 4099, 8209, 16411,
  687. 32771, 65537, 131101, 262147, 524309, 1048583,
  688. 2097169, 4194319, 8388617, 16777259, 33554467 };
  689. // Twice the approximate gap between sampling actions.
  690. // I.e., we take one sample approximately once every
  691. // tcmalloc_sample_parameter/2
  692. // bytes of allocation, i.e., ~ once every 128KB.
  693. // Must be a prime number.
  694. #ifdef NO_TCMALLOC_SAMPLES
  695. DEFINE_int64(tcmalloc_sample_parameter, 0,
  696. "Unused: code is compiled with NO_TCMALLOC_SAMPLES");
  697. static size_t sample_period = 0;
  698. #else
  699. DEFINE_int64(tcmalloc_sample_parameter, 262147,
  700. "Twice the approximate gap between sampling actions."
  701. " Must be a prime number. Otherwise will be rounded up to a "
  702. " larger prime number");
  703. static size_t sample_period = 262147;
  704. #endif
  705. // Protects sample_period above
  706. static SpinLock sample_period_lock = SPINLOCK_INITIALIZER;
  707. // Parameters for controlling how fast memory is returned to the OS.
  708. DEFINE_double(tcmalloc_release_rate, 1,
  709. "Rate at which we release unused memory to the system. "
  710. "Zero means we never release memory back to the system. "
  711. "Increase this flag to return memory faster; decrease it "
  712. "to return memory slower. Reasonable rates are in the "
  713. "range [0,10]");
  714. //-------------------------------------------------------------------
  715. // Mapping from size to size_class and vice versa
  716. //-------------------------------------------------------------------
  717. // Sizes <= 1024 have an alignment >= 8. So for such sizes we have an
  718. // array indexed by ceil(size/8). Sizes > 1024 have an alignment >= 128.
  719. // So for these larger sizes we have an array indexed by ceil(size/128).
  720. //
  721. // We flatten both logical arrays into one physical array and use
  722. // arithmetic to compute an appropriate index. The constants used by
  723. // ClassIndex() were selected to make the flattening work.
  724. //
  725. // Examples:
  726. // Size Expression Index
  727. // -------------------------------------------------------
  728. // 0 (0 + 7) / 8 0
  729. // 1 (1 + 7) / 8 1
  730. // ...
  731. // 1024 (1024 + 7) / 8 128
  732. // 1025 (1025 + 127 + (120<<7)) / 128 129
  733. // ...
  734. // 32768 (32768 + 127 + (120<<7)) / 128 376
  735. static const size_t kMaxSmallSize = 1024;
  736. static const int shift_amount[2] = { 3, 7 }; // For divides by 8 or 128
  737. static const int add_amount[2] = { 7, 127 + (120 << 7) };
  738. static unsigned char class_array[377];
  739. // Compute index of the class_array[] entry for a given size
  740. static inline int ClassIndex(size_t s) {
  741. const int i = (s > kMaxSmallSize);
  742. return static_cast<int>((s + add_amount[i]) >> shift_amount[i]);
  743. }
  744. // Mapping from size class to max size storable in that class
  745. static size_t class_to_size[kNumClasses];
  746. // Mapping from size class to number of pages to allocate at a time
  747. static size_t class_to_pages[kNumClasses];
  748. // Hardened singly linked list. We make this a class to allow compiler to
  749. // statically prevent mismatching hardened and non-hardened list
  750. class HardenedSLL {
  751. public:
  752. static ALWAYS_INLINE HardenedSLL create(void* value)
  753. {
  754. HardenedSLL result;
  755. result.m_value = value;
  756. return result;
  757. }
  758. static ALWAYS_INLINE HardenedSLL null()
  759. {
  760. HardenedSLL result;
  761. result.m_value = 0;
  762. return result;
  763. }
  764. ALWAYS_INLINE void setValue(void* value) { m_value = value; }
  765. ALWAYS_INLINE void* value() const { return m_value; }
  766. ALWAYS_INLINE bool operator!() const { return !m_value; }
  767. typedef void* (HardenedSLL::*UnspecifiedBoolType);
  768. ALWAYS_INLINE operator UnspecifiedBoolType() const { return m_value ? &HardenedSLL::m_value : 0; }
  769. bool operator!=(const HardenedSLL& other) const { return m_value != other.m_value; }
  770. bool operator==(const HardenedSLL& other) const { return m_value == other.m_value; }
  771. private:
  772. void* m_value;
  773. };
  774. // TransferCache is used to cache transfers of num_objects_to_move[size_class]
  775. // back and forth between thread caches and the central cache for a given size
  776. // class.
  777. struct TCEntry {
  778. HardenedSLL head; // Head of chain of objects.
  779. HardenedSLL tail; // Tail of chain of objects.
  780. };
  781. // A central cache freelist can have anywhere from 0 to kNumTransferEntries
  782. // slots to put link list chains into. To keep memory usage bounded the total
  783. // number of TCEntries across size classes is fixed. Currently each size
  784. // class is initially given one TCEntry which also means that the maximum any
  785. // one class can have is kNumClasses.
  786. static const int kNumTransferEntries = kNumClasses;
  787. // Note: the following only works for "n"s that fit in 32-bits, but
  788. // that is fine since we only use it for small sizes.
  789. static inline int LgFloor(size_t n) {
  790. int log = 0;
  791. for (int i = 4; i >= 0; --i) {
  792. int shift = (1 << i);
  793. size_t x = n >> shift;
  794. if (x != 0) {
  795. n = x;
  796. log += shift;
  797. }
  798. }
  799. ASSERT(n == 1);
  800. return log;
  801. }
  802. // Functions for using our simple hardened singly linked list
  803. static ALWAYS_INLINE HardenedSLL SLL_Next(HardenedSLL t, uintptr_t entropy) {
  804. return HardenedSLL::create(XOR_MASK_PTR_WITH_KEY(*(reinterpret_cast<void**>(t.value())), t.value(), entropy));
  805. }
  806. static ALWAYS_INLINE void SLL_SetNext(HardenedSLL t, HardenedSLL n, uintptr_t entropy) {
  807. *(reinterpret_cast<void**>(t.value())) = XOR_MASK_PTR_WITH_KEY(n.value(), t.value(), entropy);
  808. }
  809. static ALWAYS_INLINE void SLL_Push(HardenedSLL* list, HardenedSLL element, uintptr_t entropy) {
  810. SLL_SetNext(element, *list, entropy);
  811. *list = element;
  812. }
  813. static ALWAYS_INLINE HardenedSLL SLL_Pop(HardenedSLL *list, uintptr_t entropy) {
  814. HardenedSLL result = *list;
  815. *list = SLL_Next(*list, entropy);
  816. return result;
  817. }
  818. // Remove N elements from a linked list to which head points. head will be
  819. // modified to point to the new head. start and end will point to the first
  820. // and last nodes of the range. Note that end will point to NULL after this
  821. // function is called.
  822. static ALWAYS_INLINE void SLL_PopRange(HardenedSLL* head, int N, HardenedSLL *start, HardenedSLL *end, uintptr_t entropy) {
  823. if (N == 0) {
  824. *start = HardenedSLL::null();
  825. *end = HardenedSLL::null();
  826. return;
  827. }
  828. HardenedSLL tmp = *head;
  829. for (int i = 1; i < N; ++i) {
  830. tmp = SLL_Next(tmp, entropy);
  831. }
  832. *start = *head;
  833. *end = tmp;
  834. *head = SLL_Next(tmp, entropy);
  835. // Unlink range from list.
  836. SLL_SetNext(tmp, HardenedSLL::null(), entropy);
  837. }
  838. static ALWAYS_INLINE void SLL_PushRange(HardenedSLL *head, HardenedSLL start, HardenedSLL end, uintptr_t entropy) {
  839. if (!start) return;
  840. SLL_SetNext(end, *head, entropy);
  841. *head = start;
  842. }
  843. // Setup helper functions.
  844. static ALWAYS_INLINE size_t SizeClass(size_t size) {
  845. return class_array[ClassIndex(size)];
  846. }
  847. // Get the byte-size for a specified class
  848. static ALWAYS_INLINE size_t ByteSizeForClass(size_t cl) {
  849. return class_to_size[cl];
  850. }
  851. static int NumMoveSize(size_t size) {
  852. if (size == 0) return 0;
  853. // Use approx 64k transfers between thread and central caches.
  854. int num = static_cast<int>(64.0 * 1024.0 / size);
  855. if (num < 2) num = 2;
  856. // Clamp well below kMaxFreeListLength to avoid ping pong between central
  857. // and thread caches.
  858. if (num > static_cast<int>(0.8 * kMaxFreeListLength))
  859. num = static_cast<int>(0.8 * kMaxFreeListLength);
  860. // Also, avoid bringing in too many objects into small object free
  861. // lists. There are lots of such lists, and if we allow each one to
  862. // fetch too many at a time, we end up having to scavenge too often
  863. // (especially when there are lots of threads and each thread gets a
  864. // small allowance for its thread cache).
  865. //
  866. // TODO: Make thread cache free list sizes dynamic so that we do not
  867. // have to equally divide a fixed resource amongst lots of threads.
  868. if (num > 32) num = 32;
  869. return num;
  870. }
  871. // Initialize the mapping arrays
  872. static void InitSizeClasses() {
  873. // Do some sanity checking on add_amount[]/shift_amount[]/class_array[]
  874. if (ClassIndex(0) < 0) {
  875. MESSAGE("Invalid class index %d for size 0\n", ClassIndex(0));
  876. CRASH();
  877. }
  878. if (static_cast<size_t>(ClassIndex(kMaxSize)) >= sizeof(class_array)) {
  879. MESSAGE("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize));
  880. CRASH();
  881. }
  882. // Compute the size classes we want to use
  883. size_t sc = 1; // Next size class to assign
  884. unsigned char alignshift = kAlignShift;
  885. int last_lg = -1;
  886. for (size_t size = kAlignment; size <= kMaxSize; size += (1 << alignshift)) {
  887. int lg = LgFloor(size);
  888. if (lg > last_lg) {
  889. // Increase alignment every so often.
  890. //
  891. // Since we double the alignment every time size doubles and
  892. // size >= 128, this means that space wasted due to alignment is
  893. // at most 16/128 i.e., 12.5%. Plus we cap the alignment at 256
  894. // bytes, so the space wasted as a percentage starts falling for
  895. // sizes > 2K.
  896. if ((lg >= 7) && (alignshift < 8)) {
  897. alignshift++;
  898. }
  899. last_lg = lg;
  900. }
  901. // Allocate enough pages so leftover is less than 1/8 of total.
  902. // This bounds wasted space to at most 12.5%.
  903. size_t psize = kPageSize;
  904. while ((psize % size) > (psize >> 3)) {
  905. psize += kPageSize;
  906. }
  907. const size_t my_pages = psize >> kPageShift;
  908. if (sc > 1 && my_pages == class_to_pages[sc-1]) {
  909. // See if we can merge this into the previous class without
  910. // increasing the fragmentation of the previous class.
  911. const size_t my_objects = (my_pages << kPageShift) / size;
  912. const size_t prev_objects = (class_to_pages[sc-1] << kPageShift)
  913. / class_to_size[sc-1];
  914. if (my_objects == prev_objects) {
  915. // Adjust last class to include this size
  916. class_to_size[sc-1] = size;
  917. continue;
  918. }
  919. }
  920. // Add new class
  921. class_to_pages[sc] = my_pages;
  922. class_to_size[sc] = size;
  923. sc++;
  924. }
  925. if (sc != kNumClasses) {
  926. MESSAGE("wrong number of size classes: found %" PRIuS " instead of %d\n",
  927. sc, int(kNumClasses));
  928. CRASH();
  929. }
  930. // Initialize the mapping arrays
  931. int next_size = 0;
  932. for (unsigned char c = 1; c < kNumClasses; c++) {
  933. const size_t max_size_in_class = class_to_size[c];
  934. for (size_t s = next_size; s <= max_size_in_class; s += kAlignment) {
  935. class_array[ClassIndex(s)] = c;
  936. }
  937. next_size = static_cast<int>(max_size_in_class + kAlignment);
  938. }
  939. // Double-check sizes just to be safe
  940. for (size_t size = 0; size <= kMaxSize; size++) {
  941. const size_t sc = SizeClass(size);
  942. if (sc == 0) {
  943. MESSAGE("Bad size class %" PRIuS " for %" PRIuS "\n", sc, size);
  944. CRASH();
  945. }
  946. if (sc > 1 && size <= class_to_size[sc-1]) {
  947. MESSAGE("Allocating unnecessarily large class %" PRIuS " for %" PRIuS
  948. "\n", sc, size);
  949. CRASH();
  950. }
  951. if (sc >= kNumClasses) {
  952. MESSAGE("Bad size class %" PRIuS " for %" PRIuS "\n", sc, size);
  953. CRASH();
  954. }
  955. const size_t s = class_to_size[sc];
  956. if (size > s) {
  957. MESSAGE("Bad size %" PRIuS " for %" PRIuS " (sc = %" PRIuS ")\n", s, size, sc);
  958. CRASH();
  959. }
  960. if (s == 0) {
  961. MESSAGE("Bad size %" PRIuS " for %" PRIuS " (sc = %" PRIuS ")\n", s, size, sc);
  962. CRASH();
  963. }
  964. }
  965. // Initialize the num_objects_to_move array.
  966. for (size_t cl = 1; cl < kNumClasses; ++cl) {
  967. num_objects_to_move[cl] = NumMoveSize(ByteSizeForClass(cl));
  968. }
  969. #ifndef WTF_CHANGES
  970. if (false) {
  971. // Dump class sizes and maximum external wastage per size class
  972. for (size_t cl = 1; cl < kNumClasses; ++cl) {
  973. const int alloc_size = class_to_pages[cl] << kPageShift;
  974. const int alloc_objs = alloc_size / class_to_size[cl];
  975. const int min_used = (class_to_size[cl-1] + 1) * alloc_objs;
  976. const int max_waste = alloc_size - min_used;
  977. MESSAGE("SC %3d [ %8d .. %8d ] from %8d ; %2.0f%% maxwaste\n",
  978. int(cl),
  979. int(class_to_size[cl-1] + 1),
  980. int(class_to_size[cl]),
  981. int(class_to_pages[cl] << kPageShift),
  982. max_waste * 100.0 / alloc_size
  983. );
  984. }
  985. }
  986. #endif
  987. }
  988. // -------------------------------------------------------------------------
  989. // Simple allocator for objects of a specified type. External locking
  990. // is required before accessing one of these objects.
  991. // -------------------------------------------------------------------------
  992. // Metadata allocator -- keeps stats about how many bytes allocated
  993. static uint64_t metadata_system_bytes = 0;
  994. static void* MetaDataAlloc(size_t bytes) {
  995. void* result = TCMalloc_SystemAlloc(bytes, 0);
  996. if (result != NULL) {
  997. metadata_system_bytes += bytes;
  998. }
  999. return result;
  1000. }
  1001. #if defined(WTF_CHANGES) && OS(DARWIN)
  1002. class RemoteMemoryReader;
  1003. #endif
  1004. template <class T>
  1005. class PageHeapAllocator {
  1006. private:
  1007. // How much to allocate from system at a time
  1008. static const size_t kAllocIncrement = 32 << 10;
  1009. // Aligned size of T
  1010. static const size_t kAlignedSize
  1011. = (((sizeof(T) + kAlignment - 1) / kAlignment) * kAlignment);
  1012. // Free area from which to carve new objects
  1013. char* free_area_;
  1014. size_t free_avail_;
  1015. // Linked list of all regions allocated by this allocator
  1016. HardenedSLL allocated_regions_;
  1017. // Free list of already carved objects
  1018. HardenedSLL free_list_;
  1019. // Number of allocated but unfreed objects
  1020. int inuse_;
  1021. uintptr_t entropy_;
  1022. public:
  1023. void Init(uintptr_t entropy) {
  1024. ASSERT(kAlignedSize <= kAllocIncrement);
  1025. inuse_ = 0;
  1026. allocated_regions_ = HardenedSLL::null();
  1027. free_area_ = NULL;
  1028. free_avail_ = 0;
  1029. free_list_.setValue(NULL);
  1030. entropy_ = entropy;
  1031. }
  1032. T* New() {
  1033. // Consult free list
  1034. void* result;
  1035. if (free_list_) {
  1036. result = free_list_.value();
  1037. free_list_ = SLL_Next(free_list_, entropy_);
  1038. } else {
  1039. if (free_avail_ < kAlignedSize) {
  1040. // Need more room
  1041. char* new_allocation = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement));
  1042. if (!new_allocation)
  1043. CRASH();
  1044. HardenedSLL new_head = HardenedSLL::create(new_allocation);
  1045. SLL_SetNext(new_head, allocated_regions_, entropy_);
  1046. allocated_regions_ = new_head;
  1047. free_area_ = new_allocation + kAlignedSize;
  1048. free_avail_ = kAllocIncrement - kAlignedSize;
  1049. }
  1050. result = free_area_;
  1051. free_area_ += kAlignedSize;
  1052. free_avail_ -= kAlignedSize;
  1053. }
  1054. inuse_++;
  1055. return reinterpret_cast<T*>(result);
  1056. }
  1057. void Delete(T* p) {
  1058. HardenedSLL new_head = HardenedSLL::create(p);
  1059. SLL_SetNext(new_head, free_list_, entropy_);
  1060. free_list_ = new_head;
  1061. inuse_--;
  1062. }
  1063. int inuse() const { return inuse_; }
  1064. #if defined(WTF_CHANGES) && OS(DARWIN)
  1065. template <typename Recorder>
  1066. void recordAdministrativeRegions(Recorder&, const RemoteMemoryReader&);
  1067. #endif
  1068. };
  1069. // -------------------------------------------------------------------------
  1070. // Span - a contiguous run of pages
  1071. // -------------------------------------------------------------------------
  1072. // Type that can hold a page number
  1073. typedef uintptr_t PageID;
  1074. // Type that can hold the length of a run of pages
  1075. typedef uintptr_t Length;
  1076. static const Length kMaxValidPages = (~static_cast<Length>(0)) >> kPageShift;
  1077. // Convert byte size into pages. This won't overflow, but may return
  1078. // an unreasonably large value if bytes is huge enough.
  1079. static inline Length pages(size_t bytes) {
  1080. return (bytes >> kPageShift) +
  1081. ((bytes & (kPageSize - 1)) > 0 ? 1 : 0);
  1082. }
  1083. // Convert a user size into the number of bytes that will actually be
  1084. // allocated
  1085. static size_t AllocationSize(size_t bytes) {
  1086. if (bytes > kMaxSize) {
  1087. // Large object: we allocate an integral number of pages
  1088. ASSERT(bytes <= (kMaxValidPages << kPageShift));
  1089. return pages(bytes) << kPageShift;
  1090. } else {
  1091. // Small object: find the size class to which it belongs
  1092. return ByteSizeForClass(SizeClass(bytes));
  1093. }
  1094. }
  1095. enum {
  1096. kSpanCookieBits = 10,
  1097. kSpanCookieMask = (1 << 10) - 1,
  1098. kSpanThisShift = 7
  1099. };
  1100. static uint32_t spanValidationCookie;
  1101. static uint32_t spanInitializerCookie()
  1102. {
  1103. static uint32_t value = EntropySource<sizeof(uint32_t)>::value() & kSpanCookieMask;
  1104. spanValidationCookie = value;
  1105. return value;
  1106. }
  1107. // Information kept for a span (a contiguous run of pages).
  1108. struct Span {
  1109. PageID start; // Starting page number
  1110. Length length; // Number of pages in span
  1111. Span* next(uintptr_t entropy) const { return XOR_MASK_PTR_WITH_KEY(m_next, this, entropy); }
  1112. Span* remoteNext(const Span* remoteSpanPointer, uintptr_t entropy) const { return XOR_MASK_PTR_WITH_KEY(m_next, remoteSpanPointer, entropy); }
  1113. Span* prev(uintptr_t entropy) const { return XOR_MASK_PTR_WITH_KEY(m_prev, this, entropy); }
  1114. void setNext(Span* next, uintptr_t entropy) { m_next = XOR_MASK_PTR_WITH_KEY(next, this, entropy); }
  1115. void setPrev(Span* prev, uintptr_t entropy) { m_prev = XOR_MASK_PTR_WITH_KEY(prev, this, entropy); }
  1116. private:
  1117. Span* m_next; // Used when in link list
  1118. Span* m_prev; // Used when in link list
  1119. public:
  1120. HardenedSLL objects; // Linked list of free objects
  1121. unsigned int free : 1; // Is the span free
  1122. #ifndef NO_TCMALLOC_SAMPLES
  1123. unsigned int sample : 1; // Sampled object?
  1124. #endif
  1125. unsigned int sizeclass : 8; // Size-class for small objects (or 0)
  1126. unsigned int refcount : 11; // Number of non-free objects
  1127. bool decommitted : 1;
  1128. void initCookie()
  1129. {
  1130. m_cookie = ((reinterpret_cast<uintptr_t>(this) >> kSpanThisShift) & kSpanCookieMask) ^ spanInitializerCookie();
  1131. }
  1132. void clearCookie() { m_cookie = 0; }
  1133. bool isValid() const
  1134. {
  1135. return (((reinterpret_cast<uintptr_t>(this) >> kSpanThisShift) & kSpanCookieMask) ^ m_cookie) == spanValidationCookie;
  1136. }
  1137. private:
  1138. uint32_t m_cookie : kSpanCookieBits;
  1139. #undef SPAN_HISTORY
  1140. #ifdef SPAN_HISTORY
  1141. // For debugging, we can keep a log events per span
  1142. int nexthistory;
  1143. char history[64];
  1144. int value[64];
  1145. #endif
  1146. };
  1147. #define ASSERT_SPAN_COMMITTED(span) ASSERT(!span->decommitted)
  1148. #ifdef SPAN_HISTORY
  1149. void Event(Span* span, char op, int v = 0) {
  1150. span->history[span->nexthistory] = op;
  1151. span->value[span->nexthistory] = v;
  1152. span->nexthistory++;
  1153. if (span->nexthistory == sizeof(span->history)) span->nexthistory = 0;
  1154. }
  1155. #else
  1156. #define Event(s,o,v) ((void) 0)
  1157. #endif
  1158. // Allocator/deallocator for spans
  1159. static PageHeapAllocator<Span> span_allocator;
  1160. static Span* NewSpan(PageID p, Length len) {
  1161. Span* result = span_allocator.New();
  1162. memset(result, 0, sizeof(*result));
  1163. result->start = p;
  1164. result->length = len;
  1165. result->initCookie();
  1166. #ifdef SPAN_HISTORY
  1167. result->nexthistory = 0;
  1168. #endif
  1169. return result;
  1170. }
  1171. static inline void DeleteSpan(Span* span) {
  1172. RELEASE_ASSERT(span->isValid());
  1173. #ifndef NDEBUG
  1174. // In debug mode, trash the contents of deleted Spans
  1175. memset(span, 0x3f, sizeof(*span));
  1176. #endif
  1177. span->clearCookie();
  1178. span_allocator.Delete(span);
  1179. }
  1180. // -------------------------------------------------------------------------
  1181. // Doubly linked list of spans.
  1182. // -------------------------------------------------------------------------
  1183. static inline void DLL_Init(Span* list, uintptr_t entropy) {
  1184. list->setNext(list, entropy);
  1185. list->setPrev(list, entropy);
  1186. }
  1187. static inline void DLL_Remove(Span* span, uintptr_t entropy) {
  1188. span->prev(entropy)->setNext(span->next(entropy), entropy);
  1189. span->next(entropy)->setPrev(span->prev(entropy), entropy);
  1190. span->setPrev(NULL, entropy);
  1191. span->setNext(NULL, entropy);
  1192. }
  1193. static ALWAYS_INLINE bool DLL_IsEmpty(const Span* list, uintptr_t entropy) {
  1194. return list->next(entropy) == list;
  1195. }
  1196. static int DLL_Length(const Span* list, uintptr_t entropy) {
  1197. int result = 0;
  1198. for (Span* s = list->next(entropy); s != list; s = s->next(entropy)) {
  1199. result++;
  1200. }
  1201. return result;
  1202. }
  1203. #if 0 /* Not needed at the moment -- causes compiler warnings if not used */
  1204. static void DLL_Print(const char* label, const Span* list) {
  1205. MESSAGE("%-10s %p:", label, list);
  1206. for (const Span* s = list->next; s != list; s = s->next) {
  1207. MESSAGE(" <%p,%u,%u>", s, s->start, s->length);
  1208. }
  1209. MESSAGE("\n");
  1210. }
  1211. #endif
  1212. static inline void DLL_Prepend(Span* list, Span* span, uintptr_t entropy) {
  1213. span->setNext(list->next(entropy), entropy);
  1214. span->setPrev(list, entropy);
  1215. list->next(entropy)->setPrev(span, entropy);
  1216. list->setNext(span, entropy);
  1217. }
  1218. //-------------------------------------------------------------------
  1219. // Data kept per size-class in central cache
  1220. //-------------------------------------------------------------------
  1221. class TCMalloc_Central_FreeList {
  1222. public:
  1223. void Init(size_t cl, uintptr_t entropy);
  1224. // These methods all do internal locking.
  1225. // Insert the specified range into the central freelist. N is the number of
  1226. // elements in the range.
  1227. void InsertRange(HardenedSLL start, HardenedSLL end, int N);
  1228. // Returns the actual number of fetched elements into N.
  1229. void RemoveRange(HardenedSLL* start, HardenedSLL* end, int *N);
  1230. // Returns the number of free objects in cache.
  1231. size_t length() {
  1232. SpinLockHolder h(&lock_);
  1233. return counter_;
  1234. }
  1235. // Returns the number of free objects in the transfer cache.
  1236. int tc_length() {
  1237. SpinLockHolder h(&lock_);
  1238. return used_slots_ * num_objects_to_move[size_class_];
  1239. }
  1240. #ifdef WTF_CHANGES
  1241. template <class Finder, class Reader>
  1242. void enumerateFreeObjects(Finder& finder, const Reader& reader, TCMalloc_Central_FreeList* remoteCentralFreeList)
  1243. {
  1244. {
  1245. static const ptrdiff_t emptyOffset = reinterpret_cast<const char*>(&empty_) - reinterpret_cast<const char*>(this);
  1246. Span* remoteEmpty = reinterpret_cast<Span*>(reinterpret_cast<char*>(remoteCentralFreeList) + emptyOffset);
  1247. Span* remoteSpan = nonempty_.remoteNext(remoteEmpty, entropy_);
  1248. for (Span* span = reader(remoteEmpty); span && span != &empty_; remoteSpan = span->remoteNext(remoteSpan, entropy_), span = (remoteSpan ? reader(remoteSpan) : 0))
  1249. ASSERT(!span->objects);
  1250. }
  1251. ASSERT(!nonempty_.objects);
  1252. static const ptrdiff_t nonemptyOffset = reinterpret_cast<const char*>(&nonempty_) - reinterpret_cast<const char*>(this);
  1253. Span* remoteNonempty = reinterpret_cast<Span*>(reinterpret_cast<char*>(remoteCentralFreeList) + nonemptyOffset);
  1254. Span* remoteSpan = nonempty_.remoteNext(remoteNonempty, entropy_);
  1255. for (Span* span = reader(remoteSpan); span && remoteSpan != remoteNonempty; remoteSpan = span->remoteNext(remoteSpan, entropy_), span = (remoteSpan ? reader(remoteSpan) : 0)) {
  1256. for (HardenedSLL nextObject = span->objects; nextObject; nextObject.setValue(reader.nextEntryInHardenedLinkedList(reinterpret_cast<void**>(nextObject.value()), entropy_))) {
  1257. finder.visit(nextObject.value());
  1258. }
  1259. }
  1260. for (int slot = 0; slot < used_slots_; ++slot) {
  1261. for (HardenedSLL entry = tc_slots_[slot].head; entry; entry.setValue(reader.nextEntryInHardenedLinkedList(reinterpret_cast<void**>(entry.value()), entropy_)))
  1262. finder.visit(entry.value());
  1263. }
  1264. }
  1265. #endif
  1266. uintptr_t entropy() const { return entropy_; }
  1267. private:
  1268. // REQUIRES: lock_ is held
  1269. // Remove object from cache and return.
  1270. // Return NULL if no free entries in cache.
  1271. HardenedSLL FetchFromSpans();
  1272. // REQUIRES: lock_ is held
  1273. // Remove object from cache and return. Fetches
  1274. // from pageheap if cache is empty. Only returns
  1275. // NULL on allocation failure.
  1276. HardenedSLL FetchFromSpansSafe();
  1277. // REQUIRES: lock_ is held
  1278. // Release a linked list of objects to spans.
  1279. // May temporarily release lock_.
  1280. void ReleaseListToSpans(HardenedSLL start);
  1281. // REQUIRES: lock_ is held
  1282. // Release an object to spans.
  1283. // May temporarily release lock_.
  1284. ALWAYS_INLINE void ReleaseToSpans(HardenedSLL object);
  1285. // REQUIRES: lock_ is held
  1286. // Populate cache by fetching from the page heap.
  1287. // May temporarily release lock_.
  1288. ALWAYS_INLINE void Populate();
  1289. // REQUIRES: lock is held.
  1290. // Tries to make room for a TCEntry. If the cache is full it will try to
  1291. // expand it at the cost of some other cache size. Return false if there is
  1292. // no space.
  1293. bool MakeCacheSpace();
  1294. // REQUIRES: lock_ for locked_size_class is held.
  1295. // Picks a "random" size class to steal TCEntry slot from. In reality it
  1296. // just iterates over the sizeclasses but does so without taking a lock.
  1297. // Returns true on success.
  1298. // May temporarily lock a "random" size class.
  1299. static ALWAYS_INLINE bool EvictRandomSizeClass(size_t locked_size_class, bool force);
  1300. // REQUIRES: lock_ is *not* held.
  1301. // Tries to shrink the Cache. If force is true it will relase objects to
  1302. // spans if it allows it to shrink the cache. Return false if it failed to
  1303. // shrink the cache. Decrements cache_size_ on succeess.
  1304. // May temporarily take lock_. If it takes lock_, the locked_size_class
  1305. // lock is released to the thread from holding two size class locks
  1306. // concurrently which could lead to a deadlock.
  1307. bool ShrinkCache(int locked_size_class, bool force);
  1308. // This lock protects all the data members. cached_entries and cache_size_
  1309. // may be looked at without holding the lock.
  1310. SpinLock lock_;
  1311. // We keep linked lists of empty and non-empty spans.
  1312. size_t size_class_; // My size class
  1313. Span empty_; // Dummy header for list of empty spans
  1314. Span nonempty_; // Dummy header for list of non-empty spans
  1315. size_t counter_; // Number of free objects in cache entry
  1316. // Here we reserve space for TCEntry cache slots. Since one size class can
  1317. // end up getting all the TCEntries quota in the system we just preallocate
  1318. // sufficient number of entries here.
  1319. TCEntry tc_slots_[kNumTransferEntries];
  1320. // Number of currently used cached entries in tc_slots_. This variable is
  1321. // updated under a lock but can be read without one.
  1322. int32_t used_slots_;
  1323. // The current number of slots for this size class. This is an
  1324. // adaptive value that is increased if there is lots of traffic
  1325. // on a given size class.
  1326. int32_t cache_size_;
  1327. uintptr_t entropy_;
  1328. };
  1329. #if COMPILER(CLANG) && defined(__has_warning)
  1330. #pragma clang diagnostic push
  1331. #if __has_warning("-Wunused-private-field")
  1332. #pragma clang diagnostic ignored "-Wunused-private-field"
  1333. #endif
  1334. #endif
  1335. // Pad each CentralCache object to multiple of 64 bytes
  1336. template <size_t SizeToPad>
  1337. class TCMalloc_Central_FreeListPadded_Template : public TCMalloc_Central_FreeList {
  1338. private:
  1339. char pad[64 - SizeToPad];
  1340. };
  1341. // Zero-size specialization to avoid compiler error when TCMalloc_Central_FreeList happens
  1342. // to be exactly 64 bytes.
  1343. template <> class TCMalloc_Central_FreeListPadded_Template<0> : public TCMalloc_Central_FreeList {
  1344. };
  1345. typedef TCMalloc_Central_FreeListPadded_Template<sizeof(TCMalloc_Central_FreeList) % 64> TCMalloc_Central_FreeListPadded;
  1346. #if COMPILER(CLANG) && defined(__has_warning)
  1347. #pragma clang diagnostic pop
  1348. #endif
  1349. #if OS(DARWIN)
  1350. struct Span;
  1351. class TCMalloc_PageHeap;
  1352. class TCMalloc_ThreadCache;
  1353. template <typename T> class PageHeapAllocator;
  1354. class FastMallocZone {
  1355. public:
  1356. static void init();
  1357. static kern_return_t enumerate(task_t, void*, unsigned typeMmask, vm_address_t zoneAddress, memory_reader_t, vm_range_recorder_t);
  1358. static size_t goodSize(malloc_zone_t*, size_t size) { return size; }
  1359. static boolean_t check(malloc_zone_t*) { return true; }
  1360. static void print(malloc_zone_t*, boolean_t) { }
  1361. static void log(malloc_zone_t*, void*) { }
  1362. static void forceLock(malloc_zone_t*) { }
  1363. static void forceUnlock(malloc_zone_t*) { }
  1364. static void statistics(malloc_zone_t*, malloc_statistics_t* stats) { memset(stats, 0, sizeof(malloc_statistics_t)); }
  1365. private:
  1366. FastMallocZone(TCMalloc_PageHeap*, TCMalloc_ThreadCache**, TCMalloc_Central_FreeListPadded*, PageHeapAllocator<Span>*, PageHeapAllocator<TCMalloc_ThreadCache>*);
  1367. static size_t size(malloc_zone_t*, const void*);
  1368. static void* zoneMalloc(malloc_zone_t*, size_t);
  1369. static void* zoneCalloc(malloc_zone_t*, size_t numItems, size_t size);
  1370. static void zoneFree(malloc_zone_t*, void*);
  1371. static void* zoneRealloc(malloc_zone_t*, void*, size_t);
  1372. static void* zoneValloc(malloc_zone_t*, size_t) { LOG_ERROR("valloc is not supported"); return 0; }
  1373. static void zoneDestroy(malloc_zone_t*) { }
  1374. malloc_zone_t m_zone;
  1375. TCMalloc_PageHeap* m_pageHeap;
  1376. TCMalloc_ThreadCache** m_threadHeaps;
  1377. TCMalloc_Central_FreeListPadded* m_centralCaches;
  1378. PageHeapAllocator<Span>* m_spanAllocator;
  1379. PageHeapAllocator<TCMalloc_ThreadCache>* m_pageHeapAllocator;
  1380. };
  1381. // This method declaration, and the constants below, are taken from Libc/gen/malloc.c.
  1382. extern "C" void (*malloc_logger)(uint32_t typeFlags, uintptr_t zone, uintptr_t size, uintptr_t pointer, uintptr_t returnValue, uint32_t numberOfFramesToSkip);
  1383. #endif
  1384. class MallocHook {
  1385. static bool stackLoggingEnabled;
  1386. #if OS(DARWIN)
  1387. enum StackLoggingType {
  1388. StackLoggingTypeAlloc = 2,
  1389. StackLoggingTypeDealloc = 4,
  1390. };
  1391. static void record(uint32_t typeFlags, uintptr_t zone, uintptr_t size, void* pointer, void* returnValue, uint32_t numberOfFramesToSkip)
  1392. {
  1393. malloc_logger(typeFlags, zone, size, reinterpret_cast<uintptr_t>(pointer), reinterpret_cast<uintptr_t>(returnValue), numberOfFramesToSkip);
  1394. }
  1395. static NEVER_INLINE void recordAllocation(void* pointer, size_t size)
  1396. {
  1397. // StackLoggingTypeAlloc takes the newly-allocated address in the returnValue argument, the size of the allocation
  1398. // in the size argument and ignores all other arguments.
  1399. record(StackLoggingTypeAlloc, 0, size, 0, pointer, 0);
  1400. }
  1401. static NEVER_INLINE void recordDeallocation(void* pointer)
  1402. {
  1403. // StackLoggingTypeDealloc takes the pointer in the size argument and ignores all other arguments.
  1404. record(StackLoggingTypeDealloc, 0, reinterpret_cast<uintptr_t>(pointer), 0, 0, 0);
  1405. }
  1406. #endif
  1407. public:
  1408. static void init()
  1409. {
  1410. #if OS(DARWIN)
  1411. // If the system allocator's malloc_logger has been set up then stack logging is enabled.
  1412. stackLoggingEnabled = malloc_logger;
  1413. #endif
  1414. }
  1415. #if OS(DARWIN)
  1416. static ALWAYS_INLINE void InvokeNewHook(void* pointer, size_t size)
  1417. {
  1418. if (UNLIKELY(stackLoggingEnabled))
  1419. recordAllocation(pointer, size);
  1420. }
  1421. static ALWAYS_INLINE void InvokeDeleteHook(void* pointer)
  1422. {
  1423. if (UNLIKELY(stackLoggingEnabled))
  1424. recordDeallocation(pointer);
  1425. }
  1426. #else
  1427. static ALWAYS_INLINE void InvokeNewHook(void*, size_t) { }
  1428. static ALWAYS_INLINE void InvokeDeleteHook(void*) { }
  1429. #endif
  1430. };
  1431. bool MallocHook::stackLoggingEnabled = false;
  1432. #endif
  1433. #ifndef WTF_CHANGES
  1434. // This #ifdef should almost never be set. Set NO_TCMALLOC_SAMPLES if
  1435. // you're porting to a system where you really can't get a stacktrace.
  1436. #ifdef NO_TCMALLOC_SAMPLES
  1437. // We use #define so code compiles even if you #include stacktrace.h somehow.
  1438. # define GetStackTrace(stack, depth, skip) (0)
  1439. #else
  1440. # include <google/stacktrace.h>
  1441. #endif
  1442. #endif
  1443. // Even if we have support for thread-local storage in the compiler
  1444. // and linker, the OS may not support it. We need to check that at
  1445. // runtime. Right now, we have to keep a manual set of "bad" OSes.
  1446. #if defined(HAVE_TLS)
  1447. static bool kernel_supports_tls = false; // be conservative
  1448. static inline bool KernelSupportsTLS() {
  1449. return kernel_supports_tls;
  1450. }
  1451. # if !HAVE_DECL_UNAME // if too old for uname, probably too old for TLS
  1452. static void CheckIfKernelSupportsTLS() {
  1453. kernel_supports_tls = false;
  1454. }
  1455. # else
  1456. # include <sys/utsname.h> // DECL_UNAME checked for <sys/utsname.h> too
  1457. static void CheckIfKernelSupportsTLS() {
  1458. struct utsname buf;
  1459. if (uname(&buf) != 0) { // should be impossible
  1460. MESSAGE("uname failed assuming no TLS support (errno=%d)\n", errno);
  1461. kernel_supports_tls = false;
  1462. } else if (strcasecmp(buf.sysname, "linux") == 0) {
  1463. // The linux case: the first kernel to support TLS was 2.6.0
  1464. if (buf.release[0] < '2' && buf.release[1] == '.') // 0.x or 1.x
  1465. kernel_supports_tls = false;
  1466. else if (buf.release[0] == '2' && buf.release[1] == '.' &&
  1467. buf.release[2] >= '0' && buf.release[2] < '6' &&
  1468. buf.release[3] == '.') // 2.0 - 2.5
  1469. kernel_supports_tls = false;
  1470. else
  1471. kernel_supports_tls = true;
  1472. } else { // some other kernel, we'll be optimisitic
  1473. kernel_supports_tls = true;
  1474. }
  1475. // TODO(csilvers): VLOG(1) the tls status once we support RAW_VLOG
  1476. }
  1477. # endif // HAVE_DECL_UNAME
  1478. #endif // HAVE_TLS
  1479. // __THROW is defined in glibc systems. It means, counter-intuitively,
  1480. // "This function will never throw an exception." It's an optional
  1481. // optimization tool, but we may need to use it to match glibc prototypes.
  1482. #ifndef __THROW // I guess we're not on a glibc system
  1483. # define __THROW // __THROW is just an optimization, so ok to make it ""
  1484. #endif
  1485. // -------------------------------------------------------------------------
  1486. // Stack traces kept for sampled allocations
  1487. // The following state is protected by pageheap_lock_.
  1488. // -------------------------------------------------------------------------
  1489. // size/depth are made the same size as a pointer so that some generic
  1490. // code below can conveniently cast them back and forth to void*.
  1491. static const int kMaxStackDepth = 31;
  1492. struct StackTrace {
  1493. uintptr_t size; // Size of object
  1494. uintptr_t depth; // Number of PC values stored in array below
  1495. void* stack[kMaxStackDepth];
  1496. };
  1497. static PageHeapAllocator<StackTrace> stacktrace_allocator;
  1498. static Span sampled_objects;
  1499. // -------------------------------------------------------------------------
  1500. // Map from page-id to per-page data
  1501. // -------------------------------------------------------------------------
  1502. // We use PageMap2<> for 32-bit and PageMap3<> for 64-bit machines.
  1503. // We also use a simple one-level cache for hot PageID-to-sizeclass mappings,
  1504. // because sometimes the sizeclass is all the information we need.
  1505. // Selector class -- general selector uses 3-level map
  1506. template <int BITS> class MapSelector {
  1507. public:
  1508. typedef TCMalloc_PageMap3<BITS-kPageShift> Type;
  1509. typedef PackedCache<BITS, uint64_t> CacheType;
  1510. };
  1511. #if defined(WTF_CHANGES)
  1512. #if CPU(X86_64)
  1513. // On all known X86-64 platforms, the upper 16 bits are always unused and therefore
  1514. // can be excluded from the PageMap key.
  1515. // See http://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details
  1516. static const size_t kBitsUnusedOn64Bit = 16;
  1517. #else
  1518. static const size_t kBitsUnusedOn64Bit = 0;
  1519. #endif
  1520. // A three-level map for 64-bit machines
  1521. template <> class MapSelector<64> {
  1522. public:
  1523. typedef TCMalloc_PageMap3<64 - kPageShift - kBitsUnusedOn64Bit> Type;
  1524. typedef PackedCache<64, uint64_t> CacheType;
  1525. };
  1526. #endif
  1527. // A two-level map for 32-bit machines
  1528. template <> class MapSelector<32> {
  1529. public:
  1530. typedef TCMalloc_PageMap2<32 - kPageShift> Type;
  1531. typedef PackedCache<32 - kPageShift, uint16_t> CacheType;
  1532. };
  1533. // -------------------------------------------------------------------------
  1534. // Page-level allocator
  1535. // * Eager coalescing
  1536. //
  1537. // Heap for page-level allocation. We allow allocating and freeing a
  1538. // contiguous runs of pages (called a "span").
  1539. // -------------------------------------------------------------------------
  1540. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1541. // The page heap maintains a free list for spans that are no longer in use by
  1542. // the central cache or any thread caches. We use a background thread to
  1543. // periodically scan the free list and release a percentage of it back to the OS.
  1544. // If free_committed_pages_ exceeds kMinimumFreeCommittedPageCount, the
  1545. // background thread:
  1546. // - wakes up
  1547. // - pauses for kScavengeDelayInSeconds
  1548. // - returns to the OS a percentage of the memory that remained unused during
  1549. // that pause (kScavengePercentage * min_free_committed_pages_since_last_scavenge_)
  1550. // The goal of this strategy is to reduce memory pressure in a timely fashion
  1551. // while avoiding thrashing the OS allocator.
  1552. // Time delay before the page heap scavenger will consider returning pages to
  1553. // the OS.
  1554. static const int kScavengeDelayInSeconds = 2;
  1555. // Approximate percentage of free committed pages to return to the OS in one
  1556. // scavenge.
  1557. static const float kScavengePercentage = .5f;
  1558. // number of span lists to keep spans in when memory is returned.
  1559. static const int kMinSpanListsWithSpans = 32;
  1560. // Number of free committed pages that we want to keep around. The minimum number of pages used when there
  1561. // is 1 span in each of the first kMinSpanListsWithSpans spanlists. Currently 528 pages.
  1562. static const size_t kMinimumFreeCommittedPageCount = kMinSpanListsWithSpans * ((1.0f+kMinSpanListsWithSpans) / 2.0f);
  1563. #endif
  1564. static SpinLock pageheap_lock = SPINLOCK_INITIALIZER;
  1565. class TCMalloc_PageHeap {
  1566. public:
  1567. void init();
  1568. // Allocate a run of "n" pages. Returns zero if out of memory.
  1569. Span* New(Length n);
  1570. // Delete the span "[p, p+n-1]".
  1571. // REQUIRES: span was returned by earlier call to New() and
  1572. // has not yet been deleted.
  1573. void Delete(Span* span);
  1574. // Mark an allocated span as being used for small objects of the
  1575. // specified size-class.
  1576. // REQUIRES: span was returned by an earlier call to New()
  1577. // and has not yet been deleted.
  1578. void RegisterSizeClass(Span* span, size_t sc);
  1579. // Split an allocated span into two spans: one of length "n" pages
  1580. // followed by another span of length "span->length - n" pages.
  1581. // Modifies "*span" to point to the first span of length "n" pages.
  1582. // Returns a pointer to the second span.
  1583. //
  1584. // REQUIRES: "0 < n < span->length"
  1585. // REQUIRES: !span->free
  1586. // REQUIRES: span->sizeclass == 0
  1587. Span* Split(Span* span, Length n);
  1588. // Return the descriptor for the specified page.
  1589. inline Span* GetDescriptor(PageID p) const {
  1590. return reinterpret_cast<Span*>(pagemap_.get(p));
  1591. }
  1592. #ifdef WTF_CHANGES
  1593. inline Span* GetDescriptorEnsureSafe(PageID p)
  1594. {
  1595. pagemap_.Ensure(p, 1);
  1596. return GetDescriptor(p);
  1597. }
  1598. size_t ReturnedBytes() const;
  1599. #endif
  1600. // Dump state to stderr
  1601. #ifndef WTF_CHANGES
  1602. void Dump(TCMalloc_Printer* out);
  1603. #endif
  1604. // Return number of bytes allocated from system
  1605. inline uint64_t SystemBytes() const { return system_bytes_; }
  1606. // Return number of free bytes in heap
  1607. uint64_t FreeBytes() const {
  1608. return (static_cast<uint64_t>(free_pages_) << kPageShift);
  1609. }
  1610. bool Check();
  1611. size_t CheckList(Span* list, Length min_pages, Length max_pages, bool decommitted);
  1612. // Release all pages on the free list for reuse by the OS:
  1613. void ReleaseFreePages();
  1614. void ReleaseFreeList(Span*, Span*);
  1615. // Return 0 if we have no information, or else the correct sizeclass for p.
  1616. // Reads and writes to pagemap_cache_ do not require locking.
  1617. // The entries are 64 bits on 64-bit hardware and 16 bits on
  1618. // 32-bit hardware, and we don't mind raciness as long as each read of
  1619. // an entry yields a valid entry, not a partially updated entry.
  1620. size_t GetSizeClassIfCached(PageID p) const {
  1621. return pagemap_cache_.GetOrDefault(p, 0);
  1622. }
  1623. void CacheSizeClass(PageID p, size_t cl) const { pagemap_cache_.Put(p, cl); }
  1624. private:
  1625. // Pick the appropriate map and cache types based on pointer size
  1626. typedef MapSelector<8*sizeof(uintptr_t)>::Type PageMap;
  1627. typedef MapSelector<8*sizeof(uintptr_t)>::CacheType PageMapCache;
  1628. PageMap pagemap_;
  1629. mutable PageMapCache pagemap_cache_;
  1630. // We segregate spans of a given size into two circular linked
  1631. // lists: one for normal spans, and one for spans whose memory
  1632. // has been returned to the system.
  1633. struct SpanList {
  1634. Span normal;
  1635. Span returned;
  1636. };
  1637. // List of free spans of length >= kMaxPages
  1638. SpanList large_;
  1639. // Array mapping from span length to a doubly linked list of free spans
  1640. SpanList free_[kMaxPages];
  1641. // Number of pages kept in free lists
  1642. uintptr_t free_pages_;
  1643. // Used for hardening
  1644. uintptr_t entropy_;
  1645. // Bytes allocated from system
  1646. uint64_t system_bytes_;
  1647. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1648. // Number of pages kept in free lists that are still committed.
  1649. Length free_committed_pages_;
  1650. // Minimum number of free committed pages since last scavenge. (Can be 0 if
  1651. // we've committed new pages since the last scavenge.)
  1652. Length min_free_committed_pages_since_last_scavenge_;
  1653. #endif
  1654. bool GrowHeap(Length n);
  1655. // REQUIRES span->length >= n
  1656. // Remove span from its free list, and move any leftover part of
  1657. // span into appropriate free lists. Also update "span" to have
  1658. // length exactly "n" and mark it as non-free so it can be returned
  1659. // to the client.
  1660. //
  1661. // "released" is true iff "span" was found on a "returned" list.
  1662. void Carve(Span* span, Length n, bool released);
  1663. void RecordSpan(Span* span) {
  1664. pagemap_.set(span->start, span);
  1665. if (span->length > 1) {
  1666. pagemap_.set(span->start + span->length - 1, span);
  1667. }
  1668. }
  1669. // Allocate a large span of length == n. If successful, returns a
  1670. // span of exactly the specified length. Else, returns NULL.
  1671. Span* AllocLarge(Length n);
  1672. #if !USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1673. // Incrementally release some memory to the system.
  1674. // IncrementalScavenge(n) is called whenever n pages are freed.
  1675. void IncrementalScavenge(Length n);
  1676. #endif
  1677. // Number of pages to deallocate before doing more scavenging
  1678. int64_t scavenge_counter_;
  1679. // Index of last free list we scavenged
  1680. size_t scavenge_index_;
  1681. #if defined(WTF_CHANGES) && OS(DARWIN)
  1682. friend class FastMallocZone;
  1683. #endif
  1684. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1685. void initializeScavenger();
  1686. ALWAYS_INLINE void signalScavenger();
  1687. void scavenge();
  1688. ALWAYS_INLINE bool shouldScavenge() const;
  1689. #if HAVE(DISPATCH_H) || OS(WINDOWS)
  1690. void periodicScavenge();
  1691. ALWAYS_INLINE bool isScavengerSuspended();
  1692. ALWAYS_INLINE void scheduleScavenger();
  1693. ALWAYS_INLINE void rescheduleScavenger();
  1694. ALWAYS_INLINE void suspendScavenger();
  1695. #endif
  1696. #if HAVE(DISPATCH_H)
  1697. dispatch_queue_t m_scavengeQueue;
  1698. dispatch_source_t m_scavengeTimer;
  1699. bool m_scavengingSuspended;
  1700. #elif OS(WINDOWS)
  1701. static void CALLBACK scavengerTimerFired(void*, BOOLEAN);
  1702. HANDLE m_scavengeQueueTimer;
  1703. #else
  1704. static NO_RETURN_WITH_VALUE void* runScavengerThread(void*);
  1705. NO_RETURN void scavengerThread();
  1706. // Keeps track of whether the background thread is actively scavenging memory every kScavengeDelayInSeconds, or
  1707. // it's blocked waiting for more pages to be deleted.
  1708. bool m_scavengeThreadActive;
  1709. pthread_mutex_t m_scavengeMutex;
  1710. pthread_cond_t m_scavengeCondition;
  1711. #endif
  1712. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1713. };
  1714. void TCMalloc_PageHeap::init()
  1715. {
  1716. pagemap_.init(MetaDataAlloc);
  1717. pagemap_cache_ = PageMapCache(0);
  1718. free_pages_ = 0;
  1719. system_bytes_ = 0;
  1720. entropy_ = HARDENING_ENTROPY;
  1721. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1722. free_committed_pages_ = 0;
  1723. min_free_committed_pages_since_last_scavenge_ = 0;
  1724. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1725. scavenge_counter_ = 0;
  1726. // Start scavenging at kMaxPages list
  1727. scavenge_index_ = kMaxPages-1;
  1728. COMPILE_ASSERT(kNumClasses <= (1 << PageMapCache::kValuebits), valuebits);
  1729. DLL_Init(&large_.normal, entropy_);
  1730. DLL_Init(&large_.returned, entropy_);
  1731. for (size_t i = 0; i < kMaxPages; i++) {
  1732. DLL_Init(&free_[i].normal, entropy_);
  1733. DLL_Init(&free_[i].returned, entropy_);
  1734. }
  1735. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1736. initializeScavenger();
  1737. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1738. }
  1739. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1740. #if HAVE(DISPATCH_H)
  1741. void TCMalloc_PageHeap::initializeScavenger()
  1742. {
  1743. m_scavengeQueue = dispatch_queue_create("com.apple.JavaScriptCore.FastMallocSavenger", NULL);
  1744. m_scavengeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, m_scavengeQueue);
  1745. uint64_t scavengeDelayInNanoseconds = kScavengeDelayInSeconds * NSEC_PER_SEC;
  1746. dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, scavengeDelayInNanoseconds);
  1747. dispatch_source_set_timer(m_scavengeTimer, startTime, scavengeDelayInNanoseconds, scavengeDelayInNanoseconds / 10);
  1748. dispatch_source_set_event_handler(m_scavengeTimer, ^{ periodicScavenge(); });
  1749. m_scavengingSuspended = true;
  1750. }
  1751. ALWAYS_INLINE bool TCMalloc_PageHeap::isScavengerSuspended()
  1752. {
  1753. ASSERT(pageheap_lock.IsHeld());
  1754. return m_scavengingSuspended;
  1755. }
  1756. ALWAYS_INLINE void TCMalloc_PageHeap::scheduleScavenger()
  1757. {
  1758. ASSERT(pageheap_lock.IsHeld());
  1759. m_scavengingSuspended = false;
  1760. dispatch_resume(m_scavengeTimer);
  1761. }
  1762. ALWAYS_INLINE void TCMalloc_PageHeap::rescheduleScavenger()
  1763. {
  1764. // Nothing to do here for libdispatch.
  1765. }
  1766. ALWAYS_INLINE void TCMalloc_PageHeap::suspendScavenger()
  1767. {
  1768. ASSERT(pageheap_lock.IsHeld());
  1769. m_scavengingSuspended = true;
  1770. dispatch_suspend(m_scavengeTimer);
  1771. }
  1772. #elif OS(WINDOWS)
  1773. void TCMalloc_PageHeap::scavengerTimerFired(void* context, BOOLEAN)
  1774. {
  1775. static_cast<TCMalloc_PageHeap*>(context)->periodicScavenge();
  1776. }
  1777. void TCMalloc_PageHeap::initializeScavenger()
  1778. {
  1779. m_scavengeQueueTimer = 0;
  1780. }
  1781. ALWAYS_INLINE bool TCMalloc_PageHeap::isScavengerSuspended()
  1782. {
  1783. ASSERT(pageheap_lock.IsHeld());
  1784. return !m_scavengeQueueTimer;
  1785. }
  1786. ALWAYS_INLINE void TCMalloc_PageHeap::scheduleScavenger()
  1787. {
  1788. // We need to use WT_EXECUTEONLYONCE here and reschedule the timer, because
  1789. // Windows will fire the timer event even when the function is already running.
  1790. ASSERT(pageheap_lock.IsHeld());
  1791. CreateTimerQueueTimer(&m_scavengeQueueTimer, 0, scavengerTimerFired, this, kScavengeDelayInSeconds * 1000, 0, WT_EXECUTEONLYONCE);
  1792. }
  1793. ALWAYS_INLINE void TCMalloc_PageHeap::rescheduleScavenger()
  1794. {
  1795. // We must delete the timer and create it again, because it is not possible to retrigger a timer on Windows.
  1796. suspendScavenger();
  1797. scheduleScavenger();
  1798. }
  1799. ALWAYS_INLINE void TCMalloc_PageHeap::suspendScavenger()
  1800. {
  1801. ASSERT(pageheap_lock.IsHeld());
  1802. HANDLE scavengeQueueTimer = m_scavengeQueueTimer;
  1803. m_scavengeQueueTimer = 0;
  1804. DeleteTimerQueueTimer(0, scavengeQueueTimer, 0);
  1805. }
  1806. #else
  1807. void TCMalloc_PageHeap::initializeScavenger()
  1808. {
  1809. // Create a non-recursive mutex.
  1810. #if !defined(PTHREAD_MUTEX_NORMAL) || PTHREAD_MUTEX_NORMAL == PTHREAD_MUTEX_DEFAULT
  1811. pthread_mutex_init(&m_scavengeMutex, 0);
  1812. #else
  1813. pthread_mutexattr_t attr;
  1814. pthread_mutexattr_init(&attr);
  1815. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  1816. pthread_mutex_init(&m_scavengeMutex, &attr);
  1817. pthread_mutexattr_destroy(&attr);
  1818. #endif
  1819. pthread_cond_init(&m_scavengeCondition, 0);
  1820. m_scavengeThreadActive = true;
  1821. pthread_t thread;
  1822. pthread_create(&thread, 0, runScavengerThread, this);
  1823. }
  1824. void* TCMalloc_PageHeap::runScavengerThread(void* context)
  1825. {
  1826. static_cast<TCMalloc_PageHeap*>(context)->scavengerThread();
  1827. #if (COMPILER(MSVC) || COMPILER(SUNCC))
  1828. // Without this, Visual Studio and Sun Studio will complain that this method does not return a value.
  1829. return 0;
  1830. #endif
  1831. }
  1832. ALWAYS_INLINE void TCMalloc_PageHeap::signalScavenger()
  1833. {
  1834. // shouldScavenge() should be called only when the pageheap_lock spinlock is held, additionally,
  1835. // m_scavengeThreadActive is only set to false whilst pageheap_lock is held. The caller must ensure this is
  1836. // taken prior to calling this method. If the scavenger thread is sleeping and shouldScavenge() indicates there
  1837. // is memory to free the scavenger thread is signalled to start.
  1838. ASSERT(pageheap_lock.IsHeld());
  1839. if (!m_scavengeThreadActive && shouldScavenge())
  1840. pthread_cond_signal(&m_scavengeCondition);
  1841. }
  1842. #endif
  1843. void TCMalloc_PageHeap::scavenge()
  1844. {
  1845. size_t pagesToRelease = min_free_committed_pages_since_last_scavenge_ * kScavengePercentage;
  1846. size_t targetPageCount = std::max<size_t>(kMinimumFreeCommittedPageCount, free_committed_pages_ - pagesToRelease);
  1847. Length lastFreeCommittedPages = free_committed_pages_;
  1848. while (free_committed_pages_ > targetPageCount) {
  1849. ASSERT(Check());
  1850. for (int i = kMaxPages; i > 0 && free_committed_pages_ >= targetPageCount; i--) {
  1851. SpanList* slist = (static_cast<size_t>(i) == kMaxPages) ? &large_ : &free_[i];
  1852. // If the span size is bigger than kMinSpanListsWithSpans pages return all the spans in the list, else return all but 1 span.
  1853. // Return only 50% of a spanlist at a time so spans of size 1 are not the only ones left.
  1854. size_t length = DLL_Length(&slist->normal, entropy_);
  1855. size_t numSpansToReturn = (i > kMinSpanListsWithSpans) ? length : length / 2;
  1856. for (int j = 0; static_cast<size_t>(j) < numSpansToReturn && !DLL_IsEmpty(&slist->normal, entropy_) && free_committed_pages_ > targetPageCount; j++) {
  1857. Span* s = slist->normal.prev(entropy_);
  1858. DLL_Remove(s, entropy_);
  1859. ASSERT(!s->decommitted);
  1860. if (!s->decommitted) {
  1861. TCMalloc_SystemRelease(reinterpret_cast<void*>(s->start << kPageShift),
  1862. static_cast<size_t>(s->length << kPageShift));
  1863. ASSERT(free_committed_pages_ >= s->length);
  1864. free_committed_pages_ -= s->length;
  1865. s->decommitted = true;
  1866. }
  1867. DLL_Prepend(&slist->returned, s, entropy_);
  1868. }
  1869. }
  1870. if (lastFreeCommittedPages == free_committed_pages_)
  1871. break;
  1872. lastFreeCommittedPages = free_committed_pages_;
  1873. }
  1874. min_free_committed_pages_since_last_scavenge_ = free_committed_pages_;
  1875. }
  1876. ALWAYS_INLINE bool TCMalloc_PageHeap::shouldScavenge() const
  1877. {
  1878. return free_committed_pages_ > kMinimumFreeCommittedPageCount;
  1879. }
  1880. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1881. inline Span* TCMalloc_PageHeap::New(Length n) {
  1882. ASSERT(Check());
  1883. ASSERT(n > 0);
  1884. // Find first size >= n that has a non-empty list
  1885. for (Length s = n; s < kMaxPages; s++) {
  1886. Span* ll = NULL;
  1887. bool released = false;
  1888. if (!DLL_IsEmpty(&free_[s].normal, entropy_)) {
  1889. // Found normal span
  1890. ll = &free_[s].normal;
  1891. } else if (!DLL_IsEmpty(&free_[s].returned, entropy_)) {
  1892. // Found returned span; reallocate it
  1893. ll = &free_[s].returned;
  1894. released = true;
  1895. } else {
  1896. // Keep looking in larger classes
  1897. continue;
  1898. }
  1899. Span* result = ll->next(entropy_);
  1900. Carve(result, n, released);
  1901. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1902. // The newly allocated memory is from a span that's in the normal span list (already committed). Update the
  1903. // free committed pages count.
  1904. ASSERT(free_committed_pages_ >= n);
  1905. free_committed_pages_ -= n;
  1906. if (free_committed_pages_ < min_free_committed_pages_since_last_scavenge_)
  1907. min_free_committed_pages_since_last_scavenge_ = free_committed_pages_;
  1908. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1909. ASSERT(Check());
  1910. free_pages_ -= n;
  1911. return result;
  1912. }
  1913. Span* result = AllocLarge(n);
  1914. if (result != NULL) {
  1915. ASSERT_SPAN_COMMITTED(result);
  1916. return result;
  1917. }
  1918. // Grow the heap and try again
  1919. if (!GrowHeap(n)) {
  1920. ASSERT(Check());
  1921. return NULL;
  1922. }
  1923. return New(n);
  1924. }
  1925. Span* TCMalloc_PageHeap::AllocLarge(Length n) {
  1926. // find the best span (closest to n in size).
  1927. // The following loops implements address-ordered best-fit.
  1928. bool from_released = false;
  1929. Span *best = NULL;
  1930. // Search through normal list
  1931. for (Span* span = large_.normal.next(entropy_);
  1932. span != &large_.normal;
  1933. span = span->next(entropy_)) {
  1934. if (span->length >= n) {
  1935. if ((best == NULL)
  1936. || (span->length < best->length)
  1937. || ((span->length == best->length) && (span->start < best->start))) {
  1938. best = span;
  1939. from_released = false;
  1940. }
  1941. }
  1942. }
  1943. // Search through released list in case it has a better fit
  1944. for (Span* span = large_.returned.next(entropy_);
  1945. span != &large_.returned;
  1946. span = span->next(entropy_)) {
  1947. if (span->length >= n) {
  1948. if ((best == NULL)
  1949. || (span->length < best->length)
  1950. || ((span->length == best->length) && (span->start < best->start))) {
  1951. best = span;
  1952. from_released = true;
  1953. }
  1954. }
  1955. }
  1956. if (best != NULL) {
  1957. Carve(best, n, from_released);
  1958. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1959. // The newly allocated memory is from a span that's in the normal span list (already committed). Update the
  1960. // free committed pages count.
  1961. ASSERT(free_committed_pages_ >= n);
  1962. free_committed_pages_ -= n;
  1963. if (free_committed_pages_ < min_free_committed_pages_since_last_scavenge_)
  1964. min_free_committed_pages_since_last_scavenge_ = free_committed_pages_;
  1965. #endif // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1966. ASSERT(Check());
  1967. free_pages_ -= n;
  1968. return best;
  1969. }
  1970. return NULL;
  1971. }
  1972. Span* TCMalloc_PageHeap::Split(Span* span, Length n) {
  1973. ASSERT(0 < n);
  1974. ASSERT(n < span->length);
  1975. ASSERT(!span->free);
  1976. ASSERT(span->sizeclass == 0);
  1977. Event(span, 'T', n);
  1978. const Length extra = span->length - n;
  1979. Span* leftover = NewSpan(span->start + n, extra);
  1980. Event(leftover, 'U', extra);
  1981. RecordSpan(leftover);
  1982. pagemap_.set(span->start + n - 1, span); // Update map from pageid to span
  1983. span->length = n;
  1984. return leftover;
  1985. }
  1986. inline void TCMalloc_PageHeap::Carve(Span* span, Length n, bool released) {
  1987. ASSERT(n > 0);
  1988. DLL_Remove(span, entropy_);
  1989. span->free = 0;
  1990. Event(span, 'A', n);
  1991. if (released) {
  1992. // If the span chosen to carve from is decommited, commit the entire span at once to avoid committing spans 1 page at a time.
  1993. ASSERT(span->decommitted);
  1994. TCMalloc_SystemCommit(reinterpret_cast<void*>(span->start << kPageShift), static_cast<size_t>(span->length << kPageShift));
  1995. span->decommitted = false;
  1996. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  1997. free_committed_pages_ += span->length;
  1998. #endif
  1999. }
  2000. const int extra = static_cast<int>(span->length - n);
  2001. ASSERT(extra >= 0);
  2002. if (extra > 0) {
  2003. Span* leftover = NewSpan(span->start + n, extra);
  2004. leftover->free = 1;
  2005. leftover->decommitted = false;
  2006. Event(leftover, 'S', extra);
  2007. RecordSpan(leftover);
  2008. // Place leftover span on appropriate free list
  2009. SpanList* listpair = (static_cast<size_t>(extra) < kMaxPages) ? &free_[extra] : &large_;
  2010. Span* dst = &listpair->normal;
  2011. DLL_Prepend(dst, leftover, entropy_);
  2012. span->length = n;
  2013. pagemap_.set(span->start + n - 1, span);
  2014. }
  2015. }
  2016. static ALWAYS_INLINE void mergeDecommittedStates(Span* destination, Span* other)
  2017. {
  2018. if (destination->decommitted && !other->decommitted) {
  2019. TCMalloc_SystemRelease(reinterpret_cast<void*>(other->start << kPageShift),
  2020. static_cast<size_t>(other->length << kPageShift));
  2021. } else if (other->decommitted && !destination->decommitted) {
  2022. TCMalloc_SystemRelease(reinterpret_cast<void*>(destination->start << kPageShift),
  2023. static_cast<size_t>(destination->length << kPageShift));
  2024. destination->decommitted = true;
  2025. }
  2026. }
  2027. inline void TCMalloc_PageHeap::Delete(Span* span) {
  2028. ASSERT(Check());
  2029. ASSERT(!span->free);
  2030. ASSERT(span->length > 0);
  2031. ASSERT(GetDescriptor(span->start) == span);
  2032. ASSERT(GetDescriptor(span->start + span->length - 1) == span);
  2033. span->sizeclass = 0;
  2034. #ifndef NO_TCMALLOC_SAMPLES
  2035. span->sample = 0;
  2036. #endif
  2037. // Coalesce -- we guarantee that "p" != 0, so no bounds checking
  2038. // necessary. We do not bother resetting the stale pagemap
  2039. // entries for the pieces we are merging together because we only
  2040. // care about the pagemap entries for the boundaries.
  2041. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2042. // Track the total size of the neighboring free spans that are committed.
  2043. Length neighboringCommittedSpansLength = 0;
  2044. #endif
  2045. const PageID p = span->start;
  2046. const Length n = span->length;
  2047. Span* prev = GetDescriptor(p-1);
  2048. if (prev != NULL && prev->free) {
  2049. // Merge preceding span into this span
  2050. ASSERT(prev->start + prev->length == p);
  2051. const Length len = prev->length;
  2052. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2053. if (!prev->decommitted)
  2054. neighboringCommittedSpansLength += len;
  2055. #endif
  2056. mergeDecommittedStates(span, prev);
  2057. DLL_Remove(prev, entropy_);
  2058. DeleteSpan(prev);
  2059. span->start -= len;
  2060. span->length += len;
  2061. pagemap_.set(span->start, span);
  2062. Event(span, 'L', len);
  2063. }
  2064. Span* next = GetDescriptor(p+n);
  2065. if (next != NULL && next->free) {
  2066. // Merge next span into this span
  2067. ASSERT(next->start == p+n);
  2068. const Length len = next->length;
  2069. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2070. if (!next->decommitted)
  2071. neighboringCommittedSpansLength += len;
  2072. #endif
  2073. mergeDecommittedStates(span, next);
  2074. DLL_Remove(next, entropy_);
  2075. DeleteSpan(next);
  2076. span->length += len;
  2077. pagemap_.set(span->start + span->length - 1, span);
  2078. Event(span, 'R', len);
  2079. }
  2080. Event(span, 'D', span->length);
  2081. span->free = 1;
  2082. if (span->decommitted) {
  2083. if (span->length < kMaxPages)
  2084. DLL_Prepend(&free_[span->length].returned, span, entropy_);
  2085. else
  2086. DLL_Prepend(&large_.returned, span, entropy_);
  2087. } else {
  2088. if (span->length < kMaxPages)
  2089. DLL_Prepend(&free_[span->length].normal, span, entropy_);
  2090. else
  2091. DLL_Prepend(&large_.normal, span, entropy_);
  2092. }
  2093. free_pages_ += n;
  2094. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2095. if (span->decommitted) {
  2096. // If the merged span is decommitted, that means we decommitted any neighboring spans that were
  2097. // committed. Update the free committed pages count.
  2098. free_committed_pages_ -= neighboringCommittedSpansLength;
  2099. if (free_committed_pages_ < min_free_committed_pages_since_last_scavenge_)
  2100. min_free_committed_pages_since_last_scavenge_ = free_committed_pages_;
  2101. } else {
  2102. // If the merged span remains committed, add the deleted span's size to the free committed pages count.
  2103. free_committed_pages_ += n;
  2104. }
  2105. // Make sure the scavenge thread becomes active if we have enough freed pages to release some back to the system.
  2106. signalScavenger();
  2107. #else
  2108. IncrementalScavenge(n);
  2109. #endif
  2110. ASSERT(Check());
  2111. }
  2112. #if !USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2113. void TCMalloc_PageHeap::IncrementalScavenge(Length n) {
  2114. // Fast path; not yet time to release memory
  2115. scavenge_counter_ -= n;
  2116. if (scavenge_counter_ >= 0) return; // Not yet time to scavenge
  2117. #if PLATFORM(IOS)
  2118. static const size_t kDefaultReleaseDelay = 64;
  2119. #else
  2120. // If there is nothing to release, wait for so many pages before
  2121. // scavenging again. With 4K pages, this comes to 16MB of memory.
  2122. static const size_t kDefaultReleaseDelay = 1 << 8;
  2123. #endif
  2124. // Find index of free list to scavenge
  2125. size_t index = scavenge_index_ + 1;
  2126. uintptr_t entropy = entropy_;
  2127. for (size_t i = 0; i < kMaxPages+1; i++) {
  2128. if (index > kMaxPages) index = 0;
  2129. SpanList* slist = (index == kMaxPages) ? &large_ : &free_[index];
  2130. if (!DLL_IsEmpty(&slist->normal, entropy)) {
  2131. // Release the last span on the normal portion of this list
  2132. Span* s = slist->normal.prev(entropy);
  2133. DLL_Remove(s, entropy_);
  2134. TCMalloc_SystemRelease(reinterpret_cast<void*>(s->start << kPageShift),
  2135. static_cast<size_t>(s->length << kPageShift));
  2136. s->decommitted = true;
  2137. DLL_Prepend(&slist->returned, s, entropy);
  2138. #if PLATFORM(IOS)
  2139. scavenge_counter_ = std::max<size_t>(16UL, std::min<size_t>(kDefaultReleaseDelay, kDefaultReleaseDelay - (free_pages_ / kDefaultReleaseDelay)));
  2140. #else
  2141. scavenge_counter_ = std::max<size_t>(64UL, std::min<size_t>(kDefaultReleaseDelay, kDefaultReleaseDelay - (free_pages_ / kDefaultReleaseDelay)));
  2142. #endif
  2143. if (index == kMaxPages && !DLL_IsEmpty(&slist->normal, entropy))
  2144. scavenge_index_ = index - 1;
  2145. else
  2146. scavenge_index_ = index;
  2147. return;
  2148. }
  2149. index++;
  2150. }
  2151. // Nothing to scavenge, delay for a while
  2152. scavenge_counter_ = kDefaultReleaseDelay;
  2153. }
  2154. #endif
  2155. void TCMalloc_PageHeap::RegisterSizeClass(Span* span, size_t sc) {
  2156. // Associate span object with all interior pages as well
  2157. ASSERT(!span->free);
  2158. ASSERT(GetDescriptor(span->start) == span);
  2159. ASSERT(GetDescriptor(span->start+span->length-1) == span);
  2160. Event(span, 'C', sc);
  2161. span->sizeclass = static_cast<unsigned int>(sc);
  2162. for (Length i = 1; i < span->length-1; i++) {
  2163. pagemap_.set(span->start+i, span);
  2164. }
  2165. }
  2166. #ifdef WTF_CHANGES
  2167. size_t TCMalloc_PageHeap::ReturnedBytes() const {
  2168. size_t result = 0;
  2169. for (unsigned s = 0; s < kMaxPages; s++) {
  2170. const int r_length = DLL_Length(&free_[s].returned, entropy_);
  2171. unsigned r_pages = s * r_length;
  2172. result += r_pages << kPageShift;
  2173. }
  2174. for (Span* s = large_.returned.next(entropy_); s != &large_.returned; s = s->next(entropy_))
  2175. result += s->length << kPageShift;
  2176. return result;
  2177. }
  2178. #endif
  2179. #ifndef WTF_CHANGES
  2180. static double PagesToMB(uint64_t pages) {
  2181. return (pages << kPageShift) / 1048576.0;
  2182. }
  2183. void TCMalloc_PageHeap::Dump(TCMalloc_Printer* out) {
  2184. int nonempty_sizes = 0;
  2185. for (int s = 0; s < kMaxPages; s++) {
  2186. if (!DLL_IsEmpty(&free_[s].normal) || !DLL_IsEmpty(&free_[s].returned)) {
  2187. nonempty_sizes++;
  2188. }
  2189. }
  2190. out->printf("------------------------------------------------\n");
  2191. out->printf("PageHeap: %d sizes; %6.1f MB free\n",
  2192. nonempty_sizes, PagesToMB(free_pages_));
  2193. out->printf("------------------------------------------------\n");
  2194. uint64_t total_normal = 0;
  2195. uint64_t total_returned = 0;
  2196. for (int s = 0; s < kMaxPages; s++) {
  2197. const int n_length = DLL_Length(&free_[s].normal);
  2198. const int r_length = DLL_Length(&free_[s].returned);
  2199. if (n_length + r_length > 0) {
  2200. uint64_t n_pages = s * n_length;
  2201. uint64_t r_pages = s * r_length;
  2202. total_normal += n_pages;
  2203. total_returned += r_pages;
  2204. out->printf("%6u pages * %6u spans ~ %6.1f MB; %6.1f MB cum"
  2205. "; unmapped: %6.1f MB; %6.1f MB cum\n",
  2206. s,
  2207. (n_length + r_length),
  2208. PagesToMB(n_pages + r_pages),
  2209. PagesToMB(total_normal + total_returned),
  2210. PagesToMB(r_pages),
  2211. PagesToMB(total_returned));
  2212. }
  2213. }
  2214. uint64_t n_pages = 0;
  2215. uint64_t r_pages = 0;
  2216. int n_spans = 0;
  2217. int r_spans = 0;
  2218. out->printf("Normal large spans:\n");
  2219. for (Span* s = large_.normal.next; s != &large_.normal; s = s->next) {
  2220. out->printf(" [ %6" PRIuS " pages ] %6.1f MB\n",
  2221. s->length, PagesToMB(s->length));
  2222. n_pages += s->length;
  2223. n_spans++;
  2224. }
  2225. out->printf("Unmapped large spans:\n");
  2226. for (Span* s = large_.returned.next; s != &large_.returned; s = s->next) {
  2227. out->printf(" [ %6" PRIuS " pages ] %6.1f MB\n",
  2228. s->length, PagesToMB(s->length));
  2229. r_pages += s->length;
  2230. r_spans++;
  2231. }
  2232. total_normal += n_pages;
  2233. total_returned += r_pages;
  2234. out->printf(">255 large * %6u spans ~ %6.1f MB; %6.1f MB cum"
  2235. "; unmapped: %6.1f MB; %6.1f MB cum\n",
  2236. (n_spans + r_spans),
  2237. PagesToMB(n_pages + r_pages),
  2238. PagesToMB(total_normal + total_returned),
  2239. PagesToMB(r_pages),
  2240. PagesToMB(total_returned));
  2241. }
  2242. #endif
  2243. bool TCMalloc_PageHeap::GrowHeap(Length n) {
  2244. ASSERT(kMaxPages >= kMinSystemAlloc);
  2245. if (n > kMaxValidPages) return false;
  2246. Length ask = (n>kMinSystemAlloc) ? n : static_cast<Length>(kMinSystemAlloc);
  2247. size_t actual_size;
  2248. void* ptr = TCMalloc_SystemAlloc(ask << kPageShift, &actual_size, kPageSize);
  2249. if (ptr == NULL) {
  2250. if (n < ask) {
  2251. // Try growing just "n" pages
  2252. ask = n;
  2253. ptr = TCMalloc_SystemAlloc(ask << kPageShift, &actual_size, kPageSize);
  2254. }
  2255. if (ptr == NULL) return false;
  2256. }
  2257. ask = actual_size >> kPageShift;
  2258. uint64_t old_system_bytes = system_bytes_;
  2259. system_bytes_ += (ask << kPageShift);
  2260. const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift;
  2261. ASSERT(p > 0);
  2262. // If we have already a lot of pages allocated, just pre allocate a bunch of
  2263. // memory for the page map. This prevents fragmentation by pagemap metadata
  2264. // when a program keeps allocating and freeing large blocks.
  2265. if (old_system_bytes < kPageMapBigAllocationThreshold
  2266. && system_bytes_ >= kPageMapBigAllocationThreshold) {
  2267. pagemap_.PreallocateMoreMemory();
  2268. }
  2269. // Make sure pagemap_ has entries for all of the new pages.
  2270. // Plus ensure one before and one after so coalescing code
  2271. // does not need bounds-checking.
  2272. if (pagemap_.Ensure(p-1, ask+2)) {
  2273. // Pretend the new area is allocated and then Delete() it to
  2274. // cause any necessary coalescing to occur.
  2275. //
  2276. // We do not adjust free_pages_ here since Delete() will do it for us.
  2277. Span* span = NewSpan(p, ask);
  2278. RecordSpan(span);
  2279. Delete(span);
  2280. ASSERT(Check());
  2281. return true;
  2282. } else {
  2283. // We could not allocate memory within "pagemap_"
  2284. // TODO: Once we can return memory to the system, return the new span
  2285. return false;
  2286. }
  2287. }
  2288. bool TCMalloc_PageHeap::Check() {
  2289. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2290. size_t totalFreeCommitted = 0;
  2291. #endif
  2292. ASSERT(free_[0].normal.next(entropy_) == &free_[0].normal);
  2293. ASSERT(free_[0].returned.next(entropy_) == &free_[0].returned);
  2294. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2295. totalFreeCommitted = CheckList(&large_.normal, kMaxPages, 1000000000, false);
  2296. #else
  2297. CheckList(&large_.normal, kMaxPages, 1000000000, false);
  2298. #endif
  2299. CheckList(&large_.returned, kMaxPages, 1000000000, true);
  2300. for (Length s = 1; s < kMaxPages; s++) {
  2301. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2302. totalFreeCommitted += CheckList(&free_[s].normal, s, s, false);
  2303. #else
  2304. CheckList(&free_[s].normal, s, s, false);
  2305. #endif
  2306. CheckList(&free_[s].returned, s, s, true);
  2307. }
  2308. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2309. ASSERT(totalFreeCommitted == free_committed_pages_);
  2310. #endif
  2311. return true;
  2312. }
  2313. #if ASSERT_DISABLED
  2314. size_t TCMalloc_PageHeap::CheckList(Span*, Length, Length, bool) {
  2315. return 0;
  2316. }
  2317. #else
  2318. size_t TCMalloc_PageHeap::CheckList(Span* list, Length min_pages, Length max_pages, bool decommitted) {
  2319. size_t freeCount = 0;
  2320. for (Span* s = list->next(entropy_); s != list; s = s->next(entropy_)) {
  2321. CHECK_CONDITION(s->free);
  2322. CHECK_CONDITION(s->length >= min_pages);
  2323. CHECK_CONDITION(s->length <= max_pages);
  2324. CHECK_CONDITION(GetDescriptor(s->start) == s);
  2325. CHECK_CONDITION(GetDescriptor(s->start+s->length-1) == s);
  2326. CHECK_CONDITION(s->decommitted == decommitted);
  2327. freeCount += s->length;
  2328. }
  2329. return freeCount;
  2330. }
  2331. #endif
  2332. void TCMalloc_PageHeap::ReleaseFreeList(Span* list, Span* returned) {
  2333. // Walk backwards through list so that when we push these
  2334. // spans on the "returned" list, we preserve the order.
  2335. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2336. size_t freePageReduction = 0;
  2337. #endif
  2338. while (!DLL_IsEmpty(list, entropy_)) {
  2339. Span* s = list->prev(entropy_);
  2340. DLL_Remove(s, entropy_);
  2341. s->decommitted = true;
  2342. DLL_Prepend(returned, s, entropy_);
  2343. TCMalloc_SystemRelease(reinterpret_cast<void*>(s->start << kPageShift),
  2344. static_cast<size_t>(s->length << kPageShift));
  2345. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2346. freePageReduction += s->length;
  2347. #endif
  2348. }
  2349. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2350. free_committed_pages_ -= freePageReduction;
  2351. if (free_committed_pages_ < min_free_committed_pages_since_last_scavenge_)
  2352. min_free_committed_pages_since_last_scavenge_ = free_committed_pages_;
  2353. #endif
  2354. }
  2355. void TCMalloc_PageHeap::ReleaseFreePages() {
  2356. for (Length s = 0; s < kMaxPages; s++) {
  2357. ReleaseFreeList(&free_[s].normal, &free_[s].returned);
  2358. }
  2359. ReleaseFreeList(&large_.normal, &large_.returned);
  2360. ASSERT(Check());
  2361. }
  2362. //-------------------------------------------------------------------
  2363. // Free list
  2364. //-------------------------------------------------------------------
  2365. class TCMalloc_ThreadCache_FreeList {
  2366. private:
  2367. HardenedSLL list_; // Linked list of nodes
  2368. uint16_t length_; // Current length
  2369. uint16_t lowater_; // Low water mark for list length
  2370. uintptr_t entropy_; // Entropy source for hardening
  2371. public:
  2372. void Init(uintptr_t entropy) {
  2373. list_.setValue(NULL);
  2374. length_ = 0;
  2375. lowater_ = 0;
  2376. entropy_ = entropy;
  2377. #if ENABLE(TCMALLOC_HARDENING)
  2378. ASSERT(entropy_);
  2379. #endif
  2380. }
  2381. // Return current length of list
  2382. int length() const {
  2383. return length_;
  2384. }
  2385. // Is list empty?
  2386. bool empty() const {
  2387. return !list_;
  2388. }
  2389. // Low-water mark management
  2390. int lowwatermark() const { return lowater_; }
  2391. void clear_lowwatermark() { lowater_ = length_; }
  2392. ALWAYS_INLINE void Push(HardenedSLL ptr) {
  2393. SLL_Push(&list_, ptr, entropy_);
  2394. length_++;
  2395. }
  2396. void PushRange(int N, HardenedSLL start, HardenedSLL end) {
  2397. SLL_PushRange(&list_, start, end, entropy_);
  2398. length_ = length_ + static_cast<uint16_t>(N);
  2399. }
  2400. void PopRange(int N, HardenedSLL* start, HardenedSLL* end) {
  2401. SLL_PopRange(&list_, N, start, end, entropy_);
  2402. ASSERT(length_ >= N);
  2403. length_ = length_ - static_cast<uint16_t>(N);
  2404. if (length_ < lowater_) lowater_ = length_;
  2405. }
  2406. ALWAYS_INLINE void* Pop() {
  2407. ASSERT(list_);
  2408. length_--;
  2409. if (length_ < lowater_) lowater_ = length_;
  2410. return SLL_Pop(&list_, entropy_).value();
  2411. }
  2412. // Runs through the linked list to ensure that
  2413. // we can do that, and ensures that 'missing'
  2414. // is not present
  2415. NEVER_INLINE void Validate(HardenedSLL missing, size_t size) {
  2416. HardenedSLL node = list_;
  2417. UNUSED_PARAM(size);
  2418. while (node) {
  2419. RELEASE_ASSERT(node != missing);
  2420. RELEASE_ASSERT(IS_DEFINITELY_POISONED(node.value(), size));
  2421. node = SLL_Next(node, entropy_);
  2422. }
  2423. }
  2424. #ifdef WTF_CHANGES
  2425. template <class Finder, class Reader>
  2426. void enumerateFreeObjects(Finder& finder, const Reader& reader)
  2427. {
  2428. for (HardenedSLL nextObject = list_; nextObject; nextObject.setValue(reader.nextEntryInHardenedLinkedList(reinterpret_cast<void**>(nextObject.value()), entropy_)))
  2429. finder.visit(nextObject.value());
  2430. }
  2431. #endif
  2432. };
  2433. //-------------------------------------------------------------------
  2434. // Data kept per thread
  2435. //-------------------------------------------------------------------
  2436. class TCMalloc_ThreadCache {
  2437. private:
  2438. typedef TCMalloc_ThreadCache_FreeList FreeList;
  2439. #if OS(WINDOWS)
  2440. typedef DWORD ThreadIdentifier;
  2441. #else
  2442. typedef pthread_t ThreadIdentifier;
  2443. #endif
  2444. size_t size_; // Combined size of data
  2445. ThreadIdentifier tid_; // Which thread owns it
  2446. bool in_setspecific_; // Called pthread_setspecific?
  2447. FreeList list_[kNumClasses]; // Array indexed by size-class
  2448. // We sample allocations, biased by the size of the allocation
  2449. uint32_t rnd_; // Cheap random number generator
  2450. size_t bytes_until_sample_; // Bytes until we sample next
  2451. uintptr_t entropy_; // Entropy value used for hardening
  2452. // Allocate a new heap. REQUIRES: pageheap_lock is held.
  2453. static inline TCMalloc_ThreadCache* NewHeap(ThreadIdentifier tid, uintptr_t entropy);
  2454. // Use only as pthread thread-specific destructor function.
  2455. static void DestroyThreadCache(void* ptr);
  2456. public:
  2457. // All ThreadCache objects are kept in a linked list (for stats collection)
  2458. TCMalloc_ThreadCache* next_;
  2459. TCMalloc_ThreadCache* prev_;
  2460. void Init(ThreadIdentifier tid, uintptr_t entropy);
  2461. void Cleanup();
  2462. // Accessors (mostly just for printing stats)
  2463. int freelist_length(size_t cl) const { return list_[cl].length(); }
  2464. // Total byte size in cache
  2465. size_t Size() const { return size_; }
  2466. ALWAYS_INLINE void* Allocate(size_t size);
  2467. void Deallocate(HardenedSLL ptr, size_t size_class);
  2468. ALWAYS_INLINE void FetchFromCentralCache(size_t cl, size_t allocationSize);
  2469. void ReleaseToCentralCache(size_t cl, int N);
  2470. void Scavenge();
  2471. void Print() const;
  2472. // Record allocation of "k" bytes. Return true iff allocation
  2473. // should be sampled
  2474. bool SampleAllocation(size_t k);
  2475. // Pick next sampling point
  2476. void PickNextSample(size_t k);
  2477. static void InitModule();
  2478. static void InitTSD();
  2479. static TCMalloc_ThreadCache* GetThreadHeap();
  2480. static TCMalloc_ThreadCache* GetCache();
  2481. static TCMalloc_ThreadCache* GetCacheIfPresent();
  2482. static TCMalloc_ThreadCache* CreateCacheIfNecessary();
  2483. static void DeleteCache(TCMalloc_ThreadCache* heap);
  2484. static void BecomeIdle();
  2485. static void RecomputeThreadCacheSize();
  2486. #ifdef WTF_CHANGES
  2487. template <class Finder, class Reader>
  2488. void enumerateFreeObjects(Finder& finder, const Reader& reader)
  2489. {
  2490. for (unsigned sizeClass = 0; sizeClass < kNumClasses; sizeClass++)
  2491. list_[sizeClass].enumerateFreeObjects(finder, reader);
  2492. }
  2493. #endif
  2494. };
  2495. //-------------------------------------------------------------------
  2496. // Global variables
  2497. //-------------------------------------------------------------------
  2498. // Central cache -- a collection of free-lists, one per size-class.
  2499. // We have a separate lock per free-list to reduce contention.
  2500. static TCMalloc_Central_FreeListPadded central_cache[kNumClasses];
  2501. // Page-level allocator
  2502. static AllocAlignmentInteger pageheap_memory[(sizeof(TCMalloc_PageHeap) + sizeof(AllocAlignmentInteger) - 1) / sizeof(AllocAlignmentInteger)];
  2503. static bool phinited = false;
  2504. // Avoid extra level of indirection by making "pageheap" be just an alias
  2505. // of pageheap_memory.
  2506. typedef union {
  2507. void* m_memory;
  2508. TCMalloc_PageHeap* m_pageHeap;
  2509. } PageHeapUnion;
  2510. static inline TCMalloc_PageHeap* getPageHeap()
  2511. {
  2512. PageHeapUnion u = { &pageheap_memory[0] };
  2513. return u.m_pageHeap;
  2514. }
  2515. #define pageheap getPageHeap()
  2516. size_t fastMallocGoodSize(size_t bytes)
  2517. {
  2518. if (!phinited)
  2519. TCMalloc_ThreadCache::InitModule();
  2520. return AllocationSize(bytes);
  2521. }
  2522. #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
  2523. #if HAVE(DISPATCH_H) || OS(WINDOWS)
  2524. void TCMalloc_PageHeap::periodicScavenge()
  2525. {
  2526. SpinLockHolder h(&pageheap_lock);
  2527. pageheap->scavenge();
  2528. if (shouldScavenge()) {
  2529. rescheduleScavenger();
  2530. return;
  2531. }
  2532. suspendScavenger();
  2533. }
  2534. ALWAYS_INLINE void TCMalloc_PageHeap::signalScavenger()
  2535. {
  2536. ASSERT(pageheap_lock.IsHeld());
  2537. if (isScavengerSuspended() && shouldScavenge())
  2538. scheduleScavenger();
  2539. }
  2540. #else
  2541. void TCMalloc_PageHeap::scavengerThread()
  2542. {
  2543. #if HAVE(PTHREAD_SETNAME_NP)
  2544. pthread_setname_np("JavaScriptCore: FastMalloc scavenger");
  2545. #endif
  2546. while (1) {
  2547. pageheap_lock.Lock();
  2548. if (!shouldScavenge()) {
  2549. // Set to false so that signalScavenger() will check whether we need to be siganlled.
  2550. m_scavengeThreadActive = false;
  2551. // We need to unlock now, as this thread will block on the condvar until scavenging is required.
  2552. pageheap_lock.Unlock();
  2553. // Block until there are enough free committed pages to release back to the system.
  2554. pthread_mutex_lock(&m_scavengeMutex);
  2555. pthread_cond_wait(&m_scavengeCondition, &m_scavengeMutex);
  2556. // After exiting the pthread_cond_wait, we hold the lock on m_scavengeMutex. Unlock it to prevent
  2557. // deadlock next time round the loop.
  2558. pthread_mutex_unlock(&m_scavengeMutex);
  2559. // Set to true to prevent unnecessary signalling of the condvar.
  2560. m_scavengeThreadActive = true;
  2561. } else
  2562. pageheap_lock.Unlock();
  2563. // Wait for a while to calculate how much memory remains unused during this pause.
  2564. sleep(kScavengeDelayInSeconds);
  2565. {
  2566. SpinLockHolder h(&pageheap_lock);
  2567. pageheap->scavenge();
  2568. }
  2569. }
  2570. }
  2571. #endif
  2572. #endif
  2573. // If TLS is available, we also store a copy
  2574. // of the per-thread object in a __thread variable
  2575. // since __thread variables are faster to read
  2576. // than pthread_getspecific(). We still need
  2577. // pthread_setspecific() because __thread
  2578. // variables provide no way to run cleanup
  2579. // code when a thread is destroyed.
  2580. #ifdef HAVE_TLS
  2581. static __thread TCMalloc_ThreadCache *threadlocal_heap;
  2582. #endif
  2583. // Thread-specific key. Initialization here is somewhat tricky
  2584. // because some Linux startup code invokes malloc() before it
  2585. // is in a good enough state to handle pthread_keycreate().
  2586. // Therefore, we use TSD keys only after tsd_inited is set to true.
  2587. // Until then, we use a slow path to get the heap object.
  2588. static bool tsd_inited = false;
  2589. #if USE(PTHREAD_GETSPECIFIC_DIRECT)
  2590. static const pthread_key_t heap_key = __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0;
  2591. #else
  2592. static ThreadSpecificKey heap_key;
  2593. #endif
  2594. static ALWAYS_INLINE void setThreadHeap(TCMalloc_ThreadCache* heap)
  2595. {
  2596. #if USE(PTHREAD_GETSPECIFIC_DIRECT)
  2597. // Can't have two libraries both doing this in the same process,
  2598. // so check and make this crash right away.
  2599. if (pthread_getspecific(heap_key))
  2600. CRASH();
  2601. #endif
  2602. #if OS(DARWIN)
  2603. // Still do pthread_setspecific even if there's an alternate form
  2604. // of thread-local storage in use, to benefit from the delete callback.
  2605. pthread_setspecific(heap_key, heap);
  2606. #else
  2607. threadSpecificSet(heap_key, heap);
  2608. #endif
  2609. }
  2610. // Allocator for thread heaps
  2611. static PageHeapAllocator<TCMalloc_ThreadCache> threadheap_allocator;
  2612. // Linked list of heap objects. Protected by pageheap_lock.
  2613. static TCMalloc_ThreadCache* thread_heaps = NULL;
  2614. static int thread_heap_count = 0;
  2615. // Overall thread cache size. Protected by pageheap_lock.
  2616. static size_t overall_thread_cache_size = kDefaultOverallThreadCacheSize;
  2617. // Global per-thread cache size. Writes are protected by
  2618. // pageheap_lock. Reads are done without any locking, which should be
  2619. // fine as long as size_t can be written atomically and we don't place
  2620. // invariants between this variable and other pieces of state.
  2621. static volatile size_t per_thread_cache_size = kMaxThreadCacheSize;
  2622. //-------------------------------------------------------------------
  2623. // Central cache implementation
  2624. //-------------------------------------------------------------------
  2625. void TCMalloc_Central_FreeList::Init(size_t cl, uintptr_t entropy) {
  2626. lock_.Init();
  2627. size_class_ = cl;
  2628. entropy_ = entropy;
  2629. #if ENABLE(TCMALLOC_HARDENING)
  2630. ASSERT(entropy_);
  2631. #endif
  2632. DLL_Init(&empty_, entropy_);
  2633. DLL_Init(&nonempty_, entropy_);
  2634. counter_ = 0;
  2635. cache_size_ = 1;
  2636. used_slots_ = 0;
  2637. ASSERT(cache_size_ <= kNumTransferEntries);
  2638. }
  2639. void TCMalloc_Central_FreeList::ReleaseListToSpans(HardenedSLL start) {
  2640. while (start) {
  2641. HardenedSLL next = SLL_Next(start, entropy_);
  2642. ReleaseToSpans(start);
  2643. start = next;
  2644. }
  2645. }
  2646. ALWAYS_INLINE void TCMalloc_Central_FreeList::ReleaseToSpans(HardenedSLL object) {
  2647. const PageID p = reinterpret_cast<uintptr_t>(object.value()) >> kPageShift;
  2648. Span* span = pageheap->GetDescriptor(p);
  2649. ASSERT(span != NULL);
  2650. ASSERT(span->refcount > 0);
  2651. // If span is empty, move it to non-empty list
  2652. if (!span->objects) {
  2653. DLL_Remove(span, entropy_);
  2654. DLL_Prepend(&nonempty_, span, entropy_);
  2655. Event(span, 'N', 0);
  2656. }
  2657. // The following check is expensive, so it is disabled by default
  2658. if (false) {
  2659. // Check that object does not occur in list
  2660. unsigned got = 0;
  2661. for (HardenedSLL p = span->objects; !p; SLL_Next(p, entropy_)) {
  2662. ASSERT(p.value() != object.value());
  2663. got++;
  2664. }
  2665. ASSERT(got + span->refcount ==
  2666. (span->length<<kPageShift)/ByteSizeForClass(span->sizeclass));
  2667. }
  2668. counter_++;
  2669. span->refcount--;
  2670. if (span->refcount == 0) {
  2671. Event(span, '#', 0);
  2672. counter_ -= (span->length<<kPageShift) / ByteSizeForClass(span->sizeclass);
  2673. DLL_Remove(span, entropy_);
  2674. // Release central list lock while operating on pageheap
  2675. lock_.Unlock();
  2676. {
  2677. SpinLockHolder h(&pageheap_lock);
  2678. pageheap->Delete(span);
  2679. }
  2680. lock_.Lock();
  2681. } else {
  2682. SLL_SetNext(object, span->objects, entropy_);
  2683. span->objects.setValue(object.value());
  2684. }
  2685. }
  2686. ALWAYS_INLINE bool TCMalloc_Central_FreeList::EvictRandomSizeClass(
  2687. size_t locked_size_class, bool force) {
  2688. static int race_counter = 0;
  2689. int t = race_counter++; // Updated without a lock, but who cares.
  2690. if (t >= static_cast<int>(kNumClasses)) {
  2691. while (t >= static_cast<int>(kNumClasses)) {
  2692. t -= kNumClasses;
  2693. }
  2694. race_counter = t;
  2695. }
  2696. ASSERT(t >= 0);
  2697. ASSERT(t < static_cast<int>(kNumClasses));
  2698. if (t == static_cast<int>(locked_size_class)) return false;
  2699. return central_cache[t].ShrinkCache(static_cast<int>(locked_size_class), force);
  2700. }
  2701. bool TCMalloc_Central_FreeList::MakeCacheSpace() {
  2702. // Is there room in the cache?
  2703. if (used_slots_ < cache_size_) return true;
  2704. // Check if we can expand this cache?
  2705. if (cache_size_ == kNumTransferEntries) return false;
  2706. // Ok, we'll try to grab an entry from some other size class.
  2707. if (EvictRandomSizeClass(size_class_, false) ||
  2708. EvictRandomSizeClass(size_class_, true)) {
  2709. // Succeeded in evicting, we're going to make our cache larger.
  2710. cache_size_++;
  2711. return true;
  2712. }
  2713. return false;
  2714. }
  2715. namespace {
  2716. class LockInverter {
  2717. private:
  2718. SpinLock *held_, *temp_;
  2719. public:
  2720. inline explicit LockInverter(SpinLock* held, SpinLock *temp)
  2721. : held_(held), temp_(temp) { held_->Unlock(); temp_->Lock(); }
  2722. inline ~LockInverter() { temp_->Unlock(); held_->Lock(); }
  2723. };
  2724. }
  2725. bool TCMalloc_Central_FreeList::ShrinkCache(int locked_size_class, bool force) {
  2726. // Start with a quick check without taking a lock.
  2727. if (cache_size_ == 0) return false;
  2728. // We don't evict from a full cache unless we are 'forcing'.
  2729. if (force == false && used_slots_ == cache_size_) return false;
  2730. // Grab lock, but first release the other lock held by this thread. We use
  2731. // the lock inverter to ensure that we never hold two size class locks
  2732. // concurrently. That can create a deadlock because there is no well
  2733. // defined nesting order.
  2734. LockInverter li(&central_cache[locked_size_class].lock_, &lock_);
  2735. ASSERT(used_slots_ <= cache_size_);
  2736. ASSERT(0 <= cache_size_);
  2737. if (cache_size_ == 0) return false;
  2738. if (used_slots_ == cache_size_) {
  2739. if (force == false) return false;
  2740. // ReleaseListToSpans releases the lock, so we have to make all the
  2741. // updates to the central list before calling it.
  2742. cache_size_--;
  2743. used_slots_--;
  2744. ReleaseListToSpans(tc_slots_[used_slots_].head);
  2745. return true;
  2746. }
  2747. cache_size_--;
  2748. return true;
  2749. }
  2750. void TCMalloc_Central_FreeList::InsertRange(HardenedSLL start, HardenedSLL end, int N) {
  2751. SpinLockHolder h(&lock_);
  2752. if (N == num_objects_to_move[size_class_] &&
  2753. MakeCacheSpace()) {
  2754. int slot = used_slots_++;
  2755. ASSERT(slot >=0);
  2756. ASSERT(slot < kNumTransferEntries);
  2757. TCEntry *entry = &tc_slots_[slot];
  2758. entry->head = start;
  2759. entry->tail = end;
  2760. return;
  2761. }
  2762. ReleaseListToSpans(start);
  2763. }
  2764. ALWAYS_INLINE void TCMalloc_Central_FreeList::RemoveRange(HardenedSLL* start, HardenedSLL* end, int *N) {
  2765. int num = *N;
  2766. ASSERT(num > 0);
  2767. SpinLockHolder h(&lock_);
  2768. if (num == num_objects_to_move[size_class_] && used_slots_ > 0) {
  2769. int slot = --used_slots_;
  2770. ASSERT(slot >= 0);
  2771. TCEntry *entry = &tc_slots_[slot];
  2772. *start = entry->head;
  2773. *end = entry->tail;
  2774. return;
  2775. }
  2776. // TODO: Prefetch multiple TCEntries?
  2777. HardenedSLL tail = FetchFromSpansSafe();
  2778. if (!tail) {
  2779. // We are completely out of memory.
  2780. *start = *end = HardenedSLL::null();
  2781. *N = 0;
  2782. return;
  2783. }
  2784. SLL_SetNext(tail, HardenedSLL::null(), entropy_);
  2785. HardenedSLL head = tail;
  2786. int count = 1;
  2787. while (count < num) {
  2788. HardenedSLL t = FetchFromSpans();
  2789. if (!t) break;
  2790. SLL_Push(&head, t, entropy_);
  2791. count++;
  2792. }
  2793. *start = head;
  2794. *end = tail;
  2795. *N = count;
  2796. }
  2797. ALWAYS_INLINE HardenedSLL TCMalloc_Central_FreeList::FetchFromSpansSafe() {
  2798. HardenedSLL t = FetchFromSpans();
  2799. if (!t) {
  2800. Populate();
  2801. t = FetchFromSpans();
  2802. }
  2803. return t;
  2804. }
  2805. HardenedSLL TCMalloc_Central_FreeList::FetchFromSpans() {
  2806. if (DLL_IsEmpty(&nonempty_, entropy_)) return HardenedSLL::null();
  2807. Span* span = nonempty_.next(entropy_);
  2808. ASSERT(span->objects);
  2809. ASSERT_SPAN_COMMITTED(span);
  2810. span->refcount++;
  2811. HardenedSLL result = span->objects;
  2812. span->objects = SLL_Next(result, entropy_);
  2813. if (!span->objects) {
  2814. // Move to empty list
  2815. DLL_Remove(span, entropy_);
  2816. DLL_Prepend(&empty_, span, entropy_);
  2817. Event(span, 'E', 0);
  2818. }
  2819. counter_--;
  2820. return result;
  2821. }
  2822. // Fetch memory from the system and add to the central cache freelist.
  2823. ALWAYS_INLINE void TCMalloc_Central_FreeList::Populate() {
  2824. // Release central list lock while operating on pageheap
  2825. lock_.Unlock();
  2826. const size_t npages = class_to_pages[size_class_];
  2827. Span* span;
  2828. {
  2829. SpinLockHolder h(&pageheap_lock);
  2830. span = pageheap->New(npages);
  2831. if (span) pageheap->RegisterSizeClass(span, size_class_);
  2832. }
  2833. if (span == NULL) {
  2834. #if HAVE(ERRNO_H)
  2835. MESSAGE("allocation failed: %d\n", errno);
  2836. #elif OS(WINDOWS)
  2837. MESSAGE("allocation failed: %d\n", ::GetLastError());
  2838. #else
  2839. MESSAGE("allocation failed\n");
  2840. #endif
  2841. lock_.Lock();
  2842. return;
  2843. }
  2844. ASSERT_SPAN_COMMITTED(span);
  2845. ASSERT(span->length == npages);
  2846. // Cache sizeclass info eagerly. Locking is not necessary.
  2847. // (Instead of being eager, we could just replace any stale info
  2848. // about this span, but that seems to be no better in practice.)
  2849. for (size_t i = 0; i < npages; i++) {
  2850. pageheap->CacheSizeClass(span->start + i, size_class_);
  2851. }
  2852. // Split the block into pieces and add to the free-list
  2853. // TODO: coloring of objects to avoid cache conflicts?
  2854. HardenedSLL head = HardenedSLL::null();
  2855. char* start = reinterpret_cast<char*>(span->start << kPageShift);
  2856. const size_t size = ByteSizeForClass(size_class_);
  2857. char* ptr = start + (npages << kPageShift) - ((npages << kPageShift) % size);
  2858. int num = 0;
  2859. #if ENABLE(TCMALLOC_HARDENING)
  2860. uint32_t startPoison = freedObjectStartPoison();
  2861. uint32_t endPoison = freedObjectEndPoison();
  2862. #endif
  2863. while (ptr > start) {
  2864. ptr -= size;
  2865. HardenedSLL node = HardenedSLL::create(ptr);
  2866. POISON_DEALLOCATION_EXPLICIT(ptr, size, startPoison, endPoison);
  2867. SLL_SetNext(node, head, entropy_);
  2868. head = node;
  2869. num++;
  2870. }
  2871. ASSERT(ptr == start);
  2872. ASSERT(ptr == head.value());
  2873. #ifndef NDEBUG
  2874. {
  2875. HardenedSLL node = head;
  2876. while (node) {
  2877. ASSERT(IS_DEFINITELY_POISONED(node.value(), size));
  2878. node = SLL_Next(node, entropy_);
  2879. }
  2880. }
  2881. #endif
  2882. span->objects = head;
  2883. ASSERT(span->objects.value() == head.value());
  2884. span->refcount = 0; // No sub-object in use yet
  2885. // Add span to list of non-empty spans
  2886. lock_.Lock();
  2887. DLL_Prepend(&nonempty_, span, entropy_);
  2888. counter_ += num;
  2889. }
  2890. //-------------------------------------------------------------------
  2891. // TCMalloc_ThreadCache implementation
  2892. //-------------------------------------------------------------------
  2893. inline bool TCMalloc_ThreadCache::SampleAllocation(size_t k) {
  2894. if (bytes_until_sample_ < k) {
  2895. PickNextSample(k);
  2896. return true;
  2897. } else {
  2898. bytes_until_sample_ -= k;
  2899. return false;
  2900. }
  2901. }
  2902. void TCMalloc_ThreadCache::Init(ThreadIdentifier tid, uintptr_t entropy) {
  2903. size_ = 0;
  2904. next_ = NULL;
  2905. prev_ = NULL;
  2906. tid_ = tid;
  2907. in_setspecific_ = false;
  2908. entropy_ = entropy;
  2909. #if ENABLE(TCMALLOC_HARDENING)
  2910. ASSERT(entropy_);
  2911. #endif
  2912. for (size_t cl = 0; cl < kNumClasses; ++cl) {
  2913. list_[cl].Init(entropy_);
  2914. }
  2915. // Initialize RNG -- run it for a bit to get to good values
  2916. bytes_until_sample_ = 0;
  2917. rnd_ = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this));
  2918. for (int i = 0; i < 100; i++) {
  2919. PickNextSample(static_cast<size_t>(FLAGS_tcmalloc_sample_parameter * 2));
  2920. }
  2921. }
  2922. void TCMalloc_ThreadCache::Cleanup() {
  2923. // Put unused memory back into central cache
  2924. for (size_t cl = 0; cl < kNumClasses; ++cl) {
  2925. if (list_[cl].length() > 0) {
  2926. ReleaseToCentralCache(cl, list_[cl].length());
  2927. }
  2928. }
  2929. }
  2930. ALWAYS_INLINE void* TCMalloc_ThreadCache::Allocate(size_t size) {
  2931. ASSERT(size <= kMaxSize);
  2932. const size_t cl = SizeClass(size);
  2933. FreeList* list = &list_[cl];
  2934. size_t allocationSize = ByteSizeForClass(cl);
  2935. if (list->empty()) {
  2936. FetchFromCentralCache(cl, allocationSize);
  2937. if (list->empty()) return NULL;
  2938. }
  2939. size_ -= allocationSize;
  2940. void* result = list->Pop();
  2941. if (!result)
  2942. return 0;
  2943. RELEASE_ASSERT(IS_DEFINITELY_POISONED(result, allocationSize));
  2944. POISON_ALLOCATION(result, allocationSize);
  2945. return result;
  2946. }
  2947. inline void TCMalloc_ThreadCache::Deallocate(HardenedSLL ptr, size_t cl) {
  2948. size_t allocationSize = ByteSizeForClass(cl);
  2949. size_ += allocationSize;
  2950. FreeList* list = &list_[cl];
  2951. if (MAY_BE_POISONED(ptr.value(), allocationSize))
  2952. list->Validate(ptr, allocationSize);
  2953. POISON_DEALLOCATION(ptr.value(), allocationSize);
  2954. list->Push(ptr);
  2955. // If enough data is free, put back into central cache
  2956. if (list->length() > kMaxFreeListLength) {
  2957. ReleaseToCentralCache(cl, num_objects_to_move[cl]);
  2958. }
  2959. if (size_ >= per_thread_cache_size) Scavenge();
  2960. }
  2961. // Remove some objects of class "cl" from central cache and add to thread heap
  2962. ALWAYS_INLINE void TCMalloc_ThreadCache::FetchFromCentralCache(size_t cl, size_t allocationSize) {
  2963. int fetch_count = num_objects_to_move[cl];
  2964. HardenedSLL start, end;
  2965. central_cache[cl].RemoveRange(&start, &end, &fetch_count);
  2966. list_[cl].PushRange(fetch_count, start, end);
  2967. size_ += allocationSize * fetch_count;
  2968. }
  2969. // Remove some objects of class "cl" from thread heap and add to central cache
  2970. inline void TCMalloc_ThreadCache::ReleaseToCentralCache(size_t cl, int N) {
  2971. ASSERT(N > 0);
  2972. FreeList* src = &list_[cl];
  2973. if (N > src->length()) N = src->length();
  2974. size_ -= N*ByteSizeForClass(cl);
  2975. // We return prepackaged chains of the correct size to the central cache.
  2976. // TODO: Use the same format internally in the thread caches?
  2977. int batch_size = num_objects_to_move[cl];
  2978. while (N > batch_size) {
  2979. HardenedSLL tail, head;
  2980. src->PopRange(batch_size, &head, &tail);
  2981. central_cache[cl].InsertRange(head, tail, batch_size);
  2982. N -= batch_size;
  2983. }
  2984. HardenedSLL tail, head;
  2985. src->PopRange(N, &head, &tail);
  2986. central_cache[cl].InsertRange(head, tail, N);
  2987. }
  2988. // Release idle memory to the central cache
  2989. inline void TCMalloc_ThreadCache::Scavenge() {
  2990. // If the low-water mark for the free list is L, it means we would
  2991. // not have had to allocate anything from the central cache even if
  2992. // we had reduced the free list size by L. We aim to get closer to
  2993. // that situation by dropping L/2 nodes from the free list. This
  2994. // may not release much memory, but if so we will call scavenge again
  2995. // pretty soon and the low-water marks will be high on that call.
  2996. //int64 start = CycleClock::Now();
  2997. for (size_t cl = 0; cl < kNumClasses; cl++) {
  2998. FreeList* list = &list_[cl];
  2999. const int lowmark = list->lowwatermark();
  3000. if (lowmark > 0) {
  3001. const int drop = (lowmark > 1) ? lowmark/2 : 1;
  3002. ReleaseToCentralCache(cl, drop);
  3003. }
  3004. list->clear_lowwatermark();
  3005. }
  3006. //int64 finish = CycleClock::Now();
  3007. //CycleTimer ct;
  3008. //MESSAGE("GC: %.0f ns\n", ct.CyclesToUsec(finish-start)*1000.0);
  3009. }
  3010. void TCMalloc_ThreadCache::PickNextSample(size_t k) {
  3011. // Make next "random" number
  3012. // x^32+x^22+x^2+x^1+1 is a primitive polynomial for random numbers
  3013. static const uint32_t kPoly = (1 << 22) | (1 << 2) | (1 << 1) | (1 << 0);
  3014. uint32_t r = rnd_;
  3015. rnd_ = (r << 1) ^ ((static_cast<int32_t>(r) >> 31) & kPoly);
  3016. // Next point is "rnd_ % (sample_period)". I.e., average
  3017. // increment is "sample_period/2".
  3018. const int flag_value = static_cast<int>(FLAGS_tcmalloc_sample_parameter);
  3019. static int last_flag_value = -1;
  3020. if (flag_value != last_flag_value) {
  3021. SpinLockHolder h(&sample_period_lock);
  3022. int i;
  3023. for (i = 0; i < (static_cast<int>(sizeof(primes_list)/sizeof(primes_list[0])) - 1); i++) {
  3024. if (primes_list[i] >= flag_value) {
  3025. break;
  3026. }
  3027. }
  3028. sample_period = primes_list[i];
  3029. last_flag_value = flag_value;
  3030. }
  3031. bytes_until_sample_ += rnd_ % sample_period;
  3032. if (k > (static_cast<size_t>(-1) >> 2)) {
  3033. // If the user has asked for a huge allocation then it is possible
  3034. // for the code below to loop infinitely. Just return (note that
  3035. // this throws off the sampling accuracy somewhat, but a user who
  3036. // is allocating more than 1G of memory at a time can live with a
  3037. // minor inaccuracy in profiling of small allocations, and also
  3038. // would rather not wait for the loop below to terminate).
  3039. return;
  3040. }
  3041. while (bytes_until_sample_ < k) {
  3042. // Increase bytes_until_sample_ by enough average sampling periods
  3043. // (sample_period >> 1) to allow us to sample past the current
  3044. // allocation.
  3045. bytes_until_sample_ += (sample_period >> 1);
  3046. }
  3047. bytes_until_sample_ -= k;
  3048. }
  3049. void TCMalloc_ThreadCache::InitModule() {
  3050. // There is a slight potential race here because of double-checked
  3051. // locking idiom. However, as long as the program does a small
  3052. // allocation before switching to multi-threaded mode, we will be
  3053. // fine. We increase the chances of doing such a small allocation
  3054. // by doing one in the constructor of the module_enter_exit_hook
  3055. // object declared below.
  3056. SpinLockHolder h(&pageheap_lock);
  3057. if (!phinited) {
  3058. uintptr_t entropy = HARDENING_ENTROPY;
  3059. #ifdef WTF_CHANGES
  3060. InitTSD();
  3061. #endif
  3062. InitSizeClasses();
  3063. threadheap_allocator.Init(entropy);
  3064. span_allocator.Init(entropy);
  3065. span_allocator.New(); // Reduce cache conflicts
  3066. span_allocator.New(); // Reduce cache conflicts
  3067. stacktrace_allocator.Init(entropy);
  3068. DLL_Init(&sampled_objects, entropy);
  3069. for (size_t i = 0; i < kNumClasses; ++i) {
  3070. central_cache[i].Init(i, entropy);
  3071. }
  3072. pageheap->init();
  3073. phinited = 1;
  3074. #if defined(WTF_CHANGES) && OS(DARWIN)
  3075. MallocHook::init();
  3076. FastMallocZone::init();
  3077. #endif
  3078. }
  3079. }
  3080. inline TCMalloc_ThreadCache* TCMalloc_ThreadCache::NewHeap(ThreadIdentifier tid, uintptr_t entropy) {
  3081. // Create the heap and add it to the linked list
  3082. TCMalloc_ThreadCache *heap = threadheap_allocator.New();
  3083. heap->Init(tid, entropy);
  3084. heap->next_ = thread_heaps;
  3085. heap->prev_ = NULL;
  3086. if (thread_heaps != NULL) thread_heaps->prev_ = heap;
  3087. thread_heaps = heap;
  3088. thread_heap_count++;
  3089. RecomputeThreadCacheSize();
  3090. return heap;
  3091. }
  3092. inline TCMalloc_ThreadCache* TCMalloc_ThreadCache::GetThreadHeap() {
  3093. #ifdef HAVE_TLS
  3094. // __thread is faster, but only when the kernel supports it
  3095. if (KernelSupportsTLS())
  3096. return threadlocal_heap;
  3097. #elif OS(DARWIN)
  3098. return static_cast<TCMalloc_ThreadCache*>(pthread_getspecific(heap_key));
  3099. #else
  3100. return static_cast<TCMalloc_ThreadCache*>(threadSpecificGet(heap_key));
  3101. #endif
  3102. }
  3103. inline TCMalloc_ThreadCache* TCMalloc_ThreadCache::GetCache() {
  3104. TCMalloc_ThreadCache* ptr = NULL;
  3105. if (!tsd_inited) {
  3106. InitModule();
  3107. } else {
  3108. ptr = GetThreadHeap();
  3109. }
  3110. if (ptr == NULL) ptr = CreateCacheIfNecessary();
  3111. return ptr;
  3112. }
  3113. // In deletion paths, we do not try to create a thread-cache. This is
  3114. // because we may be in the thread destruction code and may have
  3115. // already cleaned up the cache for this thread.
  3116. inline TCMalloc_ThreadCache* TCMalloc_ThreadCache::GetCacheIfPresent() {
  3117. if (!tsd_inited) return NULL;
  3118. void* const p = GetThreadHeap();
  3119. return reinterpret_cast<TCMalloc_ThreadCache*>(p);
  3120. }
  3121. void TCMalloc_ThreadCache::InitTSD() {
  3122. ASSERT(!tsd_inited);
  3123. #if USE(PTHREAD_GETSPECIFIC_DIRECT)
  3124. pthread_key_init_np(heap_key, DestroyThreadCache);
  3125. #else
  3126. threadSpecificKeyCreate(&heap_key, DestroyThreadCache);
  3127. #endif
  3128. tsd_inited = true;
  3129. #if !OS(WINDOWS)
  3130. // We may have used a fake pthread_t for the main thread. Fix it.
  3131. pthread_t zero;
  3132. memset(&zero, 0, sizeof(zero));
  3133. #endif
  3134. #ifndef WTF_CHANGES
  3135. SpinLockHolder h(&pageheap_lock);
  3136. #else
  3137. ASSERT(pageheap_lock.IsHeld());
  3138. #endif
  3139. for (TCMalloc_ThreadCache* h = thread_heaps; h != NULL; h = h->next_) {
  3140. #if OS(WINDOWS)
  3141. if (h->tid_ == 0) {
  3142. h->tid_ = GetCurrentThreadId();
  3143. }
  3144. #else
  3145. if (pthread_equal(h->tid_, zero)) {
  3146. h->tid_ = pthread_self();
  3147. }
  3148. #endif
  3149. }
  3150. }
  3151. TCMalloc_ThreadCache* TCMalloc_ThreadCache::CreateCacheIfNecessary() {
  3152. // Initialize per-thread data if necessary
  3153. TCMalloc_ThreadCache* heap = NULL;
  3154. {
  3155. SpinLockHolder h(&pageheap_lock);
  3156. #if OS(WINDOWS)
  3157. DWORD me;
  3158. if (!tsd_inited) {
  3159. me = 0;
  3160. } else {
  3161. me = GetCurrentThreadId();
  3162. }
  3163. #else
  3164. // Early on in glibc's life, we cannot even call pthread_self()
  3165. pthread_t me;
  3166. if (!tsd_inited) {
  3167. memset(&me, 0, sizeof(me));
  3168. } else {
  3169. me = pthread_self();
  3170. }
  3171. #endif
  3172. // This may be a recursive malloc call from pthread_setspecific()
  3173. // In that case, the heap for this thread has already been created
  3174. // and added to the linked list. So we search for that first.
  3175. for (TCMalloc_ThreadCache* h = thread_heaps; h != NULL; h = h->next_) {
  3176. #if OS(WINDOWS)
  3177. if (h->tid_ == me) {
  3178. #else
  3179. if (pthread_equal(h->tid_, me)) {
  3180. #endif
  3181. heap = h;
  3182. break;
  3183. }
  3184. }
  3185. if (heap == NULL) heap = NewHeap(me, HARDENING_ENTROPY);
  3186. }
  3187. // We call pthread_setspecific() outside the lock because it may
  3188. // call malloc() recursively. The recursive call will never get
  3189. // here again because it will find the already allocated heap in the
  3190. // linked list of heaps.
  3191. if (!heap->in_setspecific_ && tsd_inited) {
  3192. heap->in_setspecific_ = true;
  3193. setThreadHeap(heap);
  3194. }
  3195. return heap;
  3196. }
  3197. void TCMalloc_ThreadCache::BecomeIdle() {
  3198. if (!tsd_inited) return; // No caches yet
  3199. TCMalloc_ThreadCache* heap = GetThreadHeap();
  3200. if (heap == NULL) return; // No thread cache to remove
  3201. if (heap->in_setspecific_) return; // Do not disturb the active caller
  3202. heap->in_setspecific_ = true;
  3203. setThreadHeap(NULL);
  3204. #ifdef HAVE_TLS
  3205. // Also update the copy in __thread
  3206. threadlocal_heap = NULL;
  3207. #endif
  3208. heap->in_setspecific_ = false;
  3209. if (GetThreadHeap() == heap) {
  3210. // Somehow heap got reinstated by a recursive call to malloc
  3211. // from pthread_setspecific. We give up in this case.
  3212. return;
  3213. }
  3214. // We can now get rid of the heap
  3215. DeleteCache(heap);
  3216. }
  3217. void TCMalloc_ThreadCache::DestroyThreadCache(void* ptr) {
  3218. // Note that "ptr" cannot be NULL since pthread promises not
  3219. // to invoke the destructor on NULL values, but for safety,
  3220. // we check anyway.
  3221. if (ptr == NULL) return;
  3222. #ifdef HAVE_TLS
  3223. // Prevent fast path of GetThreadHeap() from returning heap.
  3224. threadlocal_heap = NULL;
  3225. #endif
  3226. DeleteCache(reinterpret_cast<TCMalloc_ThreadCache*>(ptr));
  3227. }
  3228. void TCMalloc_ThreadCache::DeleteCache(TCMalloc_ThreadCache* heap) {
  3229. // Remove all memory from heap
  3230. heap->Cleanup();
  3231. // Remove from linked list
  3232. SpinLockHolder h(&pageheap_lock);
  3233. if (heap->next_ != NULL) heap->next_->prev_ = heap->prev_;
  3234. if (heap->prev_ != NULL) heap->prev_->next_ = heap->next_;
  3235. if (thread_heaps == heap) thread_heaps = heap->next_;
  3236. thread_heap_count--;
  3237. RecomputeThreadCacheSize();
  3238. threadheap_allocator.Delete(heap);
  3239. }
  3240. void TCMalloc_ThreadCache::RecomputeThreadCacheSize() {
  3241. // Divide available space across threads
  3242. int n = thread_heap_count > 0 ? thread_heap_count : 1;
  3243. size_t space = overall_thread_cache_size / n;
  3244. // Limit to allowed range
  3245. if (space < kMinThreadCacheSize) space = kMinThreadCacheSize;
  3246. if (space > kMaxThreadCacheSize) space = kMaxThreadCacheSize;
  3247. per_thread_cache_size = space;
  3248. }
  3249. void TCMalloc_ThreadCache::Print() const {
  3250. for (size_t cl = 0; cl < kNumClasses; ++cl) {
  3251. MESSAGE(" %5" PRIuS " : %4d len; %4d lo\n",
  3252. ByteSizeForClass(cl),
  3253. list_[cl].length(),
  3254. list_[cl].lowwatermark());
  3255. }
  3256. }
  3257. // Extract interesting stats
  3258. struct TCMallocStats {
  3259. uint64_t system_bytes; // Bytes alloced from system
  3260. uint64_t thread_bytes; // Bytes in thread caches
  3261. uint64_t central_bytes; // Bytes in central cache
  3262. uint64_t transfer_bytes; // Bytes in central transfer cache
  3263. uint64_t pageheap_bytes; // Bytes in page heap
  3264. uint64_t metadata_bytes; // Bytes alloced for metadata
  3265. };
  3266. #ifndef WTF_CHANGES
  3267. // Get stats into "r". Also get per-size-class counts if class_count != NULL
  3268. static void ExtractStats(TCMallocStats* r, uint64_t* class_count) {
  3269. r->central_bytes = 0;
  3270. r->transfer_bytes = 0;
  3271. for (int cl = 0; cl < kNumClasses; ++cl) {
  3272. const int length = central_cache[cl].length();
  3273. const int tc_length = central_cache[cl].tc_length();
  3274. r->central_bytes += static_cast<uint64_t>(ByteSizeForClass(cl)) * length;
  3275. r->transfer_bytes +=
  3276. static_cast<uint64_t>(ByteSizeForClass(cl)) * tc_length;
  3277. if (class_count) class_count[cl] = length + tc_length;
  3278. }
  3279. // Add stats from per-thread heaps
  3280. r->thread_bytes = 0;
  3281. { // scope
  3282. SpinLockHolder h(&pageheap_lock);
  3283. for (TCMalloc_ThreadCache* h = thread_heaps; h != NULL; h = h->next_) {
  3284. r->thread_bytes += h->Size();
  3285. if (class_count) {
  3286. for (size_t cl = 0; cl < kNumClasses; ++cl) {
  3287. class_count[cl] += h->freelist_length(cl);
  3288. }
  3289. }
  3290. }
  3291. }
  3292. { //scope
  3293. SpinLockHolder h(&pageheap_lock);
  3294. r->system_bytes = pageheap->SystemBytes();
  3295. r->metadata_bytes = metadata_system_bytes;
  3296. r->pageheap_bytes = pageheap->FreeBytes();
  3297. }
  3298. }
  3299. #endif
  3300. #ifndef WTF_CHANGES
  3301. // WRITE stats to "out"
  3302. static void DumpStats(TCMalloc_Printer* out, int level) {
  3303. TCMallocStats stats;
  3304. uint64_t class_count[kNumClasses];
  3305. ExtractStats(&stats, (level >= 2 ? class_count : NULL));
  3306. if (level >= 2) {
  3307. out->printf("------------------------------------------------\n");
  3308. uint64_t cumulative = 0;
  3309. for (int cl = 0; cl < kNumClasses; ++cl) {
  3310. if (class_count[cl] > 0) {
  3311. uint64_t class_bytes = class_count[cl] * ByteSizeForClass(cl);
  3312. cumulative += class_bytes;
  3313. out->printf("class %3d [ %8" PRIuS " bytes ] : "
  3314. "%8" PRIu64 " objs; %5.1f MB; %5.1f cum MB\n",
  3315. cl, ByteSizeForClass(cl),
  3316. class_count[cl],
  3317. class_bytes / 1048576.0,
  3318. cumulative / 1048576.0);
  3319. }
  3320. }
  3321. SpinLockHolder h(&pageheap_lock);
  3322. pageheap->Dump(out);
  3323. }
  3324. const uint64_t bytes_in_use = stats.system_bytes
  3325. - stats.pageheap_bytes
  3326. - stats.central_bytes
  3327. - stats.transfer_bytes
  3328. - stats.thread_bytes;
  3329. out->printf("------------------------------------------------\n"
  3330. "MALLOC: %12" PRIu64 " Heap size\n"
  3331. "MALLOC: %12" PRIu64 " Bytes in use by application\n"
  3332. "MALLOC: %12" PRIu64 " Bytes free in page heap\n"
  3333. "MALLOC: %12" PRIu64 " Bytes free in central cache\n"
  3334. "MALLOC: %12" PRIu64 " Bytes free in transfer cache\n"
  3335. "MALLOC: %12" PRIu64 " Bytes free in thread caches\n"
  3336. "MALLOC: %12" PRIu64 " Spans in use\n"
  3337. "MALLOC: %12" PRIu64 " Thread heaps in use\n"
  3338. "MALLOC: %12" PRIu64 " Metadata allocated\n"
  3339. "------------------------------------------------\n",
  3340. stats.system_bytes,
  3341. bytes_in_use,
  3342. stats.pageheap_bytes,
  3343. stats.central_bytes,
  3344. stats.transfer_bytes,
  3345. stats.thread_bytes,
  3346. uint64_t(span_allocator.inuse()),
  3347. uint64_t(threadheap_allocator.inuse()),
  3348. stats.metadata_bytes);
  3349. }
  3350. static void PrintStats(int level) {
  3351. const int kBufferSize = 16 << 10;
  3352. char* buffer = new char[kBufferSize];
  3353. TCMalloc_Printer printer(buffer, kBufferSize);
  3354. DumpStats(&printer, level);
  3355. write(STDERR_FILENO, buffer, strlen(buffer));
  3356. delete[] buffer;
  3357. }
  3358. static void** DumpStackTraces() {
  3359. // Count how much space we need
  3360. int needed_slots = 0;
  3361. {
  3362. SpinLockHolder h(&pageheap_lock);
  3363. for (Span* s = sampled_objects.next; s != &sampled_objects; s = s->next) {
  3364. StackTrace* stack = reinterpret_cast<StackTrace*>(s->objects);
  3365. needed_slots += 3 + stack->depth;
  3366. }
  3367. needed_slots += 100; // Slop in case sample grows
  3368. needed_slots += needed_slots/8; // An extra 12.5% slop
  3369. }
  3370. void** result = new void*[needed_slots];
  3371. if (result == NULL) {
  3372. MESSAGE("tcmalloc: could not allocate %d slots for stack traces\n",
  3373. needed_slots);
  3374. return NULL;
  3375. }
  3376. SpinLockHolder h(&pageheap_lock);
  3377. int used_slots = 0;
  3378. for (Span* s = sampled_objects.next; s != &sampled_objects; s = s->next) {
  3379. ASSERT(used_slots < needed_slots); // Need to leave room for terminator
  3380. StackTrace* stack = reinterpret_cast<StackTrace*>(s->objects);
  3381. if (used_slots + 3 + stack->depth >= needed_slots) {
  3382. // No more room
  3383. break;
  3384. }
  3385. result[used_slots+0] = reinterpret_cast<void*>(static_cast<uintptr_t>(1));
  3386. result[used_slots+1] = reinterpret_cast<void*>(stack->size);
  3387. result[used_slots+2] = reinterpret_cast<void*>(stack->depth);
  3388. for (int d = 0; d < stack->depth; d++) {
  3389. result[used_slots+3+d] = stack->stack[d];
  3390. }
  3391. used_slots += 3 + stack->depth;
  3392. }
  3393. result[used_slots] = reinterpret_cast<void*>(static_cast<uintptr_t>(0));
  3394. return result;
  3395. }
  3396. #endif
  3397. #ifndef WTF_CHANGES
  3398. // TCMalloc's support for extra malloc interfaces
  3399. class TCMallocImplementation : public MallocExtension {
  3400. public:
  3401. virtual void GetStats(char* buffer, int buffer_length) {
  3402. ASSERT(buffer_length > 0);
  3403. TCMalloc_Printer printer(buffer, buffer_length);
  3404. // Print level one stats unless lots of space is available
  3405. if (buffer_length < 10000) {
  3406. DumpStats(&printer, 1);
  3407. } else {
  3408. DumpStats(&printer, 2);
  3409. }
  3410. }
  3411. virtual void** ReadStackTraces() {
  3412. return DumpStackTraces();
  3413. }
  3414. virtual bool GetNumericProperty(const char* name, size_t* value) {
  3415. ASSERT(name != NULL);
  3416. if (strcmp(name, "generic.current_allocated_bytes") == 0) {
  3417. TCMallocStats stats;
  3418. ExtractStats(&stats, NULL);
  3419. *value = stats.system_bytes
  3420. - stats.thread_bytes
  3421. - stats.central_bytes
  3422. - stats.pageheap_bytes;
  3423. return true;
  3424. }
  3425. if (strcmp(name, "generic.heap_size") == 0) {
  3426. TCMallocStats stats;
  3427. ExtractStats(&stats, NULL);
  3428. *value = stats.system_bytes;
  3429. return true;
  3430. }
  3431. if (strcmp(name, "tcmalloc.slack_bytes") == 0) {
  3432. // We assume that bytes in the page heap are not fragmented too
  3433. // badly, and are therefore available for allocation.
  3434. SpinLockHolder l(&pageheap_lock);
  3435. *value = pageheap->FreeBytes();
  3436. return true;
  3437. }
  3438. if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) {
  3439. SpinLockHolder l(&pageheap_lock);
  3440. *value = overall_thread_cache_size;
  3441. return true;
  3442. }
  3443. if (strcmp(name, "tcmalloc.current_total_thread_cache_bytes") == 0) {
  3444. TCMallocStats stats;
  3445. ExtractStats(&stats, NULL);
  3446. *value = stats.thread_bytes;
  3447. return true;
  3448. }
  3449. return false;
  3450. }
  3451. virtual bool SetNumericProperty(const char* name, size_t value) {
  3452. ASSERT(name != NULL);
  3453. if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) {
  3454. // Clip the value to a reasonable range
  3455. if (value < kMinThreadCacheSize) value = kMinThreadCacheSize;
  3456. if (value > (1<<30)) value = (1<<30); // Limit to 1GB
  3457. SpinLockHolder l(&pageheap_lock);
  3458. overall_thread_cache_size = static_cast<size_t>(value);
  3459. TCMalloc_ThreadCache::RecomputeThreadCacheSize();
  3460. return true;
  3461. }
  3462. return false;
  3463. }
  3464. virtual void MarkThreadIdle() {
  3465. TCMalloc_ThreadCache::BecomeIdle();
  3466. }
  3467. virtual void ReleaseFreeMemory() {
  3468. SpinLockHolder h(&pageheap_lock);
  3469. pageheap->ReleaseFreePages();
  3470. }
  3471. };
  3472. #endif
  3473. // The constructor allocates an object to ensure that initialization
  3474. // runs before main(), and therefore we do not have a chance to become
  3475. // multi-threaded before initialization. We also create the TSD key
  3476. // here. Presumably by the time this constructor runs, glibc is in
  3477. // good enough shape to handle pthread_key_create().
  3478. //
  3479. // The constructor also takes the opportunity to tell STL to use
  3480. // tcmalloc. We want to do this early, before construct time, so
  3481. // all user STL allocations go through tcmalloc (which works really
  3482. // well for STL).
  3483. //
  3484. // The destructor prints stats when the program exits.
  3485. class TCMallocGuard {
  3486. public:
  3487. TCMallocGuard() {
  3488. #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS
  3489. // Check whether the kernel also supports TLS (needs to happen at runtime)
  3490. CheckIfKernelSupportsTLS();
  3491. #endif
  3492. #ifndef WTF_CHANGES
  3493. #ifdef WIN32 // patch the windows VirtualAlloc, etc.
  3494. PatchWindowsFunctions(); // defined in windows/patch_functions.cc
  3495. #endif
  3496. #endif
  3497. free(malloc(1));
  3498. TCMalloc_ThreadCache::InitTSD();
  3499. free(malloc(1));
  3500. #ifndef WTF_CHANGES
  3501. MallocExtension::Register(new TCMallocImplementation);
  3502. #endif
  3503. }
  3504. #ifndef WTF_CHANGES
  3505. ~TCMallocGuard() {
  3506. const char* env = getenv("MALLOCSTATS");
  3507. if (env != NULL) {
  3508. int level = atoi(env);
  3509. if (level < 1) level = 1;
  3510. PrintStats(level);
  3511. }
  3512. #ifdef WIN32
  3513. UnpatchWindowsFunctions();
  3514. #endif
  3515. }
  3516. #endif
  3517. };
  3518. #ifndef WTF_CHANGES
  3519. static TCMallocGuard module_enter_exit_hook;
  3520. #endif
  3521. //-------------------------------------------------------------------
  3522. // Helpers for the exported routines below
  3523. //-------------------------------------------------------------------
  3524. #ifndef WTF_CHANGES
  3525. static Span* DoSampledAllocation(size_t size) {
  3526. // Grab the stack trace outside the heap lock
  3527. StackTrace tmp;
  3528. tmp.depth = GetStackTrace(tmp.stack, kMaxStackDepth, 1);
  3529. tmp.size = size;
  3530. SpinLockHolder h(&pageheap_lock);
  3531. // Allocate span
  3532. Span *span = pageheap->New(pages(size == 0 ? 1 : size));
  3533. if (span == NULL) {
  3534. return NULL;
  3535. }
  3536. // Allocate stack trace
  3537. StackTrace *stack = stacktrace_allocator.New();
  3538. if (stack == NULL) {
  3539. // Sampling failed because of lack of memory
  3540. return span;
  3541. }
  3542. *stack = tmp;
  3543. span->sample = 1;
  3544. span->objects = stack;
  3545. DLL_Prepend(&sampled_objects, span);
  3546. return span;
  3547. }
  3548. #endif
  3549. #if !ASSERT_DISABLED
  3550. static inline bool CheckCachedSizeClass(void *ptr) {
  3551. PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift;
  3552. size_t cached_value = pageheap->GetSizeClassIfCached(p);
  3553. return cached_value == 0 ||
  3554. cached_value == pageheap->GetDescriptor(p)->sizeclass;
  3555. }
  3556. #endif
  3557. static inline void* CheckedMallocResult(void *result)
  3558. {
  3559. ASSERT(result == 0 || CheckCachedSizeClass(result));
  3560. return result;
  3561. }
  3562. static inline void* SpanToMallocResult(Span *span) {
  3563. ASSERT_SPAN_COMMITTED(span);
  3564. pageheap->CacheSizeClass(span->start, 0);
  3565. void* result = reinterpret_cast<void*>(span->start << kPageShift);
  3566. POISON_ALLOCATION(result, span->length << kPageShift);
  3567. return CheckedMallocResult(result);
  3568. }
  3569. #ifdef WTF_CHANGES
  3570. template <bool crashOnFailure>
  3571. #endif
  3572. static ALWAYS_INLINE void* do_malloc(size_t size) {
  3573. void* ret = NULL;
  3574. #ifdef WTF_CHANGES
  3575. ASSERT(!isForbidden());
  3576. #endif
  3577. // The following call forces module initialization
  3578. TCMalloc_ThreadCache* heap = TCMalloc_ThreadCache::GetCache();
  3579. #ifndef WTF_CHANGES
  3580. if ((FLAGS_tcmalloc_sample_parameter > 0) && heap->SampleAllocation(size)) {
  3581. Span* span = DoSampledAllocation(size);
  3582. if (span != NULL) {
  3583. ret = SpanToMallocResult(span);
  3584. }
  3585. } else
  3586. #endif
  3587. if (size > kMaxSize) {
  3588. // Use page-level allocator
  3589. SpinLockHolder h(&pageheap_lock);
  3590. Span* span = pageheap->New(pages(size));
  3591. if (span != NULL) {
  3592. ret = SpanToMallocResult(span);
  3593. }
  3594. } else {
  3595. // The common case, and also the simplest. This just pops the
  3596. // size-appropriate freelist, afer replenishing it if it's empty.
  3597. ret = CheckedMallocResult(heap->Allocate(size));
  3598. }
  3599. if (!ret) {
  3600. #ifdef WTF_CHANGES
  3601. if (crashOnFailure) // This branch should be optimized out by the compiler.
  3602. CRASH();
  3603. #else
  3604. errno = ENOMEM;
  3605. #endif
  3606. }
  3607. return ret;
  3608. }
  3609. static ALWAYS_INLINE void do_free(void* ptr) {
  3610. if (ptr == NULL) return;
  3611. ASSERT(pageheap != NULL); // Should not call free() before malloc()
  3612. const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift;
  3613. Span* span = pageheap->GetDescriptor(p);
  3614. RELEASE_ASSERT(span->isValid());
  3615. size_t cl = span->sizeclass;
  3616. if (cl) {
  3617. size_t byteSizeForClass = ByteSizeForClass(cl);
  3618. RELEASE_ASSERT(!((reinterpret_cast<char*>(ptr) - reinterpret_cast<char*>(span->start << kPageShift)) % byteSizeForClass));
  3619. pageheap->CacheSizeClass(p, cl);
  3620. #ifndef NO_TCMALLOC_SAMPLES
  3621. ASSERT(!pageheap->GetDescriptor(p)->sample);
  3622. #endif
  3623. TCMalloc_ThreadCache* heap = TCMalloc_ThreadCache::GetCacheIfPresent();
  3624. if (heap != NULL) {
  3625. heap->Deallocate(HardenedSLL::create(ptr), cl);
  3626. } else {
  3627. // Delete directly into central cache
  3628. POISON_DEALLOCATION(ptr, byteSizeForClass);
  3629. SLL_SetNext(HardenedSLL::create(ptr), HardenedSLL::null(), central_cache[cl].entropy());
  3630. central_cache[cl].InsertRange(HardenedSLL::create(ptr), HardenedSLL::create(ptr), 1);
  3631. }
  3632. } else {
  3633. SpinLockHolder h(&pageheap_lock);
  3634. ASSERT(reinterpret_cast<uintptr_t>(ptr) % kPageSize == 0);
  3635. ASSERT(span != NULL && span->start == p);
  3636. #ifndef NO_TCMALLOC_SAMPLES
  3637. if (span->sample) {
  3638. DLL_Remove(span);
  3639. stacktrace_allocator.Delete(reinterpret_cast<StackTrace*>(span->objects));
  3640. span->objects = NULL;
  3641. }
  3642. #endif
  3643. RELEASE_ASSERT(reinterpret_cast<void*>(span->start << kPageShift) == ptr);
  3644. POISON_DEALLOCATION(ptr, span->length << kPageShift);
  3645. pageheap->Delete(span);
  3646. }
  3647. }
  3648. #ifndef WTF_CHANGES
  3649. // For use by exported routines below that want specific alignments
  3650. //
  3651. // Note: this code can be slow, and can significantly fragment memory.
  3652. // The expectation is that memalign/posix_memalign/valloc/pvalloc will
  3653. // not be invoked very often. This requirement simplifies our
  3654. // implementation and allows us to tune for expected allocation
  3655. // patterns.
  3656. static void* do_memalign(size_t align, size_t size) {
  3657. ASSERT((align & (align - 1)) == 0);
  3658. ASSERT(align > 0);
  3659. if (pageheap == NULL) TCMalloc_ThreadCache::InitModule();
  3660. // Allocate at least one byte to avoid boundary conditions below
  3661. if (size == 0) size = 1;
  3662. if (size <= kMaxSize && align < kPageSize) {
  3663. // Search through acceptable size classes looking for one with
  3664. // enough alignment. This depends on the fact that
  3665. // InitSizeClasses() currently produces several size classes that
  3666. // are aligned at powers of two. We will waste time and space if
  3667. // we miss in the size class array, but that is deemed acceptable
  3668. // since memalign() should be used rarely.
  3669. size_t cl = SizeClass(size);
  3670. while (cl < kNumClasses && ((class_to_size[cl] & (align - 1)) != 0)) {
  3671. cl++;
  3672. }
  3673. if (cl < kNumClasses) {
  3674. TCMalloc_ThreadCache* heap = TCMalloc_ThreadCache::GetCache();
  3675. return CheckedMallocResult(heap->Allocate(class_to_size[cl]));
  3676. }
  3677. }
  3678. // We will allocate directly from the page heap
  3679. SpinLockHolder h(&pageheap_lock);
  3680. if (align <= kPageSize) {
  3681. // Any page-level allocation will be fine
  3682. // TODO: We could put the rest of this page in the appropriate
  3683. // TODO: cache but it does not seem worth it.
  3684. Span* span = pageheap->New(pages(size));
  3685. return span == NULL ? NULL : SpanToMallocResult(span);
  3686. }
  3687. // Allocate extra pages and carve off an aligned portion
  3688. const Length alloc = pages(size + align);
  3689. Span* span = pageheap->New(alloc);
  3690. if (span == NULL) return NULL;
  3691. // Skip starting portion so that we end up aligned
  3692. Length skip = 0;
  3693. while ((((span->start+skip) << kPageShift) & (align - 1)) != 0) {
  3694. skip++;
  3695. }
  3696. ASSERT(skip < alloc);
  3697. if (skip > 0) {
  3698. Span* rest = pageheap->Split(span, skip);
  3699. pageheap->Delete(span);
  3700. span = rest;
  3701. }
  3702. // Skip trailing portion that we do not need to return
  3703. const Length needed = pages(size);
  3704. ASSERT(span->length >= needed);
  3705. if (span->length > needed) {
  3706. Span* trailer = pageheap->Split(span, needed);
  3707. pageheap->Delete(trailer);
  3708. }
  3709. return SpanToMallocResult(span);
  3710. }
  3711. #endif
  3712. // Helpers for use by exported routines below:
  3713. #ifndef WTF_CHANGES
  3714. static inline void do_malloc_stats() {
  3715. PrintStats(1);
  3716. }
  3717. static inline int do_mallopt(int, int) {
  3718. return 1; // Indicates error
  3719. }
  3720. #endif
  3721. #ifdef HAVE_STRUCT_MALLINFO // mallinfo isn't defined on freebsd, for instance
  3722. static inline struct mallinfo do_mallinfo() {
  3723. TCMallocStats stats;
  3724. ExtractStats(&stats, NULL);
  3725. // Just some of the fields are filled in.
  3726. struct mallinfo info;
  3727. memset(&info, 0, sizeof(info));
  3728. // Unfortunately, the struct contains "int" field, so some of the
  3729. // size values will be truncated.
  3730. info.arena = static_cast<int>(stats.system_bytes);
  3731. info.fsmblks = static_cast<int>(stats.thread_bytes
  3732. + stats.central_bytes
  3733. + stats.transfer_bytes);
  3734. info.fordblks = static_cast<int>(stats.pageheap_bytes);
  3735. info.uordblks = static_cast<int>(stats.system_bytes
  3736. - stats.thread_bytes
  3737. - stats.central_bytes
  3738. - stats.transfer_bytes
  3739. - stats.pageheap_bytes);
  3740. return info;
  3741. }
  3742. #endif
  3743. //-------------------------------------------------------------------
  3744. // Exported routines
  3745. //-------------------------------------------------------------------
  3746. // CAVEAT: The code structure below ensures that MallocHook methods are always
  3747. // called from the stack frame of the invoked allocation function.
  3748. // heap-checker.cc depends on this to start a stack trace from
  3749. // the call to the (de)allocation function.
  3750. #ifndef WTF_CHANGES
  3751. extern "C"
  3752. #else
  3753. #define do_malloc do_malloc<crashOnFailure>
  3754. template <bool crashOnFailure>
  3755. ALWAYS_INLINE void* malloc(size_t);
  3756. void* fastMalloc(size_t size)
  3757. {
  3758. return malloc<true>(size);
  3759. }
  3760. TryMallocReturnValue tryFastMalloc(size_t size)
  3761. {
  3762. return malloc<false>(size);
  3763. }
  3764. template <bool crashOnFailure>
  3765. ALWAYS_INLINE
  3766. #endif
  3767. void* malloc(size_t size) {
  3768. #if ENABLE(WTF_MALLOC_VALIDATION)
  3769. if (std::numeric_limits<size_t>::max() - Internal::ValidationBufferSize <= size) // If overflow would occur...
  3770. return 0;
  3771. void* result = do_malloc(size + Internal::ValidationBufferSize);
  3772. if (!result)
  3773. return 0;
  3774. Internal::ValidationHeader* header = static_cast<Internal::ValidationHeader*>(result);
  3775. header->m_size = size;
  3776. header->m_type = Internal::AllocTypeMalloc;
  3777. header->m_prefix = static_cast<unsigned>(Internal::ValidationPrefix);
  3778. result = header + 1;
  3779. *Internal::fastMallocValidationSuffix(result) = Internal::ValidationSuffix;
  3780. fastMallocValidate(result);
  3781. #else
  3782. void* result = do_malloc(size);
  3783. #endif
  3784. MallocHook::InvokeNewHook(result, size);
  3785. return result;
  3786. }
  3787. #ifndef WTF_CHANGES
  3788. extern "C"
  3789. #endif
  3790. void free(void* ptr) {
  3791. MallocHook::InvokeDeleteHook(ptr);
  3792. #if ENABLE(WTF_MALLOC_VALIDATION)
  3793. if (!ptr)
  3794. return;
  3795. fastMallocValidate(ptr);
  3796. Internal::ValidationHeader* header = Internal::fastMallocValidationHeader(ptr);
  3797. memset(ptr, 0xCC, header->m_size);
  3798. do_free(header);
  3799. #else
  3800. do_free(ptr);
  3801. #endif
  3802. }
  3803. #ifndef WTF_CHANGES
  3804. extern "C"
  3805. #else
  3806. template <bool crashOnFailure>
  3807. ALWAYS_INLINE void* calloc(size_t, size_t);
  3808. void* fastCalloc(size_t n, size_t elem_size)
  3809. {
  3810. void* result = calloc<true>(n, elem_size);
  3811. #if ENABLE(WTF_MALLOC_VALIDATION)
  3812. fastMallocValidate(result);
  3813. #endif
  3814. return result;
  3815. }
  3816. TryMallocReturnValue tryFastCalloc(size_t n, size_t elem_size)
  3817. {
  3818. void* result = calloc<false>(n, elem_size);
  3819. #if ENABLE(WTF_MALLOC_VALIDATION)
  3820. fastMallocValidate(result);
  3821. #endif
  3822. return result;
  3823. }
  3824. template <bool crashOnFailure>
  3825. ALWAYS_INLINE
  3826. #endif
  3827. void* calloc(size_t n, size_t elem_size) {
  3828. size_t totalBytes = n * elem_size;
  3829. // Protect against overflow
  3830. if (n > 1 && elem_size && (totalBytes / elem_size) != n)
  3831. return 0;
  3832. #if ENABLE(WTF_MALLOC_VALIDATION)
  3833. void* result = malloc<crashOnFailure>(totalBytes);
  3834. if (!result)
  3835. return 0;
  3836. memset(result, 0, totalBytes);
  3837. fastMallocValidate(result);
  3838. #else
  3839. void* result = do_malloc(totalBytes);
  3840. if (result != NULL) {
  3841. memset(result, 0, totalBytes);
  3842. }
  3843. #endif
  3844. MallocHook::InvokeNewHook(result, totalBytes);
  3845. return result;
  3846. }
  3847. // Since cfree isn't used anywhere, we don't compile it in.
  3848. #ifndef WTF_CHANGES
  3849. #ifndef WTF_CHANGES
  3850. extern "C"
  3851. #endif
  3852. void cfree(void* ptr) {
  3853. #ifndef WTF_CHANGES
  3854. MallocHook::InvokeDeleteHook(ptr);
  3855. #endif
  3856. do_free(ptr);
  3857. }
  3858. #endif
  3859. #ifndef WTF_CHANGES
  3860. extern "C"
  3861. #else
  3862. template <bool crashOnFailure>
  3863. ALWAYS_INLINE void* realloc(void*, size_t);
  3864. void* fastRealloc(void* old_ptr, size_t new_size)
  3865. {
  3866. #if ENABLE(WTF_MALLOC_VALIDATION)
  3867. fastMallocValidate(old_ptr);
  3868. #endif
  3869. void* result = realloc<true>(old_ptr, new_size);
  3870. #if ENABLE(WTF_MALLOC_VALIDATION)
  3871. fastMallocValidate(result);
  3872. #endif
  3873. return result;
  3874. }
  3875. TryMallocReturnValue tryFastRealloc(void* old_ptr, size_t new_size)
  3876. {
  3877. #if ENABLE(WTF_MALLOC_VALIDATION)
  3878. fastMallocValidate(old_ptr);
  3879. #endif
  3880. void* result = realloc<false>(old_ptr, new_size);
  3881. #if ENABLE(WTF_MALLOC_VALIDATION)
  3882. fastMallocValidate(result);
  3883. #endif
  3884. return result;
  3885. }
  3886. template <bool crashOnFailure>
  3887. ALWAYS_INLINE
  3888. #endif
  3889. void* realloc(void* old_ptr, size_t new_size) {
  3890. if (old_ptr == NULL) {
  3891. #if ENABLE(WTF_MALLOC_VALIDATION)
  3892. void* result = malloc<crashOnFailure>(new_size);
  3893. #else
  3894. void* result = do_malloc(new_size);
  3895. MallocHook::InvokeNewHook(result, new_size);
  3896. #endif
  3897. return result;
  3898. }
  3899. if (new_size == 0) {
  3900. MallocHook::InvokeDeleteHook(old_ptr);
  3901. free(old_ptr);
  3902. return NULL;
  3903. }
  3904. #if ENABLE(WTF_MALLOC_VALIDATION)
  3905. if (std::numeric_limits<size_t>::max() - Internal::ValidationBufferSize <= new_size) // If overflow would occur...
  3906. return 0;
  3907. Internal::ValidationHeader* header = Internal::fastMallocValidationHeader(old_ptr);
  3908. fastMallocValidate(old_ptr);
  3909. old_ptr = header;
  3910. header->m_size = new_size;
  3911. new_size += Internal::ValidationBufferSize;
  3912. #endif
  3913. // Get the size of the old entry
  3914. const PageID p = reinterpret_cast<uintptr_t>(old_ptr) >> kPageShift;
  3915. size_t cl = pageheap->GetSizeClassIfCached(p);
  3916. Span *span = NULL;
  3917. size_t old_size;
  3918. if (cl == 0) {
  3919. span = pageheap->GetDescriptor(p);
  3920. cl = span->sizeclass;
  3921. pageheap->CacheSizeClass(p, cl);
  3922. }
  3923. if (cl != 0) {
  3924. old_size = ByteSizeForClass(cl);
  3925. } else {
  3926. ASSERT(span != NULL);
  3927. old_size = span->length << kPageShift;
  3928. }
  3929. // Reallocate if the new size is larger than the old size,
  3930. // or if the new size is significantly smaller than the old size.
  3931. if ((new_size > old_size) || (AllocationSize(new_size) < old_size)) {
  3932. // Need to reallocate
  3933. void* new_ptr = do_malloc(new_size);
  3934. if (new_ptr == NULL) {
  3935. return NULL;
  3936. }
  3937. MallocHook::InvokeNewHook(new_ptr, new_size);
  3938. memcpy(new_ptr, old_ptr, ((old_size < new_size) ? old_size : new_size));
  3939. MallocHook::InvokeDeleteHook(old_ptr);
  3940. // We could use a variant of do_free() that leverages the fact
  3941. // that we already know the sizeclass of old_ptr. The benefit
  3942. // would be small, so don't bother.
  3943. do_free(old_ptr);
  3944. #if ENABLE(WTF_MALLOC_VALIDATION)
  3945. new_ptr = static_cast<Internal::ValidationHeader*>(new_ptr) + 1;
  3946. *Internal::fastMallocValidationSuffix(new_ptr) = Internal::ValidationSuffix;
  3947. #endif
  3948. return new_ptr;
  3949. } else {
  3950. #if ENABLE(WTF_MALLOC_VALIDATION)
  3951. old_ptr = static_cast<Internal::ValidationHeader*>(old_ptr) + 1; // Set old_ptr back to the user pointer.
  3952. *Internal::fastMallocValidationSuffix(old_ptr) = Internal::ValidationSuffix;
  3953. #endif
  3954. return old_ptr;
  3955. }
  3956. }
  3957. #ifdef WTF_CHANGES
  3958. #undef do_malloc
  3959. #else
  3960. static SpinLock set_new_handler_lock = SPINLOCK_INITIALIZER;
  3961. static inline void* cpp_alloc(size_t size, bool nothrow) {
  3962. for (;;) {
  3963. void* p = do_malloc(size);
  3964. #ifdef PREANSINEW
  3965. return p;
  3966. #else
  3967. if (p == NULL) { // allocation failed
  3968. // Get the current new handler. NB: this function is not
  3969. // thread-safe. We make a feeble stab at making it so here, but
  3970. // this lock only protects against tcmalloc interfering with
  3971. // itself, not with other libraries calling set_new_handler.
  3972. std::new_handler nh;
  3973. {
  3974. SpinLockHolder h(&set_new_handler_lock);
  3975. nh = std::set_new_handler(0);
  3976. (void) std::set_new_handler(nh);
  3977. }
  3978. // If no new_handler is established, the allocation failed.
  3979. if (!nh) {
  3980. if (nothrow) return 0;
  3981. throw std::bad_alloc();
  3982. }
  3983. // Otherwise, try the new_handler. If it returns, retry the
  3984. // allocation. If it throws std::bad_alloc, fail the allocation.
  3985. // if it throws something else, don't interfere.
  3986. try {
  3987. (*nh)();
  3988. } catch (const std::bad_alloc&) {
  3989. if (!nothrow) throw;
  3990. return p;
  3991. }
  3992. } else { // allocation success
  3993. return p;
  3994. }
  3995. #endif
  3996. }
  3997. }
  3998. #if ENABLE(GLOBAL_FASTMALLOC_NEW)
  3999. void* operator new(size_t size) {
  4000. void* p = cpp_alloc(size, false);
  4001. // We keep this next instruction out of cpp_alloc for a reason: when
  4002. // it's in, and new just calls cpp_alloc, the optimizer may fold the
  4003. // new call into cpp_alloc, which messes up our whole section-based
  4004. // stacktracing (see ATTRIBUTE_SECTION, above). This ensures cpp_alloc
  4005. // isn't the last thing this fn calls, and prevents the folding.
  4006. MallocHook::InvokeNewHook(p, size);
  4007. return p;
  4008. }
  4009. void* operator new(size_t size, const std::nothrow_t&) __THROW {
  4010. void* p = cpp_alloc(size, true);
  4011. MallocHook::InvokeNewHook(p, size);
  4012. return p;
  4013. }
  4014. void operator delete(void* p) __THROW {
  4015. MallocHook::InvokeDeleteHook(p);
  4016. do_free(p);
  4017. }
  4018. void operator delete(void* p, const std::nothrow_t&) __THROW {
  4019. MallocHook::InvokeDeleteHook(p);
  4020. do_free(p);
  4021. }
  4022. void* operator new[](size_t size) {
  4023. void* p = cpp_alloc(size, false);
  4024. // We keep this next instruction out of cpp_alloc for a reason: when
  4025. // it's in, and new just calls cpp_alloc, the optimizer may fold the
  4026. // new call into cpp_alloc, which messes up our whole section-based
  4027. // stacktracing (see ATTRIBUTE_SECTION, above). This ensures cpp_alloc
  4028. // isn't the last thing this fn calls, and prevents the folding.
  4029. MallocHook::InvokeNewHook(p, size);
  4030. return p;
  4031. }
  4032. void* operator new[](size_t size, const std::nothrow_t&) __THROW {
  4033. void* p = cpp_alloc(size, true);
  4034. MallocHook::InvokeNewHook(p, size);
  4035. return p;
  4036. }
  4037. void operator delete[](void* p) __THROW {
  4038. MallocHook::InvokeDeleteHook(p);
  4039. do_free(p);
  4040. }
  4041. void operator delete[](void* p, const std::nothrow_t&) __THROW {
  4042. MallocHook::InvokeDeleteHook(p);
  4043. do_free(p);
  4044. }
  4045. #endif
  4046. extern "C" void* memalign(size_t align, size_t size) __THROW {
  4047. void* result = do_memalign(align, size);
  4048. MallocHook::InvokeNewHook(result, size);
  4049. return result;
  4050. }
  4051. extern "C" int posix_memalign(void** result_ptr, size_t align, size_t size)
  4052. __THROW {
  4053. if (((align % sizeof(void*)) != 0) ||
  4054. ((align & (align - 1)) != 0) ||
  4055. (align == 0)) {
  4056. return EINVAL;
  4057. }
  4058. void* result = do_memalign(align, size);
  4059. MallocHook::InvokeNewHook(result, size);
  4060. if (result == NULL) {
  4061. return ENOMEM;
  4062. } else {
  4063. *result_ptr = result;
  4064. return 0;
  4065. }
  4066. }
  4067. static size_t pagesize = 0;
  4068. extern "C" void* valloc(size_t size) __THROW {
  4069. // Allocate page-aligned object of length >= size bytes
  4070. if (pagesize == 0) pagesize = getpagesize();
  4071. void* result = do_memalign(pagesize, size);
  4072. MallocHook::InvokeNewHook(result, size);
  4073. return result;
  4074. }
  4075. extern "C" void* pvalloc(size_t size) __THROW {
  4076. // Round up size to a multiple of pagesize
  4077. if (pagesize == 0) pagesize = getpagesize();
  4078. size = (size + pagesize - 1) & ~(pagesize - 1);
  4079. void* result = do_memalign(pagesize, size);
  4080. MallocHook::InvokeNewHook(result, size);
  4081. return result;
  4082. }
  4083. extern "C" void malloc_stats(void) {
  4084. do_malloc_stats();
  4085. }
  4086. extern "C" int mallopt(int cmd, int value) {
  4087. return do_mallopt(cmd, value);
  4088. }
  4089. #ifdef HAVE_STRUCT_MALLINFO
  4090. extern "C" struct mallinfo mallinfo(void) {
  4091. return do_mallinfo();
  4092. }
  4093. #endif
  4094. //-------------------------------------------------------------------
  4095. // Some library routines on RedHat 9 allocate memory using malloc()
  4096. // and free it using __libc_free() (or vice-versa). Since we provide
  4097. // our own implementations of malloc/free, we need to make sure that
  4098. // the __libc_XXX variants (defined as part of glibc) also point to
  4099. // the same implementations.
  4100. //-------------------------------------------------------------------
  4101. #if defined(__GLIBC__)
  4102. extern "C" {
  4103. #if COMPILER(GCC) && !defined(__MACH__) && defined(HAVE___ATTRIBUTE__)
  4104. // Potentially faster variants that use the gcc alias extension.
  4105. // Mach-O (Darwin) does not support weak aliases, hence the __MACH__ check.
  4106. # define ALIAS(x) __attribute__ ((weak, alias (x)))
  4107. void* __libc_malloc(size_t size) ALIAS("malloc");
  4108. void __libc_free(void* ptr) ALIAS("free");
  4109. void* __libc_realloc(void* ptr, size_t size) ALIAS("realloc");
  4110. void* __libc_calloc(size_t n, size_t size) ALIAS("calloc");
  4111. void __libc_cfree(void* ptr) ALIAS("cfree");
  4112. void* __libc_memalign(size_t align, size_t s) ALIAS("memalign");
  4113. void* __libc_valloc(size_t size) ALIAS("valloc");
  4114. void* __libc_pvalloc(size_t size) ALIAS("pvalloc");
  4115. int __posix_memalign(void** r, size_t a, size_t s) ALIAS("posix_memalign");
  4116. # undef ALIAS
  4117. # else /* not __GNUC__ */
  4118. // Portable wrappers
  4119. void* __libc_malloc(size_t size) { return malloc(size); }
  4120. void __libc_free(void* ptr) { free(ptr); }
  4121. void* __libc_realloc(void* ptr, size_t size) { return realloc(ptr, size); }
  4122. void* __libc_calloc(size_t n, size_t size) { return calloc(n, size); }
  4123. void __libc_cfree(void* ptr) { cfree(ptr); }
  4124. void* __libc_memalign(size_t align, size_t s) { return memalign(align, s); }
  4125. void* __libc_valloc(size_t size) { return valloc(size); }
  4126. void* __libc_pvalloc(size_t size) { return pvalloc(size); }
  4127. int __posix_memalign(void** r, size_t a, size_t s) {
  4128. return posix_memalign(r, a, s);
  4129. }
  4130. # endif /* __GNUC__ */
  4131. }
  4132. #endif /* __GLIBC__ */
  4133. // Override __libc_memalign in libc on linux boxes specially.
  4134. // They have a bug in libc that causes them to (very rarely) allocate
  4135. // with __libc_memalign() yet deallocate with free() and the
  4136. // definitions above don't catch it.
  4137. // This function is an exception to the rule of calling MallocHook method
  4138. // from the stack frame of the allocation function;
  4139. // heap-checker handles this special case explicitly.
  4140. static void *MemalignOverride(size_t align, size_t size, const void *caller)
  4141. __THROW {
  4142. void* result = do_memalign(align, size);
  4143. MallocHook::InvokeNewHook(result, size);
  4144. return result;
  4145. }
  4146. void *(*__memalign_hook)(size_t, size_t, const void *) = MemalignOverride;
  4147. #endif
  4148. #ifdef WTF_CHANGES
  4149. void releaseFastMallocFreeMemory()
  4150. {
  4151. // Flush free pages in the current thread cache back to the page heap.
  4152. if (TCMalloc_ThreadCache* threadCache = TCMalloc_ThreadCache::GetCacheIfPresent())
  4153. threadCache->Cleanup();
  4154. SpinLockHolder h(&pageheap_lock);
  4155. pageheap->ReleaseFreePages();
  4156. }
  4157. FastMallocStatistics fastMallocStatistics()
  4158. {
  4159. FastMallocStatistics statistics;
  4160. SpinLockHolder lockHolder(&pageheap_lock);
  4161. statistics.reservedVMBytes = static_cast<size_t>(pageheap->SystemBytes());
  4162. statistics.committedVMBytes = statistics.reservedVMBytes - pageheap->ReturnedBytes();
  4163. statistics.freeListBytes = 0;
  4164. for (unsigned cl = 0; cl < kNumClasses; ++cl) {
  4165. const int length = central_cache[cl].length();
  4166. const int tc_length = central_cache[cl].tc_length();
  4167. statistics.freeListBytes += ByteSizeForClass(cl) * (length + tc_length);
  4168. }
  4169. for (TCMalloc_ThreadCache* threadCache = thread_heaps; threadCache ; threadCache = threadCache->next_)
  4170. statistics.freeListBytes += threadCache->Size();
  4171. return statistics;
  4172. }
  4173. size_t fastMallocSize(const void* ptr)
  4174. {
  4175. #if ENABLE(WTF_MALLOC_VALIDATION)
  4176. return Internal::fastMallocValidationHeader(const_cast<void*>(ptr))->m_size;
  4177. #else
  4178. const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift;
  4179. Span* span = pageheap->GetDescriptorEnsureSafe(p);
  4180. if (!span || span->free)
  4181. return 0;
  4182. for (HardenedSLL free = span->objects; free; free = SLL_Next(free, HARDENING_ENTROPY)) {
  4183. if (ptr == free.value())
  4184. return 0;
  4185. }
  4186. if (size_t cl = span->sizeclass)
  4187. return ByteSizeForClass(cl);
  4188. return span->length << kPageShift;
  4189. #endif
  4190. }
  4191. #if OS(DARWIN)
  4192. class RemoteMemoryReader {
  4193. task_t m_task;
  4194. memory_reader_t* m_reader;
  4195. public:
  4196. RemoteMemoryReader(task_t task, memory_reader_t* reader)
  4197. : m_task(task)
  4198. , m_reader(reader)
  4199. { }
  4200. void* operator()(vm_address_t address, size_t size) const
  4201. {
  4202. void* output;
  4203. kern_return_t err = (*m_reader)(m_task, address, size, static_cast<void**>(&output));
  4204. if (err)
  4205. output = 0;
  4206. return output;
  4207. }
  4208. template <typename T>
  4209. T* operator()(T* address, size_t size = sizeof(T)) const
  4210. {
  4211. return static_cast<T*>((*this)(reinterpret_cast<vm_address_t>(address), size));
  4212. }
  4213. template <typename T>
  4214. T* nextEntryInHardenedLinkedList(T** remoteAddress, uintptr_t entropy) const
  4215. {
  4216. T** localAddress = (*this)(remoteAddress);
  4217. if (!localAddress)
  4218. return 0;
  4219. T* hardenedNext = *localAddress;
  4220. if (!hardenedNext || hardenedNext == (void*)entropy)
  4221. return 0;
  4222. return XOR_MASK_PTR_WITH_KEY(hardenedNext, remoteAddress, entropy);
  4223. }
  4224. };
  4225. template <typename T>
  4226. template <typename Recorder>
  4227. void PageHeapAllocator<T>::recordAdministrativeRegions(Recorder& recorder, const RemoteMemoryReader& reader)
  4228. {
  4229. for (HardenedSLL adminAllocation = allocated_regions_; adminAllocation; adminAllocation.setValue(reader.nextEntryInHardenedLinkedList(reinterpret_cast<void**>(adminAllocation.value()), entropy_)))
  4230. recorder.recordRegion(reinterpret_cast<vm_address_t>(adminAllocation.value()), kAllocIncrement);
  4231. }
  4232. class FreeObjectFinder {
  4233. const RemoteMemoryReader& m_reader;
  4234. HashSet<void*> m_freeObjects;
  4235. public:
  4236. FreeObjectFinder(const RemoteMemoryReader& reader) : m_reader(reader) { }
  4237. void visit(void* ptr) { m_freeObjects.add(ptr); }
  4238. bool isFreeObject(void* ptr) const { return m_freeObjects.contains(ptr); }
  4239. bool isFreeObject(vm_address_t ptr) const { return isFreeObject(reinterpret_cast<void*>(ptr)); }
  4240. size_t freeObjectCount() const { return m_freeObjects.size(); }
  4241. void findFreeObjects(TCMalloc_ThreadCache* threadCache)
  4242. {
  4243. for (; threadCache; threadCache = (threadCache->next_ ? m_reader(threadCache->next_) : 0))
  4244. threadCache->enumerateFreeObjects(*this, m_reader);
  4245. }
  4246. void findFreeObjects(TCMalloc_Central_FreeListPadded* centralFreeList, size_t numSizes, TCMalloc_Central_FreeListPadded* remoteCentralFreeList)
  4247. {
  4248. for (unsigned i = 0; i < numSizes; i++)
  4249. centralFreeList[i].enumerateFreeObjects(*this, m_reader, remoteCentralFreeList + i);
  4250. }
  4251. };
  4252. class PageMapFreeObjectFinder {
  4253. const RemoteMemoryReader& m_reader;
  4254. FreeObjectFinder& m_freeObjectFinder;
  4255. uintptr_t m_entropy;
  4256. public:
  4257. PageMapFreeObjectFinder(const RemoteMemoryReader& reader, FreeObjectFinder& freeObjectFinder, uintptr_t entropy)
  4258. : m_reader(reader)
  4259. , m_freeObjectFinder(freeObjectFinder)
  4260. , m_entropy(entropy)
  4261. {
  4262. #if ENABLE(TCMALLOC_HARDENING)
  4263. ASSERT(m_entropy);
  4264. #endif
  4265. }
  4266. int visit(void* ptr) const
  4267. {
  4268. if (!ptr)
  4269. return 1;
  4270. Span* span = m_reader(reinterpret_cast<Span*>(ptr));
  4271. if (!span)
  4272. return 1;
  4273. if (span->free) {
  4274. void* ptr = reinterpret_cast<void*>(span->start << kPageShift);
  4275. m_freeObjectFinder.visit(ptr);
  4276. } else if (span->sizeclass) {
  4277. // Walk the free list of the small-object span, keeping track of each object seen
  4278. for (HardenedSLL nextObject = span->objects; nextObject; nextObject.setValue(m_reader.nextEntryInHardenedLinkedList(reinterpret_cast<void**>(nextObject.value()), m_entropy)))
  4279. m_freeObjectFinder.visit(nextObject.value());
  4280. }
  4281. return span->length;
  4282. }
  4283. };
  4284. class PageMapMemoryUsageRecorder {
  4285. task_t m_task;
  4286. void* m_context;
  4287. unsigned m_typeMask;
  4288. vm_range_recorder_t* m_recorder;
  4289. const RemoteMemoryReader& m_reader;
  4290. const FreeObjectFinder& m_freeObjectFinder;
  4291. HashSet<void*> m_seenPointers;
  4292. Vector<Span*> m_coalescedSpans;
  4293. public:
  4294. PageMapMemoryUsageRecorder(task_t task, void* context, unsigned typeMask, vm_range_recorder_t* recorder, const RemoteMemoryReader& reader, const FreeObjectFinder& freeObjectFinder)
  4295. : m_task(task)
  4296. , m_context(context)
  4297. , m_typeMask(typeMask)
  4298. , m_recorder(recorder)
  4299. , m_reader(reader)
  4300. , m_freeObjectFinder(freeObjectFinder)
  4301. { }
  4302. ~PageMapMemoryUsageRecorder()
  4303. {
  4304. ASSERT(!m_coalescedSpans.size());
  4305. }
  4306. void recordPendingRegions()
  4307. {
  4308. if (!(m_typeMask & (MALLOC_PTR_IN_USE_RANGE_TYPE | MALLOC_PTR_REGION_RANGE_TYPE))) {
  4309. m_coalescedSpans.clear();
  4310. return;
  4311. }
  4312. Vector<vm_range_t, 1024> allocatedPointers;
  4313. for (size_t i = 0; i < m_coalescedSpans.size(); ++i) {
  4314. Span *theSpan = m_coalescedSpans[i];
  4315. if (theSpan->free)
  4316. continue;
  4317. vm_address_t spanStartAddress = theSpan->start << kPageShift;
  4318. vm_size_t spanSizeInBytes = theSpan->length * kPageSize;
  4319. if (!theSpan->sizeclass) {
  4320. // If it's an allocated large object span, mark it as in use
  4321. if (!m_freeObjectFinder.isFreeObject(spanStartAddress))
  4322. allocatedPointers.append((vm_range_t){spanStartAddress, spanSizeInBytes});
  4323. } else {
  4324. const size_t objectSize = ByteSizeForClass(theSpan->sizeclass);
  4325. // Mark each allocated small object within the span as in use
  4326. const vm_address_t endOfSpan = spanStartAddress + spanSizeInBytes;
  4327. for (vm_address_t object = spanStartAddress; object + objectSize <= endOfSpan; object += objectSize) {
  4328. if (!m_freeObjectFinder.isFreeObject(object))
  4329. allocatedPointers.append((vm_range_t){object, objectSize});
  4330. }
  4331. }
  4332. }
  4333. (*m_recorder)(m_task, m_context, m_typeMask & (MALLOC_PTR_IN_USE_RANGE_TYPE | MALLOC_PTR_REGION_RANGE_TYPE), allocatedPointers.data(), allocatedPointers.size());
  4334. m_coalescedSpans.clear();
  4335. }
  4336. int visit(void* ptr)
  4337. {
  4338. if (!ptr)
  4339. return 1;
  4340. Span* span = m_reader(reinterpret_cast<Span*>(ptr));
  4341. if (!span || !span->start)
  4342. return 1;
  4343. if (!m_seenPointers.add(ptr).isNewEntry)
  4344. return span->length;
  4345. if (!m_coalescedSpans.size()) {
  4346. m_coalescedSpans.append(span);
  4347. return span->length;
  4348. }
  4349. Span* previousSpan = m_coalescedSpans[m_coalescedSpans.size() - 1];
  4350. vm_address_t previousSpanStartAddress = previousSpan->start << kPageShift;
  4351. vm_size_t previousSpanSizeInBytes = previousSpan->length * kPageSize;
  4352. // If the new span is adjacent to the previous span, do nothing for now.
  4353. vm_address_t spanStartAddress = span->start << kPageShift;
  4354. if (spanStartAddress == previousSpanStartAddress + previousSpanSizeInBytes) {
  4355. m_coalescedSpans.append(span);
  4356. return span->length;
  4357. }
  4358. // New span is not adjacent to previous span, so record the spans coalesced so far.
  4359. recordPendingRegions();
  4360. m_coalescedSpans.append(span);
  4361. return span->length;
  4362. }
  4363. };
  4364. class AdminRegionRecorder {
  4365. task_t m_task;
  4366. void* m_context;
  4367. unsigned m_typeMask;
  4368. vm_range_recorder_t* m_recorder;
  4369. Vector<vm_range_t, 1024> m_pendingRegions;
  4370. public:
  4371. AdminRegionRecorder(task_t task, void* context, unsigned typeMask, vm_range_recorder_t* recorder)
  4372. : m_task(task)
  4373. , m_context(context)
  4374. , m_typeMask(typeMask)
  4375. , m_recorder(recorder)
  4376. { }
  4377. void recordRegion(vm_address_t ptr, size_t size)
  4378. {
  4379. if (m_typeMask & MALLOC_ADMIN_REGION_RANGE_TYPE)
  4380. m_pendingRegions.append((vm_range_t){ ptr, size });
  4381. }
  4382. void visit(void *ptr, size_t size)
  4383. {
  4384. recordRegion(reinterpret_cast<vm_address_t>(ptr), size);
  4385. }
  4386. void recordPendingRegions()
  4387. {
  4388. if (m_pendingRegions.size()) {
  4389. (*m_recorder)(m_task, m_context, MALLOC_ADMIN_REGION_RANGE_TYPE, m_pendingRegions.data(), m_pendingRegions.size());
  4390. m_pendingRegions.clear();
  4391. }
  4392. }
  4393. ~AdminRegionRecorder()
  4394. {
  4395. ASSERT(!m_pendingRegions.size());
  4396. }
  4397. };
  4398. kern_return_t FastMallocZone::enumerate(task_t task, void* context, unsigned typeMask, vm_address_t zoneAddress, memory_reader_t reader, vm_range_recorder_t recorder)
  4399. {
  4400. RemoteMemoryReader memoryReader(task, reader);
  4401. InitSizeClasses();
  4402. FastMallocZone* mzone = memoryReader(reinterpret_cast<FastMallocZone*>(zoneAddress));
  4403. TCMalloc_PageHeap* pageHeap = memoryReader(mzone->m_pageHeap);
  4404. TCMalloc_ThreadCache** threadHeapsPointer = memoryReader(mzone->m_threadHeaps);
  4405. TCMalloc_ThreadCache* threadHeaps = memoryReader(*threadHeapsPointer);
  4406. TCMalloc_Central_FreeListPadded* centralCaches = memoryReader(mzone->m_centralCaches, sizeof(TCMalloc_Central_FreeListPadded) * kNumClasses);
  4407. FreeObjectFinder finder(memoryReader);
  4408. finder.findFreeObjects(threadHeaps);
  4409. finder.findFreeObjects(centralCaches, kNumClasses, mzone->m_centralCaches);
  4410. TCMalloc_PageHeap::PageMap* pageMap = &pageHeap->pagemap_;
  4411. PageMapFreeObjectFinder pageMapFinder(memoryReader, finder, pageHeap->entropy_);
  4412. pageMap->visitValues(pageMapFinder, memoryReader);
  4413. PageMapMemoryUsageRecorder usageRecorder(task, context, typeMask, recorder, memoryReader, finder);
  4414. pageMap->visitValues(usageRecorder, memoryReader);
  4415. usageRecorder.recordPendingRegions();
  4416. AdminRegionRecorder adminRegionRecorder(task, context, typeMask, recorder);
  4417. pageMap->visitAllocations(adminRegionRecorder, memoryReader);
  4418. PageHeapAllocator<Span>* spanAllocator = memoryReader(mzone->m_spanAllocator);
  4419. PageHeapAllocator<TCMalloc_ThreadCache>* pageHeapAllocator = memoryReader(mzone->m_pageHeapAllocator);
  4420. spanAllocator->recordAdministrativeRegions(adminRegionRecorder, memoryReader);
  4421. pageHeapAllocator->recordAdministrativeRegions(adminRegionRecorder, memoryReader);
  4422. adminRegionRecorder.recordPendingRegions();
  4423. return 0;
  4424. }
  4425. size_t FastMallocZone::size(malloc_zone_t*, const void*)
  4426. {
  4427. return 0;
  4428. }
  4429. void* FastMallocZone::zoneMalloc(malloc_zone_t*, size_t)
  4430. {
  4431. return 0;
  4432. }
  4433. void* FastMallocZone::zoneCalloc(malloc_zone_t*, size_t, size_t)
  4434. {
  4435. return 0;
  4436. }
  4437. void FastMallocZone::zoneFree(malloc_zone_t*, void* ptr)
  4438. {
  4439. // Due to <rdar://problem/5671357> zoneFree may be called by the system free even if the pointer
  4440. // is not in this zone. When this happens, the pointer being freed was not allocated by any
  4441. // zone so we need to print a useful error for the application developer.
  4442. malloc_printf("*** error for object %p: pointer being freed was not allocated\n", ptr);
  4443. }
  4444. void* FastMallocZone::zoneRealloc(malloc_zone_t*, void*, size_t)
  4445. {
  4446. return 0;
  4447. }
  4448. #undef malloc
  4449. #undef free
  4450. #undef realloc
  4451. #undef calloc
  4452. extern "C" {
  4453. malloc_introspection_t jscore_fastmalloc_introspection = { &FastMallocZone::enumerate, &FastMallocZone::goodSize, &FastMallocZone::check, &FastMallocZone::print,
  4454. &FastMallocZone::log, &FastMallocZone::forceLock, &FastMallocZone::forceUnlock, &FastMallocZone::statistics
  4455. , 0 // zone_locked will not be called on the zone unless it advertises itself as version five or higher.
  4456. #if OS(IOS) || __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  4457. , 0, 0, 0, 0 // These members will not be used unless the zone advertises itself as version seven or higher.
  4458. #endif
  4459. };
  4460. }
  4461. FastMallocZone::FastMallocZone(TCMalloc_PageHeap* pageHeap, TCMalloc_ThreadCache** threadHeaps, TCMalloc_Central_FreeListPadded* centralCaches, PageHeapAllocator<Span>* spanAllocator, PageHeapAllocator<TCMalloc_ThreadCache>* pageHeapAllocator)
  4462. : m_pageHeap(pageHeap)
  4463. , m_threadHeaps(threadHeaps)
  4464. , m_centralCaches(centralCaches)
  4465. , m_spanAllocator(spanAllocator)
  4466. , m_pageHeapAllocator(pageHeapAllocator)
  4467. {
  4468. memset(&m_zone, 0, sizeof(m_zone));
  4469. m_zone.version = 4;
  4470. m_zone.zone_name = "JavaScriptCore FastMalloc";
  4471. m_zone.size = &FastMallocZone::size;
  4472. m_zone.malloc = &FastMallocZone::zoneMalloc;
  4473. m_zone.calloc = &FastMallocZone::zoneCalloc;
  4474. m_zone.realloc = &FastMallocZone::zoneRealloc;
  4475. m_zone.free = &FastMallocZone::zoneFree;
  4476. m_zone.valloc = &FastMallocZone::zoneValloc;
  4477. m_zone.destroy = &FastMallocZone::zoneDestroy;
  4478. m_zone.introspect = &jscore_fastmalloc_introspection;
  4479. malloc_zone_register(&m_zone);
  4480. }
  4481. void FastMallocZone::init()
  4482. {
  4483. static FastMallocZone zone(pageheap, &thread_heaps, static_cast<TCMalloc_Central_FreeListPadded*>(central_cache), &span_allocator, &threadheap_allocator);
  4484. }
  4485. #endif // OS(DARWIN)
  4486. } // namespace WTF
  4487. #endif // WTF_CHANGES
  4488. #endif // FORCE_SYSTEM_MALLOC
  4489. #endif // #if JITBRIDGE_ENABLE_DEBUG_ARENA