Slab Memory Management Source code Analysis

Source: Internet
Author: User
Tags data structures

Learn the principle of computer, it is best to practice or see the source code written by the master, to a certain extent, no longer feel the abstraction of the principle. About slab Some principle material, may download here or to the website to have more information and the material. The slab memory management mechanism has been widely used and it is not difficult to find open source code that uses slab to manage memory, such as memory management in some OS cores. Since to analyze and understand slab, it is best to choose the complexity and the amount of code is not too large, where I selected glib-2.12.9 gslice.c implementation of the slab mechanism related code as the Analysis object. Note The glib library is for user-level rather than OS kernel level.

In gslice.c, three kinds of memory allocation mechanisms are implemented: one is slab, the other is more suitable for magazine than Slab, and three is to use pure malloc. This article is only for slab related source code analysis.

In the analysis of code, mainly from the following aspects: first from the distributor of the overall data structure of the relationship to describe the second is to see how the allocator allocator initialization; The next step is to analyze how the allocator allocates and reclaims memory (chunk).

Allocator distributor Overall Structure:

Let's look at some important data structures and variables:

The 
 ...//dot represents the omitted code 130 typedef struct _CHUNKLINK chunklink;;
  131 typedef struct _SLABINFO slabinfo;
  132 typedef struct _CACHEDMAGAZINE cachedmagazine;
  This structure also indicates that the minimum value of a chunk is two pointer size struct _chunklink {134 Chunklink *next; 135 Chunklink *data;
  This field is not used in slab 136};
  137 struct _slabinfo {138 Chunklink *chunks;
  139 Guint n_allocated;
  140 Slabinfo *next, *prev;
  141};
  ............. The typedef struct {151 Gboolean always_malloc;/is true to use pure malloc 152 Gboolean the bypass_magazines;//To TRUE indicates the use of SL
  AB 153 gsize Working_set_msecs;
  154 Guint Color_increment;
  Sliceconfig};
  156 typedef struct {157/* const after initialization * * 158 gsize min_page_size, max_page_size;
  159 Sliceconfig config;
  ...............
  Slab Allocator * * * 169 Gmutex *slab_mutex; Slabinfo pointer array with a maximum value of max_slab_index (allocator) 170 slabinfo **slab_stack;
  /* Array of Max_slab_index (allocator) */171 guint COLOR_ACCU; 172} AlloCator; .../This variable if 0 indicates that allocator has not been initialized, if the number greater than 0 indicates allocator//has been initialized, and its value is the size of the system page value 189 static Gsize Sys_page_size =
  0; 190 static Allocator Allocator[1] = {0,},};   Memory allocator///configuration selection in variable Slice_config with that allocation mechanism, it is known by default//is to use magazine allocation mechanism 191 static Sliceconfig Slice_config = {192 False,/* Always_malloc/193 false,/* Bypass_magazines////Set this value to true to actually use slab 194 * 1000, * workin
  G_set_msecs * * 195 1,/* color increment, ALT:0X7FFFFFFF * * 196}; ... 
.....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.