5.7.5. Allocating Memory
- simplest form
kmalloc(size, flags)
- flags see
linux/gfp.h
- may sleep
GFP_KERNEL
, or notGFP_ATOMIC
- fast, continuous, doesn't clear (security? ->
kzalloc
) - free with
kfree(ptr)
- flags see
- more memory needed? allocate pages:
-
get_zeroed_page(flags)
-
__get_free_page(flags) /* non zeroed */
-
__get_free_pages(flags, order)
- allocate 2^order pages
- calculate oder from size:
get_order(size)
- freeing
-
free_page(ptr)
-
free_pages(ptr, order)
-
- advantages: slightly faster and avoids fragementation
-
-
vmalloc
/vfree
- above functions allocate physically consecutive memory
-
vmalloc
allocated memory is only virtually consecutive - cons:
- slower because page tables need to be setup
- not usable from outside of CPU (DMA)
- pro:
- large allocation less likely to fail due to fragementation
- -> ok for e.g. large software only buffer
- Optimizations (premature opt ...)
- Situation: driver allocates many objects of the same size over and over again
- lookaside cache (see slab cache in DocBook "Memory Management in Linux")
- nice: cache usage information in /proc/slabinfo
- Sitation: memory allocation is not allowed to fail
- mempool can be used for reserving memory in emergencies
- Usually bad: memory is idle. Better deal with allocation failures
- Situation: driver allocates many objects of the same size over and over again
5.7.4.4. Other locking techniques | 1. Denx Training Topics | 5.7.6. How to delay execution | |||
Prev | Home | Next | |||