PJSIP UA分析(1)——内存管理
1 池工厂
1 /**
2 * @} // PJ_POOL_FACTORY
3 */
4
5 /* **************************************************************************/
6
7 /**
8 * @defgroup PJ_CACHING_POOL Caching Pool Factory
9 * @ingroup PJ_POOL_GROUP
10 * @brief
11 * Caching pool 是池工厂的一个简单实现,能够重用内存创建pool。应用程序
13 * 定义了池工厂能够容纳的最大内存量,还定义了一个pool被释放之后,是
14 * 销毁这个pool还是保留它用于未来使用。
15 * 如果Caching pool中的内存总量仍然在限定的量之内,那么这个工厂会保留16 * 这个pool,否则,将会被销毁,将占用的内存返回给系统。
17 *
18 *
19 * @{
20 */
21
22 /**
23 * Number of unique sizes, to be used as index to the free list.
24 * Each pool in the free list is organized by it's size.
25 */
26 #define PJ_CACHING_POOL_ARRAY_SIZE 16
27
28 /**
29 * Declaration for caching pool. Application doesn't normally need to
30 * care about the contents of this struct, it is only provided here because
31 * application need to define an instance of this struct (we can not allocate
32 * the struct from a pool since there is no pool factory yet!).
33 */
34 struct pj_caching_pool
35 {
36 /** Pool factory interface, must be declared first. */
37 pj_pool_factory factory;
38
39 /** Current factory's capacity, i.e. number of bytes that are allocated
40 * and available for application in this factory. The factory's
41 * capacity represents the size of all pools kept by this factory
42 * in it's free list, which will be returned to application when it
43 * requests to create a new pool.
44 */
45 pj_size_t capacity; /*分配给应用程序以及应用程序能够使用的总的字节数*/
46
47 /** Maximum size that can be held by this factory. Once the capacity
48 * has exceeded @a max_capacity, further #pj_pool_release() will
49 * flush the pool. If the capacity is still below the @a max_capacity,
50 * #pj_pool_release() will save the pool to the factory's free list.
51 */
52 pj_size_t max_capacity;/*一旦capacity超过了这个值,pj_pool_release将清空这个factory?*/
53
54 /**
55 * Number of pools currently held by applications. This number gets
56 * incremented everytime #pj_pool_create() is called, and gets
57 * decremented when #pj_pool_release() is called.
58 */
59 pj_size_t used_count;
60
61 /**
62 * Total size of memory currently used by application.
63 */
64 pj_size_t used_size;
65
66 /**
67 * The maximum size of memory used by application throughout the life
68 * of the caching pool.
69 */
70 pj_size_t peak_used_size;
71
72 /**
73 * Lists of pools in the cache, indexed by pool size.
74 */
75 pj_list free_list[PJ_CACHING_POOL_ARRAY_SIZE];
76
77 /**
78 * List of pools currently allocated by applications.
79 */
80 pj_list used_list;
81
82 /**
83 * Internal pool.
84 */
85 char pool_buf[256 * (sizeof(long) / 4)];
86
87 /**
88 * Mutex.
89 */
90 pj_lock_t *lock;
91 };
2 池
1 /**
2 * This structure describes the memory pool. Only implementors of pool factory
3 * need to care about the contents of this structure.
4 */
5 struct pj_pool_t
6 {
7 PJ_DECL_LIST_MEMBER(struct pj_pool_t); /**< Standard list elements. */
8
9 /** Pool name */
10 char obj_name[PJ_MAX_OBJ_NAME];
11
12 /** Pool factory. */
13 pj_pool_factory *factory;
14
15 /** Data put by factory */
16 void *factory_data;
17
18 /** Current capacity allocated by the pool. */
19 pj_size_t capacity;
20
21 /** Size of memory block to be allocated when the pool runs out of memory */
22 pj_size_t increment_size;
23
24 /** List of memory blocks allcoated by the pool. */
25 pj_pool_block block_list;
26
27 /** The callback to be called when the pool is unable to allocate memory. */
28 pj_pool_callback *callback;
29
30 };
31