uClibc-0.9.28-mutex-cancel.patch 236 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631
  1. diff --git a/include/printf.h b/include/printf.h
  2. index 340b6cb..2dea58f 100644
  3. --- a/include/printf.h
  4. +++ b/include/printf.h
  5. @@ -75,6 +75,7 @@ struct printf_info
  6. unsigned int is_short:1; /* h flag. */
  7. unsigned int is_long:1; /* l flag. */
  8. unsigned int is_long_double:1;/* L flag. */
  9. + unsigned int __padding:20;/* non-gnu -- total of 32 bits on 32bit arch */
  10. #elif __BYTE_ORDER == __BIG_ENDIAN
  11. diff --git a/include/pthread.h b/include/pthread.h
  12. index 8c01172..cee112b 100644
  13. --- a/include/pthread.h
  14. +++ b/include/pthread.h
  15. @@ -644,7 +644,8 @@ extern void _pthread_cleanup_pop (struct
  16. /* Install a cleanup handler as pthread_cleanup_push does, but also
  17. saves the current cancellation type and set it to deferred cancellation. */
  18. -#ifdef __USE_GNU
  19. +/* #ifdef __USE_GNU */
  20. +#if defined(__USE_GNU) || defined(_LIBC)
  21. # define pthread_cleanup_push_defer_np(routine,arg) \
  22. { struct _pthread_cleanup_buffer _buffer; \
  23. _pthread_cleanup_push_defer (&_buffer, (routine), (arg));
  24. diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c
  25. index 181c5ad..659bf5d 100644
  26. --- a/libc/inet/getnetent.c
  27. +++ b/libc/inet/getnetent.c
  28. @@ -22,18 +22,9 @@
  29. #include <netdb.h>
  30. #include <arpa/inet.h>
  31. +#include <bits/uClibc_mutex.h>
  32. -#ifdef __UCLIBC_HAS_THREADS__
  33. -#include <pthread.h>
  34. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  35. -# define LOCK __pthread_mutex_lock(&mylock)
  36. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  37. -#else
  38. -# define LOCK
  39. -# define UNLOCK
  40. -#endif
  41. -
  42. -
  43. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  44. #define MAXALIASES 35
  45. static const char NETDB[] = _PATH_NETWORKS;
  46. @@ -46,25 +37,25 @@ int _net_stayopen;
  47. void setnetent(int f)
  48. {
  49. - LOCK;
  50. + __UCLIBC_MUTEX_LOCK(mylock);
  51. if (netf == NULL)
  52. - netf = fopen(NETDB, "r" );
  53. + netf = fopen(NETDB, "r" );
  54. else
  55. - rewind(netf);
  56. + rewind(netf);
  57. _net_stayopen |= f;
  58. - UNLOCK;
  59. + __UCLIBC_MUTEX_UNLOCK(mylock);
  60. return;
  61. }
  62. void endnetent(void)
  63. {
  64. - LOCK;
  65. + __UCLIBC_MUTEX_LOCK(mylock);
  66. if (netf) {
  67. - fclose(netf);
  68. - netf = NULL;
  69. + fclose(netf);
  70. + netf = NULL;
  71. }
  72. _net_stayopen = 0;
  73. - UNLOCK;
  74. + __UCLIBC_MUTEX_UNLOCK(mylock);
  75. }
  76. static char * any(register char *cp, char *match)
  77. @@ -72,10 +63,10 @@ static char * any(register char *cp, cha
  78. register char *mp, c;
  79. while ((c = *cp)) {
  80. - for (mp = match; *mp; mp++)
  81. - if (*mp == c)
  82. - return (cp);
  83. - cp++;
  84. + for (mp = match; *mp; mp++)
  85. + if (*mp == c)
  86. + return (cp);
  87. + cp++;
  88. }
  89. return ((char *)0);
  90. }
  91. @@ -84,59 +75,62 @@ struct netent * getnetent(void)
  92. {
  93. char *p;
  94. register char *cp, **q;
  95. + struct netent *rv = NULL;
  96. - LOCK;
  97. + __UCLIBC_MUTEX_LOCK(mylock);
  98. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  99. - UNLOCK;
  100. - return (NULL);
  101. + goto DONE;
  102. }
  103. -again:
  104. + again:
  105. if (!line) {
  106. - line = malloc(BUFSIZ + 1);
  107. - if (!line)
  108. - abort();
  109. + line = malloc(BUFSIZ + 1);
  110. + if (!line)
  111. + abort();
  112. }
  113. p = fgets(line, BUFSIZ, netf);
  114. if (p == NULL) {
  115. - UNLOCK;
  116. - return (NULL);
  117. + goto DONE;
  118. }
  119. if (*p == '#')
  120. - goto again;
  121. + goto again;
  122. cp = any(p, "#\n");
  123. if (cp == NULL)
  124. - goto again;
  125. + goto again;
  126. *cp = '\0';
  127. net.n_name = p;
  128. cp = any(p, " \t");
  129. if (cp == NULL)
  130. - goto again;
  131. + goto again;
  132. *cp++ = '\0';
  133. while (*cp == ' ' || *cp == '\t')
  134. - cp++;
  135. + cp++;
  136. p = any(cp, " \t");
  137. if (p != NULL)
  138. - *p++ = '\0';
  139. + *p++ = '\0';
  140. net.n_net = inet_network(cp);
  141. net.n_addrtype = AF_INET;
  142. q = net.n_aliases = net_aliases;
  143. if (p != NULL)
  144. - cp = p;
  145. + cp = p;
  146. while (cp && *cp) {
  147. - if (*cp == ' ' || *cp == '\t') {
  148. - cp++;
  149. - continue;
  150. - }
  151. - if (q < &net_aliases[MAXALIASES - 1])
  152. - *q++ = cp;
  153. - cp = any(cp, " \t");
  154. - if (cp != NULL)
  155. - *cp++ = '\0';
  156. + if (*cp == ' ' || *cp == '\t') {
  157. + cp++;
  158. + continue;
  159. + }
  160. + if (q < &net_aliases[MAXALIASES - 1])
  161. + *q++ = cp;
  162. + cp = any(cp, " \t");
  163. + if (cp != NULL)
  164. + *cp++ = '\0';
  165. }
  166. *q = NULL;
  167. - UNLOCK;
  168. - return (&net);
  169. +
  170. + rv = &net;
  171. +
  172. + DONE:
  173. + __UCLIBC_MUTEX_UNLOCK(mylock);
  174. + return rv;
  175. }
  176. diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c
  177. index c9f35f1..3665d89 100644
  178. --- a/libc/inet/getproto.c
  179. +++ b/libc/inet/getproto.c
  180. @@ -62,17 +62,9 @@
  181. #include <string.h>
  182. #include <errno.h>
  183. -#ifdef __UCLIBC_HAS_THREADS__
  184. -#include <pthread.h>
  185. -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  186. -# define LOCK __pthread_mutex_lock(&mylock)
  187. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  188. -#else
  189. -# define LOCK
  190. -# define UNLOCK
  191. -#endif
  192. -
  193. +#include <bits/uClibc_mutex.h>
  194. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  195. #define MAXALIASES 35
  196. #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
  197. @@ -85,109 +77,114 @@ static int proto_stayopen;
  198. static void __initbuf(void)
  199. {
  200. if (!static_aliases) {
  201. - static_aliases = malloc(SBUFSIZE);
  202. - if (!static_aliases)
  203. - abort();
  204. + static_aliases = malloc(SBUFSIZE);
  205. + if (!static_aliases)
  206. + abort();
  207. }
  208. }
  209. void setprotoent(int f)
  210. {
  211. - LOCK;
  212. + __UCLIBC_MUTEX_LOCK(mylock);
  213. if (protof == NULL)
  214. - protof = fopen(_PATH_PROTOCOLS, "r" );
  215. + protof = fopen(_PATH_PROTOCOLS, "r" );
  216. else
  217. - rewind(protof);
  218. + rewind(protof);
  219. proto_stayopen |= f;
  220. - UNLOCK;
  221. + __UCLIBC_MUTEX_UNLOCK(mylock);
  222. }
  223. void endprotoent(void)
  224. {
  225. - LOCK;
  226. + __UCLIBC_MUTEX_LOCK(mylock);
  227. if (protof) {
  228. - fclose(protof);
  229. - protof = NULL;
  230. + fclose(protof);
  231. + protof = NULL;
  232. }
  233. proto_stayopen = 0;
  234. - UNLOCK;
  235. + __UCLIBC_MUTEX_UNLOCK(mylock);
  236. }
  237. int getprotoent_r(struct protoent *result_buf,
  238. - char *buf, size_t buflen,
  239. - struct protoent **result)
  240. + char *buf, size_t buflen,
  241. + struct protoent **result)
  242. {
  243. char *p;
  244. register char *cp, **q;
  245. char **proto_aliases;
  246. char *line;
  247. + int rv;
  248. *result = NULL;
  249. if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
  250. - errno=ERANGE;
  251. - return errno;
  252. + errno=ERANGE;
  253. + return errno;
  254. }
  255. - LOCK;
  256. +
  257. + __UCLIBC_MUTEX_LOCK(mylock);
  258. proto_aliases=(char **)buf;
  259. buf+=sizeof(*proto_aliases)*MAXALIASES;
  260. buflen-=sizeof(*proto_aliases)*MAXALIASES;
  261. if (buflen < BUFSIZ+1) {
  262. - UNLOCK;
  263. - errno=ERANGE;
  264. - return errno;
  265. + errno=rv=ERANGE;
  266. + goto DONE;
  267. }
  268. line=buf;
  269. buf+=BUFSIZ+1;
  270. buflen-=BUFSIZ+1;
  271. if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
  272. - UNLOCK;
  273. - return errno;
  274. + rv=errno;
  275. + goto DONE;
  276. }
  277. -again:
  278. + again:
  279. if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
  280. - UNLOCK;
  281. - return TRY_AGAIN;
  282. + rv=TRY_AGAIN;
  283. + goto DONE;
  284. }
  285. if (*p == '#')
  286. - goto again;
  287. + goto again;
  288. cp = strpbrk(p, "#\n");
  289. if (cp == NULL)
  290. - goto again;
  291. + goto again;
  292. *cp = '\0';
  293. result_buf->p_name = p;
  294. cp = strpbrk(p, " \t");
  295. if (cp == NULL)
  296. - goto again;
  297. + goto again;
  298. *cp++ = '\0';
  299. while (*cp == ' ' || *cp == '\t')
  300. - cp++;
  301. + cp++;
  302. p = strpbrk(cp, " \t");
  303. if (p != NULL)
  304. - *p++ = '\0';
  305. + *p++ = '\0';
  306. result_buf->p_proto = atoi(cp);
  307. q = result_buf->p_aliases = proto_aliases;
  308. if (p != NULL) {
  309. - cp = p;
  310. - while (cp && *cp) {
  311. - if (*cp == ' ' || *cp == '\t') {
  312. - cp++;
  313. - continue;
  314. - }
  315. - if (q < &proto_aliases[MAXALIASES - 1])
  316. - *q++ = cp;
  317. - cp = strpbrk(cp, " \t");
  318. - if (cp != NULL)
  319. - *cp++ = '\0';
  320. - }
  321. + cp = p;
  322. + while (cp && *cp) {
  323. + if (*cp == ' ' || *cp == '\t') {
  324. + cp++;
  325. + continue;
  326. + }
  327. + if (q < &proto_aliases[MAXALIASES - 1])
  328. + *q++ = cp;
  329. + cp = strpbrk(cp, " \t");
  330. + if (cp != NULL)
  331. + *cp++ = '\0';
  332. + }
  333. }
  334. *q = NULL;
  335. *result=result_buf;
  336. - UNLOCK;
  337. - return 0;
  338. +
  339. + rv = 0;
  340. +
  341. + DONE:
  342. + __UCLIBC_MUTEX_UNLOCK(mylock);
  343. + return rv;
  344. }
  345. struct protoent * getprotoent(void)
  346. @@ -201,26 +198,26 @@ struct protoent * getprotoent(void)
  347. int getprotobyname_r(const char *name,
  348. - struct protoent *result_buf,
  349. - char *buf, size_t buflen,
  350. - struct protoent **result)
  351. + struct protoent *result_buf,
  352. + char *buf, size_t buflen,
  353. + struct protoent **result)
  354. {
  355. register char **cp;
  356. int ret;
  357. - LOCK;
  358. + __UCLIBC_MUTEX_LOCK(mylock);
  359. setprotoent(proto_stayopen);
  360. while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
  361. - if (strcmp(result_buf->p_name, name) == 0)
  362. - break;
  363. - for (cp = result_buf->p_aliases; *cp != 0; cp++)
  364. - if (strcmp(*cp, name) == 0)
  365. - goto found;
  366. + if (strcmp(result_buf->p_name, name) == 0)
  367. + break;
  368. + for (cp = result_buf->p_aliases; *cp != 0; cp++)
  369. + if (strcmp(*cp, name) == 0)
  370. + goto found;
  371. }
  372. -found:
  373. + found:
  374. if (!proto_stayopen)
  375. - endprotoent();
  376. - UNLOCK;
  377. + endprotoent();
  378. + __UCLIBC_MUTEX_UNLOCK(mylock);
  379. return *result?0:ret;
  380. }
  381. @@ -236,20 +233,20 @@ struct protoent * getprotobyname(const c
  382. int getprotobynumber_r (int proto_num,
  383. - struct protoent *result_buf,
  384. - char *buf, size_t buflen,
  385. - struct protoent **result)
  386. + struct protoent *result_buf,
  387. + char *buf, size_t buflen,
  388. + struct protoent **result)
  389. {
  390. int ret;
  391. - LOCK;
  392. + __UCLIBC_MUTEX_LOCK(mylock);
  393. setprotoent(proto_stayopen);
  394. while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
  395. - if (result_buf->p_proto == proto_num)
  396. - break;
  397. + if (result_buf->p_proto == proto_num)
  398. + break;
  399. if (!proto_stayopen)
  400. - endprotoent();
  401. - UNLOCK;
  402. + endprotoent();
  403. + __UCLIBC_MUTEX_UNLOCK(mylock);
  404. return *result?0:ret;
  405. }
  406. diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c
  407. index cbe5c50..b666057 100644
  408. --- a/libc/inet/getservice.c
  409. +++ b/libc/inet/getservice.c
  410. @@ -65,20 +65,9 @@
  411. #include <arpa/inet.h>
  412. #include <errno.h>
  413. +#include <bits/uClibc_mutex.h>
  414. -
  415. -#ifdef __UCLIBC_HAS_THREADS__
  416. -#include <pthread.h>
  417. -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  418. -# define LOCK __pthread_mutex_lock(&mylock)
  419. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  420. -#else
  421. -# define LOCK
  422. -# define UNLOCK
  423. -#endif
  424. -
  425. -
  426. -
  427. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  428. #define MAXALIASES 35
  429. #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
  430. @@ -91,32 +80,32 @@ static int serv_stayopen;
  431. static void __initbuf(void)
  432. {
  433. if (!servbuf) {
  434. - servbuf = malloc(SBUFSIZE);
  435. - if (!servbuf)
  436. - abort();
  437. + servbuf = malloc(SBUFSIZE);
  438. + if (!servbuf)
  439. + abort();
  440. }
  441. }
  442. void setservent(int f)
  443. {
  444. - LOCK;
  445. + __UCLIBC_MUTEX_LOCK(mylock);
  446. if (servf == NULL)
  447. - servf = fopen(_PATH_SERVICES, "r" );
  448. + servf = fopen(_PATH_SERVICES, "r" );
  449. else
  450. - rewind(servf);
  451. + rewind(servf);
  452. serv_stayopen |= f;
  453. - UNLOCK;
  454. + __UCLIBC_MUTEX_UNLOCK(mylock);
  455. }
  456. void endservent(void)
  457. {
  458. - LOCK;
  459. + __UCLIBC_MUTEX_LOCK(mylock);
  460. if (servf) {
  461. - fclose(servf);
  462. - servf = NULL;
  463. + fclose(servf);
  464. + servf = NULL;
  465. }
  466. serv_stayopen = 0;
  467. - UNLOCK;
  468. + __UCLIBC_MUTEX_UNLOCK(mylock);
  469. }
  470. struct servent * getservent(void)
  471. @@ -149,127 +138,129 @@ struct servent * getservbyport(int port,
  472. }
  473. int getservent_r(struct servent * result_buf,
  474. - char * buf, size_t buflen,
  475. - struct servent ** result)
  476. + char * buf, size_t buflen,
  477. + struct servent ** result)
  478. {
  479. char *p;
  480. register char *cp, **q;
  481. char **serv_aliases;
  482. char *line;
  483. + int rv;
  484. *result=NULL;
  485. if (buflen < sizeof(*serv_aliases)*MAXALIASES) {
  486. - errno=ERANGE;
  487. - return errno;
  488. + errno=ERANGE;
  489. + return errno;
  490. }
  491. - LOCK;
  492. + __UCLIBC_MUTEX_LOCK(mylock);
  493. serv_aliases=(char **)buf;
  494. buf+=sizeof(*serv_aliases)*MAXALIASES;
  495. buflen-=sizeof(*serv_aliases)*MAXALIASES;
  496. if (buflen < BUFSIZ+1) {
  497. - UNLOCK;
  498. - errno=ERANGE;
  499. - return errno;
  500. + errno=rv=ERANGE;
  501. + goto DONE;
  502. }
  503. line=buf;
  504. buf+=BUFSIZ+1;
  505. buflen-=BUFSIZ+1;
  506. if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) {
  507. - UNLOCK;
  508. - errno=EIO;
  509. - return errno;
  510. + errno=rv=EIO;
  511. + goto DONE;
  512. }
  513. -again:
  514. + again:
  515. if ((p = fgets(line, BUFSIZ, servf)) == NULL) {
  516. - UNLOCK;
  517. - errno=EIO;
  518. - return errno;
  519. + errno=rv=EIO;
  520. + goto DONE;
  521. }
  522. if (*p == '#')
  523. - goto again;
  524. + goto again;
  525. cp = strpbrk(p, "#\n");
  526. if (cp == NULL)
  527. - goto again;
  528. + goto again;
  529. *cp = '\0';
  530. result_buf->s_name = p;
  531. p = strpbrk(p, " \t");
  532. if (p == NULL)
  533. - goto again;
  534. + goto again;
  535. *p++ = '\0';
  536. while (*p == ' ' || *p == '\t')
  537. - p++;
  538. + p++;
  539. cp = strpbrk(p, ",/");
  540. if (cp == NULL)
  541. - goto again;
  542. + goto again;
  543. *cp++ = '\0';
  544. result_buf->s_port = htons((u_short)atoi(p));
  545. result_buf->s_proto = cp;
  546. q = result_buf->s_aliases = serv_aliases;
  547. cp = strpbrk(cp, " \t");
  548. if (cp != NULL)
  549. - *cp++ = '\0';
  550. + *cp++ = '\0';
  551. while (cp && *cp) {
  552. - if (*cp == ' ' || *cp == '\t') {
  553. - cp++;
  554. - continue;
  555. - }
  556. - if (q < &serv_aliases[MAXALIASES - 1])
  557. - *q++ = cp;
  558. - cp = strpbrk(cp, " \t");
  559. - if (cp != NULL)
  560. - *cp++ = '\0';
  561. + if (*cp == ' ' || *cp == '\t') {
  562. + cp++;
  563. + continue;
  564. + }
  565. + if (q < &serv_aliases[MAXALIASES - 1])
  566. + *q++ = cp;
  567. + cp = strpbrk(cp, " \t");
  568. + if (cp != NULL)
  569. + *cp++ = '\0';
  570. }
  571. *q = NULL;
  572. *result=result_buf;
  573. - UNLOCK;
  574. - return 0;
  575. +
  576. + rv = 0;
  577. +
  578. + DONE:
  579. + __UCLIBC_MUTEX_UNLOCK(mylock);
  580. + return rv;
  581. }
  582. int getservbyname_r(const char *name, const char *proto,
  583. - struct servent * result_buf, char * buf, size_t buflen,
  584. - struct servent ** result)
  585. + struct servent * result_buf, char * buf, size_t buflen,
  586. + struct servent ** result)
  587. {
  588. register char **cp;
  589. int ret;
  590. - LOCK;
  591. + __UCLIBC_MUTEX_LOCK(mylock);
  592. setservent(serv_stayopen);
  593. while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
  594. - if (strcmp(name, result_buf->s_name) == 0)
  595. - goto gotname;
  596. - for (cp = result_buf->s_aliases; *cp; cp++)
  597. - if (strcmp(name, *cp) == 0)
  598. - goto gotname;
  599. - continue;
  600. -gotname:
  601. - if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  602. - break;
  603. + if (strcmp(name, result_buf->s_name) == 0)
  604. + goto gotname;
  605. + for (cp = result_buf->s_aliases; *cp; cp++)
  606. + if (strcmp(name, *cp) == 0)
  607. + goto gotname;
  608. + continue;
  609. + gotname:
  610. + if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  611. + break;
  612. }
  613. if (!serv_stayopen)
  614. - endservent();
  615. - UNLOCK;
  616. + endservent();
  617. + __UCLIBC_MUTEX_UNLOCK(mylock);
  618. return *result?0:ret;
  619. }
  620. int getservbyport_r(int port, const char *proto,
  621. - struct servent * result_buf, char * buf,
  622. - size_t buflen, struct servent ** result)
  623. + struct servent * result_buf, char * buf,
  624. + size_t buflen, struct servent ** result)
  625. {
  626. int ret;
  627. - LOCK;
  628. + __UCLIBC_MUTEX_LOCK(mylock);
  629. setservent(serv_stayopen);
  630. while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
  631. - if (result_buf->s_port != port)
  632. - continue;
  633. - if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  634. - break;
  635. + if (result_buf->s_port != port)
  636. + continue;
  637. + if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  638. + break;
  639. }
  640. if (!serv_stayopen)
  641. - endservent();
  642. - UNLOCK;
  643. + endservent();
  644. + __UCLIBC_MUTEX_UNLOCK(mylock);
  645. return *result?0:ret;
  646. }
  647. diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c
  648. index 27b60ef..0f583ab 100644
  649. --- a/libc/inet/resolv.c
  650. +++ b/libc/inet/resolv.c
  651. @@ -7,7 +7,7 @@
  652. * modify it under the terms of the GNU Library General Public
  653. * License as published by the Free Software Foundation; either
  654. * version 2 of the License, or (at your option) any later version.
  655. -*/
  656. + */
  657. /*
  658. * Portions Copyright (c) 1985, 1993
  659. @@ -153,6 +153,11 @@
  660. #include <sys/utsname.h>
  661. #include <sys/un.h>
  662. +#include <bits/uClibc_mutex.h>
  663. +
  664. +__UCLIBC_MUTEX_EXTERN(__resolv_lock);
  665. +
  666. +
  667. #define MAX_RECURSE 5
  668. #define REPLY_TIMEOUT 10
  669. #define MAX_RETRIES 3
  670. @@ -180,18 +185,6 @@ extern char * __nameserver[MAX_SERVERS];
  671. extern int __searchdomains;
  672. extern char * __searchdomain[MAX_SEARCH];
  673. -#ifdef __UCLIBC_HAS_THREADS__
  674. -#include <pthread.h>
  675. -extern pthread_mutex_t __resolv_lock;
  676. -# define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
  677. -# define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
  678. -#else
  679. -# define BIGLOCK
  680. -# define BIGUNLOCK
  681. -#endif
  682. -
  683. -
  684. -
  685. /* Structs */
  686. struct resolv_header {
  687. int id;
  688. @@ -229,49 +222,49 @@ enum etc_hosts_action {
  689. /* function prototypes */
  690. extern int __get_hosts_byname_r(const char * name, int type,
  691. - struct hostent * result_buf,
  692. - char * buf, size_t buflen,
  693. - struct hostent ** result,
  694. - int * h_errnop);
  695. + struct hostent * result_buf,
  696. + char * buf, size_t buflen,
  697. + struct hostent ** result,
  698. + int * h_errnop);
  699. extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
  700. - struct hostent * result_buf,
  701. - char * buf, size_t buflen,
  702. - struct hostent ** result,
  703. - int * h_errnop);
  704. + struct hostent * result_buf,
  705. + char * buf, size_t buflen,
  706. + struct hostent ** result,
  707. + int * h_errnop);
  708. extern void __open_etc_hosts(FILE **fp);
  709. extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
  710. - enum etc_hosts_action action,
  711. - struct hostent * result_buf,
  712. - char * buf, size_t buflen,
  713. - struct hostent ** result,
  714. - int * h_errnop);
  715. + enum etc_hosts_action action,
  716. + struct hostent * result_buf,
  717. + char * buf, size_t buflen,
  718. + struct hostent ** result,
  719. + int * h_errnop);
  720. extern int __dns_lookup(const char * name, int type, int nscount,
  721. - char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  722. + char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  723. extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  724. extern int __decode_dotted(const unsigned char * message, int offset,
  725. - char * dest, int maxlen);
  726. + char * dest, int maxlen);
  727. extern int __length_dotted(const unsigned char * message, int offset);
  728. extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  729. extern int __decode_header(unsigned char * data, struct resolv_header * h);
  730. extern int __encode_question(struct resolv_question * q,
  731. - unsigned char * dest, int maxlen);
  732. + unsigned char * dest, int maxlen);
  733. extern int __decode_question(unsigned char * message, int offset,
  734. - struct resolv_question * q);
  735. + struct resolv_question * q);
  736. extern int __encode_answer(struct resolv_answer * a,
  737. - unsigned char * dest, int maxlen);
  738. + unsigned char * dest, int maxlen);
  739. extern int __decode_answer(unsigned char * message, int offset,
  740. - struct resolv_answer * a);
  741. + struct resolv_answer * a);
  742. extern int __length_question(unsigned char * message, int offset);
  743. extern int __open_nameservers(void);
  744. extern void __close_nameservers(void);
  745. extern int __dn_expand(const u_char *, const u_char *, const u_char *,
  746. - char *, int);
  747. + char *, int);
  748. extern int __ns_name_uncompress(const u_char *, const u_char *,
  749. - const u_char *, char *, size_t);
  750. + const u_char *, char *, size_t);
  751. extern int __ns_name_ntop(const u_char *, char *, size_t);
  752. extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
  753. - u_char *, size_t);
  754. + u_char *, size_t);
  755. #ifdef L_encodeh
  756. @@ -361,7 +354,7 @@ int __encode_dotted(const char *dotted,
  757. This routine understands compressed data. */
  758. int __decode_dotted(const unsigned char *data, int offset,
  759. - char *dest, int maxlen)
  760. + char *dest, int maxlen)
  761. {
  762. int l;
  763. int measure = 1;
  764. @@ -435,7 +428,7 @@ int __length_dotted(const unsigned char
  765. #ifdef L_encodeq
  766. int __encode_question(struct resolv_question *q,
  767. - unsigned char *dest, int maxlen)
  768. + unsigned char *dest, int maxlen)
  769. {
  770. int i;
  771. @@ -460,7 +453,7 @@ int __encode_question(struct resolv_ques
  772. #ifdef L_decodeq
  773. int __decode_question(unsigned char *message, int offset,
  774. - struct resolv_question *q)
  775. + struct resolv_question *q)
  776. {
  777. char temp[256];
  778. int i;
  779. @@ -525,7 +518,7 @@ int __encode_answer(struct resolv_answer
  780. #ifdef L_decodea
  781. int __decode_answer(unsigned char *message, int offset,
  782. - struct resolv_answer *a)
  783. + struct resolv_answer *a)
  784. {
  785. char temp[256];
  786. int i;
  787. @@ -557,11 +550,11 @@ int __decode_answer(unsigned char *messa
  788. #ifdef L_encodep
  789. int __encode_packet(struct resolv_header *h,
  790. - struct resolv_question **q,
  791. - struct resolv_answer **an,
  792. - struct resolv_answer **ns,
  793. - struct resolv_answer **ar,
  794. - unsigned char *dest, int maxlen)
  795. + struct resolv_question **q,
  796. + struct resolv_answer **an,
  797. + struct resolv_answer **ns,
  798. + struct resolv_answer **ar,
  799. + unsigned char *dest, int maxlen)
  800. {
  801. int i, total = 0;
  802. int j;
  803. @@ -621,7 +614,7 @@ int __decode_packet(unsigned char *data,
  804. #ifdef L_formquery
  805. int __form_query(int id, const char *name, int type, unsigned char *packet,
  806. - int maxlen)
  807. + int maxlen)
  808. {
  809. struct resolv_header h;
  810. struct resolv_question q;
  811. @@ -649,14 +642,7 @@ int __form_query(int id, const char *nam
  812. #ifdef L_dnslookup
  813. -#ifdef __UCLIBC_HAS_THREADS__
  814. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  815. -# define LOCK __pthread_mutex_lock(&mylock)
  816. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  817. -#else
  818. -# define LOCK
  819. -# define UNLOCK
  820. -#endif
  821. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  822. /* Just for the record, having to lock __dns_lookup() just for these two globals
  823. * is pretty lame. I think these two variables can probably be de-global-ized,
  824. @@ -665,7 +651,7 @@ static pthread_mutex_t mylock = PTHREAD_
  825. static int ns=0, id=1;
  826. int __dns_lookup(const char *name, int type, int nscount, char **nsip,
  827. - unsigned char **outpacket, struct resolv_answer *a)
  828. + unsigned char **outpacket, struct resolv_answer *a)
  829. {
  830. int i, j, len, fd, pos, rc;
  831. struct timeval tv;
  832. @@ -693,10 +679,10 @@ int __dns_lookup(const char *name, int t
  833. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  834. /* Mess with globals while under lock */
  835. - LOCK;
  836. + __UCLIBC_MUTEX_LOCK(mylock);
  837. local_ns = ns % nscount;
  838. local_id = id;
  839. - UNLOCK;
  840. + __UCLIBC_MUTEX_UNLOCK(mylock);
  841. while (retries < MAX_RETRIES) {
  842. if (fd != -1)
  843. @@ -722,13 +708,13 @@ int __dns_lookup(const char *name, int t
  844. strncpy(lookup,name,MAXDNAME);
  845. if (variant >= 0) {
  846. - BIGLOCK;
  847. - if (variant < __searchdomains) {
  848. - strncat(lookup,".", MAXDNAME);
  849. - strncat(lookup,__searchdomain[variant], MAXDNAME);
  850. - }
  851. - BIGUNLOCK;
  852. - }
  853. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  854. + if (variant < __searchdomains) {
  855. + strncat(lookup,".", MAXDNAME);
  856. + strncat(lookup,__searchdomain[variant], MAXDNAME);
  857. + }
  858. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  859. + }
  860. DPRINTF("lookup name: %s\n", lookup);
  861. q.dotted = (char *)lookup;
  862. q.qtype = type;
  863. @@ -750,7 +736,7 @@ int __dns_lookup(const char *name, int t
  864. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  865. #endif
  866. if (fd < 0) {
  867. - retries++;
  868. + retries++;
  869. continue;
  870. }
  871. @@ -772,11 +758,11 @@ int __dns_lookup(const char *name, int t
  872. #endif
  873. if (rc < 0) {
  874. if (errno == ENETUNREACH) {
  875. - /* routing error, presume not transient */
  876. - goto tryall;
  877. + /* routing error, presume not transient */
  878. + goto tryall;
  879. } else
  880. - /* retry */
  881. - retries++;
  882. + /* retry */
  883. + retries++;
  884. continue;
  885. }
  886. @@ -838,55 +824,55 @@ int __dns_lookup(const char *name, int t
  887. first_answer = 1;
  888. for (j=0;j<h.ancount;j++,pos += i)
  889. - {
  890. - i = __decode_answer(packet, pos, &ma);
  891. + {
  892. + i = __decode_answer(packet, pos, &ma);
  893. - if (i<0) {
  894. - DPRINTF("failed decode %d\n", i);
  895. - goto again;
  896. - }
  897. + if (i<0) {
  898. + DPRINTF("failed decode %d\n", i);
  899. + goto again;
  900. + }
  901. - if ( first_answer )
  902. - {
  903. - ma.buf = a->buf;
  904. - ma.buflen = a->buflen;
  905. - ma.add_count = a->add_count;
  906. - memcpy(a, &ma, sizeof(ma));
  907. - if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
  908. - {
  909. - break;
  910. - }
  911. - if (a->atype != type)
  912. - {
  913. - free(a->dotted);
  914. - continue;
  915. - }
  916. - a->add_count = h.ancount - j - 1;
  917. - if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
  918. - {
  919. - break;
  920. - }
  921. - a->add_count = 0;
  922. - first_answer = 0;
  923. - }
  924. - else
  925. - {
  926. - free(ma.dotted);
  927. - if (ma.atype != type)
  928. - {
  929. - continue;
  930. - }
  931. - if (a->rdlength != ma.rdlength)
  932. - {
  933. - free(a->dotted);
  934. - DPRINTF("Answer address len(%u) differs from original(%u)\n",
  935. - ma.rdlength, a->rdlength);
  936. - goto again;
  937. + if ( first_answer )
  938. + {
  939. + ma.buf = a->buf;
  940. + ma.buflen = a->buflen;
  941. + ma.add_count = a->add_count;
  942. + memcpy(a, &ma, sizeof(ma));
  943. + if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
  944. + {
  945. + break;
  946. + }
  947. + if (a->atype != type)
  948. + {
  949. + free(a->dotted);
  950. + continue;
  951. + }
  952. + a->add_count = h.ancount - j - 1;
  953. + if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
  954. + {
  955. + break;
  956. + }
  957. + a->add_count = 0;
  958. + first_answer = 0;
  959. + }
  960. + else
  961. + {
  962. + free(ma.dotted);
  963. + if (ma.atype != type)
  964. + {
  965. + continue;
  966. + }
  967. + if (a->rdlength != ma.rdlength)
  968. + {
  969. + free(a->dotted);
  970. + DPRINTF("Answer address len(%u) differs from original(%u)\n",
  971. + ma.rdlength, a->rdlength);
  972. + goto again;
  973. + }
  974. + memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
  975. + ++a->add_count;
  976. + }
  977. }
  978. - memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
  979. - ++a->add_count;
  980. - }
  981. - }
  982. DPRINTF("Answer name = |%s|\n", a->dotted);
  983. DPRINTF("Answer type = |%d|\n", a->atype);
  984. @@ -900,48 +886,48 @@ int __dns_lookup(const char *name, int t
  985. free(lookup);
  986. /* Mess with globals while under lock */
  987. - LOCK;
  988. + __UCLIBC_MUTEX_LOCK(mylock);
  989. ns = local_ns;
  990. id = local_id;
  991. - UNLOCK;
  992. + __UCLIBC_MUTEX_UNLOCK(mylock);
  993. return (len); /* success! */
  994. - tryall:
  995. + tryall:
  996. /* if there are other nameservers, give them a go,
  997. otherwise return with error */
  998. {
  999. variant = -1;
  1000. - local_ns = (local_ns + 1) % nscount;
  1001. - if (local_ns == 0)
  1002. - retries++;
  1003. + local_ns = (local_ns + 1) % nscount;
  1004. + if (local_ns == 0)
  1005. + retries++;
  1006. - continue;
  1007. + continue;
  1008. }
  1009. - again:
  1010. + again:
  1011. /* if there are searchdomains, try them or fallback as passed */
  1012. {
  1013. int sdomains;
  1014. - BIGLOCK;
  1015. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1016. sdomains=__searchdomains;
  1017. - BIGUNLOCK;
  1018. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1019. if (variant < sdomains - 1) {
  1020. - /* next search */
  1021. - variant++;
  1022. + /* next search */
  1023. + variant++;
  1024. } else {
  1025. - /* next server, first search */
  1026. - local_ns = (local_ns + 1) % nscount;
  1027. - if (local_ns == 0)
  1028. - retries++;
  1029. + /* next server, first search */
  1030. + local_ns = (local_ns + 1) % nscount;
  1031. + if (local_ns == 0)
  1032. + retries++;
  1033. - variant = -1;
  1034. + variant = -1;
  1035. }
  1036. }
  1037. }
  1038. -fail:
  1039. + fail:
  1040. if (fd != -1)
  1041. close(fd);
  1042. if (lookup)
  1043. @@ -951,10 +937,10 @@ fail:
  1044. h_errno = NETDB_INTERNAL;
  1045. /* Mess with globals while under lock */
  1046. if (local_ns != -1) {
  1047. - LOCK;
  1048. + __UCLIBC_MUTEX_LOCK(mylock);
  1049. ns = local_ns;
  1050. id = local_id;
  1051. - UNLOCK;
  1052. + __UCLIBC_MUTEX_UNLOCK(mylock);
  1053. }
  1054. return -1;
  1055. }
  1056. @@ -966,9 +952,8 @@ int __nameservers;
  1057. char * __nameserver[MAX_SERVERS];
  1058. int __searchdomains;
  1059. char * __searchdomain[MAX_SEARCH];
  1060. -#ifdef __UCLIBC_HAS_THREADS__
  1061. -pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
  1062. -#endif
  1063. +
  1064. +__UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_MUTEX_INITIALIZER);
  1065. /*
  1066. * we currently read formats not quite the same as that on normal
  1067. @@ -982,60 +967,63 @@ int __open_nameservers()
  1068. #define RESOLV_ARGS 5
  1069. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  1070. int argc;
  1071. + int rv = 0;
  1072. - BIGLOCK;
  1073. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1074. if (__nameservers > 0) {
  1075. - BIGUNLOCK;
  1076. - return 0;
  1077. + goto DONE;
  1078. }
  1079. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  1080. - (fp = fopen("/etc/config/resolv.conf", "r")))
  1081. - {
  1082. -
  1083. - while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  1084. + (fp = fopen("/etc/config/resolv.conf", "r")))
  1085. + {
  1086. - for (p = szBuffer; *p && isspace(*p); p++)
  1087. - /* skip white space */;
  1088. - if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  1089. - continue;
  1090. - argc = 0;
  1091. - while (*p && argc < RESOLV_ARGS) {
  1092. - argv[argc++] = p;
  1093. - while (*p && !isspace(*p) && *p != '\n')
  1094. - p++;
  1095. - while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  1096. - *p++ = '\0';
  1097. - }
  1098. + while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  1099. - if (strcmp(argv[0], "nameserver") == 0) {
  1100. - for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  1101. - __nameserver[__nameservers++] = strdup(argv[i]);
  1102. - DPRINTF("adding nameserver %s\n", argv[i]);
  1103. + for (p = szBuffer; *p && isspace(*p); p++)
  1104. + /* skip white space */;
  1105. + if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  1106. + continue;
  1107. + argc = 0;
  1108. + while (*p && argc < RESOLV_ARGS) {
  1109. + argv[argc++] = p;
  1110. + while (*p && !isspace(*p) && *p != '\n')
  1111. + p++;
  1112. + while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  1113. + *p++ = '\0';
  1114. }
  1115. - }
  1116. - /* domain and search are mutually exclusive, the last one wins */
  1117. - if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  1118. - while (__searchdomains > 0) {
  1119. - free(__searchdomain[--__searchdomains]);
  1120. - __searchdomain[__searchdomains] = NULL;
  1121. + if (strcmp(argv[0], "nameserver") == 0) {
  1122. + for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  1123. + __nameserver[__nameservers++] = strdup(argv[i]);
  1124. + DPRINTF("adding nameserver %s\n", argv[i]);
  1125. + }
  1126. }
  1127. - for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  1128. - __searchdomain[__searchdomains++] = strdup(argv[i]);
  1129. - DPRINTF("adding search %s\n", argv[i]);
  1130. +
  1131. + /* domain and search are mutually exclusive, the last one wins */
  1132. + if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  1133. + while (__searchdomains > 0) {
  1134. + free(__searchdomain[--__searchdomains]);
  1135. + __searchdomain[__searchdomains] = NULL;
  1136. + }
  1137. + for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  1138. + __searchdomain[__searchdomains++] = strdup(argv[i]);
  1139. + DPRINTF("adding search %s\n", argv[i]);
  1140. + }
  1141. }
  1142. }
  1143. + fclose(fp);
  1144. + DPRINTF("nameservers = %d\n", __nameservers);
  1145. + goto DONE;
  1146. }
  1147. - fclose(fp);
  1148. - DPRINTF("nameservers = %d\n", __nameservers);
  1149. - BIGUNLOCK;
  1150. - return 0;
  1151. - }
  1152. DPRINTF("failed to open %s\n", "resolv.conf");
  1153. h_errno = NO_RECOVERY;
  1154. - BIGUNLOCK;
  1155. - return -1;
  1156. +
  1157. + rv = -1;
  1158. +
  1159. + DONE:
  1160. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1161. + return rv;
  1162. }
  1163. #endif
  1164. @@ -1044,7 +1032,7 @@ int __open_nameservers()
  1165. void __close_nameservers(void)
  1166. {
  1167. - BIGLOCK;
  1168. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1169. while (__nameservers > 0) {
  1170. free(__nameserver[--__nameservers]);
  1171. __nameserver[__nameservers] = NULL;
  1172. @@ -1053,7 +1041,7 @@ void __close_nameservers(void)
  1173. free(__searchdomain[--__searchdomains]);
  1174. __searchdomain[__searchdomains] = NULL;
  1175. }
  1176. - BIGUNLOCK;
  1177. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1178. }
  1179. #endif
  1180. @@ -1063,8 +1051,8 @@ struct hostent *gethostbyname(const char
  1181. {
  1182. static struct hostent h;
  1183. static char buf[sizeof(struct in_addr) +
  1184. - sizeof(struct in_addr *)*2 +
  1185. - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1186. + sizeof(struct in_addr *)*2 +
  1187. + sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1188. struct hostent *hp;
  1189. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  1190. @@ -1082,8 +1070,8 @@ struct hostent *gethostbyname2(const cha
  1191. #else /* __UCLIBC_HAS_IPV6__ */
  1192. static struct hostent h;
  1193. static char buf[sizeof(struct in6_addr) +
  1194. - sizeof(struct in6_addr *)*2 +
  1195. - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1196. + sizeof(struct in6_addr *)*2 +
  1197. + sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1198. struct hostent *hp;
  1199. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  1200. @@ -1119,7 +1107,7 @@ int res_init(void)
  1201. /** rp->rhook = NULL; **/
  1202. /** rp->_u._ext.nsinit = 0; **/
  1203. - BIGLOCK;
  1204. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1205. if(__searchdomains) {
  1206. int i;
  1207. for(i=0; i<__searchdomains; i++) {
  1208. @@ -1139,7 +1127,7 @@ int res_init(void)
  1209. }
  1210. }
  1211. rp->nscount = __nameservers;
  1212. - BIGUNLOCK;
  1213. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1214. return(0);
  1215. }
  1216. @@ -1175,10 +1163,10 @@ int res_query(const char *dname, int cla
  1217. memset((char *) &a, '\0', sizeof(a));
  1218. - BIGLOCK;
  1219. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1220. __nameserversXX=__nameservers;
  1221. __nameserverXX=__nameserver;
  1222. - BIGUNLOCK;
  1223. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1224. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  1225. if (i < 0) {
  1226. @@ -1207,10 +1195,10 @@ int res_query(const char *dname, int cla
  1227. * is detected. Error code, if any, is left in h_errno.
  1228. */
  1229. int res_search(name, class, type, answer, anslen)
  1230. - const char *name; /* domain name */
  1231. - int class, type; /* class and type of query */
  1232. - u_char *answer; /* buffer to put answer */
  1233. - int anslen; /* size of answer */
  1234. + const char *name; /* domain name */
  1235. + int class, type; /* class and type of query */
  1236. + u_char *answer; /* buffer to put answer */
  1237. + int anslen; /* size of answer */
  1238. {
  1239. const char *cp, * const *domain;
  1240. HEADER *hp = (HEADER *)(void *)answer;
  1241. @@ -1256,11 +1244,11 @@ int res_search(name, class, type, answer
  1242. int done = 0;
  1243. for (domain = (const char * const *)_res.dnsrch;
  1244. - *domain && !done;
  1245. - domain++) {
  1246. + *domain && !done;
  1247. + domain++) {
  1248. ret = res_querydomain(name, *domain, class, type,
  1249. - answer, anslen);
  1250. + answer, anslen);
  1251. if (ret > 0)
  1252. return (ret);
  1253. @@ -1283,22 +1271,22 @@ int res_search(name, class, type, answer
  1254. }
  1255. switch (h_errno) {
  1256. - case NO_DATA:
  1257. - got_nodata++;
  1258. - /* FALLTHROUGH */
  1259. - case HOST_NOT_FOUND:
  1260. - /* keep trying */
  1261. - break;
  1262. - case TRY_AGAIN:
  1263. - if (hp->rcode == SERVFAIL) {
  1264. - /* try next search element, if any */
  1265. - got_servfail++;
  1266. + case NO_DATA:
  1267. + got_nodata++;
  1268. + /* FALLTHROUGH */
  1269. + case HOST_NOT_FOUND:
  1270. + /* keep trying */
  1271. break;
  1272. - }
  1273. - /* FALLTHROUGH */
  1274. - default:
  1275. - /* anything else implies that we're done */
  1276. - done++;
  1277. + case TRY_AGAIN:
  1278. + if (hp->rcode == SERVFAIL) {
  1279. + /* try next search element, if any */
  1280. + got_servfail++;
  1281. + break;
  1282. + }
  1283. + /* FALLTHROUGH */
  1284. + default:
  1285. + /* anything else implies that we're done */
  1286. + done++;
  1287. }
  1288. /*
  1289. * if we got here for some reason other than DNSRCH,
  1290. @@ -1342,10 +1330,10 @@ int res_search(name, class, type, answer
  1291. * removing a trailing dot from name if domain is NULL.
  1292. */
  1293. int res_querydomain(name, domain, class, type, answer, anslen)
  1294. - const char *name, *domain;
  1295. - int class, type; /* class and type of query */
  1296. - u_char *answer; /* buffer to put answer */
  1297. - int anslen; /* size of answer */
  1298. + const char *name, *domain;
  1299. + int class, type; /* class and type of query */
  1300. + u_char *answer; /* buffer to put answer */
  1301. + int anslen; /* size of answer */
  1302. {
  1303. char nbuf[MAXDNAME];
  1304. const char *longname = nbuf;
  1305. @@ -1359,7 +1347,7 @@ int res_querydomain(name, domain, class,
  1306. #ifdef DEBUG
  1307. if (_res.options & RES_DEBUG)
  1308. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1309. - name, domain?domain:"<Nil>", class, type);
  1310. + name, domain?domain:"<Nil>", class, type);
  1311. #endif
  1312. if (domain == NULL) {
  1313. /*
  1314. @@ -1400,11 +1388,11 @@ struct hostent *gethostbyaddr (const voi
  1315. static struct hostent h;
  1316. static char buf[
  1317. #ifndef __UCLIBC_HAS_IPV6__
  1318. - sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1319. + sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1320. #else
  1321. - sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1322. + sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1323. #endif /* __UCLIBC_HAS_IPV6__ */
  1324. - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1325. + sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1326. struct hostent *hp;
  1327. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1328. @@ -1425,11 +1413,11 @@ void __open_etc_hosts(FILE **fp)
  1329. }
  1330. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1331. - enum etc_hosts_action action,
  1332. - struct hostent * result_buf,
  1333. - char * buf, size_t buflen,
  1334. - struct hostent ** result,
  1335. - int * h_errnop)
  1336. + enum etc_hosts_action action,
  1337. + struct hostent * result_buf,
  1338. + char * buf, size_t buflen,
  1339. + struct hostent ** result,
  1340. + int * h_errnop)
  1341. {
  1342. struct in_addr *in=NULL;
  1343. struct in_addr **addr_list=NULL;
  1344. @@ -1576,56 +1564,49 @@ int __read_etc_hosts_r(FILE * fp, const
  1345. #ifdef L_gethostent
  1346. -#ifdef __UCLIBC_HAS_THREADS__
  1347. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1348. -# define LOCK __pthread_mutex_lock(&mylock)
  1349. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  1350. -#else
  1351. -# define LOCK
  1352. -# define UNLOCK
  1353. -#endif
  1354. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  1355. static int __stay_open;
  1356. static FILE * __gethostent_fp;
  1357. void endhostent (void)
  1358. {
  1359. - LOCK;
  1360. + __UCLIBC_MUTEX_LOCK(mylock);
  1361. __stay_open = 0;
  1362. if (__gethostent_fp) {
  1363. - fclose(__gethostent_fp);
  1364. + fclose(__gethostent_fp);
  1365. }
  1366. - UNLOCK;
  1367. + __UCLIBC_MUTEX_UNLOCK(mylock);
  1368. }
  1369. void sethostent (int stay_open)
  1370. {
  1371. - LOCK;
  1372. + __UCLIBC_MUTEX_LOCK(mylock);
  1373. __stay_open = stay_open;
  1374. - UNLOCK;
  1375. + __UCLIBC_MUTEX_UNLOCK(mylock);
  1376. }
  1377. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  1378. - struct hostent **result, int *h_errnop)
  1379. + struct hostent **result, int *h_errnop)
  1380. {
  1381. - int ret;
  1382. + int ret = 0;
  1383. - LOCK;
  1384. + __UCLIBC_MUTEX_LOCK(mylock);
  1385. if (__gethostent_fp == NULL) {
  1386. - __open_etc_hosts(&__gethostent_fp);
  1387. - if (__gethostent_fp == NULL) {
  1388. - UNLOCK;
  1389. - *result=NULL;
  1390. - return 0;
  1391. - }
  1392. + __open_etc_hosts(&__gethostent_fp);
  1393. + if (__gethostent_fp == NULL) {
  1394. + *result=NULL;
  1395. + goto DONE;
  1396. + }
  1397. }
  1398. ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1399. - result_buf, buf, buflen, result, h_errnop);
  1400. + result_buf, buf, buflen, result, h_errnop);
  1401. if (__stay_open==0) {
  1402. - fclose(__gethostent_fp);
  1403. + fclose(__gethostent_fp);
  1404. }
  1405. - UNLOCK;
  1406. + DONE:
  1407. + __UCLIBC_MUTEX_UNLOCK(mylock);
  1408. return(ret);
  1409. }
  1410. @@ -1634,17 +1615,17 @@ struct hostent *gethostent (void)
  1411. static struct hostent h;
  1412. static char buf[
  1413. #ifndef __UCLIBC_HAS_IPV6__
  1414. - sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1415. + sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1416. #else
  1417. - sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1418. + sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1419. #endif /* __UCLIBC_HAS_IPV6__ */
  1420. - sizeof(char *)*(ALIAS_DIM) +
  1421. - 80/*namebuffer*/ + 2/* margin */];
  1422. + sizeof(char *)*(ALIAS_DIM) +
  1423. + 80/*namebuffer*/ + 2/* margin */];
  1424. struct hostent *host;
  1425. - LOCK;
  1426. + __UCLIBC_MUTEX_LOCK(mylock);
  1427. gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
  1428. - UNLOCK;
  1429. + __UCLIBC_MUTEX_UNLOCK(mylock);
  1430. return(host);
  1431. }
  1432. #endif
  1433. @@ -1652,23 +1633,23 @@ struct hostent *gethostent (void)
  1434. #ifdef L_get_hosts_byname_r
  1435. int __get_hosts_byname_r(const char * name, int type,
  1436. - struct hostent * result_buf,
  1437. - char * buf, size_t buflen,
  1438. - struct hostent ** result,
  1439. - int * h_errnop)
  1440. + struct hostent * result_buf,
  1441. + char * buf, size_t buflen,
  1442. + struct hostent ** result,
  1443. + int * h_errnop)
  1444. {
  1445. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
  1446. - result_buf, buf, buflen, result, h_errnop));
  1447. + result_buf, buf, buflen, result, h_errnop));
  1448. }
  1449. #endif
  1450. #ifdef L_get_hosts_byaddr_r
  1451. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1452. - struct hostent * result_buf,
  1453. - char * buf, size_t buflen,
  1454. - struct hostent ** result,
  1455. - int * h_errnop)
  1456. + struct hostent * result_buf,
  1457. + char * buf, size_t buflen,
  1458. + struct hostent ** result,
  1459. + int * h_errnop)
  1460. {
  1461. #ifndef __UCLIBC_HAS_IPV6__
  1462. char ipaddr[INET_ADDRSTRLEN];
  1463. @@ -1677,24 +1658,24 @@ int __get_hosts_byaddr_r(const char * ad
  1464. #endif /* __UCLIBC_HAS_IPV6__ */
  1465. switch (type) {
  1466. - case AF_INET:
  1467. - if (len != sizeof(struct in_addr))
  1468. - return 0;
  1469. - break;
  1470. + case AF_INET:
  1471. + if (len != sizeof(struct in_addr))
  1472. + return 0;
  1473. + break;
  1474. #ifdef __UCLIBC_HAS_IPV6__
  1475. - case AF_INET6:
  1476. - if (len != sizeof(struct in6_addr))
  1477. - return 0;
  1478. - break;
  1479. + case AF_INET6:
  1480. + if (len != sizeof(struct in6_addr))
  1481. + return 0;
  1482. + break;
  1483. #endif /* __UCLIBC_HAS_IPV6__ */
  1484. - default:
  1485. - return 0;
  1486. + default:
  1487. + return 0;
  1488. }
  1489. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1490. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1491. - result_buf, buf, buflen, result, h_errnop));
  1492. + result_buf, buf, buflen, result, h_errnop));
  1493. }
  1494. #endif
  1495. @@ -1705,8 +1686,8 @@ int __get_hosts_byaddr_r(const char * ad
  1496. #endif /* min */
  1497. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1498. - socklen_t hostlen, char *serv, socklen_t servlen,
  1499. - unsigned int flags)
  1500. + socklen_t hostlen, char *serv, socklen_t servlen,
  1501. + unsigned int flags)
  1502. {
  1503. int serrno = errno;
  1504. int ok = 0;
  1505. @@ -1720,167 +1701,167 @@ int getnameinfo (const struct sockaddr *
  1506. return EAI_FAMILY;
  1507. switch (sa->sa_family) {
  1508. - case AF_LOCAL:
  1509. - break;
  1510. - case AF_INET:
  1511. - if (addrlen < sizeof (struct sockaddr_in))
  1512. - return EAI_FAMILY;
  1513. - break;
  1514. + case AF_LOCAL:
  1515. + break;
  1516. + case AF_INET:
  1517. + if (addrlen < sizeof (struct sockaddr_in))
  1518. + return EAI_FAMILY;
  1519. + break;
  1520. #ifdef __UCLIBC_HAS_IPV6__
  1521. - case AF_INET6:
  1522. - if (addrlen < sizeof (struct sockaddr_in6))
  1523. - return EAI_FAMILY;
  1524. - break;
  1525. + case AF_INET6:
  1526. + if (addrlen < sizeof (struct sockaddr_in6))
  1527. + return EAI_FAMILY;
  1528. + break;
  1529. #endif /* __UCLIBC_HAS_IPV6__ */
  1530. - default:
  1531. - return EAI_FAMILY;
  1532. + default:
  1533. + return EAI_FAMILY;
  1534. }
  1535. if (host != NULL && hostlen > 0)
  1536. switch (sa->sa_family) {
  1537. - case AF_INET:
  1538. + case AF_INET:
  1539. #ifdef __UCLIBC_HAS_IPV6__
  1540. - case AF_INET6:
  1541. + case AF_INET6:
  1542. #endif /* __UCLIBC_HAS_IPV6__ */
  1543. - if (!(flags & NI_NUMERICHOST)) {
  1544. + if (!(flags & NI_NUMERICHOST)) {
  1545. #ifdef __UCLIBC_HAS_IPV6__
  1546. - if (sa->sa_family == AF_INET6)
  1547. - h = gethostbyaddr ((const void *)
  1548. - &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1549. - sizeof(struct in6_addr), AF_INET6);
  1550. - else
  1551. -#endif /* __UCLIBC_HAS_IPV6__ */
  1552. - h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1553. - sizeof(struct in_addr), AF_INET);
  1554. -
  1555. - if (h) {
  1556. - char *c;
  1557. - if ((flags & NI_NOFQDN)
  1558. - && (getdomainname (domain, sizeof(domain)) == 0)
  1559. - && (c = strstr (h->h_name, domain))
  1560. - && (c != h->h_name) && (*(--c) == '.')) {
  1561. - strncpy (host, h->h_name,
  1562. - min(hostlen, (size_t) (c - h->h_name)));
  1563. - host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1564. - ok = 1;
  1565. - } else {
  1566. - strncpy (host, h->h_name, hostlen);
  1567. - ok = 1;
  1568. + if (sa->sa_family == AF_INET6)
  1569. + h = gethostbyaddr ((const void *)
  1570. + &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1571. + sizeof(struct in6_addr), AF_INET6);
  1572. + else
  1573. +#endif /* __UCLIBC_HAS_IPV6__ */
  1574. + h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1575. + sizeof(struct in_addr), AF_INET);
  1576. +
  1577. + if (h) {
  1578. + char *c;
  1579. + if ((flags & NI_NOFQDN)
  1580. + && (getdomainname (domain, sizeof(domain)) == 0)
  1581. + && (c = strstr (h->h_name, domain))
  1582. + && (c != h->h_name) && (*(--c) == '.')) {
  1583. + strncpy (host, h->h_name,
  1584. + min(hostlen, (size_t) (c - h->h_name)));
  1585. + host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1586. + ok = 1;
  1587. + } else {
  1588. + strncpy (host, h->h_name, hostlen);
  1589. + ok = 1;
  1590. + }
  1591. }
  1592. - }
  1593. - }
  1594. + }
  1595. - if (!ok) {
  1596. - if (flags & NI_NAMEREQD) {
  1597. - errno = serrno;
  1598. - return EAI_NONAME;
  1599. - } else {
  1600. - const char *c;
  1601. + if (!ok) {
  1602. + if (flags & NI_NAMEREQD) {
  1603. + errno = serrno;
  1604. + return EAI_NONAME;
  1605. + } else {
  1606. + const char *c;
  1607. #ifdef __UCLIBC_HAS_IPV6__
  1608. - if (sa->sa_family == AF_INET6) {
  1609. - const struct sockaddr_in6 *sin6p;
  1610. + if (sa->sa_family == AF_INET6) {
  1611. + const struct sockaddr_in6 *sin6p;
  1612. - sin6p = (const struct sockaddr_in6 *) sa;
  1613. + sin6p = (const struct sockaddr_in6 *) sa;
  1614. - c = inet_ntop (AF_INET6,
  1615. - (const void *) &sin6p->sin6_addr, host, hostlen);
  1616. + c = inet_ntop (AF_INET6,
  1617. + (const void *) &sin6p->sin6_addr, host, hostlen);
  1618. #if 0
  1619. - /* Does scope id need to be supported? */
  1620. - uint32_t scopeid;
  1621. - scopeid = sin6p->sin6_scope_id;
  1622. - if (scopeid != 0) {
  1623. - /* Buffer is >= IFNAMSIZ+1. */
  1624. - char scopebuf[IFNAMSIZ + 1];
  1625. - char *scopeptr;
  1626. - int ni_numericscope = 0;
  1627. - size_t real_hostlen = __strnlen (host, hostlen);
  1628. - size_t scopelen = 0;
  1629. -
  1630. - scopebuf[0] = SCOPE_DELIMITER;
  1631. - scopebuf[1] = '\0';
  1632. - scopeptr = &scopebuf[1];
  1633. -
  1634. - if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1635. - || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1636. - if (if_indextoname (scopeid, scopeptr) == NULL)
  1637. + /* Does scope id need to be supported? */
  1638. + uint32_t scopeid;
  1639. + scopeid = sin6p->sin6_scope_id;
  1640. + if (scopeid != 0) {
  1641. + /* Buffer is >= IFNAMSIZ+1. */
  1642. + char scopebuf[IFNAMSIZ + 1];
  1643. + char *scopeptr;
  1644. + int ni_numericscope = 0;
  1645. + size_t real_hostlen = __strnlen (host, hostlen);
  1646. + size_t scopelen = 0;
  1647. +
  1648. + scopebuf[0] = SCOPE_DELIMITER;
  1649. + scopebuf[1] = '\0';
  1650. + scopeptr = &scopebuf[1];
  1651. +
  1652. + if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1653. + || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1654. + if (if_indextoname (scopeid, scopeptr) == NULL)
  1655. + ++ni_numericscope;
  1656. + else
  1657. + scopelen = strlen (scopebuf);
  1658. + } else {
  1659. ++ni_numericscope;
  1660. - else
  1661. - scopelen = strlen (scopebuf);
  1662. - } else {
  1663. - ++ni_numericscope;
  1664. - }
  1665. + }
  1666. - if (ni_numericscope)
  1667. - scopelen = 1 + snprintf (scopeptr,
  1668. - (scopebuf
  1669. - + sizeof scopebuf
  1670. - - scopeptr),
  1671. - "%u", scopeid);
  1672. -
  1673. - if (real_hostlen + scopelen + 1 > hostlen)
  1674. - return EAI_SYSTEM;
  1675. - memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1676. - }
  1677. + if (ni_numericscope)
  1678. + scopelen = 1 + snprintf (scopeptr,
  1679. + (scopebuf
  1680. + + sizeof scopebuf
  1681. + - scopeptr),
  1682. + "%u", scopeid);
  1683. +
  1684. + if (real_hostlen + scopelen + 1 > hostlen)
  1685. + return EAI_SYSTEM;
  1686. + memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1687. + }
  1688. #endif
  1689. - } else
  1690. + } else
  1691. #endif /* __UCLIBC_HAS_IPV6__ */
  1692. - c = inet_ntop (AF_INET, (const void *)
  1693. - &(((const struct sockaddr_in *) sa)->sin_addr),
  1694. - host, hostlen);
  1695. -
  1696. - if (c == NULL) {
  1697. - errno = serrno;
  1698. - return EAI_SYSTEM;
  1699. + c = inet_ntop (AF_INET, (const void *)
  1700. + &(((const struct sockaddr_in *) sa)->sin_addr),
  1701. + host, hostlen);
  1702. +
  1703. + if (c == NULL) {
  1704. + errno = serrno;
  1705. + return EAI_SYSTEM;
  1706. + }
  1707. }
  1708. + ok = 1;
  1709. }
  1710. - ok = 1;
  1711. - }
  1712. - break;
  1713. -
  1714. - case AF_LOCAL:
  1715. - if (!(flags & NI_NUMERICHOST)) {
  1716. - struct utsname utsname;
  1717. + break;
  1718. - if (!uname (&utsname)) {
  1719. - strncpy (host, utsname.nodename, hostlen);
  1720. - break;
  1721. + case AF_LOCAL:
  1722. + if (!(flags & NI_NUMERICHOST)) {
  1723. + struct utsname utsname;
  1724. +
  1725. + if (!uname (&utsname)) {
  1726. + strncpy (host, utsname.nodename, hostlen);
  1727. + break;
  1728. + };
  1729. };
  1730. - };
  1731. - if (flags & NI_NAMEREQD) {
  1732. - errno = serrno;
  1733. - return EAI_NONAME;
  1734. - }
  1735. + if (flags & NI_NAMEREQD) {
  1736. + errno = serrno;
  1737. + return EAI_NONAME;
  1738. + }
  1739. - strncpy (host, "localhost", hostlen);
  1740. - break;
  1741. + strncpy (host, "localhost", hostlen);
  1742. + break;
  1743. - default:
  1744. - return EAI_FAMILY;
  1745. - }
  1746. + default:
  1747. + return EAI_FAMILY;
  1748. + }
  1749. if (serv && (servlen > 0)) {
  1750. switch (sa->sa_family) {
  1751. - case AF_INET:
  1752. + case AF_INET:
  1753. #ifdef __UCLIBC_HAS_IPV6__
  1754. - case AF_INET6:
  1755. + case AF_INET6:
  1756. #endif /* __UCLIBC_HAS_IPV6__ */
  1757. - if (!(flags & NI_NUMERICSERV)) {
  1758. - struct servent *s;
  1759. - s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1760. - ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1761. - if (s) {
  1762. - strncpy (serv, s->s_name, servlen);
  1763. - break;
  1764. + if (!(flags & NI_NUMERICSERV)) {
  1765. + struct servent *s;
  1766. + s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1767. + ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1768. + if (s) {
  1769. + strncpy (serv, s->s_name, servlen);
  1770. + break;
  1771. + }
  1772. }
  1773. - }
  1774. - snprintf (serv, servlen, "%d",
  1775. - ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1776. - break;
  1777. + snprintf (serv, servlen, "%d",
  1778. + ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1779. + break;
  1780. - case AF_LOCAL:
  1781. - strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1782. - break;
  1783. + case AF_LOCAL:
  1784. + strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1785. + break;
  1786. }
  1787. }
  1788. if (host && (hostlen > 0))
  1789. @@ -1896,10 +1877,10 @@ int getnameinfo (const struct sockaddr *
  1790. #ifdef L_gethostbyname_r
  1791. int gethostbyname_r(const char * name,
  1792. - struct hostent * result_buf,
  1793. - char * buf, size_t buflen,
  1794. - struct hostent ** result,
  1795. - int * h_errnop)
  1796. + struct hostent * result_buf,
  1797. + char * buf, size_t buflen,
  1798. + struct hostent ** result,
  1799. + int * h_errnop)
  1800. {
  1801. struct in_addr *in;
  1802. struct in_addr **addr_list;
  1803. @@ -1921,7 +1902,7 @@ int gethostbyname_r(const char * name,
  1804. __set_errno(0); /* to check for missing /etc/hosts. */
  1805. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1806. - buf, buflen, result, h_errnop))==0)
  1807. + buf, buflen, result, h_errnop))==0)
  1808. return i;
  1809. switch (*h_errnop) {
  1810. case HOST_NOT_FOUND:
  1811. @@ -1983,60 +1964,60 @@ int gethostbyname_r(const char * name,
  1812. for (;;) {
  1813. - BIGLOCK;
  1814. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1815. __nameserversXX=__nameservers;
  1816. __nameserverXX=__nameserver;
  1817. - BIGUNLOCK;
  1818. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1819. a.buf = buf;
  1820. a.buflen = buflen;
  1821. a.add_count = 0;
  1822. i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1823. if (i < 0) {
  1824. - *h_errnop = HOST_NOT_FOUND;
  1825. - DPRINTF("__dns_lookup\n");
  1826. - return TRY_AGAIN;
  1827. + *h_errnop = HOST_NOT_FOUND;
  1828. + DPRINTF("__dns_lookup\n");
  1829. + return TRY_AGAIN;
  1830. }
  1831. if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen)
  1832. - {
  1833. - free(a.dotted);
  1834. - free(packet);
  1835. - *h_errnop = NETDB_INTERNAL;
  1836. - DPRINTF("buffer too small for all addresses\n");
  1837. - return ERANGE;
  1838. - }
  1839. + {
  1840. + free(a.dotted);
  1841. + free(packet);
  1842. + *h_errnop = NETDB_INTERNAL;
  1843. + DPRINTF("buffer too small for all addresses\n");
  1844. + return ERANGE;
  1845. + }
  1846. else if(a.add_count > 0)
  1847. - {
  1848. - memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
  1849. - addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
  1850. - addr_list[0] = in;
  1851. - for (i = a.add_count-1; i>=0; --i)
  1852. - addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
  1853. - addr_list[a.add_count + 1] = 0;
  1854. - buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
  1855. - buf = (char*)&addr_list[a.add_count + 2];
  1856. - }
  1857. + {
  1858. + memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
  1859. + addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
  1860. + addr_list[0] = in;
  1861. + for (i = a.add_count-1; i>=0; --i)
  1862. + addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
  1863. + addr_list[a.add_count + 1] = 0;
  1864. + buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
  1865. + buf = (char*)&addr_list[a.add_count + 2];
  1866. + }
  1867. strncpy(buf, a.dotted, buflen);
  1868. free(a.dotted);
  1869. if (a.atype == T_A) { /* ADDRESS */
  1870. - memcpy(in, a.rdata, sizeof(*in));
  1871. - result_buf->h_name = buf;
  1872. - result_buf->h_addrtype = AF_INET;
  1873. - result_buf->h_length = sizeof(*in);
  1874. - result_buf->h_addr_list = (char **) addr_list;
  1875. + memcpy(in, a.rdata, sizeof(*in));
  1876. + result_buf->h_name = buf;
  1877. + result_buf->h_addrtype = AF_INET;
  1878. + result_buf->h_length = sizeof(*in);
  1879. + result_buf->h_addr_list = (char **) addr_list;
  1880. #ifdef __UCLIBC_MJN3_ONLY__
  1881. #warning TODO -- generate the full list
  1882. #endif
  1883. - result_buf->h_aliases = alias; /* TODO: generate the full list */
  1884. - free(packet);
  1885. - break;
  1886. + result_buf->h_aliases = alias; /* TODO: generate the full list */
  1887. + free(packet);
  1888. + break;
  1889. } else {
  1890. - free(packet);
  1891. - *h_errnop=HOST_NOT_FOUND;
  1892. - return TRY_AGAIN;
  1893. + free(packet);
  1894. + *h_errnop=HOST_NOT_FOUND;
  1895. + return TRY_AGAIN;
  1896. }
  1897. }
  1898. @@ -2049,14 +2030,14 @@ int gethostbyname_r(const char * name,
  1899. #ifdef L_gethostbyname2_r
  1900. int gethostbyname2_r(const char *name, int family,
  1901. - struct hostent * result_buf,
  1902. - char * buf, size_t buflen,
  1903. - struct hostent ** result,
  1904. - int * h_errnop)
  1905. + struct hostent * result_buf,
  1906. + char * buf, size_t buflen,
  1907. + struct hostent ** result,
  1908. + int * h_errnop)
  1909. {
  1910. #ifndef __UCLIBC_HAS_IPV6__
  1911. return family == (AF_INET)? gethostbyname_r(name, result_buf,
  1912. - buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1913. + buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1914. #else /* __UCLIBC_HAS_IPV6__ */
  1915. struct in6_addr *in;
  1916. struct in6_addr **addr_list;
  1917. @@ -2084,7 +2065,7 @@ int gethostbyname2_r(const char *name, i
  1918. __set_errno(0); /* to check for missing /etc/hosts. */
  1919. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1920. - buf, buflen, result, h_errnop))==0)
  1921. + buf, buflen, result, h_errnop))==0)
  1922. return i;
  1923. switch (*h_errnop) {
  1924. case HOST_NOT_FOUND:
  1925. @@ -2137,10 +2118,10 @@ int gethostbyname2_r(const char *name, i
  1926. memset((char *) &a, '\0', sizeof(a));
  1927. for (;;) {
  1928. - BIGLOCK;
  1929. - __nameserversXX=__nameservers;
  1930. - __nameserverXX=__nameserver;
  1931. - BIGUNLOCK;
  1932. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1933. + __nameserversXX=__nameservers;
  1934. + __nameserverXX=__nameserver;
  1935. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1936. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1937. @@ -2190,10 +2171,10 @@ int gethostbyname2_r(const char *name, i
  1938. #ifdef L_gethostbyaddr_r
  1939. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1940. - struct hostent * result_buf,
  1941. - char * buf, size_t buflen,
  1942. - struct hostent ** result,
  1943. - int * h_errnop)
  1944. + struct hostent * result_buf,
  1945. + char * buf, size_t buflen,
  1946. + struct hostent ** result,
  1947. + int * h_errnop)
  1948. {
  1949. struct in_addr *in;
  1950. @@ -2234,7 +2215,7 @@ int gethostbyaddr_r (const void *addr, s
  1951. /* do /etc/hosts first */
  1952. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1953. - buf, buflen, result, h_errnop))==0)
  1954. + buf, buflen, result, h_errnop))==0)
  1955. return i;
  1956. switch (*h_errnop) {
  1957. case HOST_NOT_FOUND:
  1958. @@ -2294,7 +2275,7 @@ int gethostbyaddr_r (const void *addr, s
  1959. addr_list[0] = in;
  1960. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1961. - tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1962. + tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1963. #ifdef __UCLIBC_HAS_IPV6__
  1964. } else {
  1965. memcpy(in6->s6_addr, addr, len);
  1966. @@ -2304,7 +2285,7 @@ int gethostbyaddr_r (const void *addr, s
  1967. for (i = len - 1; i >= 0; i--) {
  1968. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1969. - (in6->s6_addr[i] >> 4) & 0xf);
  1970. + (in6->s6_addr[i] >> 4) & 0xf);
  1971. }
  1972. strcpy(qp, "ip6.int");
  1973. #endif /* __UCLIBC_HAS_IPV6__ */
  1974. @@ -2314,10 +2295,10 @@ int gethostbyaddr_r (const void *addr, s
  1975. for (;;) {
  1976. - BIGLOCK;
  1977. - __nameserversXX=__nameservers;
  1978. - __nameserverXX=__nameserver;
  1979. - BIGUNLOCK;
  1980. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1981. + __nameserversXX=__nameservers;
  1982. + __nameserverXX=__nameserver;
  1983. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1984. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  1985. if (i < 0) {
  1986. @@ -2381,7 +2362,7 @@ int gethostbyaddr_r (const void *addr, s
  1987. * Return size of compressed name or -1 if there was an error.
  1988. */
  1989. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  1990. - char *dst, int dstsiz)
  1991. + char *dst, int dstsiz)
  1992. {
  1993. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  1994. @@ -2401,7 +2382,7 @@ int __dn_expand(const u_char *msg, const
  1995. */
  1996. static int printable(int ch)
  1997. {
  1998. - return (ch > 0x20 && ch < 0x7f);
  1999. + return (ch > 0x20 && ch < 0x7f);
  2000. }
  2001. /*
  2002. @@ -2413,18 +2394,18 @@ static int printable(int ch)
  2003. */
  2004. static int special(int ch)
  2005. {
  2006. - switch (ch) {
  2007. + switch (ch) {
  2008. case 0x22: /* '"' */
  2009. case 0x2E: /* '.' */
  2010. case 0x3B: /* ';' */
  2011. case 0x5C: /* '\\' */
  2012. - /* Special modifiers in zone files. */
  2013. + /* Special modifiers in zone files. */
  2014. case 0x40: /* '@' */
  2015. case 0x24: /* '$' */
  2016. - return (1);
  2017. + return (1);
  2018. default:
  2019. - return (0);
  2020. - }
  2021. + return (0);
  2022. + }
  2023. }
  2024. /*
  2025. @@ -2436,7 +2417,7 @@ static int special(int ch)
  2026. * Root domain returns as "." not "".
  2027. */
  2028. int __ns_name_uncompress(const u_char *msg, const u_char *eom,
  2029. - const u_char *src, char *dst, size_t dstsiz)
  2030. + const u_char *src, char *dst, size_t dstsiz)
  2031. {
  2032. u_char tmp[NS_MAXCDNAME];
  2033. int n;
  2034. @@ -2525,7 +2506,7 @@ int __ns_name_ntop(const u_char *src, ch
  2035. return (-1);
  2036. }
  2037. *dn++ = '\0';
  2038. - return (dn - dst);
  2039. + return (dn - dst);
  2040. }
  2041. /*
  2042. @@ -2535,7 +2516,7 @@ int __ns_name_ntop(const u_char *src, ch
  2043. * -1 if it fails, or consumed octets if it succeeds.
  2044. */
  2045. int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2046. - u_char *dst, size_t dstsiz)
  2047. + u_char *dst, size_t dstsiz)
  2048. {
  2049. const u_char *srcp, *dstlim;
  2050. u_char *dstp;
  2051. @@ -2554,46 +2535,46 @@ int __ns_name_unpack(const u_char *msg,
  2052. while ((n = *srcp++) != 0) {
  2053. /* Check for indirection. */
  2054. switch (n & NS_CMPRSFLGS) {
  2055. - case 0:
  2056. - /* Limit checks. */
  2057. - if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2058. - __set_errno (EMSGSIZE);
  2059. - return (-1);
  2060. - }
  2061. - checked += n + 1;
  2062. - *dstp++ = n;
  2063. - memcpy(dstp, srcp, n);
  2064. - dstp += n;
  2065. - srcp += n;
  2066. - break;
  2067. + case 0:
  2068. + /* Limit checks. */
  2069. + if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2070. + __set_errno (EMSGSIZE);
  2071. + return (-1);
  2072. + }
  2073. + checked += n + 1;
  2074. + *dstp++ = n;
  2075. + memcpy(dstp, srcp, n);
  2076. + dstp += n;
  2077. + srcp += n;
  2078. + break;
  2079. - case NS_CMPRSFLGS:
  2080. - if (srcp >= eom) {
  2081. - __set_errno (EMSGSIZE);
  2082. - return (-1);
  2083. - }
  2084. - if (len < 0)
  2085. - len = srcp - src + 1;
  2086. - srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2087. - if (srcp < msg || srcp >= eom) { /* Out of range. */
  2088. - __set_errno (EMSGSIZE);
  2089. - return (-1);
  2090. - }
  2091. - checked += 2;
  2092. - /*
  2093. - * Check for loops in the compressed name;
  2094. - * if we've looked at the whole message,
  2095. - * there must be a loop.
  2096. - */
  2097. - if (checked >= eom - msg) {
  2098. - __set_errno (EMSGSIZE);
  2099. - return (-1);
  2100. - }
  2101. - break;
  2102. + case NS_CMPRSFLGS:
  2103. + if (srcp >= eom) {
  2104. + __set_errno (EMSGSIZE);
  2105. + return (-1);
  2106. + }
  2107. + if (len < 0)
  2108. + len = srcp - src + 1;
  2109. + srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2110. + if (srcp < msg || srcp >= eom) { /* Out of range. */
  2111. + __set_errno (EMSGSIZE);
  2112. + return (-1);
  2113. + }
  2114. + checked += 2;
  2115. + /*
  2116. + * Check for loops in the compressed name;
  2117. + * if we've looked at the whole message,
  2118. + * there must be a loop.
  2119. + */
  2120. + if (checked >= eom - msg) {
  2121. + __set_errno (EMSGSIZE);
  2122. + return (-1);
  2123. + }
  2124. + break;
  2125. - default:
  2126. - __set_errno (EMSGSIZE);
  2127. - return (-1); /* flag error */
  2128. + default:
  2129. + __set_errno (EMSGSIZE);
  2130. + return (-1); /* flag error */
  2131. }
  2132. }
  2133. *dstp = '\0';
  2134. diff --git a/libc/inet/rpc/create_xid.c b/libc/inet/rpc/create_xid.c
  2135. index cbb961e..c86cbb4 100644
  2136. --- a/libc/inet/rpc/create_xid.c
  2137. +++ b/libc/inet/rpc/create_xid.c
  2138. @@ -27,15 +27,7 @@
  2139. /* The RPC code is not threadsafe, but new code should be threadsafe. */
  2140. -#ifdef __UCLIBC_HAS_THREADS__
  2141. -#include <pthread.h>
  2142. -static pthread_mutex_t createxid_lock = PTHREAD_MUTEX_INITIALIZER;
  2143. -# define LOCK __pthread_mutex_lock(&createxid_lock)
  2144. -# define UNLOCK __pthread_mutex_unlock(&createxid_lock);
  2145. -#else
  2146. -# define LOCK
  2147. -# define UNLOCK
  2148. -#endif
  2149. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  2150. static int is_initialized;
  2151. static struct drand48_data __rpc_lrand48_data;
  2152. @@ -43,22 +35,22 @@ static struct drand48_data __rpc_lrand48
  2153. unsigned long
  2154. _create_xid (void)
  2155. {
  2156. - unsigned long res;
  2157. + unsigned long res;
  2158. - LOCK;
  2159. + __UCLIBC_MUTEX_LOCK(mylock);
  2160. - if (!is_initialized)
  2161. - {
  2162. - struct timeval now;
  2163. + if (!is_initialized)
  2164. + {
  2165. + struct timeval now;
  2166. - gettimeofday (&now, (struct timezone *) 0);
  2167. - srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
  2168. - is_initialized = 1;
  2169. - }
  2170. + gettimeofday (&now, (struct timezone *) 0);
  2171. + srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
  2172. + is_initialized = 1;
  2173. + }
  2174. - lrand48_r (&__rpc_lrand48_data, &res);
  2175. + lrand48_r (&__rpc_lrand48_data, &res);
  2176. - UNLOCK;
  2177. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2178. - return res;
  2179. + return res;
  2180. }
  2181. diff --git a/libc/misc/dirent/closedir.c b/libc/misc/dirent/closedir.c
  2182. index 068e2d3..56adb23 100644
  2183. --- a/libc/misc/dirent/closedir.c
  2184. +++ b/libc/misc/dirent/closedir.c
  2185. @@ -4,7 +4,6 @@
  2186. #include <unistd.h>
  2187. #include "dirstream.h"
  2188. -
  2189. int closedir(DIR * dir)
  2190. {
  2191. int fd;
  2192. @@ -19,14 +18,10 @@ int closedir(DIR * dir)
  2193. __set_errno(EBADF);
  2194. return -1;
  2195. }
  2196. -#ifdef __UCLIBC_HAS_THREADS__
  2197. - __pthread_mutex_lock(&(dir->dd_lock));
  2198. -#endif
  2199. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2200. fd = dir->dd_fd;
  2201. dir->dd_fd = -1;
  2202. -#ifdef __UCLIBC_HAS_THREADS__
  2203. - __pthread_mutex_unlock(&(dir->dd_lock));
  2204. -#endif
  2205. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2206. free(dir->dd_buf);
  2207. free(dir);
  2208. return close(fd);
  2209. diff --git a/libc/misc/dirent/dirstream.h b/libc/misc/dirent/dirstream.h
  2210. index 2dd0264..bd721c5 100644
  2211. --- a/libc/misc/dirent/dirstream.h
  2212. +++ b/libc/misc/dirent/dirstream.h
  2213. @@ -26,9 +26,8 @@ Cambridge, MA 02139, USA. */
  2214. #include <features.h>
  2215. #include <sys/types.h>
  2216. -#ifdef __UCLIBC_HAS_THREADS__
  2217. -#include <pthread.h>
  2218. -#endif
  2219. +
  2220. +#include <bits/uClibc_mutex.h>
  2221. /* For now, syscall readdir () only supports one entry at a time. It
  2222. * will be changed in the future.
  2223. @@ -63,11 +62,7 @@ struct __dirstream {
  2224. size_t dd_max;
  2225. /* lock */
  2226. -#ifdef __UCLIBC_HAS_THREADS__
  2227. - pthread_mutex_t dd_lock;
  2228. -#else
  2229. - void *dd_lock;
  2230. -#endif
  2231. + __UCLIBC_MUTEX(dd_lock);
  2232. }; /* stream data from opendir() */
  2233. diff --git a/libc/misc/dirent/readdir.c b/libc/misc/dirent/readdir.c
  2234. index 1f196e1..c55317a 100644
  2235. --- a/libc/misc/dirent/readdir.c
  2236. +++ b/libc/misc/dirent/readdir.c
  2237. @@ -5,7 +5,6 @@
  2238. #include <dirent.h>
  2239. #include "dirstream.h"
  2240. -
  2241. struct dirent *readdir(DIR * dir)
  2242. {
  2243. ssize_t bytes;
  2244. @@ -16,9 +15,7 @@ struct dirent *readdir(DIR * dir)
  2245. return NULL;
  2246. }
  2247. -#ifdef __UCLIBC_HAS_THREADS__
  2248. - __pthread_mutex_lock(&(dir->dd_lock));
  2249. -#endif
  2250. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2251. do {
  2252. if (dir->dd_size <= dir->dd_nextloc) {
  2253. @@ -44,8 +41,6 @@ struct dirent *readdir(DIR * dir)
  2254. } while (de->d_ino == 0);
  2255. all_done:
  2256. -#ifdef __UCLIBC_HAS_THREADS__
  2257. - __pthread_mutex_unlock(&(dir->dd_lock));
  2258. -#endif
  2259. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2260. return de;
  2261. }
  2262. diff --git a/libc/misc/dirent/readdir64.c b/libc/misc/dirent/readdir64.c
  2263. index f798c6f..6da3b0d 100644
  2264. --- a/libc/misc/dirent/readdir64.c
  2265. +++ b/libc/misc/dirent/readdir64.c
  2266. @@ -20,7 +20,6 @@
  2267. #include <dirent.h>
  2268. #include "dirstream.h"
  2269. -
  2270. struct dirent64 *readdir64(DIR * dir)
  2271. {
  2272. ssize_t bytes;
  2273. @@ -31,9 +30,7 @@ struct dirent64 *readdir64(DIR * dir)
  2274. return NULL;
  2275. }
  2276. -#ifdef __UCLIBC_HAS_THREADS__
  2277. - __pthread_mutex_lock(&(dir->dd_lock));
  2278. -#endif
  2279. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2280. do {
  2281. if (dir->dd_size <= dir->dd_nextloc) {
  2282. @@ -59,9 +56,7 @@ struct dirent64 *readdir64(DIR * dir)
  2283. } while (de->d_ino == 0);
  2284. all_done:
  2285. -#ifdef __UCLIBC_HAS_THREADS__
  2286. - __pthread_mutex_unlock(&(dir->dd_lock));
  2287. -#endif
  2288. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2289. return de;
  2290. }
  2291. diff --git a/libc/misc/dirent/readdir64_r.c b/libc/misc/dirent/readdir64_r.c
  2292. index da3564e..cc96eff 100644
  2293. --- a/libc/misc/dirent/readdir64_r.c
  2294. +++ b/libc/misc/dirent/readdir64_r.c
  2295. @@ -19,7 +19,6 @@
  2296. #include <dirent.h>
  2297. #include "dirstream.h"
  2298. -
  2299. int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
  2300. {
  2301. int ret;
  2302. @@ -32,21 +31,19 @@ int readdir64_r(DIR *dir, struct dirent6
  2303. }
  2304. de = NULL;
  2305. -#ifdef __UCLIBC_HAS_THREADS__
  2306. - __pthread_mutex_lock(&(dir->dd_lock));
  2307. -#endif
  2308. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2309. do {
  2310. if (dir->dd_size <= dir->dd_nextloc) {
  2311. - /* read dir->dd_max bytes of directory entries. */
  2312. - bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
  2313. - if (bytes <= 0) {
  2314. - *result = NULL;
  2315. - ret = errno;
  2316. - goto all_done;
  2317. - }
  2318. - dir->dd_size = bytes;
  2319. - dir->dd_nextloc = 0;
  2320. + /* read dir->dd_max bytes of directory entries. */
  2321. + bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
  2322. + if (bytes <= 0) {
  2323. + *result = NULL;
  2324. + ret = errno;
  2325. + goto all_done;
  2326. + }
  2327. + dir->dd_size = bytes;
  2328. + dir->dd_nextloc = 0;
  2329. }
  2330. de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  2331. @@ -66,12 +63,10 @@ int readdir64_r(DIR *dir, struct dirent6
  2332. }
  2333. ret = 0;
  2334. -all_done:
  2335. + all_done:
  2336. -#ifdef __UCLIBC_HAS_THREADS__
  2337. - __pthread_mutex_unlock(&(dir->dd_lock));
  2338. -#endif
  2339. - return((de != NULL)? 0 : ret);
  2340. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2341. + return((de != NULL)? 0 : ret);
  2342. }
  2343. #endif /* __UCLIBC_HAS_LFS__ */
  2344. diff --git a/libc/misc/dirent/readdir_r.c b/libc/misc/dirent/readdir_r.c
  2345. index 245dcbd..aeccdd8 100644
  2346. --- a/libc/misc/dirent/readdir_r.c
  2347. +++ b/libc/misc/dirent/readdir_r.c
  2348. @@ -5,7 +5,6 @@
  2349. #include <dirent.h>
  2350. #include "dirstream.h"
  2351. -
  2352. int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
  2353. {
  2354. int ret;
  2355. @@ -18,21 +17,19 @@ int readdir_r(DIR *dir, struct dirent *e
  2356. }
  2357. de = NULL;
  2358. -#ifdef __UCLIBC_HAS_THREADS__
  2359. - __pthread_mutex_lock(&(dir->dd_lock));
  2360. -#endif
  2361. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2362. do {
  2363. if (dir->dd_size <= dir->dd_nextloc) {
  2364. - /* read dir->dd_max bytes of directory entries. */
  2365. - bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  2366. - if (bytes <= 0) {
  2367. - *result = NULL;
  2368. - ret = errno;
  2369. - goto all_done;
  2370. - }
  2371. - dir->dd_size = bytes;
  2372. - dir->dd_nextloc = 0;
  2373. + /* read dir->dd_max bytes of directory entries. */
  2374. + bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  2375. + if (bytes <= 0) {
  2376. + *result = NULL;
  2377. + ret = errno;
  2378. + goto all_done;
  2379. + }
  2380. + dir->dd_size = bytes;
  2381. + dir->dd_nextloc = 0;
  2382. }
  2383. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  2384. @@ -52,10 +49,8 @@ int readdir_r(DIR *dir, struct dirent *e
  2385. }
  2386. ret = 0;
  2387. -all_done:
  2388. + all_done:
  2389. -#ifdef __UCLIBC_HAS_THREADS__
  2390. - __pthread_mutex_unlock(&(dir->dd_lock));
  2391. -#endif
  2392. - return((de != NULL)? 0 : ret);
  2393. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2394. + return((de != NULL)? 0 : ret);
  2395. }
  2396. diff --git a/libc/misc/dirent/rewinddir.c b/libc/misc/dirent/rewinddir.c
  2397. index 60ef71d..fe8fc2a 100644
  2398. --- a/libc/misc/dirent/rewinddir.c
  2399. +++ b/libc/misc/dirent/rewinddir.c
  2400. @@ -3,7 +3,6 @@
  2401. #include <unistd.h>
  2402. #include "dirstream.h"
  2403. -
  2404. /* rewinddir() just does an lseek(fd,0,0) - see close for comments */
  2405. void rewinddir(DIR * dir)
  2406. {
  2407. @@ -11,12 +10,8 @@ void rewinddir(DIR * dir)
  2408. __set_errno(EBADF);
  2409. return;
  2410. }
  2411. -#ifdef __UCLIBC_HAS_THREADS__
  2412. - __pthread_mutex_lock(&(dir->dd_lock));
  2413. -#endif
  2414. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2415. lseek(dir->dd_fd, 0, SEEK_SET);
  2416. dir->dd_nextoff = dir->dd_nextloc = dir->dd_size = 0;
  2417. -#ifdef __UCLIBC_HAS_THREADS__
  2418. - __pthread_mutex_unlock(&(dir->dd_lock));
  2419. -#endif
  2420. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2421. }
  2422. diff --git a/libc/misc/dirent/seekdir.c b/libc/misc/dirent/seekdir.c
  2423. index 139f1e1..6d6f5f0 100644
  2424. --- a/libc/misc/dirent/seekdir.c
  2425. +++ b/libc/misc/dirent/seekdir.c
  2426. @@ -3,19 +3,14 @@
  2427. #include <unistd.h>
  2428. #include "dirstream.h"
  2429. -
  2430. void seekdir(DIR * dir, long int offset)
  2431. {
  2432. if (!dir) {
  2433. __set_errno(EBADF);
  2434. return;
  2435. }
  2436. -#ifdef __UCLIBC_HAS_THREADS__
  2437. - __pthread_mutex_lock(&(dir->dd_lock));
  2438. -#endif
  2439. + __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  2440. dir->dd_nextoff = lseek(dir->dd_fd, offset, SEEK_SET);
  2441. dir->dd_size = dir->dd_nextloc = 0;
  2442. -#ifdef __UCLIBC_HAS_THREADS__
  2443. - __pthread_mutex_unlock(&(dir->dd_lock));
  2444. -#endif
  2445. + __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  2446. }
  2447. diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c
  2448. index d98a687..af6d848 100644
  2449. --- a/libc/misc/mntent/mntent.c
  2450. +++ b/libc/misc/mntent/mntent.c
  2451. @@ -3,15 +3,9 @@
  2452. #include <string.h>
  2453. #include <mntent.h>
  2454. -#ifdef __UCLIBC_HAS_THREADS__
  2455. -#include <pthread.h>
  2456. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  2457. -# define LOCK __pthread_mutex_lock(&mylock)
  2458. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  2459. -#else
  2460. -# define LOCK
  2461. -# define UNLOCK
  2462. -#endif
  2463. +#include <bits/uClibc_mutex.h>
  2464. +
  2465. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  2466. /* Reentrant version of getmntent. */
  2467. struct mntent *getmntent_r (FILE *filep,
  2468. @@ -67,7 +61,7 @@ struct mntent *getmntent(FILE * filep)
  2469. struct mntent *tmp;
  2470. static char *buff = NULL;
  2471. static struct mntent mnt;
  2472. - LOCK;
  2473. + __UCLIBC_MUTEX_LOCK(mylock);
  2474. if (!buff) {
  2475. buff = malloc(BUFSIZ);
  2476. @@ -76,7 +70,7 @@ struct mntent *getmntent(FILE * filep)
  2477. }
  2478. tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
  2479. - UNLOCK;
  2480. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2481. return(tmp);
  2482. }
  2483. diff --git a/libc/misc/pthread/weaks.c b/libc/misc/pthread/weaks.c
  2484. index 89c2611..c27bd10 100644
  2485. --- a/libc/misc/pthread/weaks.c
  2486. +++ b/libc/misc/pthread/weaks.c
  2487. @@ -21,6 +21,7 @@
  2488. #include <limits.h>
  2489. #include <stdlib.h>
  2490. +static void __pthread_return_void __P ((void));
  2491. static int __pthread_return_0 __P ((void));
  2492. static int __pthread_return_1 __P ((void));
  2493. @@ -104,8 +105,17 @@ weak_alias (__pthread_return_0, __pthrea
  2494. weak_alias (__pthread_return_0, __pthread_mutex_trylock)
  2495. weak_alias (__pthread_return_0, __pthread_mutex_unlock)
  2496. +weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
  2497. +weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
  2498. +
  2499. /**********************************************************************/
  2500. +static void
  2501. +__pthread_return_void (void)
  2502. +{
  2503. + return;
  2504. +}
  2505. +
  2506. static int
  2507. __pthread_return_0 (void)
  2508. {
  2509. diff --git a/libc/misc/syslog/syslog.c b/libc/misc/syslog/syslog.c
  2510. index 2b478e1..9e9ddbf 100644
  2511. --- a/libc/misc/syslog/syslog.c
  2512. +++ b/libc/misc/syslog/syslog.c
  2513. @@ -80,17 +80,9 @@
  2514. #include <ctype.h>
  2515. #include <signal.h>
  2516. +#include <bits/uClibc_mutex.h>
  2517. -#ifdef __UCLIBC_HAS_THREADS__
  2518. -#include <pthread.h>
  2519. -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  2520. -# define LOCK __pthread_mutex_lock(&mylock)
  2521. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  2522. -#else
  2523. -# define LOCK
  2524. -# define UNLOCK
  2525. -#endif
  2526. -
  2527. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  2528. static int LogFile = -1; /* fd for log */
  2529. static int connected; /* have done connect */
  2530. @@ -110,26 +102,26 @@ int setlogmask(int pmask);
  2531. static void
  2532. closelog_intern(int to_default)
  2533. {
  2534. - LOCK;
  2535. + __UCLIBC_MUTEX_LOCK(mylock);
  2536. if (LogFile != -1) {
  2537. (void) close(LogFile);
  2538. }
  2539. LogFile = -1;
  2540. connected = 0;
  2541. if (to_default)
  2542. - {
  2543. - LogStat = 0;
  2544. - LogTag = "syslog";
  2545. - LogFacility = LOG_USER;
  2546. - LogMask = 0xff;
  2547. - }
  2548. - UNLOCK;
  2549. + {
  2550. + LogStat = 0;
  2551. + LogTag = "syslog";
  2552. + LogFacility = LOG_USER;
  2553. + LogMask = 0xff;
  2554. + }
  2555. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2556. }
  2557. static void
  2558. sigpipe_handler (int sig)
  2559. {
  2560. - closelog_intern (0);
  2561. + closelog_intern (0);
  2562. }
  2563. /*
  2564. @@ -165,7 +157,7 @@ vsyslog( int pri, const char *fmt, va_li
  2565. saved_errno = errno;
  2566. - LOCK;
  2567. + __UCLIBC_MUTEX_LOCK(mylock);
  2568. /* See if we should just throw out this message. */
  2569. if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  2570. @@ -208,7 +200,7 @@ vsyslog( int pri, const char *fmt, va_li
  2571. if (p >= end || p < head_end) { /* Returned -1 in case of error... */
  2572. static const char truncate_msg[12] = "[truncated] ";
  2573. memmove(head_end + sizeof(truncate_msg), head_end,
  2574. - end - head_end - sizeof(truncate_msg));
  2575. + end - head_end - sizeof(truncate_msg));
  2576. memcpy(head_end, truncate_msg, sizeof(truncate_msg));
  2577. if (p < head_end) {
  2578. while (p < end && *p) {
  2579. @@ -261,11 +253,11 @@ vsyslog( int pri, const char *fmt, va_li
  2580. (void)close(fd);
  2581. }
  2582. -getout:
  2583. - UNLOCK;
  2584. + getout:
  2585. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2586. if (sigpipe == 0)
  2587. sigaction (SIGPIPE, &oldaction,
  2588. - (struct sigaction *) NULL);
  2589. + (struct sigaction *) NULL);
  2590. }
  2591. /*
  2592. @@ -276,48 +268,48 @@ openlog( const char *ident, int logstat,
  2593. {
  2594. int logType = SOCK_DGRAM;
  2595. - LOCK;
  2596. + __UCLIBC_MUTEX_LOCK(mylock);
  2597. if (ident != NULL)
  2598. - LogTag = ident;
  2599. + LogTag = ident;
  2600. LogStat = logstat;
  2601. if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  2602. - LogFacility = logfac;
  2603. + LogFacility = logfac;
  2604. if (LogFile == -1) {
  2605. - SyslogAddr.sa_family = AF_UNIX;
  2606. - (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  2607. - sizeof(SyslogAddr.sa_data));
  2608. -retry:
  2609. - if (LogStat & LOG_NDELAY) {
  2610. - if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
  2611. - UNLOCK;
  2612. - return;
  2613. - }
  2614. - /* fcntl(LogFile, F_SETFD, 1); */
  2615. - }
  2616. + SyslogAddr.sa_family = AF_UNIX;
  2617. + (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  2618. + sizeof(SyslogAddr.sa_data));
  2619. + retry:
  2620. + if (LogStat & LOG_NDELAY) {
  2621. + if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
  2622. + goto DONE;
  2623. + }
  2624. + /* fcntl(LogFile, F_SETFD, 1); */
  2625. + }
  2626. }
  2627. if (LogFile != -1 && !connected) {
  2628. - if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  2629. - sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
  2630. - {
  2631. - connected = 1;
  2632. - } else if (logType == SOCK_DGRAM) {
  2633. - logType = SOCK_STREAM;
  2634. - if (LogFile != -1) {
  2635. - close(LogFile);
  2636. - LogFile = -1;
  2637. - }
  2638. - goto retry;
  2639. - } else {
  2640. - if (LogFile != -1) {
  2641. - close(LogFile);
  2642. - LogFile = -1;
  2643. - }
  2644. - }
  2645. + if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  2646. + sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
  2647. + {
  2648. + connected = 1;
  2649. + } else if (logType == SOCK_DGRAM) {
  2650. + logType = SOCK_STREAM;
  2651. + if (LogFile != -1) {
  2652. + close(LogFile);
  2653. + LogFile = -1;
  2654. + }
  2655. + goto retry;
  2656. + } else {
  2657. + if (LogFile != -1) {
  2658. + close(LogFile);
  2659. + LogFile = -1;
  2660. + }
  2661. + }
  2662. }
  2663. - UNLOCK;
  2664. + DONE:
  2665. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2666. }
  2667. /*
  2668. @@ -335,10 +327,10 @@ int setlogmask(int pmask)
  2669. int omask;
  2670. omask = LogMask;
  2671. - LOCK;
  2672. + __UCLIBC_MUTEX_LOCK(mylock);
  2673. if (pmask != 0)
  2674. - LogMask = pmask;
  2675. - UNLOCK;
  2676. + LogMask = pmask;
  2677. + __UCLIBC_MUTEX_UNLOCK(mylock);
  2678. return (omask);
  2679. }
  2680. diff --git a/libc/misc/time/time.c b/libc/misc/time/time.c
  2681. index f43bb8a..6165a52 100644
  2682. --- a/libc/misc/time/time.c
  2683. +++ b/libc/misc/time/time.c
  2684. @@ -143,6 +143,8 @@
  2685. #include <locale.h>
  2686. #include <bits/uClibc_uintmaxtostr.h>
  2687. +#include <bits/uClibc_mutex.h>
  2688. +
  2689. #ifdef __UCLIBC_HAS_XLOCALE__
  2690. #include <xlocale.h>
  2691. #endif
  2692. @@ -191,21 +193,7 @@ typedef struct {
  2693. char tzname[TZNAME_MAX+1];
  2694. } rule_struct;
  2695. -#ifdef __UCLIBC_HAS_THREADS__
  2696. -
  2697. -#include <pthread.h>
  2698. -
  2699. -extern pthread_mutex_t _time_tzlock;
  2700. -
  2701. -#define TZLOCK __pthread_mutex_lock(&_time_tzlock)
  2702. -#define TZUNLOCK __pthread_mutex_unlock(&_time_tzlock)
  2703. -
  2704. -#else
  2705. -
  2706. -#define TZLOCK ((void) 0)
  2707. -#define TZUNLOCK ((void) 0)
  2708. -
  2709. -#endif
  2710. +__UCLIBC_MUTEX_EXTERN(_time_tzlock);
  2711. extern rule_struct _time_tzinfo[2];
  2712. @@ -542,13 +530,13 @@ struct tm *localtime(const time_t *timer
  2713. struct tm *localtime_r(register const time_t *__restrict timer,
  2714. register struct tm *__restrict result)
  2715. {
  2716. - TZLOCK;
  2717. + __UCLIBC_MUTEX_LOCK(_time_tzlock);
  2718. tzset();
  2719. __time_localtime_tzi(timer, result, _time_tzinfo);
  2720. - TZUNLOCK;
  2721. + __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  2722. return result;
  2723. }
  2724. @@ -1037,7 +1025,7 @@ size_t __XL(strftime)(char *__restrict s
  2725. goto LOOP;
  2726. }
  2727. - o = spec + 26; /* set to "????" */
  2728. + o = ((const char *) spec) + 26; /* set to "????" */
  2729. if ((code & MASK_SPEC) == CALC_SPEC) {
  2730. if (*p == 's') {
  2731. @@ -1073,17 +1061,15 @@ size_t __XL(strftime)(char *__restrict s
  2732. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  2733. -#define RSP_TZUNLOCK ((void) 0)
  2734. #define RSP_TZNAME timeptr->tm_zone
  2735. #define RSP_GMT_OFFSET (-timeptr->tm_gmtoff)
  2736. #else
  2737. -#define RSP_TZUNLOCK TZUNLOCK
  2738. #define RSP_TZNAME rsp->tzname
  2739. #define RSP_GMT_OFFSET rsp->gmt_offset
  2740. - TZLOCK;
  2741. + __UCLIBC_MUTEX_LOCK(_time_tzlock);
  2742. rsp = _time_tzinfo;
  2743. if (timeptr->tm_isdst > 0) {
  2744. @@ -1114,15 +1100,17 @@ size_t __XL(strftime)(char *__restrict s
  2745. }
  2746. #endif
  2747. o_count = SIZE_MAX;
  2748. - RSP_TZUNLOCK;
  2749. +/* RSP_TZUNLOCK; */
  2750. +#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  2751. goto OUTPUT;
  2752. +#endif
  2753. } else { /* z */
  2754. *s = '+';
  2755. if ((tzo = -RSP_GMT_OFFSET) < 0) {
  2756. tzo = -tzo;
  2757. *s = '-';
  2758. }
  2759. - RSP_TZUNLOCK;
  2760. +/* RSP_TZUNLOCK; */
  2761. ++s;
  2762. --count;
  2763. @@ -1131,7 +1119,13 @@ size_t __XL(strftime)(char *__restrict s
  2764. i = 16 + 6; /* 0-fill, width = 4 */
  2765. }
  2766. -
  2767. +#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  2768. +#else
  2769. + __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  2770. + if (*p == 'Z') {
  2771. + goto OUTPUT;
  2772. + }
  2773. +#endif
  2774. } else {
  2775. /* TODO: don't need year for U, W */
  2776. for (i=0 ; i < 3 ; i++) {
  2777. @@ -1664,9 +1658,7 @@ int daylight = 0;
  2778. long timezone = 0;
  2779. char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
  2780. -#ifdef __UCLIBC_HAS_THREADS__
  2781. -pthread_mutex_t _time_tzlock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  2782. -#endif
  2783. +__UCLIBC_MUTEX_INIT(_time_tzlock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  2784. rule_struct _time_tzinfo[2];
  2785. @@ -1796,7 +1788,7 @@ void tzset(void)
  2786. static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
  2787. #endif /* __UCLIBC_HAS_TZ_CACHING__ */
  2788. - TZLOCK;
  2789. + __UCLIBC_MUTEX_LOCK(_time_tzlock);
  2790. e = getenv(TZ); /* TZ env var always takes precedence. */
  2791. @@ -1962,10 +1954,10 @@ void tzset(void)
  2792. daylight = !!_time_tzinfo[1].tzname[0];
  2793. timezone = _time_tzinfo[0].gmt_offset;
  2794. -#if defined(__UCLIBC_HAS_TZ_FILE__)
  2795. +#if defined(__UCLIBC_HAS_TZ_FILE__) || defined(__UCLIBC_HAS_TZ_CACHING__)
  2796. FAST_DONE:
  2797. #endif
  2798. - TZUNLOCK;
  2799. + __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  2800. }
  2801. #endif
  2802. @@ -2167,13 +2159,13 @@ time_t _time_mktime(struct tm *timeptr,
  2803. {
  2804. time_t t;
  2805. - TZLOCK;
  2806. + __UCLIBC_MUTEX_LOCK(_time_tzlock);
  2807. tzset();
  2808. t = _time_mktime_tzi(timeptr, store_on_success, _time_tzinfo);
  2809. - TZUNLOCK;
  2810. + __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  2811. return t;
  2812. }
  2813. diff --git a/libc/misc/ttyent/getttyent.c b/libc/misc/ttyent/getttyent.c
  2814. index 6e2fbd2..c85c73a 100644
  2815. --- a/libc/misc/ttyent/getttyent.c
  2816. +++ b/libc/misc/ttyent/getttyent.c
  2817. @@ -35,9 +35,6 @@
  2818. #include <ctype.h>
  2819. #include <string.h>
  2820. #include <stdlib.h>
  2821. -#ifdef __UCLIBC_HAS_THREADS__
  2822. -#include <pthread.h>
  2823. -#endif
  2824. static char zapchar;
  2825. static FILE *tf;
  2826. @@ -50,8 +47,8 @@ struct ttyent * getttynam(const char *tt
  2827. setttyent();
  2828. while ((t = getttyent()))
  2829. - if (!strcmp(tty, t->ty_name))
  2830. - break;
  2831. + if (!strcmp(tty, t->ty_name))
  2832. + break;
  2833. endttyent();
  2834. return (t);
  2835. }
  2836. @@ -67,27 +64,27 @@ static char * skip(register char *p)
  2837. register int c, q;
  2838. for (q = 0, t = p; (c = *p) != '\0'; p++) {
  2839. - if (c == '"') {
  2840. - q ^= QUOTED; /* obscure, but nice */
  2841. - continue;
  2842. - }
  2843. - if (q == QUOTED && *p == '\\' && *(p+1) == '"')
  2844. - p++;
  2845. - *t++ = *p;
  2846. - if (q == QUOTED)
  2847. - continue;
  2848. - if (c == '#') {
  2849. - zapchar = c;
  2850. - *p = 0;
  2851. - break;
  2852. - }
  2853. - if (c == '\t' || c == ' ' || c == '\n') {
  2854. - zapchar = c;
  2855. - *p++ = 0;
  2856. - while ((c = *p) == '\t' || c == ' ' || c == '\n')
  2857. - p++;
  2858. - break;
  2859. - }
  2860. + if (c == '"') {
  2861. + q ^= QUOTED; /* obscure, but nice */
  2862. + continue;
  2863. + }
  2864. + if (q == QUOTED && *p == '\\' && *(p+1) == '"')
  2865. + p++;
  2866. + *t++ = *p;
  2867. + if (q == QUOTED)
  2868. + continue;
  2869. + if (c == '#') {
  2870. + zapchar = c;
  2871. + *p = 0;
  2872. + break;
  2873. + }
  2874. + if (c == '\t' || c == ' ' || c == '\n') {
  2875. + zapchar = c;
  2876. + *p++ = 0;
  2877. + while ((c = *p) == '\t' || c == ' ' || c == '\n')
  2878. + p++;
  2879. + break;
  2880. + }
  2881. }
  2882. *--t = '\0';
  2883. return (p);
  2884. @@ -104,46 +101,46 @@ struct ttyent * getttyent(void)
  2885. register int c;
  2886. register char *p;
  2887. static char *line = NULL;
  2888. + struct ttyent *retval = NULL;
  2889. if (!tf && !setttyent())
  2890. - return (NULL);
  2891. + return (NULL);
  2892. if (!line) {
  2893. - line = malloc(BUFSIZ);
  2894. + line = malloc(BUFSIZ);
  2895. if (!line)
  2896. abort();
  2897. }
  2898. - __STDIO_ALWAYS_THREADLOCK(tf);
  2899. + __STDIO_ALWAYS_THREADLOCK(tf);
  2900. for (;;) {
  2901. - if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
  2902. - __STDIO_ALWAYS_THREADUNLOCK(tf);
  2903. - return (NULL);
  2904. - }
  2905. - /* skip lines that are too big */
  2906. - if (!index(p, '\n')) {
  2907. - while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
  2908. - ;
  2909. - continue;
  2910. - }
  2911. - while (isspace(*p))
  2912. - ++p;
  2913. - if (*p && *p != '#')
  2914. - break;
  2915. + if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
  2916. + goto DONE;
  2917. + }
  2918. + /* skip lines that are too big */
  2919. + if (!index(p, '\n')) {
  2920. + while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
  2921. + ;
  2922. + continue;
  2923. + }
  2924. + while (isspace(*p))
  2925. + ++p;
  2926. + if (*p && *p != '#')
  2927. + break;
  2928. }
  2929. zapchar = 0;
  2930. tty.ty_name = p;
  2931. p = skip(p);
  2932. if (!*(tty.ty_getty = p))
  2933. - tty.ty_getty = tty.ty_type = NULL;
  2934. + tty.ty_getty = tty.ty_type = NULL;
  2935. else {
  2936. - p = skip(p);
  2937. - if (!*(tty.ty_type = p))
  2938. - tty.ty_type = NULL;
  2939. - else
  2940. - p = skip(p);
  2941. + p = skip(p);
  2942. + if (!*(tty.ty_type = p))
  2943. + tty.ty_type = NULL;
  2944. + else
  2945. + p = skip(p);
  2946. }
  2947. tty.ty_status = 0;
  2948. tty.ty_window = NULL;
  2949. @@ -151,43 +148,45 @@ struct ttyent * getttyent(void)
  2950. #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
  2951. #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
  2952. for (; *p; p = skip(p)) {
  2953. - if (scmp(_TTYS_OFF))
  2954. - tty.ty_status &= ~TTY_ON;
  2955. - else if (scmp(_TTYS_ON))
  2956. - tty.ty_status |= TTY_ON;
  2957. - else if (scmp(_TTYS_SECURE))
  2958. - tty.ty_status |= TTY_SECURE;
  2959. - else if (vcmp(_TTYS_WINDOW))
  2960. - tty.ty_window = value(p);
  2961. - else
  2962. - break;
  2963. + if (scmp(_TTYS_OFF))
  2964. + tty.ty_status &= ~TTY_ON;
  2965. + else if (scmp(_TTYS_ON))
  2966. + tty.ty_status |= TTY_ON;
  2967. + else if (scmp(_TTYS_SECURE))
  2968. + tty.ty_status |= TTY_SECURE;
  2969. + else if (vcmp(_TTYS_WINDOW))
  2970. + tty.ty_window = value(p);
  2971. + else
  2972. + break;
  2973. }
  2974. - /* We can release the lock only here since `zapchar' is global. */
  2975. - __STDIO_ALWAYS_THREADUNLOCK(tf);
  2976. if (zapchar == '#' || *p == '#')
  2977. - while ((c = *++p) == ' ' || c == '\t')
  2978. - ;
  2979. + while ((c = *++p) == ' ' || c == '\t')
  2980. + ;
  2981. tty.ty_comment = p;
  2982. if (*p == 0)
  2983. - tty.ty_comment = 0;
  2984. + tty.ty_comment = 0;
  2985. if ((p = index(p, '\n')))
  2986. - *p = '\0';
  2987. - return (&tty);
  2988. + *p = '\0';
  2989. + retval = &tty;
  2990. +
  2991. + DONE:
  2992. + __STDIO_ALWAYS_THREADUNLOCK(tf);
  2993. + return retval;
  2994. }
  2995. int setttyent(void)
  2996. {
  2997. if (tf) {
  2998. - rewind(tf);
  2999. - return (1);
  3000. + rewind(tf);
  3001. + return (1);
  3002. } else if ((tf = fopen(_PATH_TTYS, "r"))) {
  3003. - /* We do the locking ourselves. */
  3004. + /* We do the locking ourselves. */
  3005. #ifdef __UCLIBC_HAS_THREADS__
  3006. - __fsetlocking (tf, FSETLOCKING_BYCALLER);
  3007. + __fsetlocking (tf, FSETLOCKING_BYCALLER);
  3008. #endif
  3009. - return (1);
  3010. + return (1);
  3011. }
  3012. return (0);
  3013. }
  3014. @@ -197,9 +196,9 @@ int endttyent(void)
  3015. int rval;
  3016. if (tf) {
  3017. - rval = !(fclose(tf) == EOF);
  3018. - tf = NULL;
  3019. - return (rval);
  3020. + rval = !(fclose(tf) == EOF);
  3021. + tf = NULL;
  3022. + return (rval);
  3023. }
  3024. return (1);
  3025. }
  3026. diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
  3027. index c1d8d6f..0fc6df4 100644
  3028. --- a/libc/misc/utmp/utent.c
  3029. +++ b/libc/misc/utmp/utent.c
  3030. @@ -20,19 +20,9 @@
  3031. #include <string.h>
  3032. #include <utmp.h>
  3033. +#include <bits/uClibc_mutex.h>
  3034. -
  3035. -#ifdef __UCLIBC_HAS_THREADS__
  3036. -#include <pthread.h>
  3037. -static pthread_mutex_t utmplock = PTHREAD_MUTEX_INITIALIZER;
  3038. -# define LOCK __pthread_mutex_lock(&utmplock)
  3039. -# define UNLOCK __pthread_mutex_unlock(&utmplock)
  3040. -#else
  3041. -# define LOCK
  3042. -# define UNLOCK
  3043. -#endif
  3044. -
  3045. -
  3046. +__UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
  3047. /* Some global crap */
  3048. static int static_fd = -1;
  3049. @@ -46,19 +36,19 @@ static struct utmp *__getutent(int utmp_
  3050. {
  3051. if (utmp_fd == -1) {
  3052. - setutent();
  3053. + setutent();
  3054. }
  3055. if (utmp_fd == -1) {
  3056. - return NULL;
  3057. + return NULL;
  3058. }
  3059. - LOCK;
  3060. + __UCLIBC_MUTEX_LOCK(utmplock);
  3061. if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) != sizeof(struct utmp))
  3062. - {
  3063. - return NULL;
  3064. - }
  3065. + {
  3066. + return NULL;
  3067. + }
  3068. - UNLOCK;
  3069. + __UCLIBC_MUTEX_UNLOCK(utmplock);
  3070. return &static_utmp;
  3071. }
  3072. @@ -66,39 +56,39 @@ void setutent(void)
  3073. {
  3074. int ret;
  3075. - LOCK;
  3076. + __UCLIBC_MUTEX_LOCK(utmplock);
  3077. if (static_fd == -1) {
  3078. - if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
  3079. - if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
  3080. - goto bummer;
  3081. - }
  3082. - }
  3083. - /* Make sure the file will be closed on exec() */
  3084. - ret = fcntl(static_fd, F_GETFD, 0);
  3085. - if (ret >= 0) {
  3086. - ret = fcntl(static_fd, F_GETFD, 0);
  3087. - }
  3088. - if (ret < 0) {
  3089. -bummer:
  3090. - UNLOCK;
  3091. - static_fd = -1;
  3092. - close(static_fd);
  3093. - return;
  3094. - }
  3095. + if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
  3096. + if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
  3097. + goto bummer;
  3098. + }
  3099. + }
  3100. + /* Make sure the file will be closed on exec() */
  3101. + ret = fcntl(static_fd, F_GETFD, 0);
  3102. + if (ret >= 0) {
  3103. + ret = fcntl(static_fd, F_GETFD, 0);
  3104. + }
  3105. + if (ret < 0) {
  3106. + bummer:
  3107. + close(static_fd);
  3108. + static_fd = -1;
  3109. + goto DONE;
  3110. + }
  3111. }
  3112. lseek(static_fd, 0, SEEK_SET);
  3113. - UNLOCK;
  3114. + DONE:
  3115. + __UCLIBC_MUTEX_UNLOCK(utmplock);
  3116. return;
  3117. }
  3118. void endutent(void)
  3119. {
  3120. - LOCK;
  3121. + __UCLIBC_MUTEX_LOCK(utmplock);
  3122. if (static_fd != -1) {
  3123. - close(static_fd);
  3124. + close(static_fd);
  3125. }
  3126. static_fd = -1;
  3127. - UNLOCK;
  3128. + __UCLIBC_MUTEX_UNLOCK(utmplock);
  3129. }
  3130. /* Locking is done in __getutent */
  3131. @@ -113,22 +103,22 @@ struct utmp *getutid (const struct utmp
  3132. struct utmp *lutmp;
  3133. while ((lutmp = __getutent(static_fd)) != NULL) {
  3134. - if ( (utmp_entry->ut_type == RUN_LVL ||
  3135. - utmp_entry->ut_type == BOOT_TIME ||
  3136. - utmp_entry->ut_type == NEW_TIME ||
  3137. - utmp_entry->ut_type == OLD_TIME) &&
  3138. - lutmp->ut_type == utmp_entry->ut_type)
  3139. - {
  3140. - return lutmp;
  3141. - }
  3142. - if ( (utmp_entry->ut_type == INIT_PROCESS ||
  3143. - utmp_entry->ut_type == DEAD_PROCESS ||
  3144. - utmp_entry->ut_type == LOGIN_PROCESS ||
  3145. - utmp_entry->ut_type == USER_PROCESS) &&
  3146. - !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
  3147. - {
  3148. - return lutmp;
  3149. - }
  3150. + if ( (utmp_entry->ut_type == RUN_LVL ||
  3151. + utmp_entry->ut_type == BOOT_TIME ||
  3152. + utmp_entry->ut_type == NEW_TIME ||
  3153. + utmp_entry->ut_type == OLD_TIME) &&
  3154. + lutmp->ut_type == utmp_entry->ut_type)
  3155. + {
  3156. + return lutmp;
  3157. + }
  3158. + if ( (utmp_entry->ut_type == INIT_PROCESS ||
  3159. + utmp_entry->ut_type == DEAD_PROCESS ||
  3160. + utmp_entry->ut_type == LOGIN_PROCESS ||
  3161. + utmp_entry->ut_type == USER_PROCESS) &&
  3162. + !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
  3163. + {
  3164. + return lutmp;
  3165. + }
  3166. }
  3167. return NULL;
  3168. @@ -140,11 +130,11 @@ struct utmp *getutline(const struct utmp
  3169. struct utmp *lutmp;
  3170. while ((lutmp = __getutent(static_fd)) != NULL) {
  3171. - if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
  3172. - !strcmp(lutmp->ut_line, utmp_entry->ut_line))
  3173. - {
  3174. - return lutmp;
  3175. - }
  3176. + if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
  3177. + !strcmp(lutmp->ut_line, utmp_entry->ut_line))
  3178. + {
  3179. + return lutmp;
  3180. + }
  3181. }
  3182. return NULL;
  3183. @@ -152,42 +142,42 @@ struct utmp *getutline(const struct utmp
  3184. struct utmp *pututline (const struct utmp *utmp_entry)
  3185. {
  3186. - LOCK;
  3187. + __UCLIBC_MUTEX_LOCK(utmplock);
  3188. /* Ignore the return value. That way, if they've already positioned
  3189. the file pointer where they want it, everything will work out. */
  3190. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  3191. if (getutid(utmp_entry) != NULL) {
  3192. - lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  3193. - if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  3194. - return NULL;
  3195. + lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  3196. + if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  3197. + return NULL;
  3198. } else {
  3199. - lseek(static_fd, (off_t) 0, SEEK_END);
  3200. - if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  3201. - return NULL;
  3202. + lseek(static_fd, (off_t) 0, SEEK_END);
  3203. + if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  3204. + return NULL;
  3205. }
  3206. - UNLOCK;
  3207. + __UCLIBC_MUTEX_UNLOCK(utmplock);
  3208. return (struct utmp *)utmp_entry;
  3209. }
  3210. int utmpname (const char *new_ut_name)
  3211. {
  3212. - LOCK;
  3213. + __UCLIBC_MUTEX_LOCK(utmplock);
  3214. if (new_ut_name != NULL) {
  3215. - if (static_ut_name != default_file_name)
  3216. - free((char *)static_ut_name);
  3217. - static_ut_name = strdup(new_ut_name);
  3218. - if (static_ut_name == NULL) {
  3219. - /* We should probably whine about out-of-memory
  3220. - * errors here... Instead just reset to the default */
  3221. - static_ut_name = default_file_name;
  3222. - }
  3223. + if (static_ut_name != default_file_name)
  3224. + free((char *)static_ut_name);
  3225. + static_ut_name = strdup(new_ut_name);
  3226. + if (static_ut_name == NULL) {
  3227. + /* We should probably whine about out-of-memory
  3228. + * errors here... Instead just reset to the default */
  3229. + static_ut_name = default_file_name;
  3230. + }
  3231. }
  3232. if (static_fd != -1)
  3233. - close(static_fd);
  3234. - UNLOCK;
  3235. + close(static_fd);
  3236. + __UCLIBC_MUTEX_UNLOCK(utmplock);
  3237. return 0;
  3238. }
  3239. diff --git a/libc/misc/wchar/wstdio.c b/libc/misc/wchar/wstdio.c
  3240. index b49494f..408c57a 100644
  3241. --- a/libc/misc/wchar/wstdio.c
  3242. +++ b/libc/misc/wchar/wstdio.c
  3243. @@ -82,9 +82,6 @@ strong_alias(NAME,NAME##_unlocked) \
  3244. void NAME PARAMS
  3245. #endif
  3246. -#define __STDIO_THREADLOCK_OPENLIST
  3247. -#define __STDIO_THREADUNLOCK_OPENLIST
  3248. -
  3249. #else /* __UCLIBC_HAS_THREADS__ */
  3250. #include <pthread.h>
  3251. @@ -112,15 +109,6 @@ void NAME PARAMS \
  3252. } \
  3253. void NAME##_unlocked PARAMS
  3254. -#define __STDIO_THREADLOCK_OPENLIST \
  3255. - __pthread_mutex_lock(&_stdio_openlist_lock)
  3256. -
  3257. -#define __STDIO_THREADUNLOCK_OPENLIST \
  3258. - __pthread_mutex_unlock(&_stdio_openlist_lock)
  3259. -
  3260. -#define __STDIO_THREADTRYLOCK_OPENLIST \
  3261. - __pthread_mutex_trylock(&_stdio_openlist_lock)
  3262. -
  3263. #endif /* __UCLIBC_HAS_THREADS__ */
  3264. #ifndef __STDIO_BUFFERS
  3265. diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c
  3266. index 6b9c251..063fed4 100644
  3267. --- a/libc/pwd_grp/lckpwdf.c
  3268. +++ b/libc/pwd_grp/lckpwdf.c
  3269. @@ -27,15 +27,9 @@
  3270. #include <sys/file.h>
  3271. #include <paths.h>
  3272. -#ifdef __UCLIBC_HAS_THREADS__
  3273. -#include <pthread.h>
  3274. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  3275. -# define LOCK __pthread_mutex_lock(&mylock)
  3276. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  3277. -#else
  3278. -# define LOCK
  3279. -# define UNLOCK
  3280. -#endif
  3281. +#include <bits/uClibc_mutex.h>
  3282. +
  3283. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  3284. /* How long to wait for getting the lock before returning with an
  3285. error. */
  3286. @@ -57,18 +51,18 @@ int lckpwdf (void)
  3287. struct sigaction new_act; /* New signal action. */
  3288. struct flock fl; /* Information struct for locking. */
  3289. int result;
  3290. + int rv = -1;
  3291. if (lock_fd != -1)
  3292. /* Still locked by own process. */
  3293. return -1;
  3294. - LOCK;
  3295. + __UCLIBC_MUTEX_LOCK(mylock);
  3296. lock_fd = open (_PATH_PASSWD, O_WRONLY);
  3297. if (lock_fd == -1) {
  3298. /* Cannot create lock file. */
  3299. - UNLOCK;
  3300. - return -1;
  3301. + goto DONE;
  3302. }
  3303. /* Make sure file gets correctly closed when process finished. */
  3304. @@ -77,16 +71,14 @@ int lckpwdf (void)
  3305. /* Cannot get file flags. */
  3306. close(lock_fd);
  3307. lock_fd = -1;
  3308. - UNLOCK;
  3309. - return -1;
  3310. + goto DONE;
  3311. }
  3312. flags |= FD_CLOEXEC; /* Close on exit. */
  3313. if (fcntl (lock_fd, F_SETFD, flags) < 0) {
  3314. /* Cannot set new flags. */
  3315. close(lock_fd);
  3316. lock_fd = -1;
  3317. - UNLOCK;
  3318. - return -1;
  3319. + goto DONE;
  3320. }
  3321. /* Now we have to get exclusive write access. Since multiple
  3322. @@ -107,8 +99,7 @@ int lckpwdf (void)
  3323. /* Cannot install signal handler. */
  3324. close(lock_fd);
  3325. lock_fd = -1;
  3326. - UNLOCK;
  3327. - return -1;
  3328. + goto DONE;
  3329. }
  3330. /* Now make sure the alarm signal is not blocked. */
  3331. @@ -118,8 +109,7 @@ int lckpwdf (void)
  3332. sigaction (SIGALRM, &saved_act, NULL);
  3333. close(lock_fd);
  3334. lock_fd = -1;
  3335. - UNLOCK;
  3336. - return -1;
  3337. + goto DONE;
  3338. }
  3339. /* Start timer. If we cannot get the lock in the specified time we
  3340. @@ -146,12 +136,14 @@ int lckpwdf (void)
  3341. if (result < 0) {
  3342. close(lock_fd);
  3343. lock_fd = -1;
  3344. - UNLOCK;
  3345. - return -1;
  3346. + goto DONE;
  3347. }
  3348. - UNLOCK;
  3349. - return 0;
  3350. + rv = 0;
  3351. +
  3352. + DONE:
  3353. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3354. + return rv;
  3355. }
  3356. @@ -164,11 +156,11 @@ int ulckpwdf (void)
  3357. result = -1;
  3358. }
  3359. else {
  3360. - LOCK;
  3361. + __UCLIBC_MUTEX_LOCK(mylock);
  3362. result = close (lock_fd);
  3363. /* Mark descriptor as unused. */
  3364. lock_fd = -1;
  3365. - UNLOCK;
  3366. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3367. }
  3368. return result;
  3369. diff --git a/libc/pwd_grp/pwd_grp.c b/libc/pwd_grp/pwd_grp.c
  3370. index 91c0d83..a302c7c 100644
  3371. --- a/libc/pwd_grp/pwd_grp.c
  3372. +++ b/libc/pwd_grp/pwd_grp.c
  3373. @@ -42,9 +42,8 @@
  3374. #include <pwd.h>
  3375. #include <grp.h>
  3376. #include <shadow.h>
  3377. -#ifdef __UCLIBC_HAS_THREADS__
  3378. -#include <pthread.h>
  3379. -#endif
  3380. +
  3381. +#include <bits/uClibc_mutex.h>
  3382. /**********************************************************************/
  3383. /* Sizes for staticly allocated buffers. */
  3384. @@ -445,34 +444,27 @@ int getpw(uid_t uid, char *buf)
  3385. /**********************************************************************/
  3386. #ifdef L_getpwent_r
  3387. -#ifdef __UCLIBC_HAS_THREADS__
  3388. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  3389. -# define LOCK __pthread_mutex_lock(&mylock)
  3390. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  3391. -#else
  3392. -# define LOCK ((void) 0)
  3393. -# define UNLOCK ((void) 0)
  3394. -#endif
  3395. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  3396. static FILE *pwf /*= NULL*/;
  3397. void setpwent(void)
  3398. {
  3399. - LOCK;
  3400. + __UCLIBC_MUTEX_LOCK(mylock);
  3401. if (pwf) {
  3402. rewind(pwf);
  3403. }
  3404. - UNLOCK;
  3405. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3406. }
  3407. void endpwent(void)
  3408. {
  3409. - LOCK;
  3410. + __UCLIBC_MUTEX_LOCK(mylock);
  3411. if (pwf) {
  3412. fclose(pwf);
  3413. pwf = NULL;
  3414. }
  3415. - UNLOCK;
  3416. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3417. }
  3418. @@ -482,7 +474,7 @@ int getpwent_r(struct passwd *__restrict
  3419. {
  3420. int rv;
  3421. - LOCK;
  3422. + __UCLIBC_MUTEX_LOCK(mylock);
  3423. *result = NULL; /* In case of error... */
  3424. @@ -500,7 +492,7 @@ int getpwent_r(struct passwd *__restrict
  3425. }
  3426. ERR:
  3427. - UNLOCK;
  3428. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3429. return rv;
  3430. }
  3431. @@ -509,34 +501,27 @@ int getpwent_r(struct passwd *__restrict
  3432. /**********************************************************************/
  3433. #ifdef L_getgrent_r
  3434. -#ifdef __UCLIBC_HAS_THREADS__
  3435. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  3436. -# define LOCK __pthread_mutex_lock(&mylock)
  3437. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  3438. -#else
  3439. -# define LOCK ((void) 0)
  3440. -# define UNLOCK ((void) 0)
  3441. -#endif
  3442. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  3443. static FILE *grf /*= NULL*/;
  3444. void setgrent(void)
  3445. {
  3446. - LOCK;
  3447. + __UCLIBC_MUTEX_LOCK(mylock);
  3448. if (grf) {
  3449. rewind(grf);
  3450. }
  3451. - UNLOCK;
  3452. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3453. }
  3454. void endgrent(void)
  3455. {
  3456. - LOCK;
  3457. + __UCLIBC_MUTEX_LOCK(mylock);
  3458. if (grf) {
  3459. fclose(grf);
  3460. grf = NULL;
  3461. }
  3462. - UNLOCK;
  3463. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3464. }
  3465. int getgrent_r(struct group *__restrict resultbuf,
  3466. @@ -545,7 +530,7 @@ int getgrent_r(struct group *__restrict
  3467. {
  3468. int rv;
  3469. - LOCK;
  3470. + __UCLIBC_MUTEX_LOCK(mylock);
  3471. *result = NULL; /* In case of error... */
  3472. @@ -563,7 +548,7 @@ int getgrent_r(struct group *__restrict
  3473. }
  3474. ERR:
  3475. - UNLOCK;
  3476. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3477. return rv;
  3478. }
  3479. @@ -572,34 +557,27 @@ int getgrent_r(struct group *__restrict
  3480. /**********************************************************************/
  3481. #ifdef L_getspent_r
  3482. -#ifdef __UCLIBC_HAS_THREADS__
  3483. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  3484. -# define LOCK __pthread_mutex_lock(&mylock)
  3485. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  3486. -#else
  3487. -# define LOCK ((void) 0)
  3488. -# define UNLOCK ((void) 0)
  3489. -#endif
  3490. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  3491. static FILE *spf /*= NULL*/;
  3492. void setspent(void)
  3493. {
  3494. - LOCK;
  3495. + __UCLIBC_MUTEX_LOCK(mylock);
  3496. if (spf) {
  3497. rewind(spf);
  3498. }
  3499. - UNLOCK;
  3500. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3501. }
  3502. void endspent(void)
  3503. {
  3504. - LOCK;
  3505. + __UCLIBC_MUTEX_LOCK(mylock);
  3506. if (spf) {
  3507. fclose(spf);
  3508. spf = NULL;
  3509. }
  3510. - UNLOCK;
  3511. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3512. }
  3513. int getspent_r(struct spwd *resultbuf, char *buffer,
  3514. @@ -607,7 +585,7 @@ int getspent_r(struct spwd *resultbuf, c
  3515. {
  3516. int rv;
  3517. - LOCK;
  3518. + __UCLIBC_MUTEX_LOCK(mylock);
  3519. *result = NULL; /* In case of error... */
  3520. @@ -625,7 +603,7 @@ int getspent_r(struct spwd *resultbuf, c
  3521. }
  3522. ERR:
  3523. - UNLOCK;
  3524. + __UCLIBC_MUTEX_UNLOCK(mylock);
  3525. return rv;
  3526. }
  3527. diff --git a/libc/stdio/_READ.c b/libc/stdio/_READ.c
  3528. index 7d3c38c..fe1bc91 100644
  3529. --- a/libc/stdio/_READ.c
  3530. +++ b/libc/stdio/_READ.c
  3531. @@ -41,7 +41,7 @@ size_t __stdio_READ(register FILE *strea
  3532. #warning EINTR?
  3533. #endif
  3534. /* RETRY: */
  3535. - if ((rv = __READ(stream, buf, bufsize)) <= 0) {
  3536. + if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
  3537. if (rv == 0) {
  3538. __STDIO_STREAM_SET_EOF(stream);
  3539. } else {
  3540. diff --git a/libc/stdio/_WRITE.c b/libc/stdio/_WRITE.c
  3541. index d300d39..4131eb7 100644
  3542. --- a/libc/stdio/_WRITE.c
  3543. +++ b/libc/stdio/_WRITE.c
  3544. @@ -47,7 +47,7 @@ size_t __stdio_WRITE(register FILE *stre
  3545. return bufsize;
  3546. }
  3547. stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
  3548. - if ((rv = __WRITE(stream, buf, stodo)) >= 0) {
  3549. + if ((rv = __WRITE(stream, (char *) buf, stodo)) >= 0) {
  3550. #ifdef __UCLIBC_MJN3_ONLY__
  3551. #warning TODO: Make custom stream write return check optional.
  3552. #endif
  3553. diff --git a/libc/stdio/_fopen.c b/libc/stdio/_fopen.c
  3554. index f7f5bb6..4984f11 100644
  3555. --- a/libc/stdio/_fopen.c
  3556. +++ b/libc/stdio/_fopen.c
  3557. @@ -194,10 +194,23 @@ FILE *_stdio_fopen(intptr_t fname_or_mod
  3558. #endif
  3559. #ifdef __STDIO_HAS_OPENLIST
  3560. - __STDIO_THREADLOCK_OPENLIST;
  3561. - stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  3562. - _stdio_openlist = stream; /* the head of the list. */
  3563. - __STDIO_THREADUNLOCK_OPENLIST;
  3564. +#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  3565. + if (!(stream->__modeflags & __FLAG_FREEFILE))
  3566. + {
  3567. + /* An freopen call so the file was never removed from the list. */
  3568. + }
  3569. + else
  3570. +#endif
  3571. + {
  3572. + /* We have to lock the del mutex in case another thread wants to fclose()
  3573. + * the last file. */
  3574. + __STDIO_THREADLOCK_OPENLIST_DEL;
  3575. + __STDIO_THREADLOCK_OPENLIST_ADD;
  3576. + stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  3577. + _stdio_openlist = stream; /* the head of the list. */
  3578. + __STDIO_THREADUNLOCK_OPENLIST_ADD;
  3579. + __STDIO_THREADUNLOCK_OPENLIST_DEL;
  3580. + }
  3581. #endif
  3582. __STDIO_STREAM_VALIDATE(stream);
  3583. diff --git a/libc/stdio/_stdio.c b/libc/stdio/_stdio.c
  3584. index 4aae3c4..9cfe02c 100644
  3585. --- a/libc/stdio/_stdio.c
  3586. +++ b/libc/stdio/_stdio.c
  3587. @@ -151,8 +151,12 @@ FILE *__stdout = _stdio_streams + 1; /*
  3588. FILE *_stdio_openlist = _stdio_streams;
  3589. # ifdef __UCLIBC_HAS_THREADS__
  3590. -pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  3591. -int _stdio_openlist_delflag = 0;
  3592. +__UCLIBC_MUTEX_INIT(_stdio_openlist_add_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  3593. +#ifdef __STDIO_BUFFERS
  3594. +__UCLIBC_MUTEX_INIT(_stdio_openlist_del_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  3595. +volatile int _stdio_openlist_use_count = 0;
  3596. +int _stdio_openlist_del_count = 0;
  3597. +#endif
  3598. # endif
  3599. #endif
  3600. @@ -162,10 +166,10 @@ int _stdio_openlist_delflag = 0;
  3601. /* 2 if threading not initialized and 0 otherwise; */
  3602. int _stdio_user_locking = 2;
  3603. -void __stdio_init_mutex(pthread_mutex_t *m)
  3604. +void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
  3605. {
  3606. - static const pthread_mutex_t __stdio_mutex_initializer
  3607. - = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  3608. + const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
  3609. + PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  3610. memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
  3611. }
  3612. @@ -184,7 +188,11 @@ void _stdio_term(void)
  3613. * locked, then I suppose there is a chance that a pointer in the
  3614. * chain might be corrupt due to a partial store.
  3615. */
  3616. - __stdio_init_mutex(&_stdio_openlist_lock);
  3617. + __stdio_init_mutex(&_stdio_openlist_add_lock);
  3618. +#warning check
  3619. +#ifdef __STDIO_BUFFERS
  3620. + __stdio_init_mutex(&_stdio_openlist_del_lock);
  3621. +#endif
  3622. /* Next we need to worry about the streams themselves. If a stream
  3623. * is currently locked, then it may be in an invalid state. So we
  3624. @@ -192,7 +200,7 @@ void _stdio_term(void)
  3625. * Then we reinitialize the locks.
  3626. */
  3627. for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
  3628. - if (__STDIO_ALWAYS_THREADTRYLOCK(ptr)) {
  3629. + if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
  3630. /* The stream is already locked, so we don't want to touch it.
  3631. * However, if we have custom streams, we can't just close it
  3632. * or leave it locked since a custom stream may be stacked
  3633. @@ -258,10 +266,6 @@ void _stdio_init(void)
  3634. #error Assumption violated about __MASK_READING and __FLAG_UNGOT
  3635. #endif
  3636. -#ifdef __UCLIBC_HAS_THREADS__
  3637. -#include <pthread.h>
  3638. -#endif
  3639. -
  3640. #ifndef NDEBUG
  3641. void _stdio_validate_FILE(const FILE *stream)
  3642. diff --git a/libc/stdio/_stdio.h b/libc/stdio/_stdio.h
  3643. index e3c2c58..decf57d 100644
  3644. --- a/libc/stdio/_stdio.h
  3645. +++ b/libc/stdio/_stdio.h
  3646. @@ -22,23 +22,57 @@
  3647. #include <wchar.h>
  3648. #endif
  3649. -#ifdef __UCLIBC_HAS_THREADS__
  3650. -#include <pthread.h>
  3651. +#include <bits/uClibc_mutex.h>
  3652. -#define __STDIO_THREADLOCK_OPENLIST \
  3653. - __pthread_mutex_lock(&_stdio_openlist_lock)
  3654. +#define __STDIO_THREADLOCK_OPENLIST_ADD \
  3655. + __UCLIBC_MUTEX_LOCK(_stdio_openlist_add_lock)
  3656. -#define __STDIO_THREADUNLOCK_OPENLIST \
  3657. - __pthread_mutex_unlock(&_stdio_openlist_lock)
  3658. +#define __STDIO_THREADUNLOCK_OPENLIST_ADD \
  3659. + __UCLIBC_MUTEX_UNLOCK(_stdio_openlist_add_lock)
  3660. -#define __STDIO_THREADTRYLOCK_OPENLIST \
  3661. - __pthread_mutex_trylock(&_stdio_openlist_lock)
  3662. +#ifdef __STDIO_BUFFERS
  3663. -#else
  3664. +#define __STDIO_THREADLOCK_OPENLIST_DEL \
  3665. + __UCLIBC_MUTEX_LOCK(_stdio_openlist_del_lock)
  3666. +
  3667. +#define __STDIO_THREADUNLOCK_OPENLIST_DEL \
  3668. + __UCLIBC_MUTEX_UNLOCK(_stdio_openlist_del_lock)
  3669. -#define __STDIO_THREADLOCK_OPENLIST ((void)0)
  3670. -#define __STDIO_THREADUNLOCK_OPENLIST ((void)0)
  3671. +#define __STDIO_OPENLIST_INC_USE \
  3672. +do { \
  3673. + __STDIO_THREADLOCK_OPENLIST_DEL; \
  3674. + ++_stdio_openlist_use_count; \
  3675. + __STDIO_THREADUNLOCK_OPENLIST_DEL; \
  3676. +} while (0)
  3677. +
  3678. +extern void _stdio_openlist_dec_use(void);
  3679. +
  3680. +#define __STDIO_OPENLIST_DEC_USE \
  3681. + _stdio_openlist_dec_use()
  3682. +
  3683. +#define __STDIO_OPENLIST_INC_DEL_CNT \
  3684. +do { \
  3685. + __STDIO_THREADLOCK_OPENLIST_DEL; \
  3686. + ++_stdio_openlist_del_count; \
  3687. + __STDIO_THREADUNLOCK_OPENLIST_DEL; \
  3688. +} while (0)
  3689. +
  3690. +#define __STDIO_OPENLIST_DEC_DEL_CNT \
  3691. +do { \
  3692. + __STDIO_THREADLOCK_OPENLIST_DEL; \
  3693. + --_stdio_openlist_del_count; \
  3694. + __STDIO_THREADUNLOCK_OPENLIST_DEL; \
  3695. +} while (0)
  3696. +
  3697. +#endif /* __STDIO_BUFFERS */
  3698. +#ifndef __STDIO_THREADLOCK_OPENLIST_DEL
  3699. +#define __STDIO_THREADLOCK_OPENLIST_DEL ((void)0)
  3700. +#define __STDIO_THREADUNLOCK_OPENLIST_DEL ((void)0)
  3701. +#define __STDIO_OPENLIST_INC_USE ((void)0)
  3702. +#define __STDIO_OPENLIST_DEC_USE ((void)0)
  3703. +#define __STDIO_OPENLIST_INC_DEL_CNT ((void)0)
  3704. +#define __STDIO_OPENLIST_DEC_DEL_CNT ((void)0)
  3705. #endif
  3706. #define __UNDEFINED_OR_NONPORTABLE ((void)0)
  3707. diff --git a/libc/stdio/fclose.c b/libc/stdio/fclose.c
  3708. index 4df2e42..dfababc 100644
  3709. --- a/libc/stdio/fclose.c
  3710. +++ b/libc/stdio/fclose.c
  3711. @@ -12,30 +12,34 @@ int fclose(register FILE *stream)
  3712. int rv = 0;
  3713. __STDIO_AUTO_THREADLOCK_VAR;
  3714. - /* First, remove the file from the open file list. */
  3715. -#ifdef __STDIO_HAS_OPENLIST
  3716. - {
  3717. - register FILE *ptr;
  3718. -
  3719. - __STDIO_THREADLOCK_OPENLIST;
  3720. - if ((ptr = _stdio_openlist) == stream) {
  3721. - _stdio_openlist = stream->__nextopen;
  3722. - } else {
  3723. - while (ptr) {
  3724. - if (ptr->__nextopen == stream) {
  3725. - ptr->__nextopen = stream->__nextopen;
  3726. - break;
  3727. - }
  3728. - ptr = ptr->__nextopen;
  3729. - }
  3730. - }
  3731. - __STDIO_THREADUNLOCK_OPENLIST;
  3732. -
  3733. - if (!ptr) { /* Did not find stream in the open file list! */
  3734. - return EOF;
  3735. - }
  3736. - }
  3737. -#endif
  3738. +#warning dead code... but may want to simply check and not remove
  3739. +/* #ifdef __STDIO_HAS_OPENLIST */
  3740. +/* #if !defined(__UCLIBC_HAS_THREADS__) || !defined(__STDIO_BUFFERS) */
  3741. +/* /\* First, remove the file from the open file list. *\/ */
  3742. +/* { */
  3743. +/* register FILE *ptr; */
  3744. +
  3745. +/* __STDIO_THREADLOCK_OPENLIST; */
  3746. +/* if ((ptr = _stdio_openlist) == stream) { */
  3747. +/* #warning does a mod!!! */
  3748. +/* _stdio_openlist = stream->__nextopen; */
  3749. +/* } else { */
  3750. +/* while (ptr) { */
  3751. +/* if (ptr->__nextopen == stream) { */
  3752. +/* ptr->__nextopen = stream->__nextopen; */
  3753. +/* break; */
  3754. +/* } */
  3755. +/* ptr = ptr->__nextopen; */
  3756. +/* } */
  3757. +/* } */
  3758. +/* __STDIO_THREADUNLOCK_OPENLIST; */
  3759. +
  3760. +/* if (!ptr) { /\* Did not find stream in the open file list! *\/ */
  3761. +/* return EOF; */
  3762. +/* } */
  3763. +/* } */
  3764. +/* #endif */
  3765. +/* #endif */
  3766. __STDIO_AUTO_THREADLOCK(stream);
  3767. @@ -80,7 +84,15 @@ int fclose(register FILE *stream)
  3768. __STDIO_AUTO_THREADUNLOCK(stream);
  3769. __STDIO_STREAM_FREE_BUFFER(stream);
  3770. +#warning... inefficient - locks and unlocks twice and walks whole list
  3771. +#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  3772. + /* inefficient - locks/unlocks twice and walks whole list */
  3773. + __STDIO_OPENLIST_INC_USE;
  3774. + __STDIO_OPENLIST_INC_DEL_CNT;
  3775. + __STDIO_OPENLIST_DEC_USE; /* This with free the file if necessary. */
  3776. +#else
  3777. __STDIO_STREAM_FREE_FILE(stream);
  3778. +#endif
  3779. return rv;
  3780. }
  3781. diff --git a/libc/stdio/fcloseall.c b/libc/stdio/fcloseall.c
  3782. index dbb6000..f62281a 100644
  3783. --- a/libc/stdio/fcloseall.c
  3784. +++ b/libc/stdio/fcloseall.c
  3785. @@ -19,14 +19,34 @@ int fcloseall (void)
  3786. #ifdef __STDIO_HAS_OPENLIST
  3787. int retval = 0;
  3788. + FILE *f;
  3789. - __STDIO_THREADLOCK_OPENLIST;
  3790. - while (_stdio_openlist) {
  3791. - if (fclose(_stdio_openlist)) {
  3792. +#warning remove dead code
  3793. +/* __STDIO_THREADLOCK_OPENLIST; */
  3794. +/* while (_stdio_openlist) { */
  3795. +/* if (fclose(_stdio_openlist)) { */
  3796. +/* retval = EOF; */
  3797. +/* } */
  3798. +/* } */
  3799. +/* __STDIO_THREADUNLOCK_OPENLIST; */
  3800. +
  3801. + __STDIO_OPENLIST_INC_USE;
  3802. +
  3803. +#warning should probably have a get_head() operation
  3804. + __STDIO_THREADLOCK_OPENLIST_ADD;
  3805. + f = _stdio_openlist;
  3806. + __STDIO_THREADUNLOCK_OPENLIST_ADD;
  3807. +
  3808. + while (f) {
  3809. +#warning should probably have a get_next() operation
  3810. + FILE *n = f->__nextopen;
  3811. + if (fclose(f)) {
  3812. retval = EOF;
  3813. }
  3814. + f = n;
  3815. }
  3816. - __STDIO_THREADUNLOCK_OPENLIST;
  3817. +
  3818. + __STDIO_OPENLIST_DEC_USE;
  3819. return retval;
  3820. diff --git a/libc/stdio/fflush.c b/libc/stdio/fflush.c
  3821. index 6baa0ec..66b65cd 100644
  3822. --- a/libc/stdio/fflush.c
  3823. +++ b/libc/stdio/fflush.c
  3824. @@ -20,23 +20,50 @@ weak_alias(__fflush_unlocked,fflush_unlo
  3825. weak_alias(__fflush_unlocked,fflush);
  3826. #endif
  3827. -#ifdef __UCLIBC_HAS_THREADS__
  3828. /* Even if the stream is set to user-locking, we still need to lock
  3829. * when all (lbf) writing streams are flushed. */
  3830. -#define MY_STDIO_THREADLOCK(STREAM) \
  3831. - if (_stdio_user_locking != 2) { \
  3832. - __STDIO_ALWAYS_THREADLOCK(STREAM); \
  3833. - }
  3834. -#define MY_STDIO_THREADUNLOCK(STREAM) \
  3835. - if (_stdio_user_locking != 2) { \
  3836. - __STDIO_ALWAYS_THREADUNLOCK(STREAM); \
  3837. - }
  3838. -#else
  3839. -#define MY_STDIO_THREADLOCK(STREAM) ((void)0)
  3840. -#define MY_STDIO_THREADUNLOCK(STREAM) ((void)0)
  3841. -#endif
  3842. +#define __MY_STDIO_THREADLOCK(__stream) \
  3843. + __UCLIBC_MUTEX_CONDITIONAL_LOCK((__stream)->__lock, \
  3844. + (_stdio_user_locking != 2))
  3845. +
  3846. +#define __MY_STDIO_THREADUNLOCK(__stream) \
  3847. + __UCLIBC_MUTEX_CONDITIONAL_UNLOCK((__stream)->__lock, \
  3848. + (_stdio_user_locking != 2))
  3849. +#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  3850. +void _stdio_openlist_dec_use(void)
  3851. +{
  3852. + __STDIO_THREADLOCK_OPENLIST_DEL;
  3853. + if ((_stdio_openlist_use_count == 1) && (_stdio_openlist_del_count > 0)) {
  3854. + FILE *p = NULL;
  3855. + FILE *n;
  3856. + FILE *stream;
  3857. +
  3858. + __STDIO_THREADLOCK_OPENLIST_ADD;
  3859. + for (stream = _stdio_openlist; stream; stream = n) {
  3860. +#warning walk the list and clear out all fclosed()d files
  3861. + n = stream->__nextopen;
  3862. +#warning fix for nonatomic
  3863. + if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  3864. + == (__FLAG_READONLY|__FLAG_WRITEONLY)
  3865. + ) { /* The file was closed so remove from the list. */
  3866. + if (!p) {
  3867. + _stdio_openlist = n;
  3868. + } else {
  3869. + p->__nextopen = n;
  3870. + }
  3871. + __STDIO_STREAM_FREE_FILE(stream);
  3872. + } else {
  3873. + p = stream;
  3874. + }
  3875. + }
  3876. + __STDIO_THREADUNLOCK_OPENLIST_DEL;
  3877. + }
  3878. + --_stdio_openlist_use_count;
  3879. + __STDIO_THREADUNLOCK_OPENLIST_DEL;
  3880. +}
  3881. +#endif
  3882. int __fflush_unlocked(register FILE *stream)
  3883. {
  3884. @@ -60,23 +87,39 @@ int __fflush_unlocked(register FILE *str
  3885. }
  3886. if (!stream) { /* Flush all (lbf) writing streams. */
  3887. - __STDIO_THREADLOCK_OPENLIST;
  3888. - for (stream = _stdio_openlist; stream ; stream = stream->__nextopen) {
  3889. - MY_STDIO_THREADLOCK(stream);
  3890. - if (!(((stream->__modeflags | bufmask)
  3891. - ^ (__FLAG_WRITING|__FLAG_LBF)
  3892. - ) & (__FLAG_WRITING|__MASK_BUFMODE))
  3893. - ) {
  3894. - if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  3895. - __STDIO_STREAM_DISABLE_PUTC(stream);
  3896. - __STDIO_STREAM_CLEAR_WRITING(stream);
  3897. - } else {
  3898. - retval = EOF;
  3899. +
  3900. + __STDIO_OPENLIST_INC_USE;
  3901. +
  3902. + __STDIO_THREADLOCK_OPENLIST_ADD;
  3903. + stream = _stdio_openlist;
  3904. + __STDIO_THREADUNLOCK_OPENLIST_ADD;
  3905. +
  3906. + while(stream) {
  3907. + /* We only care about currently writing streams and do not want to
  3908. + * block trying to obtain mutexes on non-writing streams. */
  3909. +#warning fix for nonatomic
  3910. +#warning unnecessary check if no threads
  3911. + if (__STDIO_STREAM_IS_WRITING(stream)) { /* ONLY IF ATOMIC!!! */
  3912. + __MY_STDIO_THREADLOCK(stream);
  3913. + /* Need to check again once we have the lock. */
  3914. + if (!(((stream->__modeflags | bufmask)
  3915. + ^ (__FLAG_WRITING|__FLAG_LBF)
  3916. + ) & (__FLAG_WRITING|__MASK_BUFMODE))
  3917. + ) {
  3918. + if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  3919. + __STDIO_STREAM_DISABLE_PUTC(stream);
  3920. + __STDIO_STREAM_CLEAR_WRITING(stream);
  3921. + } else {
  3922. + retval = EOF;
  3923. + }
  3924. }
  3925. + __MY_STDIO_THREADUNLOCK(stream);
  3926. }
  3927. - MY_STDIO_THREADUNLOCK(stream);
  3928. + stream = stream->__nextopen;
  3929. }
  3930. - __STDIO_THREADUNLOCK_OPENLIST;
  3931. +
  3932. + __STDIO_OPENLIST_DEC_USE;
  3933. +
  3934. } else if (__STDIO_STREAM_IS_WRITING(stream)) {
  3935. if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  3936. __STDIO_STREAM_DISABLE_PUTC(stream);
  3937. diff --git a/libc/stdio/flockfile.c b/libc/stdio/flockfile.c
  3938. index 0dcc7c2..3fad711 100644
  3939. --- a/libc/stdio/flockfile.c
  3940. +++ b/libc/stdio/flockfile.c
  3941. @@ -11,6 +11,6 @@ void flockfile(FILE *stream)
  3942. {
  3943. __STDIO_STREAM_VALIDATE(stream);
  3944. - __STDIO_ALWAYS_THREADLOCK(stream);
  3945. + __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(stream);
  3946. }
  3947. diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c
  3948. index 0eccaac..36b8488 100644
  3949. --- a/libc/stdio/freopen.c
  3950. +++ b/libc/stdio/freopen.c
  3951. @@ -42,6 +42,8 @@ FILE *freopen(const char * __restrict fi
  3952. __STDIO_STREAM_VALIDATE(stream);
  3953. + __STDIO_OPENLIST_INC_USE; /* Do not remove the file from the list. */
  3954. +
  3955. /* First, flush and close, but don't deallocate, the stream. */
  3956. /* This also removes the stream for the open file list. */
  3957. dynmode = (stream->__modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
  3958. @@ -57,9 +59,16 @@ FILE *freopen(const char * __restrict fi
  3959. fp = _stdio_fopen(((intptr_t) filename), mode, stream, FILEDES_ARG);
  3960. +#warning if fp is NULL, then we do not free file (but beware stdin,stdout,stderr)
  3961. + if (fp) {
  3962. + __STDIO_OPENLIST_DEC_DEL_CNT;
  3963. + }
  3964. +
  3965. /* Reset the allocation flags. */
  3966. stream->__modeflags |= dynmode;
  3967. + __STDIO_OPENLIST_DEC_USE;
  3968. +
  3969. __STDIO_AUTO_THREADUNLOCK(stream);
  3970. return fp;
  3971. diff --git a/libc/stdio/ftello.c b/libc/stdio/ftello.c
  3972. index 7092f34..69385ce 100644
  3973. --- a/libc/stdio/ftello.c
  3974. +++ b/libc/stdio/ftello.c
  3975. @@ -48,7 +48,10 @@ OFFSET_TYPE FTELL(register FILE *stream)
  3976. __STDIO_STREAM_VALIDATE(stream);
  3977. - if ((__SEEK(stream, &pos, SEEK_CUR) < 0)
  3978. + if ((__SEEK(stream, &pos,
  3979. + ((__STDIO_STREAM_IS_WRITING(stream)
  3980. + && (stream->__modeflags & __FLAG_APPEND))
  3981. + ? SEEK_END : SEEK_CUR)) < 0)
  3982. || (__stdio_adjust_position(stream, &pos) < 0)) {
  3983. pos = -1;
  3984. }
  3985. diff --git a/libc/stdio/ftrylockfile.c b/libc/stdio/ftrylockfile.c
  3986. index d85b8ff..0d2e156 100644
  3987. --- a/libc/stdio/ftrylockfile.c
  3988. +++ b/libc/stdio/ftrylockfile.c
  3989. @@ -15,5 +15,5 @@ int ftrylockfile(FILE *stream)
  3990. {
  3991. __STDIO_STREAM_VALIDATE(stream);
  3992. - return __STDIO_ALWAYS_THREADTRYLOCK(stream);
  3993. + return __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(stream);
  3994. }
  3995. diff --git a/libc/stdio/funlockfile.c b/libc/stdio/funlockfile.c
  3996. index 048c093..2ddf097 100644
  3997. --- a/libc/stdio/funlockfile.c
  3998. +++ b/libc/stdio/funlockfile.c
  3999. @@ -11,5 +11,5 @@ void funlockfile(FILE *stream)
  4000. {
  4001. __STDIO_STREAM_VALIDATE(stream);
  4002. - __STDIO_ALWAYS_THREADUNLOCK(stream);
  4003. + __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(stream);
  4004. }
  4005. diff --git a/libc/stdio/popen.c b/libc/stdio/popen.c
  4006. index c7887ad..ab8d296 100644
  4007. --- a/libc/stdio/popen.c
  4008. +++ b/libc/stdio/popen.c
  4009. @@ -14,6 +14,7 @@
  4010. * Fix failure exit code for failed execve().
  4011. */
  4012. +#warning hmm... susv3 says "Pipe streams are byte-oriented."
  4013. #include <stdio.h>
  4014. #include <stdlib.h>
  4015. @@ -21,6 +22,8 @@
  4016. #include <unistd.h>
  4017. #include <sys/wait.h>
  4018. +#include <bits/uClibc_mutex.h>
  4019. +
  4020. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  4021. #include <sys/syscall.h>
  4022. #if ! defined __NR_vfork
  4023. @@ -29,19 +32,11 @@
  4024. # define VFORK_UNLOCK ((void) 0)
  4025. #endif
  4026. -#ifdef __UCLIBC_HAS_THREADS__
  4027. -#include <pthread.h>
  4028. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  4029. -# define LOCK __pthread_mutex_lock(&mylock)
  4030. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  4031. -#else
  4032. -# define LOCK ((void) 0)
  4033. -# define UNLOCK ((void) 0)
  4034. -#endif
  4035. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  4036. #ifndef VFORK_LOCK
  4037. -# define VFORK_LOCK LOCK
  4038. -# define VFORK_UNLOCK UNLOCK
  4039. +# define VFORK_LOCK __UCLIBC_MUTEX_LOCK(mylock)
  4040. +# define VFORK_UNLOCK __UCLIBC_MUTEX_UNLOCK(mylock)
  4041. #endif
  4042. struct popen_list_item {
  4043. @@ -118,10 +113,10 @@ FILE *popen(const char *command, const c
  4044. if (pid > 0) { /* Parent of vfork... */
  4045. pi->pid = pid;
  4046. pi->f = fp;
  4047. - LOCK;
  4048. + __UCLIBC_MUTEX_LOCK(mylock);
  4049. pi->next = popen_list;
  4050. popen_list = pi;
  4051. - UNLOCK;
  4052. + __UCLIBC_MUTEX_UNLOCK(mylock);
  4053. return fp;
  4054. }
  4055. @@ -136,6 +131,8 @@ FILE *popen(const char *command, const c
  4056. return NULL;
  4057. }
  4058. +#warning is pclose correct wrt the new mutex semantics?
  4059. +
  4060. int pclose(FILE *stream)
  4061. {
  4062. struct popen_list_item *p;
  4063. @@ -144,7 +141,7 @@ int pclose(FILE *stream)
  4064. /* First, find the list entry corresponding to stream and remove it
  4065. * from the list. Set p to the list item (NULL if not found). */
  4066. - LOCK;
  4067. + __UCLIBC_MUTEX_LOCK(mylock);
  4068. if ((p = popen_list) != NULL) {
  4069. if (p->f == stream) {
  4070. popen_list = p->next;
  4071. @@ -163,7 +160,7 @@ int pclose(FILE *stream)
  4072. } while (1);
  4073. }
  4074. }
  4075. - UNLOCK;
  4076. + __UCLIBC_MUTEX_UNLOCK(mylock);
  4077. if (p) {
  4078. pid = p->pid; /* Save the pid we need */
  4079. diff --git a/libc/stdio/setvbuf.c b/libc/stdio/setvbuf.c
  4080. index 3fe62c6..6d53ab1 100644
  4081. --- a/libc/stdio/setvbuf.c
  4082. +++ b/libc/stdio/setvbuf.c
  4083. @@ -75,8 +75,8 @@ int setvbuf(register FILE * __restrict s
  4084. }
  4085. stream->__modeflags |= alloc_flag;
  4086. - stream->__bufstart = buf;
  4087. - stream->__bufend = buf + size;
  4088. + stream->__bufstart = (unsigned char *) buf;
  4089. + stream->__bufend = (unsigned char *) buf + size;
  4090. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  4091. __STDIO_STREAM_DISABLE_GETC(stream);
  4092. __STDIO_STREAM_DISABLE_PUTC(stream);
  4093. diff --git a/libc/stdio/vasprintf.c b/libc/stdio/vasprintf.c
  4094. index 688ab7c..6d7664d 100644
  4095. --- a/libc/stdio/vasprintf.c
  4096. +++ b/libc/stdio/vasprintf.c
  4097. @@ -63,6 +63,8 @@ int vasprintf(char **__restrict buf, con
  4098. free(*buf);
  4099. *buf = NULL;
  4100. }
  4101. + } else {
  4102. + rv = -1;
  4103. }
  4104. }
  4105. diff --git a/libc/stdio/vdprintf.c b/libc/stdio/vdprintf.c
  4106. index de8362c..7cb707f 100644
  4107. --- a/libc/stdio/vdprintf.c
  4108. +++ b/libc/stdio/vdprintf.c
  4109. @@ -15,8 +15,8 @@ int vdprintf(int filedes, const char * _
  4110. #ifdef __STDIO_BUFFERS
  4111. char buf[64]; /* TODO: provide _optional_ buffering? */
  4112. - f.__bufend = buf + sizeof(buf);
  4113. - f.__bufstart = buf;
  4114. + f.__bufend = (unsigned char *) buf + sizeof(buf);
  4115. + f.__bufstart = (unsigned char *) buf;
  4116. __STDIO_STREAM_DISABLE_GETC(&f);
  4117. __STDIO_STREAM_DISABLE_PUTC(&f);
  4118. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  4119. diff --git a/libc/stdio/vfprintf.c b/libc/stdio/vfprintf.c
  4120. index 10114f0..9214e3b 100644
  4121. --- a/libc/stdio/vfprintf.c
  4122. +++ b/libc/stdio/vfprintf.c
  4123. @@ -569,7 +569,7 @@ int _ppfs_init(register ppfs_t *ppfs, co
  4124. ppfs->fmtpos = fmt0; /* rewind */
  4125. }
  4126. -#ifdef NL_MAX_ARG
  4127. +#ifdef NL_ARGMAX
  4128. /* If we have positional args, make sure we know all the types. */
  4129. {
  4130. register int *p = ppfs->argtype;
  4131. @@ -581,7 +581,7 @@ int _ppfs_init(register ppfs_t *ppfs, co
  4132. ++p;
  4133. }
  4134. }
  4135. -#endif /* NL_MAX_ARG */
  4136. +#endif /* NL_ARGMAX */
  4137. return 0;
  4138. }
  4139. @@ -1214,7 +1214,7 @@ static size_t _fp_out_narrow(FILE *fp, i
  4140. }
  4141. len = buflen;
  4142. }
  4143. - return r + OUTNSTR(fp, (const char *) buf, len);
  4144. + return r + OUTNSTR(fp, (const unsigned char *) buf, len);
  4145. }
  4146. #endif /* __STDIO_PRINTF_FLOAT */
  4147. diff --git a/libc/stdlib/abort.c b/libc/stdlib/abort.c
  4148. index 77c2cdc..9f69918 100644
  4149. --- a/libc/stdlib/abort.c
  4150. +++ b/libc/stdlib/abort.c
  4151. @@ -70,16 +70,9 @@ extern void _exit __P((int __status)) __
  4152. static int been_there_done_that = 0;
  4153. /* Be prepared in case multiple threads try to abort() */
  4154. -#ifdef __UCLIBC_HAS_THREADS__
  4155. -# include <pthread.h>
  4156. -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  4157. -# define LOCK __pthread_mutex_lock(&mylock)
  4158. -# define UNLOCK __pthread_mutex_unlock(&mylock)
  4159. -#else
  4160. -# define LOCK
  4161. -# define UNLOCK
  4162. -#endif
  4163. +#include <bits/uClibc_mutex.h>
  4164. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  4165. /* Cause an abnormal program termination with core-dump */
  4166. void abort(void)
  4167. @@ -87,7 +80,7 @@ void abort(void)
  4168. sigset_t sigset;
  4169. /* Make sure we acquire the lock before proceeding */
  4170. - LOCK;
  4171. + __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
  4172. /* Unmask SIGABRT to be sure we can get it */
  4173. if (__sigemptyset(&sigset) == 0 && __sigaddset(&sigset, SIGABRT) == 0) {
  4174. @@ -110,9 +103,9 @@ void abort(void)
  4175. #endif
  4176. abort_it:
  4177. - UNLOCK;
  4178. + __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(mylock);
  4179. raise(SIGABRT);
  4180. - LOCK;
  4181. + __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
  4182. }
  4183. /* Still here? Try to remove any signal handlers */
  4184. diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
  4185. index 280f42c..b028068 100644
  4186. --- a/libc/stdlib/atexit.c
  4187. +++ b/libc/stdlib/atexit.c
  4188. @@ -40,17 +40,9 @@
  4189. #include <stdlib.h>
  4190. #include <errno.h>
  4191. +#include <bits/uClibc_mutex.h>
  4192. -#ifdef __UCLIBC_HAS_THREADS__
  4193. -#include <pthread.h>
  4194. -extern pthread_mutex_t mylock;
  4195. -# define LOCK __pthread_mutex_lock(&mylock)
  4196. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  4197. -#else
  4198. -# define LOCK
  4199. -# define UNLOCK
  4200. -#endif
  4201. -
  4202. +__UCLIBC_MUTEX_EXTERN(__atexit_lock);
  4203. typedef void (*aefuncp) (void); /* atexit function pointer */
  4204. typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
  4205. @@ -90,8 +82,9 @@ extern struct exit_function __exit_funct
  4206. int atexit(aefuncp func)
  4207. {
  4208. struct exit_function *efp;
  4209. + int rv = -1;
  4210. - LOCK;
  4211. + __UCLIBC_MUTEX_LOCK(__atexit_lock);
  4212. if (func) {
  4213. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  4214. /* If we are out of function table slots, make some more */
  4215. @@ -99,18 +92,16 @@ int atexit(aefuncp func)
  4216. efp=realloc(__exit_function_table,
  4217. (__exit_slots+20)*sizeof(struct exit_function));
  4218. if (efp==NULL) {
  4219. - UNLOCK;
  4220. __set_errno(ENOMEM);
  4221. - return -1;
  4222. + goto DONE;
  4223. }
  4224. __exit_function_table = efp;
  4225. __exit_slots+=20;
  4226. }
  4227. #else
  4228. if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
  4229. - UNLOCK;
  4230. __set_errno(ENOMEM);
  4231. - return -1;
  4232. + goto DONE;
  4233. }
  4234. #endif
  4235. __exit_cleanup = __exit_handler; /* enable cleanup */
  4236. @@ -118,8 +109,12 @@ int atexit(aefuncp func)
  4237. efp->type = ef_atexit;
  4238. efp->funcs.atexit = func;
  4239. }
  4240. - UNLOCK;
  4241. - return 0;
  4242. +
  4243. + rv = 0;
  4244. +
  4245. + DONE:
  4246. + __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
  4247. + return rv;
  4248. }
  4249. #endif
  4250. @@ -133,8 +128,9 @@ int atexit(aefuncp func)
  4251. int on_exit(oefuncp func, void *arg)
  4252. {
  4253. struct exit_function *efp;
  4254. + int rv = -1;
  4255. - LOCK;
  4256. + __UCLIBC_MUTEX_LOCK(__atexit_lock);
  4257. if (func) {
  4258. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  4259. /* If we are out of function table slots, make some more */
  4260. @@ -142,18 +138,16 @@ int on_exit(oefuncp func, void *arg)
  4261. efp=realloc(__exit_function_table,
  4262. (__exit_slots+20)*sizeof(struct exit_function));
  4263. if (efp==NULL) {
  4264. - UNLOCK;
  4265. __set_errno(ENOMEM);
  4266. - return -1;
  4267. + goto DONE;
  4268. }
  4269. __exit_function_table=efp;
  4270. __exit_slots+=20;
  4271. }
  4272. #else
  4273. if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
  4274. - UNLOCK;
  4275. __set_errno(ENOMEM);
  4276. - return -1;
  4277. + goto DONE;
  4278. }
  4279. #endif
  4280. @@ -163,8 +157,12 @@ int on_exit(oefuncp func, void *arg)
  4281. efp->funcs.on_exit.func = func;
  4282. efp->funcs.on_exit.arg = arg;
  4283. }
  4284. - UNLOCK;
  4285. - return 0;
  4286. +
  4287. + rv = 0;
  4288. +
  4289. + DONE:
  4290. + __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
  4291. + return rv;
  4292. }
  4293. #endif
  4294. @@ -214,9 +212,8 @@ void __exit_handler(int status)
  4295. #ifdef L_exit
  4296. extern void weak_function _stdio_term(void);
  4297. void (*__exit_cleanup) (int) = 0;
  4298. -#ifdef __UCLIBC_HAS_THREADS__
  4299. -pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  4300. -#endif
  4301. +
  4302. +__UCLIBC_MUTEX_INIT(__atexit_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  4303. #ifdef __UCLIBC_CTOR_DTOR__
  4304. extern void (*__app_fini)(void);
  4305. @@ -229,11 +226,11 @@ extern void (*__rtld_fini)(void);
  4306. void exit(int rv)
  4307. {
  4308. /* Perform exit-specific cleanup (atexit and on_exit) */
  4309. - LOCK;
  4310. + __UCLIBC_MUTEX_LOCK(__atexit_lock);
  4311. if (__exit_cleanup) {
  4312. __exit_cleanup(rv);
  4313. }
  4314. - UNLOCK;
  4315. + __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
  4316. #ifdef __UCLIBC_CTOR_DTOR__
  4317. if (__app_fini != NULL)
  4318. diff --git a/libc/stdlib/malloc-simple/alloc.c b/libc/stdlib/malloc-simple/alloc.c
  4319. index ed14c37..519a875 100644
  4320. --- a/libc/stdlib/malloc-simple/alloc.c
  4321. +++ b/libc/stdlib/malloc-simple/alloc.c
  4322. @@ -108,15 +108,14 @@ void free(void *ptr)
  4323. #endif
  4324. #ifdef L_memalign
  4325. -#ifdef __UCLIBC_HAS_THREADS__
  4326. -#include <pthread.h>
  4327. -pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  4328. -# define LOCK __pthread_mutex_lock(&__malloc_lock)
  4329. -# define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
  4330. -#else
  4331. -# define LOCK
  4332. -# define UNLOCK
  4333. -#endif
  4334. +
  4335. +#include <bits/uClibc_mutex.h>
  4336. +
  4337. +__UCLIBC_MUTEX_EXTERN(__malloc_lock);
  4338. +
  4339. +#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  4340. +#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  4341. +
  4342. /* List of blocks allocated with memalign or valloc */
  4343. struct alignlist
  4344. @@ -135,7 +134,7 @@ int __libc_free_aligned(void *ptr)
  4345. if (ptr == NULL)
  4346. return 0;
  4347. - LOCK;
  4348. + __MALLOC_LOCK;
  4349. for (l = _aligned_blocks; l != NULL; l = l->next) {
  4350. if (l->aligned == ptr) {
  4351. /* Mark the block as free */
  4352. @@ -146,7 +145,7 @@ int __libc_free_aligned(void *ptr)
  4353. return 1;
  4354. }
  4355. }
  4356. - UNLOCK;
  4357. + __MALLOC_UNLOCK;
  4358. return 0;
  4359. }
  4360. void * memalign (size_t alignment, size_t size)
  4361. @@ -159,10 +158,10 @@ void * memalign (size_t alignment, size_
  4362. return NULL;
  4363. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  4364. - (char *) NULL)) % alignment;
  4365. + (char *) NULL)) % alignment;
  4366. if (adj != 0) {
  4367. struct alignlist *l;
  4368. - LOCK;
  4369. + __MALLOC_LOCK;
  4370. for (l = _aligned_blocks; l != NULL; l = l->next)
  4371. if (l->aligned == NULL)
  4372. /* This slot is free. Use it. */
  4373. @@ -171,15 +170,16 @@ void * memalign (size_t alignment, size_
  4374. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  4375. if (l == NULL) {
  4376. free(result);
  4377. - UNLOCK;
  4378. - return NULL;
  4379. + result = NULL;
  4380. + goto DONE;
  4381. }
  4382. l->next = _aligned_blocks;
  4383. _aligned_blocks = l;
  4384. }
  4385. l->exact = result;
  4386. result = l->aligned = (char *) result + alignment - adj;
  4387. - UNLOCK;
  4388. + DONE:
  4389. + __MALLOC_UNLOCK;
  4390. }
  4391. return result;
  4392. diff --git a/libc/stdlib/malloc-standard/calloc.c b/libc/stdlib/malloc-standard/calloc.c
  4393. index a67dad7..4277954 100644
  4394. --- a/libc/stdlib/malloc-standard/calloc.c
  4395. +++ b/libc/stdlib/malloc-standard/calloc.c
  4396. @@ -8,7 +8,7 @@
  4397. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  4398. Note: There may be an updated version of this malloc obtainable at
  4399. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  4400. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  4401. Check before installing!
  4402. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  4403. @@ -31,63 +31,63 @@ void* calloc(size_t n_elements, size_t e
  4404. * to fall through and call malloc(0) */
  4405. size = n_elements * elem_size;
  4406. if (n_elements && elem_size != (size / n_elements)) {
  4407. - __set_errno(ENOMEM);
  4408. - return NULL;
  4409. + __set_errno(ENOMEM);
  4410. + return NULL;
  4411. }
  4412. - LOCK;
  4413. + __MALLOC_LOCK;
  4414. mem = malloc(size);
  4415. if (mem != 0) {
  4416. - p = mem2chunk(mem);
  4417. + p = mem2chunk(mem);
  4418. - if (!chunk_is_mmapped(p))
  4419. - {
  4420. - /*
  4421. - Unroll clear of <= 36 bytes (72 if 8byte sizes)
  4422. - We know that contents have an odd number of
  4423. - size_t-sized words; minimally 3.
  4424. - */
  4425. -
  4426. - d = (size_t*)mem;
  4427. - clearsize = chunksize(p) - (sizeof(size_t));
  4428. - nclears = clearsize / sizeof(size_t);
  4429. - assert(nclears >= 3);
  4430. -
  4431. - if (nclears > 9)
  4432. - memset(d, 0, clearsize);
  4433. -
  4434. - else {
  4435. - *(d+0) = 0;
  4436. - *(d+1) = 0;
  4437. - *(d+2) = 0;
  4438. - if (nclears > 4) {
  4439. - *(d+3) = 0;
  4440. - *(d+4) = 0;
  4441. - if (nclears > 6) {
  4442. - *(d+5) = 0;
  4443. - *(d+6) = 0;
  4444. - if (nclears > 8) {
  4445. - *(d+7) = 0;
  4446. - *(d+8) = 0;
  4447. + if (!chunk_is_mmapped(p))
  4448. + {
  4449. + /*
  4450. + Unroll clear of <= 36 bytes (72 if 8byte sizes)
  4451. + We know that contents have an odd number of
  4452. + size_t-sized words; minimally 3.
  4453. + */
  4454. +
  4455. + d = (size_t*)mem;
  4456. + clearsize = chunksize(p) - (sizeof(size_t));
  4457. + nclears = clearsize / sizeof(size_t);
  4458. + assert(nclears >= 3);
  4459. +
  4460. + if (nclears > 9)
  4461. + memset(d, 0, clearsize);
  4462. +
  4463. + else {
  4464. + *(d+0) = 0;
  4465. + *(d+1) = 0;
  4466. + *(d+2) = 0;
  4467. + if (nclears > 4) {
  4468. + *(d+3) = 0;
  4469. + *(d+4) = 0;
  4470. + if (nclears > 6) {
  4471. + *(d+5) = 0;
  4472. + *(d+6) = 0;
  4473. + if (nclears > 8) {
  4474. + *(d+7) = 0;
  4475. + *(d+8) = 0;
  4476. + }
  4477. + }
  4478. + }
  4479. + }
  4480. }
  4481. - }
  4482. - }
  4483. - }
  4484. - }
  4485. #if 0
  4486. - else
  4487. - {
  4488. - /* Standard unix mmap using /dev/zero clears memory so calloc
  4489. - * doesn't need to actually zero anything....
  4490. - */
  4491. - d = (size_t*)mem;
  4492. - /* Note the additional (sizeof(size_t)) */
  4493. - clearsize = chunksize(p) - 2*(sizeof(size_t));
  4494. - memset(d, 0, clearsize);
  4495. - }
  4496. + else
  4497. + {
  4498. + /* Standard unix mmap using /dev/zero clears memory so calloc
  4499. + * doesn't need to actually zero anything....
  4500. + */
  4501. + d = (size_t*)mem;
  4502. + /* Note the additional (sizeof(size_t)) */
  4503. + clearsize = chunksize(p) - 2*(sizeof(size_t));
  4504. + memset(d, 0, clearsize);
  4505. + }
  4506. #endif
  4507. }
  4508. - UNLOCK;
  4509. + __MALLOC_UNLOCK;
  4510. return mem;
  4511. }
  4512. diff --git a/libc/stdlib/malloc-standard/free.c b/libc/stdlib/malloc-standard/free.c
  4513. index 94e1d65..4e08ef7 100644
  4514. --- a/libc/stdlib/malloc-standard/free.c
  4515. +++ b/libc/stdlib/malloc-standard/free.c
  4516. @@ -8,7 +8,7 @@
  4517. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  4518. Note: There may be an updated version of this malloc obtainable at
  4519. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  4520. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  4521. Check before installing!
  4522. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  4523. @@ -42,71 +42,71 @@ static int __malloc_trim(size_t pad, mst
  4524. if (extra > 0) {
  4525. - /*
  4526. - Only proceed if end of memory is where we last set it.
  4527. - This avoids problems if there were foreign sbrk calls.
  4528. - */
  4529. - current_brk = (char*)(MORECORE(0));
  4530. - if (current_brk == (char*)(av->top) + top_size) {
  4531. -
  4532. - /*
  4533. - Attempt to release memory. We ignore MORECORE return value,
  4534. - and instead call again to find out where new end of memory is.
  4535. - This avoids problems if first call releases less than we asked,
  4536. - of if failure somehow altered brk value. (We could still
  4537. - encounter problems if it altered brk in some very bad way,
  4538. - but the only thing we can do is adjust anyway, which will cause
  4539. - some downstream failure.)
  4540. - */
  4541. -
  4542. - MORECORE(-extra);
  4543. - new_brk = (char*)(MORECORE(0));
  4544. -
  4545. - if (new_brk != (char*)MORECORE_FAILURE) {
  4546. - released = (long)(current_brk - new_brk);
  4547. -
  4548. - if (released != 0) {
  4549. - /* Success. Adjust top. */
  4550. - av->sbrked_mem -= released;
  4551. - set_head(av->top, (top_size - released) | PREV_INUSE);
  4552. - check_malloc_state();
  4553. - return 1;
  4554. + /*
  4555. + Only proceed if end of memory is where we last set it.
  4556. + This avoids problems if there were foreign sbrk calls.
  4557. + */
  4558. + current_brk = (char*)(MORECORE(0));
  4559. + if (current_brk == (char*)(av->top) + top_size) {
  4560. +
  4561. + /*
  4562. + Attempt to release memory. We ignore MORECORE return value,
  4563. + and instead call again to find out where new end of memory is.
  4564. + This avoids problems if first call releases less than we asked,
  4565. + of if failure somehow altered brk value. (We could still
  4566. + encounter problems if it altered brk in some very bad way,
  4567. + but the only thing we can do is adjust anyway, which will cause
  4568. + some downstream failure.)
  4569. + */
  4570. +
  4571. + MORECORE(-extra);
  4572. + new_brk = (char*)(MORECORE(0));
  4573. +
  4574. + if (new_brk != (char*)MORECORE_FAILURE) {
  4575. + released = (long)(current_brk - new_brk);
  4576. +
  4577. + if (released != 0) {
  4578. + /* Success. Adjust top. */
  4579. + av->sbrked_mem -= released;
  4580. + set_head(av->top, (top_size - released) | PREV_INUSE);
  4581. + check_malloc_state();
  4582. + return 1;
  4583. + }
  4584. + }
  4585. }
  4586. - }
  4587. - }
  4588. }
  4589. return 0;
  4590. }
  4591. /* ------------------------- malloc_trim -------------------------
  4592. - malloc_trim(size_t pad);
  4593. + malloc_trim(size_t pad);
  4594. - If possible, gives memory back to the system (via negative
  4595. - arguments to sbrk) if there is unused memory at the `high' end of
  4596. - the malloc pool. You can call this after freeing large blocks of
  4597. - memory to potentially reduce the system-level memory requirements
  4598. - of a program. However, it cannot guarantee to reduce memory. Under
  4599. - some allocation patterns, some large free blocks of memory will be
  4600. - locked between two used chunks, so they cannot be given back to
  4601. - the system.
  4602. -
  4603. - The `pad' argument to malloc_trim represents the amount of free
  4604. - trailing space to leave untrimmed. If this argument is zero,
  4605. - only the minimum amount of memory to maintain internal data
  4606. - structures will be left (one page or less). Non-zero arguments
  4607. - can be supplied to maintain enough trailing space to service
  4608. - future expected allocations without having to re-obtain memory
  4609. - from the system.
  4610. -
  4611. - Malloc_trim returns 1 if it actually released any memory, else 0.
  4612. - On systems that do not support "negative sbrks", it will always
  4613. - return 0.
  4614. + If possible, gives memory back to the system (via negative
  4615. + arguments to sbrk) if there is unused memory at the `high' end of
  4616. + the malloc pool. You can call this after freeing large blocks of
  4617. + memory to potentially reduce the system-level memory requirements
  4618. + of a program. However, it cannot guarantee to reduce memory. Under
  4619. + some allocation patterns, some large free blocks of memory will be
  4620. + locked between two used chunks, so they cannot be given back to
  4621. + the system.
  4622. +
  4623. + The `pad' argument to malloc_trim represents the amount of free
  4624. + trailing space to leave untrimmed. If this argument is zero,
  4625. + only the minimum amount of memory to maintain internal data
  4626. + structures will be left (one page or less). Non-zero arguments
  4627. + can be supplied to maintain enough trailing space to service
  4628. + future expected allocations without having to re-obtain memory
  4629. + from the system.
  4630. +
  4631. + Malloc_trim returns 1 if it actually released any memory, else 0.
  4632. + On systems that do not support "negative sbrks", it will always
  4633. + return 0.
  4634. */
  4635. int malloc_trim(size_t pad)
  4636. {
  4637. - mstate av = get_malloc_state();
  4638. - __malloc_consolidate(av);
  4639. - return __malloc_trim(pad, av);
  4640. + mstate av = get_malloc_state();
  4641. + __malloc_consolidate(av);
  4642. + return __malloc_trim(pad, av);
  4643. }
  4644. /*
  4645. @@ -125,8 +125,8 @@ static void malloc_init_state(mstate av)
  4646. /* Establish circular links for normal bins */
  4647. for (i = 1; i < NBINS; ++i) {
  4648. - bin = bin_at(av,i);
  4649. - bin->fd = bin->bk = bin;
  4650. + bin = bin_at(av,i);
  4651. + bin->fd = bin->bk = bin;
  4652. }
  4653. av->top_pad = DEFAULT_TOP_PAD;
  4654. @@ -157,15 +157,15 @@ static void malloc_init_state(mstate av)
  4655. /* ------------------------- __malloc_consolidate -------------------------
  4656. - __malloc_consolidate is a specialized version of free() that tears
  4657. - down chunks held in fastbins. Free itself cannot be used for this
  4658. - purpose since, among other things, it might place chunks back onto
  4659. - fastbins. So, instead, we need to use a minor variant of the same
  4660. - code.
  4661. -
  4662. - Also, because this routine needs to be called the first time through
  4663. - malloc anyway, it turns out to be the perfect place to trigger
  4664. - initialization code.
  4665. +__malloc_consolidate is a specialized version of free() that tears
  4666. +down chunks held in fastbins. Free itself cannot be used for this
  4667. +purpose since, among other things, it might place chunks back onto
  4668. +fastbins. So, instead, we need to use a minor variant of the same
  4669. +code.
  4670. +
  4671. +Also, because this routine needs to be called the first time through
  4672. +malloc anyway, it turns out to be the perfect place to trigger
  4673. +initialization code.
  4674. */
  4675. void __malloc_consolidate(mstate av)
  4676. {
  4677. @@ -186,78 +186,78 @@ void __malloc_consolidate(mstate av)
  4678. mchunkptr fwd;
  4679. /*
  4680. - If max_fast is 0, we know that av hasn't
  4681. - yet been initialized, in which case do so below
  4682. - */
  4683. + If max_fast is 0, we know that av hasn't
  4684. + yet been initialized, in which case do so below
  4685. + */
  4686. if (av->max_fast != 0) {
  4687. - clear_fastchunks(av);
  4688. + clear_fastchunks(av);
  4689. - unsorted_bin = unsorted_chunks(av);
  4690. + unsorted_bin = unsorted_chunks(av);
  4691. - /*
  4692. - Remove each chunk from fast bin and consolidate it, placing it
  4693. - then in unsorted bin. Among other reasons for doing this,
  4694. - placing in unsorted bin avoids needing to calculate actual bins
  4695. - until malloc is sure that chunks aren't immediately going to be
  4696. - reused anyway.
  4697. - */
  4698. -
  4699. - maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
  4700. - fb = &(av->fastbins[0]);
  4701. - do {
  4702. - if ( (p = *fb) != 0) {
  4703. - *fb = 0;
  4704. + /*
  4705. + Remove each chunk from fast bin and consolidate it, placing it
  4706. + then in unsorted bin. Among other reasons for doing this,
  4707. + placing in unsorted bin avoids needing to calculate actual bins
  4708. + until malloc is sure that chunks aren't immediately going to be
  4709. + reused anyway.
  4710. + */
  4711. + maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
  4712. + fb = &(av->fastbins[0]);
  4713. do {
  4714. - check_inuse_chunk(p);
  4715. - nextp = p->fd;
  4716. + if ( (p = *fb) != 0) {
  4717. + *fb = 0;
  4718. - /* Slightly streamlined version of consolidation code in free() */
  4719. - size = p->size & ~PREV_INUSE;
  4720. - nextchunk = chunk_at_offset(p, size);
  4721. - nextsize = chunksize(nextchunk);
  4722. + do {
  4723. + check_inuse_chunk(p);
  4724. + nextp = p->fd;
  4725. +
  4726. + /* Slightly streamlined version of consolidation code in free() */
  4727. + size = p->size & ~PREV_INUSE;
  4728. + nextchunk = chunk_at_offset(p, size);
  4729. + nextsize = chunksize(nextchunk);
  4730. +
  4731. + if (!prev_inuse(p)) {
  4732. + prevsize = p->prev_size;
  4733. + size += prevsize;
  4734. + p = chunk_at_offset(p, -((long) prevsize));
  4735. + unlink(p, bck, fwd);
  4736. + }
  4737. +
  4738. + if (nextchunk != av->top) {
  4739. + nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
  4740. + set_head(nextchunk, nextsize);
  4741. +
  4742. + if (!nextinuse) {
  4743. + size += nextsize;
  4744. + unlink(nextchunk, bck, fwd);
  4745. + }
  4746. +
  4747. + first_unsorted = unsorted_bin->fd;
  4748. + unsorted_bin->fd = p;
  4749. + first_unsorted->bk = p;
  4750. +
  4751. + set_head(p, size | PREV_INUSE);
  4752. + p->bk = unsorted_bin;
  4753. + p->fd = first_unsorted;
  4754. + set_foot(p, size);
  4755. + }
  4756. +
  4757. + else {
  4758. + size += nextsize;
  4759. + set_head(p, size | PREV_INUSE);
  4760. + av->top = p;
  4761. + }
  4762. - if (!prev_inuse(p)) {
  4763. - prevsize = p->prev_size;
  4764. - size += prevsize;
  4765. - p = chunk_at_offset(p, -((long) prevsize));
  4766. - unlink(p, bck, fwd);
  4767. - }
  4768. + } while ( (p = nextp) != 0);
  4769. - if (nextchunk != av->top) {
  4770. - nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
  4771. - set_head(nextchunk, nextsize);
  4772. -
  4773. - if (!nextinuse) {
  4774. - size += nextsize;
  4775. - unlink(nextchunk, bck, fwd);
  4776. }
  4777. -
  4778. - first_unsorted = unsorted_bin->fd;
  4779. - unsorted_bin->fd = p;
  4780. - first_unsorted->bk = p;
  4781. -
  4782. - set_head(p, size | PREV_INUSE);
  4783. - p->bk = unsorted_bin;
  4784. - p->fd = first_unsorted;
  4785. - set_foot(p, size);
  4786. - }
  4787. -
  4788. - else {
  4789. - size += nextsize;
  4790. - set_head(p, size | PREV_INUSE);
  4791. - av->top = p;
  4792. - }
  4793. -
  4794. - } while ( (p = nextp) != 0);
  4795. -
  4796. - }
  4797. - } while (fb++ != maxfb);
  4798. + } while (fb++ != maxfb);
  4799. }
  4800. else {
  4801. - malloc_init_state(av);
  4802. - check_malloc_state();
  4803. + malloc_init_state(av);
  4804. + check_malloc_state();
  4805. }
  4806. }
  4807. @@ -279,9 +279,9 @@ void free(void* mem)
  4808. /* free(0) has no effect */
  4809. if (mem == NULL)
  4810. - return;
  4811. + return;
  4812. - LOCK;
  4813. + __MALLOC_LOCK;
  4814. av = get_malloc_state();
  4815. p = mem2chunk(mem);
  4816. size = chunksize(p);
  4817. @@ -289,9 +289,9 @@ void free(void* mem)
  4818. check_inuse_chunk(p);
  4819. /*
  4820. - If eligible, place chunk on a fastbin so it can be found
  4821. - and used quickly in malloc.
  4822. - */
  4823. + If eligible, place chunk on a fastbin so it can be found
  4824. + and used quickly in malloc.
  4825. + */
  4826. if ((unsigned long)(size) <= (unsigned long)(av->max_fast)
  4827. @@ -300,114 +300,114 @@ void free(void* mem)
  4828. bordering top into fastbins */
  4829. && (chunk_at_offset(p, size) != av->top)
  4830. #endif
  4831. - ) {
  4832. + ) {
  4833. - set_fastchunks(av);
  4834. - fb = &(av->fastbins[fastbin_index(size)]);
  4835. - p->fd = *fb;
  4836. - *fb = p;
  4837. + set_fastchunks(av);
  4838. + fb = &(av->fastbins[fastbin_index(size)]);
  4839. + p->fd = *fb;
  4840. + *fb = p;
  4841. }
  4842. /*
  4843. - Consolidate other non-mmapped chunks as they arrive.
  4844. - */
  4845. + Consolidate other non-mmapped chunks as they arrive.
  4846. + */
  4847. else if (!chunk_is_mmapped(p)) {
  4848. - set_anychunks(av);
  4849. + set_anychunks(av);
  4850. +
  4851. + nextchunk = chunk_at_offset(p, size);
  4852. + nextsize = chunksize(nextchunk);
  4853. +
  4854. + /* consolidate backward */
  4855. + if (!prev_inuse(p)) {
  4856. + prevsize = p->prev_size;
  4857. + size += prevsize;
  4858. + p = chunk_at_offset(p, -((long) prevsize));
  4859. + unlink(p, bck, fwd);
  4860. + }
  4861. +
  4862. + if (nextchunk != av->top) {
  4863. + /* get and clear inuse bit */
  4864. + nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
  4865. + set_head(nextchunk, nextsize);
  4866. +
  4867. + /* consolidate forward */
  4868. + if (!nextinuse) {
  4869. + unlink(nextchunk, bck, fwd);
  4870. + size += nextsize;
  4871. + }
  4872. +
  4873. + /*
  4874. + Place the chunk in unsorted chunk list. Chunks are
  4875. + not placed into regular bins until after they have
  4876. + been given one chance to be used in malloc.
  4877. + */
  4878. +
  4879. + bck = unsorted_chunks(av);
  4880. + fwd = bck->fd;
  4881. + p->bk = bck;
  4882. + p->fd = fwd;
  4883. + bck->fd = p;
  4884. + fwd->bk = p;
  4885. - nextchunk = chunk_at_offset(p, size);
  4886. - nextsize = chunksize(nextchunk);
  4887. + set_head(p, size | PREV_INUSE);
  4888. + set_foot(p, size);
  4889. +
  4890. + check_free_chunk(p);
  4891. + }
  4892. +
  4893. + /*
  4894. + If the chunk borders the current high end of memory,
  4895. + consolidate into top
  4896. + */
  4897. - /* consolidate backward */
  4898. - if (!prev_inuse(p)) {
  4899. - prevsize = p->prev_size;
  4900. - size += prevsize;
  4901. - p = chunk_at_offset(p, -((long) prevsize));
  4902. - unlink(p, bck, fwd);
  4903. - }
  4904. -
  4905. - if (nextchunk != av->top) {
  4906. - /* get and clear inuse bit */
  4907. - nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
  4908. - set_head(nextchunk, nextsize);
  4909. -
  4910. - /* consolidate forward */
  4911. - if (!nextinuse) {
  4912. - unlink(nextchunk, bck, fwd);
  4913. - size += nextsize;
  4914. - }
  4915. -
  4916. - /*
  4917. - Place the chunk in unsorted chunk list. Chunks are
  4918. - not placed into regular bins until after they have
  4919. - been given one chance to be used in malloc.
  4920. - */
  4921. -
  4922. - bck = unsorted_chunks(av);
  4923. - fwd = bck->fd;
  4924. - p->bk = bck;
  4925. - p->fd = fwd;
  4926. - bck->fd = p;
  4927. - fwd->bk = p;
  4928. -
  4929. - set_head(p, size | PREV_INUSE);
  4930. - set_foot(p, size);
  4931. -
  4932. - check_free_chunk(p);
  4933. - }
  4934. -
  4935. - /*
  4936. - If the chunk borders the current high end of memory,
  4937. - consolidate into top
  4938. - */
  4939. -
  4940. - else {
  4941. - size += nextsize;
  4942. - set_head(p, size | PREV_INUSE);
  4943. - av->top = p;
  4944. - check_chunk(p);
  4945. - }
  4946. -
  4947. - /*
  4948. - If freeing a large space, consolidate possibly-surrounding
  4949. - chunks. Then, if the total unused topmost memory exceeds trim
  4950. - threshold, ask malloc_trim to reduce top.
  4951. -
  4952. - Unless max_fast is 0, we don't know if there are fastbins
  4953. - bordering top, so we cannot tell for sure whether threshold
  4954. - has been reached unless fastbins are consolidated. But we
  4955. - don't want to consolidate on each free. As a compromise,
  4956. - consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
  4957. - is reached.
  4958. - */
  4959. -
  4960. - if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
  4961. - if (have_fastchunks(av))
  4962. - __malloc_consolidate(av);
  4963. -
  4964. - if ((unsigned long)(chunksize(av->top)) >=
  4965. - (unsigned long)(av->trim_threshold))
  4966. - __malloc_trim(av->top_pad, av);
  4967. - }
  4968. + else {
  4969. + size += nextsize;
  4970. + set_head(p, size | PREV_INUSE);
  4971. + av->top = p;
  4972. + check_chunk(p);
  4973. + }
  4974. +
  4975. + /*
  4976. + If freeing a large space, consolidate possibly-surrounding
  4977. + chunks. Then, if the total unused topmost memory exceeds trim
  4978. + threshold, ask malloc_trim to reduce top.
  4979. +
  4980. + Unless max_fast is 0, we don't know if there are fastbins
  4981. + bordering top, so we cannot tell for sure whether threshold
  4982. + has been reached unless fastbins are consolidated. But we
  4983. + don't want to consolidate on each free. As a compromise,
  4984. + consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
  4985. + is reached.
  4986. + */
  4987. +
  4988. + if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
  4989. + if (have_fastchunks(av))
  4990. + __malloc_consolidate(av);
  4991. +
  4992. + if ((unsigned long)(chunksize(av->top)) >=
  4993. + (unsigned long)(av->trim_threshold))
  4994. + __malloc_trim(av->top_pad, av);
  4995. + }
  4996. }
  4997. /*
  4998. - If the chunk was allocated via mmap, release via munmap()
  4999. - Note that if HAVE_MMAP is false but chunk_is_mmapped is
  5000. - true, then user must have overwritten memory. There's nothing
  5001. - we can do to catch this error unless DEBUG is set, in which case
  5002. - check_inuse_chunk (above) will have triggered error.
  5003. - */
  5004. + If the chunk was allocated via mmap, release via munmap()
  5005. + Note that if HAVE_MMAP is false but chunk_is_mmapped is
  5006. + true, then user must have overwritten memory. There's nothing
  5007. + we can do to catch this error unless DEBUG is set, in which case
  5008. + check_inuse_chunk (above) will have triggered error.
  5009. + */
  5010. else {
  5011. - int ret;
  5012. - size_t offset = p->prev_size;
  5013. - av->n_mmaps--;
  5014. - av->mmapped_mem -= (size + offset);
  5015. - ret = munmap((char*)p - offset, size + offset);
  5016. - /* munmap returns non-zero on failure */
  5017. - assert(ret == 0);
  5018. + int ret;
  5019. + size_t offset = p->prev_size;
  5020. + av->n_mmaps--;
  5021. + av->mmapped_mem -= (size + offset);
  5022. + ret = munmap((char*)p - offset, size + offset);
  5023. + /* munmap returns non-zero on failure */
  5024. + assert(ret == 0);
  5025. }
  5026. - UNLOCK;
  5027. + __MALLOC_UNLOCK;
  5028. }
  5029. diff --git a/libc/stdlib/malloc-standard/mallinfo.c b/libc/stdlib/malloc-standard/mallinfo.c
  5030. index 51ac423..1e0875c 100644
  5031. --- a/libc/stdlib/malloc-standard/mallinfo.c
  5032. +++ b/libc/stdlib/malloc-standard/mallinfo.c
  5033. @@ -8,7 +8,7 @@
  5034. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  5035. Note: There may be an updated version of this malloc obtainable at
  5036. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  5037. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  5038. Check before installing!
  5039. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  5040. @@ -30,11 +30,11 @@ struct mallinfo mallinfo(void)
  5041. int nblocks;
  5042. int nfastblocks;
  5043. - LOCK;
  5044. + __MALLOC_LOCK;
  5045. av = get_malloc_state();
  5046. /* Ensure initialization */
  5047. if (av->top == 0) {
  5048. - __malloc_consolidate(av);
  5049. + __malloc_consolidate(av);
  5050. }
  5051. check_malloc_state();
  5052. @@ -48,21 +48,21 @@ struct mallinfo mallinfo(void)
  5053. fastavail = 0;
  5054. for (i = 0; i < NFASTBINS; ++i) {
  5055. - for (p = av->fastbins[i]; p != 0; p = p->fd) {
  5056. - ++nfastblocks;
  5057. - fastavail += chunksize(p);
  5058. - }
  5059. + for (p = av->fastbins[i]; p != 0; p = p->fd) {
  5060. + ++nfastblocks;
  5061. + fastavail += chunksize(p);
  5062. + }
  5063. }
  5064. avail += fastavail;
  5065. /* traverse regular bins */
  5066. for (i = 1; i < NBINS; ++i) {
  5067. - b = bin_at(av, i);
  5068. - for (p = last(b); p != b; p = p->bk) {
  5069. - ++nblocks;
  5070. - avail += chunksize(p);
  5071. - }
  5072. + b = bin_at(av, i);
  5073. + for (p = last(b); p != b; p = p->bk) {
  5074. + ++nblocks;
  5075. + avail += chunksize(p);
  5076. + }
  5077. }
  5078. mi.smblks = nfastblocks;
  5079. @@ -75,7 +75,7 @@ struct mallinfo mallinfo(void)
  5080. mi.fsmblks = fastavail;
  5081. mi.keepcost = chunksize(av->top);
  5082. mi.usmblks = av->max_total_mem;
  5083. - UNLOCK;
  5084. + __MALLOC_UNLOCK;
  5085. return mi;
  5086. }
  5087. @@ -84,23 +84,40 @@ void malloc_stats(FILE *file)
  5088. struct mallinfo mi;
  5089. if (file==NULL) {
  5090. - file = stderr;
  5091. + file = stderr;
  5092. }
  5093. mi = mallinfo();
  5094. - fprintf(file, "total bytes allocated = %10u\n", (unsigned int)(mi.arena + mi.hblkhd));
  5095. - fprintf(file, "total bytes in use bytes = %10u\n", (unsigned int)(mi.uordblks + mi.hblkhd));
  5096. - fprintf(file, "total non-mmapped bytes allocated = %10d\n", mi.arena);
  5097. - fprintf(file, "number of mmapped regions = %10d\n", mi.hblks);
  5098. - fprintf(file, "total allocated mmap space = %10d\n", mi.hblkhd);
  5099. - fprintf(file, "total allocated sbrk space = %10d\n", mi.uordblks);
  5100. + fprintf(file,
  5101. + "total bytes allocated = %10u\n"
  5102. + "total bytes in use bytes = %10u\n"
  5103. + "total non-mmapped bytes allocated = %10d\n"
  5104. + "number of mmapped regions = %10d\n"
  5105. + "total allocated mmap space = %10d\n"
  5106. + "total allocated sbrk space = %10d\n"
  5107. #if 0
  5108. - fprintf(file, "number of free chunks = %10d\n", mi.ordblks);
  5109. - fprintf(file, "number of fastbin blocks = %10d\n", mi.smblks);
  5110. - fprintf(file, "space in freed fastbin blocks = %10d\n", mi.fsmblks);
  5111. + "number of free chunks = %10d\n"
  5112. + "number of fastbin blocks = %10d\n"
  5113. + "space in freed fastbin blocks = %10d\n"
  5114. #endif
  5115. - fprintf(file, "maximum total allocated space = %10d\n", mi.usmblks);
  5116. - fprintf(file, "total free space = %10d\n", mi.fordblks);
  5117. - fprintf(file, "memory releasable via malloc_trim = %10d\n", mi.keepcost);
  5118. + "maximum total allocated space = %10d\n"
  5119. + "total free space = %10d\n"
  5120. + "memory releasable via malloc_trim = %10d\n",
  5121. +
  5122. + (unsigned int)(mi.arena + mi.hblkhd),
  5123. + (unsigned int)(mi.uordblks + mi.hblkhd),
  5124. + mi.arena,
  5125. + mi.hblks,
  5126. + mi.hblkhd,
  5127. + mi.uordblks,
  5128. +#if 0
  5129. + mi.ordblks,
  5130. + mi.smblks,
  5131. + mi.fsmblks,
  5132. +#endif
  5133. + mi.usmblks,
  5134. + mi.fordblks,
  5135. + mi.keepcost
  5136. + );
  5137. }
  5138. diff --git a/libc/stdlib/malloc-standard/malloc.c b/libc/stdlib/malloc-standard/malloc.c
  5139. index 7025e83..60494a0 100644
  5140. --- a/libc/stdlib/malloc-standard/malloc.c
  5141. +++ b/libc/stdlib/malloc-standard/malloc.c
  5142. @@ -8,7 +8,7 @@
  5143. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  5144. Note: There may be an updated version of this malloc obtainable at
  5145. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  5146. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  5147. Check before installing!
  5148. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  5149. @@ -17,17 +17,14 @@
  5150. #define _GNU_SOURCE
  5151. #include "malloc.h"
  5152. -
  5153. -#ifdef __UCLIBC_HAS_THREADS__
  5154. -pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  5155. -#endif
  5156. +__UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  5157. /*
  5158. - There is exactly one instance of this struct in this malloc.
  5159. - If you are adapting this malloc in a way that does NOT use a static
  5160. - malloc_state, you MUST explicitly zero-fill it before using. This
  5161. - malloc relies on the property that malloc_state is initialized to
  5162. - all zeroes (as is true of C statics).
  5163. + There is exactly one instance of this struct in this malloc.
  5164. + If you are adapting this malloc in a way that does NOT use a static
  5165. + malloc_state, you MUST explicitly zero-fill it before using. This
  5166. + malloc relies on the property that malloc_state is initialized to
  5167. + all zeroes (as is true of C statics).
  5168. */
  5169. struct malloc_state __malloc_state; /* never directly referenced */
  5170. @@ -77,30 +74,30 @@ void __do_check_chunk(mchunkptr p)
  5171. if (!chunk_is_mmapped(p)) {
  5172. - /* Has legal address ... */
  5173. - if (p != av->top) {
  5174. - if (contiguous(av)) {
  5175. - assert(((char*)p) >= min_address);
  5176. - assert(((char*)p + sz) <= ((char*)(av->top)));
  5177. - }
  5178. - }
  5179. - else {
  5180. - /* top size is always at least MINSIZE */
  5181. - assert((unsigned long)(sz) >= MINSIZE);
  5182. - /* top predecessor always marked inuse */
  5183. - assert(prev_inuse(p));
  5184. - }
  5185. + /* Has legal address ... */
  5186. + if (p != av->top) {
  5187. + if (contiguous(av)) {
  5188. + assert(((char*)p) >= min_address);
  5189. + assert(((char*)p + sz) <= ((char*)(av->top)));
  5190. + }
  5191. + }
  5192. + else {
  5193. + /* top size is always at least MINSIZE */
  5194. + assert((unsigned long)(sz) >= MINSIZE);
  5195. + /* top predecessor always marked inuse */
  5196. + assert(prev_inuse(p));
  5197. + }
  5198. }
  5199. else {
  5200. - /* address is outside main heap */
  5201. - if (contiguous(av) && av->top != initial_top(av)) {
  5202. - assert(((char*)p) < min_address || ((char*)p) > max_address);
  5203. - }
  5204. - /* chunk is page-aligned */
  5205. - assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
  5206. - /* mem is aligned */
  5207. - assert(aligned_OK(chunk2mem(p)));
  5208. + /* address is outside main heap */
  5209. + if (contiguous(av) && av->top != initial_top(av)) {
  5210. + assert(((char*)p) < min_address || ((char*)p) > max_address);
  5211. + }
  5212. + /* chunk is page-aligned */
  5213. + assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
  5214. + /* mem is aligned */
  5215. + assert(aligned_OK(chunk2mem(p)));
  5216. }
  5217. }
  5218. @@ -121,21 +118,21 @@ void __do_check_free_chunk(mchunkptr p)
  5219. /* Unless a special marker, must have OK fields */
  5220. if ((unsigned long)(sz) >= MINSIZE)
  5221. - {
  5222. - assert((sz & MALLOC_ALIGN_MASK) == 0);
  5223. - assert(aligned_OK(chunk2mem(p)));
  5224. - /* ... matching footer field */
  5225. - assert(next->prev_size == sz);
  5226. - /* ... and is fully consolidated */
  5227. - assert(prev_inuse(p));
  5228. - assert (next == av->top || inuse(next));
  5229. -
  5230. - /* ... and has minimally sane links */
  5231. - assert(p->fd->bk == p);
  5232. - assert(p->bk->fd == p);
  5233. - }
  5234. + {
  5235. + assert((sz & MALLOC_ALIGN_MASK) == 0);
  5236. + assert(aligned_OK(chunk2mem(p)));
  5237. + /* ... matching footer field */
  5238. + assert(next->prev_size == sz);
  5239. + /* ... and is fully consolidated */
  5240. + assert(prev_inuse(p));
  5241. + assert (next == av->top || inuse(next));
  5242. +
  5243. + /* ... and has minimally sane links */
  5244. + assert(p->fd->bk == p);
  5245. + assert(p->bk->fd == p);
  5246. + }
  5247. else /* markers are always of size (sizeof(size_t)) */
  5248. - assert(sz == (sizeof(size_t)));
  5249. + assert(sz == (sizeof(size_t)));
  5250. }
  5251. /* Properties of inuse chunks */
  5252. @@ -146,7 +143,7 @@ void __do_check_inuse_chunk(mchunkptr p)
  5253. __do_check_chunk(p);
  5254. if (chunk_is_mmapped(p))
  5255. - return; /* mmapped chunks have no next/prev */
  5256. + return; /* mmapped chunks have no next/prev */
  5257. /* Check whether it claims to be in use ... */
  5258. assert(inuse(p));
  5259. @@ -156,20 +153,20 @@ void __do_check_inuse_chunk(mchunkptr p)
  5260. /* ... and is surrounded by OK chunks.
  5261. Since more things can be checked with free chunks than inuse ones,
  5262. if an inuse chunk borders them and debug is on, it's worth doing them.
  5263. - */
  5264. + */
  5265. if (!prev_inuse(p)) {
  5266. - /* Note that we cannot even look at prev unless it is not inuse */
  5267. - mchunkptr prv = prev_chunk(p);
  5268. - assert(next_chunk(prv) == p);
  5269. - __do_check_free_chunk(prv);
  5270. + /* Note that we cannot even look at prev unless it is not inuse */
  5271. + mchunkptr prv = prev_chunk(p);
  5272. + assert(next_chunk(prv) == p);
  5273. + __do_check_free_chunk(prv);
  5274. }
  5275. if (next == av->top) {
  5276. - assert(prev_inuse(next));
  5277. - assert(chunksize(next) >= MINSIZE);
  5278. + assert(prev_inuse(next));
  5279. + assert(chunksize(next) >= MINSIZE);
  5280. }
  5281. else if (!inuse(next))
  5282. - __do_check_free_chunk(next);
  5283. + __do_check_free_chunk(next);
  5284. }
  5285. /* Properties of chunks recycled from fastbins */
  5286. @@ -198,14 +195,14 @@ void __do_check_malloced_chunk(mchunkptr
  5287. __do_check_remalloced_chunk(p, s);
  5288. /*
  5289. - ... plus, must obey implementation invariant that prev_inuse is
  5290. - always true of any allocated chunk; i.e., that each allocated
  5291. - chunk borders either a previously allocated and still in-use
  5292. - chunk, or the base of its memory arena. This is ensured
  5293. - by making all allocations from the the `lowest' part of any found
  5294. - chunk. This does not necessarily hold however for chunks
  5295. - recycled via fastbins.
  5296. - */
  5297. + ... plus, must obey implementation invariant that prev_inuse is
  5298. + always true of any allocated chunk; i.e., that each allocated
  5299. + chunk borders either a previously allocated and still in-use
  5300. + chunk, or the base of its memory arena. This is ensured
  5301. + by making all allocations from the the `lowest' part of any found
  5302. + chunk. This does not necessarily hold however for chunks
  5303. + recycled via fastbins.
  5304. + */
  5305. assert(prev_inuse(p));
  5306. }
  5307. @@ -243,7 +240,7 @@ void __do_check_malloc_state(void)
  5308. /* cannot run remaining checks until fully initialized */
  5309. if (av->top == 0 || av->top == initial_top(av))
  5310. - return;
  5311. + return;
  5312. /* pagesize is a power of 2 */
  5313. assert((av->pagesize & (av->pagesize-1)) == 0);
  5314. @@ -256,64 +253,64 @@ void __do_check_malloc_state(void)
  5315. max_fast_bin = fastbin_index(av->max_fast);
  5316. for (i = 0; i < NFASTBINS; ++i) {
  5317. - p = av->fastbins[i];
  5318. + p = av->fastbins[i];
  5319. - /* all bins past max_fast are empty */
  5320. - if (i > max_fast_bin)
  5321. - assert(p == 0);
  5322. -
  5323. - while (p != 0) {
  5324. - /* each chunk claims to be inuse */
  5325. - __do_check_inuse_chunk(p);
  5326. - total += chunksize(p);
  5327. - /* chunk belongs in this bin */
  5328. - assert(fastbin_index(chunksize(p)) == i);
  5329. - p = p->fd;
  5330. - }
  5331. + /* all bins past max_fast are empty */
  5332. + if (i > max_fast_bin)
  5333. + assert(p == 0);
  5334. +
  5335. + while (p != 0) {
  5336. + /* each chunk claims to be inuse */
  5337. + __do_check_inuse_chunk(p);
  5338. + total += chunksize(p);
  5339. + /* chunk belongs in this bin */
  5340. + assert(fastbin_index(chunksize(p)) == i);
  5341. + p = p->fd;
  5342. + }
  5343. }
  5344. if (total != 0)
  5345. - assert(have_fastchunks(av));
  5346. + assert(have_fastchunks(av));
  5347. else if (!have_fastchunks(av))
  5348. - assert(total == 0);
  5349. + assert(total == 0);
  5350. /* check normal bins */
  5351. for (i = 1; i < NBINS; ++i) {
  5352. - b = bin_at(av,i);
  5353. + b = bin_at(av,i);
  5354. - /* binmap is accurate (except for bin 1 == unsorted_chunks) */
  5355. - if (i >= 2) {
  5356. - binbit = get_binmap(av,i);
  5357. - empty = last(b) == b;
  5358. - if (!binbit)
  5359. - assert(empty);
  5360. - else if (!empty)
  5361. - assert(binbit);
  5362. - }
  5363. -
  5364. - for (p = last(b); p != b; p = p->bk) {
  5365. - /* each chunk claims to be free */
  5366. - __do_check_free_chunk(p);
  5367. - size = chunksize(p);
  5368. - total += size;
  5369. - if (i >= 2) {
  5370. - /* chunk belongs in bin */
  5371. - idx = bin_index(size);
  5372. - assert(idx == i);
  5373. - /* lists are sorted */
  5374. - if ((unsigned long) size >= (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
  5375. - assert(p->bk == b ||
  5376. - (unsigned long)chunksize(p->bk) >=
  5377. - (unsigned long)chunksize(p));
  5378. - }
  5379. - }
  5380. - /* chunk is followed by a legal chain of inuse chunks */
  5381. - for (q = next_chunk(p);
  5382. - (q != av->top && inuse(q) &&
  5383. - (unsigned long)(chunksize(q)) >= MINSIZE);
  5384. - q = next_chunk(q))
  5385. - __do_check_inuse_chunk(q);
  5386. - }
  5387. + /* binmap is accurate (except for bin 1 == unsorted_chunks) */
  5388. + if (i >= 2) {
  5389. + binbit = get_binmap(av,i);
  5390. + empty = last(b) == b;
  5391. + if (!binbit)
  5392. + assert(empty);
  5393. + else if (!empty)
  5394. + assert(binbit);
  5395. + }
  5396. +
  5397. + for (p = last(b); p != b; p = p->bk) {
  5398. + /* each chunk claims to be free */
  5399. + __do_check_free_chunk(p);
  5400. + size = chunksize(p);
  5401. + total += size;
  5402. + if (i >= 2) {
  5403. + /* chunk belongs in bin */
  5404. + idx = bin_index(size);
  5405. + assert(idx == i);
  5406. + /* lists are sorted */
  5407. + if ((unsigned long) size >= (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
  5408. + assert(p->bk == b ||
  5409. + (unsigned long)chunksize(p->bk) >=
  5410. + (unsigned long)chunksize(p));
  5411. + }
  5412. + }
  5413. + /* chunk is followed by a legal chain of inuse chunks */
  5414. + for (q = next_chunk(p);
  5415. + (q != av->top && inuse(q) &&
  5416. + (unsigned long)(chunksize(q)) >= MINSIZE);
  5417. + q = next_chunk(q))
  5418. + __do_check_inuse_chunk(q);
  5419. + }
  5420. }
  5421. /* top chunk is OK */
  5422. @@ -326,13 +323,13 @@ void __do_check_malloc_state(void)
  5423. assert(av->n_mmaps <= av->max_n_mmaps);
  5424. assert((unsigned long)(av->sbrked_mem) <=
  5425. - (unsigned long)(av->max_sbrked_mem));
  5426. + (unsigned long)(av->max_sbrked_mem));
  5427. assert((unsigned long)(av->mmapped_mem) <=
  5428. - (unsigned long)(av->max_mmapped_mem));
  5429. + (unsigned long)(av->max_mmapped_mem));
  5430. assert((unsigned long)(av->max_total_mem) >=
  5431. - (unsigned long)(av->mmapped_mem) + (unsigned long)(av->sbrked_mem));
  5432. + (unsigned long)(av->mmapped_mem) + (unsigned long)(av->sbrked_mem));
  5433. }
  5434. #endif
  5435. @@ -370,84 +367,84 @@ static void* __malloc_alloc(size_t nb, m
  5436. size_t pagemask = av->pagesize - 1;
  5437. /*
  5438. - If there is space available in fastbins, consolidate and retry
  5439. - malloc from scratch rather than getting memory from system. This
  5440. - can occur only if nb is in smallbin range so we didn't consolidate
  5441. - upon entry to malloc. It is much easier to handle this case here
  5442. - than in malloc proper.
  5443. - */
  5444. + If there is space available in fastbins, consolidate and retry
  5445. + malloc from scratch rather than getting memory from system. This
  5446. + can occur only if nb is in smallbin range so we didn't consolidate
  5447. + upon entry to malloc. It is much easier to handle this case here
  5448. + than in malloc proper.
  5449. + */
  5450. if (have_fastchunks(av)) {
  5451. - assert(in_smallbin_range(nb));
  5452. - __malloc_consolidate(av);
  5453. - return malloc(nb - MALLOC_ALIGN_MASK);
  5454. + assert(in_smallbin_range(nb));
  5455. + __malloc_consolidate(av);
  5456. + return malloc(nb - MALLOC_ALIGN_MASK);
  5457. }
  5458. /*
  5459. - If have mmap, and the request size meets the mmap threshold, and
  5460. - the system supports mmap, and there are few enough currently
  5461. - allocated mmapped regions, try to directly map this request
  5462. - rather than expanding top.
  5463. - */
  5464. + If have mmap, and the request size meets the mmap threshold, and
  5465. + the system supports mmap, and there are few enough currently
  5466. + allocated mmapped regions, try to directly map this request
  5467. + rather than expanding top.
  5468. + */
  5469. if ((unsigned long)(nb) >= (unsigned long)(av->mmap_threshold) &&
  5470. (av->n_mmaps < av->n_mmaps_max)) {
  5471. - char* mm; /* return value from mmap call*/
  5472. -
  5473. - /*
  5474. - Round up size to nearest page. For mmapped chunks, the overhead
  5475. - is one (sizeof(size_t)) unit larger than for normal chunks, because there
  5476. - is no following chunk whose prev_size field could be used.
  5477. - */
  5478. - size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
  5479. -
  5480. - /* Don't try if size wraps around 0 */
  5481. - if ((unsigned long)(size) > (unsigned long)(nb)) {
  5482. -
  5483. - mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
  5484. -
  5485. - if (mm != (char*)(MORECORE_FAILURE)) {
  5486. + char* mm; /* return value from mmap call*/
  5487. /*
  5488. - The offset to the start of the mmapped region is stored
  5489. - in the prev_size field of the chunk. This allows us to adjust
  5490. - returned start address to meet alignment requirements here
  5491. - and in memalign(), and still be able to compute proper
  5492. - address argument for later munmap in free() and realloc().
  5493. - */
  5494. -
  5495. - front_misalign = (size_t)chunk2mem(mm) & MALLOC_ALIGN_MASK;
  5496. - if (front_misalign > 0) {
  5497. - correction = MALLOC_ALIGNMENT - front_misalign;
  5498. - p = (mchunkptr)(mm + correction);
  5499. - p->prev_size = correction;
  5500. - set_head(p, (size - correction) |IS_MMAPPED);
  5501. - }
  5502. - else {
  5503. - p = (mchunkptr)mm;
  5504. - p->prev_size = 0;
  5505. - set_head(p, size|IS_MMAPPED);
  5506. - }
  5507. + Round up size to nearest page. For mmapped chunks, the overhead
  5508. + is one (sizeof(size_t)) unit larger than for normal chunks, because there
  5509. + is no following chunk whose prev_size field could be used.
  5510. + */
  5511. + size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
  5512. +
  5513. + /* Don't try if size wraps around 0 */
  5514. + if ((unsigned long)(size) > (unsigned long)(nb)) {
  5515. +
  5516. + mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
  5517. +
  5518. + if (mm != (char*)(MORECORE_FAILURE)) {
  5519. +
  5520. + /*
  5521. + The offset to the start of the mmapped region is stored
  5522. + in the prev_size field of the chunk. This allows us to adjust
  5523. + returned start address to meet alignment requirements here
  5524. + and in memalign(), and still be able to compute proper
  5525. + address argument for later munmap in free() and realloc().
  5526. + */
  5527. +
  5528. + front_misalign = (size_t)chunk2mem(mm) & MALLOC_ALIGN_MASK;
  5529. + if (front_misalign > 0) {
  5530. + correction = MALLOC_ALIGNMENT - front_misalign;
  5531. + p = (mchunkptr)(mm + correction);
  5532. + p->prev_size = correction;
  5533. + set_head(p, (size - correction) |IS_MMAPPED);
  5534. + }
  5535. + else {
  5536. + p = (mchunkptr)mm;
  5537. + p->prev_size = 0;
  5538. + set_head(p, size|IS_MMAPPED);
  5539. + }
  5540. +
  5541. + /* update statistics */
  5542. +
  5543. + if (++av->n_mmaps > av->max_n_mmaps)
  5544. + av->max_n_mmaps = av->n_mmaps;
  5545. +
  5546. + sum = av->mmapped_mem += size;
  5547. + if (sum > (unsigned long)(av->max_mmapped_mem))
  5548. + av->max_mmapped_mem = sum;
  5549. + sum += av->sbrked_mem;
  5550. + if (sum > (unsigned long)(av->max_total_mem))
  5551. + av->max_total_mem = sum;
  5552. - /* update statistics */
  5553. + check_chunk(p);
  5554. - if (++av->n_mmaps > av->max_n_mmaps)
  5555. - av->max_n_mmaps = av->n_mmaps;
  5556. -
  5557. - sum = av->mmapped_mem += size;
  5558. - if (sum > (unsigned long)(av->max_mmapped_mem))
  5559. - av->max_mmapped_mem = sum;
  5560. - sum += av->sbrked_mem;
  5561. - if (sum > (unsigned long)(av->max_total_mem))
  5562. - av->max_total_mem = sum;
  5563. -
  5564. - check_chunk(p);
  5565. -
  5566. - return chunk2mem(p);
  5567. - }
  5568. - }
  5569. + return chunk2mem(p);
  5570. + }
  5571. + }
  5572. }
  5573. /* Record incoming configuration of top */
  5574. @@ -462,8 +459,8 @@ static void* __malloc_alloc(size_t nb, m
  5575. * be at least MINSIZE and to have prev_inuse set. */
  5576. assert((old_top == initial_top(av) && old_size == 0) ||
  5577. - ((unsigned long) (old_size) >= MINSIZE &&
  5578. - prev_inuse(old_top)));
  5579. + ((unsigned long) (old_size) >= MINSIZE &&
  5580. + prev_inuse(old_top)));
  5581. /* Precondition: not enough current space to satisfy nb request */
  5582. assert((unsigned long)(old_size) < (unsigned long)(nb + MINSIZE));
  5583. @@ -477,272 +474,272 @@ static void* __malloc_alloc(size_t nb, m
  5584. size = nb + av->top_pad + MINSIZE;
  5585. /*
  5586. - If contiguous, we can subtract out existing space that we hope to
  5587. - combine with new space. We add it back later only if
  5588. - we don't actually get contiguous space.
  5589. - */
  5590. + If contiguous, we can subtract out existing space that we hope to
  5591. + combine with new space. We add it back later only if
  5592. + we don't actually get contiguous space.
  5593. + */
  5594. if (contiguous(av))
  5595. - size -= old_size;
  5596. + size -= old_size;
  5597. /*
  5598. - Round to a multiple of page size.
  5599. - If MORECORE is not contiguous, this ensures that we only call it
  5600. - with whole-page arguments. And if MORECORE is contiguous and
  5601. - this is not first time through, this preserves page-alignment of
  5602. - previous calls. Otherwise, we correct to page-align below.
  5603. - */
  5604. + Round to a multiple of page size.
  5605. + If MORECORE is not contiguous, this ensures that we only call it
  5606. + with whole-page arguments. And if MORECORE is contiguous and
  5607. + this is not first time through, this preserves page-alignment of
  5608. + previous calls. Otherwise, we correct to page-align below.
  5609. + */
  5610. size = (size + pagemask) & ~pagemask;
  5611. /*
  5612. - Don't try to call MORECORE if argument is so big as to appear
  5613. - negative. Note that since mmap takes size_t arg, it may succeed
  5614. - below even if we cannot call MORECORE.
  5615. - */
  5616. + Don't try to call MORECORE if argument is so big as to appear
  5617. + negative. Note that since mmap takes size_t arg, it may succeed
  5618. + below even if we cannot call MORECORE.
  5619. + */
  5620. if (size > 0)
  5621. - brk = (char*)(MORECORE(size));
  5622. + brk = (char*)(MORECORE(size));
  5623. /*
  5624. - If have mmap, try using it as a backup when MORECORE fails or
  5625. - cannot be used. This is worth doing on systems that have "holes" in
  5626. - address space, so sbrk cannot extend to give contiguous space, but
  5627. - space is available elsewhere. Note that we ignore mmap max count
  5628. - and threshold limits, since the space will not be used as a
  5629. - segregated mmap region.
  5630. - */
  5631. + If have mmap, try using it as a backup when MORECORE fails or
  5632. + cannot be used. This is worth doing on systems that have "holes" in
  5633. + address space, so sbrk cannot extend to give contiguous space, but
  5634. + space is available elsewhere. Note that we ignore mmap max count
  5635. + and threshold limits, since the space will not be used as a
  5636. + segregated mmap region.
  5637. + */
  5638. if (brk == (char*)(MORECORE_FAILURE)) {
  5639. - /* Cannot merge with old top, so add its size back in */
  5640. - if (contiguous(av))
  5641. - size = (size + old_size + pagemask) & ~pagemask;
  5642. -
  5643. - /* If we are relying on mmap as backup, then use larger units */
  5644. - if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
  5645. - size = MMAP_AS_MORECORE_SIZE;
  5646. -
  5647. - /* Don't try if size wraps around 0 */
  5648. - if ((unsigned long)(size) > (unsigned long)(nb)) {
  5649. -
  5650. - brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
  5651. -
  5652. - if (brk != (char*)(MORECORE_FAILURE)) {
  5653. -
  5654. - /* We do not need, and cannot use, another sbrk call to find end */
  5655. - snd_brk = brk + size;
  5656. -
  5657. - /* Record that we no longer have a contiguous sbrk region.
  5658. - After the first time mmap is used as backup, we do not
  5659. - ever rely on contiguous space since this could incorrectly
  5660. - bridge regions.
  5661. - */
  5662. - set_noncontiguous(av);
  5663. - }
  5664. - }
  5665. + /* Cannot merge with old top, so add its size back in */
  5666. + if (contiguous(av))
  5667. + size = (size + old_size + pagemask) & ~pagemask;
  5668. +
  5669. + /* If we are relying on mmap as backup, then use larger units */
  5670. + if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
  5671. + size = MMAP_AS_MORECORE_SIZE;
  5672. +
  5673. + /* Don't try if size wraps around 0 */
  5674. + if ((unsigned long)(size) > (unsigned long)(nb)) {
  5675. +
  5676. + brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
  5677. +
  5678. + if (brk != (char*)(MORECORE_FAILURE)) {
  5679. +
  5680. + /* We do not need, and cannot use, another sbrk call to find end */
  5681. + snd_brk = brk + size;
  5682. +
  5683. + /* Record that we no longer have a contiguous sbrk region.
  5684. + After the first time mmap is used as backup, we do not
  5685. + ever rely on contiguous space since this could incorrectly
  5686. + bridge regions.
  5687. + */
  5688. + set_noncontiguous(av);
  5689. + }
  5690. + }
  5691. }
  5692. if (brk != (char*)(MORECORE_FAILURE)) {
  5693. - av->sbrked_mem += size;
  5694. + av->sbrked_mem += size;
  5695. - /*
  5696. - If MORECORE extends previous space, we can likewise extend top size.
  5697. - */
  5698. -
  5699. - if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
  5700. - set_head(old_top, (size + old_size) | PREV_INUSE);
  5701. - }
  5702. -
  5703. - /*
  5704. - Otherwise, make adjustments:
  5705. -
  5706. - * If the first time through or noncontiguous, we need to call sbrk
  5707. - just to find out where the end of memory lies.
  5708. -
  5709. - * We need to ensure that all returned chunks from malloc will meet
  5710. - MALLOC_ALIGNMENT
  5711. -
  5712. - * If there was an intervening foreign sbrk, we need to adjust sbrk
  5713. - request size to account for fact that we will not be able to
  5714. - combine new space with existing space in old_top.
  5715. -
  5716. - * Almost all systems internally allocate whole pages at a time, in
  5717. - which case we might as well use the whole last page of request.
  5718. - So we allocate enough more memory to hit a page boundary now,
  5719. - which in turn causes future contiguous calls to page-align.
  5720. - */
  5721. -
  5722. - else {
  5723. - front_misalign = 0;
  5724. - end_misalign = 0;
  5725. - correction = 0;
  5726. - aligned_brk = brk;
  5727. -
  5728. - /*
  5729. - If MORECORE returns an address lower than we have seen before,
  5730. - we know it isn't really contiguous. This and some subsequent
  5731. - checks help cope with non-conforming MORECORE functions and
  5732. - the presence of "foreign" calls to MORECORE from outside of
  5733. - malloc or by other threads. We cannot guarantee to detect
  5734. - these in all cases, but cope with the ones we do detect.
  5735. - */
  5736. - if (contiguous(av) && old_size != 0 && brk < old_end) {
  5737. - set_noncontiguous(av);
  5738. - }
  5739. -
  5740. - /* handle contiguous cases */
  5741. - if (contiguous(av)) {
  5742. -
  5743. - /* We can tolerate forward non-contiguities here (usually due
  5744. - to foreign calls) but treat them as part of our space for
  5745. - stats reporting. */
  5746. - if (old_size != 0)
  5747. - av->sbrked_mem += brk - old_end;
  5748. -
  5749. - /* Guarantee alignment of first new chunk made from this space */
  5750. -
  5751. - front_misalign = (size_t)chunk2mem(brk) & MALLOC_ALIGN_MASK;
  5752. - if (front_misalign > 0) {
  5753. -
  5754. - /*
  5755. - Skip over some bytes to arrive at an aligned position.
  5756. - We don't need to specially mark these wasted front bytes.
  5757. - They will never be accessed anyway because
  5758. - prev_inuse of av->top (and any chunk created from its start)
  5759. - is always true after initialization.
  5760. - */
  5761. + /*
  5762. + If MORECORE extends previous space, we can likewise extend top size.
  5763. + */
  5764. - correction = MALLOC_ALIGNMENT - front_misalign;
  5765. - aligned_brk += correction;
  5766. + if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
  5767. + set_head(old_top, (size + old_size) | PREV_INUSE);
  5768. }
  5769. /*
  5770. - If this isn't adjacent to existing space, then we will not
  5771. - be able to merge with old_top space, so must add to 2nd request.
  5772. - */
  5773. -
  5774. - correction += old_size;
  5775. -
  5776. - /* Extend the end address to hit a page boundary */
  5777. - end_misalign = (size_t)(brk + size + correction);
  5778. - correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
  5779. -
  5780. - assert(correction >= 0);
  5781. - snd_brk = (char*)(MORECORE(correction));
  5782. -
  5783. - if (snd_brk == (char*)(MORECORE_FAILURE)) {
  5784. - /*
  5785. - If can't allocate correction, try to at least find out current
  5786. - brk. It might be enough to proceed without failing.
  5787. - */
  5788. - correction = 0;
  5789. - snd_brk = (char*)(MORECORE(0));
  5790. - }
  5791. - else if (snd_brk < brk) {
  5792. - /*
  5793. - If the second call gives noncontiguous space even though
  5794. - it says it won't, the only course of action is to ignore
  5795. - results of second call, and conservatively estimate where
  5796. - the first call left us. Also set noncontiguous, so this
  5797. - won't happen again, leaving at most one hole.
  5798. -
  5799. - Note that this check is intrinsically incomplete. Because
  5800. - MORECORE is allowed to give more space than we ask for,
  5801. - there is no reliable way to detect a noncontiguity
  5802. - producing a forward gap for the second call.
  5803. - */
  5804. - snd_brk = brk + size;
  5805. - correction = 0;
  5806. - set_noncontiguous(av);
  5807. - }
  5808. -
  5809. - }
  5810. -
  5811. - /* handle non-contiguous cases */
  5812. - else {
  5813. - /* MORECORE/mmap must correctly align */
  5814. - assert(aligned_OK(chunk2mem(brk)));
  5815. -
  5816. - /* Find out current end of memory */
  5817. - if (snd_brk == (char*)(MORECORE_FAILURE)) {
  5818. - snd_brk = (char*)(MORECORE(0));
  5819. - av->sbrked_mem += snd_brk - brk - size;
  5820. - }
  5821. - }
  5822. -
  5823. - /* Adjust top based on results of second sbrk */
  5824. - if (snd_brk != (char*)(MORECORE_FAILURE)) {
  5825. - av->top = (mchunkptr)aligned_brk;
  5826. - set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
  5827. - av->sbrked_mem += correction;
  5828. + Otherwise, make adjustments:
  5829. - /*
  5830. - If not the first time through, we either have a
  5831. - gap due to foreign sbrk or a non-contiguous region. Insert a
  5832. - double fencepost at old_top to prevent consolidation with space
  5833. - we don't own. These fenceposts are artificial chunks that are
  5834. - marked as inuse and are in any case too small to use. We need
  5835. - two to make sizes and alignments work out.
  5836. - */
  5837. -
  5838. - if (old_size != 0) {
  5839. - /* Shrink old_top to insert fenceposts, keeping size a
  5840. - multiple of MALLOC_ALIGNMENT. We know there is at least
  5841. - enough space in old_top to do this.
  5842. - */
  5843. - old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK;
  5844. - set_head(old_top, old_size | PREV_INUSE);
  5845. -
  5846. - /*
  5847. - Note that the following assignments completely overwrite
  5848. - old_top when old_size was previously MINSIZE. This is
  5849. - intentional. We need the fencepost, even if old_top otherwise gets
  5850. - lost.
  5851. - */
  5852. - chunk_at_offset(old_top, old_size )->size =
  5853. - (sizeof(size_t))|PREV_INUSE;
  5854. -
  5855. - chunk_at_offset(old_top, old_size + (sizeof(size_t)))->size =
  5856. - (sizeof(size_t))|PREV_INUSE;
  5857. -
  5858. - /* If possible, release the rest, suppressing trimming. */
  5859. - if (old_size >= MINSIZE) {
  5860. - size_t tt = av->trim_threshold;
  5861. - av->trim_threshold = (size_t)(-1);
  5862. - free(chunk2mem(old_top));
  5863. - av->trim_threshold = tt;
  5864. - }
  5865. - }
  5866. - }
  5867. - }
  5868. -
  5869. - /* Update statistics */
  5870. - sum = av->sbrked_mem;
  5871. - if (sum > (unsigned long)(av->max_sbrked_mem))
  5872. - av->max_sbrked_mem = sum;
  5873. -
  5874. - sum += av->mmapped_mem;
  5875. - if (sum > (unsigned long)(av->max_total_mem))
  5876. - av->max_total_mem = sum;
  5877. -
  5878. - check_malloc_state();
  5879. -
  5880. - /* finally, do the allocation */
  5881. -
  5882. - p = av->top;
  5883. - size = chunksize(p);
  5884. -
  5885. - /* check that one of the above allocation paths succeeded */
  5886. - if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
  5887. - remainder_size = size - nb;
  5888. - remainder = chunk_at_offset(p, nb);
  5889. - av->top = remainder;
  5890. - set_head(p, nb | PREV_INUSE);
  5891. - set_head(remainder, remainder_size | PREV_INUSE);
  5892. - check_malloced_chunk(p, nb);
  5893. - return chunk2mem(p);
  5894. - }
  5895. + * If the first time through or noncontiguous, we need to call sbrk
  5896. + just to find out where the end of memory lies.
  5897. +
  5898. + * We need to ensure that all returned chunks from malloc will meet
  5899. + MALLOC_ALIGNMENT
  5900. +
  5901. + * If there was an intervening foreign sbrk, we need to adjust sbrk
  5902. + request size to account for fact that we will not be able to
  5903. + combine new space with existing space in old_top.
  5904. +
  5905. + * Almost all systems internally allocate whole pages at a time, in
  5906. + which case we might as well use the whole last page of request.
  5907. + So we allocate enough more memory to hit a page boundary now,
  5908. + which in turn causes future contiguous calls to page-align.
  5909. + */
  5910. +
  5911. + else {
  5912. + front_misalign = 0;
  5913. + end_misalign = 0;
  5914. + correction = 0;
  5915. + aligned_brk = brk;
  5916. +
  5917. + /*
  5918. + If MORECORE returns an address lower than we have seen before,
  5919. + we know it isn't really contiguous. This and some subsequent
  5920. + checks help cope with non-conforming MORECORE functions and
  5921. + the presence of "foreign" calls to MORECORE from outside of
  5922. + malloc or by other threads. We cannot guarantee to detect
  5923. + these in all cases, but cope with the ones we do detect.
  5924. + */
  5925. + if (contiguous(av) && old_size != 0 && brk < old_end) {
  5926. + set_noncontiguous(av);
  5927. + }
  5928. +
  5929. + /* handle contiguous cases */
  5930. + if (contiguous(av)) {
  5931. +
  5932. + /* We can tolerate forward non-contiguities here (usually due
  5933. + to foreign calls) but treat them as part of our space for
  5934. + stats reporting. */
  5935. + if (old_size != 0)
  5936. + av->sbrked_mem += brk - old_end;
  5937. +
  5938. + /* Guarantee alignment of first new chunk made from this space */
  5939. +
  5940. + front_misalign = (size_t)chunk2mem(brk) & MALLOC_ALIGN_MASK;
  5941. + if (front_misalign > 0) {
  5942. +
  5943. + /*
  5944. + Skip over some bytes to arrive at an aligned position.
  5945. + We don't need to specially mark these wasted front bytes.
  5946. + They will never be accessed anyway because
  5947. + prev_inuse of av->top (and any chunk created from its start)
  5948. + is always true after initialization.
  5949. + */
  5950. +
  5951. + correction = MALLOC_ALIGNMENT - front_misalign;
  5952. + aligned_brk += correction;
  5953. + }
  5954. +
  5955. + /*
  5956. + If this isn't adjacent to existing space, then we will not
  5957. + be able to merge with old_top space, so must add to 2nd request.
  5958. + */
  5959. +
  5960. + correction += old_size;
  5961. +
  5962. + /* Extend the end address to hit a page boundary */
  5963. + end_misalign = (size_t)(brk + size + correction);
  5964. + correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
  5965. +
  5966. + assert(correction >= 0);
  5967. + snd_brk = (char*)(MORECORE(correction));
  5968. +
  5969. + if (snd_brk == (char*)(MORECORE_FAILURE)) {
  5970. + /*
  5971. + If can't allocate correction, try to at least find out current
  5972. + brk. It might be enough to proceed without failing.
  5973. + */
  5974. + correction = 0;
  5975. + snd_brk = (char*)(MORECORE(0));
  5976. + }
  5977. + else if (snd_brk < brk) {
  5978. + /*
  5979. + If the second call gives noncontiguous space even though
  5980. + it says it won't, the only course of action is to ignore
  5981. + results of second call, and conservatively estimate where
  5982. + the first call left us. Also set noncontiguous, so this
  5983. + won't happen again, leaving at most one hole.
  5984. +
  5985. + Note that this check is intrinsically incomplete. Because
  5986. + MORECORE is allowed to give more space than we ask for,
  5987. + there is no reliable way to detect a noncontiguity
  5988. + producing a forward gap for the second call.
  5989. + */
  5990. + snd_brk = brk + size;
  5991. + correction = 0;
  5992. + set_noncontiguous(av);
  5993. + }
  5994. +
  5995. + }
  5996. +
  5997. + /* handle non-contiguous cases */
  5998. + else {
  5999. + /* MORECORE/mmap must correctly align */
  6000. + assert(aligned_OK(chunk2mem(brk)));
  6001. +
  6002. + /* Find out current end of memory */
  6003. + if (snd_brk == (char*)(MORECORE_FAILURE)) {
  6004. + snd_brk = (char*)(MORECORE(0));
  6005. + av->sbrked_mem += snd_brk - brk - size;
  6006. + }
  6007. + }
  6008. +
  6009. + /* Adjust top based on results of second sbrk */
  6010. + if (snd_brk != (char*)(MORECORE_FAILURE)) {
  6011. + av->top = (mchunkptr)aligned_brk;
  6012. + set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
  6013. + av->sbrked_mem += correction;
  6014. +
  6015. + /*
  6016. + If not the first time through, we either have a
  6017. + gap due to foreign sbrk or a non-contiguous region. Insert a
  6018. + double fencepost at old_top to prevent consolidation with space
  6019. + we don't own. These fenceposts are artificial chunks that are
  6020. + marked as inuse and are in any case too small to use. We need
  6021. + two to make sizes and alignments work out.
  6022. + */
  6023. +
  6024. + if (old_size != 0) {
  6025. + /* Shrink old_top to insert fenceposts, keeping size a
  6026. + multiple of MALLOC_ALIGNMENT. We know there is at least
  6027. + enough space in old_top to do this.
  6028. + */
  6029. + old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK;
  6030. + set_head(old_top, old_size | PREV_INUSE);
  6031. +
  6032. + /*
  6033. + Note that the following assignments completely overwrite
  6034. + old_top when old_size was previously MINSIZE. This is
  6035. + intentional. We need the fencepost, even if old_top otherwise gets
  6036. + lost.
  6037. + */
  6038. + chunk_at_offset(old_top, old_size )->size =
  6039. + (sizeof(size_t))|PREV_INUSE;
  6040. +
  6041. + chunk_at_offset(old_top, old_size + (sizeof(size_t)))->size =
  6042. + (sizeof(size_t))|PREV_INUSE;
  6043. +
  6044. + /* If possible, release the rest, suppressing trimming. */
  6045. + if (old_size >= MINSIZE) {
  6046. + size_t tt = av->trim_threshold;
  6047. + av->trim_threshold = (size_t)(-1);
  6048. + free(chunk2mem(old_top));
  6049. + av->trim_threshold = tt;
  6050. + }
  6051. + }
  6052. + }
  6053. + }
  6054. +
  6055. + /* Update statistics */
  6056. + sum = av->sbrked_mem;
  6057. + if (sum > (unsigned long)(av->max_sbrked_mem))
  6058. + av->max_sbrked_mem = sum;
  6059. +
  6060. + sum += av->mmapped_mem;
  6061. + if (sum > (unsigned long)(av->max_total_mem))
  6062. + av->max_total_mem = sum;
  6063. +
  6064. + check_malloc_state();
  6065. +
  6066. + /* finally, do the allocation */
  6067. +
  6068. + p = av->top;
  6069. + size = chunksize(p);
  6070. +
  6071. + /* check that one of the above allocation paths succeeded */
  6072. + if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
  6073. + remainder_size = size - nb;
  6074. + remainder = chunk_at_offset(p, nb);
  6075. + av->top = remainder;
  6076. + set_head(p, nb | PREV_INUSE);
  6077. + set_head(remainder, remainder_size | PREV_INUSE);
  6078. + check_malloced_chunk(p, nb);
  6079. + return chunk2mem(p);
  6080. + }
  6081. }
  6082. @@ -767,25 +764,25 @@ static int __malloc_largebin_index(unsig
  6083. #if defined(__GNUC__) && defined(i386)
  6084. __asm__("bsrl %1,%0\n\t"
  6085. - : "=r" (m)
  6086. - : "g" (x));
  6087. + : "=r" (m)
  6088. + : "g" (x));
  6089. #else
  6090. {
  6091. - /*
  6092. - Based on branch-free nlz algorithm in chapter 5 of Henry
  6093. - S. Warren Jr's book "Hacker's Delight".
  6094. - */
  6095. -
  6096. - unsigned int n = ((x - 0x100) >> 16) & 8;
  6097. - x <<= n;
  6098. - m = ((x - 0x1000) >> 16) & 4;
  6099. - n += m;
  6100. - x <<= m;
  6101. - m = ((x - 0x4000) >> 16) & 2;
  6102. - n += m;
  6103. - x = (x << m) >> 14;
  6104. - m = 13 - n + (x & ~(x>>1));
  6105. + /*
  6106. + Based on branch-free nlz algorithm in chapter 5 of Henry
  6107. + S. Warren Jr's book "Hacker's Delight".
  6108. + */
  6109. +
  6110. + unsigned int n = ((x - 0x100) >> 16) & 8;
  6111. + x <<= n;
  6112. + m = ((x - 0x1000) >> 16) & 4;
  6113. + n += m;
  6114. + x <<= m;
  6115. + m = ((x - 0x4000) >> 16) & 2;
  6116. + n += m;
  6117. + x = (x << m) >> 14;
  6118. + m = 13 - n + (x & ~(x>>1));
  6119. }
  6120. #endif
  6121. @@ -826,69 +823,70 @@ void* malloc(size_t bytes)
  6122. mchunkptr fwd; /* misc temp for linking */
  6123. mchunkptr bck; /* misc temp for linking */
  6124. void * sysmem;
  6125. + void * retval;
  6126. #if !defined(__MALLOC_GLIBC_COMPAT__)
  6127. if (!bytes) return NULL;
  6128. #endif
  6129. - LOCK;
  6130. + __MALLOC_LOCK;
  6131. av = get_malloc_state();
  6132. /*
  6133. - Convert request size to internal form by adding (sizeof(size_t)) bytes
  6134. - overhead plus possibly more to obtain necessary alignment and/or
  6135. - to obtain a size of at least MINSIZE, the smallest allocatable
  6136. - size. Also, checked_request2size traps (returning 0) request sizes
  6137. - that are so large that they wrap around zero when padded and
  6138. - aligned.
  6139. - */
  6140. + Convert request size to internal form by adding (sizeof(size_t)) bytes
  6141. + overhead plus possibly more to obtain necessary alignment and/or
  6142. + to obtain a size of at least MINSIZE, the smallest allocatable
  6143. + size. Also, checked_request2size traps (returning 0) request sizes
  6144. + that are so large that they wrap around zero when padded and
  6145. + aligned.
  6146. + */
  6147. checked_request2size(bytes, nb);
  6148. /*
  6149. - Bypass search if no frees yet
  6150. - */
  6151. + Bypass search if no frees yet
  6152. + */
  6153. if (!have_anychunks(av)) {
  6154. - if (av->max_fast == 0) /* initialization check */
  6155. - __malloc_consolidate(av);
  6156. - goto use_top;
  6157. + if (av->max_fast == 0) /* initialization check */
  6158. + __malloc_consolidate(av);
  6159. + goto use_top;
  6160. }
  6161. /*
  6162. - If the size qualifies as a fastbin, first check corresponding bin.
  6163. - */
  6164. + If the size qualifies as a fastbin, first check corresponding bin.
  6165. + */
  6166. if ((unsigned long)(nb) <= (unsigned long)(av->max_fast)) {
  6167. - fb = &(av->fastbins[(fastbin_index(nb))]);
  6168. - if ( (victim = *fb) != 0) {
  6169. - *fb = victim->fd;
  6170. - check_remalloced_chunk(victim, nb);
  6171. - UNLOCK;
  6172. - return chunk2mem(victim);
  6173. - }
  6174. + fb = &(av->fastbins[(fastbin_index(nb))]);
  6175. + if ( (victim = *fb) != 0) {
  6176. + *fb = victim->fd;
  6177. + check_remalloced_chunk(victim, nb);
  6178. + retval = chunk2mem(victim);
  6179. + goto DONE;
  6180. + }
  6181. }
  6182. /*
  6183. - If a small request, check regular bin. Since these "smallbins"
  6184. - hold one size each, no searching within bins is necessary.
  6185. - (For a large request, we need to wait until unsorted chunks are
  6186. - processed to find best fit. But for small ones, fits are exact
  6187. - anyway, so we can check now, which is faster.)
  6188. - */
  6189. + If a small request, check regular bin. Since these "smallbins"
  6190. + hold one size each, no searching within bins is necessary.
  6191. + (For a large request, we need to wait until unsorted chunks are
  6192. + processed to find best fit. But for small ones, fits are exact
  6193. + anyway, so we can check now, which is faster.)
  6194. + */
  6195. if (in_smallbin_range(nb)) {
  6196. - idx = smallbin_index(nb);
  6197. - bin = bin_at(av,idx);
  6198. + idx = smallbin_index(nb);
  6199. + bin = bin_at(av,idx);
  6200. - if ( (victim = last(bin)) != bin) {
  6201. - bck = victim->bk;
  6202. - set_inuse_bit_at_offset(victim, nb);
  6203. - bin->bk = bck;
  6204. - bck->fd = bin;
  6205. -
  6206. - check_malloced_chunk(victim, nb);
  6207. - UNLOCK;
  6208. - return chunk2mem(victim);
  6209. - }
  6210. + if ( (victim = last(bin)) != bin) {
  6211. + bck = victim->bk;
  6212. + set_inuse_bit_at_offset(victim, nb);
  6213. + bin->bk = bck;
  6214. + bck->fd = bin;
  6215. +
  6216. + check_malloced_chunk(victim, nb);
  6217. + retval = chunk2mem(victim);
  6218. + goto DONE;
  6219. + }
  6220. }
  6221. /* If this is a large request, consolidate fastbins before continuing.
  6222. @@ -899,154 +897,154 @@ void* malloc(size_t bytes)
  6223. large requests, but less often mixtures, so consolidation is not
  6224. invoked all that often in most programs. And the programs that
  6225. it is called frequently in otherwise tend to fragment.
  6226. - */
  6227. + */
  6228. else {
  6229. - idx = __malloc_largebin_index(nb);
  6230. - if (have_fastchunks(av))
  6231. - __malloc_consolidate(av);
  6232. + idx = __malloc_largebin_index(nb);
  6233. + if (have_fastchunks(av))
  6234. + __malloc_consolidate(av);
  6235. }
  6236. /*
  6237. - Process recently freed or remaindered chunks, taking one only if
  6238. - it is exact fit, or, if this a small request, the chunk is remainder from
  6239. - the most recent non-exact fit. Place other traversed chunks in
  6240. - bins. Note that this step is the only place in any routine where
  6241. - chunks are placed in bins.
  6242. - */
  6243. + Process recently freed or remaindered chunks, taking one only if
  6244. + it is exact fit, or, if this a small request, the chunk is remainder from
  6245. + the most recent non-exact fit. Place other traversed chunks in
  6246. + bins. Note that this step is the only place in any routine where
  6247. + chunks are placed in bins.
  6248. + */
  6249. while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
  6250. - bck = victim->bk;
  6251. - size = chunksize(victim);
  6252. + bck = victim->bk;
  6253. + size = chunksize(victim);
  6254. +
  6255. + /* If a small request, try to use last remainder if it is the
  6256. + only chunk in unsorted bin. This helps promote locality for
  6257. + runs of consecutive small requests. This is the only
  6258. + exception to best-fit, and applies only when there is
  6259. + no exact fit for a small chunk.
  6260. + */
  6261. +
  6262. + if (in_smallbin_range(nb) &&
  6263. + bck == unsorted_chunks(av) &&
  6264. + victim == av->last_remainder &&
  6265. + (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
  6266. +
  6267. + /* split and reattach remainder */
  6268. + remainder_size = size - nb;
  6269. + remainder = chunk_at_offset(victim, nb);
  6270. + unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6271. + av->last_remainder = remainder;
  6272. + remainder->bk = remainder->fd = unsorted_chunks(av);
  6273. +
  6274. + set_head(victim, nb | PREV_INUSE);
  6275. + set_head(remainder, remainder_size | PREV_INUSE);
  6276. + set_foot(remainder, remainder_size);
  6277. +
  6278. + check_malloced_chunk(victim, nb);
  6279. + retval = chunk2mem(victim);
  6280. + goto DONE;
  6281. + }
  6282. +
  6283. + /* remove from unsorted list */
  6284. + unsorted_chunks(av)->bk = bck;
  6285. + bck->fd = unsorted_chunks(av);
  6286. +
  6287. + /* Take now instead of binning if exact fit */
  6288. +
  6289. + if (size == nb) {
  6290. + set_inuse_bit_at_offset(victim, size);
  6291. + check_malloced_chunk(victim, nb);
  6292. + retval = chunk2mem(victim);
  6293. + goto DONE;
  6294. + }
  6295. +
  6296. + /* place chunk in bin */
  6297. - /* If a small request, try to use last remainder if it is the
  6298. - only chunk in unsorted bin. This helps promote locality for
  6299. - runs of consecutive small requests. This is the only
  6300. - exception to best-fit, and applies only when there is
  6301. - no exact fit for a small chunk.
  6302. - */
  6303. -
  6304. - if (in_smallbin_range(nb) &&
  6305. - bck == unsorted_chunks(av) &&
  6306. - victim == av->last_remainder &&
  6307. - (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
  6308. -
  6309. - /* split and reattach remainder */
  6310. - remainder_size = size - nb;
  6311. - remainder = chunk_at_offset(victim, nb);
  6312. - unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6313. - av->last_remainder = remainder;
  6314. - remainder->bk = remainder->fd = unsorted_chunks(av);
  6315. -
  6316. - set_head(victim, nb | PREV_INUSE);
  6317. - set_head(remainder, remainder_size | PREV_INUSE);
  6318. - set_foot(remainder, remainder_size);
  6319. -
  6320. - check_malloced_chunk(victim, nb);
  6321. - UNLOCK;
  6322. - return chunk2mem(victim);
  6323. - }
  6324. -
  6325. - /* remove from unsorted list */
  6326. - unsorted_chunks(av)->bk = bck;
  6327. - bck->fd = unsorted_chunks(av);
  6328. -
  6329. - /* Take now instead of binning if exact fit */
  6330. -
  6331. - if (size == nb) {
  6332. - set_inuse_bit_at_offset(victim, size);
  6333. - check_malloced_chunk(victim, nb);
  6334. - UNLOCK;
  6335. - return chunk2mem(victim);
  6336. - }
  6337. -
  6338. - /* place chunk in bin */
  6339. -
  6340. - if (in_smallbin_range(size)) {
  6341. - victim_index = smallbin_index(size);
  6342. - bck = bin_at(av, victim_index);
  6343. - fwd = bck->fd;
  6344. - }
  6345. - else {
  6346. - victim_index = __malloc_largebin_index(size);
  6347. - bck = bin_at(av, victim_index);
  6348. - fwd = bck->fd;
  6349. -
  6350. - if (fwd != bck) {
  6351. - /* if smaller than smallest, place first */
  6352. - if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
  6353. - fwd = bck;
  6354. - bck = bck->bk;
  6355. - }
  6356. - else if ((unsigned long)(size) >=
  6357. - (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
  6358. -
  6359. - /* maintain large bins in sorted order */
  6360. - size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
  6361. - while ((unsigned long)(size) < (unsigned long)(fwd->size))
  6362. - fwd = fwd->fd;
  6363. - bck = fwd->bk;
  6364. - }
  6365. - }
  6366. - }
  6367. -
  6368. - mark_bin(av, victim_index);
  6369. - victim->bk = bck;
  6370. - victim->fd = fwd;
  6371. - fwd->bk = victim;
  6372. - bck->fd = victim;
  6373. + if (in_smallbin_range(size)) {
  6374. + victim_index = smallbin_index(size);
  6375. + bck = bin_at(av, victim_index);
  6376. + fwd = bck->fd;
  6377. + }
  6378. + else {
  6379. + victim_index = __malloc_largebin_index(size);
  6380. + bck = bin_at(av, victim_index);
  6381. + fwd = bck->fd;
  6382. +
  6383. + if (fwd != bck) {
  6384. + /* if smaller than smallest, place first */
  6385. + if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
  6386. + fwd = bck;
  6387. + bck = bck->bk;
  6388. + }
  6389. + else if ((unsigned long)(size) >=
  6390. + (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
  6391. +
  6392. + /* maintain large bins in sorted order */
  6393. + size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
  6394. + while ((unsigned long)(size) < (unsigned long)(fwd->size))
  6395. + fwd = fwd->fd;
  6396. + bck = fwd->bk;
  6397. + }
  6398. + }
  6399. + }
  6400. +
  6401. + mark_bin(av, victim_index);
  6402. + victim->bk = bck;
  6403. + victim->fd = fwd;
  6404. + fwd->bk = victim;
  6405. + bck->fd = victim;
  6406. }
  6407. /*
  6408. - If a large request, scan through the chunks of current bin to
  6409. - find one that fits. (This will be the smallest that fits unless
  6410. - FIRST_SORTED_BIN_SIZE has been changed from default.) This is
  6411. - the only step where an unbounded number of chunks might be
  6412. - scanned without doing anything useful with them. However the
  6413. - lists tend to be short.
  6414. - */
  6415. + If a large request, scan through the chunks of current bin to
  6416. + find one that fits. (This will be the smallest that fits unless
  6417. + FIRST_SORTED_BIN_SIZE has been changed from default.) This is
  6418. + the only step where an unbounded number of chunks might be
  6419. + scanned without doing anything useful with them. However the
  6420. + lists tend to be short.
  6421. + */
  6422. if (!in_smallbin_range(nb)) {
  6423. - bin = bin_at(av, idx);
  6424. -
  6425. - for (victim = last(bin); victim != bin; victim = victim->bk) {
  6426. - size = chunksize(victim);
  6427. + bin = bin_at(av, idx);
  6428. - if ((unsigned long)(size) >= (unsigned long)(nb)) {
  6429. - remainder_size = size - nb;
  6430. - unlink(victim, bck, fwd);
  6431. + for (victim = last(bin); victim != bin; victim = victim->bk) {
  6432. + size = chunksize(victim);
  6433. - /* Exhaust */
  6434. - if (remainder_size < MINSIZE) {
  6435. - set_inuse_bit_at_offset(victim, size);
  6436. - check_malloced_chunk(victim, nb);
  6437. - UNLOCK;
  6438. - return chunk2mem(victim);
  6439. - }
  6440. - /* Split */
  6441. - else {
  6442. - remainder = chunk_at_offset(victim, nb);
  6443. - unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6444. - remainder->bk = remainder->fd = unsorted_chunks(av);
  6445. - set_head(victim, nb | PREV_INUSE);
  6446. - set_head(remainder, remainder_size | PREV_INUSE);
  6447. - set_foot(remainder, remainder_size);
  6448. - check_malloced_chunk(victim, nb);
  6449. - UNLOCK;
  6450. - return chunk2mem(victim);
  6451. + if ((unsigned long)(size) >= (unsigned long)(nb)) {
  6452. + remainder_size = size - nb;
  6453. + unlink(victim, bck, fwd);
  6454. +
  6455. + /* Exhaust */
  6456. + if (remainder_size < MINSIZE) {
  6457. + set_inuse_bit_at_offset(victim, size);
  6458. + check_malloced_chunk(victim, nb);
  6459. + retval = chunk2mem(victim);
  6460. + goto DONE;
  6461. + }
  6462. + /* Split */
  6463. + else {
  6464. + remainder = chunk_at_offset(victim, nb);
  6465. + unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6466. + remainder->bk = remainder->fd = unsorted_chunks(av);
  6467. + set_head(victim, nb | PREV_INUSE);
  6468. + set_head(remainder, remainder_size | PREV_INUSE);
  6469. + set_foot(remainder, remainder_size);
  6470. + check_malloced_chunk(victim, nb);
  6471. + retval = chunk2mem(victim);
  6472. + goto DONE;
  6473. + }
  6474. + }
  6475. }
  6476. - }
  6477. - }
  6478. }
  6479. /*
  6480. - Search for a chunk by scanning bins, starting with next largest
  6481. - bin. This search is strictly by best-fit; i.e., the smallest
  6482. - (with ties going to approximately the least recently used) chunk
  6483. - that fits is selected.
  6484. + Search for a chunk by scanning bins, starting with next largest
  6485. + bin. This search is strictly by best-fit; i.e., the smallest
  6486. + (with ties going to approximately the least recently used) chunk
  6487. + that fits is selected.
  6488. - The bitmap avoids needing to check that most blocks are nonempty.
  6489. - */
  6490. + The bitmap avoids needing to check that most blocks are nonempty.
  6491. + */
  6492. ++idx;
  6493. bin = bin_at(av,idx);
  6494. @@ -1056,109 +1054,111 @@ void* malloc(size_t bytes)
  6495. for (;;) {
  6496. - /* Skip rest of block if there are no more set bits in this block. */
  6497. - if (bit > map || bit == 0) {
  6498. - do {
  6499. - if (++block >= BINMAPSIZE) /* out of bins */
  6500. - goto use_top;
  6501. - } while ( (map = av->binmap[block]) == 0);
  6502. -
  6503. - bin = bin_at(av, (block << BINMAPSHIFT));
  6504. - bit = 1;
  6505. - }
  6506. -
  6507. - /* Advance to bin with set bit. There must be one. */
  6508. - while ((bit & map) == 0) {
  6509. - bin = next_bin(bin);
  6510. - bit <<= 1;
  6511. - assert(bit != 0);
  6512. - }
  6513. -
  6514. - /* Inspect the bin. It is likely to be non-empty */
  6515. - victim = last(bin);
  6516. -
  6517. - /* If a false alarm (empty bin), clear the bit. */
  6518. - if (victim == bin) {
  6519. - av->binmap[block] = map &= ~bit; /* Write through */
  6520. - bin = next_bin(bin);
  6521. - bit <<= 1;
  6522. - }
  6523. -
  6524. - else {
  6525. - size = chunksize(victim);
  6526. -
  6527. - /* We know the first chunk in this bin is big enough to use. */
  6528. - assert((unsigned long)(size) >= (unsigned long)(nb));
  6529. -
  6530. - remainder_size = size - nb;
  6531. -
  6532. - /* unlink */
  6533. - bck = victim->bk;
  6534. - bin->bk = bck;
  6535. - bck->fd = bin;
  6536. -
  6537. - /* Exhaust */
  6538. - if (remainder_size < MINSIZE) {
  6539. - set_inuse_bit_at_offset(victim, size);
  6540. - check_malloced_chunk(victim, nb);
  6541. - UNLOCK;
  6542. - return chunk2mem(victim);
  6543. - }
  6544. + /* Skip rest of block if there are no more set bits in this block. */
  6545. + if (bit > map || bit == 0) {
  6546. + do {
  6547. + if (++block >= BINMAPSIZE) /* out of bins */
  6548. + goto use_top;
  6549. + } while ( (map = av->binmap[block]) == 0);
  6550. - /* Split */
  6551. - else {
  6552. - remainder = chunk_at_offset(victim, nb);
  6553. + bin = bin_at(av, (block << BINMAPSHIFT));
  6554. + bit = 1;
  6555. + }
  6556. - unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6557. - remainder->bk = remainder->fd = unsorted_chunks(av);
  6558. - /* advertise as last remainder */
  6559. - if (in_smallbin_range(nb))
  6560. - av->last_remainder = remainder;
  6561. + /* Advance to bin with set bit. There must be one. */
  6562. + while ((bit & map) == 0) {
  6563. + bin = next_bin(bin);
  6564. + bit <<= 1;
  6565. + assert(bit != 0);
  6566. + }
  6567. - set_head(victim, nb | PREV_INUSE);
  6568. - set_head(remainder, remainder_size | PREV_INUSE);
  6569. - set_foot(remainder, remainder_size);
  6570. - check_malloced_chunk(victim, nb);
  6571. - UNLOCK;
  6572. - return chunk2mem(victim);
  6573. - }
  6574. - }
  6575. + /* Inspect the bin. It is likely to be non-empty */
  6576. + victim = last(bin);
  6577. +
  6578. + /* If a false alarm (empty bin), clear the bit. */
  6579. + if (victim == bin) {
  6580. + av->binmap[block] = map &= ~bit; /* Write through */
  6581. + bin = next_bin(bin);
  6582. + bit <<= 1;
  6583. + }
  6584. +
  6585. + else {
  6586. + size = chunksize(victim);
  6587. +
  6588. + /* We know the first chunk in this bin is big enough to use. */
  6589. + assert((unsigned long)(size) >= (unsigned long)(nb));
  6590. +
  6591. + remainder_size = size - nb;
  6592. +
  6593. + /* unlink */
  6594. + bck = victim->bk;
  6595. + bin->bk = bck;
  6596. + bck->fd = bin;
  6597. +
  6598. + /* Exhaust */
  6599. + if (remainder_size < MINSIZE) {
  6600. + set_inuse_bit_at_offset(victim, size);
  6601. + check_malloced_chunk(victim, nb);
  6602. + retval = chunk2mem(victim);
  6603. + goto DONE;
  6604. + }
  6605. +
  6606. + /* Split */
  6607. + else {
  6608. + remainder = chunk_at_offset(victim, nb);
  6609. +
  6610. + unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
  6611. + remainder->bk = remainder->fd = unsorted_chunks(av);
  6612. + /* advertise as last remainder */
  6613. + if (in_smallbin_range(nb))
  6614. + av->last_remainder = remainder;
  6615. +
  6616. + set_head(victim, nb | PREV_INUSE);
  6617. + set_head(remainder, remainder_size | PREV_INUSE);
  6618. + set_foot(remainder, remainder_size);
  6619. + check_malloced_chunk(victim, nb);
  6620. + retval = chunk2mem(victim);
  6621. + goto DONE;
  6622. + }
  6623. + }
  6624. }
  6625. -use_top:
  6626. + use_top:
  6627. /*
  6628. - If large enough, split off the chunk bordering the end of memory
  6629. - (held in av->top). Note that this is in accord with the best-fit
  6630. - search rule. In effect, av->top is treated as larger (and thus
  6631. - less well fitting) than any other available chunk since it can
  6632. - be extended to be as large as necessary (up to system
  6633. - limitations).
  6634. -
  6635. - We require that av->top always exists (i.e., has size >=
  6636. - MINSIZE) after initialization, so if it would otherwise be
  6637. - exhuasted by current request, it is replenished. (The main
  6638. - reason for ensuring it exists is that we may need MINSIZE space
  6639. - to put in fenceposts in sysmalloc.)
  6640. - */
  6641. + If large enough, split off the chunk bordering the end of memory
  6642. + (held in av->top). Note that this is in accord with the best-fit
  6643. + search rule. In effect, av->top is treated as larger (and thus
  6644. + less well fitting) than any other available chunk since it can
  6645. + be extended to be as large as necessary (up to system
  6646. + limitations).
  6647. +
  6648. + We require that av->top always exists (i.e., has size >=
  6649. + MINSIZE) after initialization, so if it would otherwise be
  6650. + exhuasted by current request, it is replenished. (The main
  6651. + reason for ensuring it exists is that we may need MINSIZE space
  6652. + to put in fenceposts in sysmalloc.)
  6653. + */
  6654. victim = av->top;
  6655. size = chunksize(victim);
  6656. if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
  6657. - remainder_size = size - nb;
  6658. - remainder = chunk_at_offset(victim, nb);
  6659. - av->top = remainder;
  6660. - set_head(victim, nb | PREV_INUSE);
  6661. - set_head(remainder, remainder_size | PREV_INUSE);
  6662. -
  6663. - check_malloced_chunk(victim, nb);
  6664. - UNLOCK;
  6665. - return chunk2mem(victim);
  6666. + remainder_size = size - nb;
  6667. + remainder = chunk_at_offset(victim, nb);
  6668. + av->top = remainder;
  6669. + set_head(victim, nb | PREV_INUSE);
  6670. + set_head(remainder, remainder_size | PREV_INUSE);
  6671. +
  6672. + check_malloced_chunk(victim, nb);
  6673. + retval = chunk2mem(victim);
  6674. + goto DONE;
  6675. }
  6676. /* If no space in top, relay to handle system-dependent cases */
  6677. sysmem = __malloc_alloc(nb, av);
  6678. - UNLOCK;
  6679. - return sysmem;
  6680. + retval = sysmem;
  6681. + DONE:
  6682. + __MALLOC_UNLOCK;
  6683. + return retval;
  6684. }
  6685. diff --git a/libc/stdlib/malloc-standard/malloc.h b/libc/stdlib/malloc-standard/malloc.h
  6686. index fbc1492..14a0dd9 100644
  6687. --- a/libc/stdlib/malloc-standard/malloc.h
  6688. +++ b/libc/stdlib/malloc-standard/malloc.h
  6689. @@ -22,16 +22,12 @@
  6690. #include <malloc.h>
  6691. #include <stdlib.h>
  6692. +#include <bits/uClibc_mutex.h>
  6693. -#ifdef __UCLIBC_HAS_THREADS__
  6694. -#include <pthread.h>
  6695. -extern pthread_mutex_t __malloc_lock;
  6696. -# define LOCK __pthread_mutex_lock(&__malloc_lock)
  6697. -# define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
  6698. -#else
  6699. -# define LOCK
  6700. -# define UNLOCK
  6701. -#endif
  6702. +__UCLIBC_MUTEX_EXTERN(__malloc_lock);
  6703. +
  6704. +#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  6705. +#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  6706. diff --git a/libc/stdlib/malloc-standard/mallopt.c b/libc/stdlib/malloc-standard/mallopt.c
  6707. index e287920..41aa614 100644
  6708. --- a/libc/stdlib/malloc-standard/mallopt.c
  6709. +++ b/libc/stdlib/malloc-standard/mallopt.c
  6710. @@ -8,7 +8,7 @@
  6711. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  6712. Note: There may be an updated version of this malloc obtainable at
  6713. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6714. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6715. Check before installing!
  6716. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  6717. @@ -25,40 +25,40 @@ int mallopt(int param_number, int value)
  6718. ret = 0;
  6719. - LOCK;
  6720. + __MALLOC_LOCK;
  6721. av = get_malloc_state();
  6722. /* Ensure initialization/consolidation */
  6723. __malloc_consolidate(av);
  6724. switch(param_number) {
  6725. - case M_MXFAST:
  6726. - if (value >= 0 && value <= MAX_FAST_SIZE) {
  6727. - set_max_fast(av, value);
  6728. - ret = 1;
  6729. - }
  6730. - break;
  6731. -
  6732. - case M_TRIM_THRESHOLD:
  6733. - av->trim_threshold = value;
  6734. - ret = 1;
  6735. - break;
  6736. -
  6737. - case M_TOP_PAD:
  6738. - av->top_pad = value;
  6739. - ret = 1;
  6740. - break;
  6741. -
  6742. - case M_MMAP_THRESHOLD:
  6743. - av->mmap_threshold = value;
  6744. - ret = 1;
  6745. - break;
  6746. -
  6747. - case M_MMAP_MAX:
  6748. - av->n_mmaps_max = value;
  6749. - ret = 1;
  6750. - break;
  6751. + case M_MXFAST:
  6752. + if (value >= 0 && value <= MAX_FAST_SIZE) {
  6753. + set_max_fast(av, value);
  6754. + ret = 1;
  6755. + }
  6756. + break;
  6757. +
  6758. + case M_TRIM_THRESHOLD:
  6759. + av->trim_threshold = value;
  6760. + ret = 1;
  6761. + break;
  6762. +
  6763. + case M_TOP_PAD:
  6764. + av->top_pad = value;
  6765. + ret = 1;
  6766. + break;
  6767. +
  6768. + case M_MMAP_THRESHOLD:
  6769. + av->mmap_threshold = value;
  6770. + ret = 1;
  6771. + break;
  6772. +
  6773. + case M_MMAP_MAX:
  6774. + av->n_mmaps_max = value;
  6775. + ret = 1;
  6776. + break;
  6777. }
  6778. - UNLOCK;
  6779. + __MALLOC_UNLOCK;
  6780. return ret;
  6781. }
  6782. diff --git a/libc/stdlib/malloc-standard/memalign.c b/libc/stdlib/malloc-standard/memalign.c
  6783. index bd95362..e78d752 100644
  6784. --- a/libc/stdlib/malloc-standard/memalign.c
  6785. +++ b/libc/stdlib/malloc-standard/memalign.c
  6786. @@ -8,7 +8,7 @@
  6787. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  6788. Note: There may be an updated version of this malloc obtainable at
  6789. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6790. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6791. Check before installing!
  6792. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  6793. @@ -35,6 +35,7 @@ void* memalign(size_t alignment, size_t
  6794. mchunkptr remainder; /* spare room at end to split off */
  6795. unsigned long remainder_size; /* its size */
  6796. size_t size;
  6797. + void *retval;
  6798. /* If need less alignment than we give anyway, just relay to malloc */
  6799. @@ -46,12 +47,12 @@ void* memalign(size_t alignment, size_t
  6800. /* Make sure alignment is power of 2 (in case MINSIZE is not). */
  6801. if ((alignment & (alignment - 1)) != 0) {
  6802. - size_t a = MALLOC_ALIGNMENT * 2;
  6803. - while ((unsigned long)a < (unsigned long)alignment) a <<= 1;
  6804. - alignment = a;
  6805. + size_t a = MALLOC_ALIGNMENT * 2;
  6806. + while ((unsigned long)a < (unsigned long)alignment) a <<= 1;
  6807. + alignment = a;
  6808. }
  6809. - LOCK;
  6810. + __MALLOC_LOCK;
  6811. checked_request2size(bytes, nb);
  6812. /* Strategy: find a spot within that chunk that meets the alignment
  6813. @@ -63,64 +64,67 @@ void* memalign(size_t alignment, size_t
  6814. m = (char*)(malloc(nb + alignment + MINSIZE));
  6815. if (m == 0) {
  6816. - UNLOCK;
  6817. - return 0; /* propagate failure */
  6818. + retval = 0; /* propagate failure */
  6819. + goto DONE;
  6820. }
  6821. p = mem2chunk(m);
  6822. if ((((unsigned long)(m)) % alignment) != 0) { /* misaligned */
  6823. - /*
  6824. - Find an aligned spot inside chunk. Since we need to give back
  6825. - leading space in a chunk of at least MINSIZE, if the first
  6826. - calculation places us at a spot with less than MINSIZE leader,
  6827. - we can move to the next aligned spot -- we've allocated enough
  6828. - total room so that this is always possible.
  6829. - */
  6830. -
  6831. - brk = (char*)mem2chunk((unsigned long)(((unsigned long)(m + alignment - 1)) &
  6832. - -((signed long) alignment)));
  6833. - if ((unsigned long)(brk - (char*)(p)) < MINSIZE)
  6834. - brk += alignment;
  6835. -
  6836. - newp = (mchunkptr)brk;
  6837. - leadsize = brk - (char*)(p);
  6838. - newsize = chunksize(p) - leadsize;
  6839. -
  6840. - /* For mmapped chunks, just adjust offset */
  6841. - if (chunk_is_mmapped(p)) {
  6842. - newp->prev_size = p->prev_size + leadsize;
  6843. - set_head(newp, newsize|IS_MMAPPED);
  6844. - UNLOCK;
  6845. - return chunk2mem(newp);
  6846. - }
  6847. -
  6848. - /* Otherwise, give back leader, use the rest */
  6849. - set_head(newp, newsize | PREV_INUSE);
  6850. - set_inuse_bit_at_offset(newp, newsize);
  6851. - set_head_size(p, leadsize);
  6852. - free(chunk2mem(p));
  6853. - p = newp;
  6854. + /*
  6855. + Find an aligned spot inside chunk. Since we need to give back
  6856. + leading space in a chunk of at least MINSIZE, if the first
  6857. + calculation places us at a spot with less than MINSIZE leader,
  6858. + we can move to the next aligned spot -- we've allocated enough
  6859. + total room so that this is always possible.
  6860. + */
  6861. +
  6862. + brk = (char*)mem2chunk((unsigned long)(((unsigned long)(m + alignment - 1)) &
  6863. + -((signed long) alignment)));
  6864. + if ((unsigned long)(brk - (char*)(p)) < MINSIZE)
  6865. + brk += alignment;
  6866. +
  6867. + newp = (mchunkptr)brk;
  6868. + leadsize = brk - (char*)(p);
  6869. + newsize = chunksize(p) - leadsize;
  6870. +
  6871. + /* For mmapped chunks, just adjust offset */
  6872. + if (chunk_is_mmapped(p)) {
  6873. + newp->prev_size = p->prev_size + leadsize;
  6874. + set_head(newp, newsize|IS_MMAPPED);
  6875. + retval = chunk2mem(newp);
  6876. + goto DONE;
  6877. + }
  6878. +
  6879. + /* Otherwise, give back leader, use the rest */
  6880. + set_head(newp, newsize | PREV_INUSE);
  6881. + set_inuse_bit_at_offset(newp, newsize);
  6882. + set_head_size(p, leadsize);
  6883. + free(chunk2mem(p));
  6884. + p = newp;
  6885. - assert (newsize >= nb &&
  6886. - (((unsigned long)(chunk2mem(p))) % alignment) == 0);
  6887. + assert (newsize >= nb &&
  6888. + (((unsigned long)(chunk2mem(p))) % alignment) == 0);
  6889. }
  6890. /* Also give back spare room at the end */
  6891. if (!chunk_is_mmapped(p)) {
  6892. - size = chunksize(p);
  6893. - if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
  6894. - remainder_size = size - nb;
  6895. - remainder = chunk_at_offset(p, nb);
  6896. - set_head(remainder, remainder_size | PREV_INUSE);
  6897. - set_head_size(p, nb);
  6898. - free(chunk2mem(remainder));
  6899. - }
  6900. + size = chunksize(p);
  6901. + if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
  6902. + remainder_size = size - nb;
  6903. + remainder = chunk_at_offset(p, nb);
  6904. + set_head(remainder, remainder_size | PREV_INUSE);
  6905. + set_head_size(p, nb);
  6906. + free(chunk2mem(remainder));
  6907. + }
  6908. }
  6909. check_inuse_chunk(p);
  6910. - UNLOCK;
  6911. - return chunk2mem(p);
  6912. + retval = chunk2mem(p);
  6913. +
  6914. + DONE:
  6915. + __MALLOC_UNLOCK;
  6916. + return retval;
  6917. }
  6918. diff --git a/libc/stdlib/malloc-standard/realloc.c b/libc/stdlib/malloc-standard/realloc.c
  6919. index 1950130..9ca4b26 100644
  6920. --- a/libc/stdlib/malloc-standard/realloc.c
  6921. +++ b/libc/stdlib/malloc-standard/realloc.c
  6922. @@ -8,7 +8,7 @@
  6923. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  6924. Note: There may be an updated version of this malloc obtainable at
  6925. - ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6926. + ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  6927. Check before installing!
  6928. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  6929. @@ -23,14 +23,14 @@ void* realloc(void* oldmem, size_t bytes
  6930. {
  6931. mstate av;
  6932. - size_t nb; /* padded request size */
  6933. + size_t nb; /* padded request size */
  6934. mchunkptr oldp; /* chunk corresponding to oldmem */
  6935. - size_t oldsize; /* its size */
  6936. + size_t oldsize; /* its size */
  6937. mchunkptr newp; /* chunk to return */
  6938. - size_t newsize; /* its size */
  6939. - void* newmem; /* corresponding user mem */
  6940. + size_t newsize; /* its size */
  6941. + void* newmem; /* corresponding user mem */
  6942. mchunkptr next; /* next contiguous chunk after oldp */
  6943. @@ -40,21 +40,23 @@ void* realloc(void* oldmem, size_t bytes
  6944. mchunkptr bck; /* misc temp for linking */
  6945. mchunkptr fwd; /* misc temp for linking */
  6946. - unsigned long copysize; /* bytes to copy */
  6947. + unsigned long copysize; /* bytes to copy */
  6948. unsigned int ncopies; /* size_t words to copy */
  6949. - size_t* s; /* copy source */
  6950. - size_t* d; /* copy destination */
  6951. + size_t* s; /* copy source */
  6952. + size_t* d; /* copy destination */
  6953. +
  6954. + void *retval;
  6955. /* Check for special cases. */
  6956. if (! oldmem)
  6957. - return malloc(bytes);
  6958. + return malloc(bytes);
  6959. if (! bytes) {
  6960. - free (oldmem);
  6961. - return malloc(bytes);
  6962. + free (oldmem);
  6963. + return malloc(bytes);
  6964. }
  6965. - LOCK;
  6966. + __MALLOC_LOCK;
  6967. av = get_malloc_state();
  6968. checked_request2size(bytes, nb);
  6969. @@ -65,173 +67,176 @@ void* realloc(void* oldmem, size_t bytes
  6970. if (!chunk_is_mmapped(oldp)) {
  6971. - if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
  6972. - /* already big enough; split below */
  6973. - newp = oldp;
  6974. - newsize = oldsize;
  6975. - }
  6976. -
  6977. - else {
  6978. - next = chunk_at_offset(oldp, oldsize);
  6979. -
  6980. - /* Try to expand forward into top */
  6981. - if (next == av->top &&
  6982. - (unsigned long)(newsize = oldsize + chunksize(next)) >=
  6983. - (unsigned long)(nb + MINSIZE)) {
  6984. - set_head_size(oldp, nb);
  6985. - av->top = chunk_at_offset(oldp, nb);
  6986. - set_head(av->top, (newsize - nb) | PREV_INUSE);
  6987. - UNLOCK;
  6988. - return chunk2mem(oldp);
  6989. - }
  6990. -
  6991. - /* Try to expand forward into next chunk; split off remainder below */
  6992. - else if (next != av->top &&
  6993. - !inuse(next) &&
  6994. - (unsigned long)(newsize = oldsize + chunksize(next)) >=
  6995. - (unsigned long)(nb)) {
  6996. - newp = oldp;
  6997. - unlink(next, bck, fwd);
  6998. - }
  6999. -
  7000. - /* allocate, copy, free */
  7001. - else {
  7002. - newmem = malloc(nb - MALLOC_ALIGN_MASK);
  7003. - if (newmem == 0) {
  7004. - UNLOCK;
  7005. - return 0; /* propagate failure */
  7006. - }
  7007. -
  7008. - newp = mem2chunk(newmem);
  7009. - newsize = chunksize(newp);
  7010. -
  7011. - /*
  7012. - Avoid copy if newp is next chunk after oldp.
  7013. - */
  7014. - if (newp == next) {
  7015. - newsize += oldsize;
  7016. - newp = oldp;
  7017. + if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
  7018. + /* already big enough; split below */
  7019. + newp = oldp;
  7020. + newsize = oldsize;
  7021. }
  7022. +
  7023. else {
  7024. - /*
  7025. - Unroll copy of <= 36 bytes (72 if 8byte sizes)
  7026. - We know that contents have an odd number of
  7027. - size_t-sized words; minimally 3.
  7028. - */
  7029. -
  7030. - copysize = oldsize - (sizeof(size_t));
  7031. - s = (size_t*)(oldmem);
  7032. - d = (size_t*)(newmem);
  7033. - ncopies = copysize / sizeof(size_t);
  7034. - assert(ncopies >= 3);
  7035. -
  7036. - if (ncopies > 9)
  7037. - memcpy(d, s, copysize);
  7038. -
  7039. - else {
  7040. - *(d+0) = *(s+0);
  7041. - *(d+1) = *(s+1);
  7042. - *(d+2) = *(s+2);
  7043. - if (ncopies > 4) {
  7044. - *(d+3) = *(s+3);
  7045. - *(d+4) = *(s+4);
  7046. - if (ncopies > 6) {
  7047. - *(d+5) = *(s+5);
  7048. - *(d+6) = *(s+6);
  7049. - if (ncopies > 8) {
  7050. - *(d+7) = *(s+7);
  7051. - *(d+8) = *(s+8);
  7052. + next = chunk_at_offset(oldp, oldsize);
  7053. +
  7054. + /* Try to expand forward into top */
  7055. + if (next == av->top &&
  7056. + (unsigned long)(newsize = oldsize + chunksize(next)) >=
  7057. + (unsigned long)(nb + MINSIZE)) {
  7058. + set_head_size(oldp, nb);
  7059. + av->top = chunk_at_offset(oldp, nb);
  7060. + set_head(av->top, (newsize - nb) | PREV_INUSE);
  7061. + retval = chunk2mem(oldp);
  7062. + goto DONE;
  7063. + }
  7064. +
  7065. + /* Try to expand forward into next chunk; split off remainder below */
  7066. + else if (next != av->top &&
  7067. + !inuse(next) &&
  7068. + (unsigned long)(newsize = oldsize + chunksize(next)) >=
  7069. + (unsigned long)(nb)) {
  7070. + newp = oldp;
  7071. + unlink(next, bck, fwd);
  7072. + }
  7073. +
  7074. + /* allocate, copy, free */
  7075. + else {
  7076. + newmem = malloc(nb - MALLOC_ALIGN_MASK);
  7077. + if (newmem == 0) {
  7078. + retval = 0; /* propagate failure */
  7079. + goto DONE;
  7080. + }
  7081. +
  7082. + newp = mem2chunk(newmem);
  7083. + newsize = chunksize(newp);
  7084. +
  7085. + /*
  7086. + Avoid copy if newp is next chunk after oldp.
  7087. + */
  7088. + if (newp == next) {
  7089. + newsize += oldsize;
  7090. + newp = oldp;
  7091. + }
  7092. + else {
  7093. + /*
  7094. + Unroll copy of <= 36 bytes (72 if 8byte sizes)
  7095. + We know that contents have an odd number of
  7096. + size_t-sized words; minimally 3.
  7097. + */
  7098. +
  7099. + copysize = oldsize - (sizeof(size_t));
  7100. + s = (size_t*)(oldmem);
  7101. + d = (size_t*)(newmem);
  7102. + ncopies = copysize / sizeof(size_t);
  7103. + assert(ncopies >= 3);
  7104. +
  7105. + if (ncopies > 9)
  7106. + memcpy(d, s, copysize);
  7107. +
  7108. + else {
  7109. + *(d+0) = *(s+0);
  7110. + *(d+1) = *(s+1);
  7111. + *(d+2) = *(s+2);
  7112. + if (ncopies > 4) {
  7113. + *(d+3) = *(s+3);
  7114. + *(d+4) = *(s+4);
  7115. + if (ncopies > 6) {
  7116. + *(d+5) = *(s+5);
  7117. + *(d+6) = *(s+6);
  7118. + if (ncopies > 8) {
  7119. + *(d+7) = *(s+7);
  7120. + *(d+8) = *(s+8);
  7121. + }
  7122. + }
  7123. + }
  7124. + }
  7125. +
  7126. + free(oldmem);
  7127. + check_inuse_chunk(newp);
  7128. + retval = chunk2mem(newp);
  7129. + goto DONE;
  7130. }
  7131. - }
  7132. }
  7133. - }
  7134. + }
  7135. +
  7136. + /* If possible, free extra space in old or extended chunk */
  7137. +
  7138. + assert((unsigned long)(newsize) >= (unsigned long)(nb));
  7139. +
  7140. + remainder_size = newsize - nb;
  7141. - free(oldmem);
  7142. - check_inuse_chunk(newp);
  7143. - UNLOCK;
  7144. - return chunk2mem(newp);
  7145. - }
  7146. - }
  7147. - }
  7148. -
  7149. - /* If possible, free extra space in old or extended chunk */
  7150. -
  7151. - assert((unsigned long)(newsize) >= (unsigned long)(nb));
  7152. -
  7153. - remainder_size = newsize - nb;
  7154. -
  7155. - if (remainder_size < MINSIZE) { /* not enough extra to split off */
  7156. - set_head_size(newp, newsize);
  7157. - set_inuse_bit_at_offset(newp, newsize);
  7158. - }
  7159. - else { /* split remainder */
  7160. - remainder = chunk_at_offset(newp, nb);
  7161. - set_head_size(newp, nb);
  7162. - set_head(remainder, remainder_size | PREV_INUSE);
  7163. - /* Mark remainder as inuse so free() won't complain */
  7164. - set_inuse_bit_at_offset(remainder, remainder_size);
  7165. - free(chunk2mem(remainder));
  7166. - }
  7167. -
  7168. - check_inuse_chunk(newp);
  7169. - UNLOCK;
  7170. - return chunk2mem(newp);
  7171. + if (remainder_size < MINSIZE) { /* not enough extra to split off */
  7172. + set_head_size(newp, newsize);
  7173. + set_inuse_bit_at_offset(newp, newsize);
  7174. + }
  7175. + else { /* split remainder */
  7176. + remainder = chunk_at_offset(newp, nb);
  7177. + set_head_size(newp, nb);
  7178. + set_head(remainder, remainder_size | PREV_INUSE);
  7179. + /* Mark remainder as inuse so free() won't complain */
  7180. + set_inuse_bit_at_offset(remainder, remainder_size);
  7181. + free(chunk2mem(remainder));
  7182. + }
  7183. +
  7184. + check_inuse_chunk(newp);
  7185. + retval = chunk2mem(newp);
  7186. + goto DONE;
  7187. }
  7188. /*
  7189. - Handle mmap cases
  7190. - */
  7191. + Handle mmap cases
  7192. + */
  7193. else {
  7194. - size_t offset = oldp->prev_size;
  7195. - size_t pagemask = av->pagesize - 1;
  7196. - char *cp;
  7197. - unsigned long sum;
  7198. -
  7199. - /* Note the extra (sizeof(size_t)) overhead */
  7200. - newsize = (nb + offset + (sizeof(size_t)) + pagemask) & ~pagemask;
  7201. -
  7202. - /* don't need to remap if still within same page */
  7203. - if (oldsize == newsize - offset) {
  7204. - UNLOCK;
  7205. - return oldmem;
  7206. - }
  7207. -
  7208. - cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
  7209. -
  7210. - if (cp != (char*)MORECORE_FAILURE) {
  7211. -
  7212. - newp = (mchunkptr)(cp + offset);
  7213. - set_head(newp, (newsize - offset)|IS_MMAPPED);
  7214. -
  7215. - assert(aligned_OK(chunk2mem(newp)));
  7216. - assert((newp->prev_size == offset));
  7217. -
  7218. - /* update statistics */
  7219. - sum = av->mmapped_mem += newsize - oldsize;
  7220. - if (sum > (unsigned long)(av->max_mmapped_mem))
  7221. - av->max_mmapped_mem = sum;
  7222. - sum += av->sbrked_mem;
  7223. - if (sum > (unsigned long)(av->max_total_mem))
  7224. - av->max_total_mem = sum;
  7225. -
  7226. - UNLOCK;
  7227. - return chunk2mem(newp);
  7228. - }
  7229. -
  7230. - /* Note the extra (sizeof(size_t)) overhead. */
  7231. - if ((unsigned long)(oldsize) >= (unsigned long)(nb + (sizeof(size_t))))
  7232. - newmem = oldmem; /* do nothing */
  7233. - else {
  7234. - /* Must alloc, copy, free. */
  7235. - newmem = malloc(nb - MALLOC_ALIGN_MASK);
  7236. - if (newmem != 0) {
  7237. - memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
  7238. - free(oldmem);
  7239. - }
  7240. - }
  7241. - UNLOCK;
  7242. - return newmem;
  7243. + size_t offset = oldp->prev_size;
  7244. + size_t pagemask = av->pagesize - 1;
  7245. + char *cp;
  7246. + unsigned long sum;
  7247. +
  7248. + /* Note the extra (sizeof(size_t)) overhead */
  7249. + newsize = (nb + offset + (sizeof(size_t)) + pagemask) & ~pagemask;
  7250. +
  7251. + /* don't need to remap if still within same page */
  7252. + if (oldsize == newsize - offset) {
  7253. + retval = oldmem;
  7254. + goto DONE;
  7255. + }
  7256. +
  7257. + cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
  7258. +
  7259. + if (cp != (char*)MORECORE_FAILURE) {
  7260. +
  7261. + newp = (mchunkptr)(cp + offset);
  7262. + set_head(newp, (newsize - offset)|IS_MMAPPED);
  7263. +
  7264. + assert(aligned_OK(chunk2mem(newp)));
  7265. + assert((newp->prev_size == offset));
  7266. +
  7267. + /* update statistics */
  7268. + sum = av->mmapped_mem += newsize - oldsize;
  7269. + if (sum > (unsigned long)(av->max_mmapped_mem))
  7270. + av->max_mmapped_mem = sum;
  7271. + sum += av->sbrked_mem;
  7272. + if (sum > (unsigned long)(av->max_total_mem))
  7273. + av->max_total_mem = sum;
  7274. +
  7275. + retval = chunk2mem(newp);
  7276. + goto DONE;
  7277. + }
  7278. +
  7279. + /* Note the extra (sizeof(size_t)) overhead. */
  7280. + if ((unsigned long)(oldsize) >= (unsigned long)(nb + (sizeof(size_t))))
  7281. + newmem = oldmem; /* do nothing */
  7282. + else {
  7283. + /* Must alloc, copy, free. */
  7284. + newmem = malloc(nb - MALLOC_ALIGN_MASK);
  7285. + if (newmem != 0) {
  7286. + memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
  7287. + free(oldmem);
  7288. + }
  7289. + }
  7290. + retval = newmem;
  7291. }
  7292. +
  7293. + DONE:
  7294. + __MALLOC_UNLOCK;
  7295. + return retval;
  7296. }
  7297. diff --git a/libc/stdlib/random.c b/libc/stdlib/random.c
  7298. index b0a00e1..1bd63bc 100644
  7299. --- a/libc/stdlib/random.c
  7300. +++ b/libc/stdlib/random.c
  7301. @@ -27,16 +27,14 @@
  7302. #include <limits.h>
  7303. #include <stddef.h>
  7304. #include <stdlib.h>
  7305. -#ifdef __UCLIBC_HAS_THREADS__
  7306. -#include <pthread.h>
  7307. +
  7308. /* POSIX.1c requires that there is mutual exclusion for the `rand' and
  7309. `srand' functions to prevent concurrent calls from modifying common
  7310. data. */
  7311. -static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  7312. -#else
  7313. -#define __pthread_mutex_lock(x)
  7314. -#define __pthread_mutex_unlock(x)
  7315. -#endif
  7316. +
  7317. +#include <bits/uClibc_mutex.h>
  7318. +
  7319. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  7320. /* An improved random number generation package. In addition to the standard
  7321. rand()/srand() like interface, this package also has a special state info
  7322. @@ -184,9 +182,9 @@ static struct random_data unsafe_state =
  7323. for default usage relies on values produced by this routine. */
  7324. void srandom (unsigned int x)
  7325. {
  7326. - __pthread_mutex_lock(&lock);
  7327. + __UCLIBC_MUTEX_LOCK(mylock);
  7328. srandom_r (x, &unsafe_state);
  7329. - __pthread_mutex_unlock(&lock);
  7330. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7331. }
  7332. weak_alias (srandom, srand)
  7333. @@ -205,10 +203,10 @@ char * initstate (unsigned int seed, cha
  7334. {
  7335. int32_t *ostate;
  7336. - __pthread_mutex_lock(&lock);
  7337. + __UCLIBC_MUTEX_LOCK(mylock);
  7338. ostate = &unsafe_state.state[-1];
  7339. initstate_r (seed, arg_state, n, &unsafe_state);
  7340. - __pthread_mutex_unlock(&lock);
  7341. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7342. return (char *) ostate;
  7343. }
  7344. @@ -224,11 +222,11 @@ char * setstate (char *arg_state)
  7345. {
  7346. int32_t *ostate;
  7347. - __pthread_mutex_lock(&lock);
  7348. + __UCLIBC_MUTEX_LOCK(mylock);
  7349. ostate = &unsafe_state.state[-1];
  7350. if (setstate_r (arg_state, &unsafe_state) < 0)
  7351. ostate = NULL;
  7352. - __pthread_mutex_unlock(&lock);
  7353. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7354. return (char *) ostate;
  7355. }
  7356. @@ -247,9 +245,9 @@ long int random ()
  7357. {
  7358. int32_t retval;
  7359. - __pthread_mutex_lock(&lock);
  7360. + __UCLIBC_MUTEX_LOCK(mylock);
  7361. random_r (&unsafe_state, &retval);
  7362. - __pthread_mutex_unlock(&lock);
  7363. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7364. return retval;
  7365. }
  7366. diff --git a/libc/stdlib/setenv.c b/libc/stdlib/setenv.c
  7367. index d0cfe52..2d899cc 100644
  7368. --- a/libc/stdlib/setenv.c
  7369. +++ b/libc/stdlib/setenv.c
  7370. @@ -17,7 +17,7 @@
  7371. 02111-1307 USA.
  7372. modified for uClibc by Erik Andersen <andersen@codepoet.org>
  7373. - */
  7374. +*/
  7375. #define _GNU_SOURCE
  7376. #include <features.h>
  7377. @@ -26,16 +26,9 @@
  7378. #include <string.h>
  7379. #include <unistd.h>
  7380. -#ifdef __UCLIBC_HAS_THREADS__
  7381. -#include <pthread.h>
  7382. -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  7383. -# define LOCK __pthread_mutex_lock(&mylock)
  7384. -# define UNLOCK __pthread_mutex_unlock(&mylock);
  7385. -#else
  7386. -# define LOCK
  7387. -# define UNLOCK
  7388. -#endif
  7389. +#include <bits/uClibc_mutex.h>
  7390. +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  7391. /* If this variable is not a null pointer we allocated the current
  7392. environment. */
  7393. @@ -49,14 +42,15 @@ static char **last_environ;
  7394. to reuse values once generated for a `setenv' call since we can never
  7395. free the strings. */
  7396. int __add_to_environ (const char *name, const char *value,
  7397. - const char *combined, int replace)
  7398. + const char *combined, int replace)
  7399. {
  7400. register char **ep;
  7401. register size_t size;
  7402. const size_t namelen = strlen (name);
  7403. const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
  7404. + int rv = -1;
  7405. - LOCK;
  7406. + __UCLIBC_MUTEX_LOCK(mylock);
  7407. /* We have to get the pointer now that we have the lock and not earlier
  7408. since another thread might have created a new environment. */
  7409. @@ -64,72 +58,72 @@ int __add_to_environ (const char *name,
  7410. size = 0;
  7411. if (ep != NULL) {
  7412. - for (; *ep != NULL; ++ep) {
  7413. - if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  7414. - break;
  7415. - else
  7416. - ++size;
  7417. - }
  7418. + for (; *ep != NULL; ++ep) {
  7419. + if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  7420. + break;
  7421. + else
  7422. + ++size;
  7423. + }
  7424. }
  7425. if (ep == NULL || *ep == NULL) {
  7426. - char **new_environ;
  7427. + char **new_environ;
  7428. - /* We allocated this space; we can extend it. */
  7429. - new_environ = (char **) realloc (last_environ,
  7430. - (size + 2) * sizeof (char *));
  7431. - if (new_environ == NULL) {
  7432. - UNLOCK;
  7433. - return -1;
  7434. - }
  7435. -
  7436. - /* If the whole entry is given add it. */
  7437. - if (combined != NULL) {
  7438. - /* We must not add the string to the search tree since it belongs
  7439. - to the user. */
  7440. - new_environ[size] = (char *) combined;
  7441. - } else {
  7442. - /* See whether the value is already known. */
  7443. - new_environ[size] = (char *) malloc (namelen + 1 + vallen);
  7444. - if (new_environ[size] == NULL) {
  7445. - __set_errno (ENOMEM);
  7446. - UNLOCK;
  7447. - return -1;
  7448. - }
  7449. -
  7450. - memcpy (new_environ[size], name, namelen);
  7451. - new_environ[size][namelen] = '=';
  7452. - memcpy (&new_environ[size][namelen + 1], value, vallen);
  7453. - }
  7454. -
  7455. - if (__environ != last_environ) {
  7456. - memcpy ((char *) new_environ, (char *) __environ,
  7457. - size * sizeof (char *));
  7458. - }
  7459. + /* We allocated this space; we can extend it. */
  7460. + new_environ = (char **) realloc (last_environ,
  7461. + (size + 2) * sizeof (char *));
  7462. + if (new_environ == NULL) {
  7463. + goto DONE;
  7464. + }
  7465. +
  7466. + /* If the whole entry is given add it. */
  7467. + if (combined != NULL) {
  7468. + /* We must not add the string to the search tree since it belongs
  7469. + to the user. */
  7470. + new_environ[size] = (char *) combined;
  7471. + } else {
  7472. + /* See whether the value is already known. */
  7473. + new_environ[size] = (char *) malloc (namelen + 1 + vallen);
  7474. + if (new_environ[size] == NULL) {
  7475. + __set_errno (ENOMEM);
  7476. + goto DONE;
  7477. + }
  7478. +
  7479. + memcpy (new_environ[size], name, namelen);
  7480. + new_environ[size][namelen] = '=';
  7481. + memcpy (&new_environ[size][namelen + 1], value, vallen);
  7482. + }
  7483. +
  7484. + if (__environ != last_environ) {
  7485. + memcpy ((char *) new_environ, (char *) __environ,
  7486. + size * sizeof (char *));
  7487. + }
  7488. - new_environ[size + 1] = NULL;
  7489. - last_environ = __environ = new_environ;
  7490. + new_environ[size + 1] = NULL;
  7491. + last_environ = __environ = new_environ;
  7492. } else if (replace) {
  7493. - char *np;
  7494. + char *np;
  7495. - /* Use the user string if given. */
  7496. - if (combined != NULL) {
  7497. - np = (char *) combined;
  7498. - } else {
  7499. - np = malloc (namelen + 1 + vallen);
  7500. - if (np == NULL) {
  7501. - UNLOCK;
  7502. - return -1;
  7503. - }
  7504. - memcpy (np, name, namelen);
  7505. - np[namelen] = '=';
  7506. - memcpy (&np[namelen + 1], value, vallen);
  7507. - }
  7508. - *ep = np;
  7509. - }
  7510. -
  7511. - UNLOCK;
  7512. - return 0;
  7513. + /* Use the user string if given. */
  7514. + if (combined != NULL) {
  7515. + np = (char *) combined;
  7516. + } else {
  7517. + np = malloc (namelen + 1 + vallen);
  7518. + if (np == NULL) {
  7519. + goto DONE;
  7520. + }
  7521. + memcpy (np, name, namelen);
  7522. + np[namelen] = '=';
  7523. + memcpy (&np[namelen + 1], value, vallen);
  7524. + }
  7525. + *ep = np;
  7526. + }
  7527. +
  7528. + rv = 0;
  7529. +
  7530. + DONE:
  7531. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7532. + return rv;
  7533. }
  7534. int setenv (const char *name, const char *value, int replace)
  7535. @@ -143,26 +137,26 @@ int unsetenv (const char *name)
  7536. char **ep;
  7537. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) {
  7538. - __set_errno (EINVAL);
  7539. - return -1;
  7540. + __set_errno (EINVAL);
  7541. + return -1;
  7542. }
  7543. len = strlen (name);
  7544. - LOCK;
  7545. + __UCLIBC_MUTEX_LOCK(mylock);
  7546. ep = __environ;
  7547. while (*ep != NULL) {
  7548. - if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
  7549. - /* Found it. Remove this pointer by moving later ones back. */
  7550. - char **dp = ep;
  7551. - do {
  7552. - dp[0] = dp[1];
  7553. - } while (*dp++);
  7554. - /* Continue the loop in case NAME appears again. */
  7555. - } else {
  7556. - ++ep;
  7557. - }
  7558. + if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
  7559. + /* Found it. Remove this pointer by moving later ones back. */
  7560. + char **dp = ep;
  7561. + do {
  7562. + dp[0] = dp[1];
  7563. + } while (*dp++);
  7564. + /* Continue the loop in case NAME appears again. */
  7565. + } else {
  7566. + ++ep;
  7567. + }
  7568. }
  7569. - UNLOCK;
  7570. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7571. return 0;
  7572. }
  7573. @@ -171,15 +165,15 @@ int unsetenv (const char *name)
  7574. for Fortran 77) requires this function. */
  7575. int clearenv (void)
  7576. {
  7577. - LOCK;
  7578. + __UCLIBC_MUTEX_LOCK(mylock);
  7579. if (__environ == last_environ && __environ != NULL) {
  7580. - /* We allocated this environment so we can free it. */
  7581. - free (__environ);
  7582. - last_environ = NULL;
  7583. + /* We allocated this environment so we can free it. */
  7584. + free (__environ);
  7585. + last_environ = NULL;
  7586. }
  7587. /* Clear the environment pointer removes the whole environment. */
  7588. __environ = NULL;
  7589. - UNLOCK;
  7590. + __UCLIBC_MUTEX_UNLOCK(mylock);
  7591. return 0;
  7592. }
  7593. @@ -190,10 +184,10 @@ int putenv (char *string)
  7594. const char *const name_end = strchr (string, '=');
  7595. if (name_end != NULL) {
  7596. - char *name = strndup(string, name_end - string);
  7597. - result = __add_to_environ (name, NULL, string, 1);
  7598. - free(name);
  7599. - return(result);
  7600. + char *name = strndup(string, name_end - string);
  7601. + result = __add_to_environ (name, NULL, string, 1);
  7602. + free(name);
  7603. + return(result);
  7604. }
  7605. unsetenv (string);
  7606. return 0;
  7607. diff --git a/libc/sysdeps/linux/common/bits/uClibc_stdio.h b/libc/sysdeps/linux/common/bits/uClibc_stdio.h
  7608. index 40cd5fe..3c6911e 100644
  7609. --- a/libc/sysdeps/linux/common/bits/uClibc_stdio.h
  7610. +++ b/libc/sysdeps/linux/common/bits/uClibc_stdio.h
  7611. @@ -116,9 +116,7 @@
  7612. #endif
  7613. /**********************************************************************/
  7614. -#ifdef __UCLIBC_HAS_THREADS__
  7615. -/* Need this for pthread_mutex_t. */
  7616. -#include <bits/pthreadtypes.h>
  7617. +#include <bits/uClibc_mutex.h>
  7618. /* user_locking
  7619. * 0 : do auto locking/unlocking
  7620. @@ -132,43 +130,37 @@
  7621. * This way, we avoid calling the weak lock/unlock functions.
  7622. */
  7623. -#define __STDIO_AUTO_THREADLOCK_VAR int __infunc_user_locking
  7624. -
  7625. -#define __STDIO_AUTO_THREADLOCK(__stream) \
  7626. - if ((__infunc_user_locking = (__stream)->__user_locking) == 0) { \
  7627. - __pthread_mutex_lock(&(__stream)->__lock); \
  7628. - }
  7629. -
  7630. -#define __STDIO_AUTO_THREADUNLOCK(__stream) \
  7631. - if (__infunc_user_locking == 0) { \
  7632. - __pthread_mutex_unlock(&(__stream)->__lock); \
  7633. - }
  7634. +#define __STDIO_AUTO_THREADLOCK_VAR \
  7635. + __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
  7636. -#define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
  7637. +#define __STDIO_AUTO_THREADLOCK(__stream) \
  7638. + __UCLIBC_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
  7639. + (__stream)->__user_locking)
  7640. -#define __STDIO_ALWAYS_THREADLOCK(__stream) \
  7641. - __pthread_mutex_lock(&(__stream)->__lock)
  7642. +#define __STDIO_AUTO_THREADUNLOCK(__stream) \
  7643. + __UCLIBC_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
  7644. -#define __STDIO_ALWAYS_THREADTRYLOCK(__stream) \
  7645. - __pthread_mutex_trylock(&(__stream)->__lock)
  7646. +#define __STDIO_ALWAYS_THREADLOCK(__stream) \
  7647. + __UCLIBC_MUTEX_LOCK((__stream)->__lock)
  7648. -#define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
  7649. - __pthread_mutex_unlock(&(__stream)->__lock)
  7650. +#define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
  7651. + __UCLIBC_MUTEX_UNLOCK((__stream)->__lock)
  7652. -#else /* __UCLIBC_HAS_THREADS__ */
  7653. +#define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
  7654. + __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
  7655. -#define __STDIO_AUTO_THREADLOCK_VAR ((void)0)
  7656. +#define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
  7657. + __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
  7658. -#define __STDIO_AUTO_THREADLOCK(__stream) ((void)0)
  7659. -#define __STDIO_AUTO_THREADUNLOCK(__stream) ((void)0)
  7660. +#define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
  7661. + __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
  7662. +#ifdef __UCLIBC_HAS_THREADS__
  7663. +#define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
  7664. +#else
  7665. #define __STDIO_SET_USER_LOCKING(__stream) ((void)0)
  7666. +#endif
  7667. -#define __STDIO_ALWAYS_THREADLOCK(__stream) ((void)0)
  7668. -#define __STDIO_ALWAYS_THREADTRYLOCK(__stream) (0) /* Always succeed. */
  7669. -#define __STDIO_ALWAYS_THREADUNLOCK(__stream) ((void)0)
  7670. -
  7671. -#endif /* __UCLIBC_HAS_THREADS__ */
  7672. /**********************************************************************/
  7673. #define __STDIO_IOFBF 0 /* Fully buffered. */
  7674. @@ -283,7 +275,7 @@ struct __STDIO_FILE_STRUCT {
  7675. #endif
  7676. #ifdef __UCLIBC_HAS_THREADS__
  7677. int __user_locking;
  7678. - pthread_mutex_t __lock;
  7679. + __UCLIBC_MUTEX(__lock);
  7680. #endif
  7681. /* Everything after this is unimplemented... and may be trashed. */
  7682. #if __STDIO_BUILTIN_BUF_SIZE > 0
  7683. @@ -358,10 +350,14 @@ extern void _stdio_term(void);
  7684. extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
  7685. #ifdef __UCLIBC_HAS_THREADS__
  7686. -extern pthread_mutex_t _stdio_openlist_lock;
  7687. -extern int _stdio_openlist_delflag;
  7688. +__UCLIBC_MUTEX_EXTERN(_stdio_openlist_add_lock);
  7689. +#ifdef __STDIO_BUFFERS
  7690. +__UCLIBC_MUTEX_EXTERN(_stdio_openlist_del_lock);
  7691. +extern volatile int _stdio_openlist_use_count; /* _stdio_openlist_del_lock */
  7692. +extern int _stdio_openlist_del_count; /* _stdio_openlist_del_lock */
  7693. +#endif
  7694. extern int _stdio_user_locking;
  7695. -extern void __stdio_init_mutex(pthread_mutex_t *m);
  7696. +extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m);
  7697. #endif
  7698. #endif
  7699. diff --git a/libc/sysdeps/linux/common/getdents.c b/libc/sysdeps/linux/common/getdents.c
  7700. index ab6a276..23463e5 100644
  7701. --- a/libc/sysdeps/linux/common/getdents.c
  7702. +++ b/libc/sysdeps/linux/common/getdents.c
  7703. @@ -30,8 +30,6 @@
  7704. #include <sys/syscall.h>
  7705. -#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  7706. -
  7707. struct kernel_dirent
  7708. {
  7709. long d_ino;
  7710. diff --git a/libc/sysdeps/linux/common/sigprocmask.c b/libc/sysdeps/linux/common/sigprocmask.c
  7711. index 70ff366..565318d 100644
  7712. --- a/libc/sysdeps/linux/common/sigprocmask.c
  7713. +++ b/libc/sysdeps/linux/common/sigprocmask.c
  7714. @@ -23,6 +23,8 @@ int sigprocmask(int how, const sigset_t
  7715. if (set &&
  7716. #if (SIG_BLOCK == 0) && (SIG_UNBLOCK == 1) && (SIG_SETMASK == 2)
  7717. (((unsigned int) how) > 2)
  7718. +#elif (SIG_BLOCK == 1) && (SIG_UNBLOCK == 2) && (SIG_SETMASK == 3)
  7719. + (((unsigned int)(how-1)) > 2)
  7720. #else
  7721. #warning "compile time assumption violated.. slow path..."
  7722. ((how != SIG_BLOCK) && (how != SIG_UNBLOCK)
  7723. @@ -48,6 +50,8 @@ int sigprocmask(int how, const sigset_t
  7724. if (set &&
  7725. #if (SIG_BLOCK == 0) && (SIG_UNBLOCK == 1) && (SIG_SETMASK == 2)
  7726. (((unsigned int) how) > 2)
  7727. +#elif (SIG_BLOCK == 1) && (SIG_UNBLOCK == 2) && (SIG_SETMASK == 3)
  7728. + (((unsigned int)(how-1)) > 2)
  7729. #else
  7730. #warning "compile time assumption violated.. slow path..."
  7731. ((how != SIG_BLOCK) && (how != SIG_UNBLOCK)
  7732. diff --git a/libc/sysdeps/linux/mips/bits/kernel_sigaction.h b/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
  7733. index b6f52cc..317e5b3 100644
  7734. --- a/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
  7735. +++ b/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
  7736. @@ -38,3 +38,6 @@ struct kernel_sigaction {
  7737. void (*sa_restorer)(void);
  7738. int s_resv[1]; /* reserved */
  7739. };
  7740. +
  7741. +extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *__unbounded,
  7742. + struct kernel_sigaction *__unbounded, size_t);
  7743. diff --git a/libc/sysdeps/linux/mips/pipe.S b/libc/sysdeps/linux/mips/pipe.S
  7744. index c3afae5..cd88074 100644
  7745. --- a/libc/sysdeps/linux/mips/pipe.S
  7746. +++ b/libc/sysdeps/linux/mips/pipe.S
  7747. @@ -7,25 +7,36 @@
  7748. #include <asm/unistd.h>
  7749. #include <asm/regdef.h>
  7750. - .globl pipe
  7751. - .ent pipe, 0
  7752. + .globl pipe
  7753. + .ent pipe, 0
  7754. pipe:
  7755. - addiu sp,sp,-24
  7756. - sw a0,16(sp)
  7757. - li v0,__NR_pipe
  7758. - syscall
  7759. - beqz a3, 1f
  7760. - la t3, errno
  7761. - sw v0, (t3)
  7762. - li v0, -1
  7763. - b 2f
  7764. + .frame sp, 24, sp
  7765. +#ifdef __PIC__
  7766. + .set noreorder
  7767. + .cpload $25
  7768. + .set reorder
  7769. + addiu sp,sp,-24
  7770. + .cprestore 16
  7771. +#else
  7772. + addiu sp,sp,-24
  7773. +#endif
  7774. + sw a0,16(sp)
  7775. + li v0,__NR_pipe
  7776. + syscall
  7777. + beqz a3, 1f
  7778. +#ifdef __PIC__
  7779. + la t0, __syscall_error
  7780. + jr t9
  7781. +#else
  7782. + j __syscall_error
  7783. +#endif
  7784. 1:
  7785. - lw a0, 16(sp)
  7786. - sw v0, 0(a0)
  7787. - sw v1, 4(a0)
  7788. - li v0, 0
  7789. + lw a0, 16(sp)
  7790. + sw v0, 0(a0)
  7791. + sw v1, 4(a0)
  7792. + li v0, 0
  7793. 2:
  7794. - addiu sp,sp,24
  7795. - j ra
  7796. - .end pipe
  7797. - .size pipe,.-pipe
  7798. + addiu sp,sp,24
  7799. + j ra
  7800. + .end pipe
  7801. + .size pipe,.-pipe
  7802. diff --git a/libcrypt/des.c b/libcrypt/des.c
  7803. index 3b49a7a..f7a6be1 100644
  7804. --- a/libcrypt/des.c
  7805. +++ b/libcrypt/des.c
  7806. @@ -504,7 +504,7 @@ do_des( u_int32_t l_in, u_int32_t r_in,
  7807. kl = kl1;
  7808. kr = kr1;
  7809. round = 16;
  7810. - while (round--) {
  7811. + do {
  7812. /*
  7813. * Expand R to 48 bits (simulate the E-box).
  7814. */
  7815. @@ -540,7 +540,7 @@ do_des( u_int32_t l_in, u_int32_t r_in,
  7816. f ^= l;
  7817. l = r;
  7818. r = f;
  7819. - }
  7820. + } while (--round);
  7821. r = l;
  7822. l = f;
  7823. }
  7824. diff --git a/libpthread/linuxthreads/ptfork.c b/libpthread/linuxthreads/ptfork.c
  7825. index eb544f3..cfec2b7 100644
  7826. --- a/libpthread/linuxthreads/ptfork.c
  7827. +++ b/libpthread/linuxthreads/ptfork.c
  7828. @@ -26,6 +26,15 @@
  7829. #include "pthread.h"
  7830. #include "internals.h"
  7831. +#warning hack alert... should be sufficent for system(), but what about other libc mutexes?
  7832. +#include <bits/uClibc_mutex.h>
  7833. +
  7834. +__UCLIBC_MUTEX_EXTERN(__malloc_lock);
  7835. +
  7836. +#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  7837. +#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  7838. +#warning hack alert block end
  7839. +
  7840. struct handler_list {
  7841. void (*handler)(void);
  7842. struct handler_list * next;
  7843. @@ -91,9 +100,18 @@ pid_t __fork(void)
  7844. parent = pthread_atfork_parent;
  7845. pthread_mutex_unlock(&pthread_atfork_lock);
  7846. pthread_call_handlers(prepare);
  7847. +
  7848. +#warning hack alert
  7849. + __MALLOC_LOCK;
  7850. +
  7851. pid = __libc_fork();
  7852. +
  7853. +#warning hack alert
  7854. + __MALLOC_UNLOCK;
  7855. +
  7856. if (pid == 0) {
  7857. __pthread_reset_main_thread();
  7858. +#warning need to reconsider __fresetlockfiles!
  7859. __fresetlockfiles();
  7860. pthread_call_handlers(child);
  7861. } else {
  7862. diff -urN -x .git uClibc-0.9.28/libc/sysdeps/linux/common/bits/uClibc_mutex.h uClibc-mjn3/libc/sysdeps/linux/common/bits/uClibc_mutex.h
  7863. --- uClibc-0.9.28/libc/sysdeps/linux/common/bits/uClibc_mutex.h 1969-12-31 17:00:00.000000000 -0700
  7864. +++ uClibc-mjn3/libc/sysdeps/linux/common/bits/uClibc_mutex.h 2006-03-08 11:21:58.000000000 -0700
  7865. @@ -0,0 +1,87 @@
  7866. +/* Copyright (C) 2006 Manuel Novoa III <mjn3@codepoet.org>
  7867. + *
  7868. + * GNU Library General Public License (LGPL) version 2 or later.
  7869. + *
  7870. + * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  7871. + */
  7872. +
  7873. +#ifndef _UCLIBC_MUTEX_H
  7874. +#define _UCLIBC_MUTEX_H
  7875. +
  7876. +#include <features.h>
  7877. +
  7878. +#ifdef __UCLIBC_HAS_THREADS__
  7879. +
  7880. +#include <pthread.h>
  7881. +
  7882. +#define __UCLIBC_MUTEX_TYPE pthread_mutex_t
  7883. +
  7884. +#define __UCLIBC_MUTEX(M) pthread_mutex_t M
  7885. +#define __UCLIBC_MUTEX_INIT(M,I) pthread_mutex_t M = I
  7886. +#define __UCLIBC_MUTEX_STATIC(M,I) static pthread_mutex_t M = I
  7887. +#define __UCLIBC_MUTEX_EXTERN(M) extern pthread_mutex_t M
  7888. +
  7889. +#define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) \
  7890. + __pthread_mutex_lock(&(M))
  7891. +
  7892. +#define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) \
  7893. + __pthread_mutex_unlock(&(M))
  7894. +
  7895. +#define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
  7896. + __pthread_mutex_trylock(&(M))
  7897. +
  7898. +#define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) \
  7899. + do { \
  7900. + struct _pthread_cleanup_buffer __infunc_pthread_cleanup_buffer; \
  7901. + if (C) { \
  7902. + _pthread_cleanup_push_defer(&__infunc_pthread_cleanup_buffer, \
  7903. + __pthread_mutex_unlock, \
  7904. + &(M)); \
  7905. + __pthread_mutex_lock(&(M)); \
  7906. + } \
  7907. + ((void)0)
  7908. +
  7909. +#define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) \
  7910. + if (C) { \
  7911. + _pthread_cleanup_pop_restore(&__infunc_pthread_cleanup_buffer,1);\
  7912. + } \
  7913. + } while (0)
  7914. +
  7915. +#define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) int A
  7916. +
  7917. +#define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) \
  7918. + __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,((A=(V)) == 0))
  7919. +
  7920. +#define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) \
  7921. + __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,(A == 0))
  7922. +
  7923. +#define __UCLIBC_MUTEX_LOCK(M) \
  7924. + __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  7925. +
  7926. +#define __UCLIBC_MUTEX_UNLOCK(M) \
  7927. + __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  7928. +
  7929. +#else
  7930. +
  7931. +#define __UCLIBC_MUTEX(M) void *__UCLIBC_MUTEX_DUMMY_ ## M
  7932. +#define __UCLIBC_MUTEX_INIT(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  7933. +#define __UCLIBC_MUTEX_STATIC(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  7934. +#define __UCLIBC_MUTEX_EXTERN(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  7935. +
  7936. +#define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) ((void)0)
  7937. +#define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) ((void)0)
  7938. +#define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) (0) /* Always succeed? */
  7939. +
  7940. +#define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) ((void)0)
  7941. +#define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) ((void)0)
  7942. +
  7943. +#define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) ((void)0)
  7944. +#define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) ((void)0)
  7945. +#define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) ((void)0)
  7946. +
  7947. +#define __UCLIBC_MUTEX_LOCK(M) ((void)0)
  7948. +#define __UCLIBC_MUTEX_UNLOCK(M) ((void)0)
  7949. +
  7950. +#endif
  7951. +
  7952. +#endif /* _UCLIBC_MUTEX_H */
  7953. diff -urN -x .git uClibc-0.9.28/libc/sysdeps/linux/mips/pipe.c uClibc-mjn3/libc/sysdeps/linux/mips/pipe.c
  7954. --- uClibc-0.9.28/libc/sysdeps/linux/mips/pipe.c 2005-08-17 16:49:44.000000000 -0600
  7955. +++ uClibc-mjn3/libc/sysdeps/linux/mips/pipe.c 1969-12-31 17:00:00.000000000 -0700
  7956. @@ -1,23 +0,0 @@
  7957. -/* pipe system call for Linux/MIPS */
  7958. -
  7959. -/*see uClibc's sh/pipe.c and glibc-2.2.4's mips/pipe.S */
  7960. -
  7961. -#include <errno.h>
  7962. -#include <unistd.h>
  7963. -#include <syscall.h>
  7964. -
  7965. -int pipe(int *fd)
  7966. -{
  7967. - register long int res __asm__ ("$2"); // v0
  7968. - register long int res2 __asm__ ("$3"); // v1
  7969. -
  7970. - asm ("move\t$4,%2\n\t" // $4 = a0
  7971. - "syscall" /* Perform the system call. */
  7972. - : "=r" (res)
  7973. - : "0" (__NR_pipe), "r" (fd)
  7974. - : "$4", "$7");
  7975. -
  7976. - fd[0] = res;
  7977. - fd[1] = res2;
  7978. - return(0);
  7979. -}