SDL_stdinc.h 199 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryStdinc
  20. *
  21. * SDL provides its own implementation of some of the most important C runtime
  22. * functions.
  23. *
  24. * Using these functions allows an app to have access to common C
  25. * functionality without depending on a specific C runtime (or a C runtime at
  26. * all). More importantly, the SDL implementations work identically across
  27. * platforms, so apps can avoid surprises like snprintf() behaving differently
  28. * between Windows and Linux builds, or itoa() only existing on some
  29. * platforms.
  30. *
  31. * For many of the most common functions, like SDL_memcpy, SDL might just call
  32. * through to the usual C runtime behind the scenes, if it makes sense to do
  33. * so (if it's faster and always available/reliable on a given platform),
  34. * reducing library size and offering the most optimized option.
  35. *
  36. * SDL also offers other C-runtime-adjacent functionality in this header that
  37. * either isn't, strictly speaking, part of any C runtime standards, like
  38. * SDL_crc32() and SDL_reinterpret_cast, etc. It also offers a few better
  39. * options, like SDL_strlcpy(), which functions as a safer form of strcpy().
  40. */
  41. #ifndef SDL_stdinc_h_
  42. #define SDL_stdinc_h_
  43. #include <SDL3/SDL_platform_defines.h>
  44. #include <stdarg.h>
  45. #include <stdint.h>
  46. #include <string.h>
  47. #include <wchar.h>
  48. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  49. defined(SDL_INCLUDE_INTTYPES_H)
  50. #include <inttypes.h>
  51. #endif
  52. #ifndef __cplusplus
  53. #if defined(__has_include) && !defined(SDL_INCLUDE_STDBOOL_H)
  54. #if __has_include(<stdbool.h>)
  55. #define SDL_INCLUDE_STDBOOL_H
  56. #endif
  57. #endif
  58. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  59. (defined(_MSC_VER) && (_MSC_VER >= 1910 /* Visual Studio 2017 */)) || \
  60. defined(SDL_INCLUDE_STDBOOL_H)
  61. #include <stdbool.h>
  62. #elif !defined(__bool_true_false_are_defined) && !defined(bool)
  63. #define bool unsigned char
  64. #define false 0
  65. #define true 1
  66. #define __bool_true_false_are_defined 1
  67. #endif
  68. #endif /* !__cplusplus */
  69. #ifndef SDL_DISABLE_ALLOCA
  70. # ifndef alloca
  71. # ifdef HAVE_ALLOCA_H
  72. # include <alloca.h>
  73. # elif defined(SDL_PLATFORM_NETBSD)
  74. # if defined(__STRICT_ANSI__)
  75. # define SDL_DISABLE_ALLOCA
  76. # else
  77. # include <stdlib.h>
  78. # endif
  79. # elif defined(__GNUC__)
  80. # define alloca __builtin_alloca
  81. # elif defined(_MSC_VER)
  82. # include <malloc.h>
  83. # define alloca _alloca
  84. # elif defined(__WATCOMC__)
  85. # include <malloc.h>
  86. # elif defined(__BORLANDC__)
  87. # include <malloc.h>
  88. # elif defined(__DMC__)
  89. # include <stdlib.h>
  90. # elif defined(SDL_PLATFORM_AIX)
  91. # pragma alloca
  92. # elif defined(__MRC__)
  93. void *alloca(unsigned);
  94. # else
  95. void *alloca(size_t);
  96. # endif
  97. # endif
  98. #endif
  99. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  100. /**
  101. * Don't let SDL use "long long" C types.
  102. *
  103. * SDL will define this if it believes the compiler doesn't understand the
  104. * "long long" syntax for C datatypes. This can happen on older compilers.
  105. *
  106. * If _your_ compiler doesn't support "long long" but SDL doesn't know it, it
  107. * is safe to define this yourself to build against the SDL headers.
  108. *
  109. * If this is defined, it will remove access to some C runtime support
  110. * functions, like SDL_ulltoa and SDL_strtoll that refer to this datatype
  111. * explicitly. The rest of SDL will still be available.
  112. *
  113. * SDL's own source code cannot be built with a compiler that has this
  114. * defined, for various technical reasons.
  115. */
  116. #define SDL_NOLONGLONG 1
  117. #elif defined(_MSC_VER) && (_MSC_VER < 1310) /* long long introduced in Visual Studio.NET 2003 */
  118. # define SDL_NOLONGLONG 1
  119. #endif
  120. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  121. /**
  122. * The largest value that a `size_t` can hold for the target platform.
  123. *
  124. * `size_t` is generally the same size as a pointer in modern times, but this
  125. * can get weird on very old and very esoteric machines. For example, on a
  126. * 16-bit Intel 286, you might have a 32-bit "far" pointer (16-bit segment
  127. * plus 16-bit offset), but `size_t` is 16 bits, because it can only deal with
  128. * the offset into an individual segment.
  129. *
  130. * In modern times, it's generally expected to cover an entire linear address
  131. * space. But be careful!
  132. *
  133. * \since This macro is available since SDL 3.2.0.
  134. */
  135. #define SDL_SIZE_MAX SIZE_MAX
  136. #elif defined(SIZE_MAX)
  137. # define SDL_SIZE_MAX SIZE_MAX
  138. #else
  139. # define SDL_SIZE_MAX ((size_t) -1)
  140. #endif
  141. #ifndef SDL_COMPILE_TIME_ASSERT
  142. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  143. /**
  144. * A compile-time assertion.
  145. *
  146. * This can check constant values _known to the compiler at build time_ for
  147. * correctness, and end the compile with the error if they fail.
  148. *
  149. * Often times these are used to verify basic truths, like the size of a
  150. * datatype is what is expected:
  151. *
  152. * ```c
  153. * SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
  154. * ```
  155. *
  156. * The `name` parameter must be a valid C symbol, and must be unique across
  157. * all compile-time asserts in the same compilation unit (one run of the
  158. * compiler), or the build might fail with cryptic errors on some targets.
  159. * This is used with a C language trick that works on older compilers that
  160. * don't support better assertion techniques.
  161. *
  162. * If you need an assertion that operates at runtime, on variable data, you
  163. * should try SDL_assert instead.
  164. *
  165. * \param name a unique identifier for this assertion.
  166. * \param x the value to test. Must be a boolean value.
  167. *
  168. * \threadsafety This macro doesn't generate any code to run.
  169. *
  170. * \since This macro is available since SDL 3.2.0.
  171. *
  172. * \sa SDL_assert
  173. */
  174. #define SDL_COMPILE_TIME_ASSERT(name, x) FailToCompileIf_x_IsFalse(x)
  175. #elif defined(__cplusplus)
  176. /* Keep C++ case alone: Some versions of gcc will define __STDC_VERSION__ even when compiling in C++ mode. */
  177. #if (__cplusplus >= 201103L)
  178. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  179. #endif
  180. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
  181. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  182. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  183. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  184. #endif
  185. #endif /* !SDL_COMPILE_TIME_ASSERT */
  186. #ifndef SDL_COMPILE_TIME_ASSERT
  187. /* universal, but may trigger -Wunused-local-typedefs */
  188. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  189. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  190. #endif
  191. /**
  192. * The number of elements in a static array.
  193. *
  194. * This will compile but return incorrect results for a pointer to an array;
  195. * it has to be an array the compiler knows the size of.
  196. *
  197. * This macro looks like it double-evaluates the argument, but it does so
  198. * inside of `sizeof`, so there are no side-effects here, as expressions do
  199. * not actually run any code in these cases.
  200. *
  201. * \since This macro is available since SDL 3.2.0.
  202. */
  203. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  204. /**
  205. * Macro useful for building other macros with strings in them.
  206. *
  207. * \param arg the text to turn into a string literal.
  208. *
  209. * \since This macro is available since SDL 3.2.0.
  210. */
  211. #define SDL_STRINGIFY_ARG(arg) #arg
  212. /**
  213. * \name Cast operators
  214. *
  215. * Use proper C++ casts when compiled as C++ to be compatible with the option
  216. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  217. */
  218. /* @{ */
  219. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  220. /**
  221. * Handle a Reinterpret Cast properly whether using C or C++.
  222. *
  223. * If compiled as C++, this macro offers a proper C++ reinterpret_cast<>.
  224. *
  225. * If compiled as C, this macro does a normal C-style cast.
  226. *
  227. * This is helpful to avoid compiler warnings in C++.
  228. *
  229. * \param type the type to cast the expression to.
  230. * \param expression the expression to cast to a different type.
  231. * \returns `expression`, cast to `type`.
  232. *
  233. * \threadsafety It is safe to call this macro from any thread.
  234. *
  235. * \since This macro is available since SDL 3.2.0.
  236. *
  237. * \sa SDL_static_cast
  238. * \sa SDL_const_cast
  239. */
  240. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression) /* or `((type)(expression))` in C */
  241. /**
  242. * Handle a Static Cast properly whether using C or C++.
  243. *
  244. * If compiled as C++, this macro offers a proper C++ static_cast<>.
  245. *
  246. * If compiled as C, this macro does a normal C-style cast.
  247. *
  248. * This is helpful to avoid compiler warnings in C++.
  249. *
  250. * \param type the type to cast the expression to.
  251. * \param expression the expression to cast to a different type.
  252. * \returns `expression`, cast to `type`.
  253. *
  254. * \threadsafety It is safe to call this macro from any thread.
  255. *
  256. * \since This macro is available since SDL 3.2.0.
  257. *
  258. * \sa SDL_reinterpret_cast
  259. * \sa SDL_const_cast
  260. */
  261. #define SDL_static_cast(type, expression) static_cast<type>(expression) /* or `((type)(expression))` in C */
  262. /**
  263. * Handle a Const Cast properly whether using C or C++.
  264. *
  265. * If compiled as C++, this macro offers a proper C++ const_cast<>.
  266. *
  267. * If compiled as C, this macro does a normal C-style cast.
  268. *
  269. * This is helpful to avoid compiler warnings in C++.
  270. *
  271. * \param type the type to cast the expression to.
  272. * \param expression the expression to cast to a different type.
  273. * \returns `expression`, cast to `type`.
  274. *
  275. * \threadsafety It is safe to call this macro from any thread.
  276. *
  277. * \since This macro is available since SDL 3.2.0.
  278. *
  279. * \sa SDL_reinterpret_cast
  280. * \sa SDL_static_cast
  281. */
  282. #define SDL_const_cast(type, expression) const_cast<type>(expression) /* or `((type)(expression))` in C */
  283. #elif defined(__cplusplus)
  284. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  285. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  286. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  287. #else
  288. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  289. #define SDL_static_cast(type, expression) ((type)(expression))
  290. #define SDL_const_cast(type, expression) ((type)(expression))
  291. #endif
  292. /* @} *//* Cast operators */
  293. /**
  294. * Define a four character code as a Uint32.
  295. *
  296. * \param A the first ASCII character.
  297. * \param B the second ASCII character.
  298. * \param C the third ASCII character.
  299. * \param D the fourth ASCII character.
  300. * \returns the four characters converted into a Uint32, one character
  301. * per-byte.
  302. *
  303. * \threadsafety It is safe to call this macro from any thread.
  304. *
  305. * \since This macro is available since SDL 3.2.0.
  306. */
  307. #define SDL_FOURCC(A, B, C, D) \
  308. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  309. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  310. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  311. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  312. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  313. /**
  314. * Append the 64 bit integer suffix to a signed integer literal.
  315. *
  316. * This helps compilers that might believe a integer literal larger than
  317. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_SINT64_C(0xFFFFFFFF1)`
  318. * instead of `0xFFFFFFFF1` by itself.
  319. *
  320. * \since This macro is available since SDL 3.2.0.
  321. *
  322. * \sa SDL_UINT64_C
  323. */
  324. #define SDL_SINT64_C(c) c ## LL /* or whatever the current compiler uses. */
  325. /**
  326. * Append the 64 bit integer suffix to an unsigned integer literal.
  327. *
  328. * This helps compilers that might believe a integer literal larger than
  329. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_UINT64_C(0xFFFFFFFF1)`
  330. * instead of `0xFFFFFFFF1` by itself.
  331. *
  332. * \since This macro is available since SDL 3.2.0.
  333. *
  334. * \sa SDL_SINT64_C
  335. */
  336. #define SDL_UINT64_C(c) c ## ULL /* or whatever the current compiler uses. */
  337. #else /* !SDL_WIKI_DOCUMENTATION_SECTION */
  338. #ifndef SDL_SINT64_C
  339. #if defined(INT64_C)
  340. #define SDL_SINT64_C(c) INT64_C(c)
  341. #elif defined(_MSC_VER)
  342. #define SDL_SINT64_C(c) c ## i64
  343. #elif defined(__LP64__) || defined(_LP64)
  344. #define SDL_SINT64_C(c) c ## L
  345. #else
  346. #define SDL_SINT64_C(c) c ## LL
  347. #endif
  348. #endif /* !SDL_SINT64_C */
  349. #ifndef SDL_UINT64_C
  350. #if defined(UINT64_C)
  351. #define SDL_UINT64_C(c) UINT64_C(c)
  352. #elif defined(_MSC_VER)
  353. #define SDL_UINT64_C(c) c ## ui64
  354. #elif defined(__LP64__) || defined(_LP64)
  355. #define SDL_UINT64_C(c) c ## UL
  356. #else
  357. #define SDL_UINT64_C(c) c ## ULL
  358. #endif
  359. #endif /* !SDL_UINT64_C */
  360. #endif /* !SDL_WIKI_DOCUMENTATION_SECTION */
  361. /**
  362. * \name Basic data types
  363. */
  364. /* @{ */
  365. /**
  366. * A signed 8-bit integer type.
  367. *
  368. * \since This macro is available since SDL 3.2.0.
  369. */
  370. typedef int8_t Sint8;
  371. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  372. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  373. /**
  374. * An unsigned 8-bit integer type.
  375. *
  376. * \since This macro is available since SDL 3.2.0.
  377. */
  378. typedef uint8_t Uint8;
  379. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  380. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  381. /**
  382. * A signed 16-bit integer type.
  383. *
  384. * \since This macro is available since SDL 3.2.0.
  385. */
  386. typedef int16_t Sint16;
  387. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  388. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  389. /**
  390. * An unsigned 16-bit integer type.
  391. *
  392. * \since This macro is available since SDL 3.2.0.
  393. */
  394. typedef uint16_t Uint16;
  395. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  396. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  397. /**
  398. * A signed 32-bit integer type.
  399. *
  400. * \since This macro is available since SDL 3.2.0.
  401. */
  402. typedef int32_t Sint32;
  403. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  404. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  405. /**
  406. * An unsigned 32-bit integer type.
  407. *
  408. * \since This macro is available since SDL 3.2.0.
  409. */
  410. typedef uint32_t Uint32;
  411. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  412. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  413. /**
  414. * A signed 64-bit integer type.
  415. *
  416. * \since This macro is available since SDL 3.2.0.
  417. *
  418. * \sa SDL_SINT64_C
  419. */
  420. typedef int64_t Sint64;
  421. #define SDL_MAX_SINT64 SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* 9223372036854775807 */
  422. #define SDL_MIN_SINT64 ~SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* -9223372036854775808 */
  423. /**
  424. * An unsigned 64-bit integer type.
  425. *
  426. * \since This macro is available since SDL 3.2.0.
  427. *
  428. * \sa SDL_UINT64_C
  429. */
  430. typedef uint64_t Uint64;
  431. #define SDL_MAX_UINT64 SDL_UINT64_C(0xFFFFFFFFFFFFFFFF) /* 18446744073709551615 */
  432. #define SDL_MIN_UINT64 SDL_UINT64_C(0x0000000000000000) /* 0 */
  433. /**
  434. * SDL times are signed, 64-bit integers representing nanoseconds since the
  435. * Unix epoch (Jan 1, 1970).
  436. *
  437. * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS()
  438. * and SDL_SECONDS_TO_NS(), and between Windows FILETIME values with
  439. * SDL_TimeToWindows() and SDL_TimeFromWindows().
  440. *
  441. * \since This macro is available since SDL 3.2.0.
  442. *
  443. * \sa SDL_MAX_SINT64
  444. * \sa SDL_MIN_SINT64
  445. */
  446. typedef Sint64 SDL_Time;
  447. #define SDL_MAX_TIME SDL_MAX_SINT64
  448. #define SDL_MIN_TIME SDL_MIN_SINT64
  449. /* @} *//* Basic data types */
  450. /**
  451. * \name Floating-point constants
  452. */
  453. /* @{ */
  454. #ifdef FLT_EPSILON
  455. #define SDL_FLT_EPSILON FLT_EPSILON
  456. #else
  457. /**
  458. * Epsilon constant, used for comparing floating-point numbers.
  459. *
  460. * Equals by default to platform-defined `FLT_EPSILON`, or
  461. * `1.1920928955078125e-07F` if that's not available.
  462. *
  463. * \since This macro is available since SDL 3.2.0.
  464. */
  465. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  466. #endif
  467. /* @} *//* Floating-point constants */
  468. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  469. /**
  470. * A printf-formatting string for an Sint64 value.
  471. *
  472. * Use it like this:
  473. *
  474. * ```c
  475. * SDL_Log("There are %" SDL_PRIs64 " bottles of beer on the wall.", bottles);
  476. * ```
  477. *
  478. * \since This macro is available since SDL 3.2.0.
  479. */
  480. #define SDL_PRIs64 "lld"
  481. /**
  482. * A printf-formatting string for a Uint64 value.
  483. *
  484. * Use it like this:
  485. *
  486. * ```c
  487. * SDL_Log("There are %" SDL_PRIu64 " bottles of beer on the wall.", bottles);
  488. * ```
  489. *
  490. * \since This macro is available since SDL 3.2.0.
  491. */
  492. #define SDL_PRIu64 "llu"
  493. /**
  494. * A printf-formatting string for a Uint64 value as lower-case hexadecimal.
  495. *
  496. * Use it like this:
  497. *
  498. * ```c
  499. * SDL_Log("There are %" SDL_PRIx64 " bottles of beer on the wall.", bottles);
  500. * ```
  501. *
  502. * \since This macro is available since SDL 3.2.0.
  503. */
  504. #define SDL_PRIx64 "llx"
  505. /**
  506. * A printf-formatting string for a Uint64 value as upper-case hexadecimal.
  507. *
  508. * Use it like this:
  509. *
  510. * ```c
  511. * SDL_Log("There are %" SDL_PRIX64 " bottles of beer on the wall.", bottles);
  512. * ```
  513. *
  514. * \since This macro is available since SDL 3.2.0.
  515. */
  516. #define SDL_PRIX64 "llX"
  517. /**
  518. * A printf-formatting string for an Sint32 value.
  519. *
  520. * Use it like this:
  521. *
  522. * ```c
  523. * SDL_Log("There are %" SDL_PRIs32 " bottles of beer on the wall.", bottles);
  524. * ```
  525. *
  526. * \since This macro is available since SDL 3.2.0.
  527. */
  528. #define SDL_PRIs32 "d"
  529. /**
  530. * A printf-formatting string for a Uint32 value.
  531. *
  532. * Use it like this:
  533. *
  534. * ```c
  535. * SDL_Log("There are %" SDL_PRIu32 " bottles of beer on the wall.", bottles);
  536. * ```
  537. *
  538. * \since This macro is available since SDL 3.2.0.
  539. */
  540. #define SDL_PRIu32 "u"
  541. /**
  542. * A printf-formatting string for a Uint32 value as lower-case hexadecimal.
  543. *
  544. * Use it like this:
  545. *
  546. * ```c
  547. * SDL_Log("There are %" SDL_PRIx32 " bottles of beer on the wall.", bottles);
  548. * ```
  549. *
  550. * \since This macro is available since SDL 3.2.0.
  551. */
  552. #define SDL_PRIx32 "x"
  553. /**
  554. * A printf-formatting string for a Uint32 value as upper-case hexadecimal.
  555. *
  556. * Use it like this:
  557. *
  558. * ```c
  559. * SDL_Log("There are %" SDL_PRIX32 " bottles of beer on the wall.", bottles);
  560. * ```
  561. *
  562. * \since This macro is available since SDL 3.2.0.
  563. */
  564. #define SDL_PRIX32 "X"
  565. /**
  566. * A printf-formatting string prefix for a `long long` value.
  567. *
  568. * This is just the prefix! You probably actually want SDL_PRILLd, SDL_PRILLu,
  569. * SDL_PRILLx, or SDL_PRILLX instead.
  570. *
  571. * Use it like this:
  572. *
  573. * ```c
  574. * SDL_Log("There are %" SDL_PRILL_PREFIX "d bottles of beer on the wall.", bottles);
  575. * ```
  576. *
  577. * \since This macro is available since SDL 3.2.0.
  578. */
  579. #define SDL_PRILL_PREFIX "ll"
  580. /**
  581. * A printf-formatting string for a `long long` value.
  582. *
  583. * Use it like this:
  584. *
  585. * ```c
  586. * SDL_Log("There are %" SDL_PRILLd " bottles of beer on the wall.", bottles);
  587. * ```
  588. *
  589. * \since This macro is available since SDL 3.2.0.
  590. */
  591. #define SDL_PRILLd SDL_PRILL_PREFIX "d"
  592. /**
  593. * A printf-formatting string for a `unsigned long long` value.
  594. *
  595. * Use it like this:
  596. *
  597. * ```c
  598. * SDL_Log("There are %" SDL_PRILLu " bottles of beer on the wall.", bottles);
  599. * ```
  600. *
  601. * \since This macro is available since SDL 3.2.0.
  602. */
  603. #define SDL_PRILLu SDL_PRILL_PREFIX "u"
  604. /**
  605. * A printf-formatting string for an `unsigned long long` value as lower-case
  606. * hexadecimal.
  607. *
  608. * Use it like this:
  609. *
  610. * ```c
  611. * SDL_Log("There are %" SDL_PRILLx " bottles of beer on the wall.", bottles);
  612. * ```
  613. *
  614. * \since This macro is available since SDL 3.2.0.
  615. */
  616. #define SDL_PRILLx SDL_PRILL_PREFIX "x"
  617. /**
  618. * A printf-formatting string for an `unsigned long long` value as upper-case
  619. * hexadecimal.
  620. *
  621. * Use it like this:
  622. *
  623. * ```c
  624. * SDL_Log("There are %" SDL_PRILLX " bottles of beer on the wall.", bottles);
  625. * ```
  626. *
  627. * \since This macro is available since SDL 3.2.0.
  628. */
  629. #define SDL_PRILLX SDL_PRILL_PREFIX "X"
  630. #endif /* SDL_WIKI_DOCUMENTATION_SECTION */
  631. /* Make sure we have macros for printing width-based integers.
  632. * <inttypes.h> should define these but this is not true all platforms.
  633. * (for example win32) */
  634. #ifndef SDL_PRIs64
  635. #if defined(SDL_PLATFORM_WINDOWS)
  636. #define SDL_PRIs64 "I64d"
  637. #elif defined(PRId64)
  638. #define SDL_PRIs64 PRId64
  639. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  640. #define SDL_PRIs64 "ld"
  641. #else
  642. #define SDL_PRIs64 "lld"
  643. #endif
  644. #endif
  645. #ifndef SDL_PRIu64
  646. #if defined(SDL_PLATFORM_WINDOWS)
  647. #define SDL_PRIu64 "I64u"
  648. #elif defined(PRIu64)
  649. #define SDL_PRIu64 PRIu64
  650. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  651. #define SDL_PRIu64 "lu"
  652. #else
  653. #define SDL_PRIu64 "llu"
  654. #endif
  655. #endif
  656. #ifndef SDL_PRIx64
  657. #if defined(SDL_PLATFORM_WINDOWS)
  658. #define SDL_PRIx64 "I64x"
  659. #elif defined(PRIx64)
  660. #define SDL_PRIx64 PRIx64
  661. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  662. #define SDL_PRIx64 "lx"
  663. #else
  664. #define SDL_PRIx64 "llx"
  665. #endif
  666. #endif
  667. #ifndef SDL_PRIX64
  668. #if defined(SDL_PLATFORM_WINDOWS)
  669. #define SDL_PRIX64 "I64X"
  670. #elif defined(PRIX64)
  671. #define SDL_PRIX64 PRIX64
  672. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  673. #define SDL_PRIX64 "lX"
  674. #else
  675. #define SDL_PRIX64 "llX"
  676. #endif
  677. #endif
  678. #ifndef SDL_PRIs32
  679. #ifdef PRId32
  680. #define SDL_PRIs32 PRId32
  681. #else
  682. #define SDL_PRIs32 "d"
  683. #endif
  684. #endif
  685. #ifndef SDL_PRIu32
  686. #ifdef PRIu32
  687. #define SDL_PRIu32 PRIu32
  688. #else
  689. #define SDL_PRIu32 "u"
  690. #endif
  691. #endif
  692. #ifndef SDL_PRIx32
  693. #ifdef PRIx32
  694. #define SDL_PRIx32 PRIx32
  695. #else
  696. #define SDL_PRIx32 "x"
  697. #endif
  698. #endif
  699. #ifndef SDL_PRIX32
  700. #ifdef PRIX32
  701. #define SDL_PRIX32 PRIX32
  702. #else
  703. #define SDL_PRIX32 "X"
  704. #endif
  705. #endif
  706. /* Specifically for the `long long` -- SDL-specific. */
  707. #ifdef SDL_PLATFORM_WINDOWS
  708. #ifndef SDL_NOLONGLONG
  709. SDL_COMPILE_TIME_ASSERT(longlong_size64, sizeof(long long) == 8); /* using I64 for windows - make sure `long long` is 64 bits. */
  710. #endif
  711. #define SDL_PRILL_PREFIX "I64"
  712. #else
  713. #define SDL_PRILL_PREFIX "ll"
  714. #endif
  715. #ifndef SDL_PRILLd
  716. #define SDL_PRILLd SDL_PRILL_PREFIX "d"
  717. #endif
  718. #ifndef SDL_PRILLu
  719. #define SDL_PRILLu SDL_PRILL_PREFIX "u"
  720. #endif
  721. #ifndef SDL_PRILLx
  722. #define SDL_PRILLx SDL_PRILL_PREFIX "x"
  723. #endif
  724. #ifndef SDL_PRILLX
  725. #define SDL_PRILLX SDL_PRILL_PREFIX "X"
  726. #endif
  727. /* Annotations to help code analysis tools */
  728. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  729. /**
  730. * Macro that annotates function params with input buffer size.
  731. *
  732. * If we were to annotate `memcpy`:
  733. *
  734. * ```c
  735. * void *memcpy(void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  736. * ```
  737. *
  738. * This notes that `src` should be `len` bytes in size and is only read by the
  739. * function. The compiler or other analysis tools can warn when this doesn't
  740. * appear to be the case.
  741. *
  742. * On compilers without this annotation mechanism, this is defined to nothing.
  743. *
  744. * \since This macro is available since SDL 3.2.0.
  745. */
  746. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  747. /**
  748. * Macro that annotates function params with input/output string buffer size.
  749. *
  750. * If we were to annotate `strlcat`:
  751. *
  752. * ```c
  753. * size_t strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  754. * ```
  755. *
  756. * This notes that `dst` is a null-terminated C string, should be `maxlen`
  757. * bytes in size, and is both read from and written to by the function. The
  758. * compiler or other analysis tools can warn when this doesn't appear to be
  759. * the case.
  760. *
  761. * On compilers without this annotation mechanism, this is defined to nothing.
  762. *
  763. * \since This macro is available since SDL 3.2.0.
  764. */
  765. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  766. /**
  767. * Macro that annotates function params with output string buffer size.
  768. *
  769. * If we were to annotate `snprintf`:
  770. *
  771. * ```c
  772. * int snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, ...);
  773. * ```
  774. *
  775. * This notes that `text` is a null-terminated C string, should be `maxlen`
  776. * bytes in size, and is only written to by the function. The compiler or
  777. * other analysis tools can warn when this doesn't appear to be the case.
  778. *
  779. * On compilers without this annotation mechanism, this is defined to nothing.
  780. *
  781. * \since This macro is available since SDL 3.2.0.
  782. */
  783. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  784. /**
  785. * Macro that annotates function params with output buffer size.
  786. *
  787. * If we were to annotate `wcsncpy`:
  788. *
  789. * ```c
  790. * char *wcscpy(SDL_OUT_CAP(bufsize) wchar_t *dst, const wchar_t *src, size_t bufsize);
  791. * ```
  792. *
  793. * This notes that `dst` should have a capacity of `bufsize` wchar_t in size,
  794. * and is only written to by the function. The compiler or other analysis
  795. * tools can warn when this doesn't appear to be the case.
  796. *
  797. * This operates on counts of objects, not bytes. Use SDL_OUT_BYTECAP for
  798. * bytes.
  799. *
  800. * On compilers without this annotation mechanism, this is defined to nothing.
  801. *
  802. * \since This macro is available since SDL 3.2.0.
  803. */
  804. #define SDL_OUT_CAP(x) _Out_cap_(x)
  805. /**
  806. * Macro that annotates function params with output buffer size.
  807. *
  808. * If we were to annotate `memcpy`:
  809. *
  810. * ```c
  811. * void *memcpy(SDL_OUT_BYTECAP(bufsize) void *dst, const void *src, size_t bufsize);
  812. * ```
  813. *
  814. * This notes that `dst` should have a capacity of `bufsize` bytes in size,
  815. * and is only written to by the function. The compiler or other analysis
  816. * tools can warn when this doesn't appear to be the case.
  817. *
  818. * On compilers without this annotation mechanism, this is defined to nothing.
  819. *
  820. * \since This macro is available since SDL 3.2.0.
  821. */
  822. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  823. /**
  824. * Macro that annotates function params with output buffer string size.
  825. *
  826. * If we were to annotate `strcpy`:
  827. *
  828. * ```c
  829. * char *strcpy(SDL_OUT_Z_BYTECAP(bufsize) char *dst, const char *src, size_t bufsize);
  830. * ```
  831. *
  832. * This notes that `dst` should have a capacity of `bufsize` bytes in size,
  833. * and a zero-terminated string is written to it by the function. The compiler
  834. * or other analysis tools can warn when this doesn't appear to be the case.
  835. *
  836. * On compilers without this annotation mechanism, this is defined to nothing.
  837. *
  838. * \since This macro is available since SDL 3.2.0.
  839. */
  840. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  841. /**
  842. * Macro that annotates function params as printf-style format strings.
  843. *
  844. * If we were to annotate `fprintf`:
  845. *
  846. * ```c
  847. * int fprintf(FILE *f, SDL_PRINTF_FORMAT_STRING const char *fmt, ...);
  848. * ```
  849. *
  850. * This notes that `fmt` should be a printf-style format string. The compiler
  851. * or other analysis tools can warn when this doesn't appear to be the case.
  852. *
  853. * On compilers without this annotation mechanism, this is defined to nothing.
  854. *
  855. * \since This macro is available since SDL 3.2.0.
  856. */
  857. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  858. /**
  859. * Macro that annotates function params as scanf-style format strings.
  860. *
  861. * If we were to annotate `fscanf`:
  862. *
  863. * ```c
  864. * int fscanf(FILE *f, SDL_SCANF_FORMAT_STRING const char *fmt, ...);
  865. * ```
  866. *
  867. * This notes that `fmt` should be a scanf-style format string. The compiler
  868. * or other analysis tools can warn when this doesn't appear to be the case.
  869. *
  870. * On compilers without this annotation mechanism, this is defined to nothing.
  871. *
  872. * \since This macro is available since SDL 3.2.0.
  873. */
  874. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  875. /**
  876. * Macro that annotates a vararg function that operates like printf.
  877. *
  878. * If we were to annotate `fprintf`:
  879. *
  880. * ```c
  881. * int fprintf(FILE *f, const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  882. * ```
  883. *
  884. * This notes that the second parameter should be a printf-style format
  885. * string, followed by `...`. The compiler or other analysis tools can warn
  886. * when this doesn't appear to be the case.
  887. *
  888. * On compilers without this annotation mechanism, this is defined to nothing.
  889. *
  890. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  891. * between them will cover at least Visual Studio, GCC, and Clang.
  892. *
  893. * \since This macro is available since SDL 3.2.0.
  894. */
  895. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  896. /**
  897. * Macro that annotates a va_list function that operates like printf.
  898. *
  899. * If we were to annotate `vfprintf`:
  900. *
  901. * ```c
  902. * int vfprintf(FILE *f, const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  903. * ```
  904. *
  905. * This notes that the second parameter should be a printf-style format
  906. * string, followed by a va_list. The compiler or other analysis tools can
  907. * warn when this doesn't appear to be the case.
  908. *
  909. * On compilers without this annotation mechanism, this is defined to nothing.
  910. *
  911. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  912. * between them will cover at least Visual Studio, GCC, and Clang.
  913. *
  914. * \since This macro is available since SDL 3.2.0.
  915. */
  916. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  917. /**
  918. * Macro that annotates a vararg function that operates like scanf.
  919. *
  920. * If we were to annotate `fscanf`:
  921. *
  922. * ```c
  923. * int fscanf(FILE *f, const char *fmt, ...) SDL_PRINTF_VARARG_FUNCV(2);
  924. * ```
  925. *
  926. * This notes that the second parameter should be a scanf-style format string,
  927. * followed by `...`. The compiler or other analysis tools can warn when this
  928. * doesn't appear to be the case.
  929. *
  930. * On compilers without this annotation mechanism, this is defined to nothing.
  931. *
  932. * This can (and should) be used with SDL_SCANF_FORMAT_STRING as well, which
  933. * between them will cover at least Visual Studio, GCC, and Clang.
  934. *
  935. * \since This macro is available since SDL 3.2.0.
  936. */
  937. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  938. /**
  939. * Macro that annotates a va_list function that operates like scanf.
  940. *
  941. * If we were to annotate `vfscanf`:
  942. *
  943. * ```c
  944. * int vfscanf(FILE *f, const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  945. * ```
  946. *
  947. * This notes that the second parameter should be a scanf-style format string,
  948. * followed by a va_list. The compiler or other analysis tools can warn when
  949. * this doesn't appear to be the case.
  950. *
  951. * On compilers without this annotation mechanism, this is defined to nothing.
  952. *
  953. * This can (and should) be used with SDL_SCANF_FORMAT_STRING as well, which
  954. * between them will cover at least Visual Studio, GCC, and Clang.
  955. *
  956. * \since This macro is available since SDL 3.2.0.
  957. */
  958. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  959. /**
  960. * Macro that annotates a vararg function that operates like wprintf.
  961. *
  962. * If we were to annotate `fwprintf`:
  963. *
  964. * ```c
  965. * int fwprintf(FILE *f, const wchar_t *fmt, ...) SDL_WPRINTF_VARARG_FUNC(2);
  966. * ```
  967. *
  968. * This notes that the second parameter should be a wprintf-style format wide
  969. * string, followed by `...`. The compiler or other analysis tools can warn
  970. * when this doesn't appear to be the case.
  971. *
  972. * On compilers without this annotation mechanism, this is defined to nothing.
  973. *
  974. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  975. * between them will cover at least Visual Studio, GCC, and Clang.
  976. *
  977. * \since This macro is available since SDL 3.2.0.
  978. */
  979. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  980. /**
  981. * Macro that annotates a va_list function that operates like wprintf.
  982. *
  983. * If we were to annotate `vfwprintf`:
  984. *
  985. * ```c
  986. * int vfwprintf(FILE *f, const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNC(2);
  987. * ```
  988. *
  989. * This notes that the second parameter should be a wprintf-style format wide
  990. * string, followed by a va_list. The compiler or other analysis tools can
  991. * warn when this doesn't appear to be the case.
  992. *
  993. * On compilers without this annotation mechanism, this is defined to nothing.
  994. *
  995. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  996. * between them will cover at least Visual Studio, GCC, and Clang.
  997. *
  998. * \since This macro is available since SDL 3.2.0.
  999. */
  1000. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
  1001. #elif defined(SDL_DISABLE_ANALYZE_MACROS)
  1002. #define SDL_IN_BYTECAP(x)
  1003. #define SDL_INOUT_Z_CAP(x)
  1004. #define SDL_OUT_Z_CAP(x)
  1005. #define SDL_OUT_CAP(x)
  1006. #define SDL_OUT_BYTECAP(x)
  1007. #define SDL_OUT_Z_BYTECAP(x)
  1008. #define SDL_PRINTF_FORMAT_STRING
  1009. #define SDL_SCANF_FORMAT_STRING
  1010. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  1011. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  1012. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  1013. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  1014. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  1015. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  1016. #else
  1017. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  1018. #include <sal.h>
  1019. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  1020. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  1021. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  1022. #define SDL_OUT_CAP(x) _Out_cap_(x)
  1023. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  1024. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  1025. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  1026. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  1027. #else
  1028. #define SDL_IN_BYTECAP(x)
  1029. #define SDL_INOUT_Z_CAP(x)
  1030. #define SDL_OUT_Z_CAP(x)
  1031. #define SDL_OUT_CAP(x)
  1032. #define SDL_OUT_BYTECAP(x)
  1033. #define SDL_OUT_Z_BYTECAP(x)
  1034. #define SDL_PRINTF_FORMAT_STRING
  1035. #define SDL_SCANF_FORMAT_STRING
  1036. #endif
  1037. #if defined(__GNUC__) || defined(__clang__)
  1038. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  1039. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  1040. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  1041. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  1042. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  1043. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
  1044. #else
  1045. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  1046. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  1047. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  1048. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  1049. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  1050. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  1051. #endif
  1052. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  1053. /** \cond */
  1054. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  1055. SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(bool) == 1);
  1056. SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1);
  1057. SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1);
  1058. SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2);
  1059. SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2);
  1060. SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
  1061. SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4);
  1062. SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8);
  1063. SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8);
  1064. #ifndef SDL_NOLONGLONG
  1065. SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long));
  1066. SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long));
  1067. #endif
  1068. typedef struct SDL_alignment_test
  1069. {
  1070. Uint8 a;
  1071. void *b;
  1072. } SDL_alignment_test;
  1073. SDL_COMPILE_TIME_ASSERT(struct_alignment, sizeof(SDL_alignment_test) == (2 * sizeof(void *)));
  1074. SDL_COMPILE_TIME_ASSERT(two_s_complement, SDL_static_cast(int, ~SDL_static_cast(int, 0)) == SDL_static_cast(int, -1));
  1075. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  1076. /** \endcond */
  1077. /* Check to make sure enums are the size of ints, for structure packing.
  1078. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  1079. enums having the size of an int must be enabled.
  1080. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  1081. */
  1082. /** \cond */
  1083. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  1084. #if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
  1085. /* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
  1086. typedef enum SDL_DUMMY_ENUM
  1087. {
  1088. DUMMY_ENUM_VALUE
  1089. } SDL_DUMMY_ENUM;
  1090. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  1091. #endif
  1092. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  1093. /** \endcond */
  1094. #include <SDL3/SDL_begin_code.h>
  1095. /* Set up for C function definitions, even when using C++ */
  1096. #ifdef __cplusplus
  1097. extern "C" {
  1098. #endif
  1099. /**
  1100. * A macro to initialize an SDL interface.
  1101. *
  1102. * This macro will initialize an SDL interface structure and should be called
  1103. * before you fill out the fields with your implementation.
  1104. *
  1105. * You can use it like this:
  1106. *
  1107. * ```c
  1108. * SDL_IOStreamInterface iface;
  1109. *
  1110. * SDL_INIT_INTERFACE(&iface);
  1111. *
  1112. * // Fill in the interface function pointers with your implementation
  1113. * iface.seek = ...
  1114. *
  1115. * stream = SDL_OpenIO(&iface, NULL);
  1116. * ```
  1117. *
  1118. * If you are using designated initializers, you can use the size of the
  1119. * interface as the version, e.g.
  1120. *
  1121. * ```c
  1122. * SDL_IOStreamInterface iface = {
  1123. * .version = sizeof(iface),
  1124. * .seek = ...
  1125. * };
  1126. * stream = SDL_OpenIO(&iface, NULL);
  1127. * ```
  1128. *
  1129. * \threadsafety It is safe to call this macro from any thread.
  1130. *
  1131. * \since This macro is available since SDL 3.2.0.
  1132. *
  1133. * \sa SDL_IOStreamInterface
  1134. * \sa SDL_StorageInterface
  1135. * \sa SDL_VirtualJoystickDesc
  1136. */
  1137. #define SDL_INIT_INTERFACE(iface) \
  1138. do { \
  1139. SDL_zerop(iface); \
  1140. (iface)->version = sizeof(*(iface)); \
  1141. } while (0)
  1142. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  1143. /**
  1144. * Allocate memory on the stack (maybe).
  1145. *
  1146. * If SDL knows how to access alloca() on the current platform, it will use it
  1147. * to stack-allocate memory here. If it doesn't, it will use SDL_malloc() to
  1148. * heap-allocate memory.
  1149. *
  1150. * Since this might not be stack memory at all, it's important that you check
  1151. * the returned pointer for NULL, and that you call SDL_stack_free on the
  1152. * memory when done with it. Since this might be stack memory, it's important
  1153. * that you don't allocate large amounts of it, or allocate in a loop without
  1154. * returning from the function, so the stack doesn't overflow.
  1155. *
  1156. * \param type the datatype of the memory to allocate.
  1157. * \param count the number of `type` objects to allocate.
  1158. * \returns newly-allocated memory, or NULL on failure.
  1159. *
  1160. * \threadsafety It is safe to call this macro from any thread.
  1161. *
  1162. * \since This macro is available since SDL 3.2.0.
  1163. *
  1164. * \sa SDL_stack_free
  1165. */
  1166. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  1167. /**
  1168. * Free memory previously allocated with SDL_stack_alloc.
  1169. *
  1170. * If SDL used alloca() to allocate this memory, this macro does nothing and
  1171. * the allocated memory will be automatically released when the function that
  1172. * called SDL_stack_alloc() returns. If SDL used SDL_malloc(), it will
  1173. * SDL_free the memory immediately.
  1174. *
  1175. * \param data the pointer, from SDL_stack_alloc(), to free.
  1176. *
  1177. * \threadsafety It is safe to call this macro from any thread.
  1178. *
  1179. * \since This macro is available since SDL 3.2.0.
  1180. *
  1181. * \sa SDL_stack_alloc
  1182. */
  1183. #define SDL_stack_free(data)
  1184. #elif !defined(SDL_DISABLE_ALLOCA)
  1185. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  1186. #define SDL_stack_free(data)
  1187. #else
  1188. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  1189. #define SDL_stack_free(data) SDL_free(data)
  1190. #endif
  1191. /**
  1192. * Allocate uninitialized memory.
  1193. *
  1194. * The allocated memory returned by this function must be freed with
  1195. * SDL_free().
  1196. *
  1197. * If `size` is 0, it will be set to 1.
  1198. *
  1199. * If the allocation is successful, the returned pointer is guaranteed to be
  1200. * aligned to either the *fundamental alignment* (`alignof(max_align_t)` in
  1201. * C11 and later) or `2 * sizeof(void *)`, whichever is smaller. Use
  1202. * SDL_aligned_alloc() if you need to allocate memory aligned to an alignment
  1203. * greater than this guarantee.
  1204. *
  1205. * \param size the size to allocate.
  1206. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  1207. *
  1208. * \threadsafety It is safe to call this function from any thread.
  1209. *
  1210. * \since This function is available since SDL 3.2.0.
  1211. *
  1212. * \sa SDL_free
  1213. * \sa SDL_calloc
  1214. * \sa SDL_realloc
  1215. * \sa SDL_aligned_alloc
  1216. */
  1217. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_malloc(size_t size);
  1218. /**
  1219. * Allocate a zero-initialized array.
  1220. *
  1221. * The memory returned by this function must be freed with SDL_free().
  1222. *
  1223. * If either of `nmemb` or `size` is 0, they will both be set to 1.
  1224. *
  1225. * If the allocation is successful, the returned pointer is guaranteed to be
  1226. * aligned to either the *fundamental alignment* (`alignof(max_align_t)` in
  1227. * C11 and later) or `2 * sizeof(void *)`, whichever is smaller.
  1228. *
  1229. * \param nmemb the number of elements in the array.
  1230. * \param size the size of each element of the array.
  1231. * \returns a pointer to the allocated array, or NULL if allocation failed.
  1232. *
  1233. * \threadsafety It is safe to call this function from any thread.
  1234. *
  1235. * \since This function is available since SDL 3.2.0.
  1236. *
  1237. * \sa SDL_free
  1238. * \sa SDL_malloc
  1239. * \sa SDL_realloc
  1240. */
  1241. extern SDL_DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
  1242. /**
  1243. * Change the size of allocated memory.
  1244. *
  1245. * The memory returned by this function must be freed with SDL_free().
  1246. *
  1247. * If `size` is 0, it will be set to 1. Note that this is unlike some other C
  1248. * runtime `realloc` implementations, which may treat `realloc(mem, 0)` the
  1249. * same way as `free(mem)`.
  1250. *
  1251. * If `mem` is NULL, the behavior of this function is equivalent to
  1252. * SDL_malloc(). Otherwise, the function can have one of three possible
  1253. * outcomes:
  1254. *
  1255. * - If it returns the same pointer as `mem`, it means that `mem` was resized
  1256. * in place without freeing.
  1257. * - If it returns a different non-NULL pointer, it means that `mem` was freed
  1258. * and cannot be dereferenced anymore.
  1259. * - If it returns NULL (indicating failure), then `mem` will remain valid and
  1260. * must still be freed with SDL_free().
  1261. *
  1262. * If the allocation is successfully resized, the returned pointer is
  1263. * guaranteed to be aligned to either the *fundamental alignment*
  1264. * (`alignof(max_align_t)` in C11 and later) or `2 * sizeof(void *)`,
  1265. * whichever is smaller.
  1266. *
  1267. * \param mem a pointer to allocated memory to reallocate, or NULL.
  1268. * \param size the new size of the memory.
  1269. * \returns a pointer to the newly allocated memory, or NULL if allocation
  1270. * failed.
  1271. *
  1272. * \threadsafety It is safe to call this function from any thread.
  1273. *
  1274. * \since This function is available since SDL 3.2.0.
  1275. *
  1276. * \sa SDL_free
  1277. * \sa SDL_malloc
  1278. * \sa SDL_calloc
  1279. */
  1280. extern SDL_DECLSPEC SDL_ALLOC_SIZE(2) void * SDLCALL SDL_realloc(void *mem, size_t size);
  1281. /**
  1282. * Free allocated memory.
  1283. *
  1284. * The pointer is no longer valid after this call and cannot be dereferenced
  1285. * anymore.
  1286. *
  1287. * If `mem` is NULL, this function does nothing.
  1288. *
  1289. * \param mem a pointer to allocated memory, or NULL.
  1290. *
  1291. * \threadsafety It is safe to call this function from any thread.
  1292. *
  1293. * \since This function is available since SDL 3.2.0.
  1294. *
  1295. * \sa SDL_malloc
  1296. * \sa SDL_calloc
  1297. * \sa SDL_realloc
  1298. */
  1299. extern SDL_DECLSPEC void SDLCALL SDL_free(void *mem);
  1300. /**
  1301. * A callback used to implement SDL_malloc().
  1302. *
  1303. * SDL will always ensure that the passed `size` is greater than 0.
  1304. *
  1305. * \param size the size to allocate.
  1306. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  1307. *
  1308. * \threadsafety It should be safe to call this callback from any thread.
  1309. *
  1310. * \since This datatype is available since SDL 3.2.0.
  1311. *
  1312. * \sa SDL_malloc
  1313. * \sa SDL_GetOriginalMemoryFunctions
  1314. * \sa SDL_GetMemoryFunctions
  1315. * \sa SDL_SetMemoryFunctions
  1316. */
  1317. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  1318. /**
  1319. * A callback used to implement SDL_calloc().
  1320. *
  1321. * SDL will always ensure that the passed `nmemb` and `size` are both greater
  1322. * than 0.
  1323. *
  1324. * \param nmemb the number of elements in the array.
  1325. * \param size the size of each element of the array.
  1326. * \returns a pointer to the allocated array, or NULL if allocation failed.
  1327. *
  1328. * \threadsafety It should be safe to call this callback from any thread.
  1329. *
  1330. * \since This datatype is available since SDL 3.2.0.
  1331. *
  1332. * \sa SDL_calloc
  1333. * \sa SDL_GetOriginalMemoryFunctions
  1334. * \sa SDL_GetMemoryFunctions
  1335. * \sa SDL_SetMemoryFunctions
  1336. */
  1337. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  1338. /**
  1339. * A callback used to implement SDL_realloc().
  1340. *
  1341. * SDL will always ensure that the passed `size` is greater than 0.
  1342. *
  1343. * \param mem a pointer to allocated memory to reallocate, or NULL.
  1344. * \param size the new size of the memory.
  1345. * \returns a pointer to the newly allocated memory, or NULL if allocation
  1346. * failed.
  1347. *
  1348. * \threadsafety It should be safe to call this callback from any thread.
  1349. *
  1350. * \since This datatype is available since SDL 3.2.0.
  1351. *
  1352. * \sa SDL_realloc
  1353. * \sa SDL_GetOriginalMemoryFunctions
  1354. * \sa SDL_GetMemoryFunctions
  1355. * \sa SDL_SetMemoryFunctions
  1356. */
  1357. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  1358. /**
  1359. * A callback used to implement SDL_free().
  1360. *
  1361. * SDL will always ensure that the passed `mem` is a non-NULL pointer.
  1362. *
  1363. * \param mem a pointer to allocated memory.
  1364. *
  1365. * \threadsafety It should be safe to call this callback from any thread.
  1366. *
  1367. * \since This datatype is available since SDL 3.2.0.
  1368. *
  1369. * \sa SDL_free
  1370. * \sa SDL_GetOriginalMemoryFunctions
  1371. * \sa SDL_GetMemoryFunctions
  1372. * \sa SDL_SetMemoryFunctions
  1373. */
  1374. typedef void (SDLCALL *SDL_free_func)(void *mem);
  1375. /**
  1376. * Get the original set of SDL memory functions.
  1377. *
  1378. * This is what SDL_malloc and friends will use by default, if there has been
  1379. * no call to SDL_SetMemoryFunctions. This is not necessarily using the C
  1380. * runtime's `malloc` functions behind the scenes! Different platforms and
  1381. * build configurations might do any number of unexpected things.
  1382. *
  1383. * \param malloc_func filled with malloc function.
  1384. * \param calloc_func filled with calloc function.
  1385. * \param realloc_func filled with realloc function.
  1386. * \param free_func filled with free function.
  1387. *
  1388. * \threadsafety It is safe to call this function from any thread.
  1389. *
  1390. * \since This function is available since SDL 3.2.0.
  1391. */
  1392. extern SDL_DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  1393. SDL_calloc_func *calloc_func,
  1394. SDL_realloc_func *realloc_func,
  1395. SDL_free_func *free_func);
  1396. /**
  1397. * Get the current set of SDL memory functions.
  1398. *
  1399. * \param malloc_func filled with malloc function.
  1400. * \param calloc_func filled with calloc function.
  1401. * \param realloc_func filled with realloc function.
  1402. * \param free_func filled with free function.
  1403. *
  1404. * \threadsafety This does not hold a lock, so do not call this in the
  1405. * unlikely event of a background thread calling
  1406. * SDL_SetMemoryFunctions simultaneously.
  1407. *
  1408. * \since This function is available since SDL 3.2.0.
  1409. *
  1410. * \sa SDL_SetMemoryFunctions
  1411. * \sa SDL_GetOriginalMemoryFunctions
  1412. */
  1413. extern SDL_DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
  1414. SDL_calloc_func *calloc_func,
  1415. SDL_realloc_func *realloc_func,
  1416. SDL_free_func *free_func);
  1417. /**
  1418. * Replace SDL's memory allocation functions with a custom set.
  1419. *
  1420. * It is not safe to call this function once any allocations have been made,
  1421. * as future calls to SDL_free will use the new allocator, even if they came
  1422. * from an SDL_malloc made with the old one!
  1423. *
  1424. * If used, usually this needs to be the first call made into the SDL library,
  1425. * if not the very first thing done at program startup time.
  1426. *
  1427. * \param malloc_func custom malloc function.
  1428. * \param calloc_func custom calloc function.
  1429. * \param realloc_func custom realloc function.
  1430. * \param free_func custom free function.
  1431. * \returns true on success or false on failure; call SDL_GetError() for more
  1432. * information.
  1433. *
  1434. * \threadsafety It is safe to call this function from any thread, but one
  1435. * should not replace the memory functions once any allocations
  1436. * are made!
  1437. *
  1438. * \since This function is available since SDL 3.2.0.
  1439. *
  1440. * \sa SDL_GetMemoryFunctions
  1441. * \sa SDL_GetOriginalMemoryFunctions
  1442. */
  1443. extern SDL_DECLSPEC bool SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  1444. SDL_calloc_func calloc_func,
  1445. SDL_realloc_func realloc_func,
  1446. SDL_free_func free_func);
  1447. /**
  1448. * Allocate memory aligned to a specific alignment.
  1449. *
  1450. * The memory returned by this function must be freed with SDL_aligned_free(),
  1451. * _not_ SDL_free().
  1452. *
  1453. * If `alignment` is less than the size of `void *`, it will be increased to
  1454. * match that.
  1455. *
  1456. * The returned memory address will be a multiple of the alignment value, and
  1457. * the size of the memory allocated will be a multiple of the alignment value.
  1458. *
  1459. * \param alignment the alignment of the memory.
  1460. * \param size the size to allocate.
  1461. * \returns a pointer to the aligned memory, or NULL if allocation failed.
  1462. *
  1463. * \threadsafety It is safe to call this function from any thread.
  1464. *
  1465. * \since This function is available since SDL 3.2.0.
  1466. *
  1467. * \sa SDL_aligned_free
  1468. */
  1469. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  1470. /**
  1471. * Free memory allocated by SDL_aligned_alloc().
  1472. *
  1473. * The pointer is no longer valid after this call and cannot be dereferenced
  1474. * anymore.
  1475. *
  1476. * If `mem` is NULL, this function does nothing.
  1477. *
  1478. * \param mem a pointer previously returned by SDL_aligned_alloc(), or NULL.
  1479. *
  1480. * \threadsafety It is safe to call this function from any thread.
  1481. *
  1482. * \since This function is available since SDL 3.2.0.
  1483. *
  1484. * \sa SDL_aligned_alloc
  1485. */
  1486. extern SDL_DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  1487. /**
  1488. * Get the number of outstanding (unfreed) allocations.
  1489. *
  1490. * \returns the number of allocations or -1 if allocation counting is
  1491. * disabled.
  1492. *
  1493. * \threadsafety It is safe to call this function from any thread.
  1494. *
  1495. * \since This function is available since SDL 3.2.0.
  1496. */
  1497. extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  1498. /**
  1499. * A thread-safe set of environment variables
  1500. *
  1501. * \since This struct is available since SDL 3.2.0.
  1502. *
  1503. * \sa SDL_GetEnvironment
  1504. * \sa SDL_CreateEnvironment
  1505. * \sa SDL_GetEnvironmentVariable
  1506. * \sa SDL_GetEnvironmentVariables
  1507. * \sa SDL_SetEnvironmentVariable
  1508. * \sa SDL_UnsetEnvironmentVariable
  1509. * \sa SDL_DestroyEnvironment
  1510. */
  1511. typedef struct SDL_Environment SDL_Environment;
  1512. /**
  1513. * Get the process environment.
  1514. *
  1515. * This is initialized at application start and is not affected by setenv()
  1516. * and unsetenv() calls after that point. Use SDL_SetEnvironmentVariable() and
  1517. * SDL_UnsetEnvironmentVariable() if you want to modify this environment, or
  1518. * SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
  1519. * in the C runtime environment after SDL_Quit().
  1520. *
  1521. * \returns a pointer to the environment for the process or NULL on failure;
  1522. * call SDL_GetError() for more information.
  1523. *
  1524. * \threadsafety It is safe to call this function from any thread.
  1525. *
  1526. * \since This function is available since SDL 3.2.0.
  1527. *
  1528. * \sa SDL_GetEnvironmentVariable
  1529. * \sa SDL_GetEnvironmentVariables
  1530. * \sa SDL_SetEnvironmentVariable
  1531. * \sa SDL_UnsetEnvironmentVariable
  1532. */
  1533. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
  1534. /**
  1535. * Create a set of environment variables
  1536. *
  1537. * \param populated true to initialize it from the C runtime environment,
  1538. * false to create an empty environment.
  1539. * \returns a pointer to the new environment or NULL on failure; call
  1540. * SDL_GetError() for more information.
  1541. *
  1542. * \threadsafety If `populated` is false, it is safe to call this function
  1543. * from any thread, otherwise it is safe if no other threads are
  1544. * calling setenv() or unsetenv()
  1545. *
  1546. * \since This function is available since SDL 3.2.0.
  1547. *
  1548. * \sa SDL_GetEnvironmentVariable
  1549. * \sa SDL_GetEnvironmentVariables
  1550. * \sa SDL_SetEnvironmentVariable
  1551. * \sa SDL_UnsetEnvironmentVariable
  1552. * \sa SDL_DestroyEnvironment
  1553. */
  1554. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_CreateEnvironment(bool populated);
  1555. /**
  1556. * Get the value of a variable in the environment.
  1557. *
  1558. * \param env the environment to query.
  1559. * \param name the name of the variable to get.
  1560. * \returns a pointer to the value of the variable or NULL if it can't be
  1561. * found.
  1562. *
  1563. * \threadsafety It is safe to call this function from any thread.
  1564. *
  1565. * \since This function is available since SDL 3.2.0.
  1566. *
  1567. * \sa SDL_GetEnvironment
  1568. * \sa SDL_CreateEnvironment
  1569. * \sa SDL_GetEnvironmentVariables
  1570. * \sa SDL_SetEnvironmentVariable
  1571. * \sa SDL_UnsetEnvironmentVariable
  1572. */
  1573. extern SDL_DECLSPEC const char * SDLCALL SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name);
  1574. /**
  1575. * Get all variables in the environment.
  1576. *
  1577. * \param env the environment to query.
  1578. * \returns a NULL terminated array of pointers to environment variables in
  1579. * the form "variable=value" or NULL on failure; call SDL_GetError()
  1580. * for more information. This is a single allocation that should be
  1581. * freed with SDL_free() when it is no longer needed.
  1582. *
  1583. * \threadsafety It is safe to call this function from any thread.
  1584. *
  1585. * \since This function is available since SDL 3.2.0.
  1586. *
  1587. * \sa SDL_GetEnvironment
  1588. * \sa SDL_CreateEnvironment
  1589. * \sa SDL_GetEnvironmentVariables
  1590. * \sa SDL_SetEnvironmentVariable
  1591. * \sa SDL_UnsetEnvironmentVariable
  1592. */
  1593. extern SDL_DECLSPEC char ** SDLCALL SDL_GetEnvironmentVariables(SDL_Environment *env);
  1594. /**
  1595. * Set the value of a variable in the environment.
  1596. *
  1597. * \param env the environment to modify.
  1598. * \param name the name of the variable to set.
  1599. * \param value the value of the variable to set.
  1600. * \param overwrite true to overwrite the variable if it exists, false to
  1601. * return success without setting the variable if it already
  1602. * exists.
  1603. * \returns true on success or false on failure; call SDL_GetError() for more
  1604. * information.
  1605. *
  1606. * \threadsafety It is safe to call this function from any thread.
  1607. *
  1608. * \since This function is available since SDL 3.2.0.
  1609. *
  1610. * \sa SDL_GetEnvironment
  1611. * \sa SDL_CreateEnvironment
  1612. * \sa SDL_GetEnvironmentVariable
  1613. * \sa SDL_GetEnvironmentVariables
  1614. * \sa SDL_UnsetEnvironmentVariable
  1615. */
  1616. extern SDL_DECLSPEC bool SDLCALL SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite);
  1617. /**
  1618. * Clear a variable from the environment.
  1619. *
  1620. * \param env the environment to modify.
  1621. * \param name the name of the variable to unset.
  1622. * \returns true on success or false on failure; call SDL_GetError() for more
  1623. * information.
  1624. *
  1625. * \threadsafety It is safe to call this function from any thread.
  1626. *
  1627. * \since This function is available since SDL 3.2.0.
  1628. *
  1629. * \sa SDL_GetEnvironment
  1630. * \sa SDL_CreateEnvironment
  1631. * \sa SDL_GetEnvironmentVariable
  1632. * \sa SDL_GetEnvironmentVariables
  1633. * \sa SDL_SetEnvironmentVariable
  1634. * \sa SDL_UnsetEnvironmentVariable
  1635. */
  1636. extern SDL_DECLSPEC bool SDLCALL SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name);
  1637. /**
  1638. * Destroy a set of environment variables.
  1639. *
  1640. * \param env the environment to destroy.
  1641. *
  1642. * \threadsafety It is safe to call this function from any thread, as long as
  1643. * the environment is no longer in use.
  1644. *
  1645. * \since This function is available since SDL 3.2.0.
  1646. *
  1647. * \sa SDL_CreateEnvironment
  1648. */
  1649. extern SDL_DECLSPEC void SDLCALL SDL_DestroyEnvironment(SDL_Environment *env);
  1650. /**
  1651. * Get the value of a variable in the environment.
  1652. *
  1653. * This function uses SDL's cached copy of the environment and is thread-safe.
  1654. *
  1655. * \param name the name of the variable to get.
  1656. * \returns a pointer to the value of the variable or NULL if it can't be
  1657. * found.
  1658. *
  1659. * \threadsafety It is safe to call this function from any thread.
  1660. *
  1661. * \since This function is available since SDL 3.2.0.
  1662. */
  1663. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv(const char *name);
  1664. /**
  1665. * Get the value of a variable in the environment.
  1666. *
  1667. * This function bypasses SDL's cached copy of the environment and is not
  1668. * thread-safe.
  1669. *
  1670. * \param name the name of the variable to get.
  1671. * \returns a pointer to the value of the variable or NULL if it can't be
  1672. * found.
  1673. *
  1674. * \threadsafety This function is not thread safe, consider using SDL_getenv()
  1675. * instead.
  1676. *
  1677. * \since This function is available since SDL 3.2.0.
  1678. *
  1679. * \sa SDL_getenv
  1680. */
  1681. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv_unsafe(const char *name);
  1682. /**
  1683. * Set the value of a variable in the environment.
  1684. *
  1685. * \param name the name of the variable to set.
  1686. * \param value the value of the variable to set.
  1687. * \param overwrite 1 to overwrite the variable if it exists, 0 to return
  1688. * success without setting the variable if it already exists.
  1689. * \returns 0 on success, -1 on error.
  1690. *
  1691. * \threadsafety This function is not thread safe, consider using
  1692. * SDL_SetEnvironmentVariable() instead.
  1693. *
  1694. * \since This function is available since SDL 3.2.0.
  1695. *
  1696. * \sa SDL_SetEnvironmentVariable
  1697. */
  1698. extern SDL_DECLSPEC int SDLCALL SDL_setenv_unsafe(const char *name, const char *value, int overwrite);
  1699. /**
  1700. * Clear a variable from the environment.
  1701. *
  1702. * \param name the name of the variable to unset.
  1703. * \returns 0 on success, -1 on error.
  1704. *
  1705. * \threadsafety This function is not thread safe, consider using
  1706. * SDL_UnsetEnvironmentVariable() instead.
  1707. *
  1708. * \since This function is available since SDL 3.2.0.
  1709. *
  1710. * \sa SDL_UnsetEnvironmentVariable
  1711. */
  1712. extern SDL_DECLSPEC int SDLCALL SDL_unsetenv_unsafe(const char *name);
  1713. /**
  1714. * A callback used with SDL sorting and binary search functions.
  1715. *
  1716. * \param a a pointer to the first element being compared.
  1717. * \param b a pointer to the second element being compared.
  1718. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1719. * before `a`, 0 if they are equal. If two elements are equal, their
  1720. * order in the sorted array is undefined.
  1721. *
  1722. * \since This callback is available since SDL 3.2.0.
  1723. *
  1724. * \sa SDL_bsearch
  1725. * \sa SDL_qsort
  1726. */
  1727. typedef int (SDLCALL *SDL_CompareCallback)(const void *a, const void *b);
  1728. /**
  1729. * Sort an array.
  1730. *
  1731. * For example:
  1732. *
  1733. * ```c
  1734. * typedef struct {
  1735. * int key;
  1736. * const char *string;
  1737. * } data;
  1738. *
  1739. * int SDLCALL compare(const void *a, const void *b)
  1740. * {
  1741. * const data *A = (const data *)a;
  1742. * const data *B = (const data *)b;
  1743. *
  1744. * if (A->n < B->n) {
  1745. * return -1;
  1746. * } else if (B->n < A->n) {
  1747. * return 1;
  1748. * } else {
  1749. * return 0;
  1750. * }
  1751. * }
  1752. *
  1753. * data values[] = {
  1754. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1755. * };
  1756. *
  1757. * SDL_qsort(values, SDL_arraysize(values), sizeof(values[0]), compare);
  1758. * ```
  1759. *
  1760. * \param base a pointer to the start of the array.
  1761. * \param nmemb the number of elements in the array.
  1762. * \param size the size of the elements in the array.
  1763. * \param compare a function used to compare elements in the array.
  1764. *
  1765. * \threadsafety It is safe to call this function from any thread.
  1766. *
  1767. * \since This function is available since SDL 3.2.0.
  1768. *
  1769. * \sa SDL_bsearch
  1770. * \sa SDL_qsort_r
  1771. */
  1772. extern SDL_DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1773. /**
  1774. * Perform a binary search on a previously sorted array.
  1775. *
  1776. * For example:
  1777. *
  1778. * ```c
  1779. * typedef struct {
  1780. * int key;
  1781. * const char *string;
  1782. * } data;
  1783. *
  1784. * int SDLCALL compare(const void *a, const void *b)
  1785. * {
  1786. * const data *A = (const data *)a;
  1787. * const data *B = (const data *)b;
  1788. *
  1789. * if (A->n < B->n) {
  1790. * return -1;
  1791. * } else if (B->n < A->n) {
  1792. * return 1;
  1793. * } else {
  1794. * return 0;
  1795. * }
  1796. * }
  1797. *
  1798. * data values[] = {
  1799. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1800. * };
  1801. * data key = { 2, NULL };
  1802. *
  1803. * data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);
  1804. * ```
  1805. *
  1806. * \param key a pointer to a key equal to the element being searched for.
  1807. * \param base a pointer to the start of the array.
  1808. * \param nmemb the number of elements in the array.
  1809. * \param size the size of the elements in the array.
  1810. * \param compare a function used to compare elements in the array.
  1811. * \returns a pointer to the matching element in the array, or NULL if not
  1812. * found.
  1813. *
  1814. * \threadsafety It is safe to call this function from any thread.
  1815. *
  1816. * \since This function is available since SDL 3.2.0.
  1817. *
  1818. * \sa SDL_bsearch_r
  1819. * \sa SDL_qsort
  1820. */
  1821. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1822. /**
  1823. * A callback used with SDL sorting and binary search functions.
  1824. *
  1825. * \param userdata the `userdata` pointer passed to the sort function.
  1826. * \param a a pointer to the first element being compared.
  1827. * \param b a pointer to the second element being compared.
  1828. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1829. * before `a`, 0 if they are equal. If two elements are equal, their
  1830. * order in the sorted array is undefined.
  1831. *
  1832. * \since This callback is available since SDL 3.2.0.
  1833. *
  1834. * \sa SDL_qsort_r
  1835. * \sa SDL_bsearch_r
  1836. */
  1837. typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);
  1838. /**
  1839. * Sort an array, passing a userdata pointer to the compare function.
  1840. *
  1841. * For example:
  1842. *
  1843. * ```c
  1844. * typedef enum {
  1845. * sort_increasing,
  1846. * sort_decreasing,
  1847. * } sort_method;
  1848. *
  1849. * typedef struct {
  1850. * int key;
  1851. * const char *string;
  1852. * } data;
  1853. *
  1854. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1855. * {
  1856. * sort_method method = (sort_method)(uintptr_t)userdata;
  1857. * const data *A = (const data *)a;
  1858. * const data *B = (const data *)b;
  1859. *
  1860. * if (A->key < B->key) {
  1861. * return (method == sort_increasing) ? -1 : 1;
  1862. * } else if (B->key < A->key) {
  1863. * return (method == sort_increasing) ? 1 : -1;
  1864. * } else {
  1865. * return 0;
  1866. * }
  1867. * }
  1868. *
  1869. * data values[] = {
  1870. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1871. * };
  1872. *
  1873. * SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1874. * ```
  1875. *
  1876. * \param base a pointer to the start of the array.
  1877. * \param nmemb the number of elements in the array.
  1878. * \param size the size of the elements in the array.
  1879. * \param compare a function used to compare elements in the array.
  1880. * \param userdata a pointer to pass to the compare function.
  1881. *
  1882. * \threadsafety It is safe to call this function from any thread.
  1883. *
  1884. * \since This function is available since SDL 3.2.0.
  1885. *
  1886. * \sa SDL_bsearch_r
  1887. * \sa SDL_qsort
  1888. */
  1889. extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1890. /**
  1891. * Perform a binary search on a previously sorted array, passing a userdata
  1892. * pointer to the compare function.
  1893. *
  1894. * For example:
  1895. *
  1896. * ```c
  1897. * typedef enum {
  1898. * sort_increasing,
  1899. * sort_decreasing,
  1900. * } sort_method;
  1901. *
  1902. * typedef struct {
  1903. * int key;
  1904. * const char *string;
  1905. * } data;
  1906. *
  1907. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1908. * {
  1909. * sort_method method = (sort_method)(uintptr_t)userdata;
  1910. * const data *A = (const data *)a;
  1911. * const data *B = (const data *)b;
  1912. *
  1913. * if (A->key < B->key) {
  1914. * return (method == sort_increasing) ? -1 : 1;
  1915. * } else if (B->key < A->key) {
  1916. * return (method == sort_increasing) ? 1 : -1;
  1917. * } else {
  1918. * return 0;
  1919. * }
  1920. * }
  1921. *
  1922. * data values[] = {
  1923. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1924. * };
  1925. * data key = { 2, NULL };
  1926. *
  1927. * data *result = SDL_bsearch_r(&key, values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1928. * ```
  1929. *
  1930. * \param key a pointer to a key equal to the element being searched for.
  1931. * \param base a pointer to the start of the array.
  1932. * \param nmemb the number of elements in the array.
  1933. * \param size the size of the elements in the array.
  1934. * \param compare a function used to compare elements in the array.
  1935. * \param userdata a pointer to pass to the compare function.
  1936. * \returns a pointer to the matching element in the array, or NULL if not
  1937. * found.
  1938. *
  1939. * \threadsafety It is safe to call this function from any thread.
  1940. *
  1941. * \since This function is available since SDL 3.2.0.
  1942. *
  1943. * \sa SDL_bsearch
  1944. * \sa SDL_qsort_r
  1945. */
  1946. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1947. /**
  1948. * Compute the absolute value of `x`.
  1949. *
  1950. * \param x an integer value.
  1951. * \returns the absolute value of x.
  1952. *
  1953. * \threadsafety It is safe to call this function from any thread.
  1954. *
  1955. * \since This function is available since SDL 3.2.0.
  1956. */
  1957. extern SDL_DECLSPEC int SDLCALL SDL_abs(int x);
  1958. /**
  1959. * Return the lesser of two values.
  1960. *
  1961. * This is a helper macro that might be more clear than writing out the
  1962. * comparisons directly, and works with any type that can be compared with the
  1963. * `<` operator. However, it double-evaluates both its parameters, so do not
  1964. * use expressions with side-effects here.
  1965. *
  1966. * \param x the first value to compare.
  1967. * \param y the second value to compare.
  1968. * \returns the lesser of `x` and `y`.
  1969. *
  1970. * \threadsafety It is safe to call this macro from any thread.
  1971. *
  1972. * \since This macro is available since SDL 3.2.0.
  1973. */
  1974. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  1975. /**
  1976. * Return the greater of two values.
  1977. *
  1978. * This is a helper macro that might be more clear than writing out the
  1979. * comparisons directly, and works with any type that can be compared with the
  1980. * `>` operator. However, it double-evaluates both its parameters, so do not
  1981. * use expressions with side-effects here.
  1982. *
  1983. * \param x the first value to compare.
  1984. * \param y the second value to compare.
  1985. * \returns the lesser of `x` and `y`.
  1986. *
  1987. * \threadsafety It is safe to call this macro from any thread.
  1988. *
  1989. * \since This macro is available since SDL 3.2.0.
  1990. */
  1991. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  1992. /**
  1993. * Return a value clamped to a range.
  1994. *
  1995. * If `x` is outside the range a values between `a` and `b`, the returned
  1996. * value will be `a` or `b` as appropriate. Otherwise, `x` is returned.
  1997. *
  1998. * This macro will produce incorrect results if `b` is less than `a`.
  1999. *
  2000. * This is a helper macro that might be more clear than writing out the
  2001. * comparisons directly, and works with any type that can be compared with the
  2002. * `<` and `>` operators. However, it double-evaluates all its parameters, so
  2003. * do not use expressions with side-effects here.
  2004. *
  2005. * \param x the value to compare.
  2006. * \param a the low end value.
  2007. * \param b the high end value.
  2008. * \returns x, clamped between a and b.
  2009. *
  2010. * \threadsafety It is safe to call this macro from any thread.
  2011. *
  2012. * \since This macro is available since SDL 3.2.0.
  2013. */
  2014. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  2015. /**
  2016. * Query if a character is alphabetic (a letter).
  2017. *
  2018. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2019. * for English 'a-z' and 'A-Z' as true.
  2020. *
  2021. * \param x character value to check.
  2022. * \returns non-zero if x falls within the character class, zero otherwise.
  2023. *
  2024. * \threadsafety It is safe to call this function from any thread.
  2025. *
  2026. * \since This function is available since SDL 3.2.0.
  2027. */
  2028. extern SDL_DECLSPEC int SDLCALL SDL_isalpha(int x);
  2029. /**
  2030. * Query if a character is alphabetic (a letter) or a number.
  2031. *
  2032. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2033. * for English 'a-z', 'A-Z', and '0-9' as true.
  2034. *
  2035. * \param x character value to check.
  2036. * \returns non-zero if x falls within the character class, zero otherwise.
  2037. *
  2038. * \threadsafety It is safe to call this function from any thread.
  2039. *
  2040. * \since This function is available since SDL 3.2.0.
  2041. */
  2042. extern SDL_DECLSPEC int SDLCALL SDL_isalnum(int x);
  2043. /**
  2044. * Report if a character is blank (a space or tab).
  2045. *
  2046. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2047. * 0x20 (space) or 0x9 (tab) as true.
  2048. *
  2049. * \param x character value to check.
  2050. * \returns non-zero if x falls within the character class, zero otherwise.
  2051. *
  2052. * \threadsafety It is safe to call this function from any thread.
  2053. *
  2054. * \since This function is available since SDL 3.2.0.
  2055. */
  2056. extern SDL_DECLSPEC int SDLCALL SDL_isblank(int x);
  2057. /**
  2058. * Report if a character is a control character.
  2059. *
  2060. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2061. * 0 through 0x1F, and 0x7F, as true.
  2062. *
  2063. * \param x character value to check.
  2064. * \returns non-zero if x falls within the character class, zero otherwise.
  2065. *
  2066. * \threadsafety It is safe to call this function from any thread.
  2067. *
  2068. * \since This function is available since SDL 3.2.0.
  2069. */
  2070. extern SDL_DECLSPEC int SDLCALL SDL_iscntrl(int x);
  2071. /**
  2072. * Report if a character is a numeric digit.
  2073. *
  2074. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2075. * '0' (0x30) through '9' (0x39), as true.
  2076. *
  2077. * \param x character value to check.
  2078. * \returns non-zero if x falls within the character class, zero otherwise.
  2079. *
  2080. * \threadsafety It is safe to call this function from any thread.
  2081. *
  2082. * \since This function is available since SDL 3.2.0.
  2083. */
  2084. extern SDL_DECLSPEC int SDLCALL SDL_isdigit(int x);
  2085. /**
  2086. * Report if a character is a hexadecimal digit.
  2087. *
  2088. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2089. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  2090. *
  2091. * \param x character value to check.
  2092. * \returns non-zero if x falls within the character class, zero otherwise.
  2093. *
  2094. * \threadsafety It is safe to call this function from any thread.
  2095. *
  2096. * \since This function is available since SDL 3.2.0.
  2097. */
  2098. extern SDL_DECLSPEC int SDLCALL SDL_isxdigit(int x);
  2099. /**
  2100. * Report if a character is a punctuation mark.
  2101. *
  2102. * **WARNING**: Regardless of system locale, this is equivalent to
  2103. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  2104. *
  2105. * \param x character value to check.
  2106. * \returns non-zero if x falls within the character class, zero otherwise.
  2107. *
  2108. * \threadsafety It is safe to call this function from any thread.
  2109. *
  2110. * \since This function is available since SDL 3.2.0.
  2111. *
  2112. * \sa SDL_isgraph
  2113. * \sa SDL_isalnum
  2114. */
  2115. extern SDL_DECLSPEC int SDLCALL SDL_ispunct(int x);
  2116. /**
  2117. * Report if a character is whitespace.
  2118. *
  2119. * **WARNING**: Regardless of system locale, this will only treat the
  2120. * following ASCII values as true:
  2121. *
  2122. * - space (0x20)
  2123. * - tab (0x09)
  2124. * - newline (0x0A)
  2125. * - vertical tab (0x0B)
  2126. * - form feed (0x0C)
  2127. * - return (0x0D)
  2128. *
  2129. * \param x character value to check.
  2130. * \returns non-zero if x falls within the character class, zero otherwise.
  2131. *
  2132. * \threadsafety It is safe to call this function from any thread.
  2133. *
  2134. * \since This function is available since SDL 3.2.0.
  2135. */
  2136. extern SDL_DECLSPEC int SDLCALL SDL_isspace(int x);
  2137. /**
  2138. * Report if a character is upper case.
  2139. *
  2140. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2141. * 'A' through 'Z' as true.
  2142. *
  2143. * \param x character value to check.
  2144. * \returns non-zero if x falls within the character class, zero otherwise.
  2145. *
  2146. * \threadsafety It is safe to call this function from any thread.
  2147. *
  2148. * \since This function is available since SDL 3.2.0.
  2149. */
  2150. extern SDL_DECLSPEC int SDLCALL SDL_isupper(int x);
  2151. /**
  2152. * Report if a character is lower case.
  2153. *
  2154. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2155. * 'a' through 'z' as true.
  2156. *
  2157. * \param x character value to check.
  2158. * \returns non-zero if x falls within the character class, zero otherwise.
  2159. *
  2160. * \threadsafety It is safe to call this function from any thread.
  2161. *
  2162. * \since This function is available since SDL 3.2.0.
  2163. */
  2164. extern SDL_DECLSPEC int SDLCALL SDL_islower(int x);
  2165. /**
  2166. * Report if a character is "printable".
  2167. *
  2168. * Be advised that "printable" has a definition that goes back to text
  2169. * terminals from the dawn of computing, making this a sort of special case
  2170. * function that is not suitable for Unicode (or most any) text management.
  2171. *
  2172. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2173. * ' ' (0x20) through '~' (0x7E) as true.
  2174. *
  2175. * \param x character value to check.
  2176. * \returns non-zero if x falls within the character class, zero otherwise.
  2177. *
  2178. * \threadsafety It is safe to call this function from any thread.
  2179. *
  2180. * \since This function is available since SDL 3.2.0.
  2181. */
  2182. extern SDL_DECLSPEC int SDLCALL SDL_isprint(int x);
  2183. /**
  2184. * Report if a character is any "printable" except space.
  2185. *
  2186. * Be advised that "printable" has a definition that goes back to text
  2187. * terminals from the dawn of computing, making this a sort of special case
  2188. * function that is not suitable for Unicode (or most any) text management.
  2189. *
  2190. * **WARNING**: Regardless of system locale, this is equivalent to
  2191. * `(SDL_isprint(x)) && ((x) != ' ')`.
  2192. *
  2193. * \param x character value to check.
  2194. * \returns non-zero if x falls within the character class, zero otherwise.
  2195. *
  2196. * \threadsafety It is safe to call this function from any thread.
  2197. *
  2198. * \since This function is available since SDL 3.2.0.
  2199. *
  2200. * \sa SDL_isprint
  2201. */
  2202. extern SDL_DECLSPEC int SDLCALL SDL_isgraph(int x);
  2203. /**
  2204. * Convert low-ASCII English letters to uppercase.
  2205. *
  2206. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2207. * values 'a' through 'z' to uppercase.
  2208. *
  2209. * This function returns the uppercase equivalent of `x`. If a character
  2210. * cannot be converted, or is already uppercase, this function returns `x`.
  2211. *
  2212. * \param x character value to check.
  2213. * \returns capitalized version of x, or x if no conversion available.
  2214. *
  2215. * \threadsafety It is safe to call this function from any thread.
  2216. *
  2217. * \since This function is available since SDL 3.2.0.
  2218. */
  2219. extern SDL_DECLSPEC int SDLCALL SDL_toupper(int x);
  2220. /**
  2221. * Convert low-ASCII English letters to lowercase.
  2222. *
  2223. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2224. * values 'A' through 'Z' to lowercase.
  2225. *
  2226. * This function returns the lowercase equivalent of `x`. If a character
  2227. * cannot be converted, or is already lowercase, this function returns `x`.
  2228. *
  2229. * \param x character value to check.
  2230. * \returns lowercase version of x, or x if no conversion available.
  2231. *
  2232. * \threadsafety It is safe to call this function from any thread.
  2233. *
  2234. * \since This function is available since SDL 3.2.0.
  2235. */
  2236. extern SDL_DECLSPEC int SDLCALL SDL_tolower(int x);
  2237. /**
  2238. * Calculate a CRC-16 value.
  2239. *
  2240. * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
  2241. *
  2242. * This function can be called multiple times, to stream data to be
  2243. * checksummed in blocks. Each call must provide the previous CRC-16 return
  2244. * value to be updated with the next block. The first call to this function
  2245. * for a set of blocks should pass in a zero CRC value.
  2246. *
  2247. * \param crc the current checksum for this data set, or 0 for a new data set.
  2248. * \param data a new block of data to add to the checksum.
  2249. * \param len the size, in bytes, of the new block of data.
  2250. * \returns a CRC-16 checksum value of all blocks in the data set.
  2251. *
  2252. * \threadsafety It is safe to call this function from any thread.
  2253. *
  2254. * \since This function is available since SDL 3.2.0.
  2255. */
  2256. extern SDL_DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  2257. /**
  2258. * Calculate a CRC-32 value.
  2259. *
  2260. * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
  2261. *
  2262. * This function can be called multiple times, to stream data to be
  2263. * checksummed in blocks. Each call must provide the previous CRC-32 return
  2264. * value to be updated with the next block. The first call to this function
  2265. * for a set of blocks should pass in a zero CRC value.
  2266. *
  2267. * \param crc the current checksum for this data set, or 0 for a new data set.
  2268. * \param data a new block of data to add to the checksum.
  2269. * \param len the size, in bytes, of the new block of data.
  2270. * \returns a CRC-32 checksum value of all blocks in the data set.
  2271. *
  2272. * \threadsafety It is safe to call this function from any thread.
  2273. *
  2274. * \since This function is available since SDL 3.2.0.
  2275. */
  2276. extern SDL_DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  2277. /**
  2278. * Calculate a 32-bit MurmurHash3 value for a block of data.
  2279. *
  2280. * https://en.wikipedia.org/wiki/MurmurHash
  2281. *
  2282. * A seed may be specified, which changes the final results consistently, but
  2283. * this does not work like SDL_crc16 and SDL_crc32: you can't feed a previous
  2284. * result from this function back into itself as the next seed value to
  2285. * calculate a hash in chunks; it won't produce the same hash as it would if
  2286. * the same data was provided in a single call.
  2287. *
  2288. * If you aren't sure what to provide for a seed, zero is fine. Murmur3 is not
  2289. * cryptographically secure, so it shouldn't be used for hashing top-secret
  2290. * data.
  2291. *
  2292. * \param data the data to be hashed.
  2293. * \param len the size of data, in bytes.
  2294. * \param seed a value that alters the final hash value.
  2295. * \returns a Murmur3 32-bit hash value.
  2296. *
  2297. * \threadsafety It is safe to call this function from any thread.
  2298. *
  2299. * \since This function is available since SDL 3.2.0.
  2300. */
  2301. extern SDL_DECLSPEC Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed);
  2302. /**
  2303. * Copy non-overlapping memory.
  2304. *
  2305. * The memory regions must not overlap. If they do, use SDL_memmove() instead.
  2306. *
  2307. * \param dst The destination memory region. Must not be NULL, and must not
  2308. * overlap with `src`.
  2309. * \param src The source memory region. Must not be NULL, and must not overlap
  2310. * with `dst`.
  2311. * \param len The length in bytes of both `dst` and `src`.
  2312. * \returns `dst`.
  2313. *
  2314. * \threadsafety It is safe to call this function from any thread.
  2315. *
  2316. * \since This function is available since SDL 3.2.0.
  2317. *
  2318. * \sa SDL_memmove
  2319. */
  2320. extern SDL_DECLSPEC void * SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  2321. /* Take advantage of compiler optimizations for memcpy */
  2322. #ifndef SDL_SLOW_MEMCPY
  2323. #ifdef SDL_memcpy
  2324. #undef SDL_memcpy
  2325. #endif
  2326. #define SDL_memcpy memcpy
  2327. #endif
  2328. /**
  2329. * A macro to copy memory between objects, with basic type checking.
  2330. *
  2331. * SDL_memcpy and SDL_memmove do not care where you copy memory to and from,
  2332. * which can lead to bugs. This macro aims to avoid most of those bugs by
  2333. * making sure that the source and destination are both pointers to objects
  2334. * that are the same size. It does not check that the objects are the same
  2335. * _type_, just that the copy will not overflow either object.
  2336. *
  2337. * The size check happens at compile time, and the compiler will throw an
  2338. * error if the objects are different sizes.
  2339. *
  2340. * Generally this is intended to copy a single object, not an array.
  2341. *
  2342. * This macro looks like it double-evaluates its parameters, but the extras
  2343. * them are in `sizeof` sections, which generate no code nor side-effects.
  2344. *
  2345. * \param dst a pointer to the destination object. Must not be NULL.
  2346. * \param src a pointer to the source object. Must not be NULL.
  2347. *
  2348. * \threadsafety It is safe to call this function from any thread.
  2349. *
  2350. * \since This function is available since SDL 3.2.0.
  2351. */
  2352. #define SDL_copyp(dst, src) \
  2353. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  2354. SDL_memcpy((dst), (src), sizeof(*(src)))
  2355. /**
  2356. * Copy memory ranges that might overlap.
  2357. *
  2358. * It is okay for the memory regions to overlap. If you are confident that the
  2359. * regions never overlap, using SDL_memcpy() may improve performance.
  2360. *
  2361. * \param dst The destination memory region. Must not be NULL.
  2362. * \param src The source memory region. Must not be NULL.
  2363. * \param len The length in bytes of both `dst` and `src`.
  2364. * \returns `dst`.
  2365. *
  2366. * \threadsafety It is safe to call this function from any thread.
  2367. *
  2368. * \since This function is available since SDL 3.2.0.
  2369. *
  2370. * \sa SDL_memcpy
  2371. */
  2372. extern SDL_DECLSPEC void * SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  2373. /* Take advantage of compiler optimizations for memmove */
  2374. #ifndef SDL_SLOW_MEMMOVE
  2375. #ifdef SDL_memmove
  2376. #undef SDL_memmove
  2377. #endif
  2378. #define SDL_memmove memmove
  2379. #endif
  2380. /**
  2381. * Initialize all bytes of buffer of memory to a specific value.
  2382. *
  2383. * This function will set `len` bytes, pointed to by `dst`, to the value
  2384. * specified in `c`.
  2385. *
  2386. * Despite `c` being an `int` instead of a `char`, this only operates on
  2387. * bytes; `c` must be a value between 0 and 255, inclusive.
  2388. *
  2389. * \param dst the destination memory region. Must not be NULL.
  2390. * \param c the byte value to set.
  2391. * \param len the length, in bytes, to set in `dst`.
  2392. * \returns `dst`.
  2393. *
  2394. * \threadsafety It is safe to call this function from any thread.
  2395. *
  2396. * \since This function is available since SDL 3.2.0.
  2397. */
  2398. extern SDL_DECLSPEC void * SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  2399. /**
  2400. * Initialize all 32-bit words of buffer of memory to a specific value.
  2401. *
  2402. * This function will set a buffer of `dwords` Uint32 values, pointed to by
  2403. * `dst`, to the value specified in `val`.
  2404. *
  2405. * Unlike SDL_memset, this sets 32-bit values, not bytes, so it's not limited
  2406. * to a range of 0-255.
  2407. *
  2408. * \param dst the destination memory region. Must not be NULL.
  2409. * \param val the Uint32 value to set.
  2410. * \param dwords the number of Uint32 values to set in `dst`.
  2411. * \returns `dst`.
  2412. *
  2413. * \threadsafety It is safe to call this function from any thread.
  2414. *
  2415. * \since This function is available since SDL 3.2.0.
  2416. */
  2417. extern SDL_DECLSPEC void * SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  2418. /* Take advantage of compiler optimizations for memset */
  2419. #ifndef SDL_SLOW_MEMSET
  2420. #ifdef SDL_memset
  2421. #undef SDL_memset
  2422. #endif
  2423. #define SDL_memset memset
  2424. #endif
  2425. /**
  2426. * Clear an object's memory to zero.
  2427. *
  2428. * This is wrapper over SDL_memset that handles calculating the object size,
  2429. * so there's no chance of copy/paste errors, and the code is cleaner.
  2430. *
  2431. * This requires an object, not a pointer to an object, nor an array.
  2432. *
  2433. * \param x the object to clear.
  2434. *
  2435. * \threadsafety It is safe to call this macro from any thread.
  2436. *
  2437. * \since This macro is available since SDL 3.2.0.
  2438. *
  2439. * \sa SDL_zerop
  2440. * \sa SDL_zeroa
  2441. */
  2442. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  2443. /**
  2444. * Clear an object's memory to zero, using a pointer.
  2445. *
  2446. * This is wrapper over SDL_memset that handles calculating the object size,
  2447. * so there's no chance of copy/paste errors, and the code is cleaner.
  2448. *
  2449. * This requires a pointer to an object, not an object itself, nor an array.
  2450. *
  2451. * \param x a pointer to the object to clear.
  2452. *
  2453. * \threadsafety It is safe to call this macro from any thread.
  2454. *
  2455. * \since This macro is available since SDL 3.2.0.
  2456. *
  2457. * \sa SDL_zero
  2458. * \sa SDL_zeroa
  2459. */
  2460. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  2461. /**
  2462. * Clear an array's memory to zero.
  2463. *
  2464. * This is wrapper over SDL_memset that handles calculating the array size, so
  2465. * there's no chance of copy/paste errors, and the code is cleaner.
  2466. *
  2467. * This requires an array, not an object, nor a pointer to an object.
  2468. *
  2469. * \param x an array to clear.
  2470. *
  2471. * \threadsafety It is safe to call this macro from any thread.
  2472. *
  2473. * \since This macro is available since SDL 3.2.0.
  2474. *
  2475. * \sa SDL_zero
  2476. * \sa SDL_zeroa
  2477. */
  2478. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  2479. /**
  2480. * Compare two buffers of memory.
  2481. *
  2482. * \param s1 the first buffer to compare. NULL is not permitted!
  2483. * \param s2 the second buffer to compare. NULL is not permitted!
  2484. * \param len the number of bytes to compare between the buffers.
  2485. * \returns less than zero if s1 is "less than" s2, greater than zero if s1 is
  2486. * "greater than" s2, and zero if the buffers match exactly for `len`
  2487. * bytes.
  2488. *
  2489. * \threadsafety It is safe to call this function from any thread.
  2490. *
  2491. * \since This function is available since SDL 3.2.0.
  2492. */
  2493. extern SDL_DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  2494. /**
  2495. * This works exactly like wcslen() but doesn't require access to a C runtime.
  2496. *
  2497. * Counts the number of wchar_t values in `wstr`, excluding the null
  2498. * terminator.
  2499. *
  2500. * Like SDL_strlen only counts bytes and not codepoints in a UTF-8 string,
  2501. * this counts wchar_t values in a string, even if the string's encoding is of
  2502. * variable width, like UTF-16.
  2503. *
  2504. * Also be aware that wchar_t is different sizes on different platforms (4
  2505. * bytes on Linux, 2 on Windows, etc).
  2506. *
  2507. * \param wstr The null-terminated wide string to read. Must not be NULL.
  2508. * \returns the length (in wchar_t values, excluding the null terminator) of
  2509. * `wstr`.
  2510. *
  2511. * \threadsafety It is safe to call this function from any thread.
  2512. *
  2513. * \since This function is available since SDL 3.2.0.
  2514. *
  2515. * \sa SDL_wcsnlen
  2516. * \sa SDL_utf8strlen
  2517. * \sa SDL_utf8strnlen
  2518. */
  2519. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  2520. /**
  2521. * This works exactly like wcsnlen() but doesn't require access to a C
  2522. * runtime.
  2523. *
  2524. * Counts up to a maximum of `maxlen` wchar_t values in `wstr`, excluding the
  2525. * null terminator.
  2526. *
  2527. * Like SDL_strnlen only counts bytes and not codepoints in a UTF-8 string,
  2528. * this counts wchar_t values in a string, even if the string's encoding is of
  2529. * variable width, like UTF-16.
  2530. *
  2531. * Also be aware that wchar_t is different sizes on different platforms (4
  2532. * bytes on Linux, 2 on Windows, etc).
  2533. *
  2534. * Also, `maxlen` is a count of wide characters, not bytes!
  2535. *
  2536. * \param wstr The null-terminated wide string to read. Must not be NULL.
  2537. * \param maxlen The maximum amount of wide characters to count.
  2538. * \returns the length (in wide characters, excluding the null terminator) of
  2539. * `wstr` but never more than `maxlen`.
  2540. *
  2541. * \threadsafety It is safe to call this function from any thread.
  2542. *
  2543. * \since This function is available since SDL 3.2.0.
  2544. *
  2545. * \sa SDL_wcslen
  2546. * \sa SDL_utf8strlen
  2547. * \sa SDL_utf8strnlen
  2548. */
  2549. extern SDL_DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  2550. /**
  2551. * Copy a wide string.
  2552. *
  2553. * This function copies `maxlen` - 1 wide characters from `src` to `dst`, then
  2554. * appends a null terminator.
  2555. *
  2556. * `src` and `dst` must not overlap.
  2557. *
  2558. * If `maxlen` is 0, no wide characters are copied and no null terminator is
  2559. * written.
  2560. *
  2561. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2562. * with `src`.
  2563. * \param src The null-terminated wide string to copy. Must not be NULL, and
  2564. * must not overlap with `dst`.
  2565. * \param maxlen The length (in wide characters) of the destination buffer.
  2566. * \returns the length (in wide characters, excluding the null terminator) of
  2567. * `src`.
  2568. *
  2569. * \threadsafety It is safe to call this function from any thread.
  2570. *
  2571. * \since This function is available since SDL 3.2.0.
  2572. *
  2573. * \sa SDL_wcslcat
  2574. */
  2575. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  2576. /**
  2577. * Concatenate wide strings.
  2578. *
  2579. * This function appends up to `maxlen` - SDL_wcslen(dst) - 1 wide characters
  2580. * from `src` to the end of the wide string in `dst`, then appends a null
  2581. * terminator.
  2582. *
  2583. * `src` and `dst` must not overlap.
  2584. *
  2585. * If `maxlen` - SDL_wcslen(dst) - 1 is less than or equal to 0, then `dst` is
  2586. * unmodified.
  2587. *
  2588. * \param dst The destination buffer already containing the first
  2589. * null-terminated wide string. Must not be NULL and must not
  2590. * overlap with `src`.
  2591. * \param src The second null-terminated wide string. Must not be NULL, and
  2592. * must not overlap with `dst`.
  2593. * \param maxlen The length (in wide characters) of the destination buffer.
  2594. * \returns the length (in wide characters, excluding the null terminator) of
  2595. * the string in `dst` plus the length of `src`.
  2596. *
  2597. * \threadsafety It is safe to call this function from any thread.
  2598. *
  2599. * \since This function is available since SDL 3.2.0.
  2600. *
  2601. * \sa SDL_wcslcpy
  2602. */
  2603. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  2604. /**
  2605. * Allocate a copy of a wide string.
  2606. *
  2607. * This allocates enough space for a null-terminated copy of `wstr`, using
  2608. * SDL_malloc, and then makes a copy of the string into this space.
  2609. *
  2610. * The returned string is owned by the caller, and should be passed to
  2611. * SDL_free when no longer needed.
  2612. *
  2613. * \param wstr the string to copy.
  2614. * \returns a pointer to the newly-allocated wide string.
  2615. *
  2616. * \threadsafety It is safe to call this function from any thread.
  2617. *
  2618. * \since This function is available since SDL 3.2.0.
  2619. */
  2620. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsdup(const wchar_t *wstr);
  2621. /**
  2622. * Search a wide string for the first instance of a specific substring.
  2623. *
  2624. * The search ends once it finds the requested substring, or a null terminator
  2625. * byte to end the string.
  2626. *
  2627. * Note that this looks for strings of _wide characters_, not _codepoints_, so
  2628. * it's legal to search for malformed and incomplete UTF-16 sequences.
  2629. *
  2630. * \param haystack the wide string to search. Must not be NULL.
  2631. * \param needle the wide string to search for. Must not be NULL.
  2632. * \returns a pointer to the first instance of `needle` in the string, or NULL
  2633. * if not found.
  2634. *
  2635. * \threadsafety It is safe to call this function from any thread.
  2636. *
  2637. * \since This function is available since SDL 3.2.0.
  2638. */
  2639. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  2640. /**
  2641. * Search a wide string, up to n wide chars, for the first instance of a
  2642. * specific substring.
  2643. *
  2644. * The search ends once it finds the requested substring, or a null terminator
  2645. * value to end the string, or `maxlen` wide character have been examined. It
  2646. * is possible to use this function on a wide string without a null
  2647. * terminator.
  2648. *
  2649. * Note that this looks for strings of _wide characters_, not _codepoints_, so
  2650. * it's legal to search for malformed and incomplete UTF-16 sequences.
  2651. *
  2652. * \param haystack the wide string to search. Must not be NULL.
  2653. * \param needle the wide string to search for. Must not be NULL.
  2654. * \param maxlen the maximum number of wide characters to search in
  2655. * `haystack`.
  2656. * \returns a pointer to the first instance of `needle` in the string, or NULL
  2657. * if not found.
  2658. *
  2659. * \threadsafety It is safe to call this function from any thread.
  2660. *
  2661. * \since This function is available since SDL 3.2.0.
  2662. */
  2663. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  2664. /**
  2665. * Compare two null-terminated wide strings.
  2666. *
  2667. * This only compares wchar_t values until it hits a null-terminating
  2668. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  2669. * depending on your platform's wchar_t size), or uses valid Unicode values.
  2670. *
  2671. * \param str1 the first string to compare. NULL is not permitted!
  2672. * \param str2 the second string to compare. NULL is not permitted!
  2673. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2674. * str1 is "greater than" str2, and zero if the strings match
  2675. * exactly.
  2676. *
  2677. * \threadsafety It is safe to call this function from any thread.
  2678. *
  2679. * \since This function is available since SDL 3.2.0.
  2680. */
  2681. extern SDL_DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  2682. /**
  2683. * Compare two wide strings up to a number of wchar_t values.
  2684. *
  2685. * This only compares wchar_t values; it does not care if the string is
  2686. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  2687. * or uses valid Unicode values.
  2688. *
  2689. * Note that while this function is intended to be used with UTF-16 (or
  2690. * UTF-32, depending on your platform's definition of wchar_t), it is
  2691. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  2692. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  2693. * sequence, it will only compare a portion of the final character.
  2694. *
  2695. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  2696. * match to this number of wide chars (or both have matched to a
  2697. * null-terminator character before this count), they will be considered
  2698. * equal.
  2699. *
  2700. * \param str1 the first string to compare. NULL is not permitted!
  2701. * \param str2 the second string to compare. NULL is not permitted!
  2702. * \param maxlen the maximum number of wchar_t to compare.
  2703. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2704. * str1 is "greater than" str2, and zero if the strings match
  2705. * exactly.
  2706. *
  2707. * \threadsafety It is safe to call this function from any thread.
  2708. *
  2709. * \since This function is available since SDL 3.2.0.
  2710. */
  2711. extern SDL_DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  2712. /**
  2713. * Compare two null-terminated wide strings, case-insensitively.
  2714. *
  2715. * This will work with Unicode strings, using a technique called
  2716. * "case-folding" to handle the vast majority of case-sensitive human
  2717. * languages regardless of system locale. It can deal with expanding values: a
  2718. * German Eszett character can compare against two ASCII 's' chars and be
  2719. * considered a match, for example. A notable exception: it does not handle
  2720. * the Turkish 'i' character; human language is complicated!
  2721. *
  2722. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  2723. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  2724. * handles Unicode, it expects the string to be well-formed and not a
  2725. * null-terminated string of arbitrary bytes. Characters that are not valid
  2726. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  2727. * CHARACTER), which is to say two strings of random bits may turn out to
  2728. * match if they convert to the same amount of replacement characters.
  2729. *
  2730. * \param str1 the first string to compare. NULL is not permitted!
  2731. * \param str2 the second string to compare. NULL is not permitted!
  2732. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2733. * str1 is "greater than" str2, and zero if the strings match
  2734. * exactly.
  2735. *
  2736. * \threadsafety It is safe to call this function from any thread.
  2737. *
  2738. * \since This function is available since SDL 3.2.0.
  2739. */
  2740. extern SDL_DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  2741. /**
  2742. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  2743. *
  2744. * This will work with Unicode strings, using a technique called
  2745. * "case-folding" to handle the vast majority of case-sensitive human
  2746. * languages regardless of system locale. It can deal with expanding values: a
  2747. * German Eszett character can compare against two ASCII 's' chars and be
  2748. * considered a match, for example. A notable exception: it does not handle
  2749. * the Turkish 'i' character; human language is complicated!
  2750. *
  2751. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  2752. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  2753. * handles Unicode, it expects the string to be well-formed and not a
  2754. * null-terminated string of arbitrary bytes. Characters that are not valid
  2755. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  2756. * CHARACTER), which is to say two strings of random bits may turn out to
  2757. * match if they convert to the same amount of replacement characters.
  2758. *
  2759. * Note that while this function might deal with variable-sized characters,
  2760. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  2761. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  2762. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  2763. * to overflow a buffer.
  2764. *
  2765. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  2766. * strings match to this number of wchar_t (or both have matched to a
  2767. * null-terminator character before this number of bytes), they will be
  2768. * considered equal.
  2769. *
  2770. * \param str1 the first string to compare. NULL is not permitted!
  2771. * \param str2 the second string to compare. NULL is not permitted!
  2772. * \param maxlen the maximum number of wchar_t values to compare.
  2773. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2774. * str1 is "greater than" str2, and zero if the strings match
  2775. * exactly.
  2776. *
  2777. * \threadsafety It is safe to call this function from any thread.
  2778. *
  2779. * \since This function is available since SDL 3.2.0.
  2780. */
  2781. extern SDL_DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  2782. /**
  2783. * Parse a `long` from a wide string.
  2784. *
  2785. * If `str` starts with whitespace, then those whitespace characters are
  2786. * skipped before attempting to parse the number.
  2787. *
  2788. * If the parsed number does not fit inside a `long`, the result is clamped to
  2789. * the minimum and maximum representable `long` values.
  2790. *
  2791. * \param str The null-terminated wide string to read. Must not be NULL.
  2792. * \param endp If not NULL, the address of the first invalid wide character
  2793. * (i.e. the next character after the parsed number) will be
  2794. * written to this pointer.
  2795. * \param base The base of the integer to read. Supported values are 0 and 2
  2796. * to 36 inclusive. If 0, the base will be inferred from the
  2797. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2798. * otherwise).
  2799. * \returns the parsed `long`, or 0 if no number could be parsed.
  2800. *
  2801. * \threadsafety It is safe to call this function from any thread.
  2802. *
  2803. * \since This function is available since SDL 3.2.0.
  2804. *
  2805. * \sa SDL_strtol
  2806. */
  2807. extern SDL_DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  2808. /**
  2809. * This works exactly like strlen() but doesn't require access to a C runtime.
  2810. *
  2811. * Counts the bytes in `str`, excluding the null terminator.
  2812. *
  2813. * If you need the length of a UTF-8 string, consider using SDL_utf8strlen().
  2814. *
  2815. * \param str The null-terminated string to read. Must not be NULL.
  2816. * \returns the length (in bytes, excluding the null terminator) of `src`.
  2817. *
  2818. * \threadsafety It is safe to call this function from any thread.
  2819. *
  2820. * \since This function is available since SDL 3.2.0.
  2821. *
  2822. * \sa SDL_strnlen
  2823. * \sa SDL_utf8strlen
  2824. * \sa SDL_utf8strnlen
  2825. */
  2826. extern SDL_DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  2827. /**
  2828. * This works exactly like strnlen() but doesn't require access to a C
  2829. * runtime.
  2830. *
  2831. * Counts up to a maximum of `maxlen` bytes in `str`, excluding the null
  2832. * terminator.
  2833. *
  2834. * If you need the length of a UTF-8 string, consider using SDL_utf8strnlen().
  2835. *
  2836. * \param str The null-terminated string to read. Must not be NULL.
  2837. * \param maxlen The maximum amount of bytes to count.
  2838. * \returns the length (in bytes, excluding the null terminator) of `src` but
  2839. * never more than `maxlen`.
  2840. *
  2841. * \threadsafety It is safe to call this function from any thread.
  2842. *
  2843. * \since This function is available since SDL 3.2.0.
  2844. *
  2845. * \sa SDL_strlen
  2846. * \sa SDL_utf8strlen
  2847. * \sa SDL_utf8strnlen
  2848. */
  2849. extern SDL_DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  2850. /**
  2851. * Copy a string.
  2852. *
  2853. * This function copies up to `maxlen` - 1 characters from `src` to `dst`,
  2854. * then appends a null terminator.
  2855. *
  2856. * If `maxlen` is 0, no characters are copied and no null terminator is
  2857. * written.
  2858. *
  2859. * If you want to copy an UTF-8 string but need to ensure that multi-byte
  2860. * sequences are not truncated, consider using SDL_utf8strlcpy().
  2861. *
  2862. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2863. * with `src`.
  2864. * \param src The null-terminated string to copy. Must not be NULL, and must
  2865. * not overlap with `dst`.
  2866. * \param maxlen The length (in characters) of the destination buffer.
  2867. * \returns the length (in characters, excluding the null terminator) of
  2868. * `src`.
  2869. *
  2870. * \threadsafety It is safe to call this function from any thread.
  2871. *
  2872. * \since This function is available since SDL 3.2.0.
  2873. *
  2874. * \sa SDL_strlcat
  2875. * \sa SDL_utf8strlcpy
  2876. */
  2877. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  2878. /**
  2879. * Copy an UTF-8 string.
  2880. *
  2881. * This function copies up to `dst_bytes` - 1 bytes from `src` to `dst` while
  2882. * also ensuring that the string written to `dst` does not end in a truncated
  2883. * multi-byte sequence. Finally, it appends a null terminator.
  2884. *
  2885. * `src` and `dst` must not overlap.
  2886. *
  2887. * Note that unlike SDL_strlcpy(), this function returns the number of bytes
  2888. * written, not the length of `src`.
  2889. *
  2890. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2891. * with `src`.
  2892. * \param src The null-terminated UTF-8 string to copy. Must not be NULL, and
  2893. * must not overlap with `dst`.
  2894. * \param dst_bytes The length (in bytes) of the destination buffer. Must not
  2895. * be 0.
  2896. * \returns the number of bytes written, excluding the null terminator.
  2897. *
  2898. * \threadsafety It is safe to call this function from any thread.
  2899. *
  2900. * \since This function is available since SDL 3.2.0.
  2901. *
  2902. * \sa SDL_strlcpy
  2903. */
  2904. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  2905. /**
  2906. * Concatenate strings.
  2907. *
  2908. * This function appends up to `maxlen` - SDL_strlen(dst) - 1 characters from
  2909. * `src` to the end of the string in `dst`, then appends a null terminator.
  2910. *
  2911. * `src` and `dst` must not overlap.
  2912. *
  2913. * If `maxlen` - SDL_strlen(dst) - 1 is less than or equal to 0, then `dst` is
  2914. * unmodified.
  2915. *
  2916. * \param dst The destination buffer already containing the first
  2917. * null-terminated string. Must not be NULL and must not overlap
  2918. * with `src`.
  2919. * \param src The second null-terminated string. Must not be NULL, and must
  2920. * not overlap with `dst`.
  2921. * \param maxlen The length (in characters) of the destination buffer.
  2922. * \returns the length (in characters, excluding the null terminator) of the
  2923. * string in `dst` plus the length of `src`.
  2924. *
  2925. * \threadsafety It is safe to call this function from any thread.
  2926. *
  2927. * \since This function is available since SDL 3.2.0.
  2928. *
  2929. * \sa SDL_strlcpy
  2930. */
  2931. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  2932. /**
  2933. * Allocate a copy of a string.
  2934. *
  2935. * This allocates enough space for a null-terminated copy of `str`, using
  2936. * SDL_malloc, and then makes a copy of the string into this space.
  2937. *
  2938. * The returned string is owned by the caller, and should be passed to
  2939. * SDL_free when no longer needed.
  2940. *
  2941. * \param str the string to copy.
  2942. * \returns a pointer to the newly-allocated string.
  2943. *
  2944. * \threadsafety It is safe to call this function from any thread.
  2945. *
  2946. * \since This function is available since SDL 3.2.0.
  2947. */
  2948. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strdup(const char *str);
  2949. /**
  2950. * Allocate a copy of a string, up to n characters.
  2951. *
  2952. * This allocates enough space for a null-terminated copy of `str`, up to
  2953. * `maxlen` bytes, using SDL_malloc, and then makes a copy of the string into
  2954. * this space.
  2955. *
  2956. * If the string is longer than `maxlen` bytes, the returned string will be
  2957. * `maxlen` bytes long, plus a null-terminator character that isn't included
  2958. * in the count.
  2959. *
  2960. * The returned string is owned by the caller, and should be passed to
  2961. * SDL_free when no longer needed.
  2962. *
  2963. * \param str the string to copy.
  2964. * \param maxlen the maximum length of the copied string, not counting the
  2965. * null-terminator character.
  2966. * \returns a pointer to the newly-allocated string.
  2967. *
  2968. * \threadsafety It is safe to call this function from any thread.
  2969. *
  2970. * \since This function is available since SDL 3.2.0.
  2971. */
  2972. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strndup(const char *str, size_t maxlen);
  2973. /**
  2974. * Reverse a string's contents.
  2975. *
  2976. * This reverses a null-terminated string in-place. Only the content of the
  2977. * string is reversed; the null-terminator character remains at the end of the
  2978. * reversed string.
  2979. *
  2980. * **WARNING**: This function reverses the _bytes_ of the string, not the
  2981. * codepoints. If `str` is a UTF-8 string with Unicode codepoints > 127, this
  2982. * will ruin the string data. You should only use this function on strings
  2983. * that are completely comprised of low ASCII characters.
  2984. *
  2985. * \param str the string to reverse.
  2986. * \returns `str`.
  2987. *
  2988. * \threadsafety It is safe to call this function from any thread.
  2989. *
  2990. * \since This function is available since SDL 3.2.0.
  2991. */
  2992. extern SDL_DECLSPEC char * SDLCALL SDL_strrev(char *str);
  2993. /**
  2994. * Convert a string to uppercase.
  2995. *
  2996. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2997. * values 'A' through 'Z' to uppercase.
  2998. *
  2999. * This function operates on a null-terminated string of bytes--even if it is
  3000. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  3001. * uppercase equivalents in-place, returning the original `str` pointer.
  3002. *
  3003. * \param str the string to convert in-place. Can not be NULL.
  3004. * \returns the `str` pointer passed into this function.
  3005. *
  3006. * \threadsafety It is safe to call this function from any thread.
  3007. *
  3008. * \since This function is available since SDL 3.2.0.
  3009. *
  3010. * \sa SDL_strlwr
  3011. */
  3012. extern SDL_DECLSPEC char * SDLCALL SDL_strupr(char *str);
  3013. /**
  3014. * Convert a string to lowercase.
  3015. *
  3016. * **WARNING**: Regardless of system locale, this will only convert ASCII
  3017. * values 'A' through 'Z' to lowercase.
  3018. *
  3019. * This function operates on a null-terminated string of bytes--even if it is
  3020. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  3021. * lowercase equivalents in-place, returning the original `str` pointer.
  3022. *
  3023. * \param str the string to convert in-place. Can not be NULL.
  3024. * \returns the `str` pointer passed into this function.
  3025. *
  3026. * \threadsafety It is safe to call this function from any thread.
  3027. *
  3028. * \since This function is available since SDL 3.2.0.
  3029. *
  3030. * \sa SDL_strupr
  3031. */
  3032. extern SDL_DECLSPEC char * SDLCALL SDL_strlwr(char *str);
  3033. /**
  3034. * Search a string for the first instance of a specific byte.
  3035. *
  3036. * The search ends once it finds the requested byte value, or a null
  3037. * terminator byte to end the string.
  3038. *
  3039. * Note that this looks for _bytes_, not _characters_, so you cannot match
  3040. * against a Unicode codepoint > 255, regardless of character encoding.
  3041. *
  3042. * \param str the string to search. Must not be NULL.
  3043. * \param c the byte value to search for.
  3044. * \returns a pointer to the first instance of `c` in the string, or NULL if
  3045. * not found.
  3046. *
  3047. * \threadsafety It is safe to call this function from any thread.
  3048. *
  3049. * \since This function is available since SDL 3.2.0.
  3050. */
  3051. extern SDL_DECLSPEC char * SDLCALL SDL_strchr(const char *str, int c);
  3052. /**
  3053. * Search a string for the last instance of a specific byte.
  3054. *
  3055. * The search must go until it finds a null terminator byte to end the string.
  3056. *
  3057. * Note that this looks for _bytes_, not _characters_, so you cannot match
  3058. * against a Unicode codepoint > 255, regardless of character encoding.
  3059. *
  3060. * \param str the string to search. Must not be NULL.
  3061. * \param c the byte value to search for.
  3062. * \returns a pointer to the last instance of `c` in the string, or NULL if
  3063. * not found.
  3064. *
  3065. * \threadsafety It is safe to call this function from any thread.
  3066. *
  3067. * \since This function is available since SDL 3.2.0.
  3068. */
  3069. extern SDL_DECLSPEC char * SDLCALL SDL_strrchr(const char *str, int c);
  3070. /**
  3071. * Search a string for the first instance of a specific substring.
  3072. *
  3073. * The search ends once it finds the requested substring, or a null terminator
  3074. * byte to end the string.
  3075. *
  3076. * Note that this looks for strings of _bytes_, not _characters_, so it's
  3077. * legal to search for malformed and incomplete UTF-8 sequences.
  3078. *
  3079. * \param haystack the string to search. Must not be NULL.
  3080. * \param needle the string to search for. Must not be NULL.
  3081. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3082. * if not found.
  3083. *
  3084. * \threadsafety It is safe to call this function from any thread.
  3085. *
  3086. * \since This function is available since SDL 3.2.0.
  3087. */
  3088. extern SDL_DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
  3089. /**
  3090. * Search a string, up to n bytes, for the first instance of a specific
  3091. * substring.
  3092. *
  3093. * The search ends once it finds the requested substring, or a null terminator
  3094. * byte to end the string, or `maxlen` bytes have been examined. It is
  3095. * possible to use this function on a string without a null terminator.
  3096. *
  3097. * Note that this looks for strings of _bytes_, not _characters_, so it's
  3098. * legal to search for malformed and incomplete UTF-8 sequences.
  3099. *
  3100. * \param haystack the string to search. Must not be NULL.
  3101. * \param needle the string to search for. Must not be NULL.
  3102. * \param maxlen the maximum number of bytes to search in `haystack`.
  3103. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3104. * if not found.
  3105. *
  3106. * \threadsafety It is safe to call this function from any thread.
  3107. *
  3108. * \since This function is available since SDL 3.2.0.
  3109. */
  3110. extern SDL_DECLSPEC char * SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  3111. /**
  3112. * Search a UTF-8 string for the first instance of a specific substring,
  3113. * case-insensitively.
  3114. *
  3115. * This will work with Unicode strings, using a technique called
  3116. * "case-folding" to handle the vast majority of case-sensitive human
  3117. * languages regardless of system locale. It can deal with expanding values: a
  3118. * German Eszett character can compare against two ASCII 's' chars and be
  3119. * considered a match, for example. A notable exception: it does not handle
  3120. * the Turkish 'i' character; human language is complicated!
  3121. *
  3122. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3123. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3124. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3125. * CHARACTER), which is to say two strings of random bits may turn out to
  3126. * match if they convert to the same amount of replacement characters.
  3127. *
  3128. * \param haystack the string to search. Must not be NULL.
  3129. * \param needle the string to search for. Must not be NULL.
  3130. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3131. * if not found.
  3132. *
  3133. * \threadsafety It is safe to call this function from any thread.
  3134. *
  3135. * \since This function is available since SDL 3.2.0.
  3136. */
  3137. extern SDL_DECLSPEC char * SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  3138. /**
  3139. * This works exactly like strtok_r() but doesn't require access to a C
  3140. * runtime.
  3141. *
  3142. * Break a string up into a series of tokens.
  3143. *
  3144. * To start tokenizing a new string, `str` should be the non-NULL address of
  3145. * the string to start tokenizing. Future calls to get the next token from the
  3146. * same string should specify a NULL.
  3147. *
  3148. * Note that this function will overwrite pieces of `str` with null chars to
  3149. * split it into tokens. This function cannot be used with const/read-only
  3150. * strings!
  3151. *
  3152. * `saveptr` just needs to point to a `char *` that can be overwritten; SDL
  3153. * will use this to save tokenizing state between calls. It is initialized if
  3154. * `str` is non-NULL, and used to resume tokenizing when `str` is NULL.
  3155. *
  3156. * \param str the string to tokenize, or NULL to continue tokenizing.
  3157. * \param delim the delimiter string that separates tokens.
  3158. * \param saveptr pointer to a char *, used for ongoing state.
  3159. * \returns A pointer to the next token, or NULL if no tokens remain.
  3160. *
  3161. * \threadsafety It is safe to call this function from any thread.
  3162. *
  3163. * \since This function is available since SDL 3.2.0.
  3164. */
  3165. extern SDL_DECLSPEC char * SDLCALL SDL_strtok_r(char *str, const char *delim, char **saveptr);
  3166. /**
  3167. * Count the number of codepoints in a UTF-8 string.
  3168. *
  3169. * Counts the _codepoints_, not _bytes_, in `str`, excluding the null
  3170. * terminator.
  3171. *
  3172. * If you need to count the bytes in a string instead, consider using
  3173. * SDL_strlen().
  3174. *
  3175. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3176. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3177. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3178. * CHARACTER), so a malformed or incomplete UTF-8 sequence might increase the
  3179. * count by several replacement characters.
  3180. *
  3181. * \param str The null-terminated UTF-8 string to read. Must not be NULL.
  3182. * \returns The length (in codepoints, excluding the null terminator) of
  3183. * `src`.
  3184. *
  3185. * \threadsafety It is safe to call this function from any thread.
  3186. *
  3187. * \since This function is available since SDL 3.2.0.
  3188. *
  3189. * \sa SDL_utf8strnlen
  3190. * \sa SDL_strlen
  3191. */
  3192. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  3193. /**
  3194. * Count the number of codepoints in a UTF-8 string, up to n bytes.
  3195. *
  3196. * Counts the _codepoints_, not _bytes_, in `str`, excluding the null
  3197. * terminator.
  3198. *
  3199. * If you need to count the bytes in a string instead, consider using
  3200. * SDL_strnlen().
  3201. *
  3202. * The counting stops at `bytes` bytes (not codepoints!). This seems
  3203. * counterintuitive, but makes it easy to express the total size of the
  3204. * string's buffer.
  3205. *
  3206. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3207. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3208. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3209. * CHARACTER), so a malformed or incomplete UTF-8 sequence might increase the
  3210. * count by several replacement characters.
  3211. *
  3212. * \param str The null-terminated UTF-8 string to read. Must not be NULL.
  3213. * \param bytes The maximum amount of bytes to count.
  3214. * \returns The length (in codepoints, excluding the null terminator) of `src`
  3215. * but never more than `maxlen`.
  3216. *
  3217. * \threadsafety It is safe to call this function from any thread.
  3218. *
  3219. * \since This function is available since SDL 3.2.0.
  3220. *
  3221. * \sa SDL_utf8strlen
  3222. * \sa SDL_strnlen
  3223. */
  3224. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  3225. /**
  3226. * Convert an integer into a string.
  3227. *
  3228. * This requires a radix to specified for string format. Specifying 10
  3229. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3230. * to 36.
  3231. *
  3232. * Note that this function will overflow a buffer if `str` is not large enough
  3233. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3234. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3235. * much more space than you expect to use (and don't forget possible negative
  3236. * signs, null terminator bytes, etc).
  3237. *
  3238. * \param value the integer to convert.
  3239. * \param str the buffer to write the string into.
  3240. * \param radix the radix to use for string generation.
  3241. * \returns `str`.
  3242. *
  3243. * \threadsafety It is safe to call this function from any thread.
  3244. *
  3245. * \since This function is available since SDL 3.2.0.
  3246. *
  3247. * \sa SDL_uitoa
  3248. * \sa SDL_ltoa
  3249. * \sa SDL_lltoa
  3250. */
  3251. extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
  3252. /**
  3253. * Convert an unsigned integer into a string.
  3254. *
  3255. * This requires a radix to specified for string format. Specifying 10
  3256. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3257. * to 36.
  3258. *
  3259. * Note that this function will overflow a buffer if `str` is not large enough
  3260. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3261. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3262. * much more space than you expect to use (and don't forget null terminator
  3263. * bytes, etc).
  3264. *
  3265. * \param value the unsigned integer to convert.
  3266. * \param str the buffer to write the string into.
  3267. * \param radix the radix to use for string generation.
  3268. * \returns `str`.
  3269. *
  3270. * \threadsafety It is safe to call this function from any thread.
  3271. *
  3272. * \since This function is available since SDL 3.2.0.
  3273. *
  3274. * \sa SDL_itoa
  3275. * \sa SDL_ultoa
  3276. * \sa SDL_ulltoa
  3277. */
  3278. extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  3279. /**
  3280. * Convert a long integer into a string.
  3281. *
  3282. * This requires a radix to specified for string format. Specifying 10
  3283. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3284. * to 36.
  3285. *
  3286. * Note that this function will overflow a buffer if `str` is not large enough
  3287. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3288. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3289. * much more space than you expect to use (and don't forget possible negative
  3290. * signs, null terminator bytes, etc).
  3291. *
  3292. * \param value the long integer to convert.
  3293. * \param str the buffer to write the string into.
  3294. * \param radix the radix to use for string generation.
  3295. * \returns `str`.
  3296. *
  3297. * \threadsafety It is safe to call this function from any thread.
  3298. *
  3299. * \since This function is available since SDL 3.2.0.
  3300. *
  3301. * \sa SDL_ultoa
  3302. * \sa SDL_itoa
  3303. * \sa SDL_lltoa
  3304. */
  3305. extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
  3306. /**
  3307. * Convert an unsigned long integer into a string.
  3308. *
  3309. * This requires a radix to specified for string format. Specifying 10
  3310. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3311. * to 36.
  3312. *
  3313. * Note that this function will overflow a buffer if `str` is not large enough
  3314. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3315. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3316. * much more space than you expect to use (and don't forget null terminator
  3317. * bytes, etc).
  3318. *
  3319. * \param value the unsigned long integer to convert.
  3320. * \param str the buffer to write the string into.
  3321. * \param radix the radix to use for string generation.
  3322. * \returns `str`.
  3323. *
  3324. * \threadsafety It is safe to call this function from any thread.
  3325. *
  3326. * \since This function is available since SDL 3.2.0.
  3327. *
  3328. * \sa SDL_ltoa
  3329. * \sa SDL_uitoa
  3330. * \sa SDL_ulltoa
  3331. */
  3332. extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  3333. #ifndef SDL_NOLONGLONG
  3334. /**
  3335. * Convert a long long integer into a string.
  3336. *
  3337. * This requires a radix to specified for string format. Specifying 10
  3338. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3339. * to 36.
  3340. *
  3341. * Note that this function will overflow a buffer if `str` is not large enough
  3342. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3343. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3344. * much more space than you expect to use (and don't forget possible negative
  3345. * signs, null terminator bytes, etc).
  3346. *
  3347. * \param value the long long integer to convert.
  3348. * \param str the buffer to write the string into.
  3349. * \param radix the radix to use for string generation.
  3350. * \returns `str`.
  3351. *
  3352. * \threadsafety It is safe to call this function from any thread.
  3353. *
  3354. * \since This function is available since SDL 3.2.0.
  3355. *
  3356. * \sa SDL_ulltoa
  3357. * \sa SDL_itoa
  3358. * \sa SDL_ltoa
  3359. */
  3360. extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
  3361. /**
  3362. * Convert an unsigned long long integer into a string.
  3363. *
  3364. * This requires a radix to specified for string format. Specifying 10
  3365. * produces a decimal number, 16 hexadecimal, etc. Must be in the range of 2
  3366. * to 36.
  3367. *
  3368. * Note that this function will overflow a buffer if `str` is not large enough
  3369. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3370. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3371. * much more space than you expect to use (and don't forget null terminator
  3372. * bytes, etc).
  3373. *
  3374. * \param value the unsigned long long integer to convert.
  3375. * \param str the buffer to write the string into.
  3376. * \param radix the radix to use for string generation.
  3377. * \returns `str`.
  3378. *
  3379. * \threadsafety It is safe to call this function from any thread.
  3380. *
  3381. * \since This function is available since SDL 3.2.0.
  3382. *
  3383. * \sa SDL_lltoa
  3384. * \sa SDL_uitoa
  3385. * \sa SDL_ultoa
  3386. */
  3387. extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
  3388. #endif
  3389. /**
  3390. * Parse an `int` from a string.
  3391. *
  3392. * The result of calling `SDL_atoi(str)` is equivalent to
  3393. * `(int)SDL_strtol(str, NULL, 10)`.
  3394. *
  3395. * \param str The null-terminated string to read. Must not be NULL.
  3396. * \returns the parsed `int`.
  3397. *
  3398. * \threadsafety It is safe to call this function from any thread.
  3399. *
  3400. * \since This function is available since SDL 3.2.0.
  3401. *
  3402. * \sa SDL_atof
  3403. * \sa SDL_strtol
  3404. * \sa SDL_strtoul
  3405. * \sa SDL_strtoll
  3406. * \sa SDL_strtoull
  3407. * \sa SDL_strtod
  3408. * \sa SDL_itoa
  3409. */
  3410. extern SDL_DECLSPEC int SDLCALL SDL_atoi(const char *str);
  3411. /**
  3412. * Parse a `double` from a string.
  3413. *
  3414. * The result of calling `SDL_atof(str)` is equivalent to `SDL_strtod(str,
  3415. * NULL)`.
  3416. *
  3417. * \param str The null-terminated string to read. Must not be NULL.
  3418. * \returns the parsed `double`.
  3419. *
  3420. * \threadsafety It is safe to call this function from any thread.
  3421. *
  3422. * \since This function is available since SDL 3.2.0.
  3423. *
  3424. * \sa SDL_atoi
  3425. * \sa SDL_strtol
  3426. * \sa SDL_strtoul
  3427. * \sa SDL_strtoll
  3428. * \sa SDL_strtoull
  3429. * \sa SDL_strtod
  3430. */
  3431. extern SDL_DECLSPEC double SDLCALL SDL_atof(const char *str);
  3432. /**
  3433. * Parse a `long` from a string.
  3434. *
  3435. * If `str` starts with whitespace, then those whitespace characters are
  3436. * skipped before attempting to parse the number.
  3437. *
  3438. * If the parsed number does not fit inside a `long`, the result is clamped to
  3439. * the minimum and maximum representable `long` values.
  3440. *
  3441. * \param str The null-terminated string to read. Must not be NULL.
  3442. * \param endp If not NULL, the address of the first invalid character (i.e.
  3443. * the next character after the parsed number) will be written to
  3444. * this pointer.
  3445. * \param base The base of the integer to read. Supported values are 0 and 2
  3446. * to 36 inclusive. If 0, the base will be inferred from the
  3447. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3448. * otherwise).
  3449. * \returns the parsed `long`, or 0 if no number could be parsed.
  3450. *
  3451. * \threadsafety It is safe to call this function from any thread.
  3452. *
  3453. * \since This function is available since SDL 3.2.0.
  3454. *
  3455. * \sa SDL_atoi
  3456. * \sa SDL_atof
  3457. * \sa SDL_strtoul
  3458. * \sa SDL_strtoll
  3459. * \sa SDL_strtoull
  3460. * \sa SDL_strtod
  3461. * \sa SDL_ltoa
  3462. * \sa SDL_wcstol
  3463. */
  3464. extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  3465. /**
  3466. * Parse an `unsigned long` from a string.
  3467. *
  3468. * If `str` starts with whitespace, then those whitespace characters are
  3469. * skipped before attempting to parse the number.
  3470. *
  3471. * If the parsed number does not fit inside an `unsigned long`, the result is
  3472. * clamped to the maximum representable `unsigned long` value.
  3473. *
  3474. * \param str The null-terminated string to read. Must not be NULL.
  3475. * \param endp If not NULL, the address of the first invalid character (i.e.
  3476. * the next character after the parsed number) will be written to
  3477. * this pointer.
  3478. * \param base The base of the integer to read. Supported values are 0 and 2
  3479. * to 36 inclusive. If 0, the base will be inferred from the
  3480. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3481. * otherwise).
  3482. * \returns the parsed `unsigned long`, or 0 if no number could be parsed.
  3483. *
  3484. * \threadsafety It is safe to call this function from any thread.
  3485. *
  3486. * \since This function is available since SDL 3.2.0.
  3487. *
  3488. * \sa SDL_atoi
  3489. * \sa SDL_atof
  3490. * \sa SDL_strtol
  3491. * \sa SDL_strtoll
  3492. * \sa SDL_strtoull
  3493. * \sa SDL_strtod
  3494. * \sa SDL_ultoa
  3495. */
  3496. extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  3497. #ifndef SDL_NOLONGLONG
  3498. /**
  3499. * Parse a `long long` from a string.
  3500. *
  3501. * If `str` starts with whitespace, then those whitespace characters are
  3502. * skipped before attempting to parse the number.
  3503. *
  3504. * If the parsed number does not fit inside a `long long`, the result is
  3505. * clamped to the minimum and maximum representable `long long` values.
  3506. *
  3507. * \param str The null-terminated string to read. Must not be NULL.
  3508. * \param endp If not NULL, the address of the first invalid character (i.e.
  3509. * the next character after the parsed number) will be written to
  3510. * this pointer.
  3511. * \param base The base of the integer to read. Supported values are 0 and 2
  3512. * to 36 inclusive. If 0, the base will be inferred from the
  3513. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3514. * otherwise).
  3515. * \returns the parsed `long long`, or 0 if no number could be parsed.
  3516. *
  3517. * \threadsafety It is safe to call this function from any thread.
  3518. *
  3519. * \since This function is available since SDL 3.2.0.
  3520. *
  3521. * \sa SDL_atoi
  3522. * \sa SDL_atof
  3523. * \sa SDL_strtol
  3524. * \sa SDL_strtoul
  3525. * \sa SDL_strtoull
  3526. * \sa SDL_strtod
  3527. * \sa SDL_lltoa
  3528. */
  3529. extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  3530. /**
  3531. * Parse an `unsigned long long` from a string.
  3532. *
  3533. * If `str` starts with whitespace, then those whitespace characters are
  3534. * skipped before attempting to parse the number.
  3535. *
  3536. * If the parsed number does not fit inside an `unsigned long long`, the
  3537. * result is clamped to the maximum representable `unsigned long long` value.
  3538. *
  3539. * \param str The null-terminated string to read. Must not be NULL.
  3540. * \param endp If not NULL, the address of the first invalid character (i.e.
  3541. * the next character after the parsed number) will be written to
  3542. * this pointer.
  3543. * \param base The base of the integer to read. Supported values are 0 and 2
  3544. * to 36 inclusive. If 0, the base will be inferred from the
  3545. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3546. * otherwise).
  3547. * \returns the parsed `unsigned long long`, or 0 if no number could be
  3548. * parsed.
  3549. *
  3550. * \threadsafety It is safe to call this function from any thread.
  3551. *
  3552. * \since This function is available since SDL 3.2.0.
  3553. *
  3554. * \sa SDL_atoi
  3555. * \sa SDL_atof
  3556. * \sa SDL_strtol
  3557. * \sa SDL_strtoll
  3558. * \sa SDL_strtoul
  3559. * \sa SDL_strtod
  3560. * \sa SDL_ulltoa
  3561. */
  3562. extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  3563. #endif
  3564. /**
  3565. * Parse a `double` from a string.
  3566. *
  3567. * This function makes fewer guarantees than the C runtime `strtod`:
  3568. *
  3569. * - Only decimal notation is guaranteed to be supported. The handling of
  3570. * scientific and hexadecimal notation is unspecified.
  3571. * - Whether or not INF and NAN can be parsed is unspecified.
  3572. * - The precision of the result is unspecified.
  3573. *
  3574. * \param str the null-terminated string to read. Must not be NULL.
  3575. * \param endp if not NULL, the address of the first invalid character (i.e.
  3576. * the next character after the parsed number) will be written to
  3577. * this pointer.
  3578. * \returns the parsed `double`, or 0 if no number could be parsed.
  3579. *
  3580. * \threadsafety It is safe to call this function from any thread.
  3581. *
  3582. * \since This function is available since SDL 3.2.0.
  3583. *
  3584. * \sa SDL_atoi
  3585. * \sa SDL_atof
  3586. * \sa SDL_strtol
  3587. * \sa SDL_strtoll
  3588. * \sa SDL_strtoul
  3589. * \sa SDL_strtoull
  3590. */
  3591. extern SDL_DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  3592. /**
  3593. * Compare two null-terminated UTF-8 strings.
  3594. *
  3595. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  3596. * since effectively this function just compares bytes until it hits a
  3597. * null-terminating character. Also due to the nature of UTF-8, this can be
  3598. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  3599. *
  3600. * \param str1 the first string to compare. NULL is not permitted!
  3601. * \param str2 the second string to compare. NULL is not permitted!
  3602. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3603. * str1 is "greater than" str2, and zero if the strings match
  3604. * exactly.
  3605. *
  3606. * \threadsafety It is safe to call this function from any thread.
  3607. *
  3608. * \since This function is available since SDL 3.2.0.
  3609. */
  3610. extern SDL_DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  3611. /**
  3612. * Compare two UTF-8 strings up to a number of bytes.
  3613. *
  3614. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  3615. * since effectively this function just compares bytes until it hits a
  3616. * null-terminating character. Also due to the nature of UTF-8, this can be
  3617. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  3618. *
  3619. * Note that while this function is intended to be used with UTF-8, it is
  3620. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  3621. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  3622. * compare a portion of the final character.
  3623. *
  3624. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  3625. * match to this number of bytes (or both have matched to a null-terminator
  3626. * character before this number of bytes), they will be considered equal.
  3627. *
  3628. * \param str1 the first string to compare. NULL is not permitted!
  3629. * \param str2 the second string to compare. NULL is not permitted!
  3630. * \param maxlen the maximum number of _bytes_ to compare.
  3631. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3632. * str1 is "greater than" str2, and zero if the strings match
  3633. * exactly.
  3634. *
  3635. * \threadsafety It is safe to call this function from any thread.
  3636. *
  3637. * \since This function is available since SDL 3.2.0.
  3638. */
  3639. extern SDL_DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  3640. /**
  3641. * Compare two null-terminated UTF-8 strings, case-insensitively.
  3642. *
  3643. * This will work with Unicode strings, using a technique called
  3644. * "case-folding" to handle the vast majority of case-sensitive human
  3645. * languages regardless of system locale. It can deal with expanding values: a
  3646. * German Eszett character can compare against two ASCII 's' chars and be
  3647. * considered a match, for example. A notable exception: it does not handle
  3648. * the Turkish 'i' character; human language is complicated!
  3649. *
  3650. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  3651. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3652. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3653. * CHARACTER), which is to say two strings of random bits may turn out to
  3654. * match if they convert to the same amount of replacement characters.
  3655. *
  3656. * \param str1 the first string to compare. NULL is not permitted!
  3657. * \param str2 the second string to compare. NULL is not permitted!
  3658. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3659. * str1 is "greater than" str2, and zero if the strings match
  3660. * exactly.
  3661. *
  3662. * \threadsafety It is safe to call this function from any thread.
  3663. *
  3664. * \since This function is available since SDL 3.2.0.
  3665. */
  3666. extern SDL_DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  3667. /**
  3668. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  3669. *
  3670. * This will work with Unicode strings, using a technique called
  3671. * "case-folding" to handle the vast majority of case-sensitive human
  3672. * languages regardless of system locale. It can deal with expanding values: a
  3673. * German Eszett character can compare against two ASCII 's' chars and be
  3674. * considered a match, for example. A notable exception: it does not handle
  3675. * the Turkish 'i' character; human language is complicated!
  3676. *
  3677. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  3678. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3679. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3680. * CHARACTER), which is to say two strings of random bits may turn out to
  3681. * match if they convert to the same amount of replacement characters.
  3682. *
  3683. * Note that while this function is intended to be used with UTF-8, `maxlen`
  3684. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  3685. * UTF-8 sequence, it may convert a portion of the final character to one or
  3686. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  3687. * a buffer.
  3688. *
  3689. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  3690. * match to this number of bytes (or both have matched to a null-terminator
  3691. * character before this number of bytes), they will be considered equal.
  3692. *
  3693. * \param str1 the first string to compare. NULL is not permitted!
  3694. * \param str2 the second string to compare. NULL is not permitted!
  3695. * \param maxlen the maximum number of bytes to compare.
  3696. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3697. * str1 is "greater than" str2, and zero if the strings match
  3698. * exactly.
  3699. *
  3700. * \threadsafety It is safe to call this function from any thread.
  3701. *
  3702. * \since This function is available since SDL 3.2.0.
  3703. */
  3704. extern SDL_DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  3705. /**
  3706. * Searches a string for the first occurrence of any character contained in a
  3707. * breakset, and returns a pointer from the string to that character.
  3708. *
  3709. * \param str The null-terminated string to be searched. Must not be NULL, and
  3710. * must not overlap with `breakset`.
  3711. * \param breakset A null-terminated string containing the list of characters
  3712. * to look for. Must not be NULL, and must not overlap with
  3713. * `str`.
  3714. * \returns A pointer to the location, in str, of the first occurrence of a
  3715. * character present in the breakset, or NULL if none is found.
  3716. *
  3717. * \threadsafety It is safe to call this function from any thread.
  3718. *
  3719. * \since This function is available since SDL 3.2.0.
  3720. */
  3721. extern SDL_DECLSPEC char * SDLCALL SDL_strpbrk(const char *str, const char *breakset);
  3722. /**
  3723. * The Unicode REPLACEMENT CHARACTER codepoint.
  3724. *
  3725. * SDL_StepUTF8() and SDL_StepBackUTF8() report this codepoint when they
  3726. * encounter a UTF-8 string with encoding errors.
  3727. *
  3728. * This tends to render as something like a question mark in most places.
  3729. *
  3730. * \since This macro is available since SDL 3.2.0.
  3731. *
  3732. * \sa SDL_StepBackUTF8
  3733. * \sa SDL_StepUTF8
  3734. */
  3735. #define SDL_INVALID_UNICODE_CODEPOINT 0xFFFD
  3736. /**
  3737. * Decode a UTF-8 string, one Unicode codepoint at a time.
  3738. *
  3739. * This will return the first Unicode codepoint in the UTF-8 encoded string in
  3740. * `*pstr`, and then advance `*pstr` past any consumed bytes before returning.
  3741. *
  3742. * It will not access more than `*pslen` bytes from the string. `*pslen` will
  3743. * be adjusted, as well, subtracting the number of bytes consumed.
  3744. *
  3745. * `pslen` is allowed to be NULL, in which case the string _must_ be
  3746. * NULL-terminated, as the function will blindly read until it sees the NULL
  3747. * char.
  3748. *
  3749. * if `*pslen` is zero, it assumes the end of string is reached and returns a
  3750. * zero codepoint regardless of the contents of the string buffer.
  3751. *
  3752. * If the resulting codepoint is zero (a NULL terminator), or `*pslen` is
  3753. * zero, it will not advance `*pstr` or `*pslen` at all.
  3754. *
  3755. * Generally this function is called in a loop until it returns zero,
  3756. * adjusting its parameters each iteration.
  3757. *
  3758. * If an invalid UTF-8 sequence is encountered, this function returns
  3759. * SDL_INVALID_UNICODE_CODEPOINT and advances the string/length by one byte
  3760. * (which is to say, a multibyte sequence might produce several
  3761. * SDL_INVALID_UNICODE_CODEPOINT returns before it syncs to the next valid
  3762. * UTF-8 sequence).
  3763. *
  3764. * Several things can generate invalid UTF-8 sequences, including overlong
  3765. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  3766. * refer to
  3767. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  3768. * for details.
  3769. *
  3770. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  3771. * \param pslen a pointer to the number of bytes in the string, to be read and
  3772. * adjusted. NULL is allowed.
  3773. * \returns the first Unicode codepoint in the string.
  3774. *
  3775. * \threadsafety It is safe to call this function from any thread.
  3776. *
  3777. * \since This function is available since SDL 3.2.0.
  3778. */
  3779. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepUTF8(const char **pstr, size_t *pslen);
  3780. /**
  3781. * Decode a UTF-8 string in reverse, one Unicode codepoint at a time.
  3782. *
  3783. * This will go to the start of the previous Unicode codepoint in the string,
  3784. * move `*pstr` to that location and return that codepoint.
  3785. *
  3786. * If `*pstr` is already at the start of the string), it will not advance
  3787. * `*pstr` at all.
  3788. *
  3789. * Generally this function is called in a loop until it returns zero,
  3790. * adjusting its parameter each iteration.
  3791. *
  3792. * If an invalid UTF-8 sequence is encountered, this function returns
  3793. * SDL_INVALID_UNICODE_CODEPOINT.
  3794. *
  3795. * Several things can generate invalid UTF-8 sequences, including overlong
  3796. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  3797. * refer to
  3798. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  3799. * for details.
  3800. *
  3801. * \param start a pointer to the beginning of the UTF-8 string.
  3802. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  3803. * \returns the previous Unicode codepoint in the string.
  3804. *
  3805. * \threadsafety It is safe to call this function from any thread.
  3806. *
  3807. * \since This function is available since SDL 3.2.0.
  3808. */
  3809. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepBackUTF8(const char *start, const char **pstr);
  3810. /**
  3811. * Convert a single Unicode codepoint to UTF-8.
  3812. *
  3813. * The buffer pointed to by `dst` must be at least 4 bytes long, as this
  3814. * function may generate between 1 and 4 bytes of output.
  3815. *
  3816. * This function returns the first byte _after_ the newly-written UTF-8
  3817. * sequence, which is useful for encoding multiple codepoints in a loop, or
  3818. * knowing where to write a NULL-terminator character to end the string (in
  3819. * either case, plan to have a buffer of _more_ than 4 bytes!).
  3820. *
  3821. * If `codepoint` is an invalid value (outside the Unicode range, or a UTF-16
  3822. * surrogate value, etc), this will use U+FFFD (REPLACEMENT CHARACTER) for the
  3823. * codepoint instead, and not set an error.
  3824. *
  3825. * If `dst` is NULL, this returns NULL immediately without writing to the
  3826. * pointer and without setting an error.
  3827. *
  3828. * \param codepoint a Unicode codepoint to convert to UTF-8.
  3829. * \param dst the location to write the encoded UTF-8. Must point to at least
  3830. * 4 bytes!
  3831. * \returns the first byte past the newly-written UTF-8 sequence.
  3832. *
  3833. * \threadsafety It is safe to call this function from any thread.
  3834. *
  3835. * \since This function is available since SDL 3.2.0.
  3836. */
  3837. extern SDL_DECLSPEC char * SDLCALL SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);
  3838. /**
  3839. * This works exactly like sscanf() but doesn't require access to a C runtime.
  3840. *
  3841. * Scan a string, matching a format string, converting each '%' item and
  3842. * storing it to pointers provided through variable arguments.
  3843. *
  3844. * \param text the string to scan. Must not be NULL.
  3845. * \param fmt a printf-style format string. Must not be NULL.
  3846. * \param ... a list of pointers to values to be filled in with scanned items.
  3847. * \returns the number of items that matched the format string.
  3848. *
  3849. * \threadsafety It is safe to call this function from any thread.
  3850. *
  3851. * \since This function is available since SDL 3.2.0.
  3852. */
  3853. extern SDL_DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  3854. /**
  3855. * This works exactly like vsscanf() but doesn't require access to a C
  3856. * runtime.
  3857. *
  3858. * Functions identically to SDL_sscanf(), except it takes a `va_list` instead
  3859. * of using `...` variable arguments.
  3860. *
  3861. * \param text the string to scan. Must not be NULL.
  3862. * \param fmt a printf-style format string. Must not be NULL.
  3863. * \param ap a `va_list` of pointers to values to be filled in with scanned
  3864. * items.
  3865. * \returns the number of items that matched the format string.
  3866. *
  3867. * \threadsafety It is safe to call this function from any thread.
  3868. *
  3869. * \since This function is available since SDL 3.2.0.
  3870. */
  3871. extern SDL_DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
  3872. /**
  3873. * This works exactly like snprintf() but doesn't require access to a C
  3874. * runtime.
  3875. *
  3876. * Format a string of up to `maxlen`-1 bytes, converting each '%' item with
  3877. * values provided through variable arguments.
  3878. *
  3879. * While some C runtimes differ on how to deal with too-large strings, this
  3880. * function null-terminates the output, by treating the null-terminator as
  3881. * part of the `maxlen` count. Note that if `maxlen` is zero, however, no
  3882. * bytes will be written at all.
  3883. *
  3884. * This function returns the number of _bytes_ (not _characters_) that should
  3885. * be written, excluding the null-terminator character. If this returns a
  3886. * number >= `maxlen`, it means the output string was truncated. A negative
  3887. * return value means an error occurred.
  3888. *
  3889. * Referencing the output string's pointer with a format item is undefined
  3890. * behavior.
  3891. *
  3892. * \param text the buffer to write the string into. Must not be NULL.
  3893. * \param maxlen the maximum bytes to write, including the null-terminator.
  3894. * \param fmt a printf-style format string. Must not be NULL.
  3895. * \param ... a list of values to be used with the format string.
  3896. * \returns the number of bytes that should be written, not counting the
  3897. * null-terminator char, or a negative value on error.
  3898. *
  3899. * \threadsafety It is safe to call this function from any thread.
  3900. *
  3901. * \since This function is available since SDL 3.2.0.
  3902. */
  3903. extern SDL_DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(3);
  3904. /**
  3905. * This works exactly like swprintf() but doesn't require access to a C
  3906. * runtime.
  3907. *
  3908. * Format a wide string of up to `maxlen`-1 wchar_t values, converting each
  3909. * '%' item with values provided through variable arguments.
  3910. *
  3911. * While some C runtimes differ on how to deal with too-large strings, this
  3912. * function null-terminates the output, by treating the null-terminator as
  3913. * part of the `maxlen` count. Note that if `maxlen` is zero, however, no wide
  3914. * characters will be written at all.
  3915. *
  3916. * This function returns the number of _wide characters_ (not _codepoints_)
  3917. * that should be written, excluding the null-terminator character. If this
  3918. * returns a number >= `maxlen`, it means the output string was truncated. A
  3919. * negative return value means an error occurred.
  3920. *
  3921. * Referencing the output string's pointer with a format item is undefined
  3922. * behavior.
  3923. *
  3924. * \param text the buffer to write the wide string into. Must not be NULL.
  3925. * \param maxlen the maximum wchar_t values to write, including the
  3926. * null-terminator.
  3927. * \param fmt a printf-style format string. Must not be NULL.
  3928. * \param ... a list of values to be used with the format string.
  3929. * \returns the number of wide characters that should be written, not counting
  3930. * the null-terminator char, or a negative value on error.
  3931. *
  3932. * \threadsafety It is safe to call this function from any thread.
  3933. *
  3934. * \since This function is available since SDL 3.2.0.
  3935. */
  3936. extern SDL_DECLSPEC int SDLCALL SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...) SDL_WPRINTF_VARARG_FUNC(3);
  3937. /**
  3938. * This works exactly like vsnprintf() but doesn't require access to a C
  3939. * runtime.
  3940. *
  3941. * Functions identically to SDL_snprintf(), except it takes a `va_list`
  3942. * instead of using `...` variable arguments.
  3943. *
  3944. * \param text the buffer to write the string into. Must not be NULL.
  3945. * \param maxlen the maximum bytes to write, including the null-terminator.
  3946. * \param fmt a printf-style format string. Must not be NULL.
  3947. * \param ap a `va_list` values to be used with the format string.
  3948. * \returns the number of bytes that should be written, not counting the
  3949. * null-terminator char, or a negative value on error.
  3950. *
  3951. * \threadsafety It is safe to call this function from any thread.
  3952. *
  3953. * \since This function is available since SDL 3.2.0.
  3954. */
  3955. extern SDL_DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3);
  3956. /**
  3957. * This works exactly like vswprintf() but doesn't require access to a C
  3958. * runtime.
  3959. *
  3960. * Functions identically to SDL_swprintf(), except it takes a `va_list`
  3961. * instead of using `...` variable arguments.
  3962. *
  3963. * \param text the buffer to write the string into. Must not be NULL.
  3964. * \param maxlen the maximum wide characters to write, including the
  3965. * null-terminator.
  3966. * \param fmt a printf-style format wide string. Must not be NULL.
  3967. * \param ap a `va_list` values to be used with the format string.
  3968. * \returns the number of wide characters that should be written, not counting
  3969. * the null-terminator char, or a negative value on error.
  3970. *
  3971. * \threadsafety It is safe to call this function from any thread.
  3972. *
  3973. * \since This function is available since SDL 3.2.0.
  3974. */
  3975. extern SDL_DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNCV(3);
  3976. /**
  3977. * This works exactly like asprintf() but doesn't require access to a C
  3978. * runtime.
  3979. *
  3980. * Functions identically to SDL_snprintf(), except it allocates a buffer large
  3981. * enough to hold the output string on behalf of the caller.
  3982. *
  3983. * On success, this function returns the number of bytes (not characters)
  3984. * comprising the output string, not counting the null-terminator character,
  3985. * and sets `*strp` to the newly-allocated string.
  3986. *
  3987. * On error, this function returns a negative number, and the value of `*strp`
  3988. * is undefined.
  3989. *
  3990. * The returned string is owned by the caller, and should be passed to
  3991. * SDL_free when no longer needed.
  3992. *
  3993. * \param strp on output, is set to the new string. Must not be NULL.
  3994. * \param fmt a printf-style format string. Must not be NULL.
  3995. * \param ... a list of values to be used with the format string.
  3996. * \returns the number of bytes in the newly-allocated string, not counting
  3997. * the null-terminator char, or a negative value on error.
  3998. *
  3999. * \threadsafety It is safe to call this function from any thread.
  4000. *
  4001. * \since This function is available since SDL 3.2.0.
  4002. */
  4003. extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  4004. /**
  4005. * This works exactly like vasprintf() but doesn't require access to a C
  4006. * runtime.
  4007. *
  4008. * Functions identically to SDL_asprintf(), except it takes a `va_list`
  4009. * instead of using `...` variable arguments.
  4010. *
  4011. * \param strp on output, is set to the new string. Must not be NULL.
  4012. * \param fmt a printf-style format string. Must not be NULL.
  4013. * \param ap a `va_list` values to be used with the format string.
  4014. * \returns the number of bytes in the newly-allocated string, not counting
  4015. * the null-terminator char, or a negative value on error.
  4016. *
  4017. * \threadsafety It is safe to call this function from any thread.
  4018. *
  4019. * \since This function is available since SDL 3.2.0.
  4020. */
  4021. extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  4022. /**
  4023. * Seeds the pseudo-random number generator.
  4024. *
  4025. * Reusing the seed number will cause SDL_rand() to repeat the same stream of
  4026. * 'random' numbers.
  4027. *
  4028. * \param seed the value to use as a random number seed, or 0 to use
  4029. * SDL_GetPerformanceCounter().
  4030. *
  4031. * \threadsafety This should be called on the same thread that calls
  4032. * SDL_rand()
  4033. *
  4034. * \since This function is available since SDL 3.2.0.
  4035. *
  4036. * \sa SDL_rand
  4037. * \sa SDL_rand_bits
  4038. * \sa SDL_randf
  4039. */
  4040. extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
  4041. /**
  4042. * Generate a pseudo-random number less than n for positive n
  4043. *
  4044. * The method used is faster and of better quality than `rand() % n`. Odds are
  4045. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  4046. * much worse as n gets bigger.
  4047. *
  4048. * Example: to simulate a d6 use `SDL_rand(6) + 1` The +1 converts 0..5 to
  4049. * 1..6
  4050. *
  4051. * If you want to generate a pseudo-random number in the full range of Sint32,
  4052. * you should use: (Sint32)SDL_rand_bits()
  4053. *
  4054. * If you want reproducible output, be sure to initialize with SDL_srand()
  4055. * first.
  4056. *
  4057. * There are no guarantees as to the quality of the random sequence produced,
  4058. * and this should not be used for security (cryptography, passwords) or where
  4059. * money is on the line (loot-boxes, casinos). There are many random number
  4060. * libraries available with different characteristics and you should pick one
  4061. * of those to meet any serious needs.
  4062. *
  4063. * \param n the number of possible outcomes. n must be positive.
  4064. * \returns a random value in the range of [0 .. n-1].
  4065. *
  4066. * \threadsafety All calls should be made from a single thread
  4067. *
  4068. * \since This function is available since SDL 3.2.0.
  4069. *
  4070. * \sa SDL_srand
  4071. * \sa SDL_randf
  4072. */
  4073. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand(Sint32 n);
  4074. /**
  4075. * Generate a uniform pseudo-random floating point number less than 1.0
  4076. *
  4077. * If you want reproducible output, be sure to initialize with SDL_srand()
  4078. * first.
  4079. *
  4080. * There are no guarantees as to the quality of the random sequence produced,
  4081. * and this should not be used for security (cryptography, passwords) or where
  4082. * money is on the line (loot-boxes, casinos). There are many random number
  4083. * libraries available with different characteristics and you should pick one
  4084. * of those to meet any serious needs.
  4085. *
  4086. * \returns a random value in the range of [0.0, 1.0).
  4087. *
  4088. * \threadsafety All calls should be made from a single thread
  4089. *
  4090. * \since This function is available since SDL 3.2.0.
  4091. *
  4092. * \sa SDL_srand
  4093. * \sa SDL_rand
  4094. */
  4095. extern SDL_DECLSPEC float SDLCALL SDL_randf(void);
  4096. /**
  4097. * Generate 32 pseudo-random bits.
  4098. *
  4099. * You likely want to use SDL_rand() to get a psuedo-random number instead.
  4100. *
  4101. * There are no guarantees as to the quality of the random sequence produced,
  4102. * and this should not be used for security (cryptography, passwords) or where
  4103. * money is on the line (loot-boxes, casinos). There are many random number
  4104. * libraries available with different characteristics and you should pick one
  4105. * of those to meet any serious needs.
  4106. *
  4107. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  4108. *
  4109. * \threadsafety All calls should be made from a single thread
  4110. *
  4111. * \since This function is available since SDL 3.2.0.
  4112. *
  4113. * \sa SDL_rand
  4114. * \sa SDL_randf
  4115. * \sa SDL_srand
  4116. */
  4117. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
  4118. /**
  4119. * Generate a pseudo-random number less than n for positive n
  4120. *
  4121. * The method used is faster and of better quality than `rand() % n`. Odds are
  4122. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  4123. * much worse as n gets bigger.
  4124. *
  4125. * Example: to simulate a d6 use `SDL_rand_r(state, 6) + 1` The +1 converts
  4126. * 0..5 to 1..6
  4127. *
  4128. * If you want to generate a pseudo-random number in the full range of Sint32,
  4129. * you should use: (Sint32)SDL_rand_bits_r(state)
  4130. *
  4131. * There are no guarantees as to the quality of the random sequence produced,
  4132. * and this should not be used for security (cryptography, passwords) or where
  4133. * money is on the line (loot-boxes, casinos). There are many random number
  4134. * libraries available with different characteristics and you should pick one
  4135. * of those to meet any serious needs.
  4136. *
  4137. * \param state a pointer to the current random number state, this may not be
  4138. * NULL.
  4139. * \param n the number of possible outcomes. n must be positive.
  4140. * \returns a random value in the range of [0 .. n-1].
  4141. *
  4142. * \threadsafety This function is thread-safe, as long as the state pointer
  4143. * isn't shared between threads.
  4144. *
  4145. * \since This function is available since SDL 3.2.0.
  4146. *
  4147. * \sa SDL_rand
  4148. * \sa SDL_rand_bits_r
  4149. * \sa SDL_randf_r
  4150. */
  4151. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_r(Uint64 *state, Sint32 n);
  4152. /**
  4153. * Generate a uniform pseudo-random floating point number less than 1.0
  4154. *
  4155. * If you want reproducible output, be sure to initialize with SDL_srand()
  4156. * first.
  4157. *
  4158. * There are no guarantees as to the quality of the random sequence produced,
  4159. * and this should not be used for security (cryptography, passwords) or where
  4160. * money is on the line (loot-boxes, casinos). There are many random number
  4161. * libraries available with different characteristics and you should pick one
  4162. * of those to meet any serious needs.
  4163. *
  4164. * \param state a pointer to the current random number state, this may not be
  4165. * NULL.
  4166. * \returns a random value in the range of [0.0, 1.0).
  4167. *
  4168. * \threadsafety This function is thread-safe, as long as the state pointer
  4169. * isn't shared between threads.
  4170. *
  4171. * \since This function is available since SDL 3.2.0.
  4172. *
  4173. * \sa SDL_rand_bits_r
  4174. * \sa SDL_rand_r
  4175. * \sa SDL_randf
  4176. */
  4177. extern SDL_DECLSPEC float SDLCALL SDL_randf_r(Uint64 *state);
  4178. /**
  4179. * Generate 32 pseudo-random bits.
  4180. *
  4181. * You likely want to use SDL_rand_r() to get a psuedo-random number instead.
  4182. *
  4183. * There are no guarantees as to the quality of the random sequence produced,
  4184. * and this should not be used for security (cryptography, passwords) or where
  4185. * money is on the line (loot-boxes, casinos). There are many random number
  4186. * libraries available with different characteristics and you should pick one
  4187. * of those to meet any serious needs.
  4188. *
  4189. * \param state a pointer to the current random number state, this may not be
  4190. * NULL.
  4191. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  4192. *
  4193. * \threadsafety This function is thread-safe, as long as the state pointer
  4194. * isn't shared between threads.
  4195. *
  4196. * \since This function is available since SDL 3.2.0.
  4197. *
  4198. * \sa SDL_rand_r
  4199. * \sa SDL_randf_r
  4200. */
  4201. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits_r(Uint64 *state);
  4202. #ifndef SDL_PI_D
  4203. /**
  4204. * The value of Pi, as a double-precision floating point literal.
  4205. *
  4206. * \since This macro is available since SDL 3.2.0.
  4207. *
  4208. * \sa SDL_PI_F
  4209. */
  4210. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  4211. #endif
  4212. #ifndef SDL_PI_F
  4213. /**
  4214. * The value of Pi, as a single-precision floating point literal.
  4215. *
  4216. * \since This macro is available since SDL 3.2.0.
  4217. *
  4218. * \sa SDL_PI_D
  4219. */
  4220. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  4221. #endif
  4222. /**
  4223. * Compute the arc cosine of `x`.
  4224. *
  4225. * The definition of `y = acos(x)` is `x = cos(y)`.
  4226. *
  4227. * Domain: `-1 <= x <= 1`
  4228. *
  4229. * Range: `0 <= y <= Pi`
  4230. *
  4231. * This function operates on double-precision floating point values, use
  4232. * SDL_acosf for single-precision floats.
  4233. *
  4234. * This function may use a different approximation across different versions,
  4235. * platforms and configurations. i.e, it can return a different value given
  4236. * the same input on different machines or operating systems, or if SDL is
  4237. * updated.
  4238. *
  4239. * \param x floating point value.
  4240. * \returns arc cosine of `x`, in radians.
  4241. *
  4242. * \threadsafety It is safe to call this function from any thread.
  4243. *
  4244. * \since This function is available since SDL 3.2.0.
  4245. *
  4246. * \sa SDL_acosf
  4247. * \sa SDL_asin
  4248. * \sa SDL_cos
  4249. */
  4250. extern SDL_DECLSPEC double SDLCALL SDL_acos(double x);
  4251. /**
  4252. * Compute the arc cosine of `x`.
  4253. *
  4254. * The definition of `y = acos(x)` is `x = cos(y)`.
  4255. *
  4256. * Domain: `-1 <= x <= 1`
  4257. *
  4258. * Range: `0 <= y <= Pi`
  4259. *
  4260. * This function operates on single-precision floating point values, use
  4261. * SDL_acos for double-precision floats.
  4262. *
  4263. * This function may use a different approximation across different versions,
  4264. * platforms and configurations. i.e, it can return a different value given
  4265. * the same input on different machines or operating systems, or if SDL is
  4266. * updated.
  4267. *
  4268. * \param x floating point value.
  4269. * \returns arc cosine of `x`, in radians.
  4270. *
  4271. * \threadsafety It is safe to call this function from any thread.
  4272. *
  4273. * \since This function is available since SDL 3.2.0.
  4274. *
  4275. * \sa SDL_acos
  4276. * \sa SDL_asinf
  4277. * \sa SDL_cosf
  4278. */
  4279. extern SDL_DECLSPEC float SDLCALL SDL_acosf(float x);
  4280. /**
  4281. * Compute the arc sine of `x`.
  4282. *
  4283. * The definition of `y = asin(x)` is `x = sin(y)`.
  4284. *
  4285. * Domain: `-1 <= x <= 1`
  4286. *
  4287. * Range: `-Pi/2 <= y <= Pi/2`
  4288. *
  4289. * This function operates on double-precision floating point values, use
  4290. * SDL_asinf for single-precision floats.
  4291. *
  4292. * This function may use a different approximation across different versions,
  4293. * platforms and configurations. i.e, it can return a different value given
  4294. * the same input on different machines or operating systems, or if SDL is
  4295. * updated.
  4296. *
  4297. * \param x floating point value.
  4298. * \returns arc sine of `x`, in radians.
  4299. *
  4300. * \threadsafety It is safe to call this function from any thread.
  4301. *
  4302. * \since This function is available since SDL 3.2.0.
  4303. *
  4304. * \sa SDL_asinf
  4305. * \sa SDL_acos
  4306. * \sa SDL_sin
  4307. */
  4308. extern SDL_DECLSPEC double SDLCALL SDL_asin(double x);
  4309. /**
  4310. * Compute the arc sine of `x`.
  4311. *
  4312. * The definition of `y = asin(x)` is `x = sin(y)`.
  4313. *
  4314. * Domain: `-1 <= x <= 1`
  4315. *
  4316. * Range: `-Pi/2 <= y <= Pi/2`
  4317. *
  4318. * This function operates on single-precision floating point values, use
  4319. * SDL_asin for double-precision floats.
  4320. *
  4321. * This function may use a different approximation across different versions,
  4322. * platforms and configurations. i.e, it can return a different value given
  4323. * the same input on different machines or operating systems, or if SDL is
  4324. * updated.
  4325. *
  4326. * \param x floating point value.
  4327. * \returns arc sine of `x`, in radians.
  4328. *
  4329. * \threadsafety It is safe to call this function from any thread.
  4330. *
  4331. * \since This function is available since SDL 3.2.0.
  4332. *
  4333. * \sa SDL_asin
  4334. * \sa SDL_acosf
  4335. * \sa SDL_sinf
  4336. */
  4337. extern SDL_DECLSPEC float SDLCALL SDL_asinf(float x);
  4338. /**
  4339. * Compute the arc tangent of `x`.
  4340. *
  4341. * The definition of `y = atan(x)` is `x = tan(y)`.
  4342. *
  4343. * Domain: `-INF <= x <= INF`
  4344. *
  4345. * Range: `-Pi/2 <= y <= Pi/2`
  4346. *
  4347. * This function operates on double-precision floating point values, use
  4348. * SDL_atanf for single-precision floats.
  4349. *
  4350. * To calculate the arc tangent of y / x, use SDL_atan2.
  4351. *
  4352. * This function may use a different approximation across different versions,
  4353. * platforms and configurations. i.e, it can return a different value given
  4354. * the same input on different machines or operating systems, or if SDL is
  4355. * updated.
  4356. *
  4357. * \param x floating point value.
  4358. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  4359. *
  4360. * \threadsafety It is safe to call this function from any thread.
  4361. *
  4362. * \since This function is available since SDL 3.2.0.
  4363. *
  4364. * \sa SDL_atanf
  4365. * \sa SDL_atan2
  4366. * \sa SDL_tan
  4367. */
  4368. extern SDL_DECLSPEC double SDLCALL SDL_atan(double x);
  4369. /**
  4370. * Compute the arc tangent of `x`.
  4371. *
  4372. * The definition of `y = atan(x)` is `x = tan(y)`.
  4373. *
  4374. * Domain: `-INF <= x <= INF`
  4375. *
  4376. * Range: `-Pi/2 <= y <= Pi/2`
  4377. *
  4378. * This function operates on single-precision floating point values, use
  4379. * SDL_atan for dboule-precision floats.
  4380. *
  4381. * To calculate the arc tangent of y / x, use SDL_atan2f.
  4382. *
  4383. * This function may use a different approximation across different versions,
  4384. * platforms and configurations. i.e, it can return a different value given
  4385. * the same input on different machines or operating systems, or if SDL is
  4386. * updated.
  4387. *
  4388. * \param x floating point value.
  4389. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  4390. *
  4391. * \threadsafety It is safe to call this function from any thread.
  4392. *
  4393. * \since This function is available since SDL 3.2.0.
  4394. *
  4395. * \sa SDL_atan
  4396. * \sa SDL_atan2f
  4397. * \sa SDL_tanf
  4398. */
  4399. extern SDL_DECLSPEC float SDLCALL SDL_atanf(float x);
  4400. /**
  4401. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  4402. * the result's quadrant.
  4403. *
  4404. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  4405. * of z is determined based on the signs of x and y.
  4406. *
  4407. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  4408. *
  4409. * Range: `-Pi <= y <= Pi`
  4410. *
  4411. * This function operates on double-precision floating point values, use
  4412. * SDL_atan2f for single-precision floats.
  4413. *
  4414. * To calculate the arc tangent of a single value, use SDL_atan.
  4415. *
  4416. * This function may use a different approximation across different versions,
  4417. * platforms and configurations. i.e, it can return a different value given
  4418. * the same input on different machines or operating systems, or if SDL is
  4419. * updated.
  4420. *
  4421. * \param y floating point value of the numerator (y coordinate).
  4422. * \param x floating point value of the denominator (x coordinate).
  4423. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  4424. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  4425. *
  4426. * \threadsafety It is safe to call this function from any thread.
  4427. *
  4428. * \since This function is available since SDL 3.2.0.
  4429. *
  4430. * \sa SDL_atan2f
  4431. * \sa SDL_atan
  4432. * \sa SDL_tan
  4433. */
  4434. extern SDL_DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  4435. /**
  4436. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  4437. * the result's quadrant.
  4438. *
  4439. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  4440. * of z is determined based on the signs of x and y.
  4441. *
  4442. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  4443. *
  4444. * Range: `-Pi <= y <= Pi`
  4445. *
  4446. * This function operates on single-precision floating point values, use
  4447. * SDL_atan2 for double-precision floats.
  4448. *
  4449. * To calculate the arc tangent of a single value, use SDL_atanf.
  4450. *
  4451. * This function may use a different approximation across different versions,
  4452. * platforms and configurations. i.e, it can return a different value given
  4453. * the same input on different machines or operating systems, or if SDL is
  4454. * updated.
  4455. *
  4456. * \param y floating point value of the numerator (y coordinate).
  4457. * \param x floating point value of the denominator (x coordinate).
  4458. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  4459. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  4460. *
  4461. * \threadsafety It is safe to call this function from any thread.
  4462. *
  4463. * \since This function is available since SDL 3.2.0.
  4464. *
  4465. * \sa SDL_atan2
  4466. * \sa SDL_atan
  4467. * \sa SDL_tan
  4468. */
  4469. extern SDL_DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  4470. /**
  4471. * Compute the ceiling of `x`.
  4472. *
  4473. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  4474. * rounded up to the nearest integer.
  4475. *
  4476. * Domain: `-INF <= x <= INF`
  4477. *
  4478. * Range: `-INF <= y <= INF`, y integer
  4479. *
  4480. * This function operates on double-precision floating point values, use
  4481. * SDL_ceilf for single-precision floats.
  4482. *
  4483. * \param x floating point value.
  4484. * \returns the ceiling of `x`.
  4485. *
  4486. * \threadsafety It is safe to call this function from any thread.
  4487. *
  4488. * \since This function is available since SDL 3.2.0.
  4489. *
  4490. * \sa SDL_ceilf
  4491. * \sa SDL_floor
  4492. * \sa SDL_trunc
  4493. * \sa SDL_round
  4494. * \sa SDL_lround
  4495. */
  4496. extern SDL_DECLSPEC double SDLCALL SDL_ceil(double x);
  4497. /**
  4498. * Compute the ceiling of `x`.
  4499. *
  4500. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  4501. * rounded up to the nearest integer.
  4502. *
  4503. * Domain: `-INF <= x <= INF`
  4504. *
  4505. * Range: `-INF <= y <= INF`, y integer
  4506. *
  4507. * This function operates on single-precision floating point values, use
  4508. * SDL_ceil for double-precision floats.
  4509. *
  4510. * \param x floating point value.
  4511. * \returns the ceiling of `x`.
  4512. *
  4513. * \threadsafety It is safe to call this function from any thread.
  4514. *
  4515. * \since This function is available since SDL 3.2.0.
  4516. *
  4517. * \sa SDL_ceil
  4518. * \sa SDL_floorf
  4519. * \sa SDL_truncf
  4520. * \sa SDL_roundf
  4521. * \sa SDL_lroundf
  4522. */
  4523. extern SDL_DECLSPEC float SDLCALL SDL_ceilf(float x);
  4524. /**
  4525. * Copy the sign of one floating-point value to another.
  4526. *
  4527. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  4528. *
  4529. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  4530. *
  4531. * Range: `-INF <= z <= INF`
  4532. *
  4533. * This function operates on double-precision floating point values, use
  4534. * SDL_copysignf for single-precision floats.
  4535. *
  4536. * \param x floating point value to use as the magnitude.
  4537. * \param y floating point value to use as the sign.
  4538. * \returns the floating point value with the sign of y and the magnitude of
  4539. * x.
  4540. *
  4541. * \threadsafety It is safe to call this function from any thread.
  4542. *
  4543. * \since This function is available since SDL 3.2.0.
  4544. *
  4545. * \sa SDL_copysignf
  4546. * \sa SDL_fabs
  4547. */
  4548. extern SDL_DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  4549. /**
  4550. * Copy the sign of one floating-point value to another.
  4551. *
  4552. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  4553. *
  4554. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  4555. *
  4556. * Range: `-INF <= z <= INF`
  4557. *
  4558. * This function operates on single-precision floating point values, use
  4559. * SDL_copysign for double-precision floats.
  4560. *
  4561. * \param x floating point value to use as the magnitude.
  4562. * \param y floating point value to use as the sign.
  4563. * \returns the floating point value with the sign of y and the magnitude of
  4564. * x.
  4565. *
  4566. * \threadsafety It is safe to call this function from any thread.
  4567. *
  4568. * \since This function is available since SDL 3.2.0.
  4569. *
  4570. * \sa SDL_copysign
  4571. * \sa SDL_fabsf
  4572. */
  4573. extern SDL_DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  4574. /**
  4575. * Compute the cosine of `x`.
  4576. *
  4577. * Domain: `-INF <= x <= INF`
  4578. *
  4579. * Range: `-1 <= y <= 1`
  4580. *
  4581. * This function operates on double-precision floating point values, use
  4582. * SDL_cosf for single-precision floats.
  4583. *
  4584. * This function may use a different approximation across different versions,
  4585. * platforms and configurations. i.e, it can return a different value given
  4586. * the same input on different machines or operating systems, or if SDL is
  4587. * updated.
  4588. *
  4589. * \param x floating point value, in radians.
  4590. * \returns cosine of `x`.
  4591. *
  4592. * \threadsafety It is safe to call this function from any thread.
  4593. *
  4594. * \since This function is available since SDL 3.2.0.
  4595. *
  4596. * \sa SDL_cosf
  4597. * \sa SDL_acos
  4598. * \sa SDL_sin
  4599. */
  4600. extern SDL_DECLSPEC double SDLCALL SDL_cos(double x);
  4601. /**
  4602. * Compute the cosine of `x`.
  4603. *
  4604. * Domain: `-INF <= x <= INF`
  4605. *
  4606. * Range: `-1 <= y <= 1`
  4607. *
  4608. * This function operates on single-precision floating point values, use
  4609. * SDL_cos for double-precision floats.
  4610. *
  4611. * This function may use a different approximation across different versions,
  4612. * platforms and configurations. i.e, it can return a different value given
  4613. * the same input on different machines or operating systems, or if SDL is
  4614. * updated.
  4615. *
  4616. * \param x floating point value, in radians.
  4617. * \returns cosine of `x`.
  4618. *
  4619. * \threadsafety It is safe to call this function from any thread.
  4620. *
  4621. * \since This function is available since SDL 3.2.0.
  4622. *
  4623. * \sa SDL_cos
  4624. * \sa SDL_acosf
  4625. * \sa SDL_sinf
  4626. */
  4627. extern SDL_DECLSPEC float SDLCALL SDL_cosf(float x);
  4628. /**
  4629. * Compute the exponential of `x`.
  4630. *
  4631. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  4632. * natural logarithm. The inverse is the natural logarithm, SDL_log.
  4633. *
  4634. * Domain: `-INF <= x <= INF`
  4635. *
  4636. * Range: `0 <= y <= INF`
  4637. *
  4638. * The output will overflow if `exp(x)` is too large to be represented.
  4639. *
  4640. * This function operates on double-precision floating point values, use
  4641. * SDL_expf for single-precision floats.
  4642. *
  4643. * This function may use a different approximation across different versions,
  4644. * platforms and configurations. i.e, it can return a different value given
  4645. * the same input on different machines or operating systems, or if SDL is
  4646. * updated.
  4647. *
  4648. * \param x floating point value.
  4649. * \returns value of `e^x`.
  4650. *
  4651. * \threadsafety It is safe to call this function from any thread.
  4652. *
  4653. * \since This function is available since SDL 3.2.0.
  4654. *
  4655. * \sa SDL_expf
  4656. * \sa SDL_log
  4657. */
  4658. extern SDL_DECLSPEC double SDLCALL SDL_exp(double x);
  4659. /**
  4660. * Compute the exponential of `x`.
  4661. *
  4662. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  4663. * natural logarithm. The inverse is the natural logarithm, SDL_logf.
  4664. *
  4665. * Domain: `-INF <= x <= INF`
  4666. *
  4667. * Range: `0 <= y <= INF`
  4668. *
  4669. * The output will overflow if `exp(x)` is too large to be represented.
  4670. *
  4671. * This function operates on single-precision floating point values, use
  4672. * SDL_exp for double-precision floats.
  4673. *
  4674. * This function may use a different approximation across different versions,
  4675. * platforms and configurations. i.e, it can return a different value given
  4676. * the same input on different machines or operating systems, or if SDL is
  4677. * updated.
  4678. *
  4679. * \param x floating point value.
  4680. * \returns value of `e^x`.
  4681. *
  4682. * \threadsafety It is safe to call this function from any thread.
  4683. *
  4684. * \since This function is available since SDL 3.2.0.
  4685. *
  4686. * \sa SDL_exp
  4687. * \sa SDL_logf
  4688. */
  4689. extern SDL_DECLSPEC float SDLCALL SDL_expf(float x);
  4690. /**
  4691. * Compute the absolute value of `x`
  4692. *
  4693. * Domain: `-INF <= x <= INF`
  4694. *
  4695. * Range: `0 <= y <= INF`
  4696. *
  4697. * This function operates on double-precision floating point values, use
  4698. * SDL_fabsf for single-precision floats.
  4699. *
  4700. * \param x floating point value to use as the magnitude.
  4701. * \returns the absolute value of `x`.
  4702. *
  4703. * \threadsafety It is safe to call this function from any thread.
  4704. *
  4705. * \since This function is available since SDL 3.2.0.
  4706. *
  4707. * \sa SDL_fabsf
  4708. */
  4709. extern SDL_DECLSPEC double SDLCALL SDL_fabs(double x);
  4710. /**
  4711. * Compute the absolute value of `x`
  4712. *
  4713. * Domain: `-INF <= x <= INF`
  4714. *
  4715. * Range: `0 <= y <= INF`
  4716. *
  4717. * This function operates on single-precision floating point values, use
  4718. * SDL_fabs for double-precision floats.
  4719. *
  4720. * \param x floating point value to use as the magnitude.
  4721. * \returns the absolute value of `x`.
  4722. *
  4723. * \threadsafety It is safe to call this function from any thread.
  4724. *
  4725. * \since This function is available since SDL 3.2.0.
  4726. *
  4727. * \sa SDL_fabs
  4728. */
  4729. extern SDL_DECLSPEC float SDLCALL SDL_fabsf(float x);
  4730. /**
  4731. * Compute the floor of `x`.
  4732. *
  4733. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  4734. * rounded down to the nearest integer.
  4735. *
  4736. * Domain: `-INF <= x <= INF`
  4737. *
  4738. * Range: `-INF <= y <= INF`, y integer
  4739. *
  4740. * This function operates on double-precision floating point values, use
  4741. * SDL_floorf for single-precision floats.
  4742. *
  4743. * \param x floating point value.
  4744. * \returns the floor of `x`.
  4745. *
  4746. * \threadsafety It is safe to call this function from any thread.
  4747. *
  4748. * \since This function is available since SDL 3.2.0.
  4749. *
  4750. * \sa SDL_floorf
  4751. * \sa SDL_ceil
  4752. * \sa SDL_trunc
  4753. * \sa SDL_round
  4754. * \sa SDL_lround
  4755. */
  4756. extern SDL_DECLSPEC double SDLCALL SDL_floor(double x);
  4757. /**
  4758. * Compute the floor of `x`.
  4759. *
  4760. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  4761. * rounded down to the nearest integer.
  4762. *
  4763. * Domain: `-INF <= x <= INF`
  4764. *
  4765. * Range: `-INF <= y <= INF`, y integer
  4766. *
  4767. * This function operates on single-precision floating point values, use
  4768. * SDL_floor for double-precision floats.
  4769. *
  4770. * \param x floating point value.
  4771. * \returns the floor of `x`.
  4772. *
  4773. * \threadsafety It is safe to call this function from any thread.
  4774. *
  4775. * \since This function is available since SDL 3.2.0.
  4776. *
  4777. * \sa SDL_floor
  4778. * \sa SDL_ceilf
  4779. * \sa SDL_truncf
  4780. * \sa SDL_roundf
  4781. * \sa SDL_lroundf
  4782. */
  4783. extern SDL_DECLSPEC float SDLCALL SDL_floorf(float x);
  4784. /**
  4785. * Truncate `x` to an integer.
  4786. *
  4787. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  4788. * the fractional part of `x`, leaving only the integer part.
  4789. *
  4790. * Domain: `-INF <= x <= INF`
  4791. *
  4792. * Range: `-INF <= y <= INF`, y integer
  4793. *
  4794. * This function operates on double-precision floating point values, use
  4795. * SDL_truncf for single-precision floats.
  4796. *
  4797. * \param x floating point value.
  4798. * \returns `x` truncated to an integer.
  4799. *
  4800. * \threadsafety It is safe to call this function from any thread.
  4801. *
  4802. * \since This function is available since SDL 3.2.0.
  4803. *
  4804. * \sa SDL_truncf
  4805. * \sa SDL_fmod
  4806. * \sa SDL_ceil
  4807. * \sa SDL_floor
  4808. * \sa SDL_round
  4809. * \sa SDL_lround
  4810. */
  4811. extern SDL_DECLSPEC double SDLCALL SDL_trunc(double x);
  4812. /**
  4813. * Truncate `x` to an integer.
  4814. *
  4815. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  4816. * the fractional part of `x`, leaving only the integer part.
  4817. *
  4818. * Domain: `-INF <= x <= INF`
  4819. *
  4820. * Range: `-INF <= y <= INF`, y integer
  4821. *
  4822. * This function operates on single-precision floating point values, use
  4823. * SDL_trunc for double-precision floats.
  4824. *
  4825. * \param x floating point value.
  4826. * \returns `x` truncated to an integer.
  4827. *
  4828. * \threadsafety It is safe to call this function from any thread.
  4829. *
  4830. * \since This function is available since SDL 3.2.0.
  4831. *
  4832. * \sa SDL_trunc
  4833. * \sa SDL_fmodf
  4834. * \sa SDL_ceilf
  4835. * \sa SDL_floorf
  4836. * \sa SDL_roundf
  4837. * \sa SDL_lroundf
  4838. */
  4839. extern SDL_DECLSPEC float SDLCALL SDL_truncf(float x);
  4840. /**
  4841. * Return the floating-point remainder of `x / y`
  4842. *
  4843. * Divides `x` by `y`, and returns the remainder.
  4844. *
  4845. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  4846. *
  4847. * Range: `-y <= z <= y`
  4848. *
  4849. * This function operates on double-precision floating point values, use
  4850. * SDL_fmodf for single-precision floats.
  4851. *
  4852. * \param x the numerator.
  4853. * \param y the denominator. Must not be 0.
  4854. * \returns the remainder of `x / y`.
  4855. *
  4856. * \threadsafety It is safe to call this function from any thread.
  4857. *
  4858. * \since This function is available since SDL 3.2.0.
  4859. *
  4860. * \sa SDL_fmodf
  4861. * \sa SDL_modf
  4862. * \sa SDL_trunc
  4863. * \sa SDL_ceil
  4864. * \sa SDL_floor
  4865. * \sa SDL_round
  4866. * \sa SDL_lround
  4867. */
  4868. extern SDL_DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  4869. /**
  4870. * Return the floating-point remainder of `x / y`
  4871. *
  4872. * Divides `x` by `y`, and returns the remainder.
  4873. *
  4874. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  4875. *
  4876. * Range: `-y <= z <= y`
  4877. *
  4878. * This function operates on single-precision floating point values, use
  4879. * SDL_fmod for double-precision floats.
  4880. *
  4881. * \param x the numerator.
  4882. * \param y the denominator. Must not be 0.
  4883. * \returns the remainder of `x / y`.
  4884. *
  4885. * \threadsafety It is safe to call this function from any thread.
  4886. *
  4887. * \since This function is available since SDL 3.2.0.
  4888. *
  4889. * \sa SDL_fmod
  4890. * \sa SDL_truncf
  4891. * \sa SDL_modff
  4892. * \sa SDL_ceilf
  4893. * \sa SDL_floorf
  4894. * \sa SDL_roundf
  4895. * \sa SDL_lroundf
  4896. */
  4897. extern SDL_DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  4898. /**
  4899. * Return whether the value is infinity.
  4900. *
  4901. * \param x double-precision floating point value.
  4902. * \returns non-zero if the value is infinity, 0 otherwise.
  4903. *
  4904. * \threadsafety It is safe to call this function from any thread.
  4905. *
  4906. * \since This function is available since SDL 3.2.0.
  4907. *
  4908. * \sa SDL_isinff
  4909. */
  4910. extern SDL_DECLSPEC int SDLCALL SDL_isinf(double x);
  4911. /**
  4912. * Return whether the value is infinity.
  4913. *
  4914. * \param x floating point value.
  4915. * \returns non-zero if the value is infinity, 0 otherwise.
  4916. *
  4917. * \threadsafety It is safe to call this function from any thread.
  4918. *
  4919. * \since This function is available since SDL 3.2.0.
  4920. *
  4921. * \sa SDL_isinf
  4922. */
  4923. extern SDL_DECLSPEC int SDLCALL SDL_isinff(float x);
  4924. /**
  4925. * Return whether the value is NaN.
  4926. *
  4927. * \param x double-precision floating point value.
  4928. * \returns non-zero if the value is NaN, 0 otherwise.
  4929. *
  4930. * \threadsafety It is safe to call this function from any thread.
  4931. *
  4932. * \since This function is available since SDL 3.2.0.
  4933. *
  4934. * \sa SDL_isnanf
  4935. */
  4936. extern SDL_DECLSPEC int SDLCALL SDL_isnan(double x);
  4937. /**
  4938. * Return whether the value is NaN.
  4939. *
  4940. * \param x floating point value.
  4941. * \returns non-zero if the value is NaN, 0 otherwise.
  4942. *
  4943. * \threadsafety It is safe to call this function from any thread.
  4944. *
  4945. * \since This function is available since SDL 3.2.0.
  4946. *
  4947. * \sa SDL_isnan
  4948. */
  4949. extern SDL_DECLSPEC int SDLCALL SDL_isnanf(float x);
  4950. /**
  4951. * Compute the natural logarithm of `x`.
  4952. *
  4953. * Domain: `0 < x <= INF`
  4954. *
  4955. * Range: `-INF <= y <= INF`
  4956. *
  4957. * It is an error for `x` to be less than or equal to 0.
  4958. *
  4959. * This function operates on double-precision floating point values, use
  4960. * SDL_logf for single-precision floats.
  4961. *
  4962. * This function may use a different approximation across different versions,
  4963. * platforms and configurations. i.e, it can return a different value given
  4964. * the same input on different machines or operating systems, or if SDL is
  4965. * updated.
  4966. *
  4967. * \param x floating point value. Must be greater than 0.
  4968. * \returns the natural logarithm of `x`.
  4969. *
  4970. * \threadsafety It is safe to call this function from any thread.
  4971. *
  4972. * \since This function is available since SDL 3.2.0.
  4973. *
  4974. * \sa SDL_logf
  4975. * \sa SDL_log10
  4976. * \sa SDL_exp
  4977. */
  4978. extern SDL_DECLSPEC double SDLCALL SDL_log(double x);
  4979. /**
  4980. * Compute the natural logarithm of `x`.
  4981. *
  4982. * Domain: `0 < x <= INF`
  4983. *
  4984. * Range: `-INF <= y <= INF`
  4985. *
  4986. * It is an error for `x` to be less than or equal to 0.
  4987. *
  4988. * This function operates on single-precision floating point values, use
  4989. * SDL_log for double-precision floats.
  4990. *
  4991. * This function may use a different approximation across different versions,
  4992. * platforms and configurations. i.e, it can return a different value given
  4993. * the same input on different machines or operating systems, or if SDL is
  4994. * updated.
  4995. *
  4996. * \param x floating point value. Must be greater than 0.
  4997. * \returns the natural logarithm of `x`.
  4998. *
  4999. * \threadsafety It is safe to call this function from any thread.
  5000. *
  5001. * \since This function is available since SDL 3.2.0.
  5002. *
  5003. * \sa SDL_log
  5004. * \sa SDL_expf
  5005. */
  5006. extern SDL_DECLSPEC float SDLCALL SDL_logf(float x);
  5007. /**
  5008. * Compute the base-10 logarithm of `x`.
  5009. *
  5010. * Domain: `0 < x <= INF`
  5011. *
  5012. * Range: `-INF <= y <= INF`
  5013. *
  5014. * It is an error for `x` to be less than or equal to 0.
  5015. *
  5016. * This function operates on double-precision floating point values, use
  5017. * SDL_log10f for single-precision floats.
  5018. *
  5019. * This function may use a different approximation across different versions,
  5020. * platforms and configurations. i.e, it can return a different value given
  5021. * the same input on different machines or operating systems, or if SDL is
  5022. * updated.
  5023. *
  5024. * \param x floating point value. Must be greater than 0.
  5025. * \returns the logarithm of `x`.
  5026. *
  5027. * \threadsafety It is safe to call this function from any thread.
  5028. *
  5029. * \since This function is available since SDL 3.2.0.
  5030. *
  5031. * \sa SDL_log10f
  5032. * \sa SDL_log
  5033. * \sa SDL_pow
  5034. */
  5035. extern SDL_DECLSPEC double SDLCALL SDL_log10(double x);
  5036. /**
  5037. * Compute the base-10 logarithm of `x`.
  5038. *
  5039. * Domain: `0 < x <= INF`
  5040. *
  5041. * Range: `-INF <= y <= INF`
  5042. *
  5043. * It is an error for `x` to be less than or equal to 0.
  5044. *
  5045. * This function operates on single-precision floating point values, use
  5046. * SDL_log10 for double-precision floats.
  5047. *
  5048. * This function may use a different approximation across different versions,
  5049. * platforms and configurations. i.e, it can return a different value given
  5050. * the same input on different machines or operating systems, or if SDL is
  5051. * updated.
  5052. *
  5053. * \param x floating point value. Must be greater than 0.
  5054. * \returns the logarithm of `x`.
  5055. *
  5056. * \threadsafety It is safe to call this function from any thread.
  5057. *
  5058. * \since This function is available since SDL 3.2.0.
  5059. *
  5060. * \sa SDL_log10
  5061. * \sa SDL_logf
  5062. * \sa SDL_powf
  5063. */
  5064. extern SDL_DECLSPEC float SDLCALL SDL_log10f(float x);
  5065. /**
  5066. * Split `x` into integer and fractional parts
  5067. *
  5068. * This function operates on double-precision floating point values, use
  5069. * SDL_modff for single-precision floats.
  5070. *
  5071. * \param x floating point value.
  5072. * \param y output pointer to store the integer part of `x`.
  5073. * \returns the fractional part of `x`.
  5074. *
  5075. * \threadsafety It is safe to call this function from any thread.
  5076. *
  5077. * \since This function is available since SDL 3.2.0.
  5078. *
  5079. * \sa SDL_modff
  5080. * \sa SDL_trunc
  5081. * \sa SDL_fmod
  5082. */
  5083. extern SDL_DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  5084. /**
  5085. * Split `x` into integer and fractional parts
  5086. *
  5087. * This function operates on single-precision floating point values, use
  5088. * SDL_modf for double-precision floats.
  5089. *
  5090. * \param x floating point value.
  5091. * \param y output pointer to store the integer part of `x`.
  5092. * \returns the fractional part of `x`.
  5093. *
  5094. * \threadsafety It is safe to call this function from any thread.
  5095. *
  5096. * \since This function is available since SDL 3.2.0.
  5097. *
  5098. * \sa SDL_modf
  5099. * \sa SDL_truncf
  5100. * \sa SDL_fmodf
  5101. */
  5102. extern SDL_DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  5103. /**
  5104. * Raise `x` to the power `y`
  5105. *
  5106. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  5107. *
  5108. * Range: `-INF <= z <= INF`
  5109. *
  5110. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  5111. * instead.
  5112. *
  5113. * This function operates on double-precision floating point values, use
  5114. * SDL_powf for single-precision floats.
  5115. *
  5116. * This function may use a different approximation across different versions,
  5117. * platforms and configurations. i.e, it can return a different value given
  5118. * the same input on different machines or operating systems, or if SDL is
  5119. * updated.
  5120. *
  5121. * \param x the base.
  5122. * \param y the exponent.
  5123. * \returns `x` raised to the power `y`.
  5124. *
  5125. * \threadsafety It is safe to call this function from any thread.
  5126. *
  5127. * \since This function is available since SDL 3.2.0.
  5128. *
  5129. * \sa SDL_powf
  5130. * \sa SDL_exp
  5131. * \sa SDL_log
  5132. */
  5133. extern SDL_DECLSPEC double SDLCALL SDL_pow(double x, double y);
  5134. /**
  5135. * Raise `x` to the power `y`
  5136. *
  5137. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  5138. *
  5139. * Range: `-INF <= z <= INF`
  5140. *
  5141. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  5142. * instead.
  5143. *
  5144. * This function operates on single-precision floating point values, use
  5145. * SDL_pow for double-precision floats.
  5146. *
  5147. * This function may use a different approximation across different versions,
  5148. * platforms and configurations. i.e, it can return a different value given
  5149. * the same input on different machines or operating systems, or if SDL is
  5150. * updated.
  5151. *
  5152. * \param x the base.
  5153. * \param y the exponent.
  5154. * \returns `x` raised to the power `y`.
  5155. *
  5156. * \threadsafety It is safe to call this function from any thread.
  5157. *
  5158. * \since This function is available since SDL 3.2.0.
  5159. *
  5160. * \sa SDL_pow
  5161. * \sa SDL_expf
  5162. * \sa SDL_logf
  5163. */
  5164. extern SDL_DECLSPEC float SDLCALL SDL_powf(float x, float y);
  5165. /**
  5166. * Round `x` to the nearest integer.
  5167. *
  5168. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5169. * rounded away from zero.
  5170. *
  5171. * Domain: `-INF <= x <= INF`
  5172. *
  5173. * Range: `-INF <= y <= INF`, y integer
  5174. *
  5175. * This function operates on double-precision floating point values, use
  5176. * SDL_roundf for single-precision floats. To get the result as an integer
  5177. * type, use SDL_lround.
  5178. *
  5179. * \param x floating point value.
  5180. * \returns the nearest integer to `x`.
  5181. *
  5182. * \threadsafety It is safe to call this function from any thread.
  5183. *
  5184. * \since This function is available since SDL 3.2.0.
  5185. *
  5186. * \sa SDL_roundf
  5187. * \sa SDL_lround
  5188. * \sa SDL_floor
  5189. * \sa SDL_ceil
  5190. * \sa SDL_trunc
  5191. */
  5192. extern SDL_DECLSPEC double SDLCALL SDL_round(double x);
  5193. /**
  5194. * Round `x` to the nearest integer.
  5195. *
  5196. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5197. * rounded away from zero.
  5198. *
  5199. * Domain: `-INF <= x <= INF`
  5200. *
  5201. * Range: `-INF <= y <= INF`, y integer
  5202. *
  5203. * This function operates on single-precision floating point values, use
  5204. * SDL_round for double-precision floats. To get the result as an integer
  5205. * type, use SDL_lroundf.
  5206. *
  5207. * \param x floating point value.
  5208. * \returns the nearest integer to `x`.
  5209. *
  5210. * \threadsafety It is safe to call this function from any thread.
  5211. *
  5212. * \since This function is available since SDL 3.2.0.
  5213. *
  5214. * \sa SDL_round
  5215. * \sa SDL_lroundf
  5216. * \sa SDL_floorf
  5217. * \sa SDL_ceilf
  5218. * \sa SDL_truncf
  5219. */
  5220. extern SDL_DECLSPEC float SDLCALL SDL_roundf(float x);
  5221. /**
  5222. * Round `x` to the nearest integer representable as a long
  5223. *
  5224. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5225. * rounded away from zero.
  5226. *
  5227. * Domain: `-INF <= x <= INF`
  5228. *
  5229. * Range: `MIN_LONG <= y <= MAX_LONG`
  5230. *
  5231. * This function operates on double-precision floating point values, use
  5232. * SDL_lroundf for single-precision floats. To get the result as a
  5233. * floating-point type, use SDL_round.
  5234. *
  5235. * \param x floating point value.
  5236. * \returns the nearest integer to `x`.
  5237. *
  5238. * \threadsafety It is safe to call this function from any thread.
  5239. *
  5240. * \since This function is available since SDL 3.2.0.
  5241. *
  5242. * \sa SDL_lroundf
  5243. * \sa SDL_round
  5244. * \sa SDL_floor
  5245. * \sa SDL_ceil
  5246. * \sa SDL_trunc
  5247. */
  5248. extern SDL_DECLSPEC long SDLCALL SDL_lround(double x);
  5249. /**
  5250. * Round `x` to the nearest integer representable as a long
  5251. *
  5252. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5253. * rounded away from zero.
  5254. *
  5255. * Domain: `-INF <= x <= INF`
  5256. *
  5257. * Range: `MIN_LONG <= y <= MAX_LONG`
  5258. *
  5259. * This function operates on single-precision floating point values, use
  5260. * SDL_lround for double-precision floats. To get the result as a
  5261. * floating-point type, use SDL_roundf.
  5262. *
  5263. * \param x floating point value.
  5264. * \returns the nearest integer to `x`.
  5265. *
  5266. * \threadsafety It is safe to call this function from any thread.
  5267. *
  5268. * \since This function is available since SDL 3.2.0.
  5269. *
  5270. * \sa SDL_lround
  5271. * \sa SDL_roundf
  5272. * \sa SDL_floorf
  5273. * \sa SDL_ceilf
  5274. * \sa SDL_truncf
  5275. */
  5276. extern SDL_DECLSPEC long SDLCALL SDL_lroundf(float x);
  5277. /**
  5278. * Scale `x` by an integer power of two.
  5279. *
  5280. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  5281. *
  5282. * Domain: `-INF <= x <= INF`, `n` integer
  5283. *
  5284. * Range: `-INF <= y <= INF`
  5285. *
  5286. * This function operates on double-precision floating point values, use
  5287. * SDL_scalbnf for single-precision floats.
  5288. *
  5289. * \param x floating point value to be scaled.
  5290. * \param n integer exponent.
  5291. * \returns `x * 2^n`.
  5292. *
  5293. * \threadsafety It is safe to call this function from any thread.
  5294. *
  5295. * \since This function is available since SDL 3.2.0.
  5296. *
  5297. * \sa SDL_scalbnf
  5298. * \sa SDL_pow
  5299. */
  5300. extern SDL_DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  5301. /**
  5302. * Scale `x` by an integer power of two.
  5303. *
  5304. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  5305. *
  5306. * Domain: `-INF <= x <= INF`, `n` integer
  5307. *
  5308. * Range: `-INF <= y <= INF`
  5309. *
  5310. * This function operates on single-precision floating point values, use
  5311. * SDL_scalbn for double-precision floats.
  5312. *
  5313. * \param x floating point value to be scaled.
  5314. * \param n integer exponent.
  5315. * \returns `x * 2^n`.
  5316. *
  5317. * \threadsafety It is safe to call this function from any thread.
  5318. *
  5319. * \since This function is available since SDL 3.2.0.
  5320. *
  5321. * \sa SDL_scalbn
  5322. * \sa SDL_powf
  5323. */
  5324. extern SDL_DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  5325. /**
  5326. * Compute the sine of `x`.
  5327. *
  5328. * Domain: `-INF <= x <= INF`
  5329. *
  5330. * Range: `-1 <= y <= 1`
  5331. *
  5332. * This function operates on double-precision floating point values, use
  5333. * SDL_sinf for single-precision floats.
  5334. *
  5335. * This function may use a different approximation across different versions,
  5336. * platforms and configurations. i.e, it can return a different value given
  5337. * the same input on different machines or operating systems, or if SDL is
  5338. * updated.
  5339. *
  5340. * \param x floating point value, in radians.
  5341. * \returns sine of `x`.
  5342. *
  5343. * \threadsafety It is safe to call this function from any thread.
  5344. *
  5345. * \since This function is available since SDL 3.2.0.
  5346. *
  5347. * \sa SDL_sinf
  5348. * \sa SDL_asin
  5349. * \sa SDL_cos
  5350. */
  5351. extern SDL_DECLSPEC double SDLCALL SDL_sin(double x);
  5352. /**
  5353. * Compute the sine of `x`.
  5354. *
  5355. * Domain: `-INF <= x <= INF`
  5356. *
  5357. * Range: `-1 <= y <= 1`
  5358. *
  5359. * This function operates on single-precision floating point values, use
  5360. * SDL_sin for double-precision floats.
  5361. *
  5362. * This function may use a different approximation across different versions,
  5363. * platforms and configurations. i.e, it can return a different value given
  5364. * the same input on different machines or operating systems, or if SDL is
  5365. * updated.
  5366. *
  5367. * \param x floating point value, in radians.
  5368. * \returns sine of `x`.
  5369. *
  5370. * \threadsafety It is safe to call this function from any thread.
  5371. *
  5372. * \since This function is available since SDL 3.2.0.
  5373. *
  5374. * \sa SDL_sin
  5375. * \sa SDL_asinf
  5376. * \sa SDL_cosf
  5377. */
  5378. extern SDL_DECLSPEC float SDLCALL SDL_sinf(float x);
  5379. /**
  5380. * Compute the square root of `x`.
  5381. *
  5382. * Domain: `0 <= x <= INF`
  5383. *
  5384. * Range: `0 <= y <= INF`
  5385. *
  5386. * This function operates on double-precision floating point values, use
  5387. * SDL_sqrtf for single-precision floats.
  5388. *
  5389. * This function may use a different approximation across different versions,
  5390. * platforms and configurations. i.e, it can return a different value given
  5391. * the same input on different machines or operating systems, or if SDL is
  5392. * updated.
  5393. *
  5394. * \param x floating point value. Must be greater than or equal to 0.
  5395. * \returns square root of `x`.
  5396. *
  5397. * \threadsafety It is safe to call this function from any thread.
  5398. *
  5399. * \since This function is available since SDL 3.2.0.
  5400. *
  5401. * \sa SDL_sqrtf
  5402. */
  5403. extern SDL_DECLSPEC double SDLCALL SDL_sqrt(double x);
  5404. /**
  5405. * Compute the square root of `x`.
  5406. *
  5407. * Domain: `0 <= x <= INF`
  5408. *
  5409. * Range: `0 <= y <= INF`
  5410. *
  5411. * This function operates on single-precision floating point values, use
  5412. * SDL_sqrt for double-precision floats.
  5413. *
  5414. * This function may use a different approximation across different versions,
  5415. * platforms and configurations. i.e, it can return a different value given
  5416. * the same input on different machines or operating systems, or if SDL is
  5417. * updated.
  5418. *
  5419. * \param x floating point value. Must be greater than or equal to 0.
  5420. * \returns square root of `x`.
  5421. *
  5422. * \threadsafety It is safe to call this function from any thread.
  5423. *
  5424. * \since This function is available since SDL 3.2.0.
  5425. *
  5426. * \sa SDL_sqrt
  5427. */
  5428. extern SDL_DECLSPEC float SDLCALL SDL_sqrtf(float x);
  5429. /**
  5430. * Compute the tangent of `x`.
  5431. *
  5432. * Domain: `-INF <= x <= INF`
  5433. *
  5434. * Range: `-INF <= y <= INF`
  5435. *
  5436. * This function operates on double-precision floating point values, use
  5437. * SDL_tanf for single-precision floats.
  5438. *
  5439. * This function may use a different approximation across different versions,
  5440. * platforms and configurations. i.e, it can return a different value given
  5441. * the same input on different machines or operating systems, or if SDL is
  5442. * updated.
  5443. *
  5444. * \param x floating point value, in radians.
  5445. * \returns tangent of `x`.
  5446. *
  5447. * \threadsafety It is safe to call this function from any thread.
  5448. *
  5449. * \since This function is available since SDL 3.2.0.
  5450. *
  5451. * \sa SDL_tanf
  5452. * \sa SDL_sin
  5453. * \sa SDL_cos
  5454. * \sa SDL_atan
  5455. * \sa SDL_atan2
  5456. */
  5457. extern SDL_DECLSPEC double SDLCALL SDL_tan(double x);
  5458. /**
  5459. * Compute the tangent of `x`.
  5460. *
  5461. * Domain: `-INF <= x <= INF`
  5462. *
  5463. * Range: `-INF <= y <= INF`
  5464. *
  5465. * This function operates on single-precision floating point values, use
  5466. * SDL_tan for double-precision floats.
  5467. *
  5468. * This function may use a different approximation across different versions,
  5469. * platforms and configurations. i.e, it can return a different value given
  5470. * the same input on different machines or operating systems, or if SDL is
  5471. * updated.
  5472. *
  5473. * \param x floating point value, in radians.
  5474. * \returns tangent of `x`.
  5475. *
  5476. * \threadsafety It is safe to call this function from any thread.
  5477. *
  5478. * \since This function is available since SDL 3.2.0.
  5479. *
  5480. * \sa SDL_tan
  5481. * \sa SDL_sinf
  5482. * \sa SDL_cosf
  5483. * \sa SDL_atanf
  5484. * \sa SDL_atan2f
  5485. */
  5486. extern SDL_DECLSPEC float SDLCALL SDL_tanf(float x);
  5487. /**
  5488. * An opaque handle representing string encoding conversion state.
  5489. *
  5490. * \since This datatype is available since SDL 3.2.0.
  5491. *
  5492. * \sa SDL_iconv_open
  5493. */
  5494. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  5495. /**
  5496. * This function allocates a context for the specified character set
  5497. * conversion.
  5498. *
  5499. * \param tocode The target character encoding, must not be NULL.
  5500. * \param fromcode The source character encoding, must not be NULL.
  5501. * \returns a handle that must be freed with SDL_iconv_close, or
  5502. * SDL_ICONV_ERROR on failure.
  5503. *
  5504. * \since This function is available since SDL 3.2.0.
  5505. *
  5506. * \sa SDL_iconv
  5507. * \sa SDL_iconv_close
  5508. * \sa SDL_iconv_string
  5509. */
  5510. extern SDL_DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  5511. const char *fromcode);
  5512. /**
  5513. * This function frees a context used for character set conversion.
  5514. *
  5515. * \param cd The character set conversion handle.
  5516. * \returns 0 on success, or -1 on failure.
  5517. *
  5518. * \since This function is available since SDL 3.2.0.
  5519. *
  5520. * \sa SDL_iconv
  5521. * \sa SDL_iconv_open
  5522. * \sa SDL_iconv_string
  5523. */
  5524. extern SDL_DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  5525. /**
  5526. * This function converts text between encodings, reading from and writing to
  5527. * a buffer.
  5528. *
  5529. * It returns the number of successful conversions on success. On error,
  5530. * SDL_ICONV_E2BIG is returned when the output buffer is too small, or
  5531. * SDL_ICONV_EILSEQ is returned when an invalid input sequence is encountered,
  5532. * or SDL_ICONV_EINVAL is returned when an incomplete input sequence is
  5533. * encountered.
  5534. *
  5535. * On exit:
  5536. *
  5537. * - inbuf will point to the beginning of the next multibyte sequence. On
  5538. * error, this is the location of the problematic input sequence. On
  5539. * success, this is the end of the input sequence.
  5540. * - inbytesleft will be set to the number of bytes left to convert, which
  5541. * will be 0 on success.
  5542. * - outbuf will point to the location where to store the next output byte.
  5543. * - outbytesleft will be set to the number of bytes left in the output
  5544. * buffer.
  5545. *
  5546. * \param cd The character set conversion context, created in
  5547. * SDL_iconv_open().
  5548. * \param inbuf Address of variable that points to the first character of the
  5549. * input sequence.
  5550. * \param inbytesleft The number of bytes in the input buffer.
  5551. * \param outbuf Address of variable that points to the output buffer.
  5552. * \param outbytesleft The number of bytes in the output buffer.
  5553. * \returns the number of conversions on success, or a negative error code.
  5554. *
  5555. * \since This function is available since SDL 3.2.0.
  5556. *
  5557. * \sa SDL_iconv_open
  5558. * \sa SDL_iconv_close
  5559. * \sa SDL_iconv_string
  5560. */
  5561. extern SDL_DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  5562. size_t *inbytesleft, char **outbuf,
  5563. size_t *outbytesleft);
  5564. #define SDL_ICONV_ERROR (size_t)-1 /**< Generic error. Check SDL_GetError()? */
  5565. #define SDL_ICONV_E2BIG (size_t)-2 /**< Output buffer was too small. */
  5566. #define SDL_ICONV_EILSEQ (size_t)-3 /**< Invalid input sequence was encountered. */
  5567. #define SDL_ICONV_EINVAL (size_t)-4 /**< Incomplete input sequence was encountered. */
  5568. /**
  5569. * Helper function to convert a string's encoding in one call.
  5570. *
  5571. * This function converts a buffer or string between encodings in one pass.
  5572. *
  5573. * The string does not need to be NULL-terminated; this function operates on
  5574. * the number of bytes specified in `inbytesleft` whether there is a NULL
  5575. * character anywhere in the buffer.
  5576. *
  5577. * The returned string is owned by the caller, and should be passed to
  5578. * SDL_free when no longer needed.
  5579. *
  5580. * \param tocode the character encoding of the output string. Examples are
  5581. * "UTF-8", "UCS-4", etc.
  5582. * \param fromcode the character encoding of data in `inbuf`.
  5583. * \param inbuf the string to convert to a different encoding.
  5584. * \param inbytesleft the size of the input string _in bytes_.
  5585. * \returns a new string, converted to the new encoding, or NULL on error.
  5586. *
  5587. * \since This function is available since SDL 3.2.0.
  5588. *
  5589. * \sa SDL_iconv_open
  5590. * \sa SDL_iconv_close
  5591. * \sa SDL_iconv
  5592. */
  5593. extern SDL_DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode,
  5594. const char *fromcode,
  5595. const char *inbuf,
  5596. size_t inbytesleft);
  5597. /* Some helper macros for common SDL_iconv_string cases... */
  5598. /**
  5599. * Convert a UTF-8 string to the current locale's character encoding.
  5600. *
  5601. * This is a helper macro that might be more clear than calling
  5602. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5603. * do not use an expression with side-effects here.
  5604. *
  5605. * \param S the string to convert.
  5606. * \returns a new string, converted to the new encoding, or NULL on error.
  5607. *
  5608. * \since This macro is available since SDL 3.2.0.
  5609. */
  5610. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  5611. /**
  5612. * Convert a UTF-8 string to UCS-2.
  5613. *
  5614. * This is a helper macro that might be more clear than calling
  5615. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5616. * do not use an expression with side-effects here.
  5617. *
  5618. * \param S the string to convert.
  5619. * \returns a new string, converted to the new encoding, or NULL on error.
  5620. *
  5621. * \since This macro is available since SDL 3.2.0.
  5622. */
  5623. #define SDL_iconv_utf8_ucs2(S) SDL_reinterpret_cast(Uint16 *, SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1))
  5624. /**
  5625. * Convert a UTF-8 string to UCS-4.
  5626. *
  5627. * This is a helper macro that might be more clear than calling
  5628. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5629. * do not use an expression with side-effects here.
  5630. *
  5631. * \param S the string to convert.
  5632. * \returns a new string, converted to the new encoding, or NULL on error.
  5633. *
  5634. * \since This macro is available since SDL 3.2.0.
  5635. */
  5636. #define SDL_iconv_utf8_ucs4(S) SDL_reinterpret_cast(Uint32 *, SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1))
  5637. /**
  5638. * Convert a wchar_t string to UTF-8.
  5639. *
  5640. * This is a helper macro that might be more clear than calling
  5641. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5642. * do not use an expression with side-effects here.
  5643. *
  5644. * \param S the string to convert.
  5645. * \returns a new string, converted to the new encoding, or NULL on error.
  5646. *
  5647. * \since This macro is available since SDL 3.2.0.
  5648. */
  5649. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", SDL_reinterpret_cast(const char *, S), (SDL_wcslen(S)+1)*sizeof(wchar_t))
  5650. /* force builds using Clang's static analysis tools to use literal C runtime
  5651. here, since there are possibly tests that are ineffective otherwise. */
  5652. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  5653. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  5654. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  5655. size_t strlcpy(char *dst, const char *src, size_t size);
  5656. #endif
  5657. /* The analyzer knows about strlcat even when the system doesn't provide it */
  5658. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  5659. size_t strlcat(char *dst, const char *src, size_t size);
  5660. #endif
  5661. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  5662. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  5663. #endif
  5664. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  5665. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  5666. #endif
  5667. #ifndef _WIN32
  5668. /* strdup is not ANSI but POSIX, and its prototype might be hidden... */
  5669. /* not for windows: might conflict with string.h where strdup may have
  5670. * dllimport attribute: https://github.com/libsdl-org/SDL/issues/12948 */
  5671. char *strdup(const char *str);
  5672. #endif
  5673. /* Starting LLVM 16, the analyser errors out if these functions do not have
  5674. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  5675. #include <stdio.h>
  5676. #include <stdlib.h>
  5677. #define SDL_malloc malloc
  5678. #define SDL_calloc calloc
  5679. #define SDL_realloc realloc
  5680. #define SDL_free free
  5681. #ifndef SDL_memcpy
  5682. #define SDL_memcpy memcpy
  5683. #endif
  5684. #ifndef SDL_memmove
  5685. #define SDL_memmove memmove
  5686. #endif
  5687. #ifndef SDL_memset
  5688. #define SDL_memset memset
  5689. #endif
  5690. #define SDL_memcmp memcmp
  5691. #define SDL_strlcpy strlcpy
  5692. #define SDL_strlcat strlcat
  5693. #define SDL_strlen strlen
  5694. #define SDL_wcslen wcslen
  5695. #define SDL_wcslcpy wcslcpy
  5696. #define SDL_wcslcat wcslcat
  5697. #define SDL_strdup strdup
  5698. #define SDL_wcsdup wcsdup
  5699. #define SDL_strchr strchr
  5700. #define SDL_strrchr strrchr
  5701. #define SDL_strstr strstr
  5702. #define SDL_wcsstr wcsstr
  5703. #define SDL_strtok_r strtok_r
  5704. #define SDL_strcmp strcmp
  5705. #define SDL_wcscmp wcscmp
  5706. #define SDL_strncmp strncmp
  5707. #define SDL_wcsncmp wcsncmp
  5708. #define SDL_strcasecmp strcasecmp
  5709. #define SDL_strncasecmp strncasecmp
  5710. #define SDL_strpbrk strpbrk
  5711. #define SDL_sscanf sscanf
  5712. #define SDL_vsscanf vsscanf
  5713. #define SDL_snprintf snprintf
  5714. #define SDL_vsnprintf vsnprintf
  5715. #endif
  5716. /**
  5717. * Multiply two integers, checking for overflow.
  5718. *
  5719. * If `a * b` would overflow, return false.
  5720. *
  5721. * Otherwise store `a * b` via ret and return true.
  5722. *
  5723. * \param a the multiplicand.
  5724. * \param b the multiplier.
  5725. * \param ret on non-overflow output, stores the multiplication result, may
  5726. * not be NULL.
  5727. * \returns false on overflow, true if result is multiplied without overflow.
  5728. *
  5729. * \threadsafety It is safe to call this function from any thread.
  5730. *
  5731. * \since This function is available since SDL 3.2.0.
  5732. */
  5733. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
  5734. {
  5735. if (a != 0 && b > SDL_SIZE_MAX / a) {
  5736. return false;
  5737. }
  5738. *ret = a * b;
  5739. return true;
  5740. }
  5741. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  5742. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  5743. /* This needs to be wrapped in an inline rather than being a direct #define,
  5744. * because __builtin_mul_overflow() is type-generic, but we want to be
  5745. * consistent about interpreting a and b as size_t. */
  5746. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  5747. {
  5748. return (__builtin_mul_overflow(a, b, ret) == 0);
  5749. }
  5750. #define SDL_size_mul_check_overflow(a, b, ret) SDL_size_mul_check_overflow_builtin(a, b, ret)
  5751. #endif
  5752. #endif
  5753. /**
  5754. * Add two integers, checking for overflow.
  5755. *
  5756. * If `a + b` would overflow, return false.
  5757. *
  5758. * Otherwise store `a + b` via ret and return true.
  5759. *
  5760. * \param a the first addend.
  5761. * \param b the second addend.
  5762. * \param ret on non-overflow output, stores the addition result, may not be
  5763. * NULL.
  5764. * \returns false on overflow, true if result is added without overflow.
  5765. *
  5766. * \threadsafety It is safe to call this function from any thread.
  5767. *
  5768. * \since This function is available since SDL 3.2.0.
  5769. */
  5770. SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
  5771. {
  5772. if (b > SDL_SIZE_MAX - a) {
  5773. return false;
  5774. }
  5775. *ret = a + b;
  5776. return true;
  5777. }
  5778. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  5779. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  5780. /* This needs to be wrapped in an inline rather than being a direct #define,
  5781. * the same as the call to __builtin_mul_overflow() above. */
  5782. SDL_FORCE_INLINE bool SDL_size_add_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  5783. {
  5784. return (__builtin_add_overflow(a, b, ret) == 0);
  5785. }
  5786. #define SDL_size_add_check_overflow(a, b, ret) SDL_size_add_check_overflow_builtin(a, b, ret)
  5787. #endif
  5788. #endif
  5789. /* This is a generic function pointer which should be cast to the type you expect */
  5790. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  5791. /**
  5792. * A generic function pointer.
  5793. *
  5794. * In theory, generic function pointers should use this, instead of `void *`,
  5795. * since some platforms could treat code addresses differently than data
  5796. * addresses. Although in current times no popular platforms make this
  5797. * distinction, it is more correct and portable to use the correct type for a
  5798. * generic pointer.
  5799. *
  5800. * If for some reason you need to force this typedef to be an actual `void *`,
  5801. * perhaps to work around a compiler or existing code, you can define
  5802. * `SDL_FUNCTION_POINTER_IS_VOID_POINTER` before including any SDL headers.
  5803. *
  5804. * \since This datatype is available since SDL 3.2.0.
  5805. */
  5806. typedef void (*SDL_FunctionPointer)(void);
  5807. #elif defined(SDL_FUNCTION_POINTER_IS_VOID_POINTER)
  5808. typedef void *SDL_FunctionPointer;
  5809. #else
  5810. typedef void (*SDL_FunctionPointer)(void);
  5811. #endif
  5812. /* Ends C function definitions when using C++ */
  5813. #ifdef __cplusplus
  5814. }
  5815. #endif
  5816. #include <SDL3/SDL_close_code.h>
  5817. #endif /* SDL_stdinc_h_ */