A custom malloc implementation in C explained - GitHub
文章推薦指數: 80 %
A custom malloc implementation in C explained. Contribute to miguelperes/custom-malloc development by creating an account on GitHub. Skiptocontent {{message}} miguelperes / custom-malloc Public Notifications Fork 2 Star 24 AcustommallocimplementationinCexplained 24 stars 2 forks Star Notifications Code Issues 1 Pullrequests 0 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights miguelperes/custom-malloc Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. master Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default 1 branch 0 tags Code Latestcommit miguelperes Mergepullrequest#2fromdolev146/master … a07735c Apr26,2022 Mergepullrequest#2fromdolev146/master addingmakefileandtest a07735c Gitstats 11 commits Files Permalink Failedtoloadlatestcommitinformation. Type Name Latestcommitmessage Committime .gitignore addingmakefileandtest Apr26,2022 README.md UpdateREADME.md Sep29,2017 main.c addingmakefileandtest Apr26,2022 makefile addingmakefileandtest Apr26,2022 mymemory.c addingmakefileandtest Apr26,2022 mymemory.h Updatefilestophase2:optimization. Nov30,2016 Viewcode CustomMemoryManagementimplementation References Howthisimplementationworks 1.DataStructure 2.Algorithm Optimization README.md CustomMemoryManagementimplementation AcustommallocimplementationinC,madeforCSC369H(OperatingSystems)-Fall2014-UniversityofToronto References Usefulreadingstobetterunderstandhowmemorymanagementworks: MemoryAPI FreeSpaceManagement Howdomalloc()andfree()work(StackOverflow) Hoard:AScalableMemoryAllocatorforMultithreadedApplications(Bergeretal.) DougLea'sarticleonoptimizingmalloc Howthisimplementationworks 1.DataStructure Thedatastructureislocatedbeforeeachchunkofmemory,tokeeptrackofitsstatus(metadata).Throughthelinkedlistofthisdatastructure,itispossibletoaccesstheaddressofanymemoryblockinthelist,itssize,availability,thenextandpreviouschunksofadjacentmemoryandtheendoftheblock.Withthisattributes,it’spossibletomanageandmanipulatethememoryusingthealgorithmquicklydescribedbelow. 2.Algorithm Itusesthe“firstfit”strategy.Thealgorithmsearchthelistfromthebeginningtoend,untilitfindsthefirstbigenough(greaterorequalthesizerequested)chunkofmemory(splittingitintwo,incaseit’sbiggerthanneeded),andfinallyreturnitsadresstotheuser.Ifthereisnomorespaceintheheap,thealgorithmthenextendsit,usingthesbrksystemcall.Tofreememory,thealgorithmchangesthe“available”stateofthechunk’smetadata(struct),releasingittofurtherallocation.Afterthat,thefreedchunkismergedwiththepreviousblock(ifthisoneisalsofreed),thisisusefultoavoidmemoryfragmentationinsmallsizedblocks(itcanhappensaftersomesplittings). Optimization Spaceoptimization:nowmyfreefunctionwillmergethetwoadjacentchunks(adjacentstothechunkbeingfreedatthemoment),iftheyareavailable(notallocated).Bydoingthis,I'mpreventingmemoryfragmentation,andtheseblockswillbeeasilyreallocatedinthenextmymalloc. Sbrkcalls:toavoidcallingsbrk()allthetime,whentheuserfirstcallsmymalloc(orwhentheheapneedextension),mymallocwillallocatemorememorythantheamountasked.Thisway,itwilltakelongerbeforetheheapneedtobeextendedagain,andlesstimessbrk()willbeinvoked. About AcustommallocimplementationinCexplained Topics c algorithm tutorial optimization malloc learn memory-management memory-allocation sbrk Resources Readme Stars 24 stars Watchers 1 watching Forks 2 forks Releases Noreleasespublished Packages0 Nopackagespublished Contributors2 miguelperes MiguelPéres dolev146 DolevDublon Languages C 95.7% Makefile 4.3% Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1Implementing malloc and free - Medium
The following code is the malloc function implementation. Our implementation keeps a doubly linke...
- 2c - How is malloc() implemented internally? - Stack Overflow
When one calls malloc , memory is taken from the large heap cell, which is returned by malloc . T...
- 3A custom malloc implementation in C explained - GitHub
A custom malloc implementation in C explained. Contribute to miguelperes/custom-malloc developmen...
- 4C Language: malloc function (Allocate Memory Block)
- 5How to use "malloc" in C - Educative IO