Memory management - common functions for allocating memory (allocating fixed-size objects: slab)

The method here is: create a frequently used or dedicated cache in memory: this cache is divided into several small caches of the same size, and each application for release is a small cache, the release mentioned here, In fact, it is not released, but returned, which is equivalent to the special cache in the slab system.

makefile

  1 LINUX_SRC :=/home/liuye/tiny4412/FriendlyARM_kernel/linux- 3.5 
  2 #obj-m += module.o #Generate module.ko but we can write module.o
   3 #module-objs= param.o #here Write the file to be compiled
   4 obj-m += mmc_group.o #This is a single-file compilation: the above two lines are multi-file compilation, which is more convenient
   5  
  6 all:
   7          make -C $(LINUX_SRC) M= `pwd` modules
   8 clean:
   9          make -C $(LINUX_SRC) M=`pwd` modules clean

mmc_group.c

  1 #include <linux/init.h>
   2 #include <linux/sched.h>
   3 #include <linux/module.h>
   4 #include <linux/mm.h>
   5 #include <linux/highmem.h>
   6 #include <linux/vmalloc.h>
   7 #include <linux/dma-mapping.h>
   8 #include <linux/slab.h>
   9  // find 
 10  // The kernel code works in two environments: 
 11  / /       1/Interrupt context does not allow sleep 
 12  //       2/Process context allows sleep 
 13  
 14  /*
 15 Many page structures stored in the kernel are consecutive: page+1 is the address of the next page
 16 page
 17 page
 18 page
 19 page
 20 page
 21 page
 22 */ 
 23  // In the kernel program, the requested memory must not have memory leaks. If it is used up, the kernel will crash. 
 24  // In the application program, if the requested memory is not used, more and more 
 25  struct nrf_st 26
  {
  27 int no;
  28 int flag;
  29 char msg[ 16 ];
  30 }
  ; 31 void ctor( void * data )
  32 {
  33 34 }
  35 static __init int test_init( void )
  36                             
  {
  37          struct kmem_cache * kmem;
  38          struct nrf_st * nrf ;
  39          // my_cache: the name can be reflected in /proc/slabinfo 
 40          // The second parameter: the size of each structure, not the size of the total memory requested . 
 41          // The fourth parameter: according to the hardware cache line 
 42          // ctor: will be called when each object in the cache is re-allocated, and the pointer of the allocated object will be passed to this function; 
 43          kmem = kmem_cache_create ( " my_cache " , sizeof ( struct nrf_st), 0 ,SLAB_HWCACHE_ALIGN,ctor);
  44  
 45          // Get the object in the cache 
 46          nrf = kmem_cache_alloc(kmem,GFP_KERNEL);
 47 
 48         //释放对象
 49         kmem_cache_free(kmem,nrf);
 50 
 51         //释放
 52         kmem_cache_destroy(kmem);
 53         return 0;
 54 }
 55 static __exit void test_exit(void)
 56 {
 57         printk("mmc exit!\n");
 58 }
 59 
 60 module_init(test_init);
 61 module_exit(test_exit);
 62 MODULE_LICENSE("GPL");

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117498&siteId=291194637
Recommended