stb.h 408 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112
  1. /* stb.h - v2.37 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h
  2. no warranty is offered or implied; use this code at your own risk
  3. This is a single header file with a bunch of useful utilities
  4. for getting stuff done in C/C++.
  5. Documentation: http://nothings.org/stb/stb_h.html
  6. Unit tests: http://nothings.org/stb/stb.c
  7. ============================================================================
  8. You MUST
  9. #define STB_DEFINE
  10. in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
  11. include, like this:
  12. #define STB_DEFINE
  13. #include "stb.h"
  14. All other files should just #include "stb.h" without the #define.
  15. ============================================================================
  16. Version History
  17. 2.36 various fixes
  18. 2.35 fix clang-cl issues with swprintf
  19. 2.34 fix warnings
  20. 2.33 more fixes to random numbers
  21. 2.32 stb_intcmprev, stb_uidict, fix random numbers on Linux
  22. 2.31 stb_ucharcmp
  23. 2.30 MinGW fix
  24. 2.29 attempt to fix use of swprintf()
  25. 2.28 various new functionality
  26. 2.27 test _WIN32 not WIN32 in STB_THREADS
  27. 2.26 various warning & bugfixes
  28. 2.25 various warning & bugfixes
  29. 2.24 various warning & bugfixes
  30. 2.23 fix 2.22
  31. 2.22 64-bit fixes from '!='; fix stb_sdict_copy() to have preferred name
  32. 2.21 utf-8 decoder rejects "overlong" encodings; attempted 64-bit improvements
  33. 2.20 fix to hash "copy" function--reported by someone with handle "!="
  34. 2.19 ???
  35. 2.18 stb_readdir_subdirs_mask
  36. 2.17 stb_cfg_dir
  37. 2.16 fix stb_bgio_, add stb_bgio_stat(); begin a streaming wrapper
  38. 2.15 upgraded hash table template to allow:
  39. - aggregate keys (explicit comparison func for EMPTY and DEL keys)
  40. - "static" implementations (so they can be culled if unused)
  41. 2.14 stb_mprintf
  42. 2.13 reduce identifiable strings in STB_NO_STB_STRINGS
  43. 2.12 fix STB_ONLY -- lots of uint32s, TRUE/FALSE things had crept in
  44. 2.11 fix bug in stb_dirtree_get() which caused "c://path" sorts of stuff
  45. 2.10 STB_F(), STB_I() inline constants (also KI,KU,KF,KD)
  46. 2.09 stb_box_face_vertex_axis_side
  47. 2.08 bugfix stb_trimwhite()
  48. 2.07 colored printing in windows (why are we in 1985?)
  49. 2.06 comparison functions are now functions-that-return-functions and
  50. accept a struct-offset as a parameter (not thread-safe)
  51. 2.05 compile and pass tests under Linux (but no threads); thread cleanup
  52. 2.04 stb_cubic_bezier_1d, smoothstep, avoid dependency on registry
  53. 2.03 ?
  54. 2.02 remove integrated documentation
  55. 2.01 integrate various fixes; stb_force_uniprocessor
  56. 2.00 revised stb_dupe to use multiple hashes
  57. 1.99 stb_charcmp
  58. 1.98 stb_arr_deleten, stb_arr_insertn
  59. 1.97 fix stb_newell_normal()
  60. 1.96 stb_hash_number()
  61. 1.95 hack stb__rec_max; clean up recursion code to use new functions
  62. 1.94 stb_dirtree; rename stb_extra to stb_ptrmap
  63. 1.93 stb_sem_new() API cleanup (no blockflag-starts blocked; use 'extra')
  64. 1.92 stb_threadqueue--multi reader/writer queue, fixed size or resizeable
  65. 1.91 stb_bgio_* for reading disk asynchronously
  66. 1.90 stb_mutex uses CRITICAL_REGION; new stb_sync primitive for thread
  67. joining; workqueue supports stb_sync instead of stb_semaphore
  68. 1.89 support ';' in constant-string wildcards; stb_mutex wrapper (can
  69. implement with EnterCriticalRegion eventually)
  70. 1.88 portable threading API (only for win32 so far); worker thread queue
  71. 1.87 fix wildcard handling in stb_readdir_recursive
  72. 1.86 support ';' in wildcards
  73. 1.85 make stb_regex work with non-constant strings;
  74. beginnings of stb_introspect()
  75. 1.84 (forgot to make notes)
  76. 1.83 whoops, stb_keep_if_different wasn't deleting the temp file
  77. 1.82 bring back stb_compress from stb_file.h for cmirror
  78. 1.81 various bugfixes, STB_FASTMALLOC_INIT inits FASTMALLOC in release
  79. 1.80 stb_readdir returns utf8; write own utf8-utf16 because lib was wrong
  80. 1.79 stb_write
  81. 1.78 calloc() support for malloc wrapper, STB_FASTMALLOC
  82. 1.77 STB_FASTMALLOC
  83. 1.76 STB_STUA - Lua-like language; (stb_image, stb_csample, stb_bilinear)
  84. 1.75 alloc/free array of blocks; stb_hheap bug; a few stb_ps_ funcs;
  85. hash*getkey, hash*copy; stb_bitset; stb_strnicmp; bugfix stb_bst
  86. 1.74 stb_replaceinplace; use stdlib C function to convert utf8 to UTF-16
  87. 1.73 fix performance bug & leak in stb_ischar (C++ port lost a 'static')
  88. 1.72 remove stb_block, stb_block_manager, stb_decompress (to stb_file.h)
  89. 1.71 stb_trimwhite, stb_tokens_nested, etc.
  90. 1.70 back out 1.69 because it might problemize mixed builds; stb_filec()
  91. 1.69 (stb_file returns 'char *' in C++)
  92. 1.68 add a special 'tree root' data type for stb_bst; stb_arr_end
  93. 1.67 full C++ port. (stb_block_manager)
  94. 1.66 stb_newell_normal
  95. 1.65 stb_lex_item_wild -- allow wildcard items which MUST match entirely
  96. 1.64 stb_data
  97. 1.63 stb_log_name
  98. 1.62 stb_define_sort; C++ cleanup
  99. 1.61 stb_hash_fast -- Paul Hsieh's hash function (beats Bob Jenkins'?)
  100. 1.60 stb_delete_directory_recursive
  101. 1.59 stb_readdir_recursive
  102. 1.58 stb_bst variant with parent pointer for O(1) iteration, not O(log N)
  103. 1.57 replace LCG random with Mersenne Twister (found a public domain one)
  104. 1.56 stb_perfect_hash, stb_ischar, stb_regex
  105. 1.55 new stb_bst API allows multiple BSTs per node (e.g. secondary keys)
  106. 1.54 bugfix: stb_define_hash, stb_wildmatch, regexp
  107. 1.53 stb_define_hash; recoded stb_extra, stb_sdict use it
  108. 1.52 stb_rand_define, stb_bst, stb_reverse
  109. 1.51 fix 'stb_arr_setlen(NULL, 0)'
  110. 1.50 stb_wordwrap
  111. 1.49 minor improvements to enable the scripting language
  112. 1.48 better approach for stb_arr using stb_malloc; more invasive, clearer
  113. 1.47 stb_lex (lexes stb.h at 1.5ML/s on 3Ghz P4; 60/70% of optimal/flex)
  114. 1.46 stb_wrapper_*, STB_MALLOC_WRAPPER
  115. 1.45 lightly tested DFA acceleration of regexp searching
  116. 1.44 wildcard matching & searching; regexp matching & searching
  117. 1.43 stb_temp
  118. 1.42 allow stb_arr to use stb_malloc/realloc; note this is global
  119. 1.41 make it compile in C++; (disable stb_arr in C++)
  120. 1.40 stb_dupe tweak; stb_swap; stb_substr
  121. 1.39 stb_dupe; improve stb_file_max to be less stupid
  122. 1.38 stb_sha1_file: generate sha1 for file, even > 4GB
  123. 1.37 stb_file_max; partial support for utf8 filenames in Windows
  124. 1.36 remove STB__NO_PREFIX - poor interaction with IDE, not worth it
  125. streamline stb_arr to make it separately publishable
  126. 1.35 bugfixes for stb_sdict, stb_malloc(0), stristr
  127. 1.34 (streaming interfaces for stb_compress)
  128. 1.33 stb_alloc; bug in stb_getopt; remove stb_overflow
  129. 1.32 (stb_compress returns, smaller&faster; encode window & 64-bit len)
  130. 1.31 stb_prefix_count
  131. 1.30 (STB__NO_PREFIX - remove stb_ prefixes for personal projects)
  132. 1.29 stb_fput_varlen64, etc.
  133. 1.28 stb_sha1
  134. 1.27 ?
  135. 1.26 stb_extra
  136. 1.25 ?
  137. 1.24 stb_copyfile
  138. 1.23 stb_readdir
  139. 1.22 ?
  140. 1.21 ?
  141. 1.20 ?
  142. 1.19 ?
  143. 1.18 ?
  144. 1.17 ?
  145. 1.16 ?
  146. 1.15 stb_fixpath, stb_splitpath, stb_strchr2
  147. 1.14 stb_arr
  148. 1.13 ?stb, stb_log, stb_fatal
  149. 1.12 ?stb_hash2
  150. 1.11 miniML
  151. 1.10 stb_crc32, stb_adler32
  152. 1.09 stb_sdict
  153. 1.08 stb_bitreverse, stb_ispow2, stb_big32
  154. stb_fopen, stb_fput_varlen, stb_fput_ranged
  155. stb_fcmp, stb_feq
  156. 1.07 (stb_encompress)
  157. 1.06 stb_compress
  158. 1.05 stb_tokens, (stb_hheap)
  159. 1.04 stb_rand
  160. 1.03 ?(s-strings)
  161. 1.02 ?stb_filelen, stb_tokens
  162. 1.01 stb_tolower
  163. 1.00 stb_hash, stb_intcmp
  164. stb_file, stb_stringfile, stb_fgets
  165. stb_prefix, stb_strlower, stb_strtok
  166. stb_image
  167. (stb_array), (stb_arena)
  168. Parenthesized items have since been removed.
  169. LICENSE
  170. See end of file for license information.
  171. CREDITS
  172. Written by Sean Barrett.
  173. Fixes:
  174. Philipp Wiesemann
  175. Robert Nix
  176. r-lyeh
  177. blackpawn
  178. github:Mojofreem
  179. Ryan Whitworth
  180. Vincent Isambart
  181. Mike Sartain
  182. Eugene Opalev
  183. Tim Sjostrand
  184. github:infatum
  185. Dave Butler (Croepha)
  186. Ethan Lee (flibitijibibo)
  187. Brian Collins
  188. Kyle Langley
  189. */
  190. #include <stdarg.h>
  191. #ifndef STB__INCLUDE_STB_H
  192. #define STB__INCLUDE_STB_H
  193. #define STB_VERSION 1
  194. #ifdef STB_INTROSPECT
  195. #define STB_DEFINE
  196. #endif
  197. #ifdef STB_DEFINE_THREADS
  198. #ifndef STB_DEFINE
  199. #define STB_DEFINE
  200. #endif
  201. #ifndef STB_THREADS
  202. #define STB_THREADS
  203. #endif
  204. #endif
  205. #if defined(_WIN32) && !defined(__MINGW32__)
  206. #ifndef _CRT_SECURE_NO_WARNINGS
  207. #define _CRT_SECURE_NO_WARNINGS
  208. #endif
  209. #ifndef _CRT_NONSTDC_NO_DEPRECATE
  210. #define _CRT_NONSTDC_NO_DEPRECATE
  211. #endif
  212. #ifndef _CRT_NON_CONFORMING_SWPRINTFS
  213. #define _CRT_NON_CONFORMING_SWPRINTFS
  214. #endif
  215. #if !defined(_MSC_VER) || _MSC_VER > 1700
  216. #include <intrin.h> // _BitScanReverse
  217. #endif
  218. #endif
  219. #include <stdlib.h> // stdlib could have min/max
  220. #include <stdio.h> // need FILE
  221. #include <string.h> // stb_define_hash needs memcpy/memset
  222. #include <time.h> // stb_dirtree
  223. #ifdef __MINGW32__
  224. #include <fcntl.h> // O_RDWR
  225. #endif
  226. #ifdef STB_PERSONAL
  227. typedef int Bool;
  228. #define False 0
  229. #define True 1
  230. #endif
  231. #ifdef STB_MALLOC_WRAPPER_PAGED
  232. #define STB_MALLOC_WRAPPER_DEBUG
  233. #endif
  234. #ifdef STB_MALLOC_WRAPPER_DEBUG
  235. #define STB_MALLOC_WRAPPER
  236. #endif
  237. #ifdef STB_MALLOC_WRAPPER_FASTMALLOC
  238. #define STB_FASTMALLOC
  239. #define STB_MALLOC_WRAPPER
  240. #endif
  241. #ifdef STB_FASTMALLOC
  242. #ifndef _WIN32
  243. #undef STB_FASTMALLOC
  244. #endif
  245. #endif
  246. #ifdef STB_DEFINE
  247. #include <assert.h>
  248. #include <stdarg.h>
  249. #include <stddef.h>
  250. #include <ctype.h>
  251. #include <math.h>
  252. #ifndef _WIN32
  253. #include <unistd.h>
  254. #else
  255. #include <io.h> // _mktemp
  256. #include <direct.h> // _rmdir
  257. #endif
  258. #include <sys/types.h> // stat()/_stat()
  259. #include <sys/stat.h> // stat()/_stat()
  260. #endif
  261. #define stb_min(a,b) ((a) < (b) ? (a) : (b))
  262. #define stb_max(a,b) ((a) > (b) ? (a) : (b))
  263. #ifndef STB_ONLY
  264. #if !defined(__cplusplus) && !defined(min) && !defined(max)
  265. #define min(x,y) stb_min(x,y)
  266. #define max(x,y) stb_max(x,y)
  267. #endif
  268. #ifndef M_PI
  269. #define M_PI 3.14159265358979323846f
  270. #endif
  271. #ifndef TRUE
  272. #define TRUE 1
  273. #define FALSE 0
  274. #endif
  275. #ifndef deg2rad
  276. #define deg2rad(a) ((a)*(M_PI/180))
  277. #endif
  278. #ifndef rad2deg
  279. #define rad2deg(a) ((a)*(180/M_PI))
  280. #endif
  281. #ifndef swap
  282. #ifndef __cplusplus
  283. #define swap(TYPE,a,b) \
  284. do { TYPE stb__t; stb__t = (a); (a) = (b); (b) = stb__t; } while (0)
  285. #endif
  286. #endif
  287. typedef unsigned char uint8 ;
  288. typedef signed char int8 ;
  289. typedef unsigned short uint16;
  290. typedef signed short int16;
  291. #if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
  292. typedef unsigned long uint32;
  293. typedef signed long int32;
  294. #else
  295. typedef unsigned int uint32;
  296. typedef signed int int32;
  297. #endif
  298. typedef unsigned char uchar ;
  299. typedef unsigned short ushort;
  300. typedef unsigned int uint ;
  301. typedef unsigned long ulong ;
  302. // produce compile errors if the sizes aren't right
  303. typedef char stb__testsize16[sizeof(int16)==2];
  304. typedef char stb__testsize32[sizeof(int32)==4];
  305. #endif
  306. #ifndef STB_TRUE
  307. #define STB_TRUE 1
  308. #define STB_FALSE 0
  309. #endif
  310. // if we're STB_ONLY, can't rely on uint32 or even uint, so all the
  311. // variables we'll use herein need typenames prefixed with 'stb':
  312. typedef unsigned char stb_uchar;
  313. typedef unsigned char stb_uint8;
  314. typedef unsigned int stb_uint;
  315. typedef unsigned short stb_uint16;
  316. typedef short stb_int16;
  317. typedef signed char stb_int8;
  318. #if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
  319. typedef unsigned long stb_uint32;
  320. typedef long stb_int32;
  321. #else
  322. typedef unsigned int stb_uint32;
  323. typedef int stb_int32;
  324. #endif
  325. typedef char stb__testsize2_16[sizeof(stb_uint16)==2 ? 1 : -1];
  326. typedef char stb__testsize2_32[sizeof(stb_uint32)==4 ? 1 : -1];
  327. #ifdef _MSC_VER
  328. typedef unsigned __int64 stb_uint64;
  329. typedef __int64 stb_int64;
  330. #define STB_IMM_UINT64(literalui64) (literalui64##ui64)
  331. #define STB_IMM_INT64(literali64) (literali64##i64)
  332. #else
  333. // ??
  334. typedef unsigned long long stb_uint64;
  335. typedef long long stb_int64;
  336. #define STB_IMM_UINT64(literalui64) (literalui64##ULL)
  337. #define STB_IMM_INT64(literali64) (literali64##LL)
  338. #endif
  339. typedef char stb__testsize2_64[sizeof(stb_uint64)==8 ? 1 : -1];
  340. // add platform-specific ways of checking for sizeof(char*) == 8,
  341. // and make those define STB_PTR64
  342. #if defined(_WIN64) || defined(__x86_64__) || defined(__ia64__) || defined(__LP64__)
  343. #define STB_PTR64
  344. #endif
  345. #ifdef STB_PTR64
  346. typedef char stb__testsize2_ptr[sizeof(char *) == 8];
  347. typedef stb_uint64 stb_uinta;
  348. typedef stb_int64 stb_inta;
  349. #else
  350. typedef char stb__testsize2_ptr[sizeof(char *) == 4];
  351. typedef stb_uint32 stb_uinta;
  352. typedef stb_int32 stb_inta;
  353. #endif
  354. typedef char stb__testsize2_uinta[sizeof(stb_uinta)==sizeof(char*) ? 1 : -1];
  355. // if so, we should define an int type that is the pointer size. until then,
  356. // we'll have to make do with this (which is not the same at all!)
  357. typedef union
  358. {
  359. unsigned int i;
  360. void * p;
  361. } stb_uintptr;
  362. #ifdef __cplusplus
  363. #define STB_EXTERN extern "C"
  364. #else
  365. #define STB_EXTERN extern
  366. #endif
  367. // check for well-known debug defines
  368. #if defined(DEBUG) || defined(_DEBUG) || defined(DBG)
  369. #ifndef NDEBUG
  370. #define STB_DEBUG
  371. #endif
  372. #endif
  373. #ifdef STB_DEBUG
  374. #include <assert.h>
  375. #endif
  376. //////////////////////////////////////////////////////////////////////////////
  377. //
  378. // C library function platform handling
  379. //
  380. #ifdef STB_DEFINE
  381. #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
  382. static FILE * stb_p_fopen(const char *filename, const char *mode)
  383. {
  384. FILE *f;
  385. if (0 == fopen_s(&f, filename, mode))
  386. return f;
  387. else
  388. return NULL;
  389. }
  390. static FILE * stb_p_wfopen(const wchar_t *filename, const wchar_t *mode)
  391. {
  392. FILE *f;
  393. if (0 == _wfopen_s(&f, filename, mode))
  394. return f;
  395. else
  396. return NULL;
  397. }
  398. static char *stb_p_strcpy_s(char *a, size_t size, const char *b)
  399. {
  400. strcpy_s(a,size,b);
  401. return a;
  402. }
  403. static char *stb_p_strncpy_s(char *a, size_t size, const char *b, size_t count)
  404. {
  405. strncpy_s(a,size,b,count);
  406. return a;
  407. }
  408. #define stb_p_mktemp(s) (_mktemp_s(s, strlen(s)+1) == 0)
  409. #define stb_p_sprintf sprintf_s
  410. #define stb_p_size(x) ,(x)
  411. #else
  412. #define stb_p_fopen fopen
  413. #define stb_p_wfopen _wfopen
  414. #define stb_p_strcpy_s(a,s,b) strcpy(a,b)
  415. #define stb_p_strncpy_s(a,s,b,c) strncpy(a,b,c)
  416. #define stb_p_mktemp(s) (mktemp(s) != NULL)
  417. #define stb_p_sprintf sprintf
  418. #define stb_p_size(x)
  419. #endif
  420. #if defined(_WIN32)
  421. #define stb_p_vsnprintf _vsnprintf
  422. #else
  423. #define stb_p_vsnprintf vsnprintf
  424. #endif
  425. #endif // STB_DEFINE
  426. #if defined(_WIN32) && (_MSC_VER >= 1300)
  427. #define stb_p_stricmp _stricmp
  428. #define stb_p_strnicmp _strnicmp
  429. #define stb_p_strdup _strdup
  430. #else
  431. #define stb_p_strdup strdup
  432. #define stb_p_stricmp stricmp
  433. #define stb_p_strnicmp strnicmp
  434. #endif
  435. STB_EXTERN void stb_wrapper_malloc(void *newp, size_t sz, char *file, int line);
  436. STB_EXTERN void stb_wrapper_free(void *oldp, char *file, int line);
  437. STB_EXTERN void stb_wrapper_realloc(void *oldp, void *newp, size_t sz, char *file, int line);
  438. STB_EXTERN void stb_wrapper_calloc(size_t num, size_t sz, char *file, int line);
  439. STB_EXTERN void stb_wrapper_listall(void (*func)(void *ptr, size_t sz, char *file, int line));
  440. STB_EXTERN void stb_wrapper_dump(char *filename);
  441. STB_EXTERN size_t stb_wrapper_allocsize(void *oldp);
  442. STB_EXTERN void stb_wrapper_check(void *oldp);
  443. #ifdef STB_DEFINE
  444. // this is a special function used inside malloc wrapper
  445. // to do allocations that aren't tracked (to avoid
  446. // reentrancy). Of course if someone _else_ wraps realloc,
  447. // this breaks, but if they're doing that AND the malloc
  448. // wrapper they need to explicitly check for reentrancy.
  449. //
  450. // only define realloc_raw() and we do realloc(NULL,sz)
  451. // for malloc() and realloc(p,0) for free().
  452. static void * stb__realloc_raw(void *p, int sz)
  453. {
  454. if (p == NULL) return malloc(sz);
  455. if (sz == 0) { free(p); return NULL; }
  456. return realloc(p,sz);
  457. }
  458. #endif
  459. #ifdef _WIN32
  460. STB_EXTERN void * stb_smalloc(size_t sz);
  461. STB_EXTERN void stb_sfree(void *p);
  462. STB_EXTERN void * stb_srealloc(void *p, size_t sz);
  463. STB_EXTERN void * stb_scalloc(size_t n, size_t sz);
  464. STB_EXTERN char * stb_sstrdup(char *s);
  465. #endif
  466. #ifdef STB_FASTMALLOC
  467. #define malloc stb_smalloc
  468. #define free stb_sfree
  469. #define realloc stb_srealloc
  470. #define strdup stb_sstrdup
  471. #define calloc stb_scalloc
  472. #endif
  473. #ifndef STB_MALLOC_ALLCHECK
  474. #define stb__check(p) 1
  475. #else
  476. #ifndef STB_MALLOC_WRAPPER
  477. #error STB_MALLOC_ALLCHECK requires STB_MALLOC_WRAPPER
  478. #else
  479. #define stb__check(p) stb_mcheck(p)
  480. #endif
  481. #endif
  482. #ifdef STB_MALLOC_WRAPPER
  483. STB_EXTERN void * stb__malloc(size_t, char *, int);
  484. STB_EXTERN void * stb__realloc(void *, size_t, char *, int);
  485. STB_EXTERN void * stb__calloc(size_t n, size_t s, char *, int);
  486. STB_EXTERN void stb__free(void *, char *file, int);
  487. STB_EXTERN char * stb__strdup(char *s, char *file, int);
  488. STB_EXTERN void stb_malloc_checkall(void);
  489. STB_EXTERN void stb_malloc_check_counter(int init_delay, int rep_delay);
  490. #ifndef STB_MALLOC_WRAPPER_DEBUG
  491. #define stb_mcheck(p) 1
  492. #else
  493. STB_EXTERN int stb_mcheck(void *);
  494. #endif
  495. #ifdef STB_DEFINE
  496. #ifdef STB_MALLOC_WRAPPER_DEBUG
  497. #define STB__PAD 32
  498. #define STB__BIAS 16
  499. #define STB__SIG 0x51b01234
  500. #define STB__FIXSIZE(sz) (((sz+3) & ~3) + STB__PAD)
  501. #define STB__ptr(x,y) ((char *) (x) + (y))
  502. #else
  503. #define STB__ptr(x,y) (x)
  504. #define STB__FIXSIZE(sz) (sz)
  505. #endif
  506. #ifdef STB_MALLOC_WRAPPER_DEBUG
  507. int stb_mcheck(void *p)
  508. {
  509. unsigned int sz;
  510. if (p == NULL) return 1;
  511. p = ((char *) p) - STB__BIAS;
  512. sz = * (unsigned int *) p;
  513. assert(* (unsigned int *) STB__ptr(p,4) == STB__SIG);
  514. assert(* (unsigned int *) STB__ptr(p,8) == STB__SIG);
  515. assert(* (unsigned int *) STB__ptr(p,12) == STB__SIG);
  516. assert(* (unsigned int *) STB__ptr(p,sz-4) == STB__SIG+1);
  517. assert(* (unsigned int *) STB__ptr(p,sz-8) == STB__SIG+1);
  518. assert(* (unsigned int *) STB__ptr(p,sz-12) == STB__SIG+1);
  519. assert(* (unsigned int *) STB__ptr(p,sz-16) == STB__SIG+1);
  520. stb_wrapper_check(STB__ptr(p, STB__BIAS));
  521. return 1;
  522. }
  523. static void stb__check2(void *p, size_t sz, char *file, int line)
  524. {
  525. stb_mcheck(p);
  526. }
  527. void stb_malloc_checkall(void)
  528. {
  529. stb_wrapper_listall(stb__check2);
  530. }
  531. #else
  532. void stb_malloc_checkall(void) { }
  533. #endif
  534. static int stb__malloc_wait=(1 << 30), stb__malloc_next_wait = (1 << 30), stb__malloc_iter;
  535. void stb_malloc_check_counter(int init_delay, int rep_delay)
  536. {
  537. stb__malloc_wait = init_delay;
  538. stb__malloc_next_wait = rep_delay;
  539. }
  540. void stb_mcheck_all(void)
  541. {
  542. #ifdef STB_MALLOC_WRAPPER_DEBUG
  543. ++stb__malloc_iter;
  544. if (--stb__malloc_wait <= 0) {
  545. stb_malloc_checkall();
  546. stb__malloc_wait = stb__malloc_next_wait;
  547. }
  548. #endif
  549. }
  550. #ifdef STB_MALLOC_WRAPPER_PAGED
  551. #define STB__WINDOWS_PAGE (1 << 12)
  552. #ifndef _WINDOWS_
  553. STB_EXTERN __declspec(dllimport) void * __stdcall VirtualAlloc(void *p, unsigned long size, unsigned long type, unsigned long protect);
  554. STB_EXTERN __declspec(dllimport) int __stdcall VirtualFree(void *p, unsigned long size, unsigned long freetype);
  555. #endif
  556. #endif
  557. static void *stb__malloc_final(size_t sz)
  558. {
  559. #ifdef STB_MALLOC_WRAPPER_PAGED
  560. size_t aligned = (sz + STB__WINDOWS_PAGE - 1) & ~(STB__WINDOWS_PAGE-1);
  561. char *p = VirtualAlloc(NULL, aligned + STB__WINDOWS_PAGE, 0x2000, 0x04); // RESERVE, READWRITE
  562. if (p == NULL) return p;
  563. VirtualAlloc(p, aligned, 0x1000, 0x04); // COMMIT, READWRITE
  564. return p;
  565. #else
  566. return malloc(sz);
  567. #endif
  568. }
  569. static void stb__free_final(void *p)
  570. {
  571. #ifdef STB_MALLOC_WRAPPER_PAGED
  572. VirtualFree(p, 0, 0x8000); // RELEASE
  573. #else
  574. free(p);
  575. #endif
  576. }
  577. int stb__malloc_failure;
  578. #ifdef STB_MALLOC_WRAPPER_PAGED
  579. static void *stb__realloc_final(void *p, size_t sz, size_t old_sz)
  580. {
  581. void *q = stb__malloc_final(sz);
  582. if (q == NULL)
  583. return ++stb__malloc_failure, q;
  584. // @TODO: deal with p being smaller!
  585. memcpy(q, p, sz < old_sz ? sz : old_sz);
  586. stb__free_final(p);
  587. return q;
  588. }
  589. #endif
  590. void stb__free(void *p, char *file, int line)
  591. {
  592. stb_mcheck_all();
  593. if (!p) return;
  594. #ifdef STB_MALLOC_WRAPPER_DEBUG
  595. stb_mcheck(p);
  596. #endif
  597. stb_wrapper_free(p,file,line);
  598. #ifdef STB_MALLOC_WRAPPER_DEBUG
  599. p = STB__ptr(p,-STB__BIAS);
  600. * (unsigned int *) STB__ptr(p,0) = 0xdeadbeef;
  601. * (unsigned int *) STB__ptr(p,4) = 0xdeadbeef;
  602. * (unsigned int *) STB__ptr(p,8) = 0xdeadbeef;
  603. * (unsigned int *) STB__ptr(p,12) = 0xdeadbeef;
  604. #endif
  605. stb__free_final(p);
  606. }
  607. void * stb__malloc(size_t sz, char *file, int line)
  608. {
  609. void *p;
  610. stb_mcheck_all();
  611. if (sz == 0) return NULL;
  612. p = stb__malloc_final(STB__FIXSIZE(sz));
  613. if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
  614. if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
  615. if (p == NULL) {
  616. ++stb__malloc_failure;
  617. #ifdef STB_MALLOC_WRAPPER_DEBUG
  618. stb_malloc_checkall();
  619. #endif
  620. return p;
  621. }
  622. #ifdef STB_MALLOC_WRAPPER_DEBUG
  623. * (int *) STB__ptr(p,0) = STB__FIXSIZE(sz);
  624. * (unsigned int *) STB__ptr(p,4) = STB__SIG;
  625. * (unsigned int *) STB__ptr(p,8) = STB__SIG;
  626. * (unsigned int *) STB__ptr(p,12) = STB__SIG;
  627. * (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-4) = STB__SIG+1;
  628. * (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-8) = STB__SIG+1;
  629. * (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-12) = STB__SIG+1;
  630. * (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-16) = STB__SIG+1;
  631. p = STB__ptr(p, STB__BIAS);
  632. #endif
  633. stb_wrapper_malloc(p,sz,file,line);
  634. return p;
  635. }
  636. void * stb__realloc(void *p, size_t sz, char *file, int line)
  637. {
  638. void *q;
  639. stb_mcheck_all();
  640. if (p == NULL) return stb__malloc(sz,file,line);
  641. if (sz == 0 ) { stb__free(p,file,line); return NULL; }
  642. #ifdef STB_MALLOC_WRAPPER_DEBUG
  643. stb_mcheck(p);
  644. p = STB__ptr(p,-STB__BIAS);
  645. #endif
  646. #ifdef STB_MALLOC_WRAPPER_PAGED
  647. {
  648. size_t n = stb_wrapper_allocsize(STB__ptr(p,STB__BIAS));
  649. if (!n)
  650. stb_wrapper_check(STB__ptr(p,STB__BIAS));
  651. q = stb__realloc_final(p, STB__FIXSIZE(sz), STB__FIXSIZE(n));
  652. }
  653. #else
  654. q = realloc(p, STB__FIXSIZE(sz));
  655. #endif
  656. if (q == NULL)
  657. return ++stb__malloc_failure, q;
  658. #ifdef STB_MALLOC_WRAPPER_DEBUG
  659. * (int *) STB__ptr(q,0) = STB__FIXSIZE(sz);
  660. * (unsigned int *) STB__ptr(q,4) = STB__SIG;
  661. * (unsigned int *) STB__ptr(q,8) = STB__SIG;
  662. * (unsigned int *) STB__ptr(q,12) = STB__SIG;
  663. * (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-4) = STB__SIG+1;
  664. * (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-8) = STB__SIG+1;
  665. * (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-12) = STB__SIG+1;
  666. * (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-16) = STB__SIG+1;
  667. q = STB__ptr(q, STB__BIAS);
  668. p = STB__ptr(p, STB__BIAS);
  669. #endif
  670. stb_wrapper_realloc(p,q,sz,file,line);
  671. return q;
  672. }
  673. STB_EXTERN int stb_log2_ceil(size_t);
  674. static void *stb__calloc(size_t n, size_t sz, char *file, int line)
  675. {
  676. void *q;
  677. stb_mcheck_all();
  678. if (n == 0 || sz == 0) return NULL;
  679. if (stb_log2_ceil(n) + stb_log2_ceil(sz) >= 32) return NULL;
  680. q = stb__malloc(n*sz, file, line);
  681. if (q) memset(q, 0, n*sz);
  682. return q;
  683. }
  684. char * stb__strdup(char *s, char *file, int line)
  685. {
  686. char *p;
  687. stb_mcheck_all();
  688. p = stb__malloc(strlen(s)+1, file, line);
  689. if (!p) return p;
  690. stb_p_strcpy_s(p, strlen(s)+1, s);
  691. return p;
  692. }
  693. #endif // STB_DEFINE
  694. #ifdef STB_FASTMALLOC
  695. #undef malloc
  696. #undef realloc
  697. #undef free
  698. #undef strdup
  699. #undef calloc
  700. #endif
  701. // include everything that might define these, BEFORE making macros
  702. #include <stdlib.h>
  703. #include <string.h>
  704. #include <malloc.h>
  705. #define malloc(s) stb__malloc ( s, __FILE__, __LINE__)
  706. #define realloc(p,s) stb__realloc(p,s, __FILE__, __LINE__)
  707. #define calloc(n,s) stb__calloc (n,s, __FILE__, __LINE__)
  708. #define free(p) stb__free (p, __FILE__, __LINE__)
  709. #define strdup(p) stb__strdup (p, __FILE__, __LINE__)
  710. #endif
  711. //////////////////////////////////////////////////////////////////////////////
  712. //
  713. // Windows pretty display
  714. //
  715. STB_EXTERN void stbprint(const char *fmt, ...);
  716. STB_EXTERN char *stb_sprintf(const char *fmt, ...);
  717. STB_EXTERN char *stb_mprintf(const char *fmt, ...);
  718. STB_EXTERN int stb_snprintf(char *s, size_t n, const char *fmt, ...);
  719. STB_EXTERN int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v);
  720. #ifdef STB_DEFINE
  721. int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v)
  722. {
  723. int res;
  724. #ifdef _WIN32
  725. #ifdef __STDC_WANT_SECURE_LIB__
  726. res = _vsnprintf_s(s, n, _TRUNCATE, fmt, v);
  727. #else
  728. res = stb_p_vsnprintf(s,n,fmt,v);
  729. #endif
  730. #else
  731. res = vsnprintf(s,n,fmt,v);
  732. #endif
  733. if (n) s[n-1] = 0;
  734. // Unix returns length output would require, Windows returns negative when truncated.
  735. return (res >= (int) n || res < 0) ? -1 : res;
  736. }
  737. int stb_snprintf(char *s, size_t n, const char *fmt, ...)
  738. {
  739. int res;
  740. va_list v;
  741. va_start(v,fmt);
  742. res = stb_vsnprintf(s, n, fmt, v);
  743. va_end(v);
  744. return res;
  745. }
  746. char *stb_sprintf(const char *fmt, ...)
  747. {
  748. static char buffer[1024];
  749. va_list v;
  750. va_start(v,fmt);
  751. stb_vsnprintf(buffer,1024,fmt,v);
  752. va_end(v);
  753. return buffer;
  754. }
  755. char *stb_mprintf(const char *fmt, ...)
  756. {
  757. static char buffer[1024];
  758. va_list v;
  759. va_start(v,fmt);
  760. stb_vsnprintf(buffer,1024,fmt,v);
  761. va_end(v);
  762. return stb_p_strdup(buffer);
  763. }
  764. #ifdef _WIN32
  765. #ifndef _WINDOWS_
  766. STB_EXTERN __declspec(dllimport) int __stdcall WriteConsoleA(void *, const void *, unsigned int, unsigned int *, void *);
  767. STB_EXTERN __declspec(dllimport) void * __stdcall GetStdHandle(unsigned int);
  768. STB_EXTERN __declspec(dllimport) int __stdcall SetConsoleTextAttribute(void *, unsigned short);
  769. #endif
  770. static void stb__print_one(void *handle, char *s, ptrdiff_t len)
  771. {
  772. if (len)
  773. if (0==WriteConsoleA(handle, s, (unsigned) len, NULL,NULL))
  774. // if it fails, maybe redirected, so output normally...
  775. // but it's supriously reporting failure now on Win7 and later
  776. {}//fwrite(s, 1, (unsigned) len, stdout);
  777. }
  778. static void stb__print(char *s)
  779. {
  780. void *handle = GetStdHandle((unsigned int) -11); // STD_OUTPUT_HANDLE
  781. int pad=0; // number of padding characters to add
  782. char *t = s;
  783. while (*s) {
  784. int lpad;
  785. while (*s && *s != '{') {
  786. if (pad) {
  787. if (*s == '\r' || *s == '\n')
  788. pad = 0;
  789. else if (s[0] == ' ' && s[1] == ' ') {
  790. stb__print_one(handle, t, s-t);
  791. t = s;
  792. while (pad) {
  793. stb__print_one(handle, t, 1);
  794. --pad;
  795. }
  796. }
  797. }
  798. ++s;
  799. }
  800. if (!*s) break;
  801. stb__print_one(handle, t, s-t);
  802. if (s[1] == '{') {
  803. ++s;
  804. continue;
  805. }
  806. if (s[1] == '#') {
  807. t = s+3;
  808. if (isxdigit(s[2]))
  809. if (isdigit(s[2]))
  810. SetConsoleTextAttribute(handle, s[2] - '0');
  811. else
  812. SetConsoleTextAttribute(handle, tolower(s[2]) - 'a' + 10);
  813. else {
  814. SetConsoleTextAttribute(handle, 0x0f);
  815. t=s+2;
  816. }
  817. } else if (s[1] == '!') {
  818. SetConsoleTextAttribute(handle, 0x0c);
  819. t = s+2;
  820. } else if (s[1] == '@') {
  821. SetConsoleTextAttribute(handle, 0x09);
  822. t = s+2;
  823. } else if (s[1] == '$') {
  824. SetConsoleTextAttribute(handle, 0x0a);
  825. t = s+2;
  826. } else {
  827. SetConsoleTextAttribute(handle, 0x08); // 0,7,8,15 => shades of grey
  828. t = s+1;
  829. }
  830. lpad = (int) (t-s);
  831. s = t;
  832. while (*s && *s != '}') ++s;
  833. if (!*s) break;
  834. stb__print_one(handle, t, s-t);
  835. if (s[1] == '}') {
  836. t = s+2;
  837. } else {
  838. pad += 1+lpad;
  839. t = s+1;
  840. }
  841. s=t;
  842. SetConsoleTextAttribute(handle, 0x07);
  843. }
  844. stb__print_one(handle, t, s-t);
  845. SetConsoleTextAttribute(handle, 0x07);
  846. }
  847. void stbprint(const char *fmt, ...)
  848. {
  849. int res;
  850. char buffer[1024];
  851. char *tbuf = buffer;
  852. va_list v;
  853. va_start(v,fmt);
  854. res = stb_vsnprintf(buffer, sizeof(buffer), fmt, v);
  855. va_end(v);
  856. if (res < 0) {
  857. tbuf = (char *) malloc(16384);
  858. va_start(v,fmt);
  859. res = stb_vsnprintf(tbuf,16384, fmt, v);
  860. va_end(v);
  861. tbuf[16383] = 0;
  862. }
  863. stb__print(tbuf);
  864. if (tbuf != buffer)
  865. free(tbuf);
  866. }
  867. #else // _WIN32
  868. void stbprint(const char *fmt, ...)
  869. {
  870. va_list v;
  871. va_start(v,fmt);
  872. vprintf(fmt,v);
  873. va_end(v);
  874. }
  875. #endif // _WIN32
  876. #endif // STB_DEFINE
  877. //////////////////////////////////////////////////////////////////////////////
  878. //
  879. // Windows UTF8 filename handling
  880. //
  881. // Windows stupidly treats 8-bit filenames as some dopey code page,
  882. // rather than utf-8. If we want to use utf8 filenames, we have to
  883. // convert them to WCHAR explicitly and call WCHAR versions of the
  884. // file functions. So, ok, we do.
  885. #ifdef _WIN32
  886. #define stb__fopen(x,y) stb_p_wfopen((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
  887. #define stb__windows(x,y) x
  888. #else
  889. #define stb__fopen(x,y) stb_p_fopen(x,y)
  890. #define stb__windows(x,y) y
  891. #endif
  892. typedef unsigned short stb__wchar;
  893. STB_EXTERN stb__wchar * stb_from_utf8(stb__wchar *buffer, const char *str, int n);
  894. STB_EXTERN char * stb_to_utf8 (char *buffer, const stb__wchar *str, int n);
  895. STB_EXTERN stb__wchar *stb__from_utf8(const char *str);
  896. STB_EXTERN stb__wchar *stb__from_utf8_alt(const char *str);
  897. STB_EXTERN char *stb__to_utf8(const stb__wchar *str);
  898. #ifdef STB_DEFINE
  899. stb__wchar * stb_from_utf8(stb__wchar *buffer, const char *ostr, int n)
  900. {
  901. unsigned char *str = (unsigned char *) ostr;
  902. stb_uint32 c;
  903. int i=0;
  904. --n;
  905. while (*str) {
  906. if (i >= n)
  907. return NULL;
  908. if (!(*str & 0x80))
  909. buffer[i++] = *str++;
  910. else if ((*str & 0xe0) == 0xc0) {
  911. if (*str < 0xc2) return NULL;
  912. c = (*str++ & 0x1f) << 6;
  913. if ((*str & 0xc0) != 0x80) return NULL;
  914. buffer[i++] = c + (*str++ & 0x3f);
  915. } else if ((*str & 0xf0) == 0xe0) {
  916. if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return NULL;
  917. if (*str == 0xed && str[1] > 0x9f) return NULL; // str[1] < 0x80 is checked below
  918. c = (*str++ & 0x0f) << 12;
  919. if ((*str & 0xc0) != 0x80) return NULL;
  920. c += (*str++ & 0x3f) << 6;
  921. if ((*str & 0xc0) != 0x80) return NULL;
  922. buffer[i++] = c + (*str++ & 0x3f);
  923. } else if ((*str & 0xf8) == 0xf0) {
  924. if (*str > 0xf4) return NULL;
  925. if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return NULL;
  926. if (*str == 0xf4 && str[1] > 0x8f) return NULL; // str[1] < 0x80 is checked below
  927. c = (*str++ & 0x07) << 18;
  928. if ((*str & 0xc0) != 0x80) return NULL;
  929. c += (*str++ & 0x3f) << 12;
  930. if ((*str & 0xc0) != 0x80) return NULL;
  931. c += (*str++ & 0x3f) << 6;
  932. if ((*str & 0xc0) != 0x80) return NULL;
  933. c += (*str++ & 0x3f);
  934. // utf-8 encodings of values used in surrogate pairs are invalid
  935. if ((c & 0xFFFFF800) == 0xD800) return NULL;
  936. if (c >= 0x10000) {
  937. c -= 0x10000;
  938. if (i + 2 > n) return NULL;
  939. buffer[i++] = 0xD800 | (0x3ff & (c >> 10));
  940. buffer[i++] = 0xDC00 | (0x3ff & (c ));
  941. }
  942. } else
  943. return NULL;
  944. }
  945. buffer[i] = 0;
  946. return buffer;
  947. }
  948. char * stb_to_utf8(char *buffer, const stb__wchar *str, int n)
  949. {
  950. int i=0;
  951. --n;
  952. while (*str) {
  953. if (*str < 0x80) {
  954. if (i+1 > n) return NULL;
  955. buffer[i++] = (char) *str++;
  956. } else if (*str < 0x800) {
  957. if (i+2 > n) return NULL;
  958. buffer[i++] = 0xc0 + (*str >> 6);
  959. buffer[i++] = 0x80 + (*str & 0x3f);
  960. str += 1;
  961. } else if (*str >= 0xd800 && *str < 0xdc00) {
  962. stb_uint32 c;
  963. if (i+4 > n) return NULL;
  964. c = ((str[0] - 0xd800) << 10) + ((str[1]) - 0xdc00) + 0x10000;
  965. buffer[i++] = 0xf0 + (c >> 18);
  966. buffer[i++] = 0x80 + ((c >> 12) & 0x3f);
  967. buffer[i++] = 0x80 + ((c >> 6) & 0x3f);
  968. buffer[i++] = 0x80 + ((c ) & 0x3f);
  969. str += 2;
  970. } else if (*str >= 0xdc00 && *str < 0xe000) {
  971. return NULL;
  972. } else {
  973. if (i+3 > n) return NULL;
  974. buffer[i++] = 0xe0 + (*str >> 12);
  975. buffer[i++] = 0x80 + ((*str >> 6) & 0x3f);
  976. buffer[i++] = 0x80 + ((*str ) & 0x3f);
  977. str += 1;
  978. }
  979. }
  980. buffer[i] = 0;
  981. return buffer;
  982. }
  983. stb__wchar *stb__from_utf8(const char *str)
  984. {
  985. static stb__wchar buffer[4096];
  986. return stb_from_utf8(buffer, str, 4096);
  987. }
  988. stb__wchar *stb__from_utf8_alt(const char *str)
  989. {
  990. static stb__wchar buffer[4096];
  991. return stb_from_utf8(buffer, str, 4096);
  992. }
  993. char *stb__to_utf8(const stb__wchar *str)
  994. {
  995. static char buffer[4096];
  996. return stb_to_utf8(buffer, str, 4096);
  997. }
  998. #endif
  999. //////////////////////////////////////////////////////////////////////////////
  1000. //
  1001. // Miscellany
  1002. //
  1003. STB_EXTERN void stb_fatal(const char *fmt, ...);
  1004. STB_EXTERN void stb_(char *fmt, ...);
  1005. STB_EXTERN void stb_append_to_file(char *file, char *fmt, ...);
  1006. STB_EXTERN void stb_log(int active);
  1007. STB_EXTERN void stb_log_fileline(int active);
  1008. STB_EXTERN void stb_log_name(char *filename);
  1009. STB_EXTERN void stb_swap(void *p, void *q, size_t sz);
  1010. STB_EXTERN void *stb_copy(void *p, size_t sz);
  1011. STB_EXTERN void stb_pointer_array_free(void *p, int len);
  1012. STB_EXTERN void **stb_array_block_alloc(int count, int blocksize);
  1013. #define stb_arrcount(x) (sizeof(x)/sizeof((x)[0]))
  1014. STB_EXTERN int stb__record_fileline(const char *f, int n);
  1015. #ifdef STB_DEFINE
  1016. static char *stb__file;
  1017. static int stb__line;
  1018. int stb__record_fileline(const char *f, int n)
  1019. {
  1020. stb__file = (char*) f;
  1021. stb__line = n;
  1022. return 0;
  1023. }
  1024. void stb_fatal(const char *s, ...)
  1025. {
  1026. va_list a;
  1027. if (stb__file)
  1028. fprintf(stderr, "[%s:%d] ", stb__file, stb__line);
  1029. va_start(a,s);
  1030. fputs("Fatal error: ", stderr);
  1031. vfprintf(stderr, s, a);
  1032. va_end(a);
  1033. fputs("\n", stderr);
  1034. #ifdef STB_DEBUG
  1035. #ifdef _MSC_VER
  1036. #ifndef STB_PTR64
  1037. __asm int 3; // trap to debugger!
  1038. #else
  1039. __debugbreak();
  1040. #endif
  1041. #else
  1042. __builtin_trap();
  1043. #endif
  1044. #endif
  1045. exit(1);
  1046. }
  1047. static int stb__log_active=1, stb__log_fileline=1;
  1048. void stb_log(int active)
  1049. {
  1050. stb__log_active = active;
  1051. }
  1052. void stb_log_fileline(int active)
  1053. {
  1054. stb__log_fileline = active;
  1055. }
  1056. #ifdef STB_NO_STB_STRINGS
  1057. const char *stb__log_filename = "temp.log";
  1058. #else
  1059. const char *stb__log_filename = "stb.log";
  1060. #endif
  1061. void stb_log_name(char *s)
  1062. {
  1063. stb__log_filename = s;
  1064. }
  1065. void stb_(char *s, ...)
  1066. {
  1067. if (stb__log_active) {
  1068. FILE *f = stb_p_fopen(stb__log_filename, "a");
  1069. if (f) {
  1070. va_list a;
  1071. if (stb__log_fileline && stb__file)
  1072. fprintf(f, "[%s:%4d] ", stb__file, stb__line);
  1073. va_start(a,s);
  1074. vfprintf(f, s, a);
  1075. va_end(a);
  1076. fputs("\n", f);
  1077. fclose(f);
  1078. }
  1079. }
  1080. }
  1081. void stb_append_to_file(char *filename, char *s, ...)
  1082. {
  1083. FILE *f = stb_p_fopen(filename, "a");
  1084. if (f) {
  1085. va_list a;
  1086. va_start(a,s);
  1087. vfprintf(f, s, a);
  1088. va_end(a);
  1089. fputs("\n", f);
  1090. fclose(f);
  1091. }
  1092. }
  1093. typedef struct { char d[4]; } stb__4;
  1094. typedef struct { char d[8]; } stb__8;
  1095. // optimize the small cases, though you shouldn't be calling this for those!
  1096. void stb_swap(void *p, void *q, size_t sz)
  1097. {
  1098. char buffer[256];
  1099. if (p == q) return;
  1100. if (sz == 4) {
  1101. stb__4 temp = * ( stb__4 *) p;
  1102. * (stb__4 *) p = * ( stb__4 *) q;
  1103. * (stb__4 *) q = temp;
  1104. return;
  1105. } else if (sz == 8) {
  1106. stb__8 temp = * ( stb__8 *) p;
  1107. * (stb__8 *) p = * ( stb__8 *) q;
  1108. * (stb__8 *) q = temp;
  1109. return;
  1110. }
  1111. while (sz > sizeof(buffer)) {
  1112. stb_swap(p, q, sizeof(buffer));
  1113. p = (char *) p + sizeof(buffer);
  1114. q = (char *) q + sizeof(buffer);
  1115. sz -= sizeof(buffer);
  1116. }
  1117. memcpy(buffer, p , sz);
  1118. memcpy(p , q , sz);
  1119. memcpy(q , buffer, sz);
  1120. }
  1121. void *stb_copy(void *p, size_t sz)
  1122. {
  1123. void *q = malloc(sz);
  1124. memcpy(q, p, sz);
  1125. return q;
  1126. }
  1127. void stb_pointer_array_free(void *q, int len)
  1128. {
  1129. void **p = (void **) q;
  1130. int i;
  1131. for (i=0; i < len; ++i)
  1132. free(p[i]);
  1133. }
  1134. void **stb_array_block_alloc(int count, int blocksize)
  1135. {
  1136. int i;
  1137. char *p = (char *) malloc(sizeof(void *) * count + count * blocksize);
  1138. void **q;
  1139. if (p == NULL) return NULL;
  1140. q = (void **) p;
  1141. p += sizeof(void *) * count;
  1142. for (i=0; i < count; ++i)
  1143. q[i] = p + i * blocksize;
  1144. return q;
  1145. }
  1146. #endif
  1147. #ifdef STB_DEBUG
  1148. // tricky hack to allow recording FILE,LINE even in varargs functions
  1149. #define STB__RECORD_FILE(x) (stb__record_fileline(__FILE__, __LINE__),(x))
  1150. #define stb_log STB__RECORD_FILE(stb_log)
  1151. #define stb_ STB__RECORD_FILE(stb_)
  1152. #ifndef STB_FATAL_CLEAN
  1153. #define stb_fatal STB__RECORD_FILE(stb_fatal)
  1154. #endif
  1155. #define STB__DEBUG(x) x
  1156. #else
  1157. #define STB__DEBUG(x)
  1158. #endif
  1159. //////////////////////////////////////////////////////////////////////////////
  1160. //
  1161. // stb_temp
  1162. //
  1163. #define stb_temp(block, sz) stb__temp(block, sizeof(block), (sz))
  1164. STB_EXTERN void * stb__temp(void *b, int b_sz, int want_sz);
  1165. STB_EXTERN void stb_tempfree(void *block, void *ptr);
  1166. #ifdef STB_DEFINE
  1167. void * stb__temp(void *b, int b_sz, int want_sz)
  1168. {
  1169. if (b_sz >= want_sz)
  1170. return b;
  1171. else
  1172. return malloc(want_sz);
  1173. }
  1174. void stb_tempfree(void *b, void *p)
  1175. {
  1176. if (p != b)
  1177. free(p);
  1178. }
  1179. #endif
  1180. //////////////////////////////////////////////////////////////////////////////
  1181. //
  1182. // math/sampling operations
  1183. //
  1184. #define stb_lerp(t,a,b) ( (a) + (t) * (float) ((b)-(a)) )
  1185. #define stb_unlerp(t,a,b) ( ((t) - (a)) / (float) ((b) - (a)) )
  1186. #define stb_clamp(x,xmin,xmax) ((x) < (xmin) ? (xmin) : (x) > (xmax) ? (xmax) : (x))
  1187. STB_EXTERN void stb_newell_normal(float *normal, int num_vert, float **vert, int normalize);
  1188. STB_EXTERN int stb_box_face_vertex_axis_side(int face_number, int vertex_number, int axis);
  1189. STB_EXTERN void stb_linear_controller(float *curpos, float target_pos, float acc, float deacc, float dt);
  1190. STB_EXTERN int stb_float_eq(float x, float y, float delta, int max_ulps);
  1191. STB_EXTERN int stb_is_prime(unsigned int m);
  1192. STB_EXTERN unsigned int stb_power_of_two_nearest_prime(int n);
  1193. STB_EXTERN float stb_smoothstep(float t);
  1194. STB_EXTERN float stb_cubic_bezier_1d(float t, float p0, float p1, float p2, float p3);
  1195. STB_EXTERN double stb_linear_remap(double x, double a, double b,
  1196. double c, double d);
  1197. #ifdef STB_DEFINE
  1198. float stb_smoothstep(float t)
  1199. {
  1200. return (3 - 2*t)*(t*t);
  1201. }
  1202. float stb_cubic_bezier_1d(float t, float p0, float p1, float p2, float p3)
  1203. {
  1204. float it = 1-t;
  1205. return it*it*it*p0 + 3*it*it*t*p1 + 3*it*t*t*p2 + t*t*t*p3;
  1206. }
  1207. void stb_newell_normal(float *normal, int num_vert, float **vert, int normalize)
  1208. {
  1209. int i,j;
  1210. float p;
  1211. normal[0] = normal[1] = normal[2] = 0;
  1212. for (i=num_vert-1,j=0; j < num_vert; i=j++) {
  1213. float *u = vert[i];
  1214. float *v = vert[j];
  1215. normal[0] += (u[1] - v[1]) * (u[2] + v[2]);
  1216. normal[1] += (u[2] - v[2]) * (u[0] + v[0]);
  1217. normal[2] += (u[0] - v[0]) * (u[1] + v[1]);
  1218. }
  1219. if (normalize) {
  1220. p = normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2];
  1221. p = (float) (1.0 / sqrt(p));
  1222. normal[0] *= p;
  1223. normal[1] *= p;
  1224. normal[2] *= p;
  1225. }
  1226. }
  1227. int stb_box_face_vertex_axis_side(int face_number, int vertex_number, int axis)
  1228. {
  1229. static int box_vertices[6][4][3] =
  1230. {
  1231. { { 1,1,1 }, { 1,0,1 }, { 1,0,0 }, { 1,1,0 } },
  1232. { { 0,0,0 }, { 0,0,1 }, { 0,1,1 }, { 0,1,0 } },
  1233. { { 0,0,0 }, { 0,1,0 }, { 1,1,0 }, { 1,0,0 } },
  1234. { { 0,0,0 }, { 1,0,0 }, { 1,0,1 }, { 0,0,1 } },
  1235. { { 1,1,1 }, { 0,1,1 }, { 0,0,1 }, { 1,0,1 } },
  1236. { { 1,1,1 }, { 1,1,0 }, { 0,1,0 }, { 0,1,1 } },
  1237. };
  1238. assert(face_number >= 0 && face_number < 6);
  1239. assert(vertex_number >= 0 && vertex_number < 4);
  1240. assert(axis >= 0 && axis < 3);
  1241. return box_vertices[face_number][vertex_number][axis];
  1242. }
  1243. void stb_linear_controller(float *curpos, float target_pos, float acc, float deacc, float dt)
  1244. {
  1245. float sign = 1, p, cp = *curpos;
  1246. if (cp == target_pos) return;
  1247. if (target_pos < cp) {
  1248. target_pos = -target_pos;
  1249. cp = -cp;
  1250. sign = -1;
  1251. }
  1252. // first decelerate
  1253. if (cp < 0) {
  1254. p = cp + deacc * dt;
  1255. if (p > 0) {
  1256. p = 0;
  1257. dt = dt - cp / deacc;
  1258. if (dt < 0) dt = 0;
  1259. } else {
  1260. dt = 0;
  1261. }
  1262. cp = p;
  1263. }
  1264. // now accelerate
  1265. p = cp + acc*dt;
  1266. if (p > target_pos) p = target_pos;
  1267. *curpos = p * sign;
  1268. // @TODO: testing
  1269. }
  1270. float stb_quadratic_controller(float target_pos, float curpos, float maxvel, float maxacc, float dt, float *curvel)
  1271. {
  1272. return 0; // @TODO
  1273. }
  1274. int stb_float_eq(float x, float y, float delta, int max_ulps)
  1275. {
  1276. if (fabs(x-y) <= delta) return 1;
  1277. if (abs(*(int *)&x - *(int *)&y) <= max_ulps) return 1;
  1278. return 0;
  1279. }
  1280. int stb_is_prime(unsigned int m)
  1281. {
  1282. unsigned int i,j;
  1283. if (m < 2) return 0;
  1284. if (m == 2) return 1;
  1285. if (!(m & 1)) return 0;
  1286. if (m % 3 == 0) return (m == 3);
  1287. for (i=5; (j=i*i), j <= m && j > i; i += 6) {
  1288. if (m % i == 0) return 0;
  1289. if (m % (i+2) == 0) return 0;
  1290. }
  1291. return 1;
  1292. }
  1293. unsigned int stb_power_of_two_nearest_prime(int n)
  1294. {
  1295. static signed char tab[32] = { 0,0,0,0,1,0,-1,0,1,-1,-1,3,-1,0,-1,2,1,
  1296. 0,2,0,-1,-4,-1,5,-1,18,-2,15,2,-1,2,0 };
  1297. if (!tab[0]) {
  1298. int i;
  1299. for (i=0; i < 32; ++i)
  1300. tab[i] = (1 << i) + 2*tab[i] - 1;
  1301. tab[1] = 2;
  1302. tab[0] = 1;
  1303. }
  1304. if (n >= 32) return 0xfffffffb;
  1305. return tab[n];
  1306. }
  1307. double stb_linear_remap(double x, double x_min, double x_max,
  1308. double out_min, double out_max)
  1309. {
  1310. return stb_lerp(stb_unlerp(x,x_min,x_max),out_min,out_max);
  1311. }
  1312. #endif
  1313. // create a macro so it's faster, but you can get at the function pointer
  1314. #define stb_linear_remap(t,a,b,c,d) stb_lerp(stb_unlerp(t,a,b),c,d)
  1315. //////////////////////////////////////////////////////////////////////////////
  1316. //
  1317. // bit operations
  1318. //
  1319. #define stb_big32(c) (((c)[0]<<24) + (c)[1]*65536 + (c)[2]*256 + (c)[3])
  1320. #define stb_little32(c) (((c)[3]<<24) + (c)[2]*65536 + (c)[1]*256 + (c)[0])
  1321. #define stb_big16(c) ((c)[0]*256 + (c)[1])
  1322. #define stb_little16(c) ((c)[1]*256 + (c)[0])
  1323. STB_EXTERN int stb_bitcount(unsigned int a);
  1324. STB_EXTERN unsigned int stb_bitreverse8(unsigned char n);
  1325. STB_EXTERN unsigned int stb_bitreverse(unsigned int n);
  1326. STB_EXTERN int stb_is_pow2(size_t);
  1327. STB_EXTERN int stb_log2_ceil(size_t);
  1328. STB_EXTERN int stb_log2_floor(size_t);
  1329. STB_EXTERN int stb_lowbit8(unsigned int n);
  1330. STB_EXTERN int stb_highbit8(unsigned int n);
  1331. #ifdef STB_DEFINE
  1332. int stb_bitcount(unsigned int a)
  1333. {
  1334. a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
  1335. a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
  1336. a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
  1337. a = (a + (a >> 8)); // max 16 per 8 bits
  1338. a = (a + (a >> 16)); // max 32 per 8 bits
  1339. return a & 0xff;
  1340. }
  1341. unsigned int stb_bitreverse8(unsigned char n)
  1342. {
  1343. n = ((n & 0xAA) >> 1) + ((n & 0x55) << 1);
  1344. n = ((n & 0xCC) >> 2) + ((n & 0x33) << 2);
  1345. return (unsigned char) ((n >> 4) + (n << 4));
  1346. }
  1347. unsigned int stb_bitreverse(unsigned int n)
  1348. {
  1349. n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
  1350. n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
  1351. n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
  1352. n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
  1353. return (n >> 16) | (n << 16);
  1354. }
  1355. int stb_is_pow2(size_t n)
  1356. {
  1357. return (n & (n-1)) == 0;
  1358. }
  1359. // tricky use of 4-bit table to identify 5 bit positions (note the '-1')
  1360. // 3-bit table would require another tree level; 5-bit table wouldn't save one
  1361. #if defined(_WIN32) && !defined(__MINGW32__)
  1362. #pragma warning(push)
  1363. #pragma warning(disable: 4035) // disable warning about no return value
  1364. int stb_log2_floor(size_t n)
  1365. {
  1366. #if _MSC_VER > 1700
  1367. unsigned long i;
  1368. #ifdef STB_PTR64
  1369. _BitScanReverse64(&i, n);
  1370. #else
  1371. _BitScanReverse(&i, n);
  1372. #endif
  1373. return i != 0 ? i : -1;
  1374. #else
  1375. __asm {
  1376. bsr eax,n
  1377. jnz done
  1378. mov eax,-1
  1379. }
  1380. done:;
  1381. #endif
  1382. }
  1383. #pragma warning(pop)
  1384. #else
  1385. int stb_log2_floor(size_t n)
  1386. {
  1387. static signed char log2_4[16] = { -1,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3 };
  1388. #ifdef STB_PTR64
  1389. if (n >= ((size_t) 1u << 32))
  1390. return stb_log2_floor(n >> 32);
  1391. #endif
  1392. // 2 compares if n < 16, 3 compares otherwise
  1393. if (n < (1U << 14))
  1394. if (n < (1U << 4)) return 0 + log2_4[n ];
  1395. else if (n < (1U << 9)) return 5 + log2_4[n >> 5];
  1396. else return 10 + log2_4[n >> 10];
  1397. else if (n < (1U << 24))
  1398. if (n < (1U << 19)) return 15 + log2_4[n >> 15];
  1399. else return 20 + log2_4[n >> 20];
  1400. else if (n < (1U << 29)) return 25 + log2_4[n >> 25];
  1401. else return 30 + log2_4[n >> 30];
  1402. }
  1403. #endif
  1404. // define ceil from floor
  1405. int stb_log2_ceil(size_t n)
  1406. {
  1407. if (stb_is_pow2(n)) return stb_log2_floor(n);
  1408. else return 1 + stb_log2_floor(n);
  1409. }
  1410. int stb_highbit8(unsigned int n)
  1411. {
  1412. return stb_log2_ceil(n&255);
  1413. }
  1414. int stb_lowbit8(unsigned int n)
  1415. {
  1416. static signed char lowbit4[16] = { -1,0,1,0, 2,0,1,0, 3,0,1,0, 2,0,1,0 };
  1417. int k = lowbit4[n & 15];
  1418. if (k >= 0) return k;
  1419. k = lowbit4[(n >> 4) & 15];
  1420. if (k >= 0) return k+4;
  1421. return k;
  1422. }
  1423. #endif
  1424. //////////////////////////////////////////////////////////////////////////////
  1425. //
  1426. // qsort Compare Routines
  1427. //
  1428. #ifdef _WIN32
  1429. #define stb_stricmp(a,b) stb_p_stricmp(a,b)
  1430. #define stb_strnicmp(a,b,n) stb_p_strnicmp(a,b,n)
  1431. #else
  1432. #define stb_stricmp(a,b) strcasecmp(a,b)
  1433. #define stb_strnicmp(a,b,n) strncasecmp(a,b,n)
  1434. #endif
  1435. STB_EXTERN int (*stb_intcmp(int offset))(const void *a, const void *b);
  1436. STB_EXTERN int (*stb_intcmprev(int offset))(const void *a, const void *b);
  1437. STB_EXTERN int (*stb_qsort_strcmp(int offset))(const void *a, const void *b);
  1438. STB_EXTERN int (*stb_qsort_stricmp(int offset))(const void *a, const void *b);
  1439. STB_EXTERN int (*stb_floatcmp(int offset))(const void *a, const void *b);
  1440. STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);
  1441. STB_EXTERN int (*stb_charcmp(int offset))(const void *a, const void *b);
  1442. #ifdef STB_DEFINE
  1443. static int stb__intcmpoffset, stb__ucharcmpoffset, stb__strcmpoffset;
  1444. static int stb__floatcmpoffset, stb__doublecmpoffset;
  1445. static int stb__memcmpoffset, stb__memcmpsize;
  1446. int stb__intcmp(const void *a, const void *b)
  1447. {
  1448. const int p = *(const int *) ((const char *) a + stb__intcmpoffset);
  1449. const int q = *(const int *) ((const char *) b + stb__intcmpoffset);
  1450. return p < q ? -1 : p > q;
  1451. }
  1452. int stb__intcmprev(const void *a, const void *b)
  1453. {
  1454. const int p = *(const int *) ((const char *) a + stb__intcmpoffset);
  1455. const int q = *(const int *) ((const char *) b + stb__intcmpoffset);
  1456. return q < p ? -1 : q > p;
  1457. }
  1458. int stb__ucharcmp(const void *a, const void *b)
  1459. {
  1460. const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);
  1461. const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);
  1462. return p < q ? -1 : p > q;
  1463. }
  1464. int stb__floatcmp(const void *a, const void *b)
  1465. {
  1466. const float p = *(const float *) ((const char *) a + stb__floatcmpoffset);
  1467. const float q = *(const float *) ((const char *) b + stb__floatcmpoffset);
  1468. return p < q ? -1 : p > q;
  1469. }
  1470. int stb__doublecmp(const void *a, const void *b)
  1471. {
  1472. const double p = *(const double *) ((const char *) a + stb__doublecmpoffset);
  1473. const double q = *(const double *) ((const char *) b + stb__doublecmpoffset);
  1474. return p < q ? -1 : p > q;
  1475. }
  1476. int stb__qsort_strcmp(const void *a, const void *b)
  1477. {
  1478. const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
  1479. const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
  1480. return strcmp(p,q);
  1481. }
  1482. int stb__qsort_stricmp(const void *a, const void *b)
  1483. {
  1484. const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
  1485. const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
  1486. return stb_stricmp(p,q);
  1487. }
  1488. int stb__memcmp(const void *a, const void *b)
  1489. {
  1490. return memcmp((char *) a + stb__memcmpoffset, (char *) b + stb__memcmpoffset, stb__memcmpsize);
  1491. }
  1492. int (*stb_intcmp(int offset))(const void *, const void *)
  1493. {
  1494. stb__intcmpoffset = offset;
  1495. return &stb__intcmp;
  1496. }
  1497. int (*stb_intcmprev(int offset))(const void *, const void *)
  1498. {
  1499. stb__intcmpoffset = offset;
  1500. return &stb__intcmprev;
  1501. }
  1502. int (*stb_ucharcmp(int offset))(const void *, const void *)
  1503. {
  1504. stb__ucharcmpoffset = offset;
  1505. return &stb__ucharcmp;
  1506. }
  1507. int (*stb_qsort_strcmp(int offset))(const void *, const void *)
  1508. {
  1509. stb__strcmpoffset = offset;
  1510. return &stb__qsort_strcmp;
  1511. }
  1512. int (*stb_qsort_stricmp(int offset))(const void *, const void *)
  1513. {
  1514. stb__strcmpoffset = offset;
  1515. return &stb__qsort_stricmp;
  1516. }
  1517. int (*stb_floatcmp(int offset))(const void *, const void *)
  1518. {
  1519. stb__floatcmpoffset = offset;
  1520. return &stb__floatcmp;
  1521. }
  1522. int (*stb_doublecmp(int offset))(const void *, const void *)
  1523. {
  1524. stb__doublecmpoffset = offset;
  1525. return &stb__doublecmp;
  1526. }
  1527. int (*stb_memcmp(int offset, int size))(const void *, const void *)
  1528. {
  1529. stb__memcmpoffset = offset;
  1530. stb__memcmpsize = size;
  1531. return &stb__memcmp;
  1532. }
  1533. #endif
  1534. //////////////////////////////////////////////////////////////////////////////
  1535. //
  1536. // Binary Search Toolkit
  1537. //
  1538. typedef struct
  1539. {
  1540. int minval, maxval, guess;
  1541. int mode, step;
  1542. } stb_search;
  1543. STB_EXTERN int stb_search_binary(stb_search *s, int minv, int maxv, int find_smallest);
  1544. STB_EXTERN int stb_search_open(stb_search *s, int minv, int find_smallest);
  1545. STB_EXTERN int stb_probe(stb_search *s, int compare, int *result); // return 0 when done
  1546. #ifdef STB_DEFINE
  1547. enum
  1548. {
  1549. STB_probe_binary_smallest,
  1550. STB_probe_binary_largest,
  1551. STB_probe_open_smallest,
  1552. STB_probe_open_largest,
  1553. };
  1554. static int stb_probe_guess(stb_search *s, int *result)
  1555. {
  1556. switch(s->mode) {
  1557. case STB_probe_binary_largest:
  1558. if (s->minval == s->maxval) {
  1559. *result = s->minval;
  1560. return 0;
  1561. }
  1562. assert(s->minval < s->maxval);
  1563. // if a < b, then a < p <= b
  1564. s->guess = s->minval + (((unsigned) s->maxval - s->minval + 1) >> 1);
  1565. break;
  1566. case STB_probe_binary_smallest:
  1567. if (s->minval == s->maxval) {
  1568. *result = s->minval;
  1569. return 0;
  1570. }
  1571. assert(s->minval < s->maxval);
  1572. // if a < b, then a <= p < b
  1573. s->guess = s->minval + (((unsigned) s->maxval - s->minval) >> 1);
  1574. break;
  1575. case STB_probe_open_smallest:
  1576. case STB_probe_open_largest:
  1577. s->guess = s->maxval; // guess the current maxval
  1578. break;
  1579. }
  1580. *result = s->guess;
  1581. return 1;
  1582. }
  1583. int stb_probe(stb_search *s, int compare, int *result)
  1584. {
  1585. switch(s->mode) {
  1586. case STB_probe_open_smallest:
  1587. case STB_probe_open_largest: {
  1588. if (compare <= 0) {
  1589. // then it lies within minval & maxval
  1590. if (s->mode == STB_probe_open_smallest)
  1591. s->mode = STB_probe_binary_smallest;
  1592. else
  1593. s->mode = STB_probe_binary_largest;
  1594. } else {
  1595. // otherwise, we need to probe larger
  1596. s->minval = s->maxval + 1;
  1597. s->maxval = s->minval + s->step;
  1598. s->step += s->step;
  1599. }
  1600. break;
  1601. }
  1602. case STB_probe_binary_smallest: {
  1603. // if compare < 0, then s->minval <= a < p
  1604. // if compare = 0, then s->minval <= a <= p
  1605. // if compare > 0, then p < a <= s->maxval
  1606. if (compare <= 0)
  1607. s->maxval = s->guess;
  1608. else
  1609. s->minval = s->guess+1;
  1610. break;
  1611. }
  1612. case STB_probe_binary_largest: {
  1613. // if compare < 0, then s->minval <= a < p
  1614. // if compare = 0, then p <= a <= s->maxval
  1615. // if compare > 0, then p < a <= s->maxval
  1616. if (compare < 0)
  1617. s->maxval = s->guess-1;
  1618. else
  1619. s->minval = s->guess;
  1620. break;
  1621. }
  1622. }
  1623. return stb_probe_guess(s, result);
  1624. }
  1625. int stb_search_binary(stb_search *s, int minv, int maxv, int find_smallest)
  1626. {
  1627. int r;
  1628. if (maxv < minv) return minv-1;
  1629. s->minval = minv;
  1630. s->maxval = maxv;
  1631. s->mode = find_smallest ? STB_probe_binary_smallest : STB_probe_binary_largest;
  1632. stb_probe_guess(s, &r);
  1633. return r;
  1634. }
  1635. int stb_search_open(stb_search *s, int minv, int find_smallest)
  1636. {
  1637. int r;
  1638. s->step = 4;
  1639. s->minval = minv;
  1640. s->maxval = minv+s->step;
  1641. s->mode = find_smallest ? STB_probe_open_smallest : STB_probe_open_largest;
  1642. stb_probe_guess(s, &r);
  1643. return r;
  1644. }
  1645. #endif
  1646. //////////////////////////////////////////////////////////////////////////////
  1647. //
  1648. // String Processing
  1649. //
  1650. #define stb_prefixi(s,t) (0==stb_strnicmp((s),(t),strlen(t)))
  1651. enum stb_splitpath_flag
  1652. {
  1653. STB_PATH = 1,
  1654. STB_FILE = 2,
  1655. STB_EXT = 4,
  1656. STB_PATH_FILE = STB_PATH + STB_FILE,
  1657. STB_FILE_EXT = STB_FILE + STB_EXT,
  1658. STB_EXT_NO_PERIOD = 8,
  1659. };
  1660. STB_EXTERN char * stb_skipwhite(char *s);
  1661. STB_EXTERN char * stb_trimwhite(char *s);
  1662. STB_EXTERN char * stb_skipnewline(char *s);
  1663. STB_EXTERN char * stb_strncpy(char *s, char *t, int n);
  1664. STB_EXTERN char * stb_substr(char *t, int n);
  1665. STB_EXTERN char * stb_duplower(char *s);
  1666. STB_EXTERN void stb_tolower (char *s);
  1667. STB_EXTERN char * stb_strchr2 (char *s, char p1, char p2);
  1668. STB_EXTERN char * stb_strrchr2(char *s, char p1, char p2);
  1669. STB_EXTERN char * stb_strtok(char *output, char *src, char *delimit);
  1670. STB_EXTERN char * stb_strtok_keep(char *output, char *src, char *delimit);
  1671. STB_EXTERN char * stb_strtok_invert(char *output, char *src, char *allowed);
  1672. STB_EXTERN char * stb_dupreplace(char *s, char *find, char *replace);
  1673. STB_EXTERN void stb_replaceinplace(char *s, char *find, char *replace);
  1674. STB_EXTERN char * stb_splitpath(char *output, char *src, int flag);
  1675. STB_EXTERN char * stb_splitpathdup(char *src, int flag);
  1676. STB_EXTERN char * stb_replacedir(char *output, char *src, char *dir);
  1677. STB_EXTERN char * stb_replaceext(char *output, char *src, char *ext);
  1678. STB_EXTERN void stb_fixpath(char *path);
  1679. STB_EXTERN char * stb_shorten_path_readable(char *path, int max_len);
  1680. STB_EXTERN int stb_suffix (char *s, char *t);
  1681. STB_EXTERN int stb_suffixi(char *s, char *t);
  1682. STB_EXTERN int stb_prefix (char *s, char *t);
  1683. STB_EXTERN char * stb_strichr(char *s, char t);
  1684. STB_EXTERN char * stb_stristr(char *s, char *t);
  1685. STB_EXTERN int stb_prefix_count(char *s, char *t);
  1686. STB_EXTERN const char * stb_plural(int n); // "s" or ""
  1687. STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n);
  1688. STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count);
  1689. STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
  1690. STB_EXTERN char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
  1691. STB_EXTERN char **stb_tokens_allowempty(char *src, char *delimit, int *count);
  1692. STB_EXTERN char **stb_tokens_stripwhite(char *src, char *delimit, int *count);
  1693. STB_EXTERN char **stb_tokens_withdelim(char *src, char *delimit, int *count);
  1694. STB_EXTERN char **stb_tokens_quoted(char *src, char *delimit, int *count);
  1695. // with 'quoted', allow delimiters to appear inside quotation marks, and don't
  1696. // strip whitespace inside them (and we delete the quotation marks unless they
  1697. // appear back to back, in which case they're considered escaped)
  1698. #ifdef STB_DEFINE
  1699. size_t stb_strscpy(char *d, const char *s, size_t n)
  1700. {
  1701. size_t len = strlen(s);
  1702. if (len >= n) {
  1703. if (n) d[0] = 0;
  1704. return 0;
  1705. }
  1706. stb_p_strcpy_s(d,n,s);
  1707. return len;
  1708. }
  1709. const char *stb_plural(int n)
  1710. {
  1711. return n == 1 ? "" : "s";
  1712. }
  1713. int stb_prefix(char *s, char *t)
  1714. {
  1715. while (*t)
  1716. if (*s++ != *t++)
  1717. return STB_FALSE;
  1718. return STB_TRUE;
  1719. }
  1720. int stb_prefix_count(char *s, char *t)
  1721. {
  1722. int c=0;
  1723. while (*t) {
  1724. if (*s++ != *t++)
  1725. break;
  1726. ++c;
  1727. }
  1728. return c;
  1729. }
  1730. int stb_suffix(char *s, char *t)
  1731. {
  1732. size_t n = strlen(s);
  1733. size_t m = strlen(t);
  1734. if (m <= n)
  1735. return 0 == strcmp(s+n-m, t);
  1736. else
  1737. return 0;
  1738. }
  1739. int stb_suffixi(char *s, char *t)
  1740. {
  1741. size_t n = strlen(s);
  1742. size_t m = strlen(t);
  1743. if (m <= n)
  1744. return 0 == stb_stricmp(s+n-m, t);
  1745. else
  1746. return 0;
  1747. }
  1748. // originally I was using this table so that I could create known sentinel
  1749. // values--e.g. change whitetable[0] to be true if I was scanning for whitespace,
  1750. // and false if I was scanning for nonwhite. I don't appear to be using that
  1751. // functionality anymore (I do for tokentable, though), so just replace it
  1752. // with isspace()
  1753. char *stb_skipwhite(char *s)
  1754. {
  1755. while (isspace((unsigned char) *s)) ++s;
  1756. return s;
  1757. }
  1758. char *stb_skipnewline(char *s)
  1759. {
  1760. if (s[0] == '\r' || s[0] == '\n') {
  1761. if (s[0]+s[1] == '\r' + '\n') ++s;
  1762. ++s;
  1763. }
  1764. return s;
  1765. }
  1766. char *stb_trimwhite(char *s)
  1767. {
  1768. int i,n;
  1769. s = stb_skipwhite(s);
  1770. n = (int) strlen(s);
  1771. for (i=n-1; i >= 0; --i)
  1772. if (!isspace(s[i]))
  1773. break;
  1774. s[i+1] = 0;
  1775. return s;
  1776. }
  1777. char *stb_strncpy(char *s, char *t, int n)
  1778. {
  1779. stb_p_strncpy_s(s,n+1,t,n);
  1780. s[n] = 0;
  1781. return s;
  1782. }
  1783. char *stb_substr(char *t, int n)
  1784. {
  1785. char *a;
  1786. int z = (int) strlen(t);
  1787. if (z < n) n = z;
  1788. a = (char *) malloc(n+1);
  1789. stb_p_strncpy_s(a,n+1,t,n);
  1790. a[n] = 0;
  1791. return a;
  1792. }
  1793. char *stb_duplower(char *s)
  1794. {
  1795. char *p = stb_p_strdup(s), *q = p;
  1796. while (*q) {
  1797. *q = tolower(*q);
  1798. ++q;
  1799. }
  1800. return p;
  1801. }
  1802. void stb_tolower(char *s)
  1803. {
  1804. while (*s) {
  1805. *s = tolower(*s);
  1806. ++s;
  1807. }
  1808. }
  1809. char *stb_strchr2(char *s, char x, char y)
  1810. {
  1811. for(; *s; ++s)
  1812. if (*s == x || *s == y)
  1813. return s;
  1814. return NULL;
  1815. }
  1816. char *stb_strrchr2(char *s, char x, char y)
  1817. {
  1818. char *r = NULL;
  1819. for(; *s; ++s)
  1820. if (*s == x || *s == y)
  1821. r = s;
  1822. return r;
  1823. }
  1824. char *stb_strichr(char *s, char t)
  1825. {
  1826. if (tolower(t) == toupper(t))
  1827. return strchr(s,t);
  1828. return stb_strchr2(s, (char) tolower(t), (char) toupper(t));
  1829. }
  1830. char *stb_stristr(char *s, char *t)
  1831. {
  1832. size_t n = strlen(t);
  1833. char *z;
  1834. if (n==0) return s;
  1835. while ((z = stb_strichr(s, *t)) != NULL) {
  1836. if (0==stb_strnicmp(z, t, n))
  1837. return z;
  1838. s = z+1;
  1839. }
  1840. return NULL;
  1841. }
  1842. static char *stb_strtok_raw(char *output, char *src, char *delimit, int keep, int invert)
  1843. {
  1844. if (invert) {
  1845. while (*src && strchr(delimit, *src) != NULL) {
  1846. *output++ = *src++;
  1847. }
  1848. } else {
  1849. while (*src && strchr(delimit, *src) == NULL) {
  1850. *output++ = *src++;
  1851. }
  1852. }
  1853. *output = 0;
  1854. if (keep)
  1855. return src;
  1856. else
  1857. return *src ? src+1 : src;
  1858. }
  1859. char *stb_strtok(char *output, char *src, char *delimit)
  1860. {
  1861. return stb_strtok_raw(output, src, delimit, 0, 0);
  1862. }
  1863. char *stb_strtok_keep(char *output, char *src, char *delimit)
  1864. {
  1865. return stb_strtok_raw(output, src, delimit, 1, 0);
  1866. }
  1867. char *stb_strtok_invert(char *output, char *src, char *delimit)
  1868. {
  1869. return stb_strtok_raw(output, src, delimit, 1,1);
  1870. }
  1871. static char **stb_tokens_raw(char *src_, char *delimit, int *count,
  1872. int stripwhite, int allow_empty, char *start, char *end)
  1873. {
  1874. int nested = 0;
  1875. unsigned char *src = (unsigned char *) src_;
  1876. static char stb_tokentable[256]; // rely on static initializion to 0
  1877. static char stable[256],etable[256];
  1878. char *out;
  1879. char **result;
  1880. int num=0;
  1881. unsigned char *s;
  1882. s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 1;
  1883. if (start) {
  1884. s = (unsigned char *) start; while (*s) stable[*s++] = 1;
  1885. s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
  1886. s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
  1887. }
  1888. stable[0] = 1;
  1889. // two passes through: the first time, counting how many
  1890. s = (unsigned char *) src;
  1891. while (*s) {
  1892. // state: just found delimiter
  1893. // skip further delimiters
  1894. if (!allow_empty) {
  1895. stb_tokentable[0] = 0;
  1896. while (stb_tokentable[*s])
  1897. ++s;
  1898. if (!*s) break;
  1899. }
  1900. ++num;
  1901. // skip further non-delimiters
  1902. stb_tokentable[0] = 1;
  1903. if (stripwhite == 2) { // quoted strings
  1904. while (!stb_tokentable[*s]) {
  1905. if (*s != '"')
  1906. ++s;
  1907. else {
  1908. ++s;
  1909. if (*s == '"')
  1910. ++s; // "" -> ", not start a string
  1911. else {
  1912. // begin a string
  1913. while (*s) {
  1914. if (s[0] == '"') {
  1915. if (s[1] == '"') s += 2; // "" -> "
  1916. else { ++s; break; } // terminating "
  1917. } else
  1918. ++s;
  1919. }
  1920. }
  1921. }
  1922. }
  1923. } else
  1924. while (nested || !stb_tokentable[*s]) {
  1925. if (stable[*s]) {
  1926. if (!*s) break;
  1927. if (end ? etable[*s] : nested)
  1928. --nested;
  1929. else
  1930. ++nested;
  1931. }
  1932. ++s;
  1933. }
  1934. if (allow_empty) {
  1935. if (*s) ++s;
  1936. }
  1937. }
  1938. // now num has the actual count... malloc our output structure
  1939. // need space for all the strings: strings won't be any longer than
  1940. // original input, since for every '\0' there's at least one delimiter
  1941. result = (char **) malloc(sizeof(*result) * (num+1) + (s-src+1));
  1942. if (result == NULL) return result;
  1943. out = (char *) (result + (num+1));
  1944. // second pass: copy out the data
  1945. s = (unsigned char *) src;
  1946. num = 0;
  1947. nested = 0;
  1948. while (*s) {
  1949. char *last_nonwhite;
  1950. // state: just found delimiter
  1951. // skip further delimiters
  1952. if (!allow_empty) {
  1953. stb_tokentable[0] = 0;
  1954. if (stripwhite)
  1955. while (stb_tokentable[*s] || isspace(*s))
  1956. ++s;
  1957. else
  1958. while (stb_tokentable[*s])
  1959. ++s;
  1960. } else if (stripwhite) {
  1961. while (isspace(*s)) ++s;
  1962. }
  1963. if (!*s) break;
  1964. // we're past any leading delimiters and whitespace
  1965. result[num] = out;
  1966. ++num;
  1967. // copy non-delimiters
  1968. stb_tokentable[0] = 1;
  1969. last_nonwhite = out-1;
  1970. if (stripwhite == 2) {
  1971. while (!stb_tokentable[*s]) {
  1972. if (*s != '"') {
  1973. if (!isspace(*s)) last_nonwhite = out;
  1974. *out++ = *s++;
  1975. } else {
  1976. ++s;
  1977. if (*s == '"') {
  1978. if (!isspace(*s)) last_nonwhite = out;
  1979. *out++ = *s++; // "" -> ", not start string
  1980. } else {
  1981. // begin a quoted string
  1982. while (*s) {
  1983. if (s[0] == '"') {
  1984. if (s[1] == '"') { *out++ = *s; s += 2; }
  1985. else { ++s; break; } // terminating "
  1986. } else
  1987. *out++ = *s++;
  1988. }
  1989. last_nonwhite = out-1; // all in quotes counts as non-white
  1990. }
  1991. }
  1992. }
  1993. } else {
  1994. while (nested || !stb_tokentable[*s]) {
  1995. if (!isspace(*s)) last_nonwhite = out;
  1996. if (stable[*s]) {
  1997. if (!*s) break;
  1998. if (end ? etable[*s] : nested)
  1999. --nested;
  2000. else
  2001. ++nested;
  2002. }
  2003. *out++ = *s++;
  2004. }
  2005. }
  2006. if (stripwhite) // rewind to last non-whitespace char
  2007. out = last_nonwhite+1;
  2008. *out++ = '\0';
  2009. if (*s) ++s; // skip delimiter
  2010. }
  2011. s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 0;
  2012. if (start) {
  2013. s = (unsigned char *) start; while (*s) stable[*s++] = 1;
  2014. s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
  2015. s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
  2016. }
  2017. if (count != NULL) *count = num;
  2018. result[num] = 0;
  2019. return result;
  2020. }
  2021. char **stb_tokens(char *src, char *delimit, int *count)
  2022. {
  2023. return stb_tokens_raw(src,delimit,count,0,0,0,0);
  2024. }
  2025. char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
  2026. {
  2027. return stb_tokens_raw(src,delimit,count,0,0,nest_in,nest_out);
  2028. }
  2029. char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
  2030. {
  2031. return stb_tokens_raw(src,delimit,count,0,1,nest_in,nest_out);
  2032. }
  2033. char **stb_tokens_allowempty(char *src, char *delimit, int *count)
  2034. {
  2035. return stb_tokens_raw(src,delimit,count,0,1,0,0);
  2036. }
  2037. char **stb_tokens_stripwhite(char *src, char *delimit, int *count)
  2038. {
  2039. return stb_tokens_raw(src,delimit,count,1,1,0,0);
  2040. }
  2041. char **stb_tokens_quoted(char *src, char *delimit, int *count)
  2042. {
  2043. return stb_tokens_raw(src,delimit,count,2,1,0,0);
  2044. }
  2045. char *stb_dupreplace(char *src, char *find, char *replace)
  2046. {
  2047. size_t len_find = strlen(find);
  2048. size_t len_replace = strlen(replace);
  2049. int count = 0;
  2050. char *s,*p,*q;
  2051. s = strstr(src, find);
  2052. if (s == NULL) return stb_p_strdup(src);
  2053. do {
  2054. ++count;
  2055. s = strstr(s + len_find, find);
  2056. } while (s != NULL);
  2057. p = (char *) malloc(strlen(src) + count * (len_replace - len_find) + 1);
  2058. if (p == NULL) return p;
  2059. q = p;
  2060. s = src;
  2061. for (;;) {
  2062. char *t = strstr(s, find);
  2063. if (t == NULL) {
  2064. stb_p_strcpy_s(q,strlen(src)+count*(len_replace-len_find)+1,s);
  2065. assert(strlen(p) == strlen(src) + count*(len_replace-len_find));
  2066. return p;
  2067. }
  2068. memcpy(q, s, t-s);
  2069. q += t-s;
  2070. memcpy(q, replace, len_replace);
  2071. q += len_replace;
  2072. s = t + len_find;
  2073. }
  2074. }
  2075. void stb_replaceinplace(char *src, char *find, char *replace)
  2076. {
  2077. size_t len_find = strlen(find);
  2078. size_t len_replace = strlen(replace);
  2079. int delta;
  2080. char *s,*p,*q;
  2081. delta = (int) (len_replace - len_find);
  2082. assert(delta <= 0);
  2083. if (delta > 0) return;
  2084. p = strstr(src, find);
  2085. if (p == NULL) return;
  2086. s = q = p;
  2087. while (*s) {
  2088. memcpy(q, replace, len_replace);
  2089. p += len_find;
  2090. q += len_replace;
  2091. s = strstr(p, find);
  2092. if (s == NULL) s = p + strlen(p);
  2093. memmove(q, p, s-p);
  2094. q += s-p;
  2095. p = s;
  2096. }
  2097. *q = 0;
  2098. }
  2099. void stb_fixpath(char *path)
  2100. {
  2101. for(; *path; ++path)
  2102. if (*path == '\\')
  2103. *path = '/';
  2104. }
  2105. void stb__add_section(char *buffer, char *data, ptrdiff_t curlen, ptrdiff_t newlen)
  2106. {
  2107. if (newlen < curlen) {
  2108. ptrdiff_t z1 = newlen >> 1, z2 = newlen-z1;
  2109. memcpy(buffer, data, z1-1);
  2110. buffer[z1-1] = '.';
  2111. buffer[z1-0] = '.';
  2112. memcpy(buffer+z1+1, data+curlen-z2+1, z2-1);
  2113. } else
  2114. memcpy(buffer, data, curlen);
  2115. }
  2116. char * stb_shorten_path_readable(char *path, int len)
  2117. {
  2118. static char buffer[1024];
  2119. ptrdiff_t n = strlen(path),n1,n2,r1,r2;
  2120. char *s;
  2121. if (n <= len) return path;
  2122. if (len > 1024) return path;
  2123. s = stb_strrchr2(path, '/', '\\');
  2124. if (s) {
  2125. n1 = s - path + 1;
  2126. n2 = n - n1;
  2127. ++s;
  2128. } else {
  2129. n1 = 0;
  2130. n2 = n;
  2131. s = path;
  2132. }
  2133. // now we need to reduce r1 and r2 so that they fit in len
  2134. if (n1 < len>>1) {
  2135. r1 = n1;
  2136. r2 = len - r1;
  2137. } else if (n2 < len >> 1) {
  2138. r2 = n2;
  2139. r1 = len - r2;
  2140. } else {
  2141. r1 = n1 * len / n;
  2142. r2 = n2 * len / n;
  2143. if (r1 < len>>2) r1 = len>>2, r2 = len-r1;
  2144. if (r2 < len>>2) r2 = len>>2, r1 = len-r2;
  2145. }
  2146. assert(r1 <= n1 && r2 <= n2);
  2147. if (n1)
  2148. stb__add_section(buffer, path, n1, r1);
  2149. stb__add_section(buffer+r1, s, n2, r2);
  2150. buffer[len] = 0;
  2151. return buffer;
  2152. }
  2153. static char *stb__splitpath_raw(char *buffer, char *path, int flag)
  2154. {
  2155. ptrdiff_t len=0,x,y, n = (int) strlen(path), f1,f2;
  2156. char *s = stb_strrchr2(path, '/', '\\');
  2157. char *t = strrchr(path, '.');
  2158. if (s && t && t < s) t = NULL;
  2159. if (!s) {
  2160. // check for drive
  2161. if (isalpha(path[0]) && path[1] == ':')
  2162. s = &path[1];
  2163. }
  2164. if (s) ++s;
  2165. if (flag == STB_EXT_NO_PERIOD)
  2166. flag |= STB_EXT;
  2167. if (!(flag & (STB_PATH | STB_FILE | STB_EXT))) return NULL;
  2168. f1 = s == NULL ? 0 : s-path; // start of filename
  2169. f2 = t == NULL ? n : t-path; // just past end of filename
  2170. if (flag & STB_PATH) {
  2171. x = 0; if (f1 == 0 && flag == STB_PATH) len=2;
  2172. } else if (flag & STB_FILE) {
  2173. x = f1;
  2174. } else {
  2175. x = f2;
  2176. if (flag & STB_EXT_NO_PERIOD)
  2177. if (path[x] == '.')
  2178. ++x;
  2179. }
  2180. if (flag & STB_EXT)
  2181. y = n;
  2182. else if (flag & STB_FILE)
  2183. y = f2;
  2184. else
  2185. y = f1;
  2186. if (buffer == NULL) {
  2187. buffer = (char *) malloc(y-x + len + 1);
  2188. if (!buffer) return NULL;
  2189. }
  2190. if (len) { stb_p_strcpy_s(buffer, 3, "./"); return buffer; }
  2191. stb_strncpy(buffer, path+(int)x, (int)(y-x));
  2192. return buffer;
  2193. }
  2194. char *stb_splitpath(char *output, char *src, int flag)
  2195. {
  2196. return stb__splitpath_raw(output, src, flag);
  2197. }
  2198. char *stb_splitpathdup(char *src, int flag)
  2199. {
  2200. return stb__splitpath_raw(NULL, src, flag);
  2201. }
  2202. char *stb_replacedir(char *output, char *src, char *dir)
  2203. {
  2204. char buffer[4096];
  2205. stb_splitpath(buffer, src, STB_FILE | STB_EXT);
  2206. if (dir)
  2207. stb_p_sprintf(output stb_p_size(9999), "%s/%s", dir, buffer);
  2208. else
  2209. stb_p_strcpy_s(output, sizeof(buffer), buffer); // @UNSAFE
  2210. return output;
  2211. }
  2212. char *stb_replaceext(char *output, char *src, char *ext)
  2213. {
  2214. char buffer[4096];
  2215. stb_splitpath(buffer, src, STB_PATH | STB_FILE);
  2216. if (ext)
  2217. stb_p_sprintf(output stb_p_size(9999), "%s.%s", buffer, ext[0] == '.' ? ext+1 : ext);
  2218. else
  2219. stb_p_strcpy_s(output, sizeof(buffer), buffer); // @UNSAFE
  2220. return output;
  2221. }
  2222. #endif
  2223. //////////////////////////////////////////////////////////////////////////////
  2224. //
  2225. // stb_alloc - hierarchical allocator
  2226. //
  2227. // inspired by http://swapped.cc/halloc
  2228. //
  2229. //
  2230. // When you alloc a given block through stb_alloc, you have these choices:
  2231. //
  2232. // 1. does it have a parent?
  2233. // 2. can it have children?
  2234. // 3. can it be freed directly?
  2235. // 4. is it transferrable?
  2236. // 5. what is its alignment?
  2237. //
  2238. // Here are interesting combinations of those:
  2239. //
  2240. // children free transfer alignment
  2241. // arena Y Y N n/a
  2242. // no-overhead, chunked N N N normal
  2243. // string pool alloc N N N 1
  2244. // parent-ptr, chunked Y N N normal
  2245. // low-overhead, unchunked N Y Y normal
  2246. // general purpose alloc Y Y Y normal
  2247. //
  2248. // Unchunked allocations will probably return 16-aligned pointers. If
  2249. // we 16-align the results, we have room for 4 pointers. For smaller
  2250. // allocations that allow finer alignment, we can reduce the pointers.
  2251. //
  2252. // The strategy is that given a pointer, assuming it has a header (only
  2253. // the no-overhead allocations have no header), we can determine the
  2254. // type of the header fields, and the number of them, by stepping backwards
  2255. // through memory and looking at the tags in the bottom bits.
  2256. //
  2257. // Implementation strategy:
  2258. // chunked allocations come from the middle of chunks, and can't
  2259. // be freed. thefore they do not need to be on a sibling chain.
  2260. // they may need child pointers if they have children.
  2261. //
  2262. // chunked, with-children
  2263. // void *parent;
  2264. //
  2265. // unchunked, no-children -- reduced storage
  2266. // void *next_sibling;
  2267. // void *prev_sibling_nextp;
  2268. //
  2269. // unchunked, general
  2270. // void *first_child;
  2271. // void *next_sibling;
  2272. // void *prev_sibling_nextp;
  2273. // void *chunks;
  2274. //
  2275. // so, if we code each of these fields with different bit patterns
  2276. // (actually same one for next/prev/child), then we can identify which
  2277. // each one is from the last field.
  2278. STB_EXTERN void stb_free(void *p);
  2279. STB_EXTERN void *stb_malloc_global(size_t size);
  2280. STB_EXTERN void *stb_malloc(void *context, size_t size);
  2281. STB_EXTERN void *stb_malloc_nofree(void *context, size_t size);
  2282. STB_EXTERN void *stb_malloc_leaf(void *context, size_t size);
  2283. STB_EXTERN void *stb_malloc_raw(void *context, size_t size);
  2284. STB_EXTERN void *stb_realloc(void *ptr, size_t newsize);
  2285. STB_EXTERN void stb_reassign(void *new_context, void *ptr);
  2286. STB_EXTERN void stb_malloc_validate(void *p, void *parent);
  2287. extern int stb_alloc_chunk_size ;
  2288. extern int stb_alloc_count_free ;
  2289. extern int stb_alloc_count_alloc;
  2290. extern int stb_alloc_alignment ;
  2291. #ifdef STB_DEFINE
  2292. int stb_alloc_chunk_size = 65536;
  2293. int stb_alloc_count_free = 0;
  2294. int stb_alloc_count_alloc = 0;
  2295. int stb_alloc_alignment = -16;
  2296. typedef struct stb__chunk
  2297. {
  2298. struct stb__chunk *next;
  2299. int data_left;
  2300. int alloc;
  2301. } stb__chunk;
  2302. typedef struct
  2303. {
  2304. void * next;
  2305. void ** prevn;
  2306. } stb__nochildren;
  2307. typedef struct
  2308. {
  2309. void ** prevn;
  2310. void * child;
  2311. void * next;
  2312. stb__chunk *chunks;
  2313. } stb__alloc;
  2314. typedef struct
  2315. {
  2316. stb__alloc *parent;
  2317. } stb__chunked;
  2318. #define STB__PARENT 1
  2319. #define STB__CHUNKS 2
  2320. typedef enum
  2321. {
  2322. STB__nochildren = 0,
  2323. STB__chunked = STB__PARENT,
  2324. STB__alloc = STB__CHUNKS,
  2325. STB__chunk_raw = 4,
  2326. } stb__alloc_type;
  2327. // these functions set the bottom bits of a pointer efficiently
  2328. #define STB__DECODE(x,v) ((void *) ((char *) (x) - (v)))
  2329. #define STB__ENCODE(x,v) ((void *) ((char *) (x) + (v)))
  2330. #define stb__parent(z) (stb__alloc *) STB__DECODE((z)->parent, STB__PARENT)
  2331. #define stb__chunks(z) (stb__chunk *) STB__DECODE((z)->chunks, STB__CHUNKS)
  2332. #define stb__setparent(z,p) (z)->parent = (stb__alloc *) STB__ENCODE((p), STB__PARENT)
  2333. #define stb__setchunks(z,c) (z)->chunks = (stb__chunk *) STB__ENCODE((c), STB__CHUNKS)
  2334. static stb__alloc stb__alloc_global =
  2335. {
  2336. NULL,
  2337. NULL,
  2338. NULL,
  2339. (stb__chunk *) STB__ENCODE(NULL, STB__CHUNKS)
  2340. };
  2341. static stb__alloc_type stb__identify(void *p)
  2342. {
  2343. void **q = (void **) p;
  2344. return (stb__alloc_type) ((stb_uinta) q[-1] & 3);
  2345. }
  2346. static void *** stb__prevn(void *p)
  2347. {
  2348. if (stb__identify(p) == STB__alloc) {
  2349. stb__alloc *s = (stb__alloc *) p - 1;
  2350. return &s->prevn;
  2351. } else {
  2352. stb__nochildren *s = (stb__nochildren *) p - 1;
  2353. return &s->prevn;
  2354. }
  2355. }
  2356. void stb_free(void *p)
  2357. {
  2358. if (p == NULL) return;
  2359. // count frees so that unit tests can see what's happening
  2360. ++stb_alloc_count_free;
  2361. switch(stb__identify(p)) {
  2362. case STB__chunked:
  2363. // freeing a chunked-block with children does nothing;
  2364. // they only get freed when the parent does
  2365. // surely this is wrong, and it should free them immediately?
  2366. // otherwise how are they getting put on the right chain?
  2367. return;
  2368. case STB__nochildren: {
  2369. stb__nochildren *s = (stb__nochildren *) p - 1;
  2370. // unlink from sibling chain
  2371. *(s->prevn) = s->next;
  2372. if (s->next)
  2373. *stb__prevn(s->next) = s->prevn;
  2374. free(s);
  2375. return;
  2376. }
  2377. case STB__alloc: {
  2378. stb__alloc *s = (stb__alloc *) p - 1;
  2379. stb__chunk *c, *n;
  2380. void *q;
  2381. // unlink from sibling chain, if any
  2382. *(s->prevn) = s->next;
  2383. if (s->next)
  2384. *stb__prevn(s->next) = s->prevn;
  2385. // first free chunks
  2386. c = (stb__chunk *) stb__chunks(s);
  2387. while (c != NULL) {
  2388. n = c->next;
  2389. stb_alloc_count_free += c->alloc;
  2390. free(c);
  2391. c = n;
  2392. }
  2393. // validating
  2394. stb__setchunks(s,NULL);
  2395. s->prevn = NULL;
  2396. s->next = NULL;
  2397. // now free children
  2398. while ((q = s->child) != NULL) {
  2399. stb_free(q);
  2400. }
  2401. // now free self
  2402. free(s);
  2403. return;
  2404. }
  2405. default:
  2406. assert(0); /* NOTREACHED */
  2407. }
  2408. }
  2409. void stb_malloc_validate(void *p, void *parent)
  2410. {
  2411. if (p == NULL) return;
  2412. switch(stb__identify(p)) {
  2413. case STB__chunked:
  2414. return;
  2415. case STB__nochildren: {
  2416. stb__nochildren *n = (stb__nochildren *) p - 1;
  2417. if (n->prevn)
  2418. assert(*n->prevn == p);
  2419. if (n->next) {
  2420. assert(*stb__prevn(n->next) == &n->next);
  2421. stb_malloc_validate(n, parent);
  2422. }
  2423. return;
  2424. }
  2425. case STB__alloc: {
  2426. stb__alloc *s = (stb__alloc *) p - 1;
  2427. if (s->prevn)
  2428. assert(*s->prevn == p);
  2429. if (s->child) {
  2430. assert(*stb__prevn(s->child) == &s->child);
  2431. stb_malloc_validate(s->child, p);
  2432. }
  2433. if (s->next) {
  2434. assert(*stb__prevn(s->next) == &s->next);
  2435. stb_malloc_validate(s->next, parent);
  2436. }
  2437. return;
  2438. }
  2439. default:
  2440. assert(0); /* NOTREACHED */
  2441. }
  2442. }
  2443. static void * stb__try_chunk(stb__chunk *c, int size, int align, int pre_align)
  2444. {
  2445. char *memblock = (char *) (c+1), *q;
  2446. stb_inta iq;
  2447. int start_offset;
  2448. // we going to allocate at the end of the chunk, not the start. confusing,
  2449. // but it means we don't need both a 'limit' and a 'cur', just a 'cur'.
  2450. // the block ends at: p + c->data_left
  2451. // then we move back by size
  2452. start_offset = c->data_left - size;
  2453. // now we need to check the alignment of that
  2454. q = memblock + start_offset;
  2455. iq = (stb_inta) q;
  2456. assert(sizeof(q) == sizeof(iq));
  2457. // suppose align = 2
  2458. // then we need to retreat iq far enough that (iq & (2-1)) == 0
  2459. // to get (iq & (align-1)) = 0 requires subtracting (iq & (align-1))
  2460. start_offset -= iq & (align-1);
  2461. assert(((stb_uinta) (memblock+start_offset) & (align-1)) == 0);
  2462. // now, if that + pre_align works, go for it!
  2463. start_offset -= pre_align;
  2464. if (start_offset >= 0) {
  2465. c->data_left = start_offset;
  2466. return memblock + start_offset;
  2467. }
  2468. return NULL;
  2469. }
  2470. static void stb__sort_chunks(stb__alloc *src)
  2471. {
  2472. // of the first two chunks, put the chunk with more data left in it first
  2473. stb__chunk *c = stb__chunks(src), *d;
  2474. if (c == NULL) return;
  2475. d = c->next;
  2476. if (d == NULL) return;
  2477. if (c->data_left > d->data_left) return;
  2478. c->next = d->next;
  2479. d->next = c;
  2480. stb__setchunks(src, d);
  2481. }
  2482. static void * stb__alloc_chunk(stb__alloc *src, int size, int align, int pre_align)
  2483. {
  2484. void *p;
  2485. stb__chunk *c = stb__chunks(src);
  2486. if (c && size <= stb_alloc_chunk_size) {
  2487. p = stb__try_chunk(c, size, align, pre_align);
  2488. if (p) { ++c->alloc; return p; }
  2489. // try a second chunk to reduce wastage
  2490. if (c->next) {
  2491. p = stb__try_chunk(c->next, size, align, pre_align);
  2492. if (p) { ++c->alloc; return p; }
  2493. // put the bigger chunk first, since the second will get buried
  2494. // the upshot of this is that, until it gets allocated from, chunk #2
  2495. // is always the largest remaining chunk. (could formalize
  2496. // this with a heap!)
  2497. stb__sort_chunks(src);
  2498. c = stb__chunks(src);
  2499. }
  2500. }
  2501. // allocate a new chunk
  2502. {
  2503. stb__chunk *n;
  2504. int chunk_size = stb_alloc_chunk_size;
  2505. // we're going to allocate a new chunk to put this in
  2506. if (size > chunk_size)
  2507. chunk_size = size;
  2508. assert(sizeof(*n) + pre_align <= 16);
  2509. // loop trying to allocate a large enough chunk
  2510. // the loop is because the alignment may cause problems if it's big...
  2511. // and we don't know what our chunk alignment is going to be
  2512. while (1) {
  2513. n = (stb__chunk *) malloc(16 + chunk_size);
  2514. if (n == NULL) return NULL;
  2515. n->data_left = chunk_size - sizeof(*n);
  2516. p = stb__try_chunk(n, size, align, pre_align);
  2517. if (p != NULL) {
  2518. n->next = c;
  2519. stb__setchunks(src, n);
  2520. // if we just used up the whole block immediately,
  2521. // move the following chunk up
  2522. n->alloc = 1;
  2523. if (size == chunk_size)
  2524. stb__sort_chunks(src);
  2525. return p;
  2526. }
  2527. free(n);
  2528. chunk_size += 16+align;
  2529. }
  2530. }
  2531. }
  2532. static stb__alloc * stb__get_context(void *context)
  2533. {
  2534. if (context == NULL) {
  2535. return &stb__alloc_global;
  2536. } else {
  2537. int u = stb__identify(context);
  2538. // if context is chunked, grab parent
  2539. if (u == STB__chunked) {
  2540. stb__chunked *s = (stb__chunked *) context - 1;
  2541. return stb__parent(s);
  2542. } else {
  2543. return (stb__alloc *) context - 1;
  2544. }
  2545. }
  2546. }
  2547. static void stb__insert_alloc(stb__alloc *src, stb__alloc *s)
  2548. {
  2549. s->prevn = &src->child;
  2550. s->next = src->child;
  2551. src->child = s+1;
  2552. if (s->next)
  2553. *stb__prevn(s->next) = &s->next;
  2554. }
  2555. static void stb__insert_nochild(stb__alloc *src, stb__nochildren *s)
  2556. {
  2557. s->prevn = &src->child;
  2558. s->next = src->child;
  2559. src->child = s+1;
  2560. if (s->next)
  2561. *stb__prevn(s->next) = &s->next;
  2562. }
  2563. static void * malloc_base(void *context, size_t size, stb__alloc_type t, int align)
  2564. {
  2565. void *p;
  2566. stb__alloc *src = stb__get_context(context);
  2567. if (align <= 0) {
  2568. // compute worst-case C packed alignment
  2569. // e.g. a 24-byte struct is 8-aligned
  2570. int align_proposed = 1 << stb_lowbit8((unsigned int) size);
  2571. if (align_proposed < 0)
  2572. align_proposed = 4;
  2573. if (align_proposed == 0) {
  2574. if (size == 0)
  2575. align_proposed = 1;
  2576. else
  2577. align_proposed = 256;
  2578. }
  2579. // a negative alignment means 'don't align any larger
  2580. // than this'; so -16 means we align 1,2,4,8, or 16
  2581. if (align < 0) {
  2582. if (align_proposed > -align)
  2583. align_proposed = -align;
  2584. }
  2585. align = align_proposed;
  2586. }
  2587. assert(stb_is_pow2(align));
  2588. // don't cause misalignment when allocating nochildren
  2589. if (t == STB__nochildren && align > 8)
  2590. t = STB__alloc;
  2591. switch (t) {
  2592. case STB__alloc: {
  2593. stb__alloc *s = (stb__alloc *) malloc(size + sizeof(*s));
  2594. if (s == NULL) return NULL;
  2595. p = s+1;
  2596. s->child = NULL;
  2597. stb__insert_alloc(src, s);
  2598. stb__setchunks(s,NULL);
  2599. break;
  2600. }
  2601. case STB__nochildren: {
  2602. stb__nochildren *s = (stb__nochildren *) malloc(size + sizeof(*s));
  2603. if (s == NULL) return NULL;
  2604. p = s+1;
  2605. stb__insert_nochild(src, s);
  2606. break;
  2607. }
  2608. case STB__chunk_raw: {
  2609. p = stb__alloc_chunk(src, (int) size, align, 0);
  2610. if (p == NULL) return NULL;
  2611. break;
  2612. }
  2613. case STB__chunked: {
  2614. stb__chunked *s;
  2615. if (align < sizeof(stb_uintptr)) align = sizeof(stb_uintptr);
  2616. s = (stb__chunked *) stb__alloc_chunk(src, (int) size, align, sizeof(*s));
  2617. if (s == NULL) return NULL;
  2618. stb__setparent(s, src);
  2619. p = s+1;
  2620. break;
  2621. }
  2622. default: p = NULL; assert(0); /* NOTREACHED */
  2623. }
  2624. ++stb_alloc_count_alloc;
  2625. return p;
  2626. }
  2627. void *stb_malloc_global(size_t size)
  2628. {
  2629. return malloc_base(NULL, size, STB__alloc, stb_alloc_alignment);
  2630. }
  2631. void *stb_malloc(void *context, size_t size)
  2632. {
  2633. return malloc_base(context, size, STB__alloc, stb_alloc_alignment);
  2634. }
  2635. void *stb_malloc_nofree(void *context, size_t size)
  2636. {
  2637. return malloc_base(context, size, STB__chunked, stb_alloc_alignment);
  2638. }
  2639. void *stb_malloc_leaf(void *context, size_t size)
  2640. {
  2641. return malloc_base(context, size, STB__nochildren, stb_alloc_alignment);
  2642. }
  2643. void *stb_malloc_raw(void *context, size_t size)
  2644. {
  2645. return malloc_base(context, size, STB__chunk_raw, stb_alloc_alignment);
  2646. }
  2647. char *stb_malloc_string(void *context, size_t size)
  2648. {
  2649. return (char *) malloc_base(context, size, STB__chunk_raw, 1);
  2650. }
  2651. void *stb_realloc(void *ptr, size_t newsize)
  2652. {
  2653. stb__alloc_type t;
  2654. if (ptr == NULL) return stb_malloc(NULL, newsize);
  2655. if (newsize == 0) { stb_free(ptr); return NULL; }
  2656. t = stb__identify(ptr);
  2657. assert(t == STB__alloc || t == STB__nochildren);
  2658. if (t == STB__alloc) {
  2659. stb__alloc *s = (stb__alloc *) ptr - 1;
  2660. s = (stb__alloc *) realloc(s, newsize + sizeof(*s));
  2661. if (s == NULL) return NULL;
  2662. ptr = s+1;
  2663. // update pointers
  2664. (*s->prevn) = ptr;
  2665. if (s->next)
  2666. *stb__prevn(s->next) = &s->next;
  2667. if (s->child)
  2668. *stb__prevn(s->child) = &s->child;
  2669. return ptr;
  2670. } else {
  2671. stb__nochildren *s = (stb__nochildren *) ptr - 1;
  2672. s = (stb__nochildren *) realloc(ptr, newsize + sizeof(s));
  2673. if (s == NULL) return NULL;
  2674. // update pointers
  2675. (*s->prevn) = s+1;
  2676. if (s->next)
  2677. *stb__prevn(s->next) = &s->next;
  2678. return s+1;
  2679. }
  2680. }
  2681. void *stb_realloc_c(void *context, void *ptr, size_t newsize)
  2682. {
  2683. if (ptr == NULL) return stb_malloc(context, newsize);
  2684. if (newsize == 0) { stb_free(ptr); return NULL; }
  2685. // @TODO: verify you haven't changed contexts
  2686. return stb_realloc(ptr, newsize);
  2687. }
  2688. void stb_reassign(void *new_context, void *ptr)
  2689. {
  2690. stb__alloc *src = stb__get_context(new_context);
  2691. stb__alloc_type t = stb__identify(ptr);
  2692. assert(t == STB__alloc || t == STB__nochildren);
  2693. if (t == STB__alloc) {
  2694. stb__alloc *s = (stb__alloc *) ptr - 1;
  2695. // unlink from old
  2696. *(s->prevn) = s->next;
  2697. if (s->next)
  2698. *stb__prevn(s->next) = s->prevn;
  2699. stb__insert_alloc(src, s);
  2700. } else {
  2701. stb__nochildren *s = (stb__nochildren *) ptr - 1;
  2702. // unlink from old
  2703. *(s->prevn) = s->next;
  2704. if (s->next)
  2705. *stb__prevn(s->next) = s->prevn;
  2706. stb__insert_nochild(src, s);
  2707. }
  2708. }
  2709. #endif
  2710. //////////////////////////////////////////////////////////////////////////////
  2711. //
  2712. // stb_arr
  2713. //
  2714. // An stb_arr is directly useable as a pointer (use the actual type in your
  2715. // definition), but when it resizes, it returns a new pointer and you can't
  2716. // use the old one, so you have to be careful to copy-in-out as necessary.
  2717. //
  2718. // Use a NULL pointer as a 0-length array.
  2719. //
  2720. // float *my_array = NULL, *temp;
  2721. //
  2722. // // add elements on the end one at a time
  2723. // stb_arr_push(my_array, 0.0f);
  2724. // stb_arr_push(my_array, 1.0f);
  2725. // stb_arr_push(my_array, 2.0f);
  2726. //
  2727. // assert(my_array[1] == 2.0f);
  2728. //
  2729. // // add an uninitialized element at the end, then assign it
  2730. // *stb_arr_add(my_array) = 3.0f;
  2731. //
  2732. // // add three uninitialized elements at the end
  2733. // temp = stb_arr_addn(my_array,3);
  2734. // temp[0] = 4.0f;
  2735. // temp[1] = 5.0f;
  2736. // temp[2] = 6.0f;
  2737. //
  2738. // assert(my_array[5] == 5.0f);
  2739. //
  2740. // // remove the last one
  2741. // stb_arr_pop(my_array);
  2742. //
  2743. // assert(stb_arr_len(my_array) == 6);
  2744. #ifdef STB_MALLOC_WRAPPER
  2745. #define STB__PARAMS , char *file, int line
  2746. #define STB__ARGS , file, line
  2747. #else
  2748. #define STB__PARAMS
  2749. #define STB__ARGS
  2750. #endif
  2751. // calling this function allocates an empty stb_arr attached to p
  2752. // (whereas NULL isn't attached to anything)
  2753. STB_EXTERN void stb_arr_malloc(void **target, void *context);
  2754. // call this function with a non-NULL value to have all successive
  2755. // stbs that are created be attached to the associated parent. Note
  2756. // that once a given stb_arr is non-empty, it stays attached to its
  2757. // current parent, even if you call this function again.
  2758. // it turns the previous value, so you can restore it
  2759. STB_EXTERN void* stb_arr_malloc_parent(void *p);
  2760. // simple functions written on top of other functions
  2761. #define stb_arr_empty(a) ( stb_arr_len(a) == 0 )
  2762. #define stb_arr_add(a) ( stb_arr_addn((a),1) )
  2763. #define stb_arr_push(a,v) ( *stb_arr_add(a)=(v) )
  2764. typedef struct
  2765. {
  2766. int len, limit;
  2767. int stb_malloc;
  2768. unsigned int signature;
  2769. } stb__arr;
  2770. #define stb_arr_signature 0x51bada7b // ends with 0123 in decimal
  2771. // access the header block stored before the data
  2772. #define stb_arrhead(a) /*lint --e(826)*/ (((stb__arr *) (a)) - 1)
  2773. #define stb_arrhead2(a) /*lint --e(826)*/ (((stb__arr *) (a)) - 1)
  2774. #ifdef STB_DEBUG
  2775. #define stb_arr_check(a) assert(!a || stb_arrhead(a)->signature == stb_arr_signature)
  2776. #define stb_arr_check2(a) assert(!a || stb_arrhead2(a)->signature == stb_arr_signature)
  2777. #else
  2778. #define stb_arr_check(a) ((void) 0)
  2779. #define stb_arr_check2(a) ((void) 0)
  2780. #endif
  2781. // ARRAY LENGTH
  2782. // get the array length; special case if pointer is NULL
  2783. #define stb_arr_len(a) (a ? stb_arrhead(a)->len : 0)
  2784. #define stb_arr_len2(a) ((stb__arr *) (a) ? stb_arrhead2(a)->len : 0)
  2785. #define stb_arr_lastn(a) (stb_arr_len(a)-1)
  2786. // check whether a given index is valid -- tests 0 <= i < stb_arr_len(a)
  2787. #define stb_arr_valid(a,i) (a ? (int) (i) < stb_arrhead(a)->len : 0)
  2788. // change the array length so is is exactly N entries long, creating
  2789. // uninitialized entries as needed
  2790. #define stb_arr_setlen(a,n) \
  2791. (stb__arr_setlen((void **) &(a), sizeof(a[0]), (n)))
  2792. // change the array length so that N is a valid index (that is, so
  2793. // it is at least N entries long), creating uninitialized entries as needed
  2794. #define stb_arr_makevalid(a,n) \
  2795. (stb_arr_len(a) < (n)+1 ? stb_arr_setlen(a,(n)+1),(a) : (a))
  2796. // remove the last element of the array, returning it
  2797. #define stb_arr_pop(a) ((stb_arr_check(a), (a))[--stb_arrhead(a)->len])
  2798. // access the last element in the array
  2799. #define stb_arr_last(a) ((stb_arr_check(a), (a))[stb_arr_len(a)-1])
  2800. // is iterator at end of list?
  2801. #define stb_arr_end(a,i) ((i) >= &(a)[stb_arr_len(a)])
  2802. // (internal) change the allocated length of the array
  2803. #define stb_arr__grow(a,n) (stb_arr_check(a), stb_arrhead(a)->len += (n))
  2804. // add N new uninitialized elements to the end of the array
  2805. #define stb_arr__addn(a,n) /*lint --e(826)*/ \
  2806. ((stb_arr_len(a)+(n) > stb_arrcurmax(a)) \
  2807. ? (stb__arr_addlen((void **) &(a),sizeof(*a),(n)),0) \
  2808. : ((stb_arr__grow(a,n), 0)))
  2809. // add N new uninitialized elements to the end of the array, and return
  2810. // a pointer to the first new one
  2811. #define stb_arr_addn(a,n) (stb_arr__addn((a),n),(a)+stb_arr_len(a)-(n))
  2812. // add N new uninitialized elements starting at index 'i'
  2813. #define stb_arr_insertn(a,i,n) (stb__arr_insertn((void **) &(a), sizeof(*a), (i), (n)))
  2814. // insert an element at i
  2815. #define stb_arr_insert(a,i,v) (stb__arr_insertn((void **) &(a), sizeof(*a), (i), (1)), ((a)[i] = v))
  2816. // delete N elements from the middle starting at index 'i'
  2817. #define stb_arr_deleten(a,i,n) (stb__arr_deleten((void **) &(a), sizeof(*a), (i), (n)))
  2818. // delete the i'th element
  2819. #define stb_arr_delete(a,i) stb_arr_deleten(a,i,1)
  2820. // delete the i'th element, swapping down from the end
  2821. #define stb_arr_fastdelete(a,i) \
  2822. (stb_swap(&a[i], &a[stb_arrhead(a)->len-1], sizeof(*a)), stb_arr_pop(a))
  2823. // ARRAY STORAGE
  2824. // get the array maximum storage; special case if NULL
  2825. #define stb_arrcurmax(a) (a ? stb_arrhead(a)->limit : 0)
  2826. #define stb_arrcurmax2(a) (a ? stb_arrhead2(a)->limit : 0)
  2827. // set the maxlength of the array to n in anticipation of further growth
  2828. #define stb_arr_setsize(a,n) (stb_arr_check(a), stb__arr_setsize((void **) &(a),sizeof((a)[0]),n))
  2829. // make sure maxlength is large enough for at least N new allocations
  2830. #define stb_arr_atleast(a,n) (stb_arr_len(a)+(n) > stb_arrcurmax(a) \
  2831. ? stb_arr_setsize((a), (n)) : 0)
  2832. // make a copy of a given array (copies contents via 'memcpy'!)
  2833. #define stb_arr_copy(a) stb__arr_copy(a, sizeof((a)[0]))
  2834. // compute the storage needed to store all the elements of the array
  2835. #define stb_arr_storage(a) (stb_arr_len(a) * sizeof((a)[0]))
  2836. #define stb_arr_for(v,arr) for((v)=(arr); (v) < (arr)+stb_arr_len(arr); ++(v))
  2837. // IMPLEMENTATION
  2838. STB_EXTERN void stb_arr_free_(void **p);
  2839. STB_EXTERN void *stb__arr_copy_(void *p, int elem_size);
  2840. STB_EXTERN void stb__arr_setsize_(void **p, int size, int limit STB__PARAMS);
  2841. STB_EXTERN void stb__arr_setlen_(void **p, int size, int newlen STB__PARAMS);
  2842. STB_EXTERN void stb__arr_addlen_(void **p, int size, int addlen STB__PARAMS);
  2843. STB_EXTERN void stb__arr_deleten_(void **p, int size, int loc, int n STB__PARAMS);
  2844. STB_EXTERN void stb__arr_insertn_(void **p, int size, int loc, int n STB__PARAMS);
  2845. #define stb_arr_free(p) stb_arr_free_((void **) &(p))
  2846. #define stb__arr_copy stb__arr_copy_
  2847. #ifndef STB_MALLOC_WRAPPER
  2848. #define stb__arr_setsize stb__arr_setsize_
  2849. #define stb__arr_setlen stb__arr_setlen_
  2850. #define stb__arr_addlen stb__arr_addlen_
  2851. #define stb__arr_deleten stb__arr_deleten_
  2852. #define stb__arr_insertn stb__arr_insertn_
  2853. #else
  2854. #define stb__arr_addlen(p,s,n) stb__arr_addlen_(p,s,n,__FILE__,__LINE__)
  2855. #define stb__arr_setlen(p,s,n) stb__arr_setlen_(p,s,n,__FILE__,__LINE__)
  2856. #define stb__arr_setsize(p,s,n) stb__arr_setsize_(p,s,n,__FILE__,__LINE__)
  2857. #define stb__arr_deleten(p,s,i,n) stb__arr_deleten_(p,s,i,n,__FILE__,__LINE__)
  2858. #define stb__arr_insertn(p,s,i,n) stb__arr_insertn_(p,s,i,n,__FILE__,__LINE__)
  2859. #endif
  2860. #ifdef STB_DEFINE
  2861. static void *stb__arr_context;
  2862. void *stb_arr_malloc_parent(void *p)
  2863. {
  2864. void *q = stb__arr_context;
  2865. stb__arr_context = p;
  2866. return q;
  2867. }
  2868. void stb_arr_malloc(void **target, void *context)
  2869. {
  2870. stb__arr *q = (stb__arr *) stb_malloc(context, sizeof(*q));
  2871. q->len = q->limit = 0;
  2872. q->stb_malloc = 1;
  2873. q->signature = stb_arr_signature;
  2874. *target = (void *) (q+1);
  2875. }
  2876. static void * stb__arr_malloc(int size)
  2877. {
  2878. if (stb__arr_context)
  2879. return stb_malloc(stb__arr_context, size);
  2880. return malloc(size);
  2881. }
  2882. void * stb__arr_copy_(void *p, int elem_size)
  2883. {
  2884. stb__arr *q;
  2885. if (p == NULL) return p;
  2886. q = (stb__arr *) stb__arr_malloc(sizeof(*q) + elem_size * stb_arrhead2(p)->limit);
  2887. stb_arr_check2(p);
  2888. memcpy(q, stb_arrhead2(p), sizeof(*q) + elem_size * stb_arrhead2(p)->len);
  2889. q->stb_malloc = !!stb__arr_context;
  2890. return q+1;
  2891. }
  2892. void stb_arr_free_(void **pp)
  2893. {
  2894. void *p = *pp;
  2895. stb_arr_check2(p);
  2896. if (p) {
  2897. stb__arr *q = stb_arrhead2(p);
  2898. if (q->stb_malloc)
  2899. stb_free(q);
  2900. else
  2901. free(q);
  2902. }
  2903. *pp = NULL;
  2904. }
  2905. static void stb__arrsize_(void **pp, int size, int limit, int len STB__PARAMS)
  2906. {
  2907. void *p = *pp;
  2908. stb__arr *a;
  2909. stb_arr_check2(p);
  2910. if (p == NULL) {
  2911. if (len == 0 && size == 0) return;
  2912. a = (stb__arr *) stb__arr_malloc(sizeof(*a) + size*limit);
  2913. a->limit = limit;
  2914. a->len = len;
  2915. a->stb_malloc = !!stb__arr_context;
  2916. a->signature = stb_arr_signature;
  2917. } else {
  2918. a = stb_arrhead2(p);
  2919. a->len = len;
  2920. if (a->limit < limit) {
  2921. void *p;
  2922. if (a->limit >= 4 && limit < a->limit * 2)
  2923. limit = a->limit * 2;
  2924. if (a->stb_malloc)
  2925. p = stb_realloc(a, sizeof(*a) + limit*size);
  2926. else
  2927. #ifdef STB_MALLOC_WRAPPER
  2928. p = stb__realloc(a, sizeof(*a) + limit*size, file, line);
  2929. #else
  2930. p = realloc(a, sizeof(*a) + limit*size);
  2931. #endif
  2932. if (p) {
  2933. a = (stb__arr *) p;
  2934. a->limit = limit;
  2935. } else {
  2936. // throw an error!
  2937. }
  2938. }
  2939. }
  2940. a->len = stb_min(a->len, a->limit);
  2941. *pp = a+1;
  2942. }
  2943. void stb__arr_setsize_(void **pp, int size, int limit STB__PARAMS)
  2944. {
  2945. void *p = *pp;
  2946. stb_arr_check2(p);
  2947. stb__arrsize_(pp, size, limit, stb_arr_len2(p) STB__ARGS);
  2948. }
  2949. void stb__arr_setlen_(void **pp, int size, int newlen STB__PARAMS)
  2950. {
  2951. void *p = *pp;
  2952. stb_arr_check2(p);
  2953. if (stb_arrcurmax2(p) < newlen || p == NULL) {
  2954. stb__arrsize_(pp, size, newlen, newlen STB__ARGS);
  2955. } else {
  2956. stb_arrhead2(p)->len = newlen;
  2957. }
  2958. }
  2959. void stb__arr_addlen_(void **p, int size, int addlen STB__PARAMS)
  2960. {
  2961. stb__arr_setlen_(p, size, stb_arr_len2(*p) + addlen STB__ARGS);
  2962. }
  2963. void stb__arr_insertn_(void **pp, int size, int i, int n STB__PARAMS)
  2964. {
  2965. void *p = *pp;
  2966. if (n) {
  2967. int z;
  2968. if (p == NULL) {
  2969. stb__arr_addlen_(pp, size, n STB__ARGS);
  2970. return;
  2971. }
  2972. z = stb_arr_len2(p);
  2973. stb__arr_addlen_(&p, size, n STB__ARGS);
  2974. memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-i));
  2975. }
  2976. *pp = p;
  2977. }
  2978. void stb__arr_deleten_(void **pp, int size, int i, int n STB__PARAMS)
  2979. {
  2980. void *p = *pp;
  2981. if (n) {
  2982. memmove((char *) p + i*size, (char *) p + (i+n)*size, size * (stb_arr_len2(p)-(i+n)));
  2983. stb_arrhead2(p)->len -= n;
  2984. }
  2985. *pp = p;
  2986. }
  2987. #endif
  2988. //////////////////////////////////////////////////////////////////////////////
  2989. //
  2990. // Hashing
  2991. //
  2992. // typical use for this is to make a power-of-two hash table.
  2993. //
  2994. // let N = size of table (2^n)
  2995. // let H = stb_hash(str)
  2996. // let S = stb_rehash(H) | 1
  2997. //
  2998. // then hash probe sequence P(i) for i=0..N-1
  2999. // P(i) = (H + S*i) & (N-1)
  3000. //
  3001. // the idea is that H has 32 bits of hash information, but the
  3002. // table has only, say, 2^20 entries so only uses 20 of the bits.
  3003. // then by rehashing the original H we get 2^12 different probe
  3004. // sequences for a given initial probe location. (So it's optimal
  3005. // for 64K tables and its optimality decreases past that.)
  3006. //
  3007. // ok, so I've added something that generates _two separate_
  3008. // 32-bit hashes simultaneously which should scale better to
  3009. // very large tables.
  3010. STB_EXTERN unsigned int stb_hash(char *str);
  3011. STB_EXTERN unsigned int stb_hashptr(void *p);
  3012. STB_EXTERN unsigned int stb_hashlen(char *str, int len);
  3013. STB_EXTERN unsigned int stb_rehash_improved(unsigned int v);
  3014. STB_EXTERN unsigned int stb_hash_fast(void *p, int len);
  3015. STB_EXTERN unsigned int stb_hash2(char *str, unsigned int *hash2_ptr);
  3016. STB_EXTERN unsigned int stb_hash_number(unsigned int hash);
  3017. #define stb_rehash(x) ((x) + ((x) >> 6) + ((x) >> 19))
  3018. #ifdef STB_DEFINE
  3019. unsigned int stb_hash(char *str)
  3020. {
  3021. unsigned int hash = 0;
  3022. while (*str)
  3023. hash = (hash << 7) + (hash >> 25) + *str++;
  3024. return hash + (hash >> 16);
  3025. }
  3026. unsigned int stb_hashlen(char *str, int len)
  3027. {
  3028. unsigned int hash = 0;
  3029. while (len-- > 0 && *str)
  3030. hash = (hash << 7) + (hash >> 25) + *str++;
  3031. return hash + (hash >> 16);
  3032. }
  3033. unsigned int stb_hashptr(void *p)
  3034. {
  3035. unsigned int x = (unsigned int)(size_t) p;
  3036. // typically lacking in low bits and high bits
  3037. x = stb_rehash(x);
  3038. x += x << 16;
  3039. // pearson's shuffle
  3040. x ^= x << 3;
  3041. x += x >> 5;
  3042. x ^= x << 2;
  3043. x += x >> 15;
  3044. x ^= x << 10;
  3045. return stb_rehash(x);
  3046. }
  3047. unsigned int stb_rehash_improved(unsigned int v)
  3048. {
  3049. return stb_hashptr((void *)(size_t) v);
  3050. }
  3051. unsigned int stb_hash2(char *str, unsigned int *hash2_ptr)
  3052. {
  3053. unsigned int hash1 = 0x3141592c;
  3054. unsigned int hash2 = 0x77f044ed;
  3055. while (*str) {
  3056. hash1 = (hash1 << 7) + (hash1 >> 25) + *str;
  3057. hash2 = (hash2 << 11) + (hash2 >> 21) + *str;
  3058. ++str;
  3059. }
  3060. *hash2_ptr = hash2 + (hash1 >> 16);
  3061. return hash1 + (hash2 >> 16);
  3062. }
  3063. // Paul Hsieh hash
  3064. #define stb__get16(p) ((p)[0] | ((p)[1] << 8))
  3065. unsigned int stb_hash_fast(void *p, int len)
  3066. {
  3067. unsigned char *q = (unsigned char *) p;
  3068. unsigned int hash = len;
  3069. if (len <= 0 || q == NULL) return 0;
  3070. /* Main loop */
  3071. for (;len > 3; len -= 4) {
  3072. unsigned int val;
  3073. hash += stb__get16(q);
  3074. val = (stb__get16(q+2) << 11);
  3075. hash = (hash << 16) ^ hash ^ val;
  3076. q += 4;
  3077. hash += hash >> 11;
  3078. }
  3079. /* Handle end cases */
  3080. switch (len) {
  3081. case 3: hash += stb__get16(q);
  3082. hash ^= hash << 16;
  3083. hash ^= q[2] << 18;
  3084. hash += hash >> 11;
  3085. break;
  3086. case 2: hash += stb__get16(q);
  3087. hash ^= hash << 11;
  3088. hash += hash >> 17;
  3089. break;
  3090. case 1: hash += q[0];
  3091. hash ^= hash << 10;
  3092. hash += hash >> 1;
  3093. break;
  3094. case 0: break;
  3095. }
  3096. /* Force "avalanching" of final 127 bits */
  3097. hash ^= hash << 3;
  3098. hash += hash >> 5;
  3099. hash ^= hash << 4;
  3100. hash += hash >> 17;
  3101. hash ^= hash << 25;
  3102. hash += hash >> 6;
  3103. return hash;
  3104. }
  3105. unsigned int stb_hash_number(unsigned int hash)
  3106. {
  3107. hash ^= hash << 3;
  3108. hash += hash >> 5;
  3109. hash ^= hash << 4;
  3110. hash += hash >> 17;
  3111. hash ^= hash << 25;
  3112. hash += hash >> 6;
  3113. return hash;
  3114. }
  3115. #endif
  3116. #ifdef STB_PERFECT_HASH
  3117. //////////////////////////////////////////////////////////////////////////////
  3118. //
  3119. // Perfect hashing for ints/pointers
  3120. //
  3121. // This is mainly useful for making faster pointer-indexed tables
  3122. // that don't change frequently. E.g. for stb_ischar().
  3123. //
  3124. typedef struct
  3125. {
  3126. stb_uint32 addend;
  3127. stb_uint multiplicand;
  3128. stb_uint b_mask;
  3129. stb_uint8 small_bmap[16];
  3130. stb_uint16 *large_bmap;
  3131. stb_uint table_mask;
  3132. stb_uint32 *table;
  3133. } stb_perfect;
  3134. STB_EXTERN int stb_perfect_create(stb_perfect *,unsigned int*,int n);
  3135. STB_EXTERN void stb_perfect_destroy(stb_perfect *);
  3136. STB_EXTERN int stb_perfect_hash(stb_perfect *, unsigned int x);
  3137. extern int stb_perfect_hash_max_failures;
  3138. #ifdef STB_DEFINE
  3139. int stb_perfect_hash_max_failures;
  3140. int stb_perfect_hash(stb_perfect *p, unsigned int x)
  3141. {
  3142. stb_uint m = x * p->multiplicand;
  3143. stb_uint y = x >> 16;
  3144. stb_uint bv = (m >> 24) + y;
  3145. stb_uint av = (m + y) >> 12;
  3146. if (p->table == NULL) return -1; // uninitialized table fails
  3147. bv &= p->b_mask;
  3148. av &= p->table_mask;
  3149. if (p->large_bmap)
  3150. av ^= p->large_bmap[bv];
  3151. else
  3152. av ^= p->small_bmap[bv];
  3153. return p->table[av] == x ? av : -1;
  3154. }
  3155. static void stb__perfect_prehash(stb_perfect *p, stb_uint x, stb_uint16 *a, stb_uint16 *b)
  3156. {
  3157. stb_uint m = x * p->multiplicand;
  3158. stb_uint y = x >> 16;
  3159. stb_uint bv = (m >> 24) + y;
  3160. stb_uint av = (m + y) >> 12;
  3161. bv &= p->b_mask;
  3162. av &= p->table_mask;
  3163. *b = bv;
  3164. *a = av;
  3165. }
  3166. static unsigned long stb__perfect_rand(void)
  3167. {
  3168. static unsigned long stb__rand;
  3169. stb__rand = stb__rand * 2147001325 + 715136305;
  3170. return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16));
  3171. }
  3172. typedef struct {
  3173. unsigned short count;
  3174. unsigned short b;
  3175. unsigned short map;
  3176. unsigned short *entries;
  3177. } stb__slot;
  3178. static int stb__slot_compare(const void *p, const void *q)
  3179. {
  3180. stb__slot *a = (stb__slot *) p;
  3181. stb__slot *b = (stb__slot *) q;
  3182. return a->count > b->count ? -1 : a->count < b->count; // sort large to small
  3183. }
  3184. int stb_perfect_create(stb_perfect *p, unsigned int *v, int n)
  3185. {
  3186. unsigned int buffer1[64], buffer2[64], buffer3[64], buffer4[64], buffer5[32];
  3187. unsigned short *as = (unsigned short *) stb_temp(buffer1, sizeof(*v)*n);
  3188. unsigned short *bs = (unsigned short *) stb_temp(buffer2, sizeof(*v)*n);
  3189. unsigned short *entries = (unsigned short *) stb_temp(buffer4, sizeof(*entries) * n);
  3190. int size = 1 << stb_log2_ceil(n), bsize=8;
  3191. int failure = 0,i,j,k;
  3192. assert(n <= 32768);
  3193. p->large_bmap = NULL;
  3194. for(;;) {
  3195. stb__slot *bcount = (stb__slot *) stb_temp(buffer3, sizeof(*bcount) * bsize);
  3196. unsigned short *bloc = (unsigned short *) stb_temp(buffer5, sizeof(*bloc) * bsize);
  3197. unsigned short *e;
  3198. int bad=0;
  3199. p->addend = stb__perfect_rand();
  3200. p->multiplicand = stb__perfect_rand() | 1;
  3201. p->table_mask = size-1;
  3202. p->b_mask = bsize-1;
  3203. p->table = (stb_uint32 *) malloc(size * sizeof(*p->table));
  3204. for (i=0; i < bsize; ++i) {
  3205. bcount[i].b = i;
  3206. bcount[i].count = 0;
  3207. bcount[i].map = 0;
  3208. }
  3209. for (i=0; i < n; ++i) {
  3210. stb__perfect_prehash(p, v[i], as+i, bs+i);
  3211. ++bcount[bs[i]].count;
  3212. }
  3213. qsort(bcount, bsize, sizeof(*bcount), stb__slot_compare);
  3214. e = entries; // now setup up their entries index
  3215. for (i=0; i < bsize; ++i) {
  3216. bcount[i].entries = e;
  3217. e += bcount[i].count;
  3218. bcount[i].count = 0;
  3219. bloc[bcount[i].b] = i;
  3220. }
  3221. // now fill them out
  3222. for (i=0; i < n; ++i) {
  3223. int b = bs[i];
  3224. int w = bloc[b];
  3225. bcount[w].entries[bcount[w].count++] = i;
  3226. }
  3227. stb_tempfree(buffer5,bloc);
  3228. // verify
  3229. for (i=0; i < bsize; ++i)
  3230. for (j=0; j < bcount[i].count; ++j)
  3231. assert(bs[bcount[i].entries[j]] == bcount[i].b);
  3232. memset(p->table, 0, size*sizeof(*p->table));
  3233. // check if any b has duplicate a
  3234. for (i=0; i < bsize; ++i) {
  3235. if (bcount[i].count > 1) {
  3236. for (j=0; j < bcount[i].count; ++j) {
  3237. if (p->table[as[bcount[i].entries[j]]])
  3238. bad = 1;
  3239. p->table[as[bcount[i].entries[j]]] = 1;
  3240. }
  3241. for (j=0; j < bcount[i].count; ++j) {
  3242. p->table[as[bcount[i].entries[j]]] = 0;
  3243. }
  3244. if (bad) break;
  3245. }
  3246. }
  3247. if (!bad) {
  3248. // go through the bs and populate the table, first fit
  3249. for (i=0; i < bsize; ++i) {
  3250. if (bcount[i].count) {
  3251. // go through the candidate table[b] values
  3252. for (j=0; j < size; ++j) {
  3253. // go through the a values and see if they fit
  3254. for (k=0; k < bcount[i].count; ++k) {
  3255. int a = as[bcount[i].entries[k]];
  3256. if (p->table[(a^j)&p->table_mask]) {
  3257. break; // fails
  3258. }
  3259. }
  3260. // if succeeded, accept
  3261. if (k == bcount[i].count) {
  3262. bcount[i].map = j;
  3263. for (k=0; k < bcount[i].count; ++k) {
  3264. int a = as[bcount[i].entries[k]];
  3265. p->table[(a^j)&p->table_mask] = 1;
  3266. }
  3267. break;
  3268. }
  3269. }
  3270. if (j == size)
  3271. break; // no match for i'th entry, so break out in failure
  3272. }
  3273. }
  3274. if (i == bsize) {
  3275. // success... fill out map
  3276. if (bsize <= 16 && size <= 256) {
  3277. p->large_bmap = NULL;
  3278. for (i=0; i < bsize; ++i)
  3279. p->small_bmap[bcount[i].b] = (stb_uint8) bcount[i].map;
  3280. } else {
  3281. p->large_bmap = (unsigned short *) malloc(sizeof(*p->large_bmap) * bsize);
  3282. for (i=0; i < bsize; ++i)
  3283. p->large_bmap[bcount[i].b] = bcount[i].map;
  3284. }
  3285. // initialize table to v[0], so empty slots will fail
  3286. for (i=0; i < size; ++i)
  3287. p->table[i] = v[0];
  3288. for (i=0; i < n; ++i)
  3289. if (p->large_bmap)
  3290. p->table[as[i] ^ p->large_bmap[bs[i]]] = v[i];
  3291. else
  3292. p->table[as[i] ^ p->small_bmap[bs[i]]] = v[i];
  3293. // and now validate that none of them collided
  3294. for (i=0; i < n; ++i)
  3295. assert(stb_perfect_hash(p, v[i]) >= 0);
  3296. stb_tempfree(buffer3, bcount);
  3297. break;
  3298. }
  3299. }
  3300. free(p->table);
  3301. p->table = NULL;
  3302. stb_tempfree(buffer3, bcount);
  3303. ++failure;
  3304. if (failure >= 4 && bsize < size) bsize *= 2;
  3305. if (failure >= 8 && (failure & 3) == 0 && size < 4*n) {
  3306. size *= 2;
  3307. bsize *= 2;
  3308. }
  3309. if (failure == 6) {
  3310. // make sure the input data is unique, so we don't infinite loop
  3311. unsigned int *data = (unsigned int *) stb_temp(buffer3, n * sizeof(*data));
  3312. memcpy(data, v, sizeof(*data) * n);
  3313. qsort(data, n, sizeof(*data), stb_intcmp(0));
  3314. for (i=1; i < n; ++i) {
  3315. if (data[i] == data[i-1])
  3316. size = 0; // size is return value, so 0 it
  3317. }
  3318. stb_tempfree(buffer3, data);
  3319. if (!size) break;
  3320. }
  3321. }
  3322. if (failure > stb_perfect_hash_max_failures)
  3323. stb_perfect_hash_max_failures = failure;
  3324. stb_tempfree(buffer1, as);
  3325. stb_tempfree(buffer2, bs);
  3326. stb_tempfree(buffer4, entries);
  3327. return size;
  3328. }
  3329. void stb_perfect_destroy(stb_perfect *p)
  3330. {
  3331. if (p->large_bmap) free(p->large_bmap);
  3332. if (p->table ) free(p->table);
  3333. p->large_bmap = NULL;
  3334. p->table = NULL;
  3335. p->b_mask = 0;
  3336. p->table_mask = 0;
  3337. }
  3338. #endif
  3339. //////////////////////////////////////////////////////////////////////////////
  3340. //
  3341. // Perfect hash clients
  3342. STB_EXTERN int stb_ischar(char s, char *set);
  3343. #ifdef STB_DEFINE
  3344. int stb_ischar(char c, char *set)
  3345. {
  3346. static unsigned char bit[8] = { 1,2,4,8,16,32,64,128 };
  3347. static stb_perfect p;
  3348. static unsigned char (*tables)[256];
  3349. static char ** sets = NULL;
  3350. int z = stb_perfect_hash(&p, (int)(size_t) set);
  3351. if (z < 0) {
  3352. int i,k,n,j,f;
  3353. // special code that means free all existing data
  3354. if (set == NULL) {
  3355. stb_arr_free(sets);
  3356. free(tables);
  3357. tables = NULL;
  3358. stb_perfect_destroy(&p);
  3359. return 0;
  3360. }
  3361. stb_arr_push(sets, set);
  3362. stb_perfect_destroy(&p);
  3363. n = stb_perfect_create(&p, (unsigned int *) (char **) sets, stb_arr_len(sets));
  3364. assert(n != 0);
  3365. k = (n+7) >> 3;
  3366. tables = (unsigned char (*)[256]) realloc(tables, sizeof(*tables) * k);
  3367. memset(tables, 0, sizeof(*tables) * k);
  3368. for (i=0; i < stb_arr_len(sets); ++i) {
  3369. k = stb_perfect_hash(&p, (int)(size_t) sets[i]);
  3370. assert(k >= 0);
  3371. n = k >> 3;
  3372. f = bit[k&7];
  3373. for (j=0; !j || sets[i][j]; ++j) {
  3374. tables[n][(unsigned char) sets[i][j]] |= f;
  3375. }
  3376. }
  3377. z = stb_perfect_hash(&p, (int)(size_t) set);
  3378. }
  3379. return tables[z >> 3][(unsigned char) c] & bit[z & 7];
  3380. }
  3381. #endif
  3382. #endif
  3383. //////////////////////////////////////////////////////////////////////////////
  3384. //
  3385. // Instantiated data structures
  3386. //
  3387. // This is an attempt to implement a templated data structure.
  3388. //
  3389. // Hash table: call stb_define_hash(TYPE,N,KEY,K1,K2,HASH,VALUE)
  3390. // TYPE -- will define a structure type containing the hash table
  3391. // N -- the name, will prefix functions named:
  3392. // N create
  3393. // N destroy
  3394. // N get
  3395. // N set, N add, N update,
  3396. // N remove
  3397. // KEY -- the type of the key. 'x == y' must be valid
  3398. // K1,K2 -- keys never used by the app, used as flags in the hashtable
  3399. // HASH -- a piece of code ending with 'return' that hashes key 'k'
  3400. // VALUE -- the type of the value. 'x = y' must be valid
  3401. //
  3402. // Note that stb_define_hash_base can be used to define more sophisticated
  3403. // hash tables, e.g. those that make copies of the key or use special
  3404. // comparisons (e.g. strcmp).
  3405. #define STB_(prefix,name) stb__##prefix##name
  3406. #define STB__(prefix,name) prefix##name
  3407. #define STB__use(x) x
  3408. #define STB__skip(x)
  3409. #define stb_declare_hash(PREFIX,TYPE,N,KEY,VALUE) \
  3410. typedef struct stb__st_##TYPE TYPE;\
  3411. PREFIX int STB__(N, init)(TYPE *h, int count);\
  3412. PREFIX int STB__(N, memory_usage)(TYPE *h);\
  3413. PREFIX TYPE * STB__(N, create)(void);\
  3414. PREFIX TYPE * STB__(N, copy)(TYPE *h);\
  3415. PREFIX void STB__(N, destroy)(TYPE *h);\
  3416. PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v);\
  3417. PREFIX VALUE STB__(N,get)(TYPE *a, KEY k);\
  3418. PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v);\
  3419. PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v);\
  3420. PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v);\
  3421. PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v);
  3422. #define STB_nocopy(x) (x)
  3423. #define STB_nodelete(x) 0
  3424. #define STB_nofields
  3425. #define STB_nonullvalue(x)
  3426. #define STB_nullvalue(x) x
  3427. #define STB_safecompare(x) x
  3428. #define STB_nosafe(x)
  3429. #define STB_noprefix
  3430. #ifdef __GNUC__
  3431. #define STB__nogcc(x)
  3432. #else
  3433. #define STB__nogcc(x) x
  3434. #endif
  3435. #define stb_define_hash_base(PREFIX,TYPE,FIELDS,N,NC,LOAD_FACTOR, \
  3436. KEY,EMPTY,DEL,COPY,DISPOSE,SAFE, \
  3437. VCOMPARE,CCOMPARE,HASH, \
  3438. VALUE,HASVNULL,VNULL) \
  3439. \
  3440. typedef struct \
  3441. { \
  3442. KEY k; \
  3443. VALUE v; \
  3444. } STB_(N,_hashpair); \
  3445. \
  3446. STB__nogcc( typedef struct stb__st_##TYPE TYPE; ) \
  3447. struct stb__st_##TYPE { \
  3448. FIELDS \
  3449. STB_(N,_hashpair) *table; \
  3450. unsigned int mask; \
  3451. int count, limit; \
  3452. int deleted; \
  3453. \
  3454. int delete_threshhold; \
  3455. int grow_threshhold; \
  3456. int shrink_threshhold; \
  3457. unsigned char alloced, has_empty, has_del; \
  3458. VALUE ev; VALUE dv; \
  3459. }; \
  3460. \
  3461. static unsigned int STB_(N, hash)(KEY k) \
  3462. { \
  3463. HASH \
  3464. } \
  3465. \
  3466. PREFIX int STB__(N, init)(TYPE *h, int count) \
  3467. { \
  3468. int i; \
  3469. if (count < 4) count = 4; \
  3470. h->limit = count; \
  3471. h->count = 0; \
  3472. h->mask = count-1; \
  3473. h->deleted = 0; \
  3474. h->grow_threshhold = (int) (count * LOAD_FACTOR); \
  3475. h->has_empty = h->has_del = 0; \
  3476. h->alloced = 0; \
  3477. if (count <= 64) \
  3478. h->shrink_threshhold = 0; \
  3479. else \
  3480. h->shrink_threshhold = (int) (count * (LOAD_FACTOR/2.25)); \
  3481. h->delete_threshhold = (int) (count * (1-LOAD_FACTOR)/2); \
  3482. h->table = (STB_(N,_hashpair)*) malloc(sizeof(h->table[0]) * count); \
  3483. if (h->table == NULL) return 0; \
  3484. /* ideally this gets turned into a memset32 automatically */ \
  3485. for (i=0; i < count; ++i) \
  3486. h->table[i].k = EMPTY; \
  3487. return 1; \
  3488. } \
  3489. \
  3490. PREFIX int STB__(N, memory_usage)(TYPE *h) \
  3491. { \
  3492. return sizeof(*h) + h->limit * sizeof(h->table[0]); \
  3493. } \
  3494. \
  3495. PREFIX TYPE * STB__(N, create)(void) \
  3496. { \
  3497. TYPE *h = (TYPE *) malloc(sizeof(*h)); \
  3498. if (h) { \
  3499. if (STB__(N, init)(h, 16)) \
  3500. h->alloced = 1; \
  3501. else { free(h); h=NULL; } \
  3502. } \
  3503. return h; \
  3504. } \
  3505. \
  3506. PREFIX void STB__(N, destroy)(TYPE *a) \
  3507. { \
  3508. int i; \
  3509. for (i=0; i < a->limit; ++i) \
  3510. if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k, DEL)) \
  3511. DISPOSE(a->table[i].k); \
  3512. free(a->table); \
  3513. if (a->alloced) \
  3514. free(a); \
  3515. } \
  3516. \
  3517. static void STB_(N, rehash)(TYPE *a, int count); \
  3518. \
  3519. PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v) \
  3520. { \
  3521. unsigned int h = STB_(N, hash)(k); \
  3522. unsigned int n = h & a->mask, s; \
  3523. if (CCOMPARE(k,EMPTY)){ if (a->has_empty) *v = a->ev; return a->has_empty;}\
  3524. if (CCOMPARE(k,DEL)) { if (a->has_del ) *v = a->dv; return a->has_del; }\
  3525. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3526. SAFE(if (!CCOMPARE(a->table[n].k,DEL))) \
  3527. if (VCOMPARE(a->table[n].k,k)) { *v = a->table[n].v; return 1; } \
  3528. s = stb_rehash(h) | 1; \
  3529. for(;;) { \
  3530. n = (n + s) & a->mask; \
  3531. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3532. SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;) \
  3533. if (VCOMPARE(a->table[n].k,k)) \
  3534. { *v = a->table[n].v; return 1; } \
  3535. } \
  3536. } \
  3537. \
  3538. HASVNULL( \
  3539. PREFIX VALUE STB__(N,get)(TYPE *a, KEY k) \
  3540. { \
  3541. VALUE v; \
  3542. if (STB__(N,get_flag)(a,k,&v)) return v; \
  3543. else return VNULL; \
  3544. } \
  3545. ) \
  3546. \
  3547. PREFIX int STB__(N,getkey)(TYPE *a, KEY k, KEY *kout) \
  3548. { \
  3549. unsigned int h = STB_(N, hash)(k); \
  3550. unsigned int n = h & a->mask, s; \
  3551. if (CCOMPARE(k,EMPTY)||CCOMPARE(k,DEL)) return 0; \
  3552. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3553. SAFE(if (!CCOMPARE(a->table[n].k,DEL))) \
  3554. if (VCOMPARE(a->table[n].k,k)) { *kout = a->table[n].k; return 1; } \
  3555. s = stb_rehash(h) | 1; \
  3556. for(;;) { \
  3557. n = (n + s) & a->mask; \
  3558. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3559. SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;) \
  3560. if (VCOMPARE(a->table[n].k,k)) \
  3561. { *kout = a->table[n].k; return 1; } \
  3562. } \
  3563. } \
  3564. \
  3565. static int STB_(N,addset)(TYPE *a, KEY k, VALUE v, \
  3566. int allow_new, int allow_old, int copy) \
  3567. { \
  3568. unsigned int h = STB_(N, hash)(k); \
  3569. unsigned int n = h & a->mask; \
  3570. int b = -1; \
  3571. if (CCOMPARE(k,EMPTY)) { \
  3572. if (a->has_empty ? allow_old : allow_new) { \
  3573. n=a->has_empty; a->ev = v; a->has_empty = 1; return !n; \
  3574. } else return 0; \
  3575. } \
  3576. if (CCOMPARE(k,DEL)) { \
  3577. if (a->has_del ? allow_old : allow_new) { \
  3578. n=a->has_del; a->dv = v; a->has_del = 1; return !n; \
  3579. } else return 0; \
  3580. } \
  3581. if (!CCOMPARE(a->table[n].k, EMPTY)) { \
  3582. unsigned int s; \
  3583. if (CCOMPARE(a->table[n].k, DEL)) \
  3584. b = n; \
  3585. else if (VCOMPARE(a->table[n].k,k)) { \
  3586. if (allow_old) \
  3587. a->table[n].v = v; \
  3588. return !allow_new; \
  3589. } \
  3590. s = stb_rehash(h) | 1; \
  3591. for(;;) { \
  3592. n = (n + s) & a->mask; \
  3593. if (CCOMPARE(a->table[n].k, EMPTY)) break; \
  3594. if (CCOMPARE(a->table[n].k, DEL)) { \
  3595. if (b < 0) b = n; \
  3596. } else if (VCOMPARE(a->table[n].k,k)) { \
  3597. if (allow_old) \
  3598. a->table[n].v = v; \
  3599. return !allow_new; \
  3600. } \
  3601. } \
  3602. } \
  3603. if (!allow_new) return 0; \
  3604. if (b < 0) b = n; else --a->deleted; \
  3605. a->table[b].k = copy ? COPY(k) : k; \
  3606. a->table[b].v = v; \
  3607. ++a->count; \
  3608. if (a->count > a->grow_threshhold) \
  3609. STB_(N,rehash)(a, a->limit*2); \
  3610. return 1; \
  3611. } \
  3612. \
  3613. PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,1,1);}\
  3614. PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,0,1);}\
  3615. PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v){return STB_(N,addset)(a,k,v,0,1,1);}\
  3616. \
  3617. PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v) \
  3618. { \
  3619. unsigned int h = STB_(N, hash)(k); \
  3620. unsigned int n = h & a->mask, s; \
  3621. if (CCOMPARE(k,EMPTY)) { if (a->has_empty) { if(v)*v = a->ev; a->has_empty=0; return 1; } return 0; } \
  3622. if (CCOMPARE(k,DEL)) { if (a->has_del ) { if(v)*v = a->dv; a->has_del =0; return 1; } return 0; } \
  3623. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3624. if (SAFE(CCOMPARE(a->table[n].k,DEL) || ) !VCOMPARE(a->table[n].k,k)) { \
  3625. s = stb_rehash(h) | 1; \
  3626. for(;;) { \
  3627. n = (n + s) & a->mask; \
  3628. if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
  3629. SAFE(if (CCOMPARE(a->table[n].k, DEL)) continue;) \
  3630. if (VCOMPARE(a->table[n].k,k)) break; \
  3631. } \
  3632. } \
  3633. DISPOSE(a->table[n].k); \
  3634. a->table[n].k = DEL; \
  3635. --a->count; \
  3636. ++a->deleted; \
  3637. if (v != NULL) \
  3638. *v = a->table[n].v; \
  3639. if (a->count < a->shrink_threshhold) \
  3640. STB_(N, rehash)(a, a->limit >> 1); \
  3641. else if (a->deleted > a->delete_threshhold) \
  3642. STB_(N, rehash)(a, a->limit); \
  3643. return 1; \
  3644. } \
  3645. \
  3646. PREFIX TYPE * STB__(NC, copy)(TYPE *a) \
  3647. { \
  3648. int i; \
  3649. TYPE *h = (TYPE *) malloc(sizeof(*h)); \
  3650. if (!h) return NULL; \
  3651. if (!STB__(N, init)(h, a->limit)) { free(h); return NULL; } \
  3652. h->count = a->count; \
  3653. h->deleted = a->deleted; \
  3654. h->alloced = 1; \
  3655. h->ev = a->ev; h->dv = a->dv; \
  3656. h->has_empty = a->has_empty; h->has_del = a->has_del; \
  3657. memcpy(h->table, a->table, h->limit * sizeof(h->table[0])); \
  3658. for (i=0; i < a->limit; ++i) \
  3659. if (!CCOMPARE(h->table[i].k,EMPTY) && !CCOMPARE(h->table[i].k,DEL)) \
  3660. h->table[i].k = COPY(h->table[i].k); \
  3661. return h; \
  3662. } \
  3663. \
  3664. static void STB_(N, rehash)(TYPE *a, int count) \
  3665. { \
  3666. int i; \
  3667. TYPE b; \
  3668. STB__(N, init)(&b, count); \
  3669. for (i=0; i < a->limit; ++i) \
  3670. if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k,DEL)) \
  3671. STB_(N,addset)(&b, a->table[i].k, a->table[i].v,1,1,0); \
  3672. free(a->table); \
  3673. a->table = b.table; \
  3674. a->mask = b.mask; \
  3675. a->count = b.count; \
  3676. a->limit = b.limit; \
  3677. a->deleted = b.deleted; \
  3678. a->delete_threshhold = b.delete_threshhold; \
  3679. a->grow_threshhold = b.grow_threshhold; \
  3680. a->shrink_threshhold = b.shrink_threshhold; \
  3681. }
  3682. #define STB_equal(a,b) ((a) == (b))
  3683. #define stb_define_hash(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE) \
  3684. stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \
  3685. KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \
  3686. STB_equal,STB_equal,HASH, \
  3687. VALUE,STB_nonullvalue,0)
  3688. #define stb_define_hash_vnull(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE,VNULL) \
  3689. stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \
  3690. KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \
  3691. STB_equal,STB_equal,HASH, \
  3692. VALUE,STB_nullvalue,VNULL)
  3693. //////////////////////////////////////////////////////////////////////////////
  3694. //
  3695. // stb_ptrmap
  3696. //
  3697. // An stb_ptrmap data structure is an O(1) hash table between pointers. One
  3698. // application is to let you store "extra" data associated with pointers,
  3699. // which is why it was originally called stb_extra.
  3700. stb_declare_hash(STB_EXTERN, stb_ptrmap, stb_ptrmap_, void *, void *)
  3701. stb_declare_hash(STB_EXTERN, stb_idict, stb_idict_, stb_int32, stb_int32)
  3702. stb_declare_hash(STB_EXTERN, stb_uidict, stbi_uidict_, stb_uint32, stb_uint32)
  3703. STB_EXTERN void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *));
  3704. STB_EXTERN stb_ptrmap *stb_ptrmap_new(void);
  3705. STB_EXTERN stb_idict * stb_idict_new_size(int size);
  3706. STB_EXTERN void stb_idict_remove_all(stb_idict *e);
  3707. STB_EXTERN void stb_uidict_reset(stb_uidict *e);
  3708. #ifdef STB_DEFINE
  3709. #define STB_EMPTY ((void *) 2)
  3710. #define STB_EDEL ((void *) 6)
  3711. stb_define_hash_base(STB_noprefix,stb_ptrmap, STB_nofields, stb_ptrmap_,stb_ptrmap_,0.85f,
  3712. void *,STB_EMPTY,STB_EDEL,STB_nocopy,STB_nodelete,STB_nosafe,
  3713. STB_equal,STB_equal,return stb_hashptr(k);,
  3714. void *,STB_nullvalue,NULL)
  3715. stb_ptrmap *stb_ptrmap_new(void)
  3716. {
  3717. return stb_ptrmap_create();
  3718. }
  3719. void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *))
  3720. {
  3721. int i;
  3722. if (free_func)
  3723. for (i=0; i < e->limit; ++i)
  3724. if (e->table[i].k != STB_EMPTY && e->table[i].k != STB_EDEL) {
  3725. if (free_func == free)
  3726. free(e->table[i].v); // allow STB_MALLOC_WRAPPER to operate
  3727. else
  3728. free_func(e->table[i].v);
  3729. }
  3730. stb_ptrmap_destroy(e);
  3731. }
  3732. // extra fields needed for stua_dict
  3733. #define STB_IEMPTY ((int) 1)
  3734. #define STB_IDEL ((int) 3)
  3735. stb_define_hash_base(STB_noprefix, stb_idict, short type; short gc; STB_nofields, stb_idict_,stb_idict_,0.95f,
  3736. stb_int32,STB_IEMPTY,STB_IDEL,STB_nocopy,STB_nodelete,STB_nosafe,
  3737. STB_equal,STB_equal,
  3738. return stb_rehash_improved(k);,stb_int32,STB_nonullvalue,0)
  3739. stb_idict * stb_idict_new_size(int size)
  3740. {
  3741. stb_idict *e = (stb_idict *) malloc(sizeof(*e));
  3742. if (e) {
  3743. if (!stb_is_pow2(size))
  3744. size = 1 << stb_log2_ceil(size);
  3745. stb_idict_init(e, size);
  3746. e->alloced = 1;
  3747. }
  3748. return e;
  3749. }
  3750. void stb_idict_remove_all(stb_idict *e)
  3751. {
  3752. int n;
  3753. for (n=0; n < e->limit; ++n)
  3754. e->table[n].k = STB_IEMPTY;
  3755. e->has_empty = e->has_del = 0;
  3756. e->count = 0;
  3757. e->deleted = 0;
  3758. }
  3759. stb_define_hash_base(STB_noprefix, stb_uidict, STB_nofields, stb_uidict_,stb_uidict_,0.85f,
  3760. stb_int32,0xffffffff,0xfffffffe,STB_nocopy,STB_nodelete,STB_nosafe,
  3761. STB_equal,STB_equal,
  3762. return stb_rehash_improved(k);,stb_uint32,STB_nonullvalue,0)
  3763. void stb_uidict_reset(stb_uidict *e)
  3764. {
  3765. int n;
  3766. for (n=0; n < e->limit; ++n)
  3767. e->table[n].k = 0xffffffff;
  3768. e->has_empty = e->has_del = 0;
  3769. e->count = 0;
  3770. e->deleted = 0;
  3771. }
  3772. #endif
  3773. //////////////////////////////////////////////////////////////////////////////
  3774. //
  3775. // stb_sparse_ptr_matrix
  3776. //
  3777. // An stb_ptrmap data structure is an O(1) hash table storing an arbitrary
  3778. // block of data for a given pair of pointers.
  3779. //
  3780. // If create=0, returns
  3781. typedef struct stb__st_stb_spmatrix stb_spmatrix;
  3782. STB_EXTERN stb_spmatrix * stb_sparse_ptr_matrix_new(int val_size);
  3783. STB_EXTERN void stb_sparse_ptr_matrix_free(stb_spmatrix *z);
  3784. STB_EXTERN void * stb_sparse_ptr_matrix_get(stb_spmatrix *z, void *a, void *b, int create);
  3785. #ifdef STB_DEFINE
  3786. typedef struct
  3787. {
  3788. void *a;
  3789. void *b;
  3790. } stb__ptrpair;
  3791. static stb__ptrpair stb__ptrpair_empty = { (void *) 1, (void *) 1 };
  3792. static stb__ptrpair stb__ptrpair_del = { (void *) 2, (void *) 2 };
  3793. #define STB__equal_ptrpair(x,y) ((x).a == (y).a && (x).b == (y).b)
  3794. stb_define_hash_base(STB_noprefix, stb_spmatrix, int val_size; void *arena;, stb__spmatrix_,stb__spmatrix_, 0.85,
  3795. stb__ptrpair, stb__ptrpair_empty, stb__ptrpair_del,
  3796. STB_nocopy, STB_nodelete, STB_nosafe,
  3797. STB__equal_ptrpair, STB__equal_ptrpair, return stb_rehash(stb_hashptr(k.a))+stb_hashptr(k.b);,
  3798. void *, STB_nullvalue, 0)
  3799. stb_spmatrix *stb_sparse_ptr_matrix_new(int val_size)
  3800. {
  3801. stb_spmatrix *m = stb__spmatrix_create();
  3802. if (m) m->val_size = val_size;
  3803. if (m) m->arena = stb_malloc_global(1);
  3804. return m;
  3805. }
  3806. void stb_sparse_ptr_matrix_free(stb_spmatrix *z)
  3807. {
  3808. if (z->arena) stb_free(z->arena);
  3809. stb__spmatrix_destroy(z);
  3810. }
  3811. void *stb_sparse_ptr_matrix_get(stb_spmatrix *z, void *a, void *b, int create)
  3812. {
  3813. stb__ptrpair t = { a,b };
  3814. void *data = stb__spmatrix_get(z, t);
  3815. if (!data && create) {
  3816. data = stb_malloc_raw(z->arena, z->val_size);
  3817. if (!data) return NULL;
  3818. memset(data, 0, z->val_size);
  3819. stb__spmatrix_add(z, t, data);
  3820. }
  3821. return data;
  3822. }
  3823. #endif
  3824. //////////////////////////////////////////////////////////////////////////////
  3825. //
  3826. // SDICT: Hash Table for Strings (symbol table)
  3827. //
  3828. // if "use_arena=1", then strings will be copied
  3829. // into blocks and never freed until the sdict is freed;
  3830. // otherwise they're malloc()ed and free()d on the fly.
  3831. // (specify use_arena=1 if you never stb_sdict_remove)
  3832. stb_declare_hash(STB_EXTERN, stb_sdict, stb_sdict_, char *, void *)
  3833. STB_EXTERN stb_sdict * stb_sdict_new(int use_arena);
  3834. STB_EXTERN stb_sdict * stb_sdict_copy(stb_sdict*);
  3835. STB_EXTERN void stb_sdict_delete(stb_sdict *);
  3836. STB_EXTERN void * stb_sdict_change(stb_sdict *, char *str, void *p);
  3837. STB_EXTERN int stb_sdict_count(stb_sdict *d);
  3838. STB_EXTERN int stb_sdict_internal_limit(stb_sdict *d);
  3839. STB_EXTERN char * stb_sdict_internal_key(stb_sdict *d, int n);
  3840. STB_EXTERN void * stb_sdict_internal_value(stb_sdict *d, int n);
  3841. #define stb_sdict_for(d,i,q,z) \
  3842. for(i=0; i < stb_sdict_internal_limit(d) ? (q=stb_sdict_internal_key(d,i),z=stb_sdict_internal_value(d,i),1) : 0; ++i) \
  3843. if (q==NULL||q==(void *) 1);else // reversed makes macro friendly
  3844. #ifdef STB_DEFINE
  3845. // if in same translation unit, for speed, don't call accessors
  3846. #undef stb_sdict_for
  3847. #define stb_sdict_for(d,i,q,z) \
  3848. for(i=0; i < (d)->limit ? (q=(d)->table[i].k,z=(d)->table[i].v,1) : 0; ++i) \
  3849. if (q==NULL||q==(void *) 1);else // reversed makes macro friendly
  3850. #define STB_DEL ((void *) 1)
  3851. #define STB_SDEL ((char *) 1)
  3852. #define stb_sdict__copy(x) \
  3853. stb_p_strcpy_s(a->arena ? stb_malloc_string(a->arena, strlen(x)+1) \
  3854. : (char *) malloc(strlen(x)+1), strlen(x)+1, x)
  3855. #define stb_sdict__dispose(x) if (!a->arena) free(x)
  3856. stb_define_hash_base(STB_noprefix, stb_sdict, void*arena;, stb_sdict_,stb_sdictinternal_, 0.85f,
  3857. char *, NULL, STB_SDEL, stb_sdict__copy, stb_sdict__dispose,
  3858. STB_safecompare, !strcmp, STB_equal, return stb_hash(k);,
  3859. void *, STB_nullvalue, NULL)
  3860. int stb_sdict_count(stb_sdict *a)
  3861. {
  3862. return a->count;
  3863. }
  3864. int stb_sdict_internal_limit(stb_sdict *a)
  3865. {
  3866. return a->limit;
  3867. }
  3868. char* stb_sdict_internal_key(stb_sdict *a, int n)
  3869. {
  3870. return a->table[n].k;
  3871. }
  3872. void* stb_sdict_internal_value(stb_sdict *a, int n)
  3873. {
  3874. return a->table[n].v;
  3875. }
  3876. stb_sdict * stb_sdict_new(int use_arena)
  3877. {
  3878. stb_sdict *d = stb_sdict_create();
  3879. if (d == NULL) return NULL;
  3880. d->arena = use_arena ? stb_malloc_global(1) : NULL;
  3881. return d;
  3882. }
  3883. stb_sdict* stb_sdict_copy(stb_sdict *old)
  3884. {
  3885. stb_sdict *n;
  3886. void *old_arena = old->arena;
  3887. void *new_arena = old_arena ? stb_malloc_global(1) : NULL;
  3888. old->arena = new_arena;
  3889. n = stb_sdictinternal_copy(old);
  3890. old->arena = old_arena;
  3891. if (n)
  3892. n->arena = new_arena;
  3893. else if (new_arena)
  3894. stb_free(new_arena);
  3895. return n;
  3896. }
  3897. void stb_sdict_delete(stb_sdict *d)
  3898. {
  3899. if (d->arena)
  3900. stb_free(d->arena);
  3901. stb_sdict_destroy(d);
  3902. }
  3903. void * stb_sdict_change(stb_sdict *d, char *str, void *p)
  3904. {
  3905. void *q = stb_sdict_get(d, str);
  3906. stb_sdict_set(d, str, p);
  3907. return q;
  3908. }
  3909. #endif
  3910. //////////////////////////////////////////////////////////////////////////////
  3911. //
  3912. // Instantiated data structures
  3913. //
  3914. // This is an attempt to implement a templated data structure.
  3915. // What you do is define a struct foo, and then include several
  3916. // pointer fields to struct foo in your struct. Then you call
  3917. // the instantiator, which creates the functions that implement
  3918. // the data structure. This requires massive undebuggable #defines,
  3919. // so we limit the cases where we do this.
  3920. //
  3921. // AA tree is an encoding of a 2-3 tree whereas RB trees encode a 2-3-4 tree;
  3922. // much simpler code due to fewer cases.
  3923. #define stb__bst_parent(x) x
  3924. #define stb__bst_noparent(x)
  3925. #define stb_bst_fields(N) \
  3926. *STB_(N,left), *STB_(N,right); \
  3927. unsigned char STB_(N,level)
  3928. #define stb_bst_fields_parent(N) \
  3929. *STB_(N,left), *STB_(N,right), *STB_(N,parent); \
  3930. unsigned char STB_(N,level)
  3931. #define STB__level(N,x) ((x) ? (x)->STB_(N,level) : 0)
  3932. #define stb_bst_base(TYPE, N, TREE, M, compare, PAR) \
  3933. \
  3934. static int STB_(N,_compare)(TYPE *p, TYPE *q) \
  3935. { \
  3936. compare \
  3937. } \
  3938. \
  3939. static void STB_(N,setleft)(TYPE *q, TYPE *v) \
  3940. { \
  3941. q->STB_(N,left) = v; \
  3942. PAR(if (v) v->STB_(N,parent) = q;) \
  3943. } \
  3944. \
  3945. static void STB_(N,setright)(TYPE *q, TYPE *v) \
  3946. { \
  3947. q->STB_(N,right) = v; \
  3948. PAR(if (v) v->STB_(N,parent) = q;) \
  3949. } \
  3950. \
  3951. static TYPE *STB_(N,skew)(TYPE *q) \
  3952. { \
  3953. if (q == NULL) return q; \
  3954. if (q->STB_(N,left) \
  3955. && q->STB_(N,left)->STB_(N,level) == q->STB_(N,level)) { \
  3956. TYPE *p = q->STB_(N,left); \
  3957. STB_(N,setleft)(q, p->STB_(N,right)); \
  3958. STB_(N,setright)(p, q); \
  3959. return p; \
  3960. } \
  3961. return q; \
  3962. } \
  3963. \
  3964. static TYPE *STB_(N,split)(TYPE *p) \
  3965. { \
  3966. TYPE *q = p->STB_(N,right); \
  3967. if (q && q->STB_(N,right) \
  3968. && q->STB_(N,right)->STB_(N,level) == p->STB_(N,level)) { \
  3969. STB_(N,setright)(p, q->STB_(N,left)); \
  3970. STB_(N,setleft)(q,p); \
  3971. ++q->STB_(N,level); \
  3972. return q; \
  3973. } \
  3974. return p; \
  3975. } \
  3976. \
  3977. TYPE *STB__(N,insert)(TYPE *tree, TYPE *item) \
  3978. { \
  3979. int c; \
  3980. if (tree == NULL) { \
  3981. item->STB_(N,left) = NULL; \
  3982. item->STB_(N,right) = NULL; \
  3983. item->STB_(N,level) = 1; \
  3984. PAR(item->STB_(N,parent) = NULL;) \
  3985. return item; \
  3986. } \
  3987. c = STB_(N,_compare)(item,tree); \
  3988. if (c == 0) { \
  3989. if (item != tree) { \
  3990. STB_(N,setleft)(item, tree->STB_(N,left)); \
  3991. STB_(N,setright)(item, tree->STB_(N,right)); \
  3992. item->STB_(N,level) = tree->STB_(N,level); \
  3993. PAR(item->STB_(N,parent) = NULL;) \
  3994. } \
  3995. return item; \
  3996. } \
  3997. if (c < 0) \
  3998. STB_(N,setleft )(tree, STB__(N,insert)(tree->STB_(N,left), item)); \
  3999. else \
  4000. STB_(N,setright)(tree, STB__(N,insert)(tree->STB_(N,right), item)); \
  4001. tree = STB_(N,skew)(tree); \
  4002. tree = STB_(N,split)(tree); \
  4003. PAR(tree->STB_(N,parent) = NULL;) \
  4004. return tree; \
  4005. } \
  4006. \
  4007. TYPE *STB__(N,remove)(TYPE *tree, TYPE *item) \
  4008. { \
  4009. static TYPE *delnode, *leaf, *restore; \
  4010. if (tree == NULL) return NULL; \
  4011. leaf = tree; \
  4012. if (STB_(N,_compare)(item, tree) < 0) { \
  4013. STB_(N,setleft)(tree, STB__(N,remove)(tree->STB_(N,left), item)); \
  4014. } else { \
  4015. TYPE *r; \
  4016. delnode = tree; \
  4017. r = STB__(N,remove)(tree->STB_(N,right), item); \
  4018. /* maybe move 'leaf' up to this location */ \
  4019. if (restore == tree) { tree = leaf; leaf = restore = NULL; } \
  4020. STB_(N,setright)(tree,r); \
  4021. assert(tree->STB_(N,right) != tree); \
  4022. } \
  4023. if (tree == leaf) { \
  4024. if (delnode == item) { \
  4025. tree = tree->STB_(N,right); \
  4026. assert(leaf->STB_(N,left) == NULL); \
  4027. /* move leaf (the right sibling) up to delnode */ \
  4028. STB_(N,setleft )(leaf, item->STB_(N,left )); \
  4029. STB_(N,setright)(leaf, item->STB_(N,right)); \
  4030. leaf->STB_(N,level) = item->STB_(N,level); \
  4031. if (leaf != item) \
  4032. restore = delnode; \
  4033. } \
  4034. delnode = NULL; \
  4035. } else { \
  4036. if (STB__level(N,tree->STB_(N,left) ) < tree->STB_(N,level)-1 || \
  4037. STB__level(N,tree->STB_(N,right)) < tree->STB_(N,level)-1) { \
  4038. --tree->STB_(N,level); \
  4039. if (STB__level(N,tree->STB_(N,right)) > tree->STB_(N,level)) \
  4040. tree->STB_(N,right)->STB_(N,level) = tree->STB_(N,level); \
  4041. tree = STB_(N,skew)(tree); \
  4042. STB_(N,setright)(tree, STB_(N,skew)(tree->STB_(N,right))); \
  4043. if (tree->STB_(N,right)) \
  4044. STB_(N,setright)(tree->STB_(N,right), \
  4045. STB_(N,skew)(tree->STB_(N,right)->STB_(N,right))); \
  4046. tree = STB_(N,split)(tree); \
  4047. if (tree->STB_(N,right)) \
  4048. STB_(N,setright)(tree, STB_(N,split)(tree->STB_(N,right))); \
  4049. } \
  4050. } \
  4051. PAR(if (tree) tree->STB_(N,parent) = NULL;) \
  4052. return tree; \
  4053. } \
  4054. \
  4055. TYPE *STB__(N,last)(TYPE *tree) \
  4056. { \
  4057. if (tree) \
  4058. while (tree->STB_(N,right)) tree = tree->STB_(N,right); \
  4059. return tree; \
  4060. } \
  4061. \
  4062. TYPE *STB__(N,first)(TYPE *tree) \
  4063. { \
  4064. if (tree) \
  4065. while (tree->STB_(N,left)) tree = tree->STB_(N,left); \
  4066. return tree; \
  4067. } \
  4068. \
  4069. TYPE *STB__(N,next)(TYPE *tree, TYPE *item) \
  4070. { \
  4071. TYPE *next = NULL; \
  4072. if (item->STB_(N,right)) \
  4073. return STB__(N,first)(item->STB_(N,right)); \
  4074. PAR( \
  4075. while(item->STB_(N,parent)) { \
  4076. TYPE *up = item->STB_(N,parent); \
  4077. if (up->STB_(N,left) == item) return up; \
  4078. item = up; \
  4079. } \
  4080. return NULL; \
  4081. ) \
  4082. while (tree != item) { \
  4083. if (STB_(N,_compare)(item, tree) < 0) { \
  4084. next = tree; \
  4085. tree = tree->STB_(N,left); \
  4086. } else { \
  4087. tree = tree->STB_(N,right); \
  4088. } \
  4089. } \
  4090. return next; \
  4091. } \
  4092. \
  4093. TYPE *STB__(N,prev)(TYPE *tree, TYPE *item) \
  4094. { \
  4095. TYPE *next = NULL; \
  4096. if (item->STB_(N,left)) \
  4097. return STB__(N,last)(item->STB_(N,left)); \
  4098. PAR( \
  4099. while(item->STB_(N,parent)) { \
  4100. TYPE *up = item->STB_(N,parent); \
  4101. if (up->STB_(N,right) == item) return up; \
  4102. item = up; \
  4103. } \
  4104. return NULL; \
  4105. ) \
  4106. while (tree != item) { \
  4107. if (STB_(N,_compare)(item, tree) < 0) { \
  4108. tree = tree->STB_(N,left); \
  4109. } else { \
  4110. next = tree; \
  4111. tree = tree->STB_(N,right); \
  4112. } \
  4113. } \
  4114. return next; \
  4115. } \
  4116. \
  4117. STB__DEBUG( \
  4118. void STB__(N,_validate)(TYPE *tree, int root) \
  4119. { \
  4120. if (tree == NULL) return; \
  4121. PAR(if(root) assert(tree->STB_(N,parent) == NULL);) \
  4122. assert(STB__level(N,tree->STB_(N,left) ) == tree->STB_(N,level)-1); \
  4123. assert(STB__level(N,tree->STB_(N,right)) <= tree->STB_(N,level)); \
  4124. assert(STB__level(N,tree->STB_(N,right)) >= tree->STB_(N,level)-1); \
  4125. if (tree->STB_(N,right)) { \
  4126. assert(STB__level(N,tree->STB_(N,right)->STB_(N,right)) \
  4127. != tree->STB_(N,level)); \
  4128. PAR(assert(tree->STB_(N,right)->STB_(N,parent) == tree);) \
  4129. } \
  4130. PAR(if(tree->STB_(N,left)) assert(tree->STB_(N,left)->STB_(N,parent) == tree);) \
  4131. STB__(N,_validate)(tree->STB_(N,left) ,0); \
  4132. STB__(N,_validate)(tree->STB_(N,right),0); \
  4133. } \
  4134. ) \
  4135. \
  4136. typedef struct \
  4137. { \
  4138. TYPE *root; \
  4139. } TREE; \
  4140. \
  4141. void STB__(M,Insert)(TREE *tree, TYPE *item) \
  4142. { tree->root = STB__(N,insert)(tree->root, item); } \
  4143. void STB__(M,Remove)(TREE *tree, TYPE *item) \
  4144. { tree->root = STB__(N,remove)(tree->root, item); } \
  4145. TYPE *STB__(M,Next)(TREE *tree, TYPE *item) \
  4146. { return STB__(N,next)(tree->root, item); } \
  4147. TYPE *STB__(M,Prev)(TREE *tree, TYPE *item) \
  4148. { return STB__(N,prev)(tree->root, item); } \
  4149. TYPE *STB__(M,First)(TREE *tree) { return STB__(N,first)(tree->root); } \
  4150. TYPE *STB__(M,Last) (TREE *tree) { return STB__(N,last) (tree->root); } \
  4151. void STB__(M,Init)(TREE *tree) { tree->root = NULL; }
  4152. #define stb_bst_find(N,tree,fcompare) \
  4153. { \
  4154. int c; \
  4155. while (tree != NULL) { \
  4156. fcompare \
  4157. if (c == 0) return tree; \
  4158. if (c < 0) tree = tree->STB_(N,left); \
  4159. else tree = tree->STB_(N,right); \
  4160. } \
  4161. return NULL; \
  4162. }
  4163. #define stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,PAR) \
  4164. stb_bst_base(TYPE,N,TREE,M, \
  4165. VTYPE a = p->vfield; VTYPE b = q->vfield; return (compare);, PAR ) \
  4166. \
  4167. TYPE *STB__(N,find)(TYPE *tree, VTYPE a) \
  4168. stb_bst_find(N,tree,VTYPE b = tree->vfield; c = (compare);) \
  4169. TYPE *STB__(M,Find)(TREE *tree, VTYPE a) \
  4170. { return STB__(N,find)(tree->root, a); }
  4171. #define stb_bst(TYPE,N,TREE,M,vfield,VTYPE,compare) \
  4172. stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,stb__bst_noparent)
  4173. #define stb_bst_parent(TYPE,N,TREE,M,vfield,VTYPE,compare) \
  4174. stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,stb__bst_parent)
  4175. //////////////////////////////////////////////////////////////////////////////
  4176. //
  4177. // Pointer Nulling
  4178. //
  4179. // This lets you automatically NULL dangling pointers to "registered"
  4180. // objects. Note that you have to make sure you call the appropriate
  4181. // functions when you free or realloc blocks of memory that contain
  4182. // pointers or pointer targets. stb.h can automatically do this for
  4183. // stb_arr, or for all frees/reallocs if it's wrapping them.
  4184. //
  4185. #ifdef STB_NPTR
  4186. STB_EXTERN void stb_nptr_set(void *address_of_pointer, void *value_to_write);
  4187. STB_EXTERN void stb_nptr_didset(void *address_of_pointer);
  4188. STB_EXTERN void stb_nptr_didfree(void *address_being_freed, int len);
  4189. STB_EXTERN void stb_nptr_free(void *address_being_freed, int len);
  4190. STB_EXTERN void stb_nptr_didrealloc(void *new_address, void *old_address, int len);
  4191. STB_EXTERN void stb_nptr_recache(void); // recache all known pointers
  4192. // do this after pointer sets outside your control, slow
  4193. #ifdef STB_DEFINE
  4194. // for fast updating on free/realloc, we need to be able to find
  4195. // all the objects (pointers and targets) within a given block;
  4196. // this precludes hashing
  4197. // we use a three-level hierarchy of memory to minimize storage:
  4198. // level 1: 65536 pointers to stb__memory_node (always uses 256 KB)
  4199. // level 2: each stb__memory_node represents a 64K block of memory
  4200. // with 256 stb__memory_leafs (worst case 64MB)
  4201. // level 3: each stb__memory_leaf represents 256 bytes of memory
  4202. // using a list of target locations and a list of pointers
  4203. // (which are hopefully fairly short normally!)
  4204. // this approach won't work in 64-bit, which has a much larger address
  4205. // space. need to redesign
  4206. #define STB__NPTR_ROOT_LOG2 16
  4207. #define STB__NPTR_ROOT_NUM (1 << STB__NPTR_ROOT_LOG2)
  4208. #define STB__NPTR_ROOT_SHIFT (32 - STB__NPTR_ROOT_LOG2)
  4209. #define STB__NPTR_NODE_LOG2 5
  4210. #define STB__NPTR_NODE_NUM (1 << STB__NPTR_NODE_LOG2)
  4211. #define STB__NPTR_NODE_MASK (STB__NPTR_NODE_NUM-1)
  4212. #define STB__NPTR_NODE_SHIFT (STB__NPTR_ROOT_SHIFT - STB__NPTR_NODE_LOG2)
  4213. #define STB__NPTR_NODE_OFFSET(x) (((x) >> STB__NPTR_NODE_SHIFT) & STB__NPTR_NODE_MASK)
  4214. typedef struct stb__st_nptr
  4215. {
  4216. void *ptr; // address of actual pointer
  4217. struct stb__st_nptr *next; // next pointer with same target
  4218. struct stb__st_nptr **prev; // prev pointer with same target, address of 'next' field (or first)
  4219. struct stb__st_nptr *next_in_block;
  4220. } stb__nptr;
  4221. typedef struct stb__st_nptr_target
  4222. {
  4223. void *ptr; // address of target
  4224. stb__nptr *first; // address of first nptr pointing to this
  4225. struct stb__st_nptr_target *next_in_block;
  4226. } stb__nptr_target;
  4227. typedef struct
  4228. {
  4229. stb__nptr *pointers;
  4230. stb__nptr_target *targets;
  4231. } stb__memory_leaf;
  4232. typedef struct
  4233. {
  4234. stb__memory_leaf *children[STB__NPTR_NODE_NUM];
  4235. } stb__memory_node;
  4236. stb__memory_node *stb__memtab_root[STB__NPTR_ROOT_NUM];
  4237. static stb__memory_leaf *stb__nptr_find_leaf(void *mem)
  4238. {
  4239. stb_uint32 address = (stb_uint32) mem;
  4240. stb__memory_node *z = stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT];
  4241. if (z)
  4242. return z->children[STB__NPTR_NODE_OFFSET(address)];
  4243. else
  4244. return NULL;
  4245. }
  4246. static void * stb__nptr_alloc(int size)
  4247. {
  4248. return stb__realloc_raw(0,size);
  4249. }
  4250. static void stb__nptr_free(void *p)
  4251. {
  4252. stb__realloc_raw(p,0);
  4253. }
  4254. static stb__memory_leaf *stb__nptr_make_leaf(void *mem)
  4255. {
  4256. stb_uint32 address = (stb_uint32) mem;
  4257. stb__memory_node *z = stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT];
  4258. stb__memory_leaf *f;
  4259. if (!z) {
  4260. int i;
  4261. z = (stb__memory_node *) stb__nptr_alloc(sizeof(*stb__memtab_root[0]));
  4262. stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT] = z;
  4263. for (i=0; i < 256; ++i)
  4264. z->children[i] = 0;
  4265. }
  4266. f = (stb__memory_leaf *) stb__nptr_alloc(sizeof(*f));
  4267. z->children[STB__NPTR_NODE_OFFSET(address)] = f;
  4268. f->pointers = NULL;
  4269. f->targets = NULL;
  4270. return f;
  4271. }
  4272. static stb__nptr_target *stb__nptr_find_target(void *target, int force)
  4273. {
  4274. stb__memory_leaf *p = stb__nptr_find_leaf(target);
  4275. if (p) {
  4276. stb__nptr_target *t = p->targets;
  4277. while (t) {
  4278. if (t->ptr == target)
  4279. return t;
  4280. t = t->next_in_block;
  4281. }
  4282. }
  4283. if (force) {
  4284. stb__nptr_target *t = (stb__nptr_target*) stb__nptr_alloc(sizeof(*t));
  4285. if (!p) p = stb__nptr_make_leaf(target);
  4286. t->ptr = target;
  4287. t->first = NULL;
  4288. t->next_in_block = p->targets;
  4289. p->targets = t;
  4290. return t;
  4291. } else
  4292. return NULL;
  4293. }
  4294. static stb__nptr *stb__nptr_find_pointer(void *ptr, int force)
  4295. {
  4296. stb__memory_leaf *p = stb__nptr_find_leaf(ptr);
  4297. if (p) {
  4298. stb__nptr *t = p->pointers;
  4299. while (t) {
  4300. if (t->ptr == ptr)
  4301. return t;
  4302. t = t->next_in_block;
  4303. }
  4304. }
  4305. if (force) {
  4306. stb__nptr *t = (stb__nptr *) stb__nptr_alloc(sizeof(*t));
  4307. if (!p) p = stb__nptr_make_leaf(ptr);
  4308. t->ptr = ptr;
  4309. t->next = NULL;
  4310. t->prev = NULL;
  4311. t->next_in_block = p->pointers;
  4312. p->pointers = t;
  4313. return t;
  4314. } else
  4315. return NULL;
  4316. }
  4317. void stb_nptr_set(void *address_of_pointer, void *value_to_write)
  4318. {
  4319. if (*(void **)address_of_pointer != value_to_write) {
  4320. *(void **) address_of_pointer = value_to_write;
  4321. stb_nptr_didset(address_of_pointer);
  4322. }
  4323. }
  4324. void stb_nptr_didset(void *address_of_pointer)
  4325. {
  4326. // first unlink from old chain
  4327. void *new_address;
  4328. stb__nptr *p = stb__nptr_find_pointer(address_of_pointer, 1); // force building if doesn't exist
  4329. if (p->prev) { // if p->prev is NULL, we just built it, or it was NULL
  4330. *(p->prev) = p->next;
  4331. if (p->next) p->next->prev = p->prev;
  4332. }
  4333. // now add to new chain
  4334. new_address = *(void **)address_of_pointer;
  4335. if (new_address != NULL) {
  4336. stb__nptr_target *t = stb__nptr_find_target(new_address, 1);
  4337. p->next = t->first;
  4338. if (p->next) p->next->prev = &p->next;
  4339. p->prev = &t->first;
  4340. t->first = p;
  4341. } else {
  4342. p->prev = NULL;
  4343. p->next = NULL;
  4344. }
  4345. }
  4346. void stb__nptr_block(void *address, int len, void (*function)(stb__memory_leaf *f, int datum, void *start, void *end), int datum)
  4347. {
  4348. void *end_address = (void *) ((char *) address + len - 1);
  4349. stb__memory_node *n;
  4350. stb_uint32 start = (stb_uint32) address;
  4351. stb_uint32 end = start + len - 1;
  4352. int b0 = start >> STB__NPTR_ROOT_SHIFT;
  4353. int b1 = end >> STB__NPTR_ROOT_SHIFT;
  4354. int b=b0,i,e0,e1;
  4355. e0 = STB__NPTR_NODE_OFFSET(start);
  4356. if (datum <= 0) {
  4357. // first block
  4358. n = stb__memtab_root[b0];
  4359. if (n) {
  4360. if (b0 != b1)
  4361. e1 = STB__NPTR_NODE_NUM-1;
  4362. else
  4363. e1 = STB__NPTR_NODE_OFFSET(end);
  4364. for (i=e0; i <= e1; ++i)
  4365. if (n->children[i])
  4366. function(n->children[i], datum, address, end_address);
  4367. }
  4368. if (b1 > b0) {
  4369. // blocks other than the first and last block
  4370. for (b=b0+1; b < b1; ++b) {
  4371. n = stb__memtab_root[b];
  4372. if (n)
  4373. for (i=0; i <= STB__NPTR_NODE_NUM-1; ++i)
  4374. if (n->children[i])
  4375. function(n->children[i], datum, address, end_address);
  4376. }
  4377. // last block
  4378. n = stb__memtab_root[b1];
  4379. if (n) {
  4380. e1 = STB__NPTR_NODE_OFFSET(end);
  4381. for (i=0; i <= e1; ++i)
  4382. if (n->children[i])
  4383. function(n->children[i], datum, address, end_address);
  4384. }
  4385. }
  4386. } else {
  4387. if (b1 > b0) {
  4388. // last block
  4389. n = stb__memtab_root[b1];
  4390. if (n) {
  4391. e1 = STB__NPTR_NODE_OFFSET(end);
  4392. for (i=e1; i >= 0; --i)
  4393. if (n->children[i])
  4394. function(n->children[i], datum, address, end_address);
  4395. }
  4396. // blocks other than the first and last block
  4397. for (b=b1-1; b > b0; --b) {
  4398. n = stb__memtab_root[b];
  4399. if (n)
  4400. for (i=STB__NPTR_NODE_NUM-1; i >= 0; --i)
  4401. if (n->children[i])
  4402. function(n->children[i], datum, address, end_address);
  4403. }
  4404. }
  4405. // first block
  4406. n = stb__memtab_root[b0];
  4407. if (n) {
  4408. if (b0 != b1)
  4409. e1 = STB__NPTR_NODE_NUM-1;
  4410. else
  4411. e1 = STB__NPTR_NODE_OFFSET(end);
  4412. for (i=e1; i >= e0; --i)
  4413. if (n->children[i])
  4414. function(n->children[i], datum, address, end_address);
  4415. }
  4416. }
  4417. }
  4418. static void stb__nptr_delete_pointers(stb__memory_leaf *f, int offset, void *start, void *end)
  4419. {
  4420. stb__nptr **p = &f->pointers;
  4421. while (*p) {
  4422. stb__nptr *n = *p;
  4423. if (n->ptr >= start && n->ptr <= end) {
  4424. // unlink
  4425. if (n->prev) {
  4426. *(n->prev) = n->next;
  4427. if (n->next) n->next->prev = n->prev;
  4428. }
  4429. *p = n->next_in_block;
  4430. stb__nptr_free(n);
  4431. } else
  4432. p = &(n->next_in_block);
  4433. }
  4434. }
  4435. static void stb__nptr_delete_targets(stb__memory_leaf *f, int offset, void *start, void *end)
  4436. {
  4437. stb__nptr_target **p = &f->targets;
  4438. while (*p) {
  4439. stb__nptr_target *n = *p;
  4440. if (n->ptr >= start && n->ptr <= end) {
  4441. // null pointers
  4442. stb__nptr *z = n->first;
  4443. while (z) {
  4444. stb__nptr *y = z->next;
  4445. z->prev = NULL;
  4446. z->next = NULL;
  4447. *(void **) z->ptr = NULL;
  4448. z = y;
  4449. }
  4450. // unlink this target
  4451. *p = n->next_in_block;
  4452. stb__nptr_free(n);
  4453. } else
  4454. p = &(n->next_in_block);
  4455. }
  4456. }
  4457. void stb_nptr_didfree(void *address_being_freed, int len)
  4458. {
  4459. // step one: delete all pointers in this block
  4460. stb__nptr_block(address_being_freed, len, stb__nptr_delete_pointers, 0);
  4461. // step two: NULL all pointers to this block; do this second to avoid NULLing deleted pointers
  4462. stb__nptr_block(address_being_freed, len, stb__nptr_delete_targets, 0);
  4463. }
  4464. void stb_nptr_free(void *address_being_freed, int len)
  4465. {
  4466. free(address_being_freed);
  4467. stb_nptr_didfree(address_being_freed, len);
  4468. }
  4469. static void stb__nptr_move_targets(stb__memory_leaf *f, int offset, void *start, void *end)
  4470. {
  4471. stb__nptr_target **t = &f->targets;
  4472. while (*t) {
  4473. stb__nptr_target *n = *t;
  4474. if (n->ptr >= start && n->ptr <= end) {
  4475. stb__nptr *z;
  4476. stb__memory_leaf *f;
  4477. // unlink n
  4478. *t = n->next_in_block;
  4479. // update n to new address
  4480. n->ptr = (void *) ((char *) n->ptr + offset);
  4481. f = stb__nptr_find_leaf(n->ptr);
  4482. if (!f) f = stb__nptr_make_leaf(n->ptr);
  4483. n->next_in_block = f->targets;
  4484. f->targets = n;
  4485. // now go through all pointers and make them point here
  4486. z = n->first;
  4487. while (z) {
  4488. *(void**) z->ptr = n->ptr;
  4489. z = z->next;
  4490. }
  4491. } else
  4492. t = &(n->next_in_block);
  4493. }
  4494. }
  4495. static void stb__nptr_move_pointers(stb__memory_leaf *f, int offset, void *start, void *end)
  4496. {
  4497. stb__nptr **p = &f->pointers;
  4498. while (*p) {
  4499. stb__nptr *n = *p;
  4500. if (n->ptr >= start && n->ptr <= end) {
  4501. // unlink
  4502. *p = n->next_in_block;
  4503. n->ptr = (void *) ((int) n->ptr + offset);
  4504. // move to new block
  4505. f = stb__nptr_find_leaf(n->ptr);
  4506. if (!f) f = stb__nptr_make_leaf(n->ptr);
  4507. n->next_in_block = f->pointers;
  4508. f->pointers = n;
  4509. } else
  4510. p = &(n->next_in_block);
  4511. }
  4512. }
  4513. void stb_nptr_realloc(void *new_address, void *old_address, int len)
  4514. {
  4515. if (new_address == old_address) return;
  4516. // have to move the pointers first, because moving the targets
  4517. // requires writing to the pointers-to-the-targets, and if some of those moved too,
  4518. // we need to make sure we don't write to the old memory
  4519. // step one: move all pointers within the block
  4520. stb__nptr_block(old_address, len, stb__nptr_move_pointers, (char *) new_address - (char *) old_address);
  4521. // step two: move all targets within the block
  4522. stb__nptr_block(old_address, len, stb__nptr_move_targets, (char *) new_address - (char *) old_address);
  4523. }
  4524. void stb_nptr_move(void *new_address, void *old_address)
  4525. {
  4526. stb_nptr_realloc(new_address, old_address, 1);
  4527. }
  4528. void stb_nptr_recache(void)
  4529. {
  4530. int i,j;
  4531. for (i=0; i < STB__NPTR_ROOT_NUM; ++i)
  4532. if (stb__memtab_root[i])
  4533. for (j=0; j < STB__NPTR_NODE_NUM; ++j)
  4534. if (stb__memtab_root[i]->children[j]) {
  4535. stb__nptr *p = stb__memtab_root[i]->children[j]->pointers;
  4536. while (p) {
  4537. stb_nptr_didset(p->ptr);
  4538. p = p->next_in_block;
  4539. }
  4540. }
  4541. }
  4542. #endif // STB_DEFINE
  4543. #endif // STB_NPTR
  4544. //////////////////////////////////////////////////////////////////////////////
  4545. //
  4546. // File Processing
  4547. //
  4548. #ifdef _WIN32
  4549. #define stb_rename(x,y) _wrename((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
  4550. #else
  4551. #define stb_rename rename
  4552. #endif
  4553. STB_EXTERN void stb_fput_varlen64(FILE *f, stb_uint64 v);
  4554. STB_EXTERN stb_uint64 stb_fget_varlen64(FILE *f);
  4555. STB_EXTERN int stb_size_varlen64(stb_uint64 v);
  4556. #define stb_filec (char *) stb_file
  4557. #define stb_fileu (unsigned char *) stb_file
  4558. STB_EXTERN void * stb_file(char *filename, size_t *length);
  4559. STB_EXTERN void * stb_file_max(char *filename, size_t *length);
  4560. STB_EXTERN size_t stb_filelen(FILE *f);
  4561. STB_EXTERN int stb_filewrite(char *filename, void *data, size_t length);
  4562. STB_EXTERN int stb_filewritestr(char *filename, char *data);
  4563. STB_EXTERN char ** stb_stringfile(char *filename, int *len);
  4564. STB_EXTERN char ** stb_stringfile_trimmed(char *name, int *len, char comm);
  4565. STB_EXTERN char * stb_fgets(char *buffer, int buflen, FILE *f);
  4566. STB_EXTERN char * stb_fgets_malloc(FILE *f);
  4567. STB_EXTERN int stb_fexists(char *filename);
  4568. STB_EXTERN int stb_fcmp(char *s1, char *s2);
  4569. STB_EXTERN int stb_feq(char *s1, char *s2);
  4570. STB_EXTERN time_t stb_ftimestamp(char *filename);
  4571. STB_EXTERN int stb_fullpath(char *abs, int abs_size, char *rel);
  4572. STB_EXTERN FILE * stb_fopen(char *filename, const char *mode);
  4573. STB_EXTERN int stb_fclose(FILE *f, int keep);
  4574. enum
  4575. {
  4576. stb_keep_no = 0,
  4577. stb_keep_yes = 1,
  4578. stb_keep_if_different = 2,
  4579. };
  4580. STB_EXTERN int stb_copyfile(char *src, char *dest);
  4581. STB_EXTERN void stb_fput_varlen64(FILE *f, stb_uint64 v);
  4582. STB_EXTERN stb_uint64 stb_fget_varlen64(FILE *f);
  4583. STB_EXTERN int stb_size_varlen64(stb_uint64 v);
  4584. STB_EXTERN void stb_fwrite32(FILE *f, stb_uint32 datum);
  4585. STB_EXTERN void stb_fput_varlen (FILE *f, int v);
  4586. STB_EXTERN void stb_fput_varlenu(FILE *f, unsigned int v);
  4587. STB_EXTERN int stb_fget_varlen (FILE *f);
  4588. STB_EXTERN stb_uint stb_fget_varlenu(FILE *f);
  4589. STB_EXTERN void stb_fput_ranged (FILE *f, int v, int b, stb_uint n);
  4590. STB_EXTERN int stb_fget_ranged (FILE *f, int b, stb_uint n);
  4591. STB_EXTERN int stb_size_varlen (int v);
  4592. STB_EXTERN int stb_size_varlenu(unsigned int v);
  4593. STB_EXTERN int stb_size_ranged (int b, stb_uint n);
  4594. STB_EXTERN int stb_fread(void *data, size_t len, size_t count, void *f);
  4595. STB_EXTERN int stb_fwrite(void *data, size_t len, size_t count, void *f);
  4596. #if 0
  4597. typedef struct
  4598. {
  4599. FILE *base_file;
  4600. char *buffer;
  4601. int buffer_size;
  4602. int buffer_off;
  4603. int buffer_left;
  4604. } STBF;
  4605. STB_EXTERN STBF *stb_tfopen(char *filename, char *mode);
  4606. STB_EXTERN int stb_tfread(void *data, size_t len, size_t count, STBF *f);
  4607. STB_EXTERN int stb_tfwrite(void *data, size_t len, size_t count, STBF *f);
  4608. #endif
  4609. #ifdef STB_DEFINE
  4610. #if 0
  4611. STBF *stb_tfopen(char *filename, char *mode)
  4612. {
  4613. STBF *z;
  4614. FILE *f = stb_p_fopen(filename, mode);
  4615. if (!f) return NULL;
  4616. z = (STBF *) malloc(sizeof(*z));
  4617. if (!z) { fclose(f); return NULL; }
  4618. z->base_file = f;
  4619. if (!strcmp(mode, "rb") || !strcmp(mode, "wb")) {
  4620. z->buffer_size = 4096;
  4621. z->buffer_off = z->buffer_size;
  4622. z->buffer_left = 0;
  4623. z->buffer = malloc(z->buffer_size);
  4624. if (!z->buffer) { free(z); fclose(f); return NULL; }
  4625. } else {
  4626. z->buffer = 0;
  4627. z->buffer_size = 0;
  4628. z->buffer_left = 0;
  4629. }
  4630. return z;
  4631. }
  4632. int stb_tfread(void *data, size_t len, size_t count, STBF *f)
  4633. {
  4634. int total = len*count, done=0;
  4635. if (!total) return 0;
  4636. if (total <= z->buffer_left) {
  4637. memcpy(data, z->buffer + z->buffer_off, total);
  4638. z->buffer_off += total;
  4639. z->buffer_left -= total;
  4640. return count;
  4641. } else {
  4642. char *out = (char *) data;
  4643. // consume all buffered data
  4644. memcpy(data, z->buffer + z->buffer_off, z->buffer_left);
  4645. done = z->buffer_left;
  4646. out += z->buffer_left;
  4647. z->buffer_left=0;
  4648. if (total-done > (z->buffer_size >> 1)) {
  4649. done += fread(out
  4650. }
  4651. }
  4652. }
  4653. #endif
  4654. void stb_fwrite32(FILE *f, stb_uint32 x)
  4655. {
  4656. fwrite(&x, 4, 1, f);
  4657. }
  4658. #if defined(_WIN32)
  4659. #define stb__stat _stat
  4660. #else
  4661. #define stb__stat stat
  4662. #endif
  4663. int stb_fexists(char *filename)
  4664. {
  4665. struct stb__stat buf;
  4666. return stb__windows(
  4667. _wstat((const wchar_t *)stb__from_utf8(filename), &buf),
  4668. stat(filename,&buf)
  4669. ) == 0;
  4670. }
  4671. time_t stb_ftimestamp(char *filename)
  4672. {
  4673. struct stb__stat buf;
  4674. if (stb__windows(
  4675. _wstat((const wchar_t *)stb__from_utf8(filename), &buf),
  4676. stat(filename,&buf)
  4677. ) == 0)
  4678. {
  4679. return buf.st_mtime;
  4680. } else {
  4681. return 0;
  4682. }
  4683. }
  4684. size_t stb_filelen(FILE *f)
  4685. {
  4686. long len, pos;
  4687. pos = ftell(f);
  4688. fseek(f, 0, SEEK_END);
  4689. len = ftell(f);
  4690. fseek(f, pos, SEEK_SET);
  4691. return (size_t) len;
  4692. }
  4693. void *stb_file(char *filename, size_t *length)
  4694. {
  4695. FILE *f = stb__fopen(filename, "rb");
  4696. char *buffer;
  4697. size_t len, len2;
  4698. if (!f) return NULL;
  4699. len = stb_filelen(f);
  4700. buffer = (char *) malloc(len+2); // nul + extra
  4701. len2 = fread(buffer, 1, len, f);
  4702. if (len2 == len) {
  4703. if (length) *length = len;
  4704. buffer[len] = 0;
  4705. } else {
  4706. free(buffer);
  4707. buffer = NULL;
  4708. }
  4709. fclose(f);
  4710. return buffer;
  4711. }
  4712. int stb_filewrite(char *filename, void *data, size_t length)
  4713. {
  4714. FILE *f = stb_fopen(filename, "wb");
  4715. if (f) {
  4716. unsigned char *data_ptr = (unsigned char *) data;
  4717. size_t remaining = length;
  4718. while (remaining > 0) {
  4719. size_t len2 = remaining > 65536 ? 65536 : remaining;
  4720. size_t len3 = fwrite(data_ptr, 1, len2, f);
  4721. if (len2 != len3) {
  4722. fprintf(stderr, "Failed while writing %s\n", filename);
  4723. break;
  4724. }
  4725. remaining -= len2;
  4726. data_ptr += len2;
  4727. }
  4728. stb_fclose(f, stb_keep_if_different);
  4729. }
  4730. return f != NULL;
  4731. }
  4732. int stb_filewritestr(char *filename, char *data)
  4733. {
  4734. return stb_filewrite(filename, data, strlen(data));
  4735. }
  4736. void * stb_file_max(char *filename, size_t *length)
  4737. {
  4738. FILE *f = stb__fopen(filename, "rb");
  4739. char *buffer;
  4740. size_t len, maxlen;
  4741. if (!f) return NULL;
  4742. maxlen = *length;
  4743. buffer = (char *) malloc(maxlen+1);
  4744. len = fread(buffer, 1, maxlen, f);
  4745. buffer[len] = 0;
  4746. fclose(f);
  4747. *length = len;
  4748. return buffer;
  4749. }
  4750. char ** stb_stringfile(char *filename, int *plen)
  4751. {
  4752. FILE *f = stb__fopen(filename, "rb");
  4753. char *buffer, **list=NULL, *s;
  4754. size_t len, count, i;
  4755. if (!f) return NULL;
  4756. len = stb_filelen(f);
  4757. buffer = (char *) malloc(len+1);
  4758. len = fread(buffer, 1, len, f);
  4759. buffer[len] = 0;
  4760. fclose(f);
  4761. // two passes through: first time count lines, second time set them
  4762. for (i=0; i < 2; ++i) {
  4763. s = buffer;
  4764. if (i == 1)
  4765. list[0] = s;
  4766. count = 1;
  4767. while (*s) {
  4768. if (*s == '\n' || *s == '\r') {
  4769. // detect if both cr & lf are together
  4770. int crlf = (s[0] + s[1]) == ('\n' + '\r');
  4771. if (i == 1) *s = 0;
  4772. if (crlf) ++s;
  4773. if (s[1]) { // it's not over yet
  4774. if (i == 1) list[count] = s+1;
  4775. ++count;
  4776. }
  4777. }
  4778. ++s;
  4779. }
  4780. if (i == 0) {
  4781. list = (char **) malloc(sizeof(*list) * (count+1) + len+1);
  4782. if (!list) return NULL;
  4783. list[count] = 0;
  4784. // recopy the file so there's just a single allocation to free
  4785. memcpy(&list[count+1], buffer, len+1);
  4786. free(buffer);
  4787. buffer = (char *) &list[count+1];
  4788. if (plen) *plen = (int) count;
  4789. }
  4790. }
  4791. return list;
  4792. }
  4793. char ** stb_stringfile_trimmed(char *name, int *len, char comment)
  4794. {
  4795. int i,n,o=0;
  4796. char **s = stb_stringfile(name, &n);
  4797. if (s == NULL) return NULL;
  4798. for (i=0; i < n; ++i) {
  4799. char *p = stb_skipwhite(s[i]);
  4800. if (*p && *p != comment)
  4801. s[o++] = p;
  4802. }
  4803. s[o] = NULL;
  4804. if (len) *len = o;
  4805. return s;
  4806. }
  4807. char * stb_fgets(char *buffer, int buflen, FILE *f)
  4808. {
  4809. char *p;
  4810. buffer[0] = 0;
  4811. p = fgets(buffer, buflen, f);
  4812. if (p) {
  4813. int n = (int) (strlen(p)-1);
  4814. if (n >= 0)
  4815. if (p[n] == '\n')
  4816. p[n] = 0;
  4817. }
  4818. return p;
  4819. }
  4820. char * stb_fgets_malloc(FILE *f)
  4821. {
  4822. // avoid reallocing for small strings
  4823. char quick_buffer[800];
  4824. quick_buffer[sizeof(quick_buffer)-2] = 0;
  4825. if (!fgets(quick_buffer, sizeof(quick_buffer), f))
  4826. return NULL;
  4827. if (quick_buffer[sizeof(quick_buffer)-2] == 0) {
  4828. size_t n = strlen(quick_buffer);
  4829. if (n > 0 && quick_buffer[n-1] == '\n')
  4830. quick_buffer[n-1] = 0;
  4831. return stb_p_strdup(quick_buffer);
  4832. } else {
  4833. char *p;
  4834. char *a = stb_p_strdup(quick_buffer);
  4835. size_t len = sizeof(quick_buffer)-1;
  4836. while (!feof(f)) {
  4837. if (a[len-1] == '\n') break;
  4838. a = (char *) realloc(a, len*2);
  4839. p = &a[len];
  4840. p[len-2] = 0;
  4841. if (!fgets(p, (int) len, f))
  4842. break;
  4843. if (p[len-2] == 0) {
  4844. len += strlen(p);
  4845. break;
  4846. }
  4847. len = len + (len-1);
  4848. }
  4849. if (a[len-1] == '\n')
  4850. a[len-1] = 0;
  4851. return a;
  4852. }
  4853. }
  4854. int stb_fullpath(char *abs, int abs_size, char *rel)
  4855. {
  4856. #ifdef _WIN32
  4857. return _fullpath(abs, rel, abs_size) != NULL;
  4858. #else
  4859. if (rel[0] == '/' || rel[0] == '~') {
  4860. if ((int) strlen(rel) >= abs_size)
  4861. return 0;
  4862. stb_p_strcpy_s(abs,65536,rel);
  4863. return STB_TRUE;
  4864. } else {
  4865. int n;
  4866. getcwd(abs, abs_size);
  4867. n = strlen(abs);
  4868. if (n+(int) strlen(rel)+2 <= abs_size) {
  4869. abs[n] = '/';
  4870. stb_p_strcpy_s(abs+n+1, 65536,rel);
  4871. return STB_TRUE;
  4872. } else {
  4873. return STB_FALSE;
  4874. }
  4875. }
  4876. #endif
  4877. }
  4878. static int stb_fcmp_core(FILE *f, FILE *g)
  4879. {
  4880. char buf1[1024],buf2[1024];
  4881. int n1,n2, res=0;
  4882. while (1) {
  4883. n1 = (int) fread(buf1, 1, sizeof(buf1), f);
  4884. n2 = (int) fread(buf2, 1, sizeof(buf2), g);
  4885. res = memcmp(buf1,buf2,stb_min(n1,n2));
  4886. if (res)
  4887. break;
  4888. if (n1 != n2) {
  4889. res = n1 < n2 ? -1 : 1;
  4890. break;
  4891. }
  4892. if (n1 == 0)
  4893. break;
  4894. }
  4895. fclose(f);
  4896. fclose(g);
  4897. return res;
  4898. }
  4899. int stb_fcmp(char *s1, char *s2)
  4900. {
  4901. FILE *f = stb__fopen(s1, "rb");
  4902. FILE *g = stb__fopen(s2, "rb");
  4903. if (f == NULL || g == NULL) {
  4904. if (f) fclose(f);
  4905. if (g) {
  4906. fclose(g);
  4907. return STB_TRUE;
  4908. }
  4909. return f != NULL;
  4910. }
  4911. return stb_fcmp_core(f,g);
  4912. }
  4913. int stb_feq(char *s1, char *s2)
  4914. {
  4915. FILE *f = stb__fopen(s1, "rb");
  4916. FILE *g = stb__fopen(s2, "rb");
  4917. if (f == NULL || g == NULL) {
  4918. if (f) fclose(f);
  4919. if (g) fclose(g);
  4920. return f == g;
  4921. }
  4922. // feq is faster because it shortcuts if they're different length
  4923. if (stb_filelen(f) != stb_filelen(g)) {
  4924. fclose(f);
  4925. fclose(g);
  4926. return 0;
  4927. }
  4928. return !stb_fcmp_core(f,g);
  4929. }
  4930. static stb_ptrmap *stb__files;
  4931. typedef struct
  4932. {
  4933. char *temp_name;
  4934. char *name;
  4935. int errors;
  4936. } stb__file_data;
  4937. static FILE *stb__open_temp_file(char *temp_name, char *src_name, const char *mode)
  4938. {
  4939. size_t p;
  4940. #ifdef _MSC_VER
  4941. int j;
  4942. #endif
  4943. FILE *f;
  4944. // try to generate a temporary file in the same directory
  4945. p = strlen(src_name)-1;
  4946. while (p > 0 && src_name[p] != '/' && src_name[p] != '\\'
  4947. && src_name[p] != ':' && src_name[p] != '~')
  4948. --p;
  4949. ++p;
  4950. memcpy(temp_name, src_name, p);
  4951. #ifdef _MSC_VER
  4952. // try multiple times to make a temp file... just in
  4953. // case some other process makes the name first
  4954. for (j=0; j < 32; ++j) {
  4955. stb_p_strcpy_s(temp_name+p, 65536, "stmpXXXXXX");
  4956. if (!stb_p_mktemp(temp_name))
  4957. return 0;
  4958. f = stb_p_fopen(temp_name, mode);
  4959. if (f != NULL)
  4960. break;
  4961. }
  4962. #else
  4963. {
  4964. stb_p_strcpy_s(temp_name+p, 65536, "stmpXXXXXX");
  4965. #ifdef __MINGW32__
  4966. int fd = open(stb_p_mktemp(temp_name), O_RDWR);
  4967. #else
  4968. int fd = mkstemp(temp_name);
  4969. #endif
  4970. if (fd == -1) return NULL;
  4971. f = fdopen(fd, mode);
  4972. if (f == NULL) {
  4973. unlink(temp_name);
  4974. close(fd);
  4975. return NULL;
  4976. }
  4977. }
  4978. #endif
  4979. return f;
  4980. }
  4981. FILE * stb_fopen(char *filename, const char *mode)
  4982. {
  4983. FILE *f;
  4984. char name_full[4096];
  4985. char temp_full[sizeof(name_full) + 12];
  4986. // @TODO: if the file doesn't exist, we can also use the fastpath here
  4987. if (mode[0] != 'w' && !strchr(mode, '+'))
  4988. return stb__fopen(filename, mode);
  4989. // save away the full path to the file so if the program
  4990. // changes the cwd everything still works right! unix has
  4991. // better ways to do this, but we have to work in windows
  4992. name_full[0] = '\0'; // stb_fullpath reads name_full[0]
  4993. if (stb_fullpath(name_full, sizeof(name_full), filename)==0)
  4994. return 0;
  4995. f = stb__open_temp_file(temp_full, name_full, mode);
  4996. if (f != NULL) {
  4997. stb__file_data *d = (stb__file_data *) malloc(sizeof(*d));
  4998. if (!d) { assert(0); /* NOTREACHED */fclose(f); return NULL; }
  4999. if (stb__files == NULL) stb__files = stb_ptrmap_create();
  5000. d->temp_name = stb_p_strdup(temp_full);
  5001. d->name = stb_p_strdup(name_full);
  5002. d->errors = 0;
  5003. stb_ptrmap_add(stb__files, f, d);
  5004. return f;
  5005. }
  5006. return NULL;
  5007. }
  5008. int stb_fclose(FILE *f, int keep)
  5009. {
  5010. stb__file_data *d;
  5011. int ok = STB_FALSE;
  5012. if (f == NULL) return 0;
  5013. if (ferror(f))
  5014. keep = stb_keep_no;
  5015. fclose(f);
  5016. if (stb__files && stb_ptrmap_remove(stb__files, f, (void **) &d)) {
  5017. if (stb__files->count == 0) {
  5018. stb_ptrmap_destroy(stb__files);
  5019. stb__files = NULL;
  5020. }
  5021. } else
  5022. return STB_TRUE; // not special
  5023. if (keep == stb_keep_if_different) {
  5024. // check if the files are identical
  5025. if (stb_feq(d->name, d->temp_name)) {
  5026. keep = stb_keep_no;
  5027. ok = STB_TRUE; // report success if no change
  5028. }
  5029. }
  5030. if (keep == stb_keep_no) {
  5031. remove(d->temp_name);
  5032. } else {
  5033. if (!stb_fexists(d->name)) {
  5034. // old file doesn't exist, so just move the new file over it
  5035. stb_rename(d->temp_name, d->name);
  5036. } else {
  5037. // don't delete the old file yet in case there are troubles! First rename it!
  5038. char preserved_old_file[4096];
  5039. // generate a temp filename in the same directory (also creates it, which we don't need)
  5040. FILE *dummy = stb__open_temp_file(preserved_old_file, d->name, "wb");
  5041. if (dummy != NULL) {
  5042. // we don't actually want the open file
  5043. fclose(dummy);
  5044. // discard what we just created
  5045. remove(preserved_old_file); // if this fails, there's nothing we can do, and following logic handles it as best as possible anyway
  5046. // move the existing file to the preserved name
  5047. if (0 != stb_rename(d->name, preserved_old_file)) { // 0 on success
  5048. // failed, state is:
  5049. // filename -> old file
  5050. // tempname -> new file
  5051. // keep tempname around so we don't lose data
  5052. } else {
  5053. // state is:
  5054. // preserved -> old file
  5055. // tempname -> new file
  5056. // move the new file to the old name
  5057. if (0 == stb_rename(d->temp_name, d->name)) {
  5058. // state is:
  5059. // preserved -> old file
  5060. // filename -> new file
  5061. ok = STB_TRUE;
  5062. // 'filename -> new file' has always been the goal, so clean up
  5063. remove(preserved_old_file); // nothing to be done if it fails
  5064. } else {
  5065. // couldn't rename, so try renaming preserved file back
  5066. // state is:
  5067. // preserved -> old file
  5068. // tempname -> new file
  5069. stb_rename(preserved_old_file, d->name);
  5070. // if the rename failed, there's nothing more we can do
  5071. }
  5072. }
  5073. } else {
  5074. // we couldn't get a temp filename. do this the naive way; the worst case failure here
  5075. // leaves the filename pointing to nothing and the new file as a tempfile
  5076. remove(d->name);
  5077. stb_rename(d->temp_name, d->name);
  5078. }
  5079. }
  5080. }
  5081. free(d->temp_name);
  5082. free(d->name);
  5083. free(d);
  5084. return ok;
  5085. }
  5086. int stb_copyfile(char *src, char *dest)
  5087. {
  5088. char raw_buffer[1024];
  5089. char *buffer;
  5090. int buf_size = 65536;
  5091. FILE *f, *g;
  5092. // if file already exists at destination, do nothing
  5093. if (stb_feq(src, dest)) return STB_TRUE;
  5094. // open file
  5095. f = stb__fopen(src, "rb");
  5096. if (f == NULL) return STB_FALSE;
  5097. // open file for writing
  5098. g = stb__fopen(dest, "wb");
  5099. if (g == NULL) {
  5100. fclose(f);
  5101. return STB_FALSE;
  5102. }
  5103. buffer = (char *) malloc(buf_size);
  5104. if (buffer == NULL) {
  5105. buffer = raw_buffer;
  5106. buf_size = sizeof(raw_buffer);
  5107. }
  5108. while (!feof(f)) {
  5109. size_t n = fread(buffer, 1, buf_size, f);
  5110. if (n != 0)
  5111. fwrite(buffer, 1, n, g);
  5112. }
  5113. fclose(f);
  5114. if (buffer != raw_buffer)
  5115. free(buffer);
  5116. fclose(g);
  5117. return STB_TRUE;
  5118. }
  5119. // varlen:
  5120. // v' = (v >> 31) + (v < 0 ? ~v : v)<<1; // small abs(v) => small v'
  5121. // output v as big endian v'+k for v' <= k:
  5122. // 1 byte : v' <= 0x00000080 ( -64 <= v < 64) 7 bits
  5123. // 2 bytes: v' <= 0x00004000 (-8192 <= v < 8192) 14 bits
  5124. // 3 bytes: v' <= 0x00200000 21 bits
  5125. // 4 bytes: v' <= 0x10000000 28 bits
  5126. // the number of most significant 1-bits in the first byte
  5127. // equals the number of bytes after the first
  5128. #define stb__varlen_xform(v) (v<0 ? (~v << 1)+1 : (v << 1))
  5129. int stb_size_varlen(int v) { return stb_size_varlenu(stb__varlen_xform(v)); }
  5130. int stb_size_varlenu(unsigned int v)
  5131. {
  5132. if (v < 0x00000080) return 1;
  5133. if (v < 0x00004000) return 2;
  5134. if (v < 0x00200000) return 3;
  5135. if (v < 0x10000000) return 4;
  5136. return 5;
  5137. }
  5138. void stb_fput_varlen(FILE *f, int v) { stb_fput_varlenu(f, stb__varlen_xform(v)); }
  5139. void stb_fput_varlenu(FILE *f, unsigned int z)
  5140. {
  5141. if (z >= 0x10000000) fputc(0xF0,f);
  5142. if (z >= 0x00200000) fputc((z < 0x10000000 ? 0xE0 : 0)+(z>>24),f);
  5143. if (z >= 0x00004000) fputc((z < 0x00200000 ? 0xC0 : 0)+(z>>16),f);
  5144. if (z >= 0x00000080) fputc((z < 0x00004000 ? 0x80 : 0)+(z>> 8),f);
  5145. fputc(z,f);
  5146. }
  5147. #define stb_fgetc(f) ((unsigned char) fgetc(f))
  5148. int stb_fget_varlen(FILE *f)
  5149. {
  5150. unsigned int z = stb_fget_varlenu(f);
  5151. return (z & 1) ? ~(z>>1) : (z>>1);
  5152. }
  5153. unsigned int stb_fget_varlenu(FILE *f)
  5154. {
  5155. unsigned int z;
  5156. unsigned char d;
  5157. d = stb_fgetc(f);
  5158. if (d >= 0x80) {
  5159. if (d >= 0xc0) {
  5160. if (d >= 0xe0) {
  5161. if (d == 0xf0) z = stb_fgetc(f) << 24;
  5162. else z = (d - 0xe0) << 24;
  5163. z += stb_fgetc(f) << 16;
  5164. }
  5165. else
  5166. z = (d - 0xc0) << 16;
  5167. z += stb_fgetc(f) << 8;
  5168. } else
  5169. z = (d - 0x80) << 8;
  5170. z += stb_fgetc(f);
  5171. } else
  5172. z = d;
  5173. return z;
  5174. }
  5175. stb_uint64 stb_fget_varlen64(FILE *f)
  5176. {
  5177. stb_uint64 z;
  5178. unsigned char d;
  5179. d = stb_fgetc(f);
  5180. if (d >= 0x80) {
  5181. if (d >= 0xc0) {
  5182. if (d >= 0xe0) {
  5183. if (d >= 0xf0) {
  5184. if (d >= 0xf8) {
  5185. if (d >= 0xfc) {
  5186. if (d >= 0xfe) {
  5187. if (d >= 0xff)
  5188. z = (stb_uint64) stb_fgetc(f) << 56;
  5189. else
  5190. z = (stb_uint64) (d - 0xfe) << 56;
  5191. z |= (stb_uint64) stb_fgetc(f) << 48;
  5192. } else z = (stb_uint64) (d - 0xfc) << 48;
  5193. z |= (stb_uint64) stb_fgetc(f) << 40;
  5194. } else z = (stb_uint64) (d - 0xf8) << 40;
  5195. z |= (stb_uint64) stb_fgetc(f) << 32;
  5196. } else z = (stb_uint64) (d - 0xf0) << 32;
  5197. z |= (stb_uint) stb_fgetc(f) << 24;
  5198. } else z = (stb_uint) (d - 0xe0) << 24;
  5199. z |= (stb_uint) stb_fgetc(f) << 16;
  5200. } else z = (stb_uint) (d - 0xc0) << 16;
  5201. z |= (stb_uint) stb_fgetc(f) << 8;
  5202. } else z = (stb_uint) (d - 0x80) << 8;
  5203. z |= stb_fgetc(f);
  5204. } else
  5205. z = d;
  5206. return (z & 1) ? ~(z >> 1) : (z >> 1);
  5207. }
  5208. int stb_size_varlen64(stb_uint64 v)
  5209. {
  5210. if (v < 0x00000080) return 1;
  5211. if (v < 0x00004000) return 2;
  5212. if (v < 0x00200000) return 3;
  5213. if (v < 0x10000000) return 4;
  5214. if (v < STB_IMM_UINT64(0x0000000800000000)) return 5;
  5215. if (v < STB_IMM_UINT64(0x0000040000000000)) return 6;
  5216. if (v < STB_IMM_UINT64(0x0002000000000000)) return 7;
  5217. if (v < STB_IMM_UINT64(0x0100000000000000)) return 8;
  5218. return 9;
  5219. }
  5220. void stb_fput_varlen64(FILE *f, stb_uint64 v)
  5221. {
  5222. stb_uint64 z = stb__varlen_xform(v);
  5223. int first=1;
  5224. if (z >= STB_IMM_UINT64(0x100000000000000)) {
  5225. fputc(0xff,f);
  5226. first=0;
  5227. }
  5228. if (z >= STB_IMM_UINT64(0x02000000000000)) fputc((first ? 0xFE : 0)+(char)(z>>56),f), first=0;
  5229. if (z >= STB_IMM_UINT64(0x00040000000000)) fputc((first ? 0xFC : 0)+(char)(z>>48),f), first=0;
  5230. if (z >= STB_IMM_UINT64(0x00000800000000)) fputc((first ? 0xF8 : 0)+(char)(z>>40),f), first=0;
  5231. if (z >= STB_IMM_UINT64(0x00000010000000)) fputc((first ? 0xF0 : 0)+(char)(z>>32),f), first=0;
  5232. if (z >= STB_IMM_UINT64(0x00000000200000)) fputc((first ? 0xE0 : 0)+(char)(z>>24),f), first=0;
  5233. if (z >= STB_IMM_UINT64(0x00000000004000)) fputc((first ? 0xC0 : 0)+(char)(z>>16),f), first=0;
  5234. if (z >= STB_IMM_UINT64(0x00000000000080)) fputc((first ? 0x80 : 0)+(char)(z>> 8),f), first=0;
  5235. fputc((char)z,f);
  5236. }
  5237. void stb_fput_ranged(FILE *f, int v, int b, stb_uint n)
  5238. {
  5239. v -= b;
  5240. if (n <= (1 << 31))
  5241. assert((stb_uint) v < n);
  5242. if (n > (1 << 24)) fputc(v >> 24, f);
  5243. if (n > (1 << 16)) fputc(v >> 16, f);
  5244. if (n > (1 << 8)) fputc(v >> 8, f);
  5245. fputc(v,f);
  5246. }
  5247. int stb_fget_ranged(FILE *f, int b, stb_uint n)
  5248. {
  5249. unsigned int v=0;
  5250. if (n > (1 << 24)) v += stb_fgetc(f) << 24;
  5251. if (n > (1 << 16)) v += stb_fgetc(f) << 16;
  5252. if (n > (1 << 8)) v += stb_fgetc(f) << 8;
  5253. v += stb_fgetc(f);
  5254. return b+v;
  5255. }
  5256. int stb_size_ranged(int b, stb_uint n)
  5257. {
  5258. if (n > (1 << 24)) return 4;
  5259. if (n > (1 << 16)) return 3;
  5260. if (n > (1 << 8)) return 2;
  5261. return 1;
  5262. }
  5263. void stb_fput_string(FILE *f, char *s)
  5264. {
  5265. size_t len = strlen(s);
  5266. stb_fput_varlenu(f, (unsigned int) len);
  5267. fwrite(s, 1, len, f);
  5268. }
  5269. // inverse of the above algorithm
  5270. char *stb_fget_string(FILE *f, void *p)
  5271. {
  5272. char *s;
  5273. int len = stb_fget_varlenu(f);
  5274. if (len > 4096) return NULL;
  5275. s = p ? stb_malloc_string(p, len+1) : (char *) malloc(len+1);
  5276. fread(s, 1, len, f);
  5277. s[len] = 0;
  5278. return s;
  5279. }
  5280. char *stb_strdup(char *str, void *pool)
  5281. {
  5282. size_t len = strlen(str);
  5283. char *p = stb_malloc_string(pool, len+1);
  5284. stb_p_strcpy_s(p, len+1, str);
  5285. return p;
  5286. }
  5287. // strip the trailing '/' or '\\' from a directory so we can refer to it
  5288. // as a file for _stat()
  5289. char *stb_strip_final_slash(char *t)
  5290. {
  5291. if (t[0]) {
  5292. char *z = t + strlen(t) - 1;
  5293. // *z is the last character
  5294. if (*z == '\\' || *z == '/')
  5295. if (z != t+2 || t[1] != ':') // but don't strip it if it's e.g. "c:/"
  5296. *z = 0;
  5297. if (*z == '\\')
  5298. *z = '/'; // canonicalize to make sure it matches db
  5299. }
  5300. return t;
  5301. }
  5302. char *stb_strip_final_slash_regardless(char *t)
  5303. {
  5304. if (t[0]) {
  5305. char *z = t + strlen(t) - 1;
  5306. // *z is the last character
  5307. if (*z == '\\' || *z == '/')
  5308. *z = 0;
  5309. if (*z == '\\')
  5310. *z = '/'; // canonicalize to make sure it matches db
  5311. }
  5312. return t;
  5313. }
  5314. #endif
  5315. //////////////////////////////////////////////////////////////////////////////
  5316. //
  5317. // Options parsing
  5318. //
  5319. STB_EXTERN char **stb_getopt_param(int *argc, char **argv, char *param);
  5320. STB_EXTERN char **stb_getopt(int *argc, char **argv);
  5321. STB_EXTERN void stb_getopt_free(char **opts);
  5322. #ifdef STB_DEFINE
  5323. void stb_getopt_free(char **opts)
  5324. {
  5325. int i;
  5326. char ** o2 = opts;
  5327. for (i=0; i < stb_arr_len(o2); ++i)
  5328. free(o2[i]);
  5329. stb_arr_free(o2);
  5330. }
  5331. char **stb_getopt(int *argc, char **argv)
  5332. {
  5333. return stb_getopt_param(argc, argv, (char*) "");
  5334. }
  5335. char **stb_getopt_param(int *argc, char **argv, char *param)
  5336. {
  5337. char ** opts=NULL;
  5338. int i,j=1;
  5339. for (i=1; i < *argc; ++i) {
  5340. if (argv[i][0] != '-') {
  5341. argv[j++] = argv[i];
  5342. } else {
  5343. if (argv[i][1] == 0) { // plain - == don't parse further options
  5344. ++i;
  5345. while (i < *argc)
  5346. argv[j++] = argv[i++];
  5347. break;
  5348. } else if (argv[i][1] == '-') {
  5349. // copy argument through including initial '-' for clarity
  5350. stb_arr_push(opts, stb_p_strdup(argv[i]));
  5351. } else {
  5352. int k;
  5353. char *q = argv[i]; // traverse options list
  5354. for (k=1; q[k]; ++k) {
  5355. char *s;
  5356. if (strchr(param, q[k])) { // does it take a parameter?
  5357. char *t = &q[k+1], z = q[k];
  5358. size_t len=0;
  5359. if (*t == 0) {
  5360. if (i == *argc-1) { // takes a parameter, but none found
  5361. *argc = 0;
  5362. stb_getopt_free(opts);
  5363. return NULL;
  5364. }
  5365. t = argv[++i];
  5366. } else
  5367. k += (int) strlen(t);
  5368. len = strlen(t);
  5369. s = (char *) malloc(len+2);
  5370. if (!s) return NULL;
  5371. s[0] = z;
  5372. stb_p_strcpy_s(s+1, len+2, t);
  5373. } else {
  5374. // no parameter
  5375. s = (char *) malloc(2);
  5376. if (!s) return NULL;
  5377. s[0] = q[k];
  5378. s[1] = 0;
  5379. }
  5380. stb_arr_push(opts, s);
  5381. }
  5382. }
  5383. }
  5384. }
  5385. stb_arr_push(opts, NULL);
  5386. *argc = j;
  5387. return opts;
  5388. }
  5389. #endif
  5390. //////////////////////////////////////////////////////////////////////////////
  5391. //
  5392. // Portable directory reading
  5393. //
  5394. STB_EXTERN char **stb_readdir_files (char *dir);
  5395. STB_EXTERN char **stb_readdir_files_mask(char *dir, char *wild);
  5396. STB_EXTERN char **stb_readdir_subdirs(char *dir);
  5397. STB_EXTERN char **stb_readdir_subdirs_mask(char *dir, char *wild);
  5398. STB_EXTERN void stb_readdir_free (char **files);
  5399. STB_EXTERN char **stb_readdir_recursive(char *dir, char *filespec);
  5400. STB_EXTERN void stb_delete_directory_recursive(char *dir);
  5401. #ifdef STB_DEFINE
  5402. #ifdef _MSC_VER
  5403. #include <io.h>
  5404. #else
  5405. #include <unistd.h>
  5406. #include <dirent.h>
  5407. #endif
  5408. void stb_readdir_free(char **files)
  5409. {
  5410. char **f2 = files;
  5411. int i;
  5412. for (i=0; i < stb_arr_len(f2); ++i)
  5413. free(f2[i]);
  5414. stb_arr_free(f2);
  5415. }
  5416. static int isdotdirname(char *name)
  5417. {
  5418. if (name[0] == '.')
  5419. return (name[1] == '.') ? !name[2] : !name[1];
  5420. return 0;
  5421. }
  5422. STB_EXTERN int stb_wildmatchi(char *expr, char *candidate);
  5423. static char **readdir_raw(char *dir, int return_subdirs, char *mask)
  5424. {
  5425. char **results = NULL;
  5426. char buffer[4096], with_slash[4096];
  5427. size_t n;
  5428. #ifdef WIN32
  5429. stb__wchar *ws;
  5430. struct _wfinddata_t data;
  5431. #ifdef _WIN64
  5432. const intptr_t none = -1;
  5433. intptr_t z;
  5434. #else
  5435. const long none = -1;
  5436. long z;
  5437. #endif
  5438. #else // !WIN32
  5439. const DIR *none = NULL;
  5440. DIR *z;
  5441. #endif
  5442. n = stb_strscpy(buffer,dir,sizeof(buffer));
  5443. if (!n || n >= sizeof(buffer))
  5444. return NULL;
  5445. stb_fixpath(buffer);
  5446. if (n > 0 && (buffer[n-1] != '/')) {
  5447. buffer[n++] = '/';
  5448. }
  5449. buffer[n] = 0;
  5450. if (!stb_strscpy(with_slash,buffer,sizeof(with_slash)))
  5451. return NULL;
  5452. #ifdef WIN32
  5453. if (!stb_strscpy(buffer+n,"*.*",sizeof(buffer)-n))
  5454. return NULL;
  5455. ws = stb__from_utf8(buffer);
  5456. z = _wfindfirst((wchar_t *)ws, &data);
  5457. #else
  5458. z = opendir(dir);
  5459. #endif
  5460. if (z != none) {
  5461. int nonempty = STB_TRUE;
  5462. #ifndef WIN32
  5463. struct dirent *data = readdir(z);
  5464. nonempty = (data != NULL);
  5465. #endif
  5466. if (nonempty) {
  5467. do {
  5468. int is_subdir;
  5469. #ifdef WIN32
  5470. char *name = stb__to_utf8((stb__wchar *)data.name);
  5471. if (name == NULL) {
  5472. fprintf(stderr, "%s to convert '%S' to %s!\n", "Unable", data.name, "utf8");
  5473. continue;
  5474. }
  5475. is_subdir = !!(data.attrib & _A_SUBDIR);
  5476. #else
  5477. char *name = data->d_name;
  5478. if (!stb_strscpy(buffer+n,name,sizeof(buffer)-n))
  5479. break;
  5480. // Could follow DT_LNK, but would need to check for recursive links.
  5481. is_subdir = !!(data->d_type & DT_DIR);
  5482. #endif
  5483. if (is_subdir == return_subdirs) {
  5484. if (!is_subdir || !isdotdirname(name)) {
  5485. if (!mask || stb_wildmatchi(mask, name)) {
  5486. char buffer[4096],*p=buffer;
  5487. if ( stb_snprintf(buffer, sizeof(buffer), "%s%s", with_slash, name) < 0 )
  5488. break;
  5489. if (buffer[0] == '.' && buffer[1] == '/')
  5490. p = buffer+2;
  5491. stb_arr_push(results, stb_p_strdup(p));
  5492. }
  5493. }
  5494. }
  5495. }
  5496. #ifdef WIN32
  5497. while (0 == _wfindnext(z, &data));
  5498. #else
  5499. while ((data = readdir(z)) != NULL);
  5500. #endif
  5501. }
  5502. #ifdef WIN32
  5503. _findclose(z);
  5504. #else
  5505. closedir(z);
  5506. #endif
  5507. }
  5508. return results;
  5509. }
  5510. char **stb_readdir_files (char *dir) { return readdir_raw(dir, 0, NULL); }
  5511. char **stb_readdir_subdirs(char *dir) { return readdir_raw(dir, 1, NULL); }
  5512. char **stb_readdir_files_mask(char *dir, char *wild) { return readdir_raw(dir, 0, wild); }
  5513. char **stb_readdir_subdirs_mask(char *dir, char *wild) { return readdir_raw(dir, 1, wild); }
  5514. int stb__rec_max=0x7fffffff;
  5515. static char **stb_readdir_rec(char **sofar, char *dir, char *filespec)
  5516. {
  5517. char **files;
  5518. char ** dirs;
  5519. char **p;
  5520. if (stb_arr_len(sofar) >= stb__rec_max) return sofar;
  5521. files = stb_readdir_files_mask(dir, filespec);
  5522. stb_arr_for(p, files) {
  5523. stb_arr_push(sofar, stb_p_strdup(*p));
  5524. if (stb_arr_len(sofar) >= stb__rec_max) break;
  5525. }
  5526. stb_readdir_free(files);
  5527. if (stb_arr_len(sofar) >= stb__rec_max) return sofar;
  5528. dirs = stb_readdir_subdirs(dir);
  5529. stb_arr_for(p, dirs)
  5530. sofar = stb_readdir_rec(sofar, *p, filespec);
  5531. stb_readdir_free(dirs);
  5532. return sofar;
  5533. }
  5534. char **stb_readdir_recursive(char *dir, char *filespec)
  5535. {
  5536. return stb_readdir_rec(NULL, dir, filespec);
  5537. }
  5538. void stb_delete_directory_recursive(char *dir)
  5539. {
  5540. char **list = stb_readdir_subdirs(dir);
  5541. int i;
  5542. for (i=0; i < stb_arr_len(list); ++i)
  5543. stb_delete_directory_recursive(list[i]);
  5544. stb_arr_free(list);
  5545. list = stb_readdir_files(dir);
  5546. for (i=0; i < stb_arr_len(list); ++i)
  5547. if (!remove(list[i])) {
  5548. // on windows, try again after making it writeable; don't ALWAYS
  5549. // do this first since that would be slow in the normal case
  5550. #ifdef _MSC_VER
  5551. _chmod(list[i], _S_IWRITE);
  5552. remove(list[i]);
  5553. #endif
  5554. }
  5555. stb_arr_free(list);
  5556. stb__windows(_rmdir,rmdir)(dir);
  5557. }
  5558. #endif
  5559. //////////////////////////////////////////////////////////////////////////////
  5560. //
  5561. // construct trees from filenames; useful for cmirror summaries
  5562. typedef struct stb_dirtree2 stb_dirtree2;
  5563. struct stb_dirtree2
  5564. {
  5565. stb_dirtree2 **subdirs;
  5566. // make convenient for stb_summarize_tree
  5567. int num_subdir;
  5568. float weight;
  5569. // actual data
  5570. char *fullpath;
  5571. char *relpath;
  5572. char **files;
  5573. };
  5574. STB_EXTERN stb_dirtree2 *stb_dirtree2_from_files_relative(char *src, char **filelist, int count);
  5575. STB_EXTERN stb_dirtree2 *stb_dirtree2_from_files(char **filelist, int count);
  5576. STB_EXTERN int stb_dir_is_prefix(char *dir, int dirlen, char *file);
  5577. #ifdef STB_DEFINE
  5578. int stb_dir_is_prefix(char *dir, int dirlen, char *file)
  5579. {
  5580. if (dirlen == 0) return STB_TRUE;
  5581. if (stb_strnicmp(dir, file, dirlen)) return STB_FALSE;
  5582. if (file[dirlen] == '/' || file[dirlen] == '\\') return STB_TRUE;
  5583. return STB_FALSE;
  5584. }
  5585. stb_dirtree2 *stb_dirtree2_from_files_relative(char *src, char **filelist, int count)
  5586. {
  5587. char buffer1[1024];
  5588. int i;
  5589. int dlen = (int) strlen(src), elen;
  5590. stb_dirtree2 *d;
  5591. char ** descendents = NULL;
  5592. char ** files = NULL;
  5593. char *s;
  5594. if (!count) return NULL;
  5595. // first find all the ones that belong here... note this is will take O(NM) with N files and M subdirs
  5596. for (i=0; i < count; ++i) {
  5597. if (stb_dir_is_prefix(src, dlen, filelist[i])) {
  5598. stb_arr_push(descendents, filelist[i]);
  5599. }
  5600. }
  5601. if (descendents == NULL)
  5602. return NULL;
  5603. elen = dlen;
  5604. // skip a leading slash
  5605. if (elen == 0 && (descendents[0][0] == '/' || descendents[0][0] == '\\'))
  5606. ++elen;
  5607. else if (elen)
  5608. ++elen;
  5609. // now extract all the ones that have their root here
  5610. for (i=0; i < stb_arr_len(descendents);) {
  5611. if (!stb_strchr2(descendents[i]+elen, '/', '\\')) {
  5612. stb_arr_push(files, descendents[i]);
  5613. descendents[i] = descendents[stb_arr_len(descendents)-1];
  5614. stb_arr_pop(descendents);
  5615. } else
  5616. ++i;
  5617. }
  5618. // now create a record
  5619. d = (stb_dirtree2 *) malloc(sizeof(*d));
  5620. d->files = files;
  5621. d->subdirs = NULL;
  5622. d->fullpath = stb_p_strdup(src);
  5623. s = stb_strrchr2(d->fullpath, '/', '\\');
  5624. if (s)
  5625. ++s;
  5626. else
  5627. s = d->fullpath;
  5628. d->relpath = s;
  5629. // now create the children
  5630. qsort(descendents, stb_arr_len(descendents), sizeof(char *), stb_qsort_stricmp(0));
  5631. buffer1[0] = 0;
  5632. for (i=0; i < stb_arr_len(descendents); ++i) {
  5633. char buffer2[1024];
  5634. char *s = descendents[i] + elen, *t;
  5635. t = stb_strchr2(s, '/', '\\');
  5636. assert(t);
  5637. stb_strncpy(buffer2, descendents[i], (int) (t-descendents[i]+1));
  5638. if (stb_stricmp(buffer1, buffer2)) {
  5639. stb_dirtree2 *t = stb_dirtree2_from_files_relative(buffer2, descendents, stb_arr_len(descendents));
  5640. assert(t != NULL);
  5641. stb_p_strcpy_s(buffer1, sizeof(buffer1), buffer2);
  5642. stb_arr_push(d->subdirs, t);
  5643. }
  5644. }
  5645. d->num_subdir = stb_arr_len(d->subdirs);
  5646. d->weight = 0;
  5647. return d;
  5648. }
  5649. stb_dirtree2 *stb_dirtree2_from_files(char **filelist, int count)
  5650. {
  5651. return stb_dirtree2_from_files_relative((char*) "", filelist, count);
  5652. }
  5653. #endif
  5654. //////////////////////////////////////////////////////////////////////////////
  5655. //
  5656. // Checksums: CRC-32, ADLER32, SHA-1
  5657. //
  5658. // CRC-32 and ADLER32 allow streaming blocks
  5659. // SHA-1 requires either a complete buffer, max size 2^32 - 73
  5660. // or it can checksum directly from a file, max 2^61
  5661. #define STB_ADLER32_SEED 1
  5662. #define STB_CRC32_SEED 0 // note that we logical NOT this in the code
  5663. STB_EXTERN stb_uint
  5664. stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen);
  5665. STB_EXTERN stb_uint
  5666. stb_crc32_block(stb_uint crc32, stb_uchar *buffer, stb_uint len);
  5667. STB_EXTERN stb_uint stb_crc32(unsigned char *buffer, stb_uint len);
  5668. STB_EXTERN void stb_sha1(
  5669. unsigned char output[20], unsigned char *buffer, unsigned int len);
  5670. STB_EXTERN int stb_sha1_file(unsigned char output[20], char *file);
  5671. STB_EXTERN void stb_sha1_readable(char display[27], unsigned char sha[20]);
  5672. #ifdef STB_DEFINE
  5673. stb_uint stb_crc32_block(stb_uint crc, unsigned char *buffer, stb_uint len)
  5674. {
  5675. static stb_uint crc_table[256];
  5676. stb_uint i,j,s;
  5677. crc = ~crc;
  5678. if (crc_table[1] == 0)
  5679. for(i=0; i < 256; i++) {
  5680. for (s=i, j=0; j < 8; ++j)
  5681. s = (s >> 1) ^ (s & 1 ? 0xedb88320 : 0);
  5682. crc_table[i] = s;
  5683. }
  5684. for (i=0; i < len; ++i)
  5685. crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
  5686. return ~crc;
  5687. }
  5688. stb_uint stb_crc32(unsigned char *buffer, stb_uint len)
  5689. {
  5690. return stb_crc32_block(0, buffer, len);
  5691. }
  5692. stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen)
  5693. {
  5694. const unsigned long ADLER_MOD = 65521;
  5695. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  5696. unsigned long blocklen, i;
  5697. blocklen = buflen % 5552;
  5698. while (buflen) {
  5699. for (i=0; i + 7 < blocklen; i += 8) {
  5700. s1 += buffer[0], s2 += s1;
  5701. s1 += buffer[1], s2 += s1;
  5702. s1 += buffer[2], s2 += s1;
  5703. s1 += buffer[3], s2 += s1;
  5704. s1 += buffer[4], s2 += s1;
  5705. s1 += buffer[5], s2 += s1;
  5706. s1 += buffer[6], s2 += s1;
  5707. s1 += buffer[7], s2 += s1;
  5708. buffer += 8;
  5709. }
  5710. for (; i < blocklen; ++i)
  5711. s1 += *buffer++, s2 += s1;
  5712. s1 %= ADLER_MOD, s2 %= ADLER_MOD;
  5713. buflen -= blocklen;
  5714. blocklen = 5552;
  5715. }
  5716. return (s2 << 16) + s1;
  5717. }
  5718. static void stb__sha1(stb_uchar *chunk, stb_uint h[5])
  5719. {
  5720. int i;
  5721. stb_uint a,b,c,d,e;
  5722. stb_uint w[80];
  5723. for (i=0; i < 16; ++i)
  5724. w[i] = stb_big32(&chunk[i*4]);
  5725. for (i=16; i < 80; ++i) {
  5726. stb_uint t;
  5727. t = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
  5728. w[i] = (t + t) | (t >> 31);
  5729. }
  5730. a = h[0];
  5731. b = h[1];
  5732. c = h[2];
  5733. d = h[3];
  5734. e = h[4];
  5735. #define STB__SHA1(k,f) \
  5736. { \
  5737. stb_uint temp = (a << 5) + (a >> 27) + (f) + e + (k) + w[i]; \
  5738. e = d; \
  5739. d = c; \
  5740. c = (b << 30) + (b >> 2); \
  5741. b = a; \
  5742. a = temp; \
  5743. }
  5744. i=0;
  5745. for (; i < 20; ++i) STB__SHA1(0x5a827999, d ^ (b & (c ^ d)) );
  5746. for (; i < 40; ++i) STB__SHA1(0x6ed9eba1, b ^ c ^ d );
  5747. for (; i < 60; ++i) STB__SHA1(0x8f1bbcdc, (b & c) + (d & (b ^ c)) );
  5748. for (; i < 80; ++i) STB__SHA1(0xca62c1d6, b ^ c ^ d );
  5749. #undef STB__SHA1
  5750. h[0] += a;
  5751. h[1] += b;
  5752. h[2] += c;
  5753. h[3] += d;
  5754. h[4] += e;
  5755. }
  5756. void stb_sha1(stb_uchar output[20], stb_uchar *buffer, stb_uint len)
  5757. {
  5758. unsigned char final_block[128];
  5759. stb_uint end_start, final_len, j;
  5760. int i;
  5761. stb_uint h[5];
  5762. h[0] = 0x67452301;
  5763. h[1] = 0xefcdab89;
  5764. h[2] = 0x98badcfe;
  5765. h[3] = 0x10325476;
  5766. h[4] = 0xc3d2e1f0;
  5767. // we need to write padding to the last one or two
  5768. // blocks, so build those first into 'final_block'
  5769. // we have to write one special byte, plus the 8-byte length
  5770. // compute the block where the data runs out
  5771. end_start = len & ~63;
  5772. // compute the earliest we can encode the length
  5773. if (((len+9) & ~63) == end_start) {
  5774. // it all fits in one block, so fill a second-to-last block
  5775. end_start -= 64;
  5776. }
  5777. final_len = end_start + 128;
  5778. // now we need to copy the data in
  5779. assert(end_start + 128 >= len+9);
  5780. assert(end_start < len || len < 64-9);
  5781. j = 0;
  5782. if (end_start > len)
  5783. j = (stb_uint) - (int) end_start;
  5784. for (; end_start + j < len; ++j)
  5785. final_block[j] = buffer[end_start + j];
  5786. final_block[j++] = 0x80;
  5787. while (j < 128-5) // 5 byte length, so write 4 extra padding bytes
  5788. final_block[j++] = 0;
  5789. // big-endian size
  5790. final_block[j++] = len >> 29;
  5791. final_block[j++] = len >> 21;
  5792. final_block[j++] = len >> 13;
  5793. final_block[j++] = len >> 5;
  5794. final_block[j++] = len << 3;
  5795. assert(j == 128 && end_start + j == final_len);
  5796. for (j=0; j < final_len; j += 64) { // 512-bit chunks
  5797. if (j+64 >= end_start+64)
  5798. stb__sha1(&final_block[j - end_start], h);
  5799. else
  5800. stb__sha1(&buffer[j], h);
  5801. }
  5802. for (i=0; i < 5; ++i) {
  5803. output[i*4 + 0] = h[i] >> 24;
  5804. output[i*4 + 1] = h[i] >> 16;
  5805. output[i*4 + 2] = h[i] >> 8;
  5806. output[i*4 + 3] = h[i] >> 0;
  5807. }
  5808. }
  5809. #ifdef _MSC_VER
  5810. int stb_sha1_file(stb_uchar output[20], char *file)
  5811. {
  5812. int i;
  5813. stb_uint64 length=0;
  5814. unsigned char buffer[128];
  5815. FILE *f = stb__fopen(file, "rb");
  5816. stb_uint h[5];
  5817. if (f == NULL) return 0; // file not found
  5818. h[0] = 0x67452301;
  5819. h[1] = 0xefcdab89;
  5820. h[2] = 0x98badcfe;
  5821. h[3] = 0x10325476;
  5822. h[4] = 0xc3d2e1f0;
  5823. for(;;) {
  5824. size_t n = fread(buffer, 1, 64, f);
  5825. if (n == 64) {
  5826. stb__sha1(buffer, h);
  5827. length += n;
  5828. } else {
  5829. int block = 64;
  5830. length += n;
  5831. buffer[n++] = 0x80;
  5832. // if there isn't enough room for the length, double the block
  5833. if (n + 8 > 64)
  5834. block = 128;
  5835. // pad to end
  5836. memset(buffer+n, 0, block-8-n);
  5837. i = block - 8;
  5838. buffer[i++] = (stb_uchar) (length >> 53);
  5839. buffer[i++] = (stb_uchar) (length >> 45);
  5840. buffer[i++] = (stb_uchar) (length >> 37);
  5841. buffer[i++] = (stb_uchar) (length >> 29);
  5842. buffer[i++] = (stb_uchar) (length >> 21);
  5843. buffer[i++] = (stb_uchar) (length >> 13);
  5844. buffer[i++] = (stb_uchar) (length >> 5);
  5845. buffer[i++] = (stb_uchar) (length << 3);
  5846. assert(i == block);
  5847. stb__sha1(buffer, h);
  5848. if (block == 128)
  5849. stb__sha1(buffer+64, h);
  5850. else
  5851. assert(block == 64);
  5852. break;
  5853. }
  5854. }
  5855. fclose(f);
  5856. for (i=0; i < 5; ++i) {
  5857. output[i*4 + 0] = h[i] >> 24;
  5858. output[i*4 + 1] = h[i] >> 16;
  5859. output[i*4 + 2] = h[i] >> 8;
  5860. output[i*4 + 3] = h[i] >> 0;
  5861. }
  5862. return 1;
  5863. }
  5864. #endif // _MSC_VER
  5865. // client can truncate this wherever they like
  5866. void stb_sha1_readable(char display[27], unsigned char sha[20])
  5867. {
  5868. char encoding[65] = "0123456789abcdefghijklmnopqrstuv"
  5869. "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%$";
  5870. int num_bits = 0, acc=0;
  5871. int i=0,o=0;
  5872. while (o < 26) {
  5873. int v;
  5874. // expand the accumulator
  5875. if (num_bits < 6) {
  5876. assert(i != 20);
  5877. acc += sha[i++] << num_bits;
  5878. num_bits += 8;
  5879. }
  5880. v = acc & ((1 << 6) - 1);
  5881. display[o++] = encoding[v];
  5882. acc >>= 6;
  5883. num_bits -= 6;
  5884. }
  5885. assert(num_bits == 20*8 - 26*6);
  5886. display[o++] = encoding[acc];
  5887. }
  5888. #endif // STB_DEFINE
  5889. ///////////////////////////////////////////////////////////
  5890. //
  5891. // simplified WINDOWS registry interface... hopefully
  5892. // we'll never actually use this?
  5893. #if defined(_WIN32)
  5894. STB_EXTERN void * stb_reg_open(const char *mode, const char *where); // mode: "rHKLM" or "rHKCU" or "w.."
  5895. STB_EXTERN void stb_reg_close(void *reg);
  5896. STB_EXTERN int stb_reg_read(void *zreg, const char *str, void *data, unsigned long len);
  5897. STB_EXTERN int stb_reg_read_string(void *zreg, const char *str, char *data, int len);
  5898. STB_EXTERN void stb_reg_write(void *zreg, const char *str, const void *data, unsigned long len);
  5899. STB_EXTERN void stb_reg_write_string(void *zreg, const char *str, const char *data);
  5900. #if defined(STB_DEFINE) && !defined(STB_NO_REGISTRY)
  5901. #define STB_HAS_REGISTRY
  5902. #ifndef _WINDOWS_
  5903. #define HKEY void *
  5904. STB_EXTERN __declspec(dllimport) long __stdcall RegCloseKey ( HKEY hKey );
  5905. STB_EXTERN __declspec(dllimport) long __stdcall RegCreateKeyExA ( HKEY hKey, const char * lpSubKey,
  5906. int Reserved, char * lpClass, int dwOptions,
  5907. int samDesired, void *lpSecurityAttributes, HKEY * phkResult, int * lpdwDisposition );
  5908. STB_EXTERN __declspec(dllimport) long __stdcall RegDeleteKeyA ( HKEY hKey, const char * lpSubKey );
  5909. STB_EXTERN __declspec(dllimport) long __stdcall RegQueryValueExA ( HKEY hKey, const char * lpValueName,
  5910. int * lpReserved, unsigned long * lpType, unsigned char * lpData, unsigned long * lpcbData );
  5911. STB_EXTERN __declspec(dllimport) long __stdcall RegSetValueExA ( HKEY hKey, const char * lpValueName,
  5912. int Reserved, int dwType, const unsigned char* lpData, int cbData );
  5913. STB_EXTERN __declspec(dllimport) long __stdcall RegOpenKeyExA ( HKEY hKey, const char * lpSubKey,
  5914. int ulOptions, int samDesired, HKEY * phkResult );
  5915. #endif // _WINDOWS_
  5916. #define STB__REG_OPTION_NON_VOLATILE 0
  5917. #define STB__REG_KEY_ALL_ACCESS 0x000f003f
  5918. #define STB__REG_KEY_READ 0x00020019
  5919. #ifdef _M_AMD64
  5920. #define STB__HKEY_CURRENT_USER 0x80000001ull
  5921. #define STB__HKEY_LOCAL_MACHINE 0x80000002ull
  5922. #else
  5923. #define STB__HKEY_CURRENT_USER 0x80000001
  5924. #define STB__HKEY_LOCAL_MACHINE 0x80000002
  5925. #endif
  5926. void *stb_reg_open(const char *mode, const char *where)
  5927. {
  5928. long res;
  5929. HKEY base;
  5930. HKEY zreg;
  5931. if (!stb_stricmp(mode+1, "cu") || !stb_stricmp(mode+1, "hkcu"))
  5932. base = (HKEY) STB__HKEY_CURRENT_USER;
  5933. else if (!stb_stricmp(mode+1, "lm") || !stb_stricmp(mode+1, "hklm"))
  5934. base = (HKEY) STB__HKEY_LOCAL_MACHINE;
  5935. else
  5936. return NULL;
  5937. if (mode[0] == 'r')
  5938. res = RegOpenKeyExA(base, where, 0, STB__REG_KEY_READ, &zreg);
  5939. else if (mode[0] == 'w')
  5940. res = RegCreateKeyExA(base, where, 0, NULL, STB__REG_OPTION_NON_VOLATILE, STB__REG_KEY_ALL_ACCESS, NULL, &zreg, NULL);
  5941. else
  5942. return NULL;
  5943. return res ? NULL : zreg;
  5944. }
  5945. void stb_reg_close(void *reg)
  5946. {
  5947. RegCloseKey((HKEY) reg);
  5948. }
  5949. #define STB__REG_SZ 1
  5950. #define STB__REG_BINARY 3
  5951. #define STB__REG_DWORD 4
  5952. int stb_reg_read(void *zreg, const char *str, void *data, unsigned long len)
  5953. {
  5954. unsigned long type;
  5955. unsigned long alen = len;
  5956. if (0 == RegQueryValueExA((HKEY) zreg, str, 0, &type, (unsigned char *) data, &len))
  5957. if (type == STB__REG_BINARY || type == STB__REG_SZ || type == STB__REG_DWORD) {
  5958. if (len < alen)
  5959. *((char *) data + len) = 0;
  5960. return 1;
  5961. }
  5962. return 0;
  5963. }
  5964. void stb_reg_write(void *zreg, const char *str, const void *data, unsigned long len)
  5965. {
  5966. if (zreg)
  5967. RegSetValueExA((HKEY) zreg, str, 0, STB__REG_BINARY, (const unsigned char *) data, len);
  5968. }
  5969. int stb_reg_read_string(void *zreg, const char *str, char *data, int len)
  5970. {
  5971. if (!stb_reg_read(zreg, str, data, len)) return 0;
  5972. data[len-1] = 0; // force a 0 at the end of the string no matter what
  5973. return 1;
  5974. }
  5975. void stb_reg_write_string(void *zreg, const char *str, const char *data)
  5976. {
  5977. if (zreg)
  5978. RegSetValueExA((HKEY) zreg, str, 0, STB__REG_SZ, (const unsigned char *) data, (int) strlen(data)+1);
  5979. }
  5980. #endif // STB_DEFINE
  5981. #endif // _WIN32
  5982. //////////////////////////////////////////////////////////////////////////////
  5983. //
  5984. // stb_cfg - This is like the registry, but the config info
  5985. // is all stored in plain old files where we can
  5986. // backup and restore them easily. The LOCATION of
  5987. // the config files is gotten from... the registry!
  5988. #ifndef STB_NO_STB_STRINGS
  5989. typedef struct stb_cfg_st stb_cfg;
  5990. STB_EXTERN stb_cfg * stb_cfg_open(char *config, const char *mode); // mode = "r", "w"
  5991. STB_EXTERN void stb_cfg_close(stb_cfg *cfg);
  5992. STB_EXTERN int stb_cfg_read(stb_cfg *cfg, char *key, void *value, int len);
  5993. STB_EXTERN void stb_cfg_write(stb_cfg *cfg, char *key, void *value, int len);
  5994. STB_EXTERN int stb_cfg_read_string(stb_cfg *cfg, char *key, char *value, int len);
  5995. STB_EXTERN void stb_cfg_write_string(stb_cfg *cfg, char *key, char *value);
  5996. STB_EXTERN int stb_cfg_delete(stb_cfg *cfg, char *key);
  5997. STB_EXTERN void stb_cfg_set_directory(char *dir);
  5998. #ifdef STB_DEFINE
  5999. typedef struct
  6000. {
  6001. char *key;
  6002. void *value;
  6003. int value_len;
  6004. } stb__cfg_item;
  6005. struct stb_cfg_st
  6006. {
  6007. stb__cfg_item *data;
  6008. char *loaded_file; // this needs to be freed
  6009. FILE *f; // write the data to this file on close
  6010. };
  6011. static const char *stb__cfg_sig = "sTbCoNfIg!\0\0";
  6012. static char stb__cfg_dir[512];
  6013. STB_EXTERN void stb_cfg_set_directory(char *dir)
  6014. {
  6015. stb_p_strcpy_s(stb__cfg_dir, sizeof(stb__cfg_dir), dir);
  6016. }
  6017. STB_EXTERN stb_cfg * stb_cfg_open(char *config, const char *mode)
  6018. {
  6019. size_t len;
  6020. stb_cfg *z;
  6021. char file[512];
  6022. if (mode[0] != 'r' && mode[0] != 'w') return NULL;
  6023. if (!stb__cfg_dir[0]) {
  6024. #ifdef _WIN32
  6025. stb_p_strcpy_s(stb__cfg_dir, sizeof(stb__cfg_dir), "c:/stb");
  6026. #else
  6027. strcpy(stb__cfg_dir, "~/.stbconfig");
  6028. #endif
  6029. #ifdef STB_HAS_REGISTRY
  6030. {
  6031. void *reg = stb_reg_open("rHKLM", "Software\\SilverSpaceship\\stb");
  6032. if (reg) {
  6033. stb_reg_read_string(reg, "config_dir", stb__cfg_dir, sizeof(stb__cfg_dir));
  6034. stb_reg_close(reg);
  6035. }
  6036. }
  6037. #endif
  6038. }
  6039. stb_p_sprintf(file stb_p_size(sizeof(file)), "%s/%s.cfg", stb__cfg_dir, config);
  6040. z = (stb_cfg *) stb_malloc(0, sizeof(*z));
  6041. z->data = NULL;
  6042. z->loaded_file = stb_filec(file, &len);
  6043. if (z->loaded_file) {
  6044. char *s = z->loaded_file;
  6045. if (!memcmp(s, stb__cfg_sig, 12)) {
  6046. char *s = z->loaded_file + 12;
  6047. while (s < z->loaded_file + len) {
  6048. stb__cfg_item a;
  6049. int n = *(stb_int16 *) s;
  6050. a.key = s+2;
  6051. s = s+2 + n;
  6052. a.value_len = *(int *) s;
  6053. s += 4;
  6054. a.value = s;
  6055. s += a.value_len;
  6056. stb_arr_push(z->data, a);
  6057. }
  6058. assert(s == z->loaded_file + len);
  6059. }
  6060. }
  6061. if (mode[0] == 'w')
  6062. z->f = stb_p_fopen(file, "wb");
  6063. else
  6064. z->f = NULL;
  6065. return z;
  6066. }
  6067. void stb_cfg_close(stb_cfg *z)
  6068. {
  6069. if (z->f) {
  6070. int i;
  6071. // write the file out
  6072. fwrite(stb__cfg_sig, 12, 1, z->f);
  6073. for (i=0; i < stb_arr_len(z->data); ++i) {
  6074. stb_int16 n = (stb_int16) strlen(z->data[i].key)+1;
  6075. fwrite(&n, 2, 1, z->f);
  6076. fwrite(z->data[i].key, n, 1, z->f);
  6077. fwrite(&z->data[i].value_len, 4, 1, z->f);
  6078. fwrite(z->data[i].value, z->data[i].value_len, 1, z->f);
  6079. }
  6080. fclose(z->f);
  6081. }
  6082. stb_arr_free(z->data);
  6083. stb_free(z);
  6084. }
  6085. int stb_cfg_read(stb_cfg *z, char *key, void *value, int len)
  6086. {
  6087. int i;
  6088. for (i=0; i < stb_arr_len(z->data); ++i) {
  6089. if (!stb_stricmp(z->data[i].key, key)) {
  6090. int n = stb_min(len, z->data[i].value_len);
  6091. memcpy(value, z->data[i].value, n);
  6092. if (n < len)
  6093. *((char *) value + n) = 0;
  6094. return 1;
  6095. }
  6096. }
  6097. return 0;
  6098. }
  6099. void stb_cfg_write(stb_cfg *z, char *key, void *value, int len)
  6100. {
  6101. int i;
  6102. for (i=0; i < stb_arr_len(z->data); ++i)
  6103. if (!stb_stricmp(z->data[i].key, key))
  6104. break;
  6105. if (i == stb_arr_len(z->data)) {
  6106. stb__cfg_item p;
  6107. p.key = stb_strdup(key, z);
  6108. p.value = NULL;
  6109. p.value_len = 0;
  6110. stb_arr_push(z->data, p);
  6111. }
  6112. z->data[i].value = stb_malloc(z, len);
  6113. z->data[i].value_len = len;
  6114. memcpy(z->data[i].value, value, len);
  6115. }
  6116. int stb_cfg_delete(stb_cfg *z, char *key)
  6117. {
  6118. int i;
  6119. for (i=0; i < stb_arr_len(z->data); ++i)
  6120. if (!stb_stricmp(z->data[i].key, key)) {
  6121. stb_arr_fastdelete(z->data, i);
  6122. return 1;
  6123. }
  6124. return 0;
  6125. }
  6126. int stb_cfg_read_string(stb_cfg *z, char *key, char *value, int len)
  6127. {
  6128. if (!stb_cfg_read(z, key, value, len)) return 0;
  6129. value[len-1] = 0;
  6130. return 1;
  6131. }
  6132. void stb_cfg_write_string(stb_cfg *z, char *key, char *value)
  6133. {
  6134. stb_cfg_write(z, key, value, (int) strlen(value)+1);
  6135. }
  6136. #endif
  6137. //////////////////////////////////////////////////////////////////////////////
  6138. //
  6139. // stb_dirtree - load a description of a directory tree
  6140. // uses a cache and stat()s the directories for changes
  6141. // MUCH faster on NTFS, _wrong_ on FAT32, so should
  6142. // ignore the db on FAT32
  6143. #ifdef _WIN32
  6144. typedef struct
  6145. {
  6146. char * path; // full path from passed-in root
  6147. time_t last_modified;
  6148. int num_files;
  6149. int flag;
  6150. } stb_dirtree_dir;
  6151. typedef struct
  6152. {
  6153. char *name; // name relative to path
  6154. int dir; // index into dirs[] array
  6155. stb_int64 size; // size, max 4GB
  6156. time_t last_modified;
  6157. int flag;
  6158. } stb_dirtree_file;
  6159. typedef struct
  6160. {
  6161. stb_dirtree_dir *dirs;
  6162. stb_dirtree_file *files;
  6163. // internal use
  6164. void * string_pool; // used to free data en masse
  6165. } stb_dirtree;
  6166. extern void stb_dirtree_free ( stb_dirtree *d );
  6167. extern stb_dirtree *stb_dirtree_get ( char *dir);
  6168. extern stb_dirtree *stb_dirtree_get_dir ( char *dir, char *cache_dir);
  6169. extern stb_dirtree *stb_dirtree_get_with_file ( char *dir, char *cache_file);
  6170. // get a list of all the files recursively underneath 'dir'
  6171. //
  6172. // cache_file is used to store a copy of the directory tree to speed up
  6173. // later calls. It must be unique to 'dir' and the current working
  6174. // directory! Otherwise who knows what will happen (a good solution
  6175. // is to put it _in_ dir, but this API doesn't force that).
  6176. //
  6177. // Also, it might be possible to break this if you have two different processes
  6178. // do a call to stb_dirtree_get() with the same cache file at about the same
  6179. // time, but I _think_ it might just work.
  6180. // i needed to build an identical data structure representing the state of
  6181. // a mirrored copy WITHOUT bothering to rescan it (i.e. we're mirroring to
  6182. // it WITHOUT scanning it, e.g. it's over the net), so this requires access
  6183. // to all of the innards.
  6184. extern void stb_dirtree_db_add_dir(stb_dirtree *active, char *path, time_t last);
  6185. extern void stb_dirtree_db_add_file(stb_dirtree *active, char *name, int dir, stb_int64 size, time_t last);
  6186. extern void stb_dirtree_db_read(stb_dirtree *target, char *filename, char *dir);
  6187. extern void stb_dirtree_db_write(stb_dirtree *target, char *filename, char *dir);
  6188. #ifdef STB_DEFINE
  6189. static void stb__dirtree_add_dir(char *path, time_t last, stb_dirtree *active)
  6190. {
  6191. stb_dirtree_dir d;
  6192. d.last_modified = last;
  6193. d.num_files = 0;
  6194. d.path = stb_strdup(path, active->string_pool);
  6195. stb_arr_push(active->dirs, d);
  6196. }
  6197. static void stb__dirtree_add_file(char *name, int dir, stb_int64 size, time_t last, stb_dirtree *active)
  6198. {
  6199. stb_dirtree_file f;
  6200. f.dir = dir;
  6201. f.size = size;
  6202. f.last_modified = last;
  6203. f.name = stb_strdup(name, active->string_pool);
  6204. ++active->dirs[dir].num_files;
  6205. stb_arr_push(active->files, f);
  6206. }
  6207. // version 02 supports > 4GB files
  6208. static char stb__signature[12] = { 's', 'T', 'b', 'D', 'i', 'R', 't', 'R', 'e', 'E', '0', '2' };
  6209. static void stb__dirtree_save_db(char *filename, stb_dirtree *data, char *root)
  6210. {
  6211. int i, num_dirs_final=0, num_files_final;
  6212. char *info = root ? root : (char*)"";
  6213. int *remap;
  6214. FILE *f = stb_p_fopen(filename, "wb");
  6215. if (!f) return;
  6216. fwrite(stb__signature, sizeof(stb__signature), 1, f);
  6217. fwrite(info, strlen(info)+1, 1, f);
  6218. // need to be slightly tricky and not write out NULLed directories, nor the root
  6219. // build remapping table of all dirs we'll be writing out
  6220. remap = (int *) malloc(sizeof(remap[0]) * stb_arr_len(data->dirs));
  6221. for (i=0; i < stb_arr_len(data->dirs); ++i) {
  6222. if (data->dirs[i].path == NULL || (root && 0==stb_stricmp(data->dirs[i].path, root))) {
  6223. remap[i] = -1;
  6224. } else {
  6225. remap[i] = num_dirs_final++;
  6226. }
  6227. }
  6228. fwrite(&num_dirs_final, 4, 1, f);
  6229. for (i=0; i < stb_arr_len(data->dirs); ++i) {
  6230. if (remap[i] >= 0) {
  6231. fwrite(&data->dirs[i].last_modified, 4, 1, f);
  6232. stb_fput_string(f, data->dirs[i].path);
  6233. }
  6234. }
  6235. num_files_final = 0;
  6236. for (i=0; i < stb_arr_len(data->files); ++i)
  6237. if (remap[data->files[i].dir] >= 0 && data->files[i].name)
  6238. ++num_files_final;
  6239. fwrite(&num_files_final, 4, 1, f);
  6240. for (i=0; i < stb_arr_len(data->files); ++i) {
  6241. if (remap[data->files[i].dir] >= 0 && data->files[i].name) {
  6242. stb_fput_ranged(f, remap[data->files[i].dir], 0, num_dirs_final);
  6243. stb_fput_varlen64(f, data->files[i].size);
  6244. fwrite(&data->files[i].last_modified, 4, 1, f);
  6245. stb_fput_string(f, data->files[i].name);
  6246. }
  6247. }
  6248. fclose(f);
  6249. }
  6250. // note: stomps any existing data, rather than appending
  6251. static void stb__dirtree_load_db(char *filename, stb_dirtree *data, char *dir)
  6252. {
  6253. char sig[2048];
  6254. int i,n;
  6255. FILE *f = stb_p_fopen(filename, "rb");
  6256. if (!f) return;
  6257. data->string_pool = stb_malloc(0,1);
  6258. fread(sig, sizeof(stb__signature), 1, f);
  6259. if (memcmp(stb__signature, sig, sizeof(stb__signature))) { fclose(f); return; }
  6260. if (!fread(sig, strlen(dir)+1, 1, f)) { fclose(f); return; }
  6261. if (stb_stricmp(sig,dir)) { fclose(f); return; }
  6262. // we can just read them straight in, because they're guaranteed to be valid
  6263. fread(&n, 4, 1, f);
  6264. stb_arr_setlen(data->dirs, n);
  6265. for(i=0; i < stb_arr_len(data->dirs); ++i) {
  6266. fread(&data->dirs[i].last_modified, 4, 1, f);
  6267. data->dirs[i].path = stb_fget_string(f, data->string_pool);
  6268. if (data->dirs[i].path == NULL) goto bail;
  6269. }
  6270. fread(&n, 4, 1, f);
  6271. stb_arr_setlen(data->files, n);
  6272. for (i=0; i < stb_arr_len(data->files); ++i) {
  6273. data->files[i].dir = stb_fget_ranged(f, 0, stb_arr_len(data->dirs));
  6274. data->files[i].size = stb_fget_varlen64(f);
  6275. fread(&data->files[i].last_modified, 4, 1, f);
  6276. data->files[i].name = stb_fget_string(f, data->string_pool);
  6277. if (data->files[i].name == NULL) goto bail;
  6278. }
  6279. if (0) {
  6280. bail:
  6281. stb_arr_free(data->dirs);
  6282. stb_arr_free(data->files);
  6283. }
  6284. fclose(f);
  6285. }
  6286. FILE *hlog;
  6287. static int stb__dircount, stb__dircount_mask, stb__showfile;
  6288. static void stb__dirtree_scandir(char *path, time_t last_time, stb_dirtree *active)
  6289. {
  6290. // this is dumb depth first; theoretically it might be faster
  6291. // to fully traverse each directory before visiting its children,
  6292. // but it's complicated and didn't seem like a gain in the test app
  6293. int n;
  6294. struct _wfinddatai64_t c_file;
  6295. long hFile;
  6296. stb__wchar full_path[1024];
  6297. int has_slash;
  6298. if (stb__showfile) printf("<");
  6299. has_slash = (path[0] && path[strlen(path)-1] == '/');
  6300. // @TODO: do this concatenation without using swprintf to avoid this mess:
  6301. #if (defined(_MSC_VER) && _MSC_VER < 1400) // || (defined(__clang__))
  6302. // confusingly, Windows Kits\10 needs to go down this path?!?
  6303. // except now it doesn't, I don't know what changed
  6304. if (has_slash)
  6305. swprintf(full_path, L"%s*", stb__from_utf8(path));
  6306. else
  6307. swprintf(full_path, L"%s/*", stb__from_utf8(path));
  6308. #else
  6309. if (has_slash)
  6310. swprintf((wchar_t *) full_path, (size_t) 1024, L"%s*", (wchar_t *) stb__from_utf8(path));
  6311. else
  6312. swprintf((wchar_t *) full_path, (size_t) 1024, L"%s/*", (wchar_t *) stb__from_utf8(path));
  6313. #endif
  6314. // it's possible this directory is already present: that means it was in the
  6315. // cache, but its parent wasn't... in that case, we're done with it
  6316. if (stb__showfile) printf("C[%d]", stb_arr_len(active->dirs));
  6317. for (n=0; n < stb_arr_len(active->dirs); ++n)
  6318. if (0 == stb_stricmp(active->dirs[n].path, path)) {
  6319. if (stb__showfile) printf("D");
  6320. return;
  6321. }
  6322. if (stb__showfile) printf("E");
  6323. // otherwise, we need to add it
  6324. stb__dirtree_add_dir(path, last_time, active);
  6325. n = stb_arr_lastn(active->dirs);
  6326. if (stb__showfile) printf("[");
  6327. if( (hFile = (long) _wfindfirsti64( (wchar_t *) full_path, &c_file )) != -1L ) {
  6328. do {
  6329. if (stb__showfile) printf(")");
  6330. if (c_file.attrib & _A_SUBDIR) {
  6331. // ignore subdirectories starting with '.', e.g. "." and ".."
  6332. if (c_file.name[0] != '.') {
  6333. char *new_path = (char *) full_path;
  6334. char *temp = stb__to_utf8((stb__wchar *) c_file.name);
  6335. if (has_slash)
  6336. stb_p_sprintf(new_path stb_p_size(sizeof(full_path)), "%s%s", path, temp);
  6337. else
  6338. stb_p_sprintf(new_path stb_p_size(sizeof(full_path)), "%s/%s", path, temp);
  6339. if (stb__dircount_mask) {
  6340. ++stb__dircount;
  6341. if (!(stb__dircount & stb__dircount_mask)) {
  6342. char dummy_path[128], *pad;
  6343. stb_strncpy(dummy_path, new_path, sizeof(dummy_path)-1);
  6344. if (strlen(dummy_path) > 96) {
  6345. stb_p_strcpy_s(dummy_path+96/2-1,128, "...");
  6346. stb_p_strcpy_s(dummy_path+96/2+2,128, new_path + strlen(new_path)-96/2+2);
  6347. }
  6348. pad = dummy_path + strlen(dummy_path);
  6349. while (pad < dummy_path+98)
  6350. *pad++ = ' ';
  6351. *pad = 0;
  6352. printf("%s\r", dummy_path);
  6353. #if 0
  6354. if (hlog == 0) {
  6355. hlog = stb_p_fopen("c:/x/temp.log", "w");
  6356. fprintf(hlog, "%s\n", dummy_path);
  6357. }
  6358. #endif
  6359. }
  6360. }
  6361. stb__dirtree_scandir(new_path, c_file.time_write, active);
  6362. }
  6363. } else {
  6364. char *temp = stb__to_utf8((stb__wchar *) c_file.name);
  6365. stb__dirtree_add_file(temp, n, c_file.size, c_file.time_write, active);
  6366. }
  6367. if (stb__showfile) printf("(");
  6368. } while( _wfindnexti64( hFile, &c_file ) == 0 );
  6369. if (stb__showfile) printf("]");
  6370. _findclose( hFile );
  6371. }
  6372. if (stb__showfile) printf(">\n");
  6373. }
  6374. // scan the database and see if it's all valid
  6375. static int stb__dirtree_update_db(stb_dirtree *db, stb_dirtree *active)
  6376. {
  6377. int changes_detected = STB_FALSE;
  6378. int i;
  6379. int *remap;
  6380. int *rescan=NULL;
  6381. remap = (int *) malloc(sizeof(remap[0]) * stb_arr_len(db->dirs));
  6382. memset(remap, 0, sizeof(remap[0]) * stb_arr_len(db->dirs));
  6383. rescan = NULL;
  6384. for (i=0; i < stb_arr_len(db->dirs); ++i) {
  6385. struct _stat info;
  6386. if (stb__dircount_mask) {
  6387. ++stb__dircount;
  6388. if (!(stb__dircount & stb__dircount_mask)) {
  6389. printf(".");
  6390. }
  6391. }
  6392. if (0 == _stat(db->dirs[i].path, &info)) {
  6393. if (info.st_mode & _S_IFDIR) {
  6394. // it's still a directory, as expected
  6395. int n = abs((int) (info.st_mtime - db->dirs[i].last_modified));
  6396. if (n > 1 && n != 3600) { // the 3600 is a hack because sometimes this jumps for no apparent reason, even when no time zone or DST issues are at play
  6397. // it's changed! force a rescan
  6398. // we don't want to scan it until we've stat()d its
  6399. // subdirs, though, so we queue it
  6400. if (stb__showfile) printf("Changed: %s - %08x:%08x\n", db->dirs[i].path, (unsigned int) db->dirs[i].last_modified, (unsigned int) info.st_mtime);
  6401. stb_arr_push(rescan, i);
  6402. // update the last_mod time
  6403. db->dirs[i].last_modified = info.st_mtime;
  6404. // ignore existing files in this dir
  6405. remap[i] = -1;
  6406. changes_detected = STB_TRUE;
  6407. } else {
  6408. // it hasn't changed, just copy it through unchanged
  6409. stb__dirtree_add_dir(db->dirs[i].path, db->dirs[i].last_modified, active);
  6410. remap[i] = stb_arr_lastn(active->dirs);
  6411. }
  6412. } else {
  6413. // this path used to refer to a directory, but now it's a file!
  6414. // assume that the parent directory is going to be forced to rescan anyway
  6415. goto delete_entry;
  6416. }
  6417. } else {
  6418. delete_entry:
  6419. // directory no longer exists, so don't copy it
  6420. // we don't free it because it's in the string pool now
  6421. db->dirs[i].path = NULL;
  6422. remap[i] = -1;
  6423. changes_detected = STB_TRUE;
  6424. }
  6425. }
  6426. // at this point, we have:
  6427. //
  6428. // <rescan> holds a list of directory indices that need to be scanned due to being out of date
  6429. // <remap> holds the directory index in <active> for each dir in <db>, if it exists; -1 if not
  6430. // directories in <rescan> are not in <active> yet
  6431. // so we can go ahead and remap all the known files right now
  6432. for (i=0; i < stb_arr_len(db->files); ++i) {
  6433. int dir = db->files[i].dir;
  6434. if (remap[dir] >= 0) {
  6435. stb__dirtree_add_file(db->files[i].name, remap[dir], db->files[i].size, db->files[i].last_modified, active);
  6436. }
  6437. }
  6438. // at this point we're done with db->files, and done with remap
  6439. free(remap);
  6440. // now scan those directories using the standard scan
  6441. for (i=0; i < stb_arr_len(rescan); ++i) {
  6442. int z = rescan[i];
  6443. stb__dirtree_scandir(db->dirs[z].path, db->dirs[z].last_modified, active);
  6444. }
  6445. stb_arr_free(rescan);
  6446. return changes_detected;
  6447. }
  6448. static void stb__dirtree_free_raw(stb_dirtree *d)
  6449. {
  6450. stb_free(d->string_pool);
  6451. stb_arr_free(d->dirs);
  6452. stb_arr_free(d->files);
  6453. }
  6454. stb_dirtree *stb_dirtree_get_with_file(char *dir, char *cache_file)
  6455. {
  6456. stb_dirtree *output = (stb_dirtree *) malloc(sizeof(*output));
  6457. stb_dirtree db,active;
  6458. int prev_dir_count, cache_mismatch;
  6459. char *stripped_dir; // store the directory name without a trailing '/' or '\\'
  6460. // load the database of last-known state on disk
  6461. db.string_pool = NULL;
  6462. db.files = NULL;
  6463. db.dirs = NULL;
  6464. stripped_dir = stb_strip_final_slash(stb_p_strdup(dir));
  6465. if (cache_file != NULL)
  6466. stb__dirtree_load_db(cache_file, &db, stripped_dir);
  6467. else if (stb__showfile)
  6468. printf("No cache file\n");
  6469. active.files = NULL;
  6470. active.dirs = NULL;
  6471. active.string_pool = stb_malloc(0,1); // @TODO: share string pools between both?
  6472. // check all the directories in the database; make note if
  6473. // anything we scanned had changed, and rescan those things
  6474. cache_mismatch = stb__dirtree_update_db(&db, &active);
  6475. // check the root tree
  6476. prev_dir_count = stb_arr_len(active.dirs); // record how many directories we've seen
  6477. stb__dirtree_scandir(stripped_dir, 0, &active); // no last_modified time available for root
  6478. if (stb__dircount_mask)
  6479. printf(" \r");
  6480. // done with the DB; write it back out if any changes, i.e. either
  6481. // 1. any inconsistency found between cached information and actual disk
  6482. // or 2. if scanning the root found any new directories--which we detect because
  6483. // more than one directory got added to the active db during that scan
  6484. if (cache_mismatch || stb_arr_len(active.dirs) > prev_dir_count+1)
  6485. stb__dirtree_save_db(cache_file, &active, stripped_dir);
  6486. free(stripped_dir);
  6487. stb__dirtree_free_raw(&db);
  6488. *output = active;
  6489. return output;
  6490. }
  6491. stb_dirtree *stb_dirtree_get_dir(char *dir, char *cache_dir)
  6492. {
  6493. int i;
  6494. stb_uint8 sha[20];
  6495. char dir_lower[1024];
  6496. char cache_file[1024],*s;
  6497. if (cache_dir == NULL)
  6498. return stb_dirtree_get_with_file(dir, NULL);
  6499. stb_p_strcpy_s(dir_lower, sizeof(dir_lower), dir);
  6500. stb_tolower(dir_lower);
  6501. stb_sha1(sha, (unsigned char *) dir_lower, (unsigned int) strlen(dir_lower));
  6502. stb_p_strcpy_s(cache_file, sizeof(cache_file), cache_dir);
  6503. s = cache_file + strlen(cache_file);
  6504. if (s[-1] != '/' && s[-1] != '\\') *s++ = '/';
  6505. stb_p_strcpy_s(s, sizeof(cache_file), "dirtree_");
  6506. s += strlen(s);
  6507. for (i=0; i < 8; ++i) {
  6508. char *hex = (char*)"0123456789abcdef";
  6509. stb_uint z = sha[i];
  6510. *s++ = hex[z >> 4];
  6511. *s++ = hex[z & 15];
  6512. }
  6513. stb_p_strcpy_s(s, sizeof(cache_file), ".bin");
  6514. return stb_dirtree_get_with_file(dir, cache_file);
  6515. }
  6516. stb_dirtree *stb_dirtree_get(char *dir)
  6517. {
  6518. char cache_dir[256];
  6519. stb_p_strcpy_s(cache_dir, sizeof(cache_dir), "c:/bindata");
  6520. #ifdef STB_HAS_REGISTRY
  6521. {
  6522. void *reg = stb_reg_open("rHKLM", "Software\\SilverSpaceship\\stb");
  6523. if (reg) {
  6524. stb_reg_read(reg, "dirtree", cache_dir, sizeof(cache_dir));
  6525. stb_reg_close(reg);
  6526. }
  6527. }
  6528. #endif
  6529. return stb_dirtree_get_dir(dir, cache_dir);
  6530. }
  6531. void stb_dirtree_free(stb_dirtree *d)
  6532. {
  6533. stb__dirtree_free_raw(d);
  6534. free(d);
  6535. }
  6536. void stb_dirtree_db_add_dir(stb_dirtree *active, char *path, time_t last)
  6537. {
  6538. stb__dirtree_add_dir(path, last, active);
  6539. }
  6540. void stb_dirtree_db_add_file(stb_dirtree *active, char *name, int dir, stb_int64 size, time_t last)
  6541. {
  6542. stb__dirtree_add_file(name, dir, size, last, active);
  6543. }
  6544. void stb_dirtree_db_read(stb_dirtree *target, char *filename, char *dir)
  6545. {
  6546. char *s = stb_strip_final_slash(stb_p_strdup(dir));
  6547. target->dirs = 0;
  6548. target->files = 0;
  6549. target->string_pool = 0;
  6550. stb__dirtree_load_db(filename, target, s);
  6551. free(s);
  6552. }
  6553. void stb_dirtree_db_write(stb_dirtree *target, char *filename, char *dir)
  6554. {
  6555. stb__dirtree_save_db(filename, target, 0); // don't strip out any directories
  6556. }
  6557. #endif // STB_DEFINE
  6558. #endif // _WIN32
  6559. #endif // STB_NO_STB_STRINGS
  6560. //////////////////////////////////////////////////////////////////////////////
  6561. //
  6562. // STB_MALLOC_WRAPPER
  6563. //
  6564. // you can use the wrapper functions with your own malloc wrapper,
  6565. // or define STB_MALLOC_WRAPPER project-wide to have
  6566. // malloc/free/realloc/strdup all get vectored to it
  6567. // this has too many very specific error messages you could google for and find in stb.h,
  6568. // so don't use it if they don't want any stb.h-identifiable strings
  6569. #if defined(STB_DEFINE) && !defined(STB_NO_STB_STRINGS)
  6570. typedef struct
  6571. {
  6572. void *p;
  6573. char *file;
  6574. int line;
  6575. size_t size;
  6576. } stb_malloc_record;
  6577. #ifndef STB_MALLOC_HISTORY_COUNT
  6578. #define STB_MALLOC_HISTORY_COUNT 50 // 800 bytes
  6579. #endif
  6580. stb_malloc_record *stb__allocations;
  6581. static int stb__alloc_size, stb__alloc_limit, stb__alloc_mask;
  6582. int stb__alloc_count;
  6583. stb_malloc_record stb__alloc_history[STB_MALLOC_HISTORY_COUNT];
  6584. int stb__history_pos;
  6585. static int stb__hashfind(void *p)
  6586. {
  6587. stb_uint32 h = stb_hashptr(p);
  6588. int s,n = h & stb__alloc_mask;
  6589. if (stb__allocations[n].p == p)
  6590. return n;
  6591. s = stb_rehash(h)|1;
  6592. for(;;) {
  6593. if (stb__allocations[n].p == NULL)
  6594. return -1;
  6595. n = (n+s) & stb__alloc_mask;
  6596. if (stb__allocations[n].p == p)
  6597. return n;
  6598. }
  6599. }
  6600. size_t stb_wrapper_allocsize(void *p)
  6601. {
  6602. int n = stb__hashfind(p);
  6603. if (n < 0) return 0;
  6604. return stb__allocations[n].size;
  6605. }
  6606. static int stb__historyfind(void *p)
  6607. {
  6608. int n = stb__history_pos;
  6609. int i;
  6610. for (i=0; i < STB_MALLOC_HISTORY_COUNT; ++i) {
  6611. if (--n < 0) n = STB_MALLOC_HISTORY_COUNT-1;
  6612. if (stb__alloc_history[n].p == p)
  6613. return n;
  6614. }
  6615. return -1;
  6616. }
  6617. static void stb__add_alloc(void *p, size_t sz, char *file, int line);
  6618. static void stb__grow_alloc(void)
  6619. {
  6620. int i,old_num = stb__alloc_size;
  6621. stb_malloc_record *old = stb__allocations;
  6622. if (stb__alloc_size == 0)
  6623. stb__alloc_size = 64;
  6624. else
  6625. stb__alloc_size *= 2;
  6626. stb__allocations = (stb_malloc_record *) stb__realloc_raw(NULL, stb__alloc_size * sizeof(stb__allocations[0]));
  6627. if (stb__allocations == NULL)
  6628. stb_fatal("Internal error: couldn't grow malloc wrapper table");
  6629. memset(stb__allocations, 0, stb__alloc_size * sizeof(stb__allocations[0]));
  6630. stb__alloc_limit = (stb__alloc_size*3)>>2;
  6631. stb__alloc_mask = stb__alloc_size-1;
  6632. stb__alloc_count = 0;
  6633. for (i=0; i < old_num; ++i)
  6634. if (old[i].p > STB_DEL) {
  6635. stb__add_alloc(old[i].p, old[i].size, old[i].file, old[i].line);
  6636. assert(stb__hashfind(old[i].p) >= 0);
  6637. }
  6638. for (i=0; i < old_num; ++i)
  6639. if (old[i].p > STB_DEL)
  6640. assert(stb__hashfind(old[i].p) >= 0);
  6641. stb__realloc_raw(old, 0);
  6642. }
  6643. static void stb__add_alloc(void *p, size_t sz, char *file, int line)
  6644. {
  6645. stb_uint32 h;
  6646. int n;
  6647. if (stb__alloc_count >= stb__alloc_limit)
  6648. stb__grow_alloc();
  6649. h = stb_hashptr(p);
  6650. n = h & stb__alloc_mask;
  6651. if (stb__allocations[n].p > STB_DEL) {
  6652. int s = stb_rehash(h)|1;
  6653. do {
  6654. n = (n+s) & stb__alloc_mask;
  6655. } while (stb__allocations[n].p > STB_DEL);
  6656. }
  6657. assert(stb__allocations[n].p == NULL || stb__allocations[n].p == STB_DEL);
  6658. stb__allocations[n].p = p;
  6659. stb__allocations[n].size = sz;
  6660. stb__allocations[n].line = line;
  6661. stb__allocations[n].file = file;
  6662. ++stb__alloc_count;
  6663. }
  6664. static void stb__remove_alloc(int n, char *file, int line)
  6665. {
  6666. stb__alloc_history[stb__history_pos] = stb__allocations[n];
  6667. stb__alloc_history[stb__history_pos].file = file;
  6668. stb__alloc_history[stb__history_pos].line = line;
  6669. if (++stb__history_pos == STB_MALLOC_HISTORY_COUNT)
  6670. stb__history_pos = 0;
  6671. stb__allocations[n].p = STB_DEL;
  6672. --stb__alloc_count;
  6673. }
  6674. void stb_wrapper_malloc(void *p, size_t sz, char *file, int line)
  6675. {
  6676. if (!p) return;
  6677. stb__add_alloc(p,sz,file,line);
  6678. }
  6679. void stb_wrapper_free(void *p, char *file, int line)
  6680. {
  6681. int n;
  6682. if (p == NULL) return;
  6683. n = stb__hashfind(p);
  6684. if (n >= 0)
  6685. stb__remove_alloc(n, file, line);
  6686. else {
  6687. // tried to free something we hadn't allocated!
  6688. n = stb__historyfind(p);
  6689. assert(0); /* NOTREACHED */
  6690. if (n >= 0)
  6691. stb_fatal("Attempted to free %d-byte block %p at %s:%d previously freed/realloced at %s:%d",
  6692. stb__alloc_history[n].size, p,
  6693. file, line,
  6694. stb__alloc_history[n].file, stb__alloc_history[n].line);
  6695. else
  6696. stb_fatal("Attempted to free unknown block %p at %s:%d", p, file,line);
  6697. }
  6698. }
  6699. void stb_wrapper_check(void *p)
  6700. {
  6701. int n;
  6702. if (p == NULL) return;
  6703. n = stb__hashfind(p);
  6704. if (n >= 0) return;
  6705. for (n=0; n < stb__alloc_size; ++n)
  6706. if (stb__allocations[n].p == p)
  6707. stb_fatal("Internal error: pointer %p was allocated, but hash search failed", p);
  6708. // tried to free something that wasn't allocated!
  6709. n = stb__historyfind(p);
  6710. if (n >= 0)
  6711. stb_fatal("Checked %d-byte block %p previously freed/realloced at %s:%d",
  6712. stb__alloc_history[n].size, p,
  6713. stb__alloc_history[n].file, stb__alloc_history[n].line);
  6714. stb_fatal("Checked unknown block %p");
  6715. }
  6716. void stb_wrapper_realloc(void *p, void *q, size_t sz, char *file, int line)
  6717. {
  6718. int n;
  6719. if (p == NULL) { stb_wrapper_malloc(q, sz, file, line); return; }
  6720. if (q == NULL) return; // nothing happened
  6721. n = stb__hashfind(p);
  6722. if (n == -1) {
  6723. // tried to free something we hadn't allocated!
  6724. // this is weird, though, because we got past the realloc!
  6725. n = stb__historyfind(p);
  6726. assert(0); /* NOTREACHED */
  6727. if (n >= 0)
  6728. stb_fatal("Attempted to realloc %d-byte block %p at %s:%d previously freed/realloced at %s:%d",
  6729. stb__alloc_history[n].size, p,
  6730. file, line,
  6731. stb__alloc_history[n].file, stb__alloc_history[n].line);
  6732. else
  6733. stb_fatal("Attempted to realloc unknown block %p at %s:%d", p, file,line);
  6734. } else {
  6735. if (q == p) {
  6736. stb__allocations[n].size = sz;
  6737. stb__allocations[n].file = file;
  6738. stb__allocations[n].line = line;
  6739. } else {
  6740. stb__remove_alloc(n, file, line);
  6741. stb__add_alloc(q,sz,file,line);
  6742. }
  6743. }
  6744. }
  6745. void stb_wrapper_listall(void (*func)(void *ptr, size_t sz, char *file, int line))
  6746. {
  6747. int i;
  6748. for (i=0; i < stb__alloc_size; ++i)
  6749. if (stb__allocations[i].p > STB_DEL)
  6750. func(stb__allocations[i].p , stb__allocations[i].size,
  6751. stb__allocations[i].file, stb__allocations[i].line);
  6752. }
  6753. void stb_wrapper_dump(char *filename)
  6754. {
  6755. int i;
  6756. FILE *f = stb_p_fopen(filename, "w");
  6757. if (!f) return;
  6758. for (i=0; i < stb__alloc_size; ++i)
  6759. if (stb__allocations[i].p > STB_DEL)
  6760. fprintf(f, "%p %7d - %4d %s\n",
  6761. stb__allocations[i].p , (int) stb__allocations[i].size,
  6762. stb__allocations[i].line, stb__allocations[i].file);
  6763. }
  6764. #endif // STB_DEFINE
  6765. //////////////////////////////////////////////////////////////////////////////
  6766. //
  6767. // stb_pointer_set
  6768. //
  6769. //
  6770. // For data structures that support querying by key, data structure
  6771. // classes always hand-wave away the issue of what to do if two entries
  6772. // have the same key: basically, store a linked list of all the nodes
  6773. // which have the same key (a LISP-style list).
  6774. //
  6775. // The thing is, it's not that trivial. If you have an O(log n)
  6776. // lookup data structure, but then n/4 items have the same value,
  6777. // you don't want to spend O(n) time scanning that list when
  6778. // deleting an item if you already have a pointer to the item.
  6779. // (You have to spend O(n) time enumerating all the items with
  6780. // a given key, sure, and you can't accelerate deleting a particular
  6781. // item if you only have the key, not a pointer to the item.)
  6782. //
  6783. // I'm going to call this data structure, whatever it turns out to
  6784. // be, a "pointer set", because we don't store any associated data for
  6785. // items in this data structure, we just answer the question of
  6786. // whether an item is in it or not (it's effectively one bit per pointer).
  6787. // Technically they don't have to be pointers; you could cast ints
  6788. // to (void *) if you want, but you can't store 0 or 1 because of the
  6789. // hash table.
  6790. //
  6791. // Since the fastest data structure we might want to add support for
  6792. // identical-keys to is a hash table with O(1)-ish lookup time,
  6793. // that means that the conceptual "linked list of all items with
  6794. // the same indexed value" that we build needs to have the same
  6795. // performance; that way when we index a table we think is arbitrary
  6796. // ints, but in fact half of them are 0, we don't get screwed.
  6797. //
  6798. // Therefore, it needs to be a hash table, at least when it gets
  6799. // large. On the other hand, when the data has totally arbitrary ints
  6800. // or floats, there won't be many collisions, and we'll have tons of
  6801. // 1-item bitmaps. That will be grossly inefficient as hash tables;
  6802. // trade-off; the hash table is reasonably efficient per-item when
  6803. // it's large, but not when it's small. So we need to do something
  6804. // Judy-like and use different strategies depending on the size.
  6805. //
  6806. // Like Judy, we'll use the bottom bit to encode the strategy:
  6807. //
  6808. // bottom bits:
  6809. // 00 - direct pointer
  6810. // 01 - 4-item bucket (16 bytes, no length, NULLs)
  6811. // 10 - N-item array
  6812. // 11 - hash table
  6813. typedef struct stb_ps stb_ps;
  6814. STB_EXTERN int stb_ps_find (stb_ps *ps, void *value);
  6815. STB_EXTERN stb_ps * stb_ps_add (stb_ps *ps, void *value);
  6816. STB_EXTERN stb_ps * stb_ps_remove(stb_ps *ps, void *value);
  6817. STB_EXTERN stb_ps * stb_ps_remove_any(stb_ps *ps, void **value);
  6818. STB_EXTERN void stb_ps_delete(stb_ps *ps);
  6819. STB_EXTERN int stb_ps_count (stb_ps *ps);
  6820. STB_EXTERN stb_ps * stb_ps_copy (stb_ps *ps);
  6821. STB_EXTERN int stb_ps_subset(stb_ps *bigger, stb_ps *smaller);
  6822. STB_EXTERN int stb_ps_eq (stb_ps *p0, stb_ps *p1);
  6823. STB_EXTERN void ** stb_ps_getlist (stb_ps *ps, int *count);
  6824. STB_EXTERN int stb_ps_writelist(stb_ps *ps, void **list, int size );
  6825. // enum and fastlist don't allocate storage, but you must consume the
  6826. // list before there's any chance the data structure gets screwed up;
  6827. STB_EXTERN int stb_ps_enum (stb_ps *ps, void *data,
  6828. int (*func)(void *value, void*data) );
  6829. STB_EXTERN void ** stb_ps_fastlist(stb_ps *ps, int *count);
  6830. // result:
  6831. // returns a list, *count is the length of that list,
  6832. // but some entries of the list may be invalid;
  6833. // test with 'stb_ps_fastlist_valid(x)'
  6834. #define stb_ps_fastlist_valid(x) ((stb_uinta) (x) > 1)
  6835. #ifdef STB_DEFINE
  6836. enum
  6837. {
  6838. STB_ps_direct = 0,
  6839. STB_ps_bucket = 1,
  6840. STB_ps_array = 2,
  6841. STB_ps_hash = 3,
  6842. };
  6843. #define STB_BUCKET_SIZE 4
  6844. typedef struct
  6845. {
  6846. void *p[STB_BUCKET_SIZE];
  6847. } stb_ps_bucket;
  6848. #define GetBucket(p) ((stb_ps_bucket *) ((char *) (p) - STB_ps_bucket))
  6849. #define EncodeBucket(p) ((stb_ps *) ((char *) (p) + STB_ps_bucket))
  6850. static void stb_bucket_free(stb_ps_bucket *b)
  6851. {
  6852. free(b);
  6853. }
  6854. static stb_ps_bucket *stb_bucket_create2(void *v0, void *v1)
  6855. {
  6856. stb_ps_bucket *b = (stb_ps_bucket*) malloc(sizeof(*b));
  6857. b->p[0] = v0;
  6858. b->p[1] = v1;
  6859. b->p[2] = NULL;
  6860. b->p[3] = NULL;
  6861. return b;
  6862. }
  6863. static stb_ps_bucket * stb_bucket_create3(void **v)
  6864. {
  6865. stb_ps_bucket *b = (stb_ps_bucket*) malloc(sizeof(*b));
  6866. b->p[0] = v[0];
  6867. b->p[1] = v[1];
  6868. b->p[2] = v[2];
  6869. b->p[3] = NULL;
  6870. return b;
  6871. }
  6872. // could use stb_arr, but this will save us memory
  6873. typedef struct
  6874. {
  6875. int count;
  6876. void *p[1];
  6877. } stb_ps_array;
  6878. #define GetArray(p) ((stb_ps_array *) ((char *) (p) - STB_ps_array))
  6879. #define EncodeArray(p) ((stb_ps *) ((char *) (p) + STB_ps_array))
  6880. static int stb_ps_array_max = 13;
  6881. typedef struct
  6882. {
  6883. int size, mask;
  6884. int count, count_deletes;
  6885. int grow_threshhold;
  6886. int shrink_threshhold;
  6887. int rehash_threshhold;
  6888. int any_offset;
  6889. void *table[1];
  6890. } stb_ps_hash;
  6891. #define GetHash(p) ((stb_ps_hash *) ((char *) (p) - STB_ps_hash))
  6892. #define EncodeHash(p) ((stb_ps *) ((char *) (p) + STB_ps_hash))
  6893. #define stb_ps_empty(v) (((stb_uint32) v) <= 1)
  6894. static stb_ps_hash *stb_ps_makehash(int size, int old_size, void **old_data)
  6895. {
  6896. int i;
  6897. stb_ps_hash *h = (stb_ps_hash *) malloc(sizeof(*h) + (size-1) * sizeof(h->table[0]));
  6898. assert(stb_is_pow2(size));
  6899. h->size = size;
  6900. h->mask = size-1;
  6901. h->shrink_threshhold = (int) (0.3f * size);
  6902. h-> grow_threshhold = (int) (0.8f * size);
  6903. h->rehash_threshhold = (int) (0.9f * size);
  6904. h->count = 0;
  6905. h->count_deletes = 0;
  6906. h->any_offset = 0;
  6907. memset(h->table, 0, size * sizeof(h->table[0]));
  6908. for (i=0; i < old_size; ++i)
  6909. if (!stb_ps_empty((size_t)old_data[i]))
  6910. stb_ps_add(EncodeHash(h), old_data[i]);
  6911. return h;
  6912. }
  6913. void stb_ps_delete(stb_ps *ps)
  6914. {
  6915. switch (3 & (int)(size_t) ps) {
  6916. case STB_ps_direct: break;
  6917. case STB_ps_bucket: stb_bucket_free(GetBucket(ps)); break;
  6918. case STB_ps_array : free(GetArray(ps)); break;
  6919. case STB_ps_hash : free(GetHash(ps)); break;
  6920. }
  6921. }
  6922. stb_ps *stb_ps_copy(stb_ps *ps)
  6923. {
  6924. int i;
  6925. // not a switch: order based on expected performance/power-law distribution
  6926. switch (3 & (int)(size_t) ps) {
  6927. case STB_ps_direct: return ps;
  6928. case STB_ps_bucket: {
  6929. stb_ps_bucket *n = (stb_ps_bucket *) malloc(sizeof(*n));
  6930. *n = *GetBucket(ps);
  6931. return EncodeBucket(n);
  6932. }
  6933. case STB_ps_array: {
  6934. stb_ps_array *a = GetArray(ps);
  6935. stb_ps_array *n = (stb_ps_array *) malloc(sizeof(*n) + stb_ps_array_max * sizeof(n->p[0]));
  6936. n->count = a->count;
  6937. for (i=0; i < a->count; ++i)
  6938. n->p[i] = a->p[i];
  6939. return EncodeArray(n);
  6940. }
  6941. case STB_ps_hash: {
  6942. stb_ps_hash *h = GetHash(ps);
  6943. stb_ps_hash *n = stb_ps_makehash(h->size, h->size, h->table);
  6944. return EncodeHash(n);
  6945. }
  6946. }
  6947. assert(0); /* NOTREACHED */
  6948. return NULL;
  6949. }
  6950. int stb_ps_find(stb_ps *ps, void *value)
  6951. {
  6952. int i, code = 3 & (int)(size_t) ps;
  6953. assert((3 & (int)(size_t) value) == STB_ps_direct);
  6954. assert(stb_ps_fastlist_valid(value));
  6955. // not a switch: order based on expected performance/power-law distribution
  6956. if (code == STB_ps_direct)
  6957. return value == ps;
  6958. if (code == STB_ps_bucket) {
  6959. stb_ps_bucket *b = GetBucket(ps);
  6960. assert(STB_BUCKET_SIZE == 4);
  6961. if (b->p[0] == value || b->p[1] == value ||
  6962. b->p[2] == value || b->p[3] == value)
  6963. return STB_TRUE;
  6964. return STB_FALSE;
  6965. }
  6966. if (code == STB_ps_array) {
  6967. stb_ps_array *a = GetArray(ps);
  6968. for (i=0; i < a->count; ++i)
  6969. if (a->p[i] == value)
  6970. return STB_TRUE;
  6971. return STB_FALSE;
  6972. } else {
  6973. stb_ps_hash *h = GetHash(ps);
  6974. stb_uint32 hash = stb_hashptr(value);
  6975. stb_uint32 s, n = hash & h->mask;
  6976. void **t = h->table;
  6977. if (t[n] == value) return STB_TRUE;
  6978. if (t[n] == NULL) return STB_FALSE;
  6979. s = stb_rehash(hash) | 1;
  6980. do {
  6981. n = (n + s) & h->mask;
  6982. if (t[n] == value) return STB_TRUE;
  6983. } while (t[n] != NULL);
  6984. return STB_FALSE;
  6985. }
  6986. }
  6987. stb_ps * stb_ps_add (stb_ps *ps, void *value)
  6988. {
  6989. #ifdef STB_DEBUG
  6990. assert(!stb_ps_find(ps,value));
  6991. #endif
  6992. if (value == NULL) return ps; // ignore NULL adds to avoid bad breakage
  6993. assert((3 & (int)(size_t) value) == STB_ps_direct);
  6994. assert(stb_ps_fastlist_valid(value));
  6995. assert(value != STB_DEL); // STB_DEL is less likely
  6996. switch (3 & (int)(size_t) ps) {
  6997. case STB_ps_direct:
  6998. if (ps == NULL) return (stb_ps *) value;
  6999. return EncodeBucket(stb_bucket_create2(ps,value));
  7000. case STB_ps_bucket: {
  7001. stb_ps_bucket *b = GetBucket(ps);
  7002. stb_ps_array *a;
  7003. assert(STB_BUCKET_SIZE == 4);
  7004. if (b->p[0] == NULL) { b->p[0] = value; return ps; }
  7005. if (b->p[1] == NULL) { b->p[1] = value; return ps; }
  7006. if (b->p[2] == NULL) { b->p[2] = value; return ps; }
  7007. if (b->p[3] == NULL) { b->p[3] = value; return ps; }
  7008. a = (stb_ps_array *) malloc(sizeof(*a) + 7 * sizeof(a->p[0])); // 8 slots, must be 2^k
  7009. memcpy(a->p, b, sizeof(*b));
  7010. a->p[4] = value;
  7011. a->count = 5;
  7012. stb_bucket_free(b);
  7013. return EncodeArray(a);
  7014. }
  7015. case STB_ps_array: {
  7016. stb_ps_array *a = GetArray(ps);
  7017. if (a->count == stb_ps_array_max) {
  7018. // promote from array to hash
  7019. stb_ps_hash *h = stb_ps_makehash(2 << stb_log2_ceil(a->count), a->count, a->p);
  7020. free(a);
  7021. return stb_ps_add(EncodeHash(h), value);
  7022. }
  7023. // do we need to resize the array? the array doubles in size when it
  7024. // crosses a power-of-two
  7025. if ((a->count & (a->count-1))==0) {
  7026. int newsize = a->count*2;
  7027. // clamp newsize to max if:
  7028. // 1. it's larger than max
  7029. // 2. newsize*1.5 is larger than max (to avoid extra resizing)
  7030. if (newsize + a->count > stb_ps_array_max)
  7031. newsize = stb_ps_array_max;
  7032. a = (stb_ps_array *) realloc(a, sizeof(*a) + (newsize-1) * sizeof(a->p[0]));
  7033. }
  7034. a->p[a->count++] = value;
  7035. return EncodeArray(a);
  7036. }
  7037. case STB_ps_hash: {
  7038. stb_ps_hash *h = GetHash(ps);
  7039. stb_uint32 hash = stb_hashptr(value);
  7040. stb_uint32 n = hash & h->mask;
  7041. void **t = h->table;
  7042. // find first NULL or STB_DEL entry
  7043. if (!stb_ps_empty((size_t)t[n])) {
  7044. stb_uint32 s = stb_rehash(hash) | 1;
  7045. do {
  7046. n = (n + s) & h->mask;
  7047. } while (!stb_ps_empty((size_t)t[n]));
  7048. }
  7049. if (t[n] == STB_DEL)
  7050. -- h->count_deletes;
  7051. t[n] = value;
  7052. ++ h->count;
  7053. if (h->count == h->grow_threshhold) {
  7054. stb_ps_hash *h2 = stb_ps_makehash(h->size*2, h->size, t);
  7055. free(h);
  7056. return EncodeHash(h2);
  7057. }
  7058. if (h->count + h->count_deletes == h->rehash_threshhold) {
  7059. stb_ps_hash *h2 = stb_ps_makehash(h->size, h->size, t);
  7060. free(h);
  7061. return EncodeHash(h2);
  7062. }
  7063. return ps;
  7064. }
  7065. }
  7066. return NULL; /* NOTREACHED */
  7067. }
  7068. stb_ps *stb_ps_remove(stb_ps *ps, void *value)
  7069. {
  7070. #ifdef STB_DEBUG
  7071. assert(stb_ps_find(ps, value));
  7072. #endif
  7073. assert((3 & (int)(size_t) value) == STB_ps_direct);
  7074. if (value == NULL) return ps; // ignore NULL removes to avoid bad breakage
  7075. switch (3 & (int)(size_t) ps) {
  7076. case STB_ps_direct:
  7077. return ps == value ? NULL : ps;
  7078. case STB_ps_bucket: {
  7079. stb_ps_bucket *b = GetBucket(ps);
  7080. int count=0;
  7081. assert(STB_BUCKET_SIZE == 4);
  7082. if (b->p[0] == value) b->p[0] = NULL; else count += (b->p[0] != NULL);
  7083. if (b->p[1] == value) b->p[1] = NULL; else count += (b->p[1] != NULL);
  7084. if (b->p[2] == value) b->p[2] = NULL; else count += (b->p[2] != NULL);
  7085. if (b->p[3] == value) b->p[3] = NULL; else count += (b->p[3] != NULL);
  7086. if (count == 1) { // shrink bucket at size 1
  7087. value = b->p[0];
  7088. if (value == NULL) value = b->p[1];
  7089. if (value == NULL) value = b->p[2];
  7090. if (value == NULL) value = b->p[3];
  7091. assert(value != NULL);
  7092. stb_bucket_free(b);
  7093. return (stb_ps *) value; // return STB_ps_direct of value
  7094. }
  7095. return ps;
  7096. }
  7097. case STB_ps_array: {
  7098. stb_ps_array *a = GetArray(ps);
  7099. int i;
  7100. for (i=0; i < a->count; ++i) {
  7101. if (a->p[i] == value) {
  7102. a->p[i] = a->p[--a->count];
  7103. if (a->count == 3) { // shrink to bucket!
  7104. stb_ps_bucket *b = stb_bucket_create3(a->p);
  7105. free(a);
  7106. return EncodeBucket(b);
  7107. }
  7108. return ps;
  7109. }
  7110. }
  7111. return ps;
  7112. }
  7113. case STB_ps_hash: {
  7114. stb_ps_hash *h = GetHash(ps);
  7115. stb_uint32 hash = stb_hashptr(value);
  7116. stb_uint32 s, n = hash & h->mask;
  7117. void **t = h->table;
  7118. if (t[n] != value) {
  7119. s = stb_rehash(hash) | 1;
  7120. do {
  7121. n = (n + s) & h->mask;
  7122. } while (t[n] != value);
  7123. }
  7124. t[n] = STB_DEL;
  7125. -- h->count;
  7126. ++ h->count_deletes;
  7127. // should we shrink down to an array?
  7128. if (h->count < stb_ps_array_max) {
  7129. int n = 1 << stb_log2_floor(stb_ps_array_max);
  7130. if (h->count < n) {
  7131. stb_ps_array *a = (stb_ps_array *) malloc(sizeof(*a) + (n-1) * sizeof(a->p[0]));
  7132. int i,j=0;
  7133. for (i=0; i < h->size; ++i)
  7134. if (!stb_ps_empty((size_t)t[i]))
  7135. a->p[j++] = t[i];
  7136. assert(j == h->count);
  7137. a->count = j;
  7138. free(h);
  7139. return EncodeArray(a);
  7140. }
  7141. }
  7142. if (h->count == h->shrink_threshhold) {
  7143. stb_ps_hash *h2 = stb_ps_makehash(h->size >> 1, h->size, t);
  7144. free(h);
  7145. return EncodeHash(h2);
  7146. }
  7147. return ps;
  7148. }
  7149. }
  7150. return ps; /* NOTREACHED */
  7151. }
  7152. stb_ps *stb_ps_remove_any(stb_ps *ps, void **value)
  7153. {
  7154. assert(ps != NULL);
  7155. switch (3 & (int)(size_t) ps) {
  7156. case STB_ps_direct:
  7157. *value = ps;
  7158. return NULL;
  7159. case STB_ps_bucket: {
  7160. stb_ps_bucket *b = GetBucket(ps);
  7161. int count=0, slast=0, last=0;
  7162. assert(STB_BUCKET_SIZE == 4);
  7163. if (b->p[0]) { ++count; last = 0; }
  7164. if (b->p[1]) { ++count; slast = last; last = 1; }
  7165. if (b->p[2]) { ++count; slast = last; last = 2; }
  7166. if (b->p[3]) { ++count; slast = last; last = 3; }
  7167. *value = b->p[last];
  7168. b->p[last] = 0;
  7169. if (count == 2) {
  7170. void *leftover = b->p[slast]; // second to last
  7171. stb_bucket_free(b);
  7172. return (stb_ps *) leftover;
  7173. }
  7174. return ps;
  7175. }
  7176. case STB_ps_array: {
  7177. stb_ps_array *a = GetArray(ps);
  7178. *value = a->p[a->count-1];
  7179. if (a->count == 4)
  7180. return stb_ps_remove(ps, *value);
  7181. --a->count;
  7182. return ps;
  7183. }
  7184. case STB_ps_hash: {
  7185. stb_ps_hash *h = GetHash(ps);
  7186. void **t = h->table;
  7187. stb_uint32 n = h->any_offset;
  7188. while (stb_ps_empty((size_t)t[n]))
  7189. n = (n + 1) & h->mask;
  7190. *value = t[n];
  7191. h->any_offset = (n+1) & h->mask;
  7192. // check if we need to skip down to the previous type
  7193. if (h->count-1 < stb_ps_array_max || h->count-1 == h->shrink_threshhold)
  7194. return stb_ps_remove(ps, *value);
  7195. t[n] = STB_DEL;
  7196. -- h->count;
  7197. ++ h->count_deletes;
  7198. return ps;
  7199. }
  7200. }
  7201. return ps; /* NOTREACHED */
  7202. }
  7203. void ** stb_ps_getlist(stb_ps *ps, int *count)
  7204. {
  7205. int i,n=0;
  7206. void **p = NULL;
  7207. switch (3 & (int)(size_t) ps) {
  7208. case STB_ps_direct:
  7209. if (ps == NULL) { *count = 0; return NULL; }
  7210. p = (void **) malloc(sizeof(*p) * 1);
  7211. p[0] = ps;
  7212. *count = 1;
  7213. return p;
  7214. case STB_ps_bucket: {
  7215. stb_ps_bucket *b = GetBucket(ps);
  7216. p = (void **) malloc(sizeof(*p) * STB_BUCKET_SIZE);
  7217. for (i=0; i < STB_BUCKET_SIZE; ++i)
  7218. if (b->p[i] != NULL)
  7219. p[n++] = b->p[i];
  7220. break;
  7221. }
  7222. case STB_ps_array: {
  7223. stb_ps_array *a = GetArray(ps);
  7224. p = (void **) malloc(sizeof(*p) * a->count);
  7225. memcpy(p, a->p, sizeof(*p) * a->count);
  7226. *count = a->count;
  7227. return p;
  7228. }
  7229. case STB_ps_hash: {
  7230. stb_ps_hash *h = GetHash(ps);
  7231. p = (void **) malloc(sizeof(*p) * h->count);
  7232. for (i=0; i < h->size; ++i)
  7233. if (!stb_ps_empty((size_t)h->table[i]))
  7234. p[n++] = h->table[i];
  7235. break;
  7236. }
  7237. }
  7238. *count = n;
  7239. return p;
  7240. }
  7241. int stb_ps_writelist(stb_ps *ps, void **list, int size )
  7242. {
  7243. int i,n=0;
  7244. switch (3 & (int)(size_t) ps) {
  7245. case STB_ps_direct:
  7246. if (ps == NULL || size <= 0) return 0;
  7247. list[0] = ps;
  7248. return 1;
  7249. case STB_ps_bucket: {
  7250. stb_ps_bucket *b = GetBucket(ps);
  7251. for (i=0; i < STB_BUCKET_SIZE; ++i)
  7252. if (b->p[i] != NULL && n < size)
  7253. list[n++] = b->p[i];
  7254. return n;
  7255. }
  7256. case STB_ps_array: {
  7257. stb_ps_array *a = GetArray(ps);
  7258. n = stb_min(size, a->count);
  7259. memcpy(list, a->p, sizeof(*list) * n);
  7260. return n;
  7261. }
  7262. case STB_ps_hash: {
  7263. stb_ps_hash *h = GetHash(ps);
  7264. if (size <= 0) return 0;
  7265. for (i=0; i < h->count; ++i) {
  7266. if (!stb_ps_empty((size_t)h->table[i])) {
  7267. list[n++] = h->table[i];
  7268. if (n == size) break;
  7269. }
  7270. }
  7271. return n;
  7272. }
  7273. }
  7274. return 0; /* NOTREACHED */
  7275. }
  7276. int stb_ps_enum(stb_ps *ps, void *data, int (*func)(void *value, void *data))
  7277. {
  7278. int i;
  7279. switch (3 & (int)(size_t) ps) {
  7280. case STB_ps_direct:
  7281. if (ps == NULL) return STB_TRUE;
  7282. return func(ps, data);
  7283. case STB_ps_bucket: {
  7284. stb_ps_bucket *b = GetBucket(ps);
  7285. for (i=0; i < STB_BUCKET_SIZE; ++i)
  7286. if (b->p[i] != NULL)
  7287. if (!func(b->p[i], data))
  7288. return STB_FALSE;
  7289. return STB_TRUE;
  7290. }
  7291. case STB_ps_array: {
  7292. stb_ps_array *a = GetArray(ps);
  7293. for (i=0; i < a->count; ++i)
  7294. if (!func(a->p[i], data))
  7295. return STB_FALSE;
  7296. return STB_TRUE;
  7297. }
  7298. case STB_ps_hash: {
  7299. stb_ps_hash *h = GetHash(ps);
  7300. for (i=0; i < h->count; ++i)
  7301. if (!stb_ps_empty((size_t)h->table[i]))
  7302. if (!func(h->table[i], data))
  7303. return STB_FALSE;
  7304. return STB_TRUE;
  7305. }
  7306. }
  7307. return STB_TRUE; /* NOTREACHED */
  7308. }
  7309. int stb_ps_count (stb_ps *ps)
  7310. {
  7311. switch (3 & (int)(size_t) ps) {
  7312. case STB_ps_direct:
  7313. return ps != NULL;
  7314. case STB_ps_bucket: {
  7315. stb_ps_bucket *b = GetBucket(ps);
  7316. return (b->p[0] != NULL) + (b->p[1] != NULL) +
  7317. (b->p[2] != NULL) + (b->p[3] != NULL);
  7318. }
  7319. case STB_ps_array: {
  7320. stb_ps_array *a = GetArray(ps);
  7321. return a->count;
  7322. }
  7323. case STB_ps_hash: {
  7324. stb_ps_hash *h = GetHash(ps);
  7325. return h->count;
  7326. }
  7327. }
  7328. return 0;
  7329. }
  7330. void ** stb_ps_fastlist(stb_ps *ps, int *count)
  7331. {
  7332. static void *storage;
  7333. switch (3 & (int)(size_t) ps) {
  7334. case STB_ps_direct:
  7335. if (ps == NULL) { *count = 0; return NULL; }
  7336. storage = ps;
  7337. *count = 1;
  7338. return &storage;
  7339. case STB_ps_bucket: {
  7340. stb_ps_bucket *b = GetBucket(ps);
  7341. *count = STB_BUCKET_SIZE;
  7342. return b->p;
  7343. }
  7344. case STB_ps_array: {
  7345. stb_ps_array *a = GetArray(ps);
  7346. *count = a->count;
  7347. return a->p;
  7348. }
  7349. case STB_ps_hash: {
  7350. stb_ps_hash *h = GetHash(ps);
  7351. *count = h->size;
  7352. return h->table;
  7353. }
  7354. }
  7355. return NULL; /* NOTREACHED */
  7356. }
  7357. int stb_ps_subset(stb_ps *bigger, stb_ps *smaller)
  7358. {
  7359. int i, listlen;
  7360. void **list = stb_ps_fastlist(smaller, &listlen);
  7361. for(i=0; i < listlen; ++i)
  7362. if (stb_ps_fastlist_valid(list[i]))
  7363. if (!stb_ps_find(bigger, list[i]))
  7364. return 0;
  7365. return 1;
  7366. }
  7367. int stb_ps_eq(stb_ps *p0, stb_ps *p1)
  7368. {
  7369. if (stb_ps_count(p0) != stb_ps_count(p1))
  7370. return 0;
  7371. return stb_ps_subset(p0, p1);
  7372. }
  7373. #undef GetBucket
  7374. #undef GetArray
  7375. #undef GetHash
  7376. #undef EncodeBucket
  7377. #undef EncodeArray
  7378. #undef EncodeHash
  7379. #endif
  7380. //////////////////////////////////////////////////////////////////////////////
  7381. //
  7382. // Random Numbers via Meresenne Twister or LCG
  7383. //
  7384. STB_EXTERN unsigned int stb_srandLCG(unsigned int seed);
  7385. STB_EXTERN unsigned int stb_randLCG(void);
  7386. STB_EXTERN double stb_frandLCG(void);
  7387. STB_EXTERN void stb_srand(unsigned int seed);
  7388. STB_EXTERN unsigned int stb_rand(void);
  7389. STB_EXTERN double stb_frand(void);
  7390. STB_EXTERN void stb_shuffle(void *p, size_t n, size_t sz,
  7391. unsigned int seed);
  7392. STB_EXTERN void stb_reverse(void *p, size_t n, size_t sz);
  7393. STB_EXTERN unsigned int stb_randLCG_explicit(unsigned int seed);
  7394. #define stb_rand_define(x,y) \
  7395. \
  7396. unsigned int x(void) \
  7397. { \
  7398. static unsigned int stb__rand = y; \
  7399. stb__rand = stb__rand * 2147001325 + 715136305; /* BCPL */ \
  7400. return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16)); \
  7401. }
  7402. #ifdef STB_DEFINE
  7403. unsigned int stb_randLCG_explicit(unsigned int seed)
  7404. {
  7405. return seed * 2147001325 + 715136305;
  7406. }
  7407. static unsigned int stb__rand_seed=0;
  7408. unsigned int stb_srandLCG(unsigned int seed)
  7409. {
  7410. unsigned int previous = stb__rand_seed;
  7411. stb__rand_seed = seed;
  7412. return previous;
  7413. }
  7414. unsigned int stb_randLCG(void)
  7415. {
  7416. stb__rand_seed = stb__rand_seed * 2147001325 + 715136305; // BCPL generator
  7417. // shuffle non-random bits to the middle, and xor to decorrelate with seed
  7418. return 0x31415926 ^ ((stb__rand_seed >> 16) + (stb__rand_seed << 16));
  7419. }
  7420. double stb_frandLCG(void)
  7421. {
  7422. return stb_randLCG() / ((double) (1 << 16) * (1 << 16));
  7423. }
  7424. void stb_shuffle(void *p, size_t n, size_t sz, unsigned int seed)
  7425. {
  7426. char *a;
  7427. unsigned int old_seed;
  7428. int i;
  7429. if (seed)
  7430. old_seed = stb_srandLCG(seed);
  7431. a = (char *) p + (n-1) * sz;
  7432. for (i=(int) n; i > 1; --i) {
  7433. int j = stb_randLCG() % i;
  7434. stb_swap(a, (char *) p + j * sz, sz);
  7435. a -= sz;
  7436. }
  7437. if (seed)
  7438. stb_srandLCG(old_seed);
  7439. }
  7440. void stb_reverse(void *p, size_t n, size_t sz)
  7441. {
  7442. size_t i,j = n-1;
  7443. for (i=0; i < j; ++i,--j) {
  7444. stb_swap((char *) p + i * sz, (char *) p + j * sz, sz);
  7445. }
  7446. }
  7447. // public domain Mersenne Twister by Michael Brundage
  7448. #define STB__MT_LEN 624
  7449. int stb__mt_index = STB__MT_LEN*sizeof(int)+1;
  7450. unsigned int stb__mt_buffer[STB__MT_LEN];
  7451. void stb_srand(unsigned int seed)
  7452. {
  7453. int i;
  7454. stb__mt_buffer[0]= seed & 0xffffffffUL;
  7455. for (i=1 ; i < STB__MT_LEN; ++i)
  7456. stb__mt_buffer[i] = (1812433253UL * (stb__mt_buffer[i-1] ^ (stb__mt_buffer[i-1] >> 30)) + i);
  7457. stb__mt_index = STB__MT_LEN*sizeof(unsigned int);
  7458. }
  7459. #define STB__MT_IA 397
  7460. #define STB__MT_IB (STB__MT_LEN - STB__MT_IA)
  7461. #define STB__UPPER_MASK 0x80000000
  7462. #define STB__LOWER_MASK 0x7FFFFFFF
  7463. #define STB__MATRIX_A 0x9908B0DF
  7464. #define STB__TWIST(b,i,j) ((b)[i] & STB__UPPER_MASK) | ((b)[j] & STB__LOWER_MASK)
  7465. #define STB__MAGIC(s) (((s)&1)*STB__MATRIX_A)
  7466. unsigned int stb_rand()
  7467. {
  7468. unsigned int * b = stb__mt_buffer;
  7469. int idx = stb__mt_index;
  7470. unsigned int s,r;
  7471. int i;
  7472. if (idx >= STB__MT_LEN*sizeof(unsigned int)) {
  7473. if (idx > STB__MT_LEN*sizeof(unsigned int))
  7474. stb_srand(0);
  7475. idx = 0;
  7476. i = 0;
  7477. for (; i < STB__MT_IB; i++) {
  7478. s = STB__TWIST(b, i, i+1);
  7479. b[i] = b[i + STB__MT_IA] ^ (s >> 1) ^ STB__MAGIC(s);
  7480. }
  7481. for (; i < STB__MT_LEN-1; i++) {
  7482. s = STB__TWIST(b, i, i+1);
  7483. b[i] = b[i - STB__MT_IB] ^ (s >> 1) ^ STB__MAGIC(s);
  7484. }
  7485. s = STB__TWIST(b, STB__MT_LEN-1, 0);
  7486. b[STB__MT_LEN-1] = b[STB__MT_IA-1] ^ (s >> 1) ^ STB__MAGIC(s);
  7487. }
  7488. stb__mt_index = idx + sizeof(unsigned int);
  7489. r = *(unsigned int *)((unsigned char *)b + idx);
  7490. r ^= (r >> 11);
  7491. r ^= (r << 7) & 0x9D2C5680;
  7492. r ^= (r << 15) & 0xEFC60000;
  7493. r ^= (r >> 18);
  7494. return r;
  7495. }
  7496. double stb_frand(void)
  7497. {
  7498. return stb_rand() / ((double) (1 << 16) * (1 << 16));
  7499. }
  7500. #endif
  7501. //////////////////////////////////////////////////////////////////////////////
  7502. //
  7503. // stb_dupe
  7504. //
  7505. // stb_dupe is a duplicate-finding system for very, very large data
  7506. // structures--large enough that sorting is too slow, but not so large
  7507. // that we can't keep all the data in memory. using it works as follows:
  7508. //
  7509. // 1. create an stb_dupe:
  7510. // provide a hash function
  7511. // provide an equality function
  7512. // provide an estimate for the size
  7513. // optionally provide a comparison function
  7514. //
  7515. // 2. traverse your data, 'adding' pointers to the stb_dupe
  7516. //
  7517. // 3. finish and ask for duplicates
  7518. //
  7519. // the stb_dupe will discard its intermediate data and build
  7520. // a collection of sorted lists of duplicates, with non-duplicate
  7521. // entries omitted entirely
  7522. //
  7523. //
  7524. // Implementation strategy:
  7525. //
  7526. // while collecting the N items, we keep a hash table of approximate
  7527. // size sqrt(N). (if you tell use the N up front, the hash table is
  7528. // just that size exactly)
  7529. //
  7530. // each entry in the hash table is just an stb__arr of pointers (no need
  7531. // to use stb_ps, because we don't need to delete from these)
  7532. //
  7533. // for step 3, for each entry in the hash table, we apply stb_dupe to it
  7534. // recursively. once the size gets small enough (or doesn't decrease
  7535. // significantly), we switch to either using qsort() on the comparison
  7536. // function, or else we just do the icky N^2 gather
  7537. typedef struct stb_dupe stb_dupe;
  7538. typedef int (*stb_compare_func)(void *a, void *b);
  7539. typedef int (*stb_hash_func)(void *a, unsigned int seed);
  7540. STB_EXTERN void stb_dupe_free(stb_dupe *sd);
  7541. STB_EXTERN stb_dupe *stb_dupe_create(stb_hash_func hash,
  7542. stb_compare_func eq, int size, stb_compare_func ineq);
  7543. STB_EXTERN void stb_dupe_add(stb_dupe *sd, void *item);
  7544. STB_EXTERN void stb_dupe_finish(stb_dupe *sd);
  7545. STB_EXTERN int stb_dupe_numsets(stb_dupe *sd);
  7546. STB_EXTERN void **stb_dupe_set(stb_dupe *sd, int num);
  7547. STB_EXTERN int stb_dupe_set_count(stb_dupe *sd, int num);
  7548. struct stb_dupe
  7549. {
  7550. void ***hash_table;
  7551. int hash_size;
  7552. int size_log2;
  7553. int population;
  7554. int hash_shift;
  7555. stb_hash_func hash;
  7556. stb_compare_func eq;
  7557. stb_compare_func ineq;
  7558. void ***dupes;
  7559. };
  7560. #ifdef STB_DEFINE
  7561. int stb_dupe_numsets(stb_dupe *sd)
  7562. {
  7563. assert(sd->hash_table == NULL);
  7564. return stb_arr_len(sd->dupes);
  7565. }
  7566. void **stb_dupe_set(stb_dupe *sd, int num)
  7567. {
  7568. assert(sd->hash_table == NULL);
  7569. return sd->dupes[num];
  7570. }
  7571. int stb_dupe_set_count(stb_dupe *sd, int num)
  7572. {
  7573. assert(sd->hash_table == NULL);
  7574. return stb_arr_len(sd->dupes[num]);
  7575. }
  7576. stb_dupe *stb_dupe_create(stb_hash_func hash, stb_compare_func eq, int size,
  7577. stb_compare_func ineq)
  7578. {
  7579. int i, hsize;
  7580. stb_dupe *sd = (stb_dupe *) malloc(sizeof(*sd));
  7581. sd->size_log2 = 4;
  7582. hsize = 1 << sd->size_log2;
  7583. while (hsize * hsize < size) {
  7584. ++sd->size_log2;
  7585. hsize *= 2;
  7586. }
  7587. sd->hash = hash;
  7588. sd->eq = eq;
  7589. sd->ineq = ineq;
  7590. sd->hash_shift = 0;
  7591. sd->population = 0;
  7592. sd->hash_size = hsize;
  7593. sd->hash_table = (void ***) malloc(sizeof(*sd->hash_table) * hsize);
  7594. for (i=0; i < hsize; ++i)
  7595. sd->hash_table[i] = NULL;
  7596. sd->dupes = NULL;
  7597. return sd;
  7598. }
  7599. void stb_dupe_add(stb_dupe *sd, void *item)
  7600. {
  7601. stb_uint32 hash = sd->hash(item, sd->hash_shift);
  7602. int z = hash & (sd->hash_size-1);
  7603. stb_arr_push(sd->hash_table[z], item);
  7604. ++sd->population;
  7605. }
  7606. void stb_dupe_free(stb_dupe *sd)
  7607. {
  7608. int i;
  7609. for (i=0; i < stb_arr_len(sd->dupes); ++i)
  7610. if (sd->dupes[i])
  7611. stb_arr_free(sd->dupes[i]);
  7612. stb_arr_free(sd->dupes);
  7613. free(sd);
  7614. }
  7615. static stb_compare_func stb__compare;
  7616. static int stb__dupe_compare(const void *a, const void *b)
  7617. {
  7618. void *p = *(void **) a;
  7619. void *q = *(void **) b;
  7620. return stb__compare(p,q);
  7621. }
  7622. void stb_dupe_finish(stb_dupe *sd)
  7623. {
  7624. int i,j,k;
  7625. assert(sd->dupes == NULL);
  7626. for (i=0; i < sd->hash_size; ++i) {
  7627. void ** list = sd->hash_table[i];
  7628. if (list != NULL) {
  7629. int n = stb_arr_len(list);
  7630. // @TODO: measure to find good numbers instead of just making them up!
  7631. int thresh = (sd->ineq ? 200 : 20);
  7632. // if n is large enough to be worth it, and n is smaller than
  7633. // before (so we can guarantee we'll use a smaller hash table);
  7634. // and there are enough hash bits left, assuming full 32-bit hash
  7635. if (n > thresh && n < (sd->population >> 3) && sd->hash_shift + sd->size_log2*2 < 32) {
  7636. // recursively process this row using stb_dupe, O(N log log N)
  7637. stb_dupe *d = stb_dupe_create(sd->hash, sd->eq, n, sd->ineq);
  7638. d->hash_shift = stb_randLCG_explicit(sd->hash_shift);
  7639. for (j=0; j < n; ++j)
  7640. stb_dupe_add(d, list[j]);
  7641. stb_arr_free(sd->hash_table[i]);
  7642. stb_dupe_finish(d);
  7643. for (j=0; j < stb_arr_len(d->dupes); ++j) {
  7644. stb_arr_push(sd->dupes, d->dupes[j]);
  7645. d->dupes[j] = NULL; // take over ownership
  7646. }
  7647. stb_dupe_free(d);
  7648. } else if (sd->ineq) {
  7649. // process this row using qsort(), O(N log N)
  7650. stb__compare = sd->ineq;
  7651. qsort(list, n, sizeof(list[0]), stb__dupe_compare);
  7652. // find equal subsequences of the list
  7653. for (j=0; j < n-1; ) {
  7654. // find a subsequence from j..k
  7655. for (k=j; k < n; ++k)
  7656. // only use ineq so eq can be left undefined
  7657. if (sd->ineq(list[j], list[k]))
  7658. break;
  7659. // k is the first one not in the subsequence
  7660. if (k-j > 1) {
  7661. void **mylist = NULL;
  7662. stb_arr_setlen(mylist, k-j);
  7663. memcpy(mylist, list+j, sizeof(list[j]) * (k-j));
  7664. stb_arr_push(sd->dupes, mylist);
  7665. }
  7666. j = k;
  7667. }
  7668. stb_arr_free(sd->hash_table[i]);
  7669. } else {
  7670. // process this row using eq(), O(N^2)
  7671. for (j=0; j < n; ++j) {
  7672. if (list[j] != NULL) {
  7673. void **output = NULL;
  7674. for (k=j+1; k < n; ++k) {
  7675. if (sd->eq(list[j], list[k])) {
  7676. if (output == NULL)
  7677. stb_arr_push(output, list[j]);
  7678. stb_arr_push(output, list[k]);
  7679. list[k] = NULL;
  7680. }
  7681. }
  7682. list[j] = NULL;
  7683. if (output)
  7684. stb_arr_push(sd->dupes, output);
  7685. }
  7686. }
  7687. stb_arr_free(sd->hash_table[i]);
  7688. }
  7689. }
  7690. }
  7691. free(sd->hash_table);
  7692. sd->hash_table = NULL;
  7693. }
  7694. #endif
  7695. //////////////////////////////////////////////////////////////////////////////
  7696. //
  7697. // templatized Sort routine
  7698. //
  7699. // This is an attempt to implement a templated sorting algorithm.
  7700. // To use it, you have to explicitly instantiate it as a _function_,
  7701. // then you call that function. This allows the comparison to be inlined,
  7702. // giving the sort similar performance to C++ sorts.
  7703. //
  7704. // It implements quicksort with three-way-median partitioning (generally
  7705. // well-behaved), with a final insertion sort pass.
  7706. //
  7707. // When you define the compare expression, you should assume you have
  7708. // elements of your array pointed to by 'a' and 'b', and perform the comparison
  7709. // on those. OR you can use one or more statements; first say '0;', then
  7710. // write whatever code you want, and compute the result into a variable 'c'.
  7711. #define stb_declare_sort(FUNCNAME, TYPE) \
  7712. void FUNCNAME(TYPE *p, int n)
  7713. #define stb_define_sort(FUNCNAME,TYPE,COMPARE) \
  7714. stb__define_sort( void, FUNCNAME,TYPE,COMPARE)
  7715. #define stb_define_sort_static(FUNCNAME,TYPE,COMPARE) \
  7716. stb__define_sort(static void, FUNCNAME,TYPE,COMPARE)
  7717. #define stb__define_sort(MODE, FUNCNAME, TYPE, COMPARE) \
  7718. \
  7719. static void STB_(FUNCNAME,_ins_sort)(TYPE *p, int n) \
  7720. { \
  7721. int i,j; \
  7722. for (i=1; i < n; ++i) { \
  7723. TYPE t = p[i], *a = &t; \
  7724. j = i; \
  7725. while (j > 0) { \
  7726. TYPE *b = &p[j-1]; \
  7727. int c = COMPARE; \
  7728. if (!c) break; \
  7729. p[j] = p[j-1]; \
  7730. --j; \
  7731. } \
  7732. if (i != j) \
  7733. p[j] = t; \
  7734. } \
  7735. } \
  7736. \
  7737. static void STB_(FUNCNAME,_quicksort)(TYPE *p, int n) \
  7738. { \
  7739. /* threshold for transitioning to insertion sort */ \
  7740. while (n > 12) { \
  7741. TYPE *a,*b,t; \
  7742. int c01,c12,c,m,i,j; \
  7743. \
  7744. /* compute median of three */ \
  7745. m = n >> 1; \
  7746. a = &p[0]; \
  7747. b = &p[m]; \
  7748. c = COMPARE; \
  7749. c01 = c; \
  7750. a = &p[m]; \
  7751. b = &p[n-1]; \
  7752. c = COMPARE; \
  7753. c12 = c; \
  7754. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */ \
  7755. if (c01 != c12) { \
  7756. /* otherwise, we'll need to swap something else to middle */ \
  7757. int z; \
  7758. a = &p[0]; \
  7759. b = &p[n-1]; \
  7760. c = COMPARE; \
  7761. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */ \
  7762. /* 0<mid && mid>n: 0>n => 0; 0<n => n */ \
  7763. z = (c == c12) ? 0 : n-1; \
  7764. t = p[z]; \
  7765. p[z] = p[m]; \
  7766. p[m] = t; \
  7767. } \
  7768. /* now p[m] is the median-of-three */ \
  7769. /* swap it to the beginning so it won't move around */ \
  7770. t = p[0]; \
  7771. p[0] = p[m]; \
  7772. p[m] = t; \
  7773. \
  7774. /* partition loop */ \
  7775. i=1; \
  7776. j=n-1; \
  7777. for(;;) { \
  7778. /* handling of equality is crucial here */ \
  7779. /* for sentinels & efficiency with duplicates */ \
  7780. b = &p[0]; \
  7781. for (;;++i) { \
  7782. a=&p[i]; \
  7783. c = COMPARE; \
  7784. if (!c) break; \
  7785. } \
  7786. a = &p[0]; \
  7787. for (;;--j) { \
  7788. b=&p[j]; \
  7789. c = COMPARE; \
  7790. if (!c) break; \
  7791. } \
  7792. /* make sure we haven't crossed */ \
  7793. if (i >= j) break; \
  7794. t = p[i]; \
  7795. p[i] = p[j]; \
  7796. p[j] = t; \
  7797. \
  7798. ++i; \
  7799. --j; \
  7800. } \
  7801. /* recurse on smaller side, iterate on larger */ \
  7802. if (j < (n-i)) { \
  7803. STB_(FUNCNAME,_quicksort)(p,j); \
  7804. p = p+i; \
  7805. n = n-i; \
  7806. } else { \
  7807. STB_(FUNCNAME,_quicksort)(p+i, n-i); \
  7808. n = j; \
  7809. } \
  7810. } \
  7811. } \
  7812. \
  7813. MODE FUNCNAME(TYPE *p, int n) \
  7814. { \
  7815. STB_(FUNCNAME, _quicksort)(p, n); \
  7816. STB_(FUNCNAME, _ins_sort)(p, n); \
  7817. } \
  7818. //////////////////////////////////////////////////////////////////////////////
  7819. //
  7820. // stb_bitset an array of booleans indexed by integers
  7821. //
  7822. typedef stb_uint32 stb_bitset;
  7823. STB_EXTERN stb_bitset *stb_bitset_new(int value, int len);
  7824. #define stb_bitset_clearall(arr,len) (memset(arr, 0, 4 * (len)))
  7825. #define stb_bitset_setall(arr,len) (memset(arr, 255, 4 * (len)))
  7826. #define stb_bitset_setbit(arr,n) ((arr)[(n) >> 5] |= (1 << (n & 31)))
  7827. #define stb_bitset_clearbit(arr,n) ((arr)[(n) >> 5] &= ~(1 << (n & 31)))
  7828. #define stb_bitset_testbit(arr,n) ((arr)[(n) >> 5] & (1 << (n & 31)))
  7829. STB_EXTERN stb_bitset *stb_bitset_union(stb_bitset *p0, stb_bitset *p1, int len);
  7830. STB_EXTERN int *stb_bitset_getlist(stb_bitset *out, int start, int end);
  7831. STB_EXTERN int stb_bitset_eq(stb_bitset *p0, stb_bitset *p1, int len);
  7832. STB_EXTERN int stb_bitset_disjoint(stb_bitset *p0, stb_bitset *p1, int len);
  7833. STB_EXTERN int stb_bitset_disjoint_0(stb_bitset *p0, stb_bitset *p1, int len);
  7834. STB_EXTERN int stb_bitset_subset(stb_bitset *bigger, stb_bitset *smaller, int len);
  7835. STB_EXTERN int stb_bitset_unioneq_changed(stb_bitset *p0, stb_bitset *p1, int len);
  7836. #ifdef STB_DEFINE
  7837. int stb_bitset_eq(stb_bitset *p0, stb_bitset *p1, int len)
  7838. {
  7839. int i;
  7840. for (i=0; i < len; ++i)
  7841. if (p0[i] != p1[i]) return 0;
  7842. return 1;
  7843. }
  7844. int stb_bitset_disjoint(stb_bitset *p0, stb_bitset *p1, int len)
  7845. {
  7846. int i;
  7847. for (i=0; i < len; ++i)
  7848. if (p0[i] & p1[i]) return 0;
  7849. return 1;
  7850. }
  7851. int stb_bitset_disjoint_0(stb_bitset *p0, stb_bitset *p1, int len)
  7852. {
  7853. int i;
  7854. for (i=0; i < len; ++i)
  7855. if ((p0[i] | p1[i]) != 0xffffffff) return 0;
  7856. return 1;
  7857. }
  7858. int stb_bitset_subset(stb_bitset *bigger, stb_bitset *smaller, int len)
  7859. {
  7860. int i;
  7861. for (i=0; i < len; ++i)
  7862. if ((bigger[i] & smaller[i]) != smaller[i]) return 0;
  7863. return 1;
  7864. }
  7865. stb_bitset *stb_bitset_union(stb_bitset *p0, stb_bitset *p1, int len)
  7866. {
  7867. int i;
  7868. stb_bitset *d = (stb_bitset *) malloc(sizeof(*d) * len);
  7869. for (i=0; i < len; ++i) d[i] = p0[i] | p1[i];
  7870. return d;
  7871. }
  7872. int stb_bitset_unioneq_changed(stb_bitset *p0, stb_bitset *p1, int len)
  7873. {
  7874. int i, changed=0;
  7875. for (i=0; i < len; ++i) {
  7876. stb_bitset d = p0[i] | p1[i];
  7877. if (d != p0[i]) {
  7878. p0[i] = d;
  7879. changed = 1;
  7880. }
  7881. }
  7882. return changed;
  7883. }
  7884. stb_bitset *stb_bitset_new(int value, int len)
  7885. {
  7886. int i;
  7887. stb_bitset *d = (stb_bitset *) malloc(sizeof(*d) * len);
  7888. if (value) value = 0xffffffff;
  7889. for (i=0; i < len; ++i) d[i] = value;
  7890. return d;
  7891. }
  7892. int *stb_bitset_getlist(stb_bitset *out, int start, int end)
  7893. {
  7894. int *list = NULL;
  7895. int i;
  7896. for (i=start; i < end; ++i)
  7897. if (stb_bitset_testbit(out, i))
  7898. stb_arr_push(list, i);
  7899. return list;
  7900. }
  7901. #endif
  7902. //////////////////////////////////////////////////////////////////////////////
  7903. //
  7904. // stb_wordwrap quality word-wrapping for fixed-width fonts
  7905. //
  7906. STB_EXTERN int stb_wordwrap(int *pairs, int pair_max, int count, char *str);
  7907. STB_EXTERN int *stb_wordwrapalloc(int count, char *str);
  7908. #ifdef STB_DEFINE
  7909. int stb_wordwrap(int *pairs, int pair_max, int count, char *str)
  7910. {
  7911. int n=0,i=0, start=0,nonwhite=0;
  7912. if (pairs == NULL) pair_max = 0x7ffffff0;
  7913. else pair_max *= 2;
  7914. // parse
  7915. for(;;) {
  7916. int s=i; // first whitespace char; last nonwhite+1
  7917. int w; // word start
  7918. // accept whitespace
  7919. while (isspace(str[i])) {
  7920. if (str[i] == '\n' || str[i] == '\r') {
  7921. if (str[i] + str[i+1] == '\n' + '\r') ++i;
  7922. if (n >= pair_max) return -1;
  7923. if (pairs) pairs[n] = start, pairs[n+1] = s-start;
  7924. n += 2;
  7925. nonwhite=0;
  7926. start = i+1;
  7927. s = start;
  7928. }
  7929. ++i;
  7930. }
  7931. if (i >= start+count) {
  7932. // we've gone off the end using whitespace
  7933. if (nonwhite) {
  7934. if (n >= pair_max) return -1;
  7935. if (pairs) pairs[n] = start, pairs[n+1] = s-start;
  7936. n += 2;
  7937. start = s = i;
  7938. nonwhite=0;
  7939. } else {
  7940. // output all the whitespace
  7941. while (i >= start+count) {
  7942. if (n >= pair_max) return -1;
  7943. if (pairs) pairs[n] = start, pairs[n+1] = count;
  7944. n += 2;
  7945. start += count;
  7946. }
  7947. s = start;
  7948. }
  7949. }
  7950. if (str[i] == 0) break;
  7951. // now scan out a word and see if it fits
  7952. w = i;
  7953. while (str[i] && !isspace(str[i])) {
  7954. ++i;
  7955. }
  7956. // wrapped?
  7957. if (i > start + count) {
  7958. // huge?
  7959. if (i-s <= count) {
  7960. if (n >= pair_max) return -1;
  7961. if (pairs) pairs[n] = start, pairs[n+1] = s-start;
  7962. n += 2;
  7963. start = w;
  7964. } else {
  7965. // This word is longer than one line. If we wrap it onto N lines
  7966. // there are leftover chars. do those chars fit on the cur line?
  7967. // But if we have leading whitespace, we force it to start here.
  7968. if ((w-start) + ((i-w) % count) <= count || !nonwhite) {
  7969. // output a full line
  7970. if (n >= pair_max) return -1;
  7971. if (pairs) pairs[n] = start, pairs[n+1] = count;
  7972. n += 2;
  7973. start += count;
  7974. w = start;
  7975. } else {
  7976. // output a partial line, trimming trailing whitespace
  7977. if (s != start) {
  7978. if (n >= pair_max) return -1;
  7979. if (pairs) pairs[n] = start, pairs[n+1] = s-start;
  7980. n += 2;
  7981. start = w;
  7982. }
  7983. }
  7984. // now output full lines as needed
  7985. while (start + count <= i) {
  7986. if (n >= pair_max) return -1;
  7987. if (pairs) pairs[n] = start, pairs[n+1] = count;
  7988. n += 2;
  7989. start += count;
  7990. }
  7991. }
  7992. }
  7993. nonwhite=1;
  7994. }
  7995. if (start < i) {
  7996. if (n >= pair_max) return -1;
  7997. if (pairs) pairs[n] = start, pairs[n+1] = i-start;
  7998. n += 2;
  7999. }
  8000. return n>>1;
  8001. }
  8002. int *stb_wordwrapalloc(int count, char *str)
  8003. {
  8004. int n = stb_wordwrap(NULL,0,count,str);
  8005. int *z = NULL;
  8006. stb_arr_setlen(z, n*2);
  8007. stb_wordwrap(z, n, count, str);
  8008. return z;
  8009. }
  8010. #endif
  8011. //////////////////////////////////////////////////////////////////////////////
  8012. //
  8013. // stb_match: wildcards and regexping
  8014. //
  8015. STB_EXTERN int stb_wildmatch (char *expr, char *candidate);
  8016. STB_EXTERN int stb_wildmatchi(char *expr, char *candidate);
  8017. STB_EXTERN int stb_wildfind (char *expr, char *candidate);
  8018. STB_EXTERN int stb_wildfindi (char *expr, char *candidate);
  8019. STB_EXTERN int stb_regex(char *regex, char *candidate);
  8020. typedef struct stb_matcher stb_matcher;
  8021. STB_EXTERN stb_matcher *stb_regex_matcher(char *regex);
  8022. STB_EXTERN int stb_matcher_match(stb_matcher *m, char *str);
  8023. STB_EXTERN int stb_matcher_find(stb_matcher *m, char *str);
  8024. STB_EXTERN void stb_matcher_free(stb_matcher *f);
  8025. STB_EXTERN stb_matcher *stb_lex_matcher(void);
  8026. STB_EXTERN int stb_lex_item(stb_matcher *m, const char *str, int result);
  8027. STB_EXTERN int stb_lex_item_wild(stb_matcher *matcher, const char *regex, int result);
  8028. STB_EXTERN int stb_lex(stb_matcher *m, char *str, int *len);
  8029. #ifdef STB_DEFINE
  8030. static int stb__match_qstring(char *candidate, char *qstring, int qlen, int insensitive)
  8031. {
  8032. int i;
  8033. if (insensitive) {
  8034. for (i=0; i < qlen; ++i)
  8035. if (qstring[i] == '?') {
  8036. if (!candidate[i]) return 0;
  8037. } else
  8038. if (tolower(qstring[i]) != tolower(candidate[i]))
  8039. return 0;
  8040. } else {
  8041. for (i=0; i < qlen; ++i)
  8042. if (qstring[i] == '?') {
  8043. if (!candidate[i]) return 0;
  8044. } else
  8045. if (qstring[i] != candidate[i])
  8046. return 0;
  8047. }
  8048. return 1;
  8049. }
  8050. static int stb__find_qstring(char *candidate, char *qstring, int qlen, int insensitive)
  8051. {
  8052. char c;
  8053. int offset=0;
  8054. while (*qstring == '?') {
  8055. ++qstring;
  8056. --qlen;
  8057. ++candidate;
  8058. if (qlen == 0) return 0;
  8059. if (*candidate == 0) return -1;
  8060. }
  8061. c = *qstring++;
  8062. --qlen;
  8063. if (insensitive) c = tolower(c);
  8064. while (candidate[offset]) {
  8065. if (c == (insensitive ? tolower(candidate[offset]) : candidate[offset]))
  8066. if (stb__match_qstring(candidate+offset+1, qstring, qlen, insensitive))
  8067. return offset;
  8068. ++offset;
  8069. }
  8070. return -1;
  8071. }
  8072. int stb__wildmatch_raw2(char *expr, char *candidate, int search, int insensitive)
  8073. {
  8074. int where=0;
  8075. int start = -1;
  8076. if (!search) {
  8077. // parse to first '*'
  8078. if (*expr != '*')
  8079. start = 0;
  8080. while (*expr != '*') {
  8081. if (!*expr)
  8082. return *candidate == 0 ? 0 : -1;
  8083. if (*expr == '?') {
  8084. if (!*candidate) return -1;
  8085. } else {
  8086. if (insensitive) {
  8087. if (tolower(*candidate) != tolower(*expr))
  8088. return -1;
  8089. } else
  8090. if (*candidate != *expr)
  8091. return -1;
  8092. }
  8093. ++candidate, ++expr, ++where;
  8094. }
  8095. } else {
  8096. // 0-length search string
  8097. if (!*expr)
  8098. return 0;
  8099. }
  8100. assert(search || *expr == '*');
  8101. if (!search)
  8102. ++expr;
  8103. // implicit '*' at this point
  8104. while (*expr) {
  8105. int o=0;
  8106. // combine redundant * characters
  8107. while (expr[0] == '*') ++expr;
  8108. // ok, at this point, expr[-1] == '*',
  8109. // and expr[0] != '*'
  8110. if (!expr[0]) return start >= 0 ? start : 0;
  8111. // now find next '*'
  8112. o = 0;
  8113. while (expr[o] != '*') {
  8114. if (expr[o] == 0)
  8115. break;
  8116. ++o;
  8117. }
  8118. // if no '*', scan to end, then match at end
  8119. if (expr[o] == 0 && !search) {
  8120. int z;
  8121. for (z=0; z < o; ++z)
  8122. if (candidate[z] == 0)
  8123. return -1;
  8124. while (candidate[z])
  8125. ++z;
  8126. // ok, now check if they match
  8127. if (stb__match_qstring(candidate+z-o, expr, o, insensitive))
  8128. return start >= 0 ? start : 0;
  8129. return -1;
  8130. } else {
  8131. // if yes '*', then do stb__find_qmatch on the intervening chars
  8132. int n = stb__find_qstring(candidate, expr, o, insensitive);
  8133. if (n < 0)
  8134. return -1;
  8135. if (start < 0)
  8136. start = where + n;
  8137. expr += o;
  8138. candidate += n+o;
  8139. }
  8140. if (*expr == 0) {
  8141. assert(search);
  8142. return start;
  8143. }
  8144. assert(*expr == '*');
  8145. ++expr;
  8146. }
  8147. return start >= 0 ? start : 0;
  8148. }
  8149. int stb__wildmatch_raw(char *expr, char *candidate, int search, int insensitive)
  8150. {
  8151. char buffer[256];
  8152. // handle multiple search strings
  8153. char *s = strchr(expr, ';');
  8154. char *last = expr;
  8155. while (s) {
  8156. int z;
  8157. // need to allow for non-writeable strings... assume they're small
  8158. if (s - last < 256) {
  8159. stb_strncpy(buffer, last, (int) (s-last+1));
  8160. buffer[s-last] = 0;
  8161. z = stb__wildmatch_raw2(buffer, candidate, search, insensitive);
  8162. } else {
  8163. *s = 0;
  8164. z = stb__wildmatch_raw2(last, candidate, search, insensitive);
  8165. *s = ';';
  8166. }
  8167. if (z >= 0) return z;
  8168. last = s+1;
  8169. s = strchr(last, ';');
  8170. }
  8171. return stb__wildmatch_raw2(last, candidate, search, insensitive);
  8172. }
  8173. int stb_wildmatch(char *expr, char *candidate)
  8174. {
  8175. return stb__wildmatch_raw(expr, candidate, 0,0) >= 0;
  8176. }
  8177. int stb_wildmatchi(char *expr, char *candidate)
  8178. {
  8179. return stb__wildmatch_raw(expr, candidate, 0,1) >= 0;
  8180. }
  8181. int stb_wildfind(char *expr, char *candidate)
  8182. {
  8183. return stb__wildmatch_raw(expr, candidate, 1,0);
  8184. }
  8185. int stb_wildfindi(char *expr, char *candidate)
  8186. {
  8187. return stb__wildmatch_raw(expr, candidate, 1,1);
  8188. }
  8189. typedef struct
  8190. {
  8191. stb_int16 transition[256];
  8192. } stb_dfa;
  8193. // an NFA node represents a state you're in; it then has
  8194. // an arbitrary number of edges dangling off of it
  8195. // note this isn't utf8-y
  8196. typedef struct
  8197. {
  8198. stb_int16 match; // character/set to match
  8199. stb_uint16 node; // output node to go to
  8200. } stb_nfa_edge;
  8201. typedef struct
  8202. {
  8203. stb_int16 goal; // does reaching this win the prize?
  8204. stb_uint8 active; // is this in the active list
  8205. stb_nfa_edge *out;
  8206. stb_uint16 *eps; // list of epsilon closures
  8207. } stb_nfa_node;
  8208. #define STB__DFA_UNDEF -1
  8209. #define STB__DFA_GOAL -2
  8210. #define STB__DFA_END -3
  8211. #define STB__DFA_MGOAL -4
  8212. #define STB__DFA_VALID 0
  8213. #define STB__NFA_STOP_GOAL -1
  8214. // compiled regexp
  8215. struct stb_matcher
  8216. {
  8217. stb_uint16 start_node;
  8218. stb_int16 dfa_start;
  8219. stb_uint32 *charset;
  8220. int num_charset;
  8221. int match_start;
  8222. stb_nfa_node *nodes;
  8223. int does_lex;
  8224. // dfa matcher
  8225. stb_dfa * dfa;
  8226. stb_uint32 * dfa_mapping;
  8227. stb_int16 * dfa_result;
  8228. int num_words_per_dfa;
  8229. };
  8230. static int stb__add_node(stb_matcher *matcher)
  8231. {
  8232. stb_nfa_node z;
  8233. z.active = 0;
  8234. z.eps = 0;
  8235. z.goal = 0;
  8236. z.out = 0;
  8237. stb_arr_push(matcher->nodes, z);
  8238. return stb_arr_len(matcher->nodes)-1;
  8239. }
  8240. static void stb__add_epsilon(stb_matcher *matcher, int from, int to)
  8241. {
  8242. assert(from != to);
  8243. if (matcher->nodes[from].eps == NULL)
  8244. stb_arr_malloc((void **) &matcher->nodes[from].eps, matcher);
  8245. stb_arr_push(matcher->nodes[from].eps, to);
  8246. }
  8247. static void stb__add_edge(stb_matcher *matcher, int from, int to, int type)
  8248. {
  8249. stb_nfa_edge z = { (stb_int16)type, (stb_uint16)to };
  8250. if (matcher->nodes[from].out == NULL)
  8251. stb_arr_malloc((void **) &matcher->nodes[from].out, matcher);
  8252. stb_arr_push(matcher->nodes[from].out, z);
  8253. }
  8254. static char *stb__reg_parse_alt(stb_matcher *m, int s, char *r, stb_uint16 *e);
  8255. static char *stb__reg_parse(stb_matcher *matcher, int start, char *regex, stb_uint16 *end)
  8256. {
  8257. int n;
  8258. int last_start = -1;
  8259. stb_uint16 last_end = start;
  8260. while (*regex) {
  8261. switch (*regex) {
  8262. case '(':
  8263. last_start = last_end;
  8264. regex = stb__reg_parse_alt(matcher, last_end, regex+1, &last_end);
  8265. if (regex == NULL || *regex != ')')
  8266. return NULL;
  8267. ++regex;
  8268. break;
  8269. case '|':
  8270. case ')':
  8271. *end = last_end;
  8272. return regex;
  8273. case '?':
  8274. if (last_start < 0) return NULL;
  8275. stb__add_epsilon(matcher, last_start, last_end);
  8276. ++regex;
  8277. break;
  8278. case '*':
  8279. if (last_start < 0) return NULL;
  8280. stb__add_epsilon(matcher, last_start, last_end);
  8281. // fall through
  8282. case '+':
  8283. if (last_start < 0) return NULL;
  8284. stb__add_epsilon(matcher, last_end, last_start);
  8285. // prevent links back to last_end from chaining to last_start
  8286. n = stb__add_node(matcher);
  8287. stb__add_epsilon(matcher, last_end, n);
  8288. last_end = n;
  8289. ++regex;
  8290. break;
  8291. case '{': // not supported!
  8292. // @TODO: given {n,m}, clone last_start to last_end m times,
  8293. // and include epsilons from start to first m-n blocks
  8294. return NULL;
  8295. case '\\':
  8296. ++regex;
  8297. if (!*regex) return NULL;
  8298. // fallthrough
  8299. default: // match exactly this character
  8300. n = stb__add_node(matcher);
  8301. stb__add_edge(matcher, last_end, n, *regex);
  8302. last_start = last_end;
  8303. last_end = n;
  8304. ++regex;
  8305. break;
  8306. case '$':
  8307. n = stb__add_node(matcher);
  8308. stb__add_edge(matcher, last_end, n, '\n');
  8309. last_start = last_end;
  8310. last_end = n;
  8311. ++regex;
  8312. break;
  8313. case '.':
  8314. n = stb__add_node(matcher);
  8315. stb__add_edge(matcher, last_end, n, -1);
  8316. last_start = last_end;
  8317. last_end = n;
  8318. ++regex;
  8319. break;
  8320. case '[': {
  8321. stb_uint8 flags[256];
  8322. int invert = 0,z;
  8323. ++regex;
  8324. if (matcher->num_charset == 0) {
  8325. matcher->charset = (stb_uint *) stb_malloc(matcher, sizeof(*matcher->charset) * 256);
  8326. memset(matcher->charset, 0, sizeof(*matcher->charset) * 256);
  8327. }
  8328. memset(flags,0,sizeof(flags));
  8329. // leading ^ is special
  8330. if (*regex == '^')
  8331. ++regex, invert = 1;
  8332. // leading ] is special
  8333. if (*regex == ']') {
  8334. flags[(int) ']'] = 1;
  8335. ++regex;
  8336. }
  8337. while (*regex != ']') {
  8338. stb_uint a;
  8339. if (!*regex) return NULL;
  8340. a = *regex++;
  8341. if (regex[0] == '-' && regex[1] != ']') {
  8342. stb_uint i,b = regex[1];
  8343. regex += 2;
  8344. if (b == 0) return NULL;
  8345. if (a > b) return NULL;
  8346. for (i=a; i <= b; ++i)
  8347. flags[i] = 1;
  8348. } else
  8349. flags[a] = 1;
  8350. }
  8351. ++regex;
  8352. if (invert) {
  8353. int i;
  8354. for (i=0; i < 256; ++i)
  8355. flags[i] = 1-flags[i];
  8356. }
  8357. // now check if any existing charset matches
  8358. for (z=0; z < matcher->num_charset; ++z) {
  8359. int i, k[2] = { 0, 1 << z};
  8360. for (i=0; i < 256; ++i) {
  8361. unsigned int f = k[flags[i]];
  8362. if ((matcher->charset[i] & k[1]) != f)
  8363. break;
  8364. }
  8365. if (i == 256) break;
  8366. }
  8367. if (z == matcher->num_charset) {
  8368. int i;
  8369. ++matcher->num_charset;
  8370. if (matcher->num_charset > 32) {
  8371. assert(0); /* NOTREACHED */
  8372. return NULL; // too many charsets, oops
  8373. }
  8374. for (i=0; i < 256; ++i)
  8375. if (flags[i])
  8376. matcher->charset[i] |= (1 << z);
  8377. }
  8378. n = stb__add_node(matcher);
  8379. stb__add_edge(matcher, last_end, n, -2 - z);
  8380. last_start = last_end;
  8381. last_end = n;
  8382. break;
  8383. }
  8384. }
  8385. }
  8386. *end = last_end;
  8387. return regex;
  8388. }
  8389. static char *stb__reg_parse_alt(stb_matcher *matcher, int start, char *regex, stb_uint16 *end)
  8390. {
  8391. stb_uint16 last_end = start;
  8392. stb_uint16 main_end;
  8393. int head, tail;
  8394. head = stb__add_node(matcher);
  8395. stb__add_epsilon(matcher, start, head);
  8396. regex = stb__reg_parse(matcher, head, regex, &last_end);
  8397. if (regex == NULL) return NULL;
  8398. if (*regex == 0 || *regex == ')') {
  8399. *end = last_end;
  8400. return regex;
  8401. }
  8402. main_end = last_end;
  8403. tail = stb__add_node(matcher);
  8404. stb__add_epsilon(matcher, last_end, tail);
  8405. // start alternatives from the same starting node; use epsilon
  8406. // transitions to combine their endings
  8407. while(*regex && *regex != ')') {
  8408. assert(*regex == '|');
  8409. head = stb__add_node(matcher);
  8410. stb__add_epsilon(matcher, start, head);
  8411. regex = stb__reg_parse(matcher, head, regex+1, &last_end);
  8412. if (regex == NULL)
  8413. return NULL;
  8414. stb__add_epsilon(matcher, last_end, tail);
  8415. }
  8416. *end = tail;
  8417. return regex;
  8418. }
  8419. static char *stb__wild_parse(stb_matcher *matcher, int start, char *str, stb_uint16 *end)
  8420. {
  8421. int n;
  8422. stb_uint16 last_end;
  8423. last_end = stb__add_node(matcher);
  8424. stb__add_epsilon(matcher, start, last_end);
  8425. while (*str) {
  8426. switch (*str) {
  8427. // fallthrough
  8428. default: // match exactly this character
  8429. n = stb__add_node(matcher);
  8430. if (toupper(*str) == tolower(*str)) {
  8431. stb__add_edge(matcher, last_end, n, *str);
  8432. } else {
  8433. stb__add_edge(matcher, last_end, n, tolower(*str));
  8434. stb__add_edge(matcher, last_end, n, toupper(*str));
  8435. }
  8436. last_end = n;
  8437. ++str;
  8438. break;
  8439. case '?':
  8440. n = stb__add_node(matcher);
  8441. stb__add_edge(matcher, last_end, n, -1);
  8442. last_end = n;
  8443. ++str;
  8444. break;
  8445. case '*':
  8446. n = stb__add_node(matcher);
  8447. stb__add_edge(matcher, last_end, n, -1);
  8448. stb__add_epsilon(matcher, last_end, n);
  8449. stb__add_epsilon(matcher, n, last_end);
  8450. last_end = n;
  8451. ++str;
  8452. break;
  8453. }
  8454. }
  8455. // now require end of string to match
  8456. n = stb__add_node(matcher);
  8457. stb__add_edge(matcher, last_end, n, 0);
  8458. last_end = n;
  8459. *end = last_end;
  8460. return str;
  8461. }
  8462. static int stb__opt(stb_matcher *m, int n)
  8463. {
  8464. for(;;) {
  8465. stb_nfa_node *p = &m->nodes[n];
  8466. if (p->goal) return n;
  8467. if (stb_arr_len(p->out)) return n;
  8468. if (stb_arr_len(p->eps) != 1) return n;
  8469. n = p->eps[0];
  8470. }
  8471. }
  8472. static void stb__optimize(stb_matcher *m)
  8473. {
  8474. // if the target of any edge is a node with exactly
  8475. // one out-epsilon, shorten it
  8476. int i,j;
  8477. for (i=0; i < stb_arr_len(m->nodes); ++i) {
  8478. stb_nfa_node *p = &m->nodes[i];
  8479. for (j=0; j < stb_arr_len(p->out); ++j)
  8480. p->out[j].node = stb__opt(m,p->out[j].node);
  8481. for (j=0; j < stb_arr_len(p->eps); ++j)
  8482. p->eps[j] = stb__opt(m,p->eps[j] );
  8483. }
  8484. m->start_node = stb__opt(m,m->start_node);
  8485. }
  8486. void stb_matcher_free(stb_matcher *f)
  8487. {
  8488. stb_free(f);
  8489. }
  8490. static stb_matcher *stb__alloc_matcher(void)
  8491. {
  8492. stb_matcher *matcher = (stb_matcher *) stb_malloc(0,sizeof(*matcher));
  8493. matcher->start_node = 0;
  8494. stb_arr_malloc((void **) &matcher->nodes, matcher);
  8495. matcher->num_charset = 0;
  8496. matcher->match_start = 0;
  8497. matcher->does_lex = 0;
  8498. matcher->dfa_start = STB__DFA_UNDEF;
  8499. stb_arr_malloc((void **) &matcher->dfa, matcher);
  8500. stb_arr_malloc((void **) &matcher->dfa_mapping, matcher);
  8501. stb_arr_malloc((void **) &matcher->dfa_result, matcher);
  8502. stb__add_node(matcher);
  8503. return matcher;
  8504. }
  8505. static void stb__lex_reset(stb_matcher *matcher)
  8506. {
  8507. // flush cached dfa data
  8508. stb_arr_setlen(matcher->dfa, 0);
  8509. stb_arr_setlen(matcher->dfa_mapping, 0);
  8510. stb_arr_setlen(matcher->dfa_result, 0);
  8511. matcher->dfa_start = STB__DFA_UNDEF;
  8512. }
  8513. stb_matcher *stb_regex_matcher(char *regex)
  8514. {
  8515. char *z;
  8516. stb_uint16 end;
  8517. stb_matcher *matcher = stb__alloc_matcher();
  8518. if (*regex == '^') {
  8519. matcher->match_start = 1;
  8520. ++regex;
  8521. }
  8522. z = stb__reg_parse_alt(matcher, matcher->start_node, regex, &end);
  8523. if (!z || *z) {
  8524. stb_free(matcher);
  8525. return NULL;
  8526. }
  8527. ((matcher->nodes)[(int) end]).goal = STB__NFA_STOP_GOAL;
  8528. return matcher;
  8529. }
  8530. stb_matcher *stb_lex_matcher(void)
  8531. {
  8532. stb_matcher *matcher = stb__alloc_matcher();
  8533. matcher->match_start = 1;
  8534. matcher->does_lex = 1;
  8535. return matcher;
  8536. }
  8537. int stb_lex_item(stb_matcher *matcher, const char *regex, int result)
  8538. {
  8539. char *z;
  8540. stb_uint16 end;
  8541. z = stb__reg_parse_alt(matcher, matcher->start_node, (char*) regex, &end);
  8542. if (z == NULL)
  8543. return 0;
  8544. stb__lex_reset(matcher);
  8545. matcher->nodes[(int) end].goal = result;
  8546. return 1;
  8547. }
  8548. int stb_lex_item_wild(stb_matcher *matcher, const char *regex, int result)
  8549. {
  8550. char *z;
  8551. stb_uint16 end;
  8552. z = stb__wild_parse(matcher, matcher->start_node, (char*) regex, &end);
  8553. if (z == NULL)
  8554. return 0;
  8555. stb__lex_reset(matcher);
  8556. matcher->nodes[(int) end].goal = result;
  8557. return 1;
  8558. }
  8559. static void stb__clear(stb_matcher *m, stb_uint16 *list)
  8560. {
  8561. int i;
  8562. for (i=0; i < stb_arr_len(list); ++i)
  8563. m->nodes[(int) list[i]].active = 0;
  8564. }
  8565. static int stb__clear_goalcheck(stb_matcher *m, stb_uint16 *list)
  8566. {
  8567. int i, t=0;
  8568. for (i=0; i < stb_arr_len(list); ++i) {
  8569. t += m->nodes[(int) list[i]].goal;
  8570. m->nodes[(int) list[i]].active = 0;
  8571. }
  8572. return t;
  8573. }
  8574. static stb_uint16 * stb__add_if_inactive(stb_matcher *m, stb_uint16 *list, int n)
  8575. {
  8576. if (!m->nodes[n].active) {
  8577. stb_arr_push(list, n);
  8578. m->nodes[n].active = 1;
  8579. }
  8580. return list;
  8581. }
  8582. static stb_uint16 * stb__eps_closure(stb_matcher *m, stb_uint16 *list)
  8583. {
  8584. int i,n = stb_arr_len(list);
  8585. for(i=0; i < n; ++i) {
  8586. stb_uint16 *e = m->nodes[(int) list[i]].eps;
  8587. if (e) {
  8588. int j,k = stb_arr_len(e);
  8589. for (j=0; j < k; ++j)
  8590. list = stb__add_if_inactive(m, list, e[j]);
  8591. n = stb_arr_len(list);
  8592. }
  8593. }
  8594. return list;
  8595. }
  8596. int stb_matcher_match(stb_matcher *m, char *str)
  8597. {
  8598. int result = 0;
  8599. int i,j,y,z;
  8600. stb_uint16 *previous = NULL;
  8601. stb_uint16 *current = NULL;
  8602. stb_uint16 *temp;
  8603. stb_arr_setsize(previous, 4);
  8604. stb_arr_setsize(current, 4);
  8605. previous = stb__add_if_inactive(m, previous, m->start_node);
  8606. previous = stb__eps_closure(m,previous);
  8607. stb__clear(m, previous);
  8608. while (*str && stb_arr_len(previous)) {
  8609. y = stb_arr_len(previous);
  8610. for (i=0; i < y; ++i) {
  8611. stb_nfa_node *n = &m->nodes[(int) previous[i]];
  8612. z = stb_arr_len(n->out);
  8613. for (j=0; j < z; ++j) {
  8614. if (n->out[j].match >= 0) {
  8615. if (n->out[j].match == *str)
  8616. current = stb__add_if_inactive(m, current, n->out[j].node);
  8617. } else if (n->out[j].match == -1) {
  8618. if (*str != '\n')
  8619. current = stb__add_if_inactive(m, current, n->out[j].node);
  8620. } else if (n->out[j].match < -1) {
  8621. int z = -n->out[j].match - 2;
  8622. if (m->charset[(stb_uint8) *str] & (1 << z))
  8623. current = stb__add_if_inactive(m, current, n->out[j].node);
  8624. }
  8625. }
  8626. }
  8627. stb_arr_setlen(previous, 0);
  8628. temp = previous;
  8629. previous = current;
  8630. current = temp;
  8631. previous = stb__eps_closure(m,previous);
  8632. stb__clear(m, previous);
  8633. ++str;
  8634. }
  8635. // transition to pick up a '$' at the end
  8636. y = stb_arr_len(previous);
  8637. for (i=0; i < y; ++i)
  8638. m->nodes[(int) previous[i]].active = 1;
  8639. for (i=0; i < y; ++i) {
  8640. stb_nfa_node *n = &m->nodes[(int) previous[i]];
  8641. z = stb_arr_len(n->out);
  8642. for (j=0; j < z; ++j) {
  8643. if (n->out[j].match == '\n')
  8644. current = stb__add_if_inactive(m, current, n->out[j].node);
  8645. }
  8646. }
  8647. previous = stb__eps_closure(m,previous);
  8648. stb__clear(m, previous);
  8649. y = stb_arr_len(previous);
  8650. for (i=0; i < y; ++i)
  8651. if (m->nodes[(int) previous[i]].goal)
  8652. result = 1;
  8653. stb_arr_free(previous);
  8654. stb_arr_free(current);
  8655. return result && *str == 0;
  8656. }
  8657. stb_int16 stb__get_dfa_node(stb_matcher *m, stb_uint16 *list)
  8658. {
  8659. stb_uint16 node;
  8660. stb_uint32 data[8], *state, *newstate;
  8661. int i,j,n;
  8662. state = (stb_uint32 *) stb_temp(data, m->num_words_per_dfa * 4);
  8663. memset(state, 0, m->num_words_per_dfa*4);
  8664. n = stb_arr_len(list);
  8665. for (i=0; i < n; ++i) {
  8666. int x = list[i];
  8667. state[x >> 5] |= 1 << (x & 31);
  8668. }
  8669. // @TODO use a hash table
  8670. n = stb_arr_len(m->dfa_mapping);
  8671. i=j=0;
  8672. for(; j < n; ++i, j += m->num_words_per_dfa) {
  8673. // @TODO special case for <= 32
  8674. if (!memcmp(state, m->dfa_mapping + j, m->num_words_per_dfa*4)) {
  8675. node = i;
  8676. goto done;
  8677. }
  8678. }
  8679. assert(stb_arr_len(m->dfa) == i);
  8680. node = i;
  8681. newstate = stb_arr_addn(m->dfa_mapping, m->num_words_per_dfa);
  8682. memcpy(newstate, state, m->num_words_per_dfa*4);
  8683. // set all transitions to 'unknown'
  8684. stb_arr_add(m->dfa);
  8685. memset(m->dfa[i].transition, -1, sizeof(m->dfa[i].transition));
  8686. if (m->does_lex) {
  8687. int result = -1;
  8688. n = stb_arr_len(list);
  8689. for (i=0; i < n; ++i) {
  8690. if (m->nodes[(int) list[i]].goal > result)
  8691. result = m->nodes[(int) list[i]].goal;
  8692. }
  8693. stb_arr_push(m->dfa_result, result);
  8694. }
  8695. done:
  8696. stb_tempfree(data, state);
  8697. return node;
  8698. }
  8699. static int stb__matcher_dfa(stb_matcher *m, char *str_c, int *len)
  8700. {
  8701. stb_uint8 *str = (stb_uint8 *) str_c;
  8702. stb_int16 node,prevnode;
  8703. stb_dfa *trans;
  8704. int match_length = 0;
  8705. stb_int16 match_result=0;
  8706. if (m->dfa_start == STB__DFA_UNDEF) {
  8707. stb_uint16 *list;
  8708. m->num_words_per_dfa = (stb_arr_len(m->nodes)+31) >> 5;
  8709. stb__optimize(m);
  8710. list = stb__add_if_inactive(m, NULL, m->start_node);
  8711. list = stb__eps_closure(m,list);
  8712. if (m->does_lex) {
  8713. m->dfa_start = stb__get_dfa_node(m,list);
  8714. stb__clear(m, list);
  8715. // DON'T allow start state to be a goal state!
  8716. // this allows people to specify regexes that can match 0
  8717. // characters without them actually matching (also we don't
  8718. // check _before_ advancing anyway
  8719. if (m->dfa_start <= STB__DFA_MGOAL)
  8720. m->dfa_start = -(m->dfa_start - STB__DFA_MGOAL);
  8721. } else {
  8722. if (stb__clear_goalcheck(m, list))
  8723. m->dfa_start = STB__DFA_GOAL;
  8724. else
  8725. m->dfa_start = stb__get_dfa_node(m,list);
  8726. }
  8727. stb_arr_free(list);
  8728. }
  8729. prevnode = STB__DFA_UNDEF;
  8730. node = m->dfa_start;
  8731. trans = m->dfa;
  8732. if (m->dfa_start == STB__DFA_GOAL)
  8733. return 1;
  8734. for(;;) {
  8735. assert(node >= STB__DFA_VALID);
  8736. // fast inner DFA loop; especially if STB__DFA_VALID is 0
  8737. do {
  8738. prevnode = node;
  8739. node = trans[node].transition[*str++];
  8740. } while (node >= STB__DFA_VALID);
  8741. assert(node >= STB__DFA_MGOAL - stb_arr_len(m->dfa));
  8742. assert(node < stb_arr_len(m->dfa));
  8743. // special case for lex: need _longest_ match, so notice goal
  8744. // state without stopping
  8745. if (node <= STB__DFA_MGOAL) {
  8746. match_length = (int) (str - (stb_uint8 *) str_c);
  8747. node = -(node - STB__DFA_MGOAL);
  8748. match_result = node;
  8749. continue;
  8750. }
  8751. // slow NFA->DFA conversion
  8752. // or we hit the goal or the end of the string, but those
  8753. // can only happen once per search...
  8754. if (node == STB__DFA_UNDEF) {
  8755. // build a list -- @TODO special case <= 32 states
  8756. // heck, use a more compact data structure for <= 16 and <= 8 ?!
  8757. // @TODO keep states/newstates around instead of reallocating them
  8758. stb_uint16 *states = NULL;
  8759. stb_uint16 *newstates = NULL;
  8760. int i,j,y,z;
  8761. stb_uint32 *flags = &m->dfa_mapping[prevnode * m->num_words_per_dfa];
  8762. assert(prevnode != STB__DFA_UNDEF);
  8763. stb_arr_setsize(states, 4);
  8764. stb_arr_setsize(newstates,4);
  8765. for (j=0; j < m->num_words_per_dfa; ++j) {
  8766. for (i=0; i < 32; ++i) {
  8767. if (*flags & (1 << i))
  8768. stb_arr_push(states, j*32+i);
  8769. }
  8770. ++flags;
  8771. }
  8772. // states is now the states we were in in the previous node;
  8773. // so now we can compute what node it transitions to on str[-1]
  8774. y = stb_arr_len(states);
  8775. for (i=0; i < y; ++i) {
  8776. stb_nfa_node *n = &m->nodes[(int) states[i]];
  8777. z = stb_arr_len(n->out);
  8778. for (j=0; j < z; ++j) {
  8779. if (n->out[j].match >= 0) {
  8780. if (n->out[j].match == str[-1] || (str[-1] == 0 && n->out[j].match == '\n'))
  8781. newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
  8782. } else if (n->out[j].match == -1) {
  8783. if (str[-1] != '\n' && str[-1])
  8784. newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
  8785. } else if (n->out[j].match < -1) {
  8786. int z = -n->out[j].match - 2;
  8787. if (m->charset[str[-1]] & (1 << z))
  8788. newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
  8789. }
  8790. }
  8791. }
  8792. // AND add in the start state!
  8793. if (!m->match_start || (str[-1] == '\n' && !m->does_lex))
  8794. newstates = stb__add_if_inactive(m, newstates, m->start_node);
  8795. // AND epsilon close it
  8796. newstates = stb__eps_closure(m, newstates);
  8797. // if it's a goal state, then that's all there is to it
  8798. if (stb__clear_goalcheck(m, newstates)) {
  8799. if (m->does_lex) {
  8800. match_length = (int) (str - (stb_uint8 *) str_c);
  8801. node = stb__get_dfa_node(m,newstates);
  8802. match_result = node;
  8803. node = -node + STB__DFA_MGOAL;
  8804. trans = m->dfa; // could have gotten realloc()ed
  8805. } else
  8806. node = STB__DFA_GOAL;
  8807. } else if (str[-1] == 0 || stb_arr_len(newstates) == 0) {
  8808. node = STB__DFA_END;
  8809. } else {
  8810. node = stb__get_dfa_node(m,newstates);
  8811. trans = m->dfa; // could have gotten realloc()ed
  8812. }
  8813. trans[prevnode].transition[str[-1]] = node;
  8814. if (node <= STB__DFA_MGOAL)
  8815. node = -(node - STB__DFA_MGOAL);
  8816. stb_arr_free(newstates);
  8817. stb_arr_free(states);
  8818. }
  8819. if (node == STB__DFA_GOAL) {
  8820. return 1;
  8821. }
  8822. if (node == STB__DFA_END) {
  8823. if (m->does_lex) {
  8824. if (match_result) {
  8825. if (len) *len = match_length;
  8826. return m->dfa_result[(int) match_result];
  8827. }
  8828. }
  8829. return 0;
  8830. }
  8831. assert(node != STB__DFA_UNDEF);
  8832. }
  8833. }
  8834. int stb_matcher_find(stb_matcher *m, char *str)
  8835. {
  8836. assert(m->does_lex == 0);
  8837. return stb__matcher_dfa(m, str, NULL);
  8838. }
  8839. int stb_lex(stb_matcher *m, char *str, int *len)
  8840. {
  8841. assert(m->does_lex);
  8842. return stb__matcher_dfa(m, str, len);
  8843. }
  8844. #ifdef STB_PERFECT_HASH
  8845. int stb_regex(char *regex, char *str)
  8846. {
  8847. static stb_perfect p;
  8848. static stb_matcher ** matchers;
  8849. static char ** regexps;
  8850. static char ** regexp_cache;
  8851. static unsigned short *mapping;
  8852. int z = stb_perfect_hash(&p, (int)(size_t) regex);
  8853. if (z >= 0) {
  8854. if (strcmp(regex, regexp_cache[(int) mapping[z]])) {
  8855. int i = mapping[z];
  8856. stb_matcher_free(matchers[i]);
  8857. free(regexp_cache[i]);
  8858. regexps[i] = regex;
  8859. regexp_cache[i] = stb_p_strdup(regex);
  8860. matchers[i] = stb_regex_matcher(regex);
  8861. }
  8862. } else {
  8863. int i,n;
  8864. if (regex == NULL) {
  8865. for (i=0; i < stb_arr_len(matchers); ++i) {
  8866. stb_matcher_free(matchers[i]);
  8867. free(regexp_cache[i]);
  8868. }
  8869. stb_arr_free(matchers);
  8870. stb_arr_free(regexps);
  8871. stb_arr_free(regexp_cache);
  8872. stb_perfect_destroy(&p);
  8873. free(mapping); mapping = NULL;
  8874. return -1;
  8875. }
  8876. stb_arr_push(regexps, regex);
  8877. stb_arr_push(regexp_cache, stb_p_strdup(regex));
  8878. stb_arr_push(matchers, stb_regex_matcher(regex));
  8879. stb_perfect_destroy(&p);
  8880. n = stb_perfect_create(&p, (unsigned int *) (char **) regexps, stb_arr_len(regexps));
  8881. mapping = (unsigned short *) realloc(mapping, n * sizeof(*mapping));
  8882. for (i=0; i < stb_arr_len(regexps); ++i)
  8883. mapping[stb_perfect_hash(&p, (int)(size_t) regexps[i])] = i;
  8884. z = stb_perfect_hash(&p, (int)(size_t) regex);
  8885. }
  8886. return stb_matcher_find(matchers[(int) mapping[z]], str);
  8887. }
  8888. #endif
  8889. #endif // STB_DEFINE
  8890. #if 0
  8891. //////////////////////////////////////////////////////////////////////////////
  8892. //
  8893. // C source-code introspection
  8894. //
  8895. // runtime structure
  8896. typedef struct
  8897. {
  8898. char *name;
  8899. char *type; // base type
  8900. char *comment; // content of comment field
  8901. int size; // size of base type
  8902. int offset; // field offset
  8903. int arrcount[8]; // array sizes; -1 = pointer indirection; 0 = end of list
  8904. } stb_info_field;
  8905. typedef struct
  8906. {
  8907. char *structname;
  8908. int size;
  8909. int num_fields;
  8910. stb_info_field *fields;
  8911. } stb_info_struct;
  8912. extern stb_info_struct stb_introspect_output[];
  8913. //
  8914. STB_EXTERN void stb_introspect_precompiled(stb_info_struct *compiled);
  8915. STB_EXTERN void stb__introspect(char *path, char *file);
  8916. #define stb_introspect_ship() stb__introspect(NULL, NULL, stb__introspect_output)
  8917. #ifdef STB_SHIP
  8918. #define stb_introspect() stb_introspect_ship()
  8919. #define stb_introspect_path(p) stb_introspect_ship()
  8920. #else
  8921. // bootstrapping: define stb_introspect() (or 'path') the first time
  8922. #define stb_introspect() stb__introspect(NULL, __FILE__, NULL)
  8923. #define stb_introspect_auto() stb__introspect(NULL, __FILE__, stb__introspect_output)
  8924. #define stb_introspect_path(p) stb__introspect(p, __FILE__, NULL)
  8925. #define stb_introspect_path(p) stb__introspect(p, __FILE__, NULL)
  8926. #endif
  8927. #ifdef STB_DEFINE
  8928. #ifndef STB_INTROSPECT_CPP
  8929. #ifdef __cplusplus
  8930. #define STB_INTROSPECT_CPP 1
  8931. #else
  8932. #define STB_INTROSPECT_CPP 0
  8933. #endif
  8934. #endif
  8935. void stb_introspect_precompiled(stb_info_struct *compiled)
  8936. {
  8937. }
  8938. static void stb__introspect_filename(char *buffer, char *path)
  8939. {
  8940. #if STB_INTROSPECT_CPP
  8941. stb_p_sprintf(buffer stb_p_size(9999), "%s/stb_introspect.cpp", path);
  8942. #else
  8943. stb_p_sprintf(buffer stb_p_size(9999), "%s/stb_introspect.c", path);
  8944. #endif
  8945. }
  8946. static void stb__introspect_compute(char *path, char *file)
  8947. {
  8948. int i;
  8949. char ** include_list = NULL;
  8950. char ** introspect_list = NULL;
  8951. FILE *f;
  8952. f = stb_p_fopen(file, "w");
  8953. if (!f) return;
  8954. fputs("// if you get compiler errors, change the following 0 to a 1:\n", f);
  8955. fputs("#define STB_INTROSPECT_INVALID 0\n\n", f);
  8956. fputs("// this will force the code to compile, and force the introspector\n", f);
  8957. fputs("// to run and then exit, allowing you to recompile\n\n\n", f);
  8958. fputs("#include \"stb.h\"\n\n",f );
  8959. fputs("#if STB_INTROSPECT_INVALID\n", f);
  8960. fputs(" stb_info_struct stb__introspect_output[] = { (void *) 1 }\n", f);
  8961. fputs("#else\n\n", f);
  8962. for (i=0; i < stb_arr_len(include_list); ++i)
  8963. fprintf(f, " #include \"%s\"\n", include_list[i]);
  8964. fputs(" stb_info_struct stb__introspect_output[] =\n{\n", f);
  8965. for (i=0; i < stb_arr_len(introspect_list); ++i)
  8966. fprintf(f, " stb_introspect_%s,\n", introspect_list[i]);
  8967. fputs(" };\n", f);
  8968. fputs("#endif\n", f);
  8969. fclose(f);
  8970. }
  8971. static stb_info_struct *stb__introspect_info;
  8972. #ifndef STB_SHIP
  8973. #endif
  8974. void stb__introspect(char *path, char *file, stb_info_struct *compiled)
  8975. {
  8976. static int first=1;
  8977. if (!first) return;
  8978. first=0;
  8979. stb__introspect_info = compiled;
  8980. #ifndef STB_SHIP
  8981. if (path || file) {
  8982. int bail_flag = compiled && compiled[0].structname == (void *) 1;
  8983. int needs_building = bail_flag;
  8984. struct stb__stat st;
  8985. char buffer[1024], buffer2[1024];
  8986. if (!path) {
  8987. stb_splitpath(buffer, file, STB_PATH);
  8988. path = buffer;
  8989. }
  8990. // bail if the source path doesn't exist
  8991. if (!stb_fexists(path)) return;
  8992. stb__introspect_filename(buffer2, path);
  8993. // get source/include files timestamps, compare to output-file timestamp;
  8994. // if mismatched, regenerate
  8995. if (stb__stat(buffer2, &st))
  8996. needs_building = STB_TRUE;
  8997. {
  8998. // find any file that contains an introspection command and is newer
  8999. // if needs_building is already true, we don't need to do this test,
  9000. // but we still need these arrays, so go ahead and get them
  9001. char **all[3];
  9002. all[0] = stb_readdir_files_mask(path, "*.h");
  9003. all[1] = stb_readdir_files_mask(path, "*.c");
  9004. all[2] = stb_readdir_files_mask(path, "*.cpp");
  9005. int i,j;
  9006. if (needs_building) {
  9007. for (j=0; j < 3; ++j) {
  9008. for (i=0; i < stb_arr_len(all[j]); ++i) {
  9009. struct stb__stat st2;
  9010. if (!stb__stat(all[j][i], &st2)) {
  9011. if (st.st_mtime < st2.st_mtime) {
  9012. char *z = stb_filec(all[j][i], NULL);
  9013. int found=STB_FALSE;
  9014. while (y) {
  9015. y = strstr(y, "//si");
  9016. if (y && isspace(y[4])) {
  9017. found = STB_TRUE;
  9018. break;
  9019. }
  9020. }
  9021. needs_building = STB_TRUE;
  9022. goto done;
  9023. }
  9024. }
  9025. }
  9026. }
  9027. done:;
  9028. }
  9029. char *z = stb_filec(all[i], NULL), *y = z;
  9030. int found=STB_FALSE;
  9031. while (y) {
  9032. y = strstr(y, "//si");
  9033. if (y && isspace(y[4])) {
  9034. found = STB_TRUE;
  9035. break;
  9036. }
  9037. }
  9038. if (found)
  9039. stb_arr_push(introspect_h, stb_p_strdup(all[i]));
  9040. free(z);
  9041. }
  9042. }
  9043. stb_readdir_free(all);
  9044. if (!needs_building) {
  9045. for (i=0; i < stb_arr_len(introspect_h); ++i) {
  9046. struct stb__stat st2;
  9047. if (!stb__stat(introspect_h[i], &st2))
  9048. if (st.st_mtime < st2.st_mtime)
  9049. needs_building = STB_TRUE;
  9050. }
  9051. }
  9052. if (needs_building) {
  9053. stb__introspect_compute(path, buffer2);
  9054. }
  9055. }
  9056. }
  9057. #endif
  9058. }
  9059. #endif
  9060. #endif
  9061. #ifdef STB_INTROSPECT
  9062. // compile-time code-generator
  9063. #define INTROSPECT(x) int main(int argc, char **argv) { stb__introspect(__FILE__); return 0; }
  9064. #define FILE(x)
  9065. void stb__introspect(char *filename)
  9066. {
  9067. char *file = stb_file(filename, NULL);
  9068. char *s = file, *t, **p;
  9069. char *out_name = "stb_introspect.c";
  9070. char *out_path;
  9071. STB_ARR(char) filelist = NULL;
  9072. int i,n;
  9073. if (!file) stb_fatal("Couldn't open %s", filename);
  9074. out_path = stb_splitpathdup(filename, STB_PATH);
  9075. // search for the macros
  9076. while (*s) {
  9077. char buffer[256];
  9078. while (*s && !isupper(*s)) ++s;
  9079. s = stb_strtok_invert(buffer, s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  9080. s = stb_skipwhite(s);
  9081. if (*s == '(') {
  9082. ++s;
  9083. t = strchr(s, ')');
  9084. if (t == NULL) stb_fatal("Error parsing %s", filename);
  9085. }
  9086. }
  9087. }
  9088. #endif
  9089. //////////////////////////////////////////////////////////////////////////////
  9090. //
  9091. // STB-C sliding-window dictionary compression
  9092. //
  9093. // This uses a DEFLATE-style sliding window, but no bitwise entropy.
  9094. // Everything is on byte boundaries, so you could then apply a byte-wise
  9095. // entropy code, though that's nowhere near as effective.
  9096. //
  9097. // An STB-C stream begins with a 16-byte header:
  9098. // 4 bytes: 0x57 0xBC 0x00 0x00
  9099. // 8 bytes: big-endian size of decompressed data, 64-bits
  9100. // 4 bytes: big-endian size of window (how far back decompressor may need)
  9101. //
  9102. // The following symbols appear in the stream (these were determined ad hoc,
  9103. // not by analysis):
  9104. //
  9105. // [dict] 00000100 yyyyyyyy yyyyyyyy yyyyyyyy xxxxxxxx xxxxxxxx
  9106. // [END] 00000101 11111010 cccccccc cccccccc cccccccc cccccccc
  9107. // [dict] 00000110 yyyyyyyy yyyyyyyy yyyyyyyy xxxxxxxx
  9108. // [literals] 00000111 zzzzzzzz zzzzzzzz
  9109. // [literals] 00001zzz zzzzzzzz
  9110. // [dict] 00010yyy yyyyyyyy yyyyyyyy xxxxxxxx xxxxxxxx
  9111. // [dict] 00011yyy yyyyyyyy yyyyyyyy xxxxxxxx
  9112. // [literals] 001zzzzz
  9113. // [dict] 01yyyyyy yyyyyyyy xxxxxxxx
  9114. // [dict] 1xxxxxxx yyyyyyyy
  9115. //
  9116. // xxxxxxxx: match length - 1
  9117. // yyyyyyyy: backwards distance - 1
  9118. // zzzzzzzz: num literals - 1
  9119. // cccccccc: adler32 checksum of decompressed data
  9120. // (all big-endian)
  9121. STB_EXTERN stb_uint stb_decompress_length(stb_uchar *input);
  9122. STB_EXTERN stb_uint stb_decompress(stb_uchar *out,stb_uchar *in,stb_uint len);
  9123. STB_EXTERN stb_uint stb_compress (stb_uchar *out,stb_uchar *in,stb_uint len);
  9124. STB_EXTERN void stb_compress_window(int z);
  9125. STB_EXTERN void stb_compress_hashsize(unsigned int z);
  9126. STB_EXTERN int stb_compress_tofile(char *filename, char *in, stb_uint len);
  9127. STB_EXTERN int stb_compress_intofile(FILE *f, char *input, stb_uint len);
  9128. STB_EXTERN char *stb_decompress_fromfile(char *filename, stb_uint *len);
  9129. STB_EXTERN int stb_compress_stream_start(FILE *f);
  9130. STB_EXTERN void stb_compress_stream_end(int close);
  9131. STB_EXTERN void stb_write(char *data, int data_len);
  9132. #ifdef STB_DEFINE
  9133. stb_uint stb_decompress_length(stb_uchar *input)
  9134. {
  9135. return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11];
  9136. }
  9137. //////////////////// decompressor ///////////////////////
  9138. // simple implementation that just writes whole thing into big block
  9139. static unsigned char *stb__barrier;
  9140. static unsigned char *stb__barrier2;
  9141. static unsigned char *stb__barrier3;
  9142. static unsigned char *stb__barrier4;
  9143. static stb_uchar *stb__dout;
  9144. static void stb__match(stb_uchar *data, stb_uint length)
  9145. {
  9146. // INVERSE of memmove... write each byte before copying the next...
  9147. assert (stb__dout + length <= stb__barrier);
  9148. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  9149. if (data < stb__barrier4) { stb__dout = stb__barrier+1; return; }
  9150. while (length--) *stb__dout++ = *data++;
  9151. }
  9152. static void stb__lit(stb_uchar *data, stb_uint length)
  9153. {
  9154. assert (stb__dout + length <= stb__barrier);
  9155. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  9156. if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
  9157. memcpy(stb__dout, data, length);
  9158. stb__dout += length;
  9159. }
  9160. #define stb__in2(x) ((i[x] << 8) + i[(x)+1])
  9161. #define stb__in3(x) ((i[x] << 16) + stb__in2((x)+1))
  9162. #define stb__in4(x) ((i[x] << 24) + stb__in3((x)+1))
  9163. static stb_uchar *stb_decompress_token(stb_uchar *i)
  9164. {
  9165. if (*i >= 0x20) { // use fewer if's for cases that expand small
  9166. if (*i >= 0x80) stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;
  9167. else if (*i >= 0x40) stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;
  9168. else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  9169. } else { // more ifs for cases that expand large, since overhead is amortized
  9170. if (*i >= 0x18) stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;
  9171. else if (*i >= 0x10) stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;
  9172. else if (*i >= 0x08) stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1);
  9173. else if (*i == 0x07) stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1);
  9174. else if (*i == 0x06) stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;
  9175. else if (*i == 0x04) stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;
  9176. }
  9177. return i;
  9178. }
  9179. stb_uint stb_decompress(stb_uchar *output, stb_uchar *i, stb_uint length)
  9180. {
  9181. stb_uint olen;
  9182. if (stb__in4(0) != 0x57bC0000) return 0;
  9183. if (stb__in4(4) != 0) return 0; // error! stream is > 4GB
  9184. olen = stb_decompress_length(i);
  9185. stb__barrier2 = i;
  9186. stb__barrier3 = i+length;
  9187. stb__barrier = output + olen;
  9188. stb__barrier4 = output;
  9189. i += 16;
  9190. stb__dout = output;
  9191. while (1) {
  9192. stb_uchar *old_i = i;
  9193. i = stb_decompress_token(i);
  9194. if (i == old_i) {
  9195. if (*i == 0x05 && i[1] == 0xfa) {
  9196. assert(stb__dout == output + olen);
  9197. if (stb__dout != output + olen) return 0;
  9198. if (stb_adler32(1, output, olen) != (stb_uint) stb__in4(2))
  9199. return 0;
  9200. return olen;
  9201. } else {
  9202. assert(0); /* NOTREACHED */
  9203. return 0;
  9204. }
  9205. }
  9206. assert(stb__dout <= output + olen);
  9207. if (stb__dout > output + olen)
  9208. return 0;
  9209. }
  9210. }
  9211. char *stb_decompress_fromfile(char *filename, unsigned int *len)
  9212. {
  9213. unsigned int n;
  9214. char *q;
  9215. unsigned char *p;
  9216. FILE *f = stb_p_fopen(filename, "rb"); if (f == NULL) return NULL;
  9217. fseek(f, 0, SEEK_END);
  9218. n = ftell(f);
  9219. fseek(f, 0, SEEK_SET);
  9220. p = (unsigned char * ) malloc(n); if (p == NULL) return NULL;
  9221. fread(p, 1, n, f);
  9222. fclose(f);
  9223. if (p == NULL) return NULL;
  9224. if (p[0] != 0x57 || p[1] != 0xBc || p[2] || p[3]) { free(p); return NULL; }
  9225. q = (char *) malloc(stb_decompress_length(p)+1);
  9226. if (!q) { free(p); return NULL; }
  9227. *len = stb_decompress((unsigned char *) q, p, n);
  9228. if (*len) q[*len] = 0;
  9229. free(p);
  9230. return q;
  9231. }
  9232. #if 0
  9233. // streaming decompressor
  9234. static struct
  9235. {
  9236. stb__uchar *in_buffer;
  9237. stb__uchar *match;
  9238. stb__uint pending_literals;
  9239. stb__uint pending_match;
  9240. } xx;
  9241. static void stb__match(stb_uchar *data, stb_uint length)
  9242. {
  9243. // INVERSE of memmove... write each byte before copying the next...
  9244. assert (stb__dout + length <= stb__barrier);
  9245. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  9246. if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
  9247. while (length--) *stb__dout++ = *data++;
  9248. }
  9249. static void stb__lit(stb_uchar *data, stb_uint length)
  9250. {
  9251. assert (stb__dout + length <= stb__barrier);
  9252. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  9253. if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
  9254. memcpy(stb__dout, data, length);
  9255. stb__dout += length;
  9256. }
  9257. static void sx_match(stb_uchar *data, stb_uint length)
  9258. {
  9259. xx.match = data;
  9260. xx.pending_match = length;
  9261. }
  9262. static void sx_lit(stb_uchar *data, stb_uint length)
  9263. {
  9264. xx.pending_lit = length;
  9265. }
  9266. static int stb_decompress_token_state(void)
  9267. {
  9268. stb__uchar *i = xx.in_buffer;
  9269. if (*i >= 0x20) { // use fewer if's for cases that expand small
  9270. if (*i >= 0x80) sx_match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;
  9271. else if (*i >= 0x40) sx_match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;
  9272. else /* *i >= 0x20 */ sx_lit(i+1, i[0] - 0x20 + 1), i += 1;
  9273. } else { // more ifs for cases that expand large, since overhead is amortized
  9274. if (*i >= 0x18) sx_match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;
  9275. else if (*i >= 0x10) sx_match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;
  9276. else if (*i >= 0x08) sx_lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2;
  9277. else if (*i == 0x07) sx_lit(i+3, stb__in2(1) + 1), i += 3;
  9278. else if (*i == 0x06) sx_match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;
  9279. else if (*i == 0x04) sx_match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;
  9280. else return 0;
  9281. }
  9282. xx.in_buffer = i;
  9283. return 1;
  9284. }
  9285. #endif
  9286. //////////////////// compressor ///////////////////////
  9287. static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2, stb_uint maxlen)
  9288. {
  9289. stb_uint i;
  9290. for (i=0; i < maxlen; ++i)
  9291. if (m1[i] != m2[i]) return i;
  9292. return i;
  9293. }
  9294. // simple implementation that just takes the source data in a big block
  9295. static stb_uchar *stb__out;
  9296. static FILE *stb__outfile;
  9297. static stb_uint stb__outbytes;
  9298. static void stb__write(unsigned char v)
  9299. {
  9300. fputc(v, stb__outfile);
  9301. ++stb__outbytes;
  9302. }
  9303. #define stb_out(v) (stb__out ? (void)(*stb__out++ = (stb_uchar) (v)) : stb__write((stb_uchar) (v)))
  9304. static void stb_out2(stb_uint v)
  9305. {
  9306. stb_out(v >> 8);
  9307. stb_out(v);
  9308. }
  9309. static void stb_out3(stb_uint v) { stb_out(v >> 16); stb_out(v >> 8); stb_out(v); }
  9310. static void stb_out4(stb_uint v) { stb_out(v >> 24); stb_out(v >> 16);
  9311. stb_out(v >> 8 ); stb_out(v); }
  9312. static void outliterals(stb_uchar *in, ptrdiff_t numlit)
  9313. {
  9314. while (numlit > 65536) {
  9315. outliterals(in,65536);
  9316. in += 65536;
  9317. numlit -= 65536;
  9318. }
  9319. if (numlit == 0) ;
  9320. else if (numlit <= 32) stb_out (0x000020 + (stb_uint) numlit-1);
  9321. else if (numlit <= 2048) stb_out2(0x000800 + (stb_uint) numlit-1);
  9322. else /* numlit <= 65536) */ stb_out3(0x070000 + (stb_uint) numlit-1);
  9323. if (stb__out) {
  9324. memcpy(stb__out,in,numlit);
  9325. stb__out += numlit;
  9326. } else
  9327. fwrite(in, 1, numlit, stb__outfile);
  9328. }
  9329. static int stb__window = 0x40000; // 256K
  9330. void stb_compress_window(int z)
  9331. {
  9332. if (z >= 0x1000000) z = 0x1000000; // limit of implementation
  9333. if (z < 0x100) z = 0x100; // insanely small
  9334. stb__window = z;
  9335. }
  9336. static int stb_not_crap(int best, int dist)
  9337. {
  9338. return ((best > 2 && dist <= 0x00100)
  9339. || (best > 5 && dist <= 0x04000)
  9340. || (best > 7 && dist <= 0x80000));
  9341. }
  9342. static stb_uint stb__hashsize = 32768;
  9343. void stb_compress_hashsize(unsigned int y)
  9344. {
  9345. unsigned int z = 1024;
  9346. while (z < y) z <<= 1;
  9347. stb__hashsize = z >> 2; // pass in bytes, store #pointers
  9348. }
  9349. // note that you can play with the hashing functions all you
  9350. // want without needing to change the decompressor
  9351. #define stb__hc(q,h,c) (((h) << 7) + ((h) >> 25) + q[c])
  9352. #define stb__hc2(q,h,c,d) (((h) << 14) + ((h) >> 18) + (q[c] << 7) + q[d])
  9353. #define stb__hc3(q,c,d,e) ((q[c] << 14) + (q[d] << 7) + q[e])
  9354. static stb_uint32 stb__running_adler;
  9355. static int stb_compress_chunk(stb_uchar *history,
  9356. stb_uchar *start,
  9357. stb_uchar *end,
  9358. int length,
  9359. int *pending_literals,
  9360. stb_uchar **chash,
  9361. stb_uint mask)
  9362. {
  9363. int window = stb__window;
  9364. stb_uint match_max;
  9365. stb_uchar *lit_start = start - *pending_literals;
  9366. stb_uchar *q = start;
  9367. #define STB__SCRAMBLE(h) (((h) + ((h) >> 16)) & mask)
  9368. // stop short of the end so we don't scan off the end doing
  9369. // the hashing; this means we won't compress the last few bytes
  9370. // unless they were part of something longer
  9371. while (q < start+length && q+12 < end) {
  9372. int m;
  9373. stb_uint h1,h2,h3,h4, h;
  9374. stb_uchar *t;
  9375. int best = 2, dist=0;
  9376. if (q+65536 > end)
  9377. match_max = (stb_uint) (end-q);
  9378. else
  9379. match_max = 65536u;
  9380. #define stb__nc(b,d) ((d) <= window && ((b) > 9 || stb_not_crap(b,d)))
  9381. #define STB__TRY(t,p) /* avoid retrying a match we already tried */ \
  9382. if (p ? dist != (int) (q-t) : 1) \
  9383. if ((m = (int) stb_matchlen(t, q, match_max)) > best)\
  9384. if (stb__nc(m,(int) (q-(t)))) \
  9385. best = m, dist = (int) (q - (t))
  9386. // rather than search for all matches, only try 4 candidate locations,
  9387. // chosen based on 4 different hash functions of different lengths.
  9388. // this strategy is inspired by LZO; hashing is unrolled here using the
  9389. // 'hc' macro
  9390. h = stb__hc3(q,0, 1, 2); h1 = STB__SCRAMBLE(h);
  9391. t = chash[h1]; if (t) STB__TRY(t,0);
  9392. h = stb__hc2(q,h, 3, 4); h2 = STB__SCRAMBLE(h);
  9393. h = stb__hc2(q,h, 5, 6); t = chash[h2]; if (t) STB__TRY(t,1);
  9394. h = stb__hc2(q,h, 7, 8); h3 = STB__SCRAMBLE(h);
  9395. h = stb__hc2(q,h, 9,10); t = chash[h3]; if (t) STB__TRY(t,1);
  9396. h = stb__hc2(q,h,11,12); h4 = STB__SCRAMBLE(h);
  9397. t = chash[h4]; if (t) STB__TRY(t,1);
  9398. // because we use a shared hash table, can only update it
  9399. // _after_ we've probed all of them
  9400. chash[h1] = chash[h2] = chash[h3] = chash[h4] = q;
  9401. if (best > 2)
  9402. assert(dist > 0);
  9403. // see if our best match qualifies
  9404. if (best < 3) { // fast path literals
  9405. ++q;
  9406. } else if (best > 2 && best <= 0x80 && dist <= 0x100) {
  9407. outliterals(lit_start, q-lit_start); lit_start = (q += best);
  9408. stb_out(0x80 + best-1);
  9409. stb_out(dist-1);
  9410. } else if (best > 5 && best <= 0x100 && dist <= 0x4000) {
  9411. outliterals(lit_start, q-lit_start); lit_start = (q += best);
  9412. stb_out2(0x4000 + dist-1);
  9413. stb_out(best-1);
  9414. } else if (best > 7 && best <= 0x100 && dist <= 0x80000) {
  9415. outliterals(lit_start, q-lit_start); lit_start = (q += best);
  9416. stb_out3(0x180000 + dist-1);
  9417. stb_out(best-1);
  9418. } else if (best > 8 && best <= 0x10000 && dist <= 0x80000) {
  9419. outliterals(lit_start, q-lit_start); lit_start = (q += best);
  9420. stb_out3(0x100000 + dist-1);
  9421. stb_out2(best-1);
  9422. } else if (best > 9 && dist <= 0x1000000) {
  9423. if (best > 65536) best = 65536;
  9424. outliterals(lit_start, q-lit_start); lit_start = (q += best);
  9425. if (best <= 0x100) {
  9426. stb_out(0x06);
  9427. stb_out3(dist-1);
  9428. stb_out(best-1);
  9429. } else {
  9430. stb_out(0x04);
  9431. stb_out3(dist-1);
  9432. stb_out2(best-1);
  9433. }
  9434. } else { // fallback literals if no match was a balanced tradeoff
  9435. ++q;
  9436. }
  9437. }
  9438. // if we didn't get all the way, add the rest to literals
  9439. if (q-start < length)
  9440. q = start+length;
  9441. // the literals are everything from lit_start to q
  9442. *pending_literals = (int) (q - lit_start);
  9443. stb__running_adler = stb_adler32(stb__running_adler, start, (int) (q - start));
  9444. return (int) (q - start);
  9445. }
  9446. static int stb_compress_inner(stb_uchar *input, stb_uint length)
  9447. {
  9448. int literals = 0;
  9449. stb_uint len,i;
  9450. stb_uchar **chash;
  9451. chash = (stb_uchar**) malloc(stb__hashsize * sizeof(stb_uchar*));
  9452. if (chash == NULL) return 0; // failure
  9453. for (i=0; i < stb__hashsize; ++i)
  9454. chash[i] = NULL;
  9455. // stream signature
  9456. stb_out(0x57); stb_out(0xbc);
  9457. stb_out2(0);
  9458. stb_out4(0); // 64-bit length requires 32-bit leading 0
  9459. stb_out4(length);
  9460. stb_out4(stb__window);
  9461. stb__running_adler = 1;
  9462. len = stb_compress_chunk(input, input, input+length, length, &literals, chash, stb__hashsize-1);
  9463. assert(len == length);
  9464. outliterals(input+length - literals, literals);
  9465. free(chash);
  9466. stb_out2(0x05fa); // end opcode
  9467. stb_out4(stb__running_adler);
  9468. return 1; // success
  9469. }
  9470. stb_uint stb_compress(stb_uchar *out, stb_uchar *input, stb_uint length)
  9471. {
  9472. stb__out = out;
  9473. stb__outfile = NULL;
  9474. stb_compress_inner(input, length);
  9475. return (stb_uint) (stb__out - out);
  9476. }
  9477. int stb_compress_tofile(char *filename, char *input, unsigned int length)
  9478. {
  9479. //int maxlen = length + 512 + (length >> 2); // total guess
  9480. //char *buffer = (char *) malloc(maxlen);
  9481. //int blen = stb_compress((stb_uchar*)buffer, (stb_uchar*)input, length);
  9482. stb__out = NULL;
  9483. stb__outfile = stb_p_fopen(filename, "wb");
  9484. if (!stb__outfile) return 0;
  9485. stb__outbytes = 0;
  9486. if (!stb_compress_inner((stb_uchar*)input, length))
  9487. return 0;
  9488. fclose(stb__outfile);
  9489. return stb__outbytes;
  9490. }
  9491. int stb_compress_intofile(FILE *f, char *input, unsigned int length)
  9492. {
  9493. //int maxlen = length + 512 + (length >> 2); // total guess
  9494. //char *buffer = (char*)malloc(maxlen);
  9495. //int blen = stb_compress((stb_uchar*)buffer, (stb_uchar*)input, length);
  9496. stb__out = NULL;
  9497. stb__outfile = f;
  9498. if (!stb__outfile) return 0;
  9499. stb__outbytes = 0;
  9500. if (!stb_compress_inner((stb_uchar*)input, length))
  9501. return 0;
  9502. return stb__outbytes;
  9503. }
  9504. ////////////////////// streaming I/O version /////////////////////
  9505. static size_t stb_out_backpatch_id(void)
  9506. {
  9507. if (stb__out)
  9508. return (size_t) stb__out;
  9509. else
  9510. return ftell(stb__outfile);
  9511. }
  9512. static void stb_out_backpatch(size_t id, stb_uint value)
  9513. {
  9514. stb_uchar data[4] = { (stb_uchar)(value >> 24), (stb_uchar)(value >> 16), (stb_uchar)(value >> 8), (stb_uchar)(value) };
  9515. if (stb__out) {
  9516. memcpy((void *) id, data, 4);
  9517. } else {
  9518. stb_uint where = ftell(stb__outfile);
  9519. fseek(stb__outfile, (long) id, SEEK_SET);
  9520. fwrite(data, 4, 1, stb__outfile);
  9521. fseek(stb__outfile, where, SEEK_SET);
  9522. }
  9523. }
  9524. // ok, the wraparound buffer was a total failure. let's instead
  9525. // use a copying-in-place buffer, which lets us share the code.
  9526. // This is way less efficient but it'll do for now.
  9527. static struct
  9528. {
  9529. stb_uchar *buffer;
  9530. int size; // physical size of buffer in bytes
  9531. int valid; // amount of valid data in bytes
  9532. int start; // bytes of data already output
  9533. int window;
  9534. int fsize;
  9535. int pending_literals; // bytes not-quite output but counted in start
  9536. int length_id;
  9537. stb_uint total_bytes;
  9538. stb_uchar **chash;
  9539. stb_uint hashmask;
  9540. } xtb;
  9541. static int stb_compress_streaming_start(void)
  9542. {
  9543. stb_uint i;
  9544. xtb.size = stb__window * 3;
  9545. xtb.buffer = (stb_uchar*)malloc(xtb.size);
  9546. if (!xtb.buffer) return 0;
  9547. xtb.chash = (stb_uchar**)malloc(sizeof(*xtb.chash) * stb__hashsize);
  9548. if (!xtb.chash) {
  9549. free(xtb.buffer);
  9550. return 0;
  9551. }
  9552. for (i=0; i < stb__hashsize; ++i)
  9553. xtb.chash[i] = NULL;
  9554. xtb.hashmask = stb__hashsize-1;
  9555. xtb.valid = 0;
  9556. xtb.start = 0;
  9557. xtb.window = stb__window;
  9558. xtb.fsize = stb__window;
  9559. xtb.pending_literals = 0;
  9560. xtb.total_bytes = 0;
  9561. // stream signature
  9562. stb_out(0x57); stb_out(0xbc); stb_out2(0);
  9563. stb_out4(0); // 64-bit length requires 32-bit leading 0
  9564. xtb.length_id = (int) stb_out_backpatch_id();
  9565. stb_out4(0); // we don't know the output length yet
  9566. stb_out4(stb__window);
  9567. stb__running_adler = 1;
  9568. return 1;
  9569. }
  9570. static int stb_compress_streaming_end(void)
  9571. {
  9572. // flush out any remaining data
  9573. stb_compress_chunk(xtb.buffer, xtb.buffer+xtb.start, xtb.buffer+xtb.valid,
  9574. xtb.valid-xtb.start, &xtb.pending_literals, xtb.chash, xtb.hashmask);
  9575. // write out pending literals
  9576. outliterals(xtb.buffer + xtb.valid - xtb.pending_literals, xtb.pending_literals);
  9577. stb_out2(0x05fa); // end opcode
  9578. stb_out4(stb__running_adler);
  9579. stb_out_backpatch(xtb.length_id, xtb.total_bytes);
  9580. free(xtb.buffer);
  9581. free(xtb.chash);
  9582. return 1;
  9583. }
  9584. void stb_write(char *data, int data_len)
  9585. {
  9586. stb_uint i;
  9587. // @TODO: fast path for filling the buffer and doing nothing else
  9588. // if (xtb.valid + data_len < xtb.size)
  9589. xtb.total_bytes += data_len;
  9590. while (data_len) {
  9591. // fill buffer
  9592. if (xtb.valid < xtb.size) {
  9593. int amt = xtb.size - xtb.valid;
  9594. if (data_len < amt) amt = data_len;
  9595. memcpy(xtb.buffer + xtb.valid, data, amt);
  9596. data_len -= amt;
  9597. data += amt;
  9598. xtb.valid += amt;
  9599. }
  9600. if (xtb.valid < xtb.size)
  9601. return;
  9602. // at this point, the buffer is full
  9603. // if we can process some data, go for it; make sure
  9604. // we leave an 'fsize's worth of data, though
  9605. if (xtb.start + xtb.fsize < xtb.valid) {
  9606. int amount = (xtb.valid - xtb.fsize) - xtb.start;
  9607. int n;
  9608. assert(amount > 0);
  9609. n = stb_compress_chunk(xtb.buffer, xtb.buffer + xtb.start, xtb.buffer + xtb.valid,
  9610. amount, &xtb.pending_literals, xtb.chash, xtb.hashmask);
  9611. xtb.start += n;
  9612. }
  9613. assert(xtb.start + xtb.fsize >= xtb.valid);
  9614. // at this point, our future size is too small, so we
  9615. // need to flush some history. we, in fact, flush exactly
  9616. // one window's worth of history
  9617. {
  9618. int flush = xtb.window;
  9619. assert(xtb.start >= flush);
  9620. assert(xtb.valid >= flush);
  9621. // if 'pending literals' extends back into the shift region,
  9622. // write them out
  9623. if (xtb.start - xtb.pending_literals < flush) {
  9624. outliterals(xtb.buffer + xtb.start - xtb.pending_literals, xtb.pending_literals);
  9625. xtb.pending_literals = 0;
  9626. }
  9627. // now shift the window
  9628. memmove(xtb.buffer, xtb.buffer + flush, xtb.valid - flush);
  9629. xtb.start -= flush;
  9630. xtb.valid -= flush;
  9631. for (i=0; i <= xtb.hashmask; ++i)
  9632. if (xtb.chash[i] < xtb.buffer + flush)
  9633. xtb.chash[i] = NULL;
  9634. else
  9635. xtb.chash[i] -= flush;
  9636. }
  9637. // and now that we've made room for more data, go back to the top
  9638. }
  9639. }
  9640. int stb_compress_stream_start(FILE *f)
  9641. {
  9642. stb__out = NULL;
  9643. stb__outfile = f;
  9644. if (f == NULL)
  9645. return 0;
  9646. if (!stb_compress_streaming_start())
  9647. return 0;
  9648. return 1;
  9649. }
  9650. void stb_compress_stream_end(int close)
  9651. {
  9652. stb_compress_streaming_end();
  9653. if (close && stb__outfile) {
  9654. fclose(stb__outfile);
  9655. }
  9656. }
  9657. #endif // STB_DEFINE
  9658. //////////////////////////////////////////////////////////////////////////////
  9659. //
  9660. // File abstraction... tired of not having this... we can write
  9661. // compressors to be layers over these that auto-close their children.
  9662. typedef struct stbfile
  9663. {
  9664. int (*getbyte)(struct stbfile *); // -1 on EOF
  9665. unsigned int (*getdata)(struct stbfile *, void *block, unsigned int len);
  9666. int (*putbyte)(struct stbfile *, int byte);
  9667. unsigned int (*putdata)(struct stbfile *, void *block, unsigned int len);
  9668. unsigned int (*size)(struct stbfile *);
  9669. unsigned int (*tell)(struct stbfile *);
  9670. void (*backpatch)(struct stbfile *, unsigned int tell, void *block, unsigned int len);
  9671. void (*close)(struct stbfile *);
  9672. FILE *f; // file to fread/fwrite
  9673. unsigned char *buffer; // input/output buffer
  9674. unsigned char *indata, *inend; // input buffer
  9675. union {
  9676. int various;
  9677. void *ptr;
  9678. };
  9679. } stbfile;
  9680. STB_EXTERN unsigned int stb_getc(stbfile *f); // read
  9681. STB_EXTERN int stb_putc(stbfile *f, int ch); // write
  9682. STB_EXTERN unsigned int stb_getdata(stbfile *f, void *buffer, unsigned int len); // read
  9683. STB_EXTERN unsigned int stb_putdata(stbfile *f, void *buffer, unsigned int len); // write
  9684. STB_EXTERN unsigned int stb_tell(stbfile *f); // read
  9685. STB_EXTERN unsigned int stb_size(stbfile *f); // read/write
  9686. STB_EXTERN void stb_backpatch(stbfile *f, unsigned int tell, void *buffer, unsigned int len); // write
  9687. #ifdef STB_DEFINE
  9688. unsigned int stb_getc(stbfile *f) { return f->getbyte(f); }
  9689. int stb_putc(stbfile *f, int ch) { return f->putbyte(f, ch); }
  9690. unsigned int stb_getdata(stbfile *f, void *buffer, unsigned int len)
  9691. {
  9692. return f->getdata(f, buffer, len);
  9693. }
  9694. unsigned int stb_putdata(stbfile *f, void *buffer, unsigned int len)
  9695. {
  9696. return f->putdata(f, buffer, len);
  9697. }
  9698. void stb_close(stbfile *f)
  9699. {
  9700. f->close(f);
  9701. free(f);
  9702. }
  9703. unsigned int stb_tell(stbfile *f) { return f->tell(f); }
  9704. unsigned int stb_size(stbfile *f) { return f->size(f); }
  9705. void stb_backpatch(stbfile *f, unsigned int tell, void *buffer, unsigned int len)
  9706. {
  9707. f->backpatch(f,tell,buffer,len);
  9708. }
  9709. // FILE * implementation
  9710. static int stb__fgetbyte(stbfile *f) { return fgetc(f->f); }
  9711. static int stb__fputbyte(stbfile *f, int ch) { return fputc(ch, f->f)==0; }
  9712. static unsigned int stb__fgetdata(stbfile *f, void *buffer, unsigned int len) { return (unsigned int) fread(buffer,1,len,f->f); }
  9713. static unsigned int stb__fputdata(stbfile *f, void *buffer, unsigned int len) { return (unsigned int) fwrite(buffer,1,len,f->f); }
  9714. static unsigned int stb__fsize(stbfile *f) { return (unsigned int) stb_filelen(f->f); }
  9715. static unsigned int stb__ftell(stbfile *f) { return (unsigned int) ftell(f->f); }
  9716. static void stb__fbackpatch(stbfile *f, unsigned int where, void *buffer, unsigned int len)
  9717. {
  9718. fseek(f->f, where, SEEK_SET);
  9719. fwrite(buffer, 1, len, f->f);
  9720. fseek(f->f, 0, SEEK_END);
  9721. }
  9722. static void stb__fclose(stbfile *f) { fclose(f->f); }
  9723. stbfile *stb_openf(FILE *f)
  9724. {
  9725. stbfile m = { stb__fgetbyte, stb__fgetdata,
  9726. stb__fputbyte, stb__fputdata,
  9727. stb__fsize, stb__ftell, stb__fbackpatch, stb__fclose,
  9728. 0,0,0, };
  9729. stbfile *z = (stbfile *) malloc(sizeof(*z));
  9730. if (z) {
  9731. *z = m;
  9732. z->f = f;
  9733. }
  9734. return z;
  9735. }
  9736. static int stb__nogetbyte(stbfile *f) { assert(0); return -1; }
  9737. static unsigned int stb__nogetdata(stbfile *f, void *buffer, unsigned int len) { assert(0); return 0; }
  9738. static int stb__noputbyte(stbfile *f, int ch) { assert(0); return 0; }
  9739. static unsigned int stb__noputdata(stbfile *f, void *buffer, unsigned int len) { assert(0); return 0; }
  9740. static void stb__nobackpatch(stbfile *f, unsigned int where, void *buffer, unsigned int len) { assert(0); }
  9741. static int stb__bgetbyte(stbfile *s)
  9742. {
  9743. if (s->indata < s->inend)
  9744. return *s->indata++;
  9745. else
  9746. return -1;
  9747. }
  9748. static unsigned int stb__bgetdata(stbfile *s, void *buffer, unsigned int len)
  9749. {
  9750. if (s->indata + len > s->inend)
  9751. len = (unsigned int) (s->inend - s->indata);
  9752. memcpy(buffer, s->indata, len);
  9753. s->indata += len;
  9754. return len;
  9755. }
  9756. static unsigned int stb__bsize(stbfile *s) { return (unsigned int) (s->inend - s->buffer); }
  9757. static unsigned int stb__btell(stbfile *s) { return (unsigned int) (s->indata - s->buffer); }
  9758. static void stb__bclose(stbfile *s)
  9759. {
  9760. if (s->various)
  9761. free(s->buffer);
  9762. }
  9763. stbfile *stb_open_inbuffer(void *buffer, unsigned int len)
  9764. {
  9765. stbfile m = { stb__bgetbyte, stb__bgetdata,
  9766. stb__noputbyte, stb__noputdata,
  9767. stb__bsize, stb__btell, stb__nobackpatch, stb__bclose };
  9768. stbfile *z = (stbfile *) malloc(sizeof(*z));
  9769. if (z) {
  9770. *z = m;
  9771. z->buffer = (unsigned char *) buffer;
  9772. z->indata = z->buffer;
  9773. z->inend = z->indata + len;
  9774. }
  9775. return z;
  9776. }
  9777. stbfile *stb_open_inbuffer_free(void *buffer, unsigned int len)
  9778. {
  9779. stbfile *z = stb_open_inbuffer(buffer, len);
  9780. if (z)
  9781. z->various = 1; // free
  9782. return z;
  9783. }
  9784. #ifndef STB_VERSION
  9785. // if we've been cut-and-pasted elsewhere, you get a limited
  9786. // version of stb_open, without the 'k' flag and utf8 support
  9787. static void stb__fclose2(stbfile *f)
  9788. {
  9789. fclose(f->f);
  9790. }
  9791. stbfile *stb_open(char *filename, char *mode)
  9792. {
  9793. FILE *f = stb_p_fopen(filename, mode);
  9794. stbfile *s;
  9795. if (f == NULL) return NULL;
  9796. s = stb_openf(f);
  9797. if (s)
  9798. s->close = stb__fclose2;
  9799. return s;
  9800. }
  9801. #else
  9802. // the full version depends on some code in stb.h; this
  9803. // also includes the memory buffer output format implemented with stb_arr
  9804. static void stb__fclose2(stbfile *f)
  9805. {
  9806. stb_fclose(f->f, f->various);
  9807. }
  9808. stbfile *stb_open(char *filename, char *mode)
  9809. {
  9810. FILE *f = stb_fopen(filename, mode[0] == 'k' ? mode+1 : mode);
  9811. stbfile *s;
  9812. if (f == NULL) return NULL;
  9813. s = stb_openf(f);
  9814. if (s) {
  9815. s->close = stb__fclose2;
  9816. s->various = mode[0] == 'k' ? stb_keep_if_different : stb_keep_yes;
  9817. }
  9818. return s;
  9819. }
  9820. static int stb__aputbyte(stbfile *f, int ch)
  9821. {
  9822. stb_arr_push(f->buffer, ch);
  9823. return 1;
  9824. }
  9825. static unsigned int stb__aputdata(stbfile *f, void *data, unsigned int len)
  9826. {
  9827. memcpy(stb_arr_addn(f->buffer, (int) len), data, len);
  9828. return len;
  9829. }
  9830. static unsigned int stb__asize(stbfile *f) { return stb_arr_len(f->buffer); }
  9831. static void stb__abackpatch(stbfile *f, unsigned int where, void *data, unsigned int len)
  9832. {
  9833. memcpy(f->buffer+where, data, len);
  9834. }
  9835. static void stb__aclose(stbfile *f)
  9836. {
  9837. *(unsigned char **) f->ptr = f->buffer;
  9838. }
  9839. stbfile *stb_open_outbuffer(unsigned char **update_on_close)
  9840. {
  9841. stbfile m = { stb__nogetbyte, stb__nogetdata,
  9842. stb__aputbyte, stb__aputdata,
  9843. stb__asize, stb__asize, stb__abackpatch, stb__aclose };
  9844. stbfile *z = (stbfile *) malloc(sizeof(*z));
  9845. if (z) {
  9846. z->ptr = update_on_close;
  9847. *z = m;
  9848. }
  9849. return z;
  9850. }
  9851. #endif
  9852. #endif
  9853. //////////////////////////////////////////////////////////////////////////////
  9854. //
  9855. // Arithmetic coder... based on cbloom's notes on the subject, should be
  9856. // less code than a huffman code.
  9857. typedef struct
  9858. {
  9859. unsigned int range_low;
  9860. unsigned int range_high;
  9861. unsigned int code, range; // decode
  9862. int buffered_u8;
  9863. int pending_ffs;
  9864. stbfile *output;
  9865. } stb_arith;
  9866. STB_EXTERN void stb_arith_init_encode(stb_arith *a, stbfile *out);
  9867. STB_EXTERN void stb_arith_init_decode(stb_arith *a, stbfile *in);
  9868. STB_EXTERN stbfile *stb_arith_encode_close(stb_arith *a);
  9869. STB_EXTERN stbfile *stb_arith_decode_close(stb_arith *a);
  9870. STB_EXTERN void stb_arith_encode(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq);
  9871. STB_EXTERN void stb_arith_encode_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq);
  9872. STB_EXTERN unsigned int stb_arith_decode_value(stb_arith *a, unsigned int totalfreq);
  9873. STB_EXTERN void stb_arith_decode_advance(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq);
  9874. STB_EXTERN unsigned int stb_arith_decode_value_log2(stb_arith *a, unsigned int totalfreq2);
  9875. STB_EXTERN void stb_arith_decode_advance_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq);
  9876. STB_EXTERN void stb_arith_encode_byte(stb_arith *a, int byte);
  9877. STB_EXTERN int stb_arith_decode_byte(stb_arith *a);
  9878. // this is a memory-inefficient way of doing things, but it's
  9879. // fast(?) and simple
  9880. typedef struct
  9881. {
  9882. unsigned short cumfreq;
  9883. unsigned short samples;
  9884. } stb_arith_symstate_item;
  9885. typedef struct
  9886. {
  9887. int num_sym;
  9888. unsigned int pow2;
  9889. int countdown;
  9890. stb_arith_symstate_item data[1];
  9891. } stb_arith_symstate;
  9892. #ifdef STB_DEFINE
  9893. void stb_arith_init_encode(stb_arith *a, stbfile *out)
  9894. {
  9895. a->range_low = 0;
  9896. a->range_high = 0xffffffff;
  9897. a->pending_ffs = -1; // means no buffered character currently, to speed up normal case
  9898. a->output = out;
  9899. }
  9900. static void stb__arith_carry(stb_arith *a)
  9901. {
  9902. int i;
  9903. assert(a->pending_ffs != -1); // can't carry with no data
  9904. stb_putc(a->output, a->buffered_u8);
  9905. for (i=0; i < a->pending_ffs; ++i)
  9906. stb_putc(a->output, 0);
  9907. }
  9908. static void stb__arith_putbyte(stb_arith *a, int byte)
  9909. {
  9910. if (a->pending_ffs) {
  9911. if (a->pending_ffs == -1) { // means no buffered data; encoded for fast path efficiency
  9912. if (byte == 0xff)
  9913. stb_putc(a->output, byte); // just write it immediately
  9914. else {
  9915. a->buffered_u8 = byte;
  9916. a->pending_ffs = 0;
  9917. }
  9918. } else if (byte == 0xff) {
  9919. ++a->pending_ffs;
  9920. } else {
  9921. int i;
  9922. stb_putc(a->output, a->buffered_u8);
  9923. for (i=0; i < a->pending_ffs; ++i)
  9924. stb_putc(a->output, 0xff);
  9925. }
  9926. } else if (byte == 0xff) {
  9927. ++a->pending_ffs;
  9928. } else {
  9929. // fast path
  9930. stb_putc(a->output, a->buffered_u8);
  9931. a->buffered_u8 = byte;
  9932. }
  9933. }
  9934. static void stb__arith_flush(stb_arith *a)
  9935. {
  9936. if (a->pending_ffs >= 0) {
  9937. int i;
  9938. stb_putc(a->output, a->buffered_u8);
  9939. for (i=0; i < a->pending_ffs; ++i)
  9940. stb_putc(a->output, 0xff);
  9941. }
  9942. }
  9943. static void stb__renorm_encoder(stb_arith *a)
  9944. {
  9945. stb__arith_putbyte(a, a->range_low >> 24);
  9946. a->range_low <<= 8;
  9947. a->range_high = (a->range_high << 8) | 0xff;
  9948. }
  9949. static void stb__renorm_decoder(stb_arith *a)
  9950. {
  9951. int c = stb_getc(a->output);
  9952. a->code = (a->code << 8) + (c >= 0 ? c : 0); // if EOF, insert 0
  9953. }
  9954. void stb_arith_encode(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq)
  9955. {
  9956. unsigned int range = a->range_high - a->range_low;
  9957. unsigned int old = a->range_low;
  9958. range /= totalfreq;
  9959. a->range_low += range * cumfreq;
  9960. a->range_high = a->range_low + range*freq;
  9961. if (a->range_low < old)
  9962. stb__arith_carry(a);
  9963. while (a->range_high - a->range_low < 0x1000000)
  9964. stb__renorm_encoder(a);
  9965. }
  9966. void stb_arith_encode_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq)
  9967. {
  9968. unsigned int range = a->range_high - a->range_low;
  9969. unsigned int old = a->range_low;
  9970. range >>= totalfreq2;
  9971. a->range_low += range * cumfreq;
  9972. a->range_high = a->range_low + range*freq;
  9973. if (a->range_low < old)
  9974. stb__arith_carry(a);
  9975. while (a->range_high - a->range_low < 0x1000000)
  9976. stb__renorm_encoder(a);
  9977. }
  9978. unsigned int stb_arith_decode_value(stb_arith *a, unsigned int totalfreq)
  9979. {
  9980. unsigned int freqsize = a->range / totalfreq;
  9981. unsigned int z = a->code / freqsize;
  9982. return z >= totalfreq ? totalfreq-1 : z;
  9983. }
  9984. void stb_arith_decode_advance(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq)
  9985. {
  9986. unsigned int freqsize = a->range / totalfreq; // @OPTIMIZE, share with above divide somehow?
  9987. a->code -= freqsize * cumfreq;
  9988. a->range = freqsize * freq;
  9989. while (a->range < 0x1000000)
  9990. stb__renorm_decoder(a);
  9991. }
  9992. unsigned int stb_arith_decode_value_log2(stb_arith *a, unsigned int totalfreq2)
  9993. {
  9994. unsigned int freqsize = a->range >> totalfreq2;
  9995. unsigned int z = a->code / freqsize;
  9996. return z >= (1U<<totalfreq2) ? (1U<<totalfreq2)-1 : z;
  9997. }
  9998. void stb_arith_decode_advance_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq)
  9999. {
  10000. unsigned int freqsize = a->range >> totalfreq2;
  10001. a->code -= freqsize * cumfreq;
  10002. a->range = freqsize * freq;
  10003. while (a->range < 0x1000000)
  10004. stb__renorm_decoder(a);
  10005. }
  10006. stbfile *stb_arith_encode_close(stb_arith *a)
  10007. {
  10008. // put exactly as many bytes as we'll read, so we can turn on/off arithmetic coding in a stream
  10009. stb__arith_putbyte(a, a->range_low >> 24);
  10010. stb__arith_putbyte(a, a->range_low >> 16);
  10011. stb__arith_putbyte(a, a->range_low >> 8);
  10012. stb__arith_putbyte(a, a->range_low >> 0);
  10013. stb__arith_flush(a);
  10014. return a->output;
  10015. }
  10016. stbfile *stb_arith_decode_close(stb_arith *a)
  10017. {
  10018. return a->output;
  10019. }
  10020. #endif
  10021. //////////////////////////////////////////////////////////////////////////////
  10022. //
  10023. // Threads
  10024. //
  10025. #ifndef _WIN32
  10026. #ifdef STB_THREADS
  10027. #error "threads not implemented except for Windows"
  10028. #endif
  10029. #endif
  10030. // call this function to free any global variables for memory testing
  10031. STB_EXTERN void stb_thread_cleanup(void);
  10032. typedef void * (*stb_thread_func)(void *);
  10033. // do not rely on these types, this is an implementation detail.
  10034. // compare against STB_THREAD_NULL and ST_SEMAPHORE_NULL
  10035. typedef void *stb_thread;
  10036. typedef void *stb_semaphore;
  10037. typedef void *stb_mutex;
  10038. typedef struct stb__sync *stb_sync;
  10039. #define STB_SEMAPHORE_NULL NULL
  10040. #define STB_THREAD_NULL NULL
  10041. #define STB_MUTEX_NULL NULL
  10042. #define STB_SYNC_NULL NULL
  10043. // get the number of processors (limited to those in the affinity mask for this process).
  10044. STB_EXTERN int stb_processor_count(void);
  10045. // force to run on a single core -- needed for RDTSC to work, e.g. for iprof
  10046. STB_EXTERN void stb_force_uniprocessor(void);
  10047. // stb_work functions: queue up work to be done by some worker threads
  10048. // set number of threads to serve the queue; you can change this on the fly,
  10049. // but if you decrease it, it won't decrease until things currently on the
  10050. // queue are finished
  10051. STB_EXTERN void stb_work_numthreads(int n);
  10052. // set maximum number of units in the queue; you can only set this BEFORE running any work functions
  10053. STB_EXTERN int stb_work_maxunits(int n);
  10054. // enqueue some work to be done (can do this from any thread, or even from a piece of work);
  10055. // return value of f is stored in *return_code if non-NULL
  10056. STB_EXTERN int stb_work(stb_thread_func f, void *d, volatile void **return_code);
  10057. // as above, but stb_sync_reach is called on 'rel' after work is complete
  10058. STB_EXTERN int stb_work_reach(stb_thread_func f, void *d, volatile void **return_code, stb_sync rel);
  10059. // necessary to call this when using volatile to order writes/reads
  10060. STB_EXTERN void stb_barrier(void);
  10061. // support for independent queues with their own threads
  10062. typedef struct stb__workqueue stb_workqueue;
  10063. STB_EXTERN stb_workqueue*stb_workq_new(int numthreads, int max_units);
  10064. STB_EXTERN stb_workqueue*stb_workq_new_flags(int numthreads, int max_units, int no_add_mutex, int no_remove_mutex);
  10065. STB_EXTERN void stb_workq_delete(stb_workqueue *q);
  10066. STB_EXTERN void stb_workq_numthreads(stb_workqueue *q, int n);
  10067. STB_EXTERN int stb_workq(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code);
  10068. STB_EXTERN int stb_workq_reach(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel);
  10069. STB_EXTERN int stb_workq_length(stb_workqueue *q);
  10070. STB_EXTERN stb_thread stb_create_thread (stb_thread_func f, void *d);
  10071. STB_EXTERN stb_thread stb_create_thread2(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel);
  10072. STB_EXTERN void stb_destroy_thread(stb_thread t);
  10073. STB_EXTERN stb_semaphore stb_sem_new(int max_val);
  10074. STB_EXTERN stb_semaphore stb_sem_new_extra(int max_val, int start_val);
  10075. STB_EXTERN void stb_sem_delete (stb_semaphore s);
  10076. STB_EXTERN void stb_sem_waitfor(stb_semaphore s);
  10077. STB_EXTERN void stb_sem_release(stb_semaphore s);
  10078. STB_EXTERN stb_mutex stb_mutex_new(void);
  10079. STB_EXTERN void stb_mutex_delete(stb_mutex m);
  10080. STB_EXTERN void stb_mutex_begin(stb_mutex m);
  10081. STB_EXTERN void stb_mutex_end(stb_mutex m);
  10082. STB_EXTERN stb_sync stb_sync_new(void);
  10083. STB_EXTERN void stb_sync_delete(stb_sync s);
  10084. STB_EXTERN int stb_sync_set_target(stb_sync s, int count);
  10085. STB_EXTERN void stb_sync_reach_and_wait(stb_sync s); // wait for 'target' reachers
  10086. STB_EXTERN int stb_sync_reach(stb_sync s);
  10087. typedef struct stb__threadqueue stb_threadqueue;
  10088. #define STB_THREADQ_DYNAMIC 0
  10089. STB_EXTERN stb_threadqueue *stb_threadq_new(int item_size, int num_items, int many_add, int many_remove);
  10090. STB_EXTERN void stb_threadq_delete(stb_threadqueue *tq);
  10091. STB_EXTERN int stb_threadq_get(stb_threadqueue *tq, void *output);
  10092. STB_EXTERN void stb_threadq_get_block(stb_threadqueue *tq, void *output);
  10093. STB_EXTERN int stb_threadq_add(stb_threadqueue *tq, void *input);
  10094. // can return FALSE if STB_THREADQ_DYNAMIC and attempt to grow fails
  10095. STB_EXTERN int stb_threadq_add_block(stb_threadqueue *tq, void *input);
  10096. #ifdef STB_THREADS
  10097. #ifdef STB_DEFINE
  10098. typedef struct
  10099. {
  10100. stb_thread_func f;
  10101. void *d;
  10102. volatile void **return_val;
  10103. stb_semaphore sem;
  10104. } stb__thread;
  10105. // this is initialized along all possible paths to create threads, therefore
  10106. // it's always initialized before any other threads are create, therefore
  10107. // it's free of races AS LONG AS you only create threads through stb_*
  10108. static stb_mutex stb__threadmutex, stb__workmutex;
  10109. static void stb__threadmutex_init(void)
  10110. {
  10111. if (stb__threadmutex == STB_SEMAPHORE_NULL) {
  10112. stb__threadmutex = stb_mutex_new();
  10113. stb__workmutex = stb_mutex_new();
  10114. }
  10115. }
  10116. #ifdef STB_THREAD_TEST
  10117. volatile float stb__t1=1, stb__t2;
  10118. static void stb__wait(int n)
  10119. {
  10120. float z = 0;
  10121. int i;
  10122. for (i=0; i < n; ++i)
  10123. z += 1 / (stb__t1+i);
  10124. stb__t2 = z;
  10125. }
  10126. #else
  10127. #define stb__wait(x)
  10128. #endif
  10129. #ifdef _WIN32
  10130. // avoid including windows.h -- note that our definitions aren't
  10131. // exactly the same (we don't define the security descriptor struct)
  10132. // so if you want to include windows.h, make sure you do it first.
  10133. #include <process.h>
  10134. #ifndef _WINDOWS_ // check windows.h guard
  10135. #define STB__IMPORT STB_EXTERN __declspec(dllimport)
  10136. #define STB__DW unsigned long
  10137. STB__IMPORT int __stdcall TerminateThread(void *, STB__DW);
  10138. STB__IMPORT void * __stdcall CreateSemaphoreA(void *sec, long,long,char*);
  10139. STB__IMPORT int __stdcall CloseHandle(void *);
  10140. STB__IMPORT STB__DW __stdcall WaitForSingleObject(void *, STB__DW);
  10141. STB__IMPORT int __stdcall ReleaseSemaphore(void *, long, long *);
  10142. STB__IMPORT void __stdcall Sleep(STB__DW);
  10143. #endif
  10144. // necessary to call this when using volatile to order writes/reads
  10145. void stb_barrier(void)
  10146. {
  10147. #ifdef MemoryBarrier
  10148. MemoryBarrier();
  10149. #else
  10150. long temp;
  10151. __asm xchg temp,eax;
  10152. #endif
  10153. }
  10154. static void stb__thread_run(void *t)
  10155. {
  10156. void *res;
  10157. stb__thread info = * (stb__thread *) t;
  10158. free(t);
  10159. res = info.f(info.d);
  10160. if (info.return_val)
  10161. *info.return_val = res;
  10162. if (info.sem != STB_SEMAPHORE_NULL)
  10163. stb_sem_release(info.sem);
  10164. }
  10165. static stb_thread stb_create_thread_raw(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel)
  10166. {
  10167. #ifdef _MT
  10168. #if defined(STB_FASTMALLOC) && !defined(STB_FASTMALLOC_ITS_OKAY_I_ONLY_MALLOC_IN_ONE_THREAD)
  10169. stb_fatal("Error! Cannot use STB_FASTMALLOC with threads.\n");
  10170. return STB_THREAD_NULL;
  10171. #else
  10172. unsigned long id;
  10173. stb__thread *data = (stb__thread *) malloc(sizeof(*data));
  10174. if (!data) return NULL;
  10175. stb__threadmutex_init();
  10176. data->f = f;
  10177. data->d = d;
  10178. data->return_val = return_code;
  10179. data->sem = rel;
  10180. id = _beginthread(stb__thread_run, 0, data);
  10181. if (id == -1) return NULL;
  10182. return (void *) id;
  10183. #endif
  10184. #else
  10185. #ifdef STB_NO_STB_STRINGS
  10186. stb_fatal("Invalid compilation");
  10187. #else
  10188. stb_fatal("Must compile mult-threaded to use stb_thread/stb_work.");
  10189. #endif
  10190. return NULL;
  10191. #endif
  10192. }
  10193. // trivial win32 wrappers
  10194. void stb_destroy_thread(stb_thread t) { TerminateThread(t,0); }
  10195. stb_semaphore stb_sem_new(int maxv) {return CreateSemaphoreA(NULL,0,maxv,NULL); }
  10196. stb_semaphore stb_sem_new_extra(int maxv,int start){return CreateSemaphoreA(NULL,start,maxv,NULL); }
  10197. void stb_sem_delete(stb_semaphore s) { if (s != NULL) CloseHandle(s); }
  10198. void stb_sem_waitfor(stb_semaphore s) { WaitForSingleObject(s, 0xffffffff); } // INFINITE
  10199. void stb_sem_release(stb_semaphore s) { ReleaseSemaphore(s,1,NULL); }
  10200. static void stb__thread_sleep(int ms) { Sleep(ms); }
  10201. #ifndef _WINDOWS_
  10202. STB__IMPORT int __stdcall GetProcessAffinityMask(void *, STB__DW *, STB__DW *);
  10203. STB__IMPORT void * __stdcall GetCurrentProcess(void);
  10204. STB__IMPORT int __stdcall SetProcessAffinityMask(void *, STB__DW);
  10205. #endif
  10206. int stb_processor_count(void)
  10207. {
  10208. unsigned long proc,sys;
  10209. GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
  10210. return stb_bitcount(proc);
  10211. }
  10212. void stb_force_uniprocessor(void)
  10213. {
  10214. unsigned long proc,sys;
  10215. GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
  10216. if (stb_bitcount(proc) > 1) {
  10217. int z;
  10218. for (z=0; z < 32; ++z)
  10219. if (proc & (1 << z))
  10220. break;
  10221. if (z < 32) {
  10222. proc = 1 << z;
  10223. SetProcessAffinityMask(GetCurrentProcess(), proc);
  10224. }
  10225. }
  10226. }
  10227. #ifdef _WINDOWS_
  10228. #define STB_MUTEX_NATIVE
  10229. void *stb_mutex_new(void)
  10230. {
  10231. CRITICAL_SECTION *p = (CRITICAL_SECTION *) malloc(sizeof(*p));
  10232. if (p)
  10233. #if _WIN32_WINNT >= 0x0500
  10234. InitializeCriticalSectionAndSpinCount(p, 500);
  10235. #else
  10236. InitializeCriticalSection(p);
  10237. #endif
  10238. return p;
  10239. }
  10240. void stb_mutex_delete(void *p)
  10241. {
  10242. if (p) {
  10243. DeleteCriticalSection((CRITICAL_SECTION *) p);
  10244. free(p);
  10245. }
  10246. }
  10247. void stb_mutex_begin(void *p)
  10248. {
  10249. stb__wait(500);
  10250. if (p)
  10251. EnterCriticalSection((CRITICAL_SECTION *) p);
  10252. }
  10253. void stb_mutex_end(void *p)
  10254. {
  10255. if (p)
  10256. LeaveCriticalSection((CRITICAL_SECTION *) p);
  10257. stb__wait(500);
  10258. }
  10259. #endif // _WINDOWS_
  10260. #if 0
  10261. // for future reference,
  10262. // InterlockedCompareExchange for x86:
  10263. int cas64_mp(void * dest, void * xcmp, void * xxchg) {
  10264. __asm
  10265. {
  10266. mov esi, [xxchg] ; exchange
  10267. mov ebx, [esi + 0]
  10268. mov ecx, [esi + 4]
  10269. mov esi, [xcmp] ; comparand
  10270. mov eax, [esi + 0]
  10271. mov edx, [esi + 4]
  10272. mov edi, [dest] ; destination
  10273. lock cmpxchg8b [edi]
  10274. jz yyyy;
  10275. mov [esi + 0], eax;
  10276. mov [esi + 4], edx;
  10277. yyyy:
  10278. xor eax, eax;
  10279. setz al;
  10280. };
  10281. inline unsigned __int64 _InterlockedCompareExchange64(volatile unsigned __int64 *dest
  10282. ,unsigned __int64 exchange
  10283. ,unsigned __int64 comperand)
  10284. {
  10285. //value returned in eax::edx
  10286. __asm {
  10287. lea esi,comperand;
  10288. lea edi,exchange;
  10289. mov eax,[esi];
  10290. mov edx,4[esi];
  10291. mov ebx,[edi];
  10292. mov ecx,4[edi];
  10293. mov esi,dest;
  10294. lock CMPXCHG8B [esi];
  10295. }
  10296. #endif // #if 0
  10297. #endif // _WIN32
  10298. stb_thread stb_create_thread2(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel)
  10299. {
  10300. return stb_create_thread_raw(f,d,return_code,rel);
  10301. }
  10302. stb_thread stb_create_thread(stb_thread_func f, void *d)
  10303. {
  10304. return stb_create_thread2(f,d,NULL,STB_SEMAPHORE_NULL);
  10305. }
  10306. // mutex implemented by wrapping semaphore
  10307. #ifndef STB_MUTEX_NATIVE
  10308. stb_mutex stb_mutex_new(void) { return stb_sem_new_extra(1,1); }
  10309. void stb_mutex_delete(stb_mutex m) { stb_sem_delete (m); }
  10310. void stb_mutex_begin(stb_mutex m) { stb__wait(500); if (m) stb_sem_waitfor(m); }
  10311. void stb_mutex_end(stb_mutex m) { if (m) stb_sem_release(m); stb__wait(500); }
  10312. #endif
  10313. // thread merge operation
  10314. struct stb__sync
  10315. {
  10316. int target; // target number of threads to hit it
  10317. int sofar; // total threads that hit it
  10318. int waiting; // total threads waiting
  10319. stb_mutex start; // mutex to prevent starting again before finishing previous
  10320. stb_mutex mutex; // mutex while tweaking state
  10321. stb_semaphore release; // semaphore wake up waiting threads
  10322. // we have to wake them up one at a time, rather than using a single release
  10323. // call, because win32 semaphores don't let you dynamically change the max count!
  10324. };
  10325. stb_sync stb_sync_new(void)
  10326. {
  10327. stb_sync s = (stb_sync) malloc(sizeof(*s));
  10328. if (!s) return s;
  10329. s->target = s->sofar = s->waiting = 0;
  10330. s->mutex = stb_mutex_new();
  10331. s->start = stb_mutex_new();
  10332. s->release = stb_sem_new(1);
  10333. if (s->mutex == STB_MUTEX_NULL || s->release == STB_SEMAPHORE_NULL || s->start == STB_MUTEX_NULL) {
  10334. stb_mutex_delete(s->mutex);
  10335. stb_mutex_delete(s->mutex);
  10336. stb_sem_delete(s->release);
  10337. free(s);
  10338. return NULL;
  10339. }
  10340. return s;
  10341. }
  10342. void stb_sync_delete(stb_sync s)
  10343. {
  10344. if (s->waiting) {
  10345. // it's bad to delete while there are threads waiting!
  10346. // shall we wait for them to reach, or just bail? just bail
  10347. assert(0);
  10348. }
  10349. stb_mutex_delete(s->mutex);
  10350. stb_mutex_delete(s->release);
  10351. free(s);
  10352. }
  10353. int stb_sync_set_target(stb_sync s, int count)
  10354. {
  10355. // don't allow setting a target until the last one is fully released;
  10356. // note that this can lead to inefficient pipelining, and maybe we'd
  10357. // be better off ping-ponging between two internal syncs?
  10358. // I tried seeing how often this happened using TryEnterCriticalSection
  10359. // and could _never_ get it to happen in imv(stb), even with more threads
  10360. // than processors. So who knows!
  10361. stb_mutex_begin(s->start);
  10362. // this mutex is pointless, since it's not valid for threads
  10363. // to call reach() before anyone calls set_target() anyway
  10364. stb_mutex_begin(s->mutex);
  10365. assert(s->target == 0); // enforced by start mutex
  10366. s->target = count;
  10367. s->sofar = 0;
  10368. s->waiting = 0;
  10369. stb_mutex_end(s->mutex);
  10370. return STB_TRUE;
  10371. }
  10372. void stb__sync_release(stb_sync s)
  10373. {
  10374. if (s->waiting)
  10375. stb_sem_release(s->release);
  10376. else {
  10377. s->target = 0;
  10378. stb_mutex_end(s->start);
  10379. }
  10380. }
  10381. int stb_sync_reach(stb_sync s)
  10382. {
  10383. int n;
  10384. stb_mutex_begin(s->mutex);
  10385. assert(s->sofar < s->target);
  10386. n = ++s->sofar; // record this value to avoid possible race if we did 'return s->sofar';
  10387. if (s->sofar == s->target)
  10388. stb__sync_release(s);
  10389. stb_mutex_end(s->mutex);
  10390. return n;
  10391. }
  10392. void stb_sync_reach_and_wait(stb_sync s)
  10393. {
  10394. stb_mutex_begin(s->mutex);
  10395. assert(s->sofar < s->target);
  10396. ++s->sofar;
  10397. if (s->sofar == s->target) {
  10398. stb__sync_release(s);
  10399. stb_mutex_end(s->mutex);
  10400. } else {
  10401. ++s->waiting; // we're waiting, so one more waiter
  10402. stb_mutex_end(s->mutex); // release the mutex to other threads
  10403. stb_sem_waitfor(s->release); // wait for merge completion
  10404. stb_mutex_begin(s->mutex); // on merge completion, grab the mutex
  10405. --s->waiting; // we're done waiting
  10406. stb__sync_release(s); // restart the next waiter
  10407. stb_mutex_end(s->mutex); // and now we're done
  10408. // this ends the same as the first case, but it's a lot
  10409. // clearer to understand without sharing the code
  10410. }
  10411. }
  10412. struct stb__threadqueue
  10413. {
  10414. stb_mutex add, remove;
  10415. stb_semaphore nonempty, nonfull;
  10416. int head_blockers; // number of threads blocking--used to know whether to release(avail)
  10417. int tail_blockers;
  10418. int head, tail, array_size, growable;
  10419. int item_size;
  10420. char *data;
  10421. };
  10422. static int stb__tq_wrap(volatile stb_threadqueue *z, int p)
  10423. {
  10424. if (p == z->array_size)
  10425. return p - z->array_size;
  10426. else
  10427. return p;
  10428. }
  10429. int stb__threadq_get_raw(stb_threadqueue *tq2, void *output, int block)
  10430. {
  10431. volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
  10432. if (tq->head == tq->tail && !block) return 0;
  10433. stb_mutex_begin(tq->remove);
  10434. while (tq->head == tq->tail) {
  10435. if (!block) {
  10436. stb_mutex_end(tq->remove);
  10437. return 0;
  10438. }
  10439. ++tq->head_blockers;
  10440. stb_mutex_end(tq->remove);
  10441. stb_sem_waitfor(tq->nonempty);
  10442. stb_mutex_begin(tq->remove);
  10443. --tq->head_blockers;
  10444. }
  10445. memcpy(output, tq->data + tq->head*tq->item_size, tq->item_size);
  10446. stb_barrier();
  10447. tq->head = stb__tq_wrap(tq, tq->head+1);
  10448. stb_sem_release(tq->nonfull);
  10449. if (tq->head_blockers) // can't check if actually non-empty due to race?
  10450. stb_sem_release(tq->nonempty); // if there are other blockers, wake one
  10451. stb_mutex_end(tq->remove);
  10452. return STB_TRUE;
  10453. }
  10454. int stb__threadq_grow(volatile stb_threadqueue *tq)
  10455. {
  10456. int n;
  10457. char *p;
  10458. assert(tq->remove != STB_MUTEX_NULL); // must have this to allow growth!
  10459. stb_mutex_begin(tq->remove);
  10460. n = tq->array_size * 2;
  10461. p = (char *) realloc(tq->data, n * tq->item_size);
  10462. if (p == NULL) {
  10463. stb_mutex_end(tq->remove);
  10464. stb_mutex_end(tq->add);
  10465. return STB_FALSE;
  10466. }
  10467. if (tq->tail < tq->head) {
  10468. memcpy(p + tq->array_size * tq->item_size, p, tq->tail * tq->item_size);
  10469. tq->tail += tq->array_size;
  10470. }
  10471. tq->data = p;
  10472. tq->array_size = n;
  10473. stb_mutex_end(tq->remove);
  10474. return STB_TRUE;
  10475. }
  10476. int stb__threadq_add_raw(stb_threadqueue *tq2, void *input, int block)
  10477. {
  10478. int tail,pos;
  10479. volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
  10480. stb_mutex_begin(tq->add);
  10481. for(;;) {
  10482. pos = tq->tail;
  10483. tail = stb__tq_wrap(tq, pos+1);
  10484. if (tail != tq->head) break;
  10485. // full
  10486. if (tq->growable) {
  10487. if (!stb__threadq_grow(tq)) {
  10488. stb_mutex_end(tq->add);
  10489. return STB_FALSE; // out of memory
  10490. }
  10491. } else if (!block) {
  10492. stb_mutex_end(tq->add);
  10493. return STB_FALSE;
  10494. } else {
  10495. ++tq->tail_blockers;
  10496. stb_mutex_end(tq->add);
  10497. stb_sem_waitfor(tq->nonfull);
  10498. stb_mutex_begin(tq->add);
  10499. --tq->tail_blockers;
  10500. }
  10501. }
  10502. memcpy(tq->data + tq->item_size * pos, input, tq->item_size);
  10503. stb_barrier();
  10504. tq->tail = tail;
  10505. stb_sem_release(tq->nonempty);
  10506. if (tq->tail_blockers) // can't check if actually non-full due to race?
  10507. stb_sem_release(tq->nonfull);
  10508. stb_mutex_end(tq->add);
  10509. return STB_TRUE;
  10510. }
  10511. int stb_threadq_length(stb_threadqueue *tq2)
  10512. {
  10513. int a,b,n;
  10514. volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
  10515. stb_mutex_begin(tq->add);
  10516. a = tq->head;
  10517. b = tq->tail;
  10518. n = tq->array_size;
  10519. stb_mutex_end(tq->add);
  10520. if (a > b) b += n;
  10521. return b-a;
  10522. }
  10523. int stb_threadq_get(stb_threadqueue *tq, void *output)
  10524. {
  10525. return stb__threadq_get_raw(tq, output, STB_FALSE);
  10526. }
  10527. void stb_threadq_get_block(stb_threadqueue *tq, void *output)
  10528. {
  10529. stb__threadq_get_raw(tq, output, STB_TRUE);
  10530. }
  10531. int stb_threadq_add(stb_threadqueue *tq, void *input)
  10532. {
  10533. return stb__threadq_add_raw(tq, input, STB_FALSE);
  10534. }
  10535. int stb_threadq_add_block(stb_threadqueue *tq, void *input)
  10536. {
  10537. return stb__threadq_add_raw(tq, input, STB_TRUE);
  10538. }
  10539. void stb_threadq_delete(stb_threadqueue *tq)
  10540. {
  10541. if (tq) {
  10542. free(tq->data);
  10543. stb_mutex_delete(tq->add);
  10544. stb_mutex_delete(tq->remove);
  10545. stb_sem_delete(tq->nonempty);
  10546. stb_sem_delete(tq->nonfull);
  10547. free(tq);
  10548. }
  10549. }
  10550. #define STB_THREADQUEUE_DYNAMIC 0
  10551. stb_threadqueue *stb_threadq_new(int item_size, int num_items, int many_add, int many_remove)
  10552. {
  10553. int error=0;
  10554. stb_threadqueue *tq = (stb_threadqueue *) malloc(sizeof(*tq));
  10555. if (tq == NULL) return NULL;
  10556. if (num_items == STB_THREADQUEUE_DYNAMIC) {
  10557. tq->growable = STB_TRUE;
  10558. num_items = 32;
  10559. } else
  10560. tq->growable = STB_FALSE;
  10561. tq->item_size = item_size;
  10562. tq->array_size = num_items+1;
  10563. tq->add = tq->remove = STB_MUTEX_NULL;
  10564. tq->nonempty = tq->nonfull = STB_SEMAPHORE_NULL;
  10565. tq->data = NULL;
  10566. if (many_add)
  10567. { tq->add = stb_mutex_new(); if (tq->add == STB_MUTEX_NULL) goto error; }
  10568. if (many_remove || tq->growable)
  10569. { tq->remove = stb_mutex_new(); if (tq->remove == STB_MUTEX_NULL) goto error; }
  10570. tq->nonempty = stb_sem_new(1); if (tq->nonempty == STB_SEMAPHORE_NULL) goto error;
  10571. tq->nonfull = stb_sem_new(1); if (tq->nonfull == STB_SEMAPHORE_NULL) goto error;
  10572. tq->data = (char *) malloc(tq->item_size * tq->array_size);
  10573. if (tq->data == NULL) goto error;
  10574. tq->head = tq->tail = 0;
  10575. tq->head_blockers = tq->tail_blockers = 0;
  10576. return tq;
  10577. error:
  10578. stb_threadq_delete(tq);
  10579. return NULL;
  10580. }
  10581. typedef struct
  10582. {
  10583. stb_thread_func f;
  10584. void *d;
  10585. volatile void **retval;
  10586. stb_sync sync;
  10587. } stb__workinfo;
  10588. //static volatile stb__workinfo *stb__work;
  10589. struct stb__workqueue
  10590. {
  10591. int numthreads;
  10592. stb_threadqueue *tq;
  10593. };
  10594. static stb_workqueue *stb__work_global;
  10595. static void *stb__thread_workloop(void *p)
  10596. {
  10597. volatile stb_workqueue *q = (volatile stb_workqueue *) p;
  10598. for(;;) {
  10599. void *z;
  10600. stb__workinfo w;
  10601. stb_threadq_get_block(q->tq, &w);
  10602. if (w.f == NULL) // null work is a signal to end the thread
  10603. return NULL;
  10604. z = w.f(w.d);
  10605. if (w.retval) { stb_barrier(); *w.retval = z; }
  10606. if (w.sync != STB_SYNC_NULL) stb_sync_reach(w.sync);
  10607. }
  10608. }
  10609. stb_workqueue *stb_workq_new(int num_threads, int max_units)
  10610. {
  10611. return stb_workq_new_flags(num_threads, max_units, 0,0);
  10612. }
  10613. stb_workqueue *stb_workq_new_flags(int numthreads, int max_units, int no_add_mutex, int no_remove_mutex)
  10614. {
  10615. stb_workqueue *q = (stb_workqueue *) malloc(sizeof(*q));
  10616. if (q == NULL) return NULL;
  10617. q->tq = stb_threadq_new(sizeof(stb__workinfo), max_units, !no_add_mutex, !no_remove_mutex);
  10618. if (q->tq == NULL) { free(q); return NULL; }
  10619. q->numthreads = 0;
  10620. stb_workq_numthreads(q, numthreads);
  10621. return q;
  10622. }
  10623. void stb_workq_delete(stb_workqueue *q)
  10624. {
  10625. while (stb_workq_length(q) != 0)
  10626. stb__thread_sleep(1);
  10627. stb_threadq_delete(q->tq);
  10628. free(q);
  10629. }
  10630. static int stb__work_maxitems = STB_THREADQUEUE_DYNAMIC;
  10631. static void stb_work_init(int num_threads)
  10632. {
  10633. if (stb__work_global == NULL) {
  10634. stb__threadmutex_init();
  10635. stb_mutex_begin(stb__workmutex);
  10636. stb_barrier();
  10637. if (*(stb_workqueue * volatile *) &stb__work_global == NULL)
  10638. stb__work_global = stb_workq_new(num_threads, stb__work_maxitems);
  10639. stb_mutex_end(stb__workmutex);
  10640. }
  10641. }
  10642. static int stb__work_raw(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
  10643. {
  10644. stb__workinfo w;
  10645. if (q == NULL) {
  10646. stb_work_init(1);
  10647. q = stb__work_global;
  10648. }
  10649. w.f = f;
  10650. w.d = d;
  10651. w.retval = return_code;
  10652. w.sync = rel;
  10653. return stb_threadq_add(q->tq, &w);
  10654. }
  10655. int stb_workq_length(stb_workqueue *q)
  10656. {
  10657. return stb_threadq_length(q->tq);
  10658. }
  10659. int stb_workq(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code)
  10660. {
  10661. if (f == NULL) return 0;
  10662. return stb_workq_reach(q, f, d, return_code, NULL);
  10663. }
  10664. int stb_workq_reach(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
  10665. {
  10666. if (f == NULL) return 0;
  10667. return stb__work_raw(q, f, d, return_code, rel);
  10668. }
  10669. static void stb__workq_numthreads(stb_workqueue *q, int n)
  10670. {
  10671. while (q->numthreads < n) {
  10672. stb_create_thread(stb__thread_workloop, q);
  10673. ++q->numthreads;
  10674. }
  10675. while (q->numthreads > n) {
  10676. stb__work_raw(q, NULL, NULL, NULL, NULL);
  10677. --q->numthreads;
  10678. }
  10679. }
  10680. void stb_workq_numthreads(stb_workqueue *q, int n)
  10681. {
  10682. stb_mutex_begin(stb__threadmutex);
  10683. stb__workq_numthreads(q,n);
  10684. stb_mutex_end(stb__threadmutex);
  10685. }
  10686. int stb_work_maxunits(int n)
  10687. {
  10688. if (stb__work_global == NULL) {
  10689. stb__work_maxitems = n;
  10690. stb_work_init(1);
  10691. }
  10692. return stb__work_maxitems;
  10693. }
  10694. int stb_work(stb_thread_func f, void *d, volatile void **return_code)
  10695. {
  10696. return stb_workq(stb__work_global, f,d,return_code);
  10697. }
  10698. int stb_work_reach(stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
  10699. {
  10700. return stb_workq_reach(stb__work_global, f,d,return_code,rel);
  10701. }
  10702. void stb_work_numthreads(int n)
  10703. {
  10704. if (stb__work_global == NULL)
  10705. stb_work_init(n);
  10706. else
  10707. stb_workq_numthreads(stb__work_global, n);
  10708. }
  10709. #endif // STB_DEFINE
  10710. //////////////////////////////////////////////////////////////////////////////
  10711. //
  10712. // Background disk I/O
  10713. //
  10714. //
  10715. #define STB_BGIO_READ_ALL (-1)
  10716. STB_EXTERN int stb_bgio_read (char *filename, int offset, int len, stb_uchar **result, int *olen);
  10717. STB_EXTERN int stb_bgio_readf (FILE *f , int offset, int len, stb_uchar **result, int *olen);
  10718. STB_EXTERN int stb_bgio_read_to (char *filename, int offset, int len, stb_uchar *buffer, int *olen);
  10719. STB_EXTERN int stb_bgio_readf_to(FILE *f , int offset, int len, stb_uchar *buffer, int *olen);
  10720. typedef struct
  10721. {
  10722. int have_data;
  10723. int is_valid;
  10724. int is_dir;
  10725. time_t filetime;
  10726. stb_int64 filesize;
  10727. } stb_bgstat;
  10728. STB_EXTERN int stb_bgio_stat (char *filename, stb_bgstat *result);
  10729. #ifdef STB_DEFINE
  10730. static stb_workqueue *stb__diskio;
  10731. static stb_mutex stb__diskio_mutex;
  10732. void stb_thread_cleanup(void)
  10733. {
  10734. if (stb__work_global) stb_workq_delete(stb__work_global); stb__work_global = NULL;
  10735. if (stb__threadmutex) stb_mutex_delete(stb__threadmutex); stb__threadmutex = NULL;
  10736. if (stb__workmutex) stb_mutex_delete(stb__workmutex); stb__workmutex = NULL;
  10737. if (stb__diskio) stb_workq_delete(stb__diskio); stb__diskio = NULL;
  10738. if (stb__diskio_mutex)stb_mutex_delete(stb__diskio_mutex);stb__diskio_mutex= NULL;
  10739. }
  10740. typedef struct
  10741. {
  10742. char *filename;
  10743. FILE *f;
  10744. int offset;
  10745. int len;
  10746. stb_bgstat *stat_out;
  10747. stb_uchar *output;
  10748. stb_uchar **result;
  10749. int *len_output;
  10750. int *flag;
  10751. } stb__disk_command;
  10752. #define STB__MAX_DISK_COMMAND 100
  10753. static stb__disk_command stb__dc_queue[STB__MAX_DISK_COMMAND];
  10754. static int stb__dc_offset;
  10755. void stb__io_init(void)
  10756. {
  10757. if (!stb__diskio) {
  10758. stb__threadmutex_init();
  10759. stb_mutex_begin(stb__threadmutex);
  10760. stb_barrier();
  10761. if (*(stb_thread * volatile *) &stb__diskio == NULL) {
  10762. stb__diskio_mutex = stb_mutex_new();
  10763. // use many threads so OS can try to schedule seeks
  10764. stb__diskio = stb_workq_new_flags(16,STB__MAX_DISK_COMMAND,STB_FALSE,STB_FALSE);
  10765. }
  10766. stb_mutex_end(stb__threadmutex);
  10767. }
  10768. }
  10769. static void * stb__io_error(stb__disk_command *dc)
  10770. {
  10771. if (dc->len_output) *dc->len_output = 0;
  10772. if (dc->result) *dc->result = NULL;
  10773. if (dc->flag) *dc->flag = -1;
  10774. return NULL;
  10775. }
  10776. static void * stb__io_task(void *p)
  10777. {
  10778. stb__disk_command *dc = (stb__disk_command *) p;
  10779. int len;
  10780. FILE *f;
  10781. stb_uchar *buf;
  10782. if (dc->stat_out) {
  10783. struct _stati64 s;
  10784. if (!_stati64(dc->filename, &s)) {
  10785. dc->stat_out->filesize = s.st_size;
  10786. dc->stat_out->filetime = s.st_mtime;
  10787. dc->stat_out->is_dir = s.st_mode & _S_IFDIR;
  10788. dc->stat_out->is_valid = (s.st_mode & _S_IFREG) || dc->stat_out->is_dir;
  10789. } else
  10790. dc->stat_out->is_valid = 0;
  10791. stb_barrier();
  10792. dc->stat_out->have_data = 1;
  10793. free(dc->filename);
  10794. return 0;
  10795. }
  10796. if (dc->f) {
  10797. #ifdef WIN32
  10798. f = _fdopen(_dup(_fileno(dc->f)), "rb");
  10799. #else
  10800. f = fdopen(dup(fileno(dc->f)), "rb");
  10801. #endif
  10802. if (!f)
  10803. return stb__io_error(dc);
  10804. } else {
  10805. f = fopen(dc->filename, "rb");
  10806. free(dc->filename);
  10807. if (!f)
  10808. return stb__io_error(dc);
  10809. }
  10810. len = dc->len;
  10811. if (len < 0) {
  10812. fseek(f, 0, SEEK_END);
  10813. len = ftell(f) - dc->offset;
  10814. }
  10815. if (fseek(f, dc->offset, SEEK_SET)) {
  10816. fclose(f);
  10817. return stb__io_error(dc);
  10818. }
  10819. if (dc->output)
  10820. buf = dc->output;
  10821. else {
  10822. buf = (stb_uchar *) malloc(len);
  10823. if (buf == NULL) {
  10824. fclose(f);
  10825. return stb__io_error(dc);
  10826. }
  10827. }
  10828. len = fread(buf, 1, len, f);
  10829. fclose(f);
  10830. if (dc->len_output) *dc->len_output = len;
  10831. if (dc->result) *dc->result = buf;
  10832. if (dc->flag) *dc->flag = 1;
  10833. return NULL;
  10834. }
  10835. int stb__io_add(char *fname, FILE *f, int off, int len, stb_uchar *out, stb_uchar **result, int *olen, int *flag, stb_bgstat *stat)
  10836. {
  10837. int res;
  10838. stb__io_init();
  10839. // do memory allocation outside of mutex
  10840. if (fname) fname = stb_p_strdup(fname);
  10841. stb_mutex_begin(stb__diskio_mutex);
  10842. {
  10843. stb__disk_command *dc = &stb__dc_queue[stb__dc_offset];
  10844. dc->filename = fname;
  10845. dc->f = f;
  10846. dc->offset = off;
  10847. dc->len = len;
  10848. dc->output = out;
  10849. dc->result = result;
  10850. dc->len_output = olen;
  10851. dc->flag = flag;
  10852. dc->stat_out = stat;
  10853. res = stb_workq(stb__diskio, stb__io_task, dc, NULL);
  10854. if (res)
  10855. stb__dc_offset = (stb__dc_offset + 1 == STB__MAX_DISK_COMMAND ? 0 : stb__dc_offset+1);
  10856. }
  10857. stb_mutex_end(stb__diskio_mutex);
  10858. return res;
  10859. }
  10860. int stb_bgio_read(char *filename, int offset, int len, stb_uchar **result, int *olen)
  10861. {
  10862. return stb__io_add(filename,NULL,offset,len,NULL,result,olen,NULL,NULL);
  10863. }
  10864. int stb_bgio_readf(FILE *f, int offset, int len, stb_uchar **result, int *olen)
  10865. {
  10866. return stb__io_add(NULL,f,offset,len,NULL,result,olen,NULL,NULL);
  10867. }
  10868. int stb_bgio_read_to(char *filename, int offset, int len, stb_uchar *buffer, int *olen)
  10869. {
  10870. return stb__io_add(filename,NULL,offset,len,buffer,NULL,olen,NULL,NULL);
  10871. }
  10872. int stb_bgio_readf_to(FILE *f, int offset, int len, stb_uchar *buffer, int *olen)
  10873. {
  10874. return stb__io_add(NULL,f,offset,len,buffer,NULL,olen,NULL,NULL);
  10875. }
  10876. STB_EXTERN int stb_bgio_stat (char *filename, stb_bgstat *result)
  10877. {
  10878. result->have_data = 0;
  10879. return stb__io_add(filename,NULL,0,0,0,NULL,0,NULL, result);
  10880. }
  10881. #endif
  10882. #endif
  10883. //////////////////////////////////////////////////////////////////////////////
  10884. //
  10885. // Fast malloc implementation
  10886. //
  10887. // This is a clone of TCMalloc, but without the thread support.
  10888. // 1. large objects are allocated directly, page-aligned
  10889. // 2. small objects are allocated in homogeonous heaps, 0 overhead
  10890. //
  10891. // We keep an allocation table for pages a la TCMalloc. This would
  10892. // require 4MB for the entire address space, but we only allocate
  10893. // the parts that are in use. The overhead from using homogenous heaps
  10894. // everywhere is 3MB. (That is, if you allocate 1 object of each size,
  10895. // you'll use 3MB.)
  10896. #if defined(STB_DEFINE) && ((defined(_WIN32) && !defined(_M_AMD64)) || defined(STB_FASTMALLOC))
  10897. #ifdef _WIN32
  10898. #ifndef _WINDOWS_
  10899. #ifndef STB__IMPORT
  10900. #define STB__IMPORT STB_EXTERN __declspec(dllimport)
  10901. #define STB__DW unsigned long
  10902. #endif
  10903. STB__IMPORT void * __stdcall VirtualAlloc(void *p, unsigned long size, unsigned long type, unsigned long protect);
  10904. STB__IMPORT int __stdcall VirtualFree(void *p, unsigned long size, unsigned long freetype);
  10905. #endif
  10906. #define stb__alloc_pages_raw(x) (stb_uint32) VirtualAlloc(NULL, (x), 0x3000, 0x04)
  10907. #define stb__dealloc_pages_raw(p) VirtualFree((void *) p, 0, 0x8000)
  10908. #else
  10909. #error "Platform not currently supported"
  10910. #endif
  10911. typedef struct stb__span
  10912. {
  10913. int start, len;
  10914. struct stb__span *next, *prev;
  10915. void *first_free;
  10916. unsigned short list; // 1..256 free; 257..511 sizeclass; 0=large block
  10917. short allocations; // # outstanding allocations for sizeclass
  10918. } stb__span; // 24
  10919. static stb__span **stb__span_for_page;
  10920. static int stb__firstpage, stb__lastpage;
  10921. static void stb__update_page_range(int first, int last)
  10922. {
  10923. stb__span **sfp;
  10924. int i, f,l;
  10925. if (first >= stb__firstpage && last <= stb__lastpage) return;
  10926. if (stb__span_for_page == NULL) {
  10927. f = first;
  10928. l = f+stb_max(last-f, 16384);
  10929. l = stb_min(l, 1<<20);
  10930. } else if (last > stb__lastpage) {
  10931. f = stb__firstpage;
  10932. l = f + (stb__lastpage - f) * 2;
  10933. l = stb_clamp(last, l,1<<20);
  10934. } else {
  10935. l = stb__lastpage;
  10936. f = l - (l - stb__firstpage) * 2;
  10937. f = stb_clamp(f, 0,first);
  10938. }
  10939. sfp = (stb__span **) stb__alloc_pages_raw(sizeof(void *) * (l-f));
  10940. for (i=f; i < stb__firstpage; ++i) sfp[i - f] = NULL;
  10941. for ( ; i < stb__lastpage ; ++i) sfp[i - f] = stb__span_for_page[i - stb__firstpage];
  10942. for ( ; i < l ; ++i) sfp[i - f] = NULL;
  10943. if (stb__span_for_page) stb__dealloc_pages_raw(stb__span_for_page);
  10944. stb__firstpage = f;
  10945. stb__lastpage = l;
  10946. stb__span_for_page = sfp;
  10947. }
  10948. static stb__span *stb__span_free=NULL;
  10949. static stb__span *stb__span_first, *stb__span_end;
  10950. static stb__span *stb__span_alloc(void)
  10951. {
  10952. stb__span *s = stb__span_free;
  10953. if (s)
  10954. stb__span_free = s->next;
  10955. else {
  10956. if (!stb__span_first) {
  10957. stb__span_first = (stb__span *) stb__alloc_pages_raw(65536);
  10958. if (stb__span_first == NULL) return NULL;
  10959. stb__span_end = stb__span_first + (65536 / sizeof(stb__span));
  10960. }
  10961. s = stb__span_first++;
  10962. if (stb__span_first == stb__span_end) stb__span_first = NULL;
  10963. }
  10964. return s;
  10965. }
  10966. static stb__span *stb__spanlist[512];
  10967. static void stb__spanlist_unlink(stb__span *s)
  10968. {
  10969. if (s->prev)
  10970. s->prev->next = s->next;
  10971. else {
  10972. int n = s->list;
  10973. assert(stb__spanlist[n] == s);
  10974. stb__spanlist[n] = s->next;
  10975. }
  10976. if (s->next)
  10977. s->next->prev = s->prev;
  10978. s->next = s->prev = NULL;
  10979. s->list = 0;
  10980. }
  10981. static void stb__spanlist_add(int n, stb__span *s)
  10982. {
  10983. s->list = n;
  10984. s->next = stb__spanlist[n];
  10985. s->prev = NULL;
  10986. stb__spanlist[n] = s;
  10987. if (s->next) s->next->prev = s;
  10988. }
  10989. #define stb__page_shift 12
  10990. #define stb__page_size (1 << stb__page_shift)
  10991. #define stb__page_number(x) ((x) >> stb__page_shift)
  10992. #define stb__page_address(x) ((x) << stb__page_shift)
  10993. static void stb__set_span_for_page(stb__span *s)
  10994. {
  10995. int i;
  10996. for (i=0; i < s->len; ++i)
  10997. stb__span_for_page[s->start + i - stb__firstpage] = s;
  10998. }
  10999. static stb__span *stb__coalesce(stb__span *a, stb__span *b)
  11000. {
  11001. assert(a->start + a->len == b->start);
  11002. if (a->list) stb__spanlist_unlink(a);
  11003. if (b->list) stb__spanlist_unlink(b);
  11004. a->len += b->len;
  11005. b->len = 0;
  11006. b->next = stb__span_free;
  11007. stb__span_free = b;
  11008. stb__set_span_for_page(a);
  11009. return a;
  11010. }
  11011. static void stb__free_span(stb__span *s)
  11012. {
  11013. stb__span *n = NULL;
  11014. if (s->start > stb__firstpage) {
  11015. n = stb__span_for_page[s->start-1 - stb__firstpage];
  11016. if (n && n->allocations == -2 && n->start + n->len == s->start) s = stb__coalesce(n,s);
  11017. }
  11018. if (s->start + s->len < stb__lastpage) {
  11019. n = stb__span_for_page[s->start + s->len - stb__firstpage];
  11020. if (n && n->allocations == -2 && s->start + s->len == n->start) s = stb__coalesce(s,n);
  11021. }
  11022. s->allocations = -2;
  11023. stb__spanlist_add(s->len > 256 ? 256 : s->len, s);
  11024. }
  11025. static stb__span *stb__alloc_pages(int num)
  11026. {
  11027. stb__span *s = stb__span_alloc();
  11028. int p;
  11029. if (!s) return NULL;
  11030. p = stb__alloc_pages_raw(num << stb__page_shift);
  11031. if (p == 0) { s->next = stb__span_free; stb__span_free = s; return 0; }
  11032. assert(stb__page_address(stb__page_number(p)) == p);
  11033. p = stb__page_number(p);
  11034. stb__update_page_range(p, p+num);
  11035. s->start = p;
  11036. s->len = num;
  11037. s->next = NULL;
  11038. s->prev = NULL;
  11039. stb__set_span_for_page(s);
  11040. return s;
  11041. }
  11042. static stb__span *stb__alloc_span(int pagecount)
  11043. {
  11044. int i;
  11045. stb__span *p = NULL;
  11046. for(i=pagecount; i < 256; ++i)
  11047. if (stb__spanlist[i]) {
  11048. p = stb__spanlist[i];
  11049. break;
  11050. }
  11051. if (!p) {
  11052. p = stb__spanlist[256];
  11053. while (p && p->len < pagecount)
  11054. p = p->next;
  11055. }
  11056. if (!p) {
  11057. p = stb__alloc_pages(pagecount < 16 ? 16 : pagecount);
  11058. if (p == NULL) return 0;
  11059. } else
  11060. stb__spanlist_unlink(p);
  11061. if (p->len > pagecount) {
  11062. stb__span *q = stb__span_alloc();
  11063. if (q) {
  11064. q->start = p->start + pagecount;
  11065. q->len = p->len - pagecount;
  11066. p->len = pagecount;
  11067. for (i=0; i < q->len; ++i)
  11068. stb__span_for_page[q->start+i - stb__firstpage] = q;
  11069. stb__spanlist_add(q->len > 256 ? 256 : q->len, q);
  11070. }
  11071. }
  11072. return p;
  11073. }
  11074. #define STB__MAX_SMALL_SIZE 32768
  11075. #define STB__MAX_SIZE_CLASSES 256
  11076. static unsigned char stb__class_base[32];
  11077. static unsigned char stb__class_shift[32];
  11078. static unsigned char stb__pages_for_class[STB__MAX_SIZE_CLASSES];
  11079. static int stb__size_for_class[STB__MAX_SIZE_CLASSES];
  11080. stb__span *stb__get_nonempty_sizeclass(int c)
  11081. {
  11082. int s = c + 256, i, size, tsize; // remap to span-list index
  11083. char *z;
  11084. void *q;
  11085. stb__span *p = stb__spanlist[s];
  11086. if (p) {
  11087. if (p->first_free) return p; // fast path: it's in the first one in list
  11088. for (p=p->next; p; p=p->next)
  11089. if (p->first_free) {
  11090. // move to front for future queries
  11091. stb__spanlist_unlink(p);
  11092. stb__spanlist_add(s, p);
  11093. return p;
  11094. }
  11095. }
  11096. // no non-empty ones, so allocate a new one
  11097. p = stb__alloc_span(stb__pages_for_class[c]);
  11098. if (!p) return NULL;
  11099. // create the free list up front
  11100. size = stb__size_for_class[c];
  11101. tsize = stb__pages_for_class[c] << stb__page_shift;
  11102. i = 0;
  11103. z = (char *) stb__page_address(p->start);
  11104. q = NULL;
  11105. while (i + size <= tsize) {
  11106. * (void **) z = q; q = z;
  11107. z += size;
  11108. i += size;
  11109. }
  11110. p->first_free = q;
  11111. p->allocations = 0;
  11112. stb__spanlist_add(s,p);
  11113. return p;
  11114. }
  11115. static int stb__sizeclass(size_t sz)
  11116. {
  11117. int z = stb_log2_floor(sz); // -1 below to group e.g. 13,14,15,16 correctly
  11118. return stb__class_base[z] + ((sz-1) >> stb__class_shift[z]);
  11119. }
  11120. static void stb__init_sizeclass(void)
  11121. {
  11122. int i, size, overhead;
  11123. int align_shift = 2; // allow 4-byte and 12-byte blocks as well, vs. TCMalloc
  11124. int next_class = 1;
  11125. int last_log = 0;
  11126. for (i = 0; i < align_shift; i++) {
  11127. stb__class_base [i] = next_class;
  11128. stb__class_shift[i] = align_shift;
  11129. }
  11130. for (size = 1 << align_shift; size <= STB__MAX_SMALL_SIZE; size += 1 << align_shift) {
  11131. i = stb_log2_floor(size);
  11132. if (i > last_log) {
  11133. if (size == 16) ++align_shift; // switch from 4-byte to 8-byte alignment
  11134. else if (size >= 128 && align_shift < 8) ++align_shift;
  11135. stb__class_base[i] = next_class - ((size-1) >> align_shift);
  11136. stb__class_shift[i] = align_shift;
  11137. last_log = i;
  11138. }
  11139. stb__size_for_class[next_class++] = size;
  11140. }
  11141. for (i=1; i <= STB__MAX_SMALL_SIZE; ++i)
  11142. assert(i <= stb__size_for_class[stb__sizeclass(i)]);
  11143. overhead = 0;
  11144. for (i = 1; i < next_class; i++) {
  11145. int s = stb__size_for_class[i];
  11146. size = stb__page_size;
  11147. while (size % s > size >> 3)
  11148. size += stb__page_size;
  11149. stb__pages_for_class[i] = (unsigned char) (size >> stb__page_shift);
  11150. overhead += size;
  11151. }
  11152. assert(overhead < (4 << 20)); // make sure it's under 4MB of overhead
  11153. }
  11154. #ifdef STB_DEBUG
  11155. #define stb__smemset(a,b,c) memset((void *) a, b, c)
  11156. #elif defined(STB_FASTMALLOC_INIT)
  11157. #define stb__smemset(a,b,c) memset((void *) a, b, c)
  11158. #else
  11159. #define stb__smemset(a,b,c)
  11160. #endif
  11161. void *stb_smalloc(size_t sz)
  11162. {
  11163. stb__span *s;
  11164. if (sz == 0) return NULL;
  11165. if (stb__size_for_class[1] == 0) stb__init_sizeclass();
  11166. if (sz > STB__MAX_SMALL_SIZE) {
  11167. s = stb__alloc_span((sz + stb__page_size - 1) >> stb__page_shift);
  11168. if (s == NULL) return NULL;
  11169. s->list = 0;
  11170. s->next = s->prev = NULL;
  11171. s->allocations = -32767;
  11172. stb__smemset(stb__page_address(s->start), 0xcd, (sz+3)&~3);
  11173. return (void *) stb__page_address(s->start);
  11174. } else {
  11175. void *p;
  11176. int c = stb__sizeclass(sz);
  11177. s = stb__spanlist[256+c];
  11178. if (!s || !s->first_free)
  11179. s = stb__get_nonempty_sizeclass(c);
  11180. if (s == NULL) return NULL;
  11181. p = s->first_free;
  11182. s->first_free = * (void **) p;
  11183. ++s->allocations;
  11184. stb__smemset(p,0xcd, sz);
  11185. return p;
  11186. }
  11187. }
  11188. int stb_ssize(void *p)
  11189. {
  11190. stb__span *s;
  11191. if (p == NULL) return 0;
  11192. s = stb__span_for_page[stb__page_number((stb_uint) p) - stb__firstpage];
  11193. if (s->list >= 256) {
  11194. return stb__size_for_class[s->list - 256];
  11195. } else {
  11196. assert(s->list == 0);
  11197. return s->len << stb__page_shift;
  11198. }
  11199. }
  11200. void stb_sfree(void *p)
  11201. {
  11202. stb__span *s;
  11203. if (p == NULL) return;
  11204. s = stb__span_for_page[stb__page_number((stb_uint) p) - stb__firstpage];
  11205. if (s->list >= 256) {
  11206. stb__smemset(p, 0xfe, stb__size_for_class[s->list-256]);
  11207. * (void **) p = s->first_free;
  11208. s->first_free = p;
  11209. if (--s->allocations == 0) {
  11210. stb__spanlist_unlink(s);
  11211. stb__free_span(s);
  11212. }
  11213. } else {
  11214. assert(s->list == 0);
  11215. stb__smemset(p, 0xfe, stb_ssize(p));
  11216. stb__free_span(s);
  11217. }
  11218. }
  11219. void *stb_srealloc(void *p, size_t sz)
  11220. {
  11221. size_t cur_size;
  11222. if (p == NULL) return stb_smalloc(sz);
  11223. if (sz == 0) { stb_sfree(p); return NULL; }
  11224. cur_size = stb_ssize(p);
  11225. if (sz > cur_size || sz <= (cur_size >> 1)) {
  11226. void *q;
  11227. if (sz > cur_size && sz < (cur_size << 1)) sz = cur_size << 1;
  11228. q = stb_smalloc(sz); if (q == NULL) return NULL;
  11229. memcpy(q, p, sz < cur_size ? sz : cur_size);
  11230. stb_sfree(p);
  11231. return q;
  11232. }
  11233. return p;
  11234. }
  11235. void *stb_scalloc(size_t n, size_t sz)
  11236. {
  11237. void *p;
  11238. if (n == 0 || sz == 0) return NULL;
  11239. if (stb_log2_ceil(n) + stb_log2_ceil(n) >= 32) return NULL;
  11240. p = stb_smalloc(n*sz);
  11241. if (p) memset(p, 0, n*sz);
  11242. return p;
  11243. }
  11244. char *stb_sstrdup(char *s)
  11245. {
  11246. int n = strlen(s);
  11247. char *p = (char *) stb_smalloc(n+1);
  11248. if (p) stb_p_strcpy_s(p,n+1,s);
  11249. return p;
  11250. }
  11251. #endif // STB_DEFINE
  11252. //////////////////////////////////////////////////////////////////////////////
  11253. //
  11254. // Source code constants
  11255. //
  11256. // This is a trivial system to let you specify constants in source code,
  11257. // then while running you can change the constants.
  11258. //
  11259. // Note that you can't wrap the #defines, because we need to know their
  11260. // names. So we provide a pre-wrapped version without 'STB_' for convenience;
  11261. // to request it, #define STB_CONVENIENT_H, yielding:
  11262. // KI -- integer
  11263. // KU -- unsigned integer
  11264. // KF -- float
  11265. // KD -- double
  11266. // KS -- string constant
  11267. //
  11268. // Defaults to functioning in debug build, not in release builds.
  11269. // To force on, define STB_ALWAYS_H
  11270. #ifdef STB_CONVENIENT_H
  11271. #define KI(x) STB_I(x)
  11272. #define KU(x) STB_UI(x)
  11273. #define KF(x) STB_F(x)
  11274. #define KD(x) STB_D(x)
  11275. #define KS(x) STB_S(x)
  11276. #endif
  11277. STB_EXTERN void stb_source_path(char *str);
  11278. #ifdef STB_DEFINE
  11279. char *stb__source_path;
  11280. void stb_source_path(char *path)
  11281. {
  11282. stb__source_path = path;
  11283. }
  11284. char *stb__get_sourcefile_path(char *file)
  11285. {
  11286. static char filebuf[512];
  11287. if (stb__source_path) {
  11288. stb_p_sprintf(filebuf stb_p_size(sizeof(filebuf)), "%s/%s", stb__source_path, file);
  11289. if (stb_fexists(filebuf)) return filebuf;
  11290. }
  11291. if (stb_fexists(file)) return file;
  11292. stb_p_sprintf(filebuf stb_p_size(sizeof(filebuf)), "../%s", file);
  11293. if (!stb_fexists(filebuf)) return filebuf;
  11294. return file;
  11295. }
  11296. #endif
  11297. #define STB_F(x) ((float) STB_H(x))
  11298. #define STB_UI(x) ((unsigned int) STB_I(x))
  11299. #if !defined(STB_DEBUG) && !defined(STB_ALWAYS_H)
  11300. #define STB_D(x) ((double) (x))
  11301. #define STB_I(x) ((int) (x))
  11302. #define STB_S(x) ((char *) (x))
  11303. #else
  11304. #define STB_D(x) stb__double_constant(__FILE__, __LINE__-1, (x))
  11305. #define STB_I(x) stb__int_constant(__FILE__, __LINE__-1, (x))
  11306. #define STB_S(x) stb__string_constant(__FILE__, __LINE__-1, (x))
  11307. STB_EXTERN double stb__double_constant(char *file, int line, double x);
  11308. STB_EXTERN int stb__int_constant(char *file, int line, int x);
  11309. STB_EXTERN char * stb__string_constant(char *file, int line, char *str);
  11310. #ifdef STB_DEFINE
  11311. enum
  11312. {
  11313. STB__CTYPE_int,
  11314. STB__CTYPE_uint,
  11315. STB__CTYPE_float,
  11316. STB__CTYPE_double,
  11317. STB__CTYPE_string,
  11318. };
  11319. typedef struct
  11320. {
  11321. int line;
  11322. int type;
  11323. union {
  11324. int ival;
  11325. double dval;
  11326. char *sval;
  11327. };
  11328. } stb__Entry;
  11329. typedef struct
  11330. {
  11331. stb__Entry *entries;
  11332. char *filename;
  11333. time_t timestamp;
  11334. char **file_data;
  11335. int file_len;
  11336. unsigned short *line_index;
  11337. } stb__FileEntry;
  11338. static void stb__constant_parse(stb__FileEntry *f, int i)
  11339. {
  11340. char *s;
  11341. int n;
  11342. if (!stb_arr_valid(f->entries, i)) return;
  11343. n = f->entries[i].line;
  11344. if (n >= f->file_len) return;
  11345. s = f->file_data[n];
  11346. switch (f->entries[i].type) {
  11347. case STB__CTYPE_float:
  11348. while (*s) {
  11349. if (!strncmp(s, "STB_D(", 6)) { s+=6; goto matched_float; }
  11350. if (!strncmp(s, "STB_F(", 6)) { s+=6; goto matched_float; }
  11351. if (!strncmp(s, "KD(", 3)) { s+=3; goto matched_float; }
  11352. if (!strncmp(s, "KF(", 3)) { s+=3; goto matched_float; }
  11353. ++s;
  11354. }
  11355. break;
  11356. matched_float:
  11357. f->entries[i].dval = strtod(s, NULL);
  11358. break;
  11359. case STB__CTYPE_int:
  11360. while (*s) {
  11361. if (!strncmp(s, "STB_I(", 6)) { s+=6; goto matched_int; }
  11362. if (!strncmp(s, "STB_UI(", 7)) { s+=7; goto matched_int; }
  11363. if (!strncmp(s, "KI(", 3)) { s+=3; goto matched_int; }
  11364. if (!strncmp(s, "KU(", 3)) { s+=3; goto matched_int; }
  11365. ++s;
  11366. }
  11367. break;
  11368. matched_int: {
  11369. int neg=0;
  11370. s = stb_skipwhite(s);
  11371. while (*s == '-') { neg = !neg; s = stb_skipwhite(s+1); } // handle '- - 5', pointlessly
  11372. if (s[0] == '0' && tolower(s[1]) == 'x')
  11373. f->entries[i].ival = strtol(s, NULL, 16);
  11374. else if (s[0] == '0')
  11375. f->entries[i].ival = strtol(s, NULL, 8);
  11376. else
  11377. f->entries[i].ival = strtol(s, NULL, 10);
  11378. if (neg) f->entries[i].ival = -f->entries[i].ival;
  11379. break;
  11380. }
  11381. case STB__CTYPE_string:
  11382. // @TODO
  11383. break;
  11384. }
  11385. }
  11386. static stb_sdict *stb__constant_file_hash;
  11387. stb__Entry *stb__constant_get_entry(char *filename, int line, int type)
  11388. {
  11389. int i;
  11390. stb__FileEntry *f;
  11391. if (stb__constant_file_hash == NULL)
  11392. stb__constant_file_hash = stb_sdict_new(STB_TRUE);
  11393. f = (stb__FileEntry*) stb_sdict_get(stb__constant_file_hash, filename);
  11394. if (f == NULL) {
  11395. char *s = stb__get_sourcefile_path(filename);
  11396. if (s == NULL || !stb_fexists(s)) return 0;
  11397. f = (stb__FileEntry *) malloc(sizeof(*f));
  11398. f->timestamp = stb_ftimestamp(s);
  11399. f->file_data = stb_stringfile(s, &f->file_len);
  11400. f->filename = stb_p_strdup(s); // cache the full path
  11401. f->entries = NULL;
  11402. f->line_index = 0;
  11403. stb_arr_setlen(f->line_index, f->file_len);
  11404. memset(f->line_index, 0xff, stb_arr_storage(f->line_index));
  11405. } else {
  11406. time_t t = stb_ftimestamp(f->filename);
  11407. if (f->timestamp != t) {
  11408. f->timestamp = t;
  11409. free(f->file_data);
  11410. f->file_data = stb_stringfile(f->filename, &f->file_len);
  11411. stb_arr_setlen(f->line_index, f->file_len);
  11412. for (i=0; i < stb_arr_len(f->entries); ++i)
  11413. stb__constant_parse(f, i);
  11414. }
  11415. }
  11416. if (line >= f->file_len) return 0;
  11417. if (f->line_index[line] >= stb_arr_len(f->entries)) {
  11418. // need a new entry
  11419. int n = stb_arr_len(f->entries);
  11420. stb__Entry e;
  11421. e.line = line;
  11422. if (line < f->file_len)
  11423. f->line_index[line] = n;
  11424. e.type = type;
  11425. stb_arr_push(f->entries, e);
  11426. stb__constant_parse(f, n);
  11427. }
  11428. return f->entries + f->line_index[line];
  11429. }
  11430. double stb__double_constant(char *file, int line, double x)
  11431. {
  11432. stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_float);
  11433. if (!e) return x;
  11434. return e->dval;
  11435. }
  11436. int stb__int_constant(char *file, int line, int x)
  11437. {
  11438. stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_int);
  11439. if (!e) return x;
  11440. return e->ival;
  11441. }
  11442. char * stb__string_constant(char *file, int line, char *x)
  11443. {
  11444. stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_string);
  11445. if (!e) return x;
  11446. return e->sval;
  11447. }
  11448. #endif // STB_DEFINE
  11449. #endif // !STB_DEBUG && !STB_ALWAYS_H
  11450. #undef STB_EXTERN
  11451. #endif // STB_INCLUDE_STB_H
  11452. /*
  11453. ------------------------------------------------------------------------------
  11454. This software is available under 2 licenses -- choose whichever you prefer.
  11455. ------------------------------------------------------------------------------
  11456. ALTERNATIVE A - MIT License
  11457. Copyright (c) 2017 Sean Barrett
  11458. Permission is hereby granted, free of charge, to any person obtaining a copy of
  11459. this software and associated documentation files (the "Software"), to deal in
  11460. the Software without restriction, including without limitation the rights to
  11461. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11462. of the Software, and to permit persons to whom the Software is furnished to do
  11463. so, subject to the following conditions:
  11464. The above copyright notice and this permission notice shall be included in all
  11465. copies or substantial portions of the Software.
  11466. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11467. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11468. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  11469. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11470. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  11471. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  11472. SOFTWARE.
  11473. ------------------------------------------------------------------------------
  11474. ALTERNATIVE B - Public Domain (www.unlicense.org)
  11475. This is free and unencumbered software released into the public domain.
  11476. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  11477. software, either in source code form or as a compiled binary, for any purpose,
  11478. commercial or non-commercial, and by any means.
  11479. In jurisdictions that recognize copyright laws, the author or authors of this
  11480. software dedicate any and all copyright interest in the software to the public
  11481. domain. We make this dedication for the benefit of the public at large and to
  11482. the detriment of our heirs and successors. We intend this dedication to be an
  11483. overt act of relinquishment in perpetuity of all present and future rights to
  11484. this software under copyright law.
  11485. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11486. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11487. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  11488. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  11489. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  11490. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11491. ------------------------------------------------------------------------------
  11492. */