Pjsip 初步分析

pjsip是实现sip的协议栈。相对于其它的sip协议栈,pjsip实现相对稳定且跨平台。首先分析pjlib基本库,其这些库的实现都比较精湛,是深入学习编程的好材料。并且设计了很多的测试用例,对函数的接口进行测试。

pjlib库包含操作系统的抽象,具体是可移植性的线程,锁,信号量,事件,高精度时间等;网络I/O,具体为socket抽象,网络地址解析,select I/O复用;时间管理;多种数据结构,字符串的操作,数组帮助,哈希表,链表,红黑树等;内存池管理的实现较为精湛。

如红黑树的代码实现:

rbtree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * @file rbtree.h
 * @brief Red/Black Tree
 */

#include <pj/types.h>

PJ_BEGIN_DECL

/**
 * @defgroup PJ_RBTREE Red/Black Balanced Tree
 * @ingroup PJ_DS
 * @brief
 * Red/Black tree is the variant of balanced tree, where the search, insert, 
 * and delete operation is \b guaranteed to take at most \a O( lg(n) ).
 * @{
 */
/**
 * Color type for Red-Black tree.
 */
typedef enum pj_rbcolor_t
{
    PJ_RBCOLOR_BLACK,
    PJ_RBCOLOR_RED
} pj_rbcolor_t;

/**
 * The type of the node of the R/B Tree.
 */
typedef struct pj_rbtree_node
{
    /** Pointers to the node's parent, and left and right siblings. */
    struct pj_rbtree_node *parent, *left, *right;

    /** Key associated with the node. */
    const void *key;

    /** User data associated with the node. */
    void *user_data;

    /** The R/B Tree node color. */
    pj_rbcolor_t color;

} pj_rbtree_node;


/**
 * The type of function use to compare key value of tree node.
 * @return
 *  0 if the keys are equal
 * <0 if key1 is lower than key2
 * >0 if key1 is greater than key2.
 */
typedef int pj_rbtree_comp(const void *key1, const void *key2);


/**
 * Declaration of a red-black tree. All elements in the tree must have UNIQUE
 * key.
 * A red black tree always maintains the balance of the tree, so that the
 * tree height will not be greater than lg(N). Insert, search, and delete
 * operation will take lg(N) on the worst case. But for insert and delete,
 * there is additional time needed to maintain the balance of the tree.
 */
typedef struct pj_rbtree
{
    pj_rbtree_node null_node;   /**< Constant to indicate NULL node.    */
    pj_rbtree_node *null;       /**< Constant to indicate NULL node.    */
    pj_rbtree_node *root;       /**< Root tree node.                    */
    unsigned size;              /**< Number of elements in the tree.    */
    pj_rbtree_comp *comp;       /**< Key comparison function.           */
} pj_rbtree;


/**
 * Guidance on how much memory required for each of the node.
 */
#define PJ_RBTREE_NODE_SIZE      (sizeof(pj_rbtree_node))


/**
 * Guidance on memory required for the tree.
 */
#define PJ_RBTREE_SIZE           (sizeof(pj_rbtree))


/**
 * Initialize the tree.
 * @param tree the tree to be initialized.
 * @param comp key comparison function to be used for this tree.
 */
void pj_rbtree_init( pj_rbtree *tree, pj_rbtree_comp *comp);

/**
 * Get the first element in the tree.
 * The first element always has the least value for the key, according to
 * the comparison function.
 * @param tree the tree.
 * @return the tree node, or NULL if the tree has no element.
 */
pj_rbtree_node* pj_rbtree_first( pj_rbtree *tree );

/**
 * Get the last element in the tree.
 * The last element always has the greatest key value, according to the
 * comparison function defined for the tree.
 * @param tree the tree.
 * @return the tree node, or NULL if the tree has no element.
 */
pj_rbtree_node* pj_rbtree_last( pj_rbtree *tree );

/**
 * Get the successive element for the specified node.
 * The successive element is an element with greater key value.
 * @param tree the tree.
 * @param node the node.
 * @return the successive node, or NULL if the node has no successor.
 */
pj_rbtree_node* pj_rbtree_next( pj_rbtree *tree,
                   pj_rbtree_node *node );

/**
 * The the previous node for the specified node.
 * The previous node is an element with less key value.
 * @param tree the tree.
 * @param node the node.
 * @return the previous node, or NULL if the node has no previous node.
 */
pj_rbtree_node* pj_rbtree_prev( pj_rbtree *tree,
                   pj_rbtree_node *node );

/**
 * Insert a new node. 
 * The node will be inserted at sorted location. The key of the node must 
 * be UNIQUE, i.e. it hasn't existed in the tree.
 * @param tree the tree.
 * @param node the node to be inserted.
 * @return zero on success, or -1 if the key already exist.
 */
int pj_rbtree_insert( pj_rbtree *tree,
                 pj_rbtree_node *node );

/**
 * Find a node which has the specified key.
 * @param tree the tree.
 * @param key the key to search.
 * @return the tree node with the specified key, or NULL if the key can not
 *         be found.
 */
pj_rbtree_node* pj_rbtree_find( pj_rbtree *tree,
                   const void *key );

/**
 * Erase a node from the tree.
 * @param tree the tree.
 * @param node the node to be erased.
 * @return the tree node itself.
 */
pj_rbtree_node* pj_rbtree_erase( pj_rbtree *tree,
                    pj_rbtree_node *node );

/**
 * Get the maximum tree height from the specified node.
 * @param tree the tree.
 * @param node the node, or NULL to get the root of the tree.
 * @return the maximum height, which should be at most lg(N)
 */
unsigned pj_rbtree_max_height( pj_rbtree *tree,
                  pj_rbtree_node *node );

/**
 * Get the minumum tree height from the specified node.
 * @param tree the tree.
 * @param node the node, or NULL to get the root of the tree.
 * @return the height
 */
unsigned pj_rbtree_min_height( pj_rbtree *tree,
                  pj_rbtree_node *node );

红黑树的具体实现:

rbtree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
static void left_rotate( pj_rbtree *tree, pj_rbtree_node *node )
{
    pj_rbtree_node *rnode, *parent;

    PJ_CHECK_STACK();

    rnode = node->right;
    if (rnode == tree->null)
        return;

    node->right = rnode->left;
    if (rnode->left != tree->null)
        rnode->left->parent = node;
    parent = node->parent;
    rnode->parent = parent;
    if (parent != tree->null) {
        if (parent->left == node)
     parent->left = rnode;
        else
     parent->right = rnode;
    } else {
        tree->root = rnode;
    }
    rnode->left = node;
    node->parent = rnode;
}

static void right_rotate( pj_rbtree *tree, pj_rbtree_node *node )
{
    pj_rbtree_node *lnode, *parent;

    PJ_CHECK_STACK();

    lnode = node->left;
    if (lnode == tree->null)
        return;

    node->left = lnode->right;
    if (lnode->right != tree->null)
  lnode->right->parent = node;
    parent = node->parent;
    lnode->parent = parent;

    if (parent != tree->null) {
        if (parent->left == node)
      parent->left = lnode;
  else
      parent->right = lnode;
    } else {
        tree->root = lnode;
    }
    lnode->right = node;
    node->parent = lnode;
}

static void insert_fixup( pj_rbtree *tree, pj_rbtree_node *node )
{
    pj_rbtree_node *temp, *parent;

    PJ_CHECK_STACK();

    while (node != tree->root && node->parent->color == PJ_RBCOLOR_RED) {
        parent = node->parent;
        if (parent == parent->parent->left) {
      temp = parent->parent->right;
      if (temp->color == PJ_RBCOLOR_RED) {
          temp->color = PJ_RBCOLOR_BLACK;
          node = parent;
          node->color = PJ_RBCOLOR_BLACK;
          node = node->parent;
          node->color = PJ_RBCOLOR_RED;
      } else {
          if (node == parent->right) {
         node = parent;
         left_rotate(tree, node);
          }
          temp = node->parent;
          temp->color = PJ_RBCOLOR_BLACK;
          temp = temp->parent;
          temp->color = PJ_RBCOLOR_RED;
          right_rotate( tree, temp);
      }
        } else {
      temp = parent->parent->left;
      if (temp->color == PJ_RBCOLOR_RED) {
          temp->color = PJ_RBCOLOR_BLACK;
          node = parent;
          node->color = PJ_RBCOLOR_BLACK;
          node = node->parent;
          node->color = PJ_RBCOLOR_RED;
      } else {
          if (node == parent->left) {
          node = parent;
          right_rotate(tree, node);
          }
          temp = node->parent;
          temp->color = PJ_RBCOLOR_BLACK;
          temp = temp->parent;
          temp->color = PJ_RBCOLOR_RED;
          left_rotate(tree, temp);
     }
        }
    }
  
    tree->root->color = PJ_RBCOLOR_BLACK;
}


static void delete_fixup( pj_rbtree *tree, pj_rbtree_node *node )
{
    pj_rbtree_node *temp;

    PJ_CHECK_STACK();

    while (node != tree->root && node->color == PJ_RBCOLOR_BLACK) {
        if (node->parent->left == node) {
      temp = node->parent->right;
      if (temp->color == PJ_RBCOLOR_RED) {
          temp->color = PJ_RBCOLOR_BLACK;
          node->parent->color = PJ_RBCOLOR_RED;
          left_rotate(tree, node->parent);
          temp = node->parent->right;
      }
      if (temp->left->color == PJ_RBCOLOR_BLACK &&
          temp->right->color == PJ_RBCOLOR_BLACK)
      {
          temp->color = PJ_RBCOLOR_RED;
          node = node->parent;
      } else {
          if (temp->right->color == PJ_RBCOLOR_BLACK) {
          temp->left->color = PJ_RBCOLOR_BLACK;
          temp->color = PJ_RBCOLOR_RED;
          right_rotate( tree, temp);
          temp = node->parent->right;
          }
          temp->color = node->parent->color;
          temp->right->color = PJ_RBCOLOR_BLACK;
          node->parent->color = PJ_RBCOLOR_BLACK;
          left_rotate(tree, node->parent);
          node = tree->root;
      }
        } else {
      temp = node->parent->left;
      if (temp->color == PJ_RBCOLOR_RED) {
          temp->color = PJ_RBCOLOR_BLACK;
          node->parent->color = PJ_RBCOLOR_RED;
          right_rotate( tree, node->parent);
          temp = node->parent->left;
      }
      if (temp->right->color == PJ_RBCOLOR_BLACK &&
      temp->left->color == PJ_RBCOLOR_BLACK)
      {
          temp->color = PJ_RBCOLOR_RED;
          node = node->parent;
      } else {
          if (temp->left->color == PJ_RBCOLOR_BLACK) {
          temp->right->color = PJ_RBCOLOR_BLACK;
          temp->color = PJ_RBCOLOR_RED;
          left_rotate( tree, temp);
          temp = node->parent->left;
          }
          temp->color = node->parent->color;
          node->parent->color = PJ_RBCOLOR_BLACK;
          temp->left->color = PJ_RBCOLOR_BLACK;
          right_rotate(tree, node->parent);
          node = tree->root;
      }
        }
    }
  
    node->color = PJ_RBCOLOR_BLACK;
}


void pj_rbtree_init( pj_rbtree *tree, pj_rbtree_comp *comp )
{
    PJ_CHECK_STACK();

    tree->null = tree->root = &tree->null_node;
    tree->null->key = NULL;
    tree->null->user_data = NULL;
    tree->size = 0;
    tree->null->left = tree->null->right = tree->null->parent = tree->null;
    tree->null->color = PJ_RBCOLOR_BLACK;
    tree->comp = comp;
}

pj_rbtree_node* pj_rbtree_first( pj_rbtree *tree )
{
    register pj_rbtree_node *node = tree->root;
    register pj_rbtree_node *null = tree->null;

    PJ_CHECK_STACK();

    while (node->left != null)
  node = node->left;
    return node != null ? node : NULL;
}

pj_rbtree_node* pj_rbtree_last( pj_rbtree *tree )
{
    register pj_rbtree_node *node = tree->root;
    register pj_rbtree_node *null = tree->null;

    PJ_CHECK_STACK();

    while (node->right != null)
  node = node->right;
    return node != null ? node : NULL;
}

pj_rbtree_node* pj_rbtree_next( pj_rbtree *tree,
                  register pj_rbtree_node *node )
{
    register pj_rbtree_node *null = tree->null;

    PJ_CHECK_STACK();

    if (node->right != null) {
  for (node=node->right; node->left!=null; node = node->left)
      /* void */;
    } else {
        register pj_rbtree_node *temp = node->parent;
        while (temp!=null && temp->right==node) {
      node = temp;
      temp = temp->parent;
  }
  node = temp;
    }
    return node != null ? node : NULL;
}

pj_rbtree_node* pj_rbtree_prev( pj_rbtree *tree,
                  register pj_rbtree_node *node )
{
    register pj_rbtree_node *null = tree->null;

    PJ_CHECK_STACK();

    if (node->left != null) {
        for (node=node->left; node->right!=null; node=node->right)
     /* void */;
    } else {
        register pj_rbtree_node *temp = node->parent;
        while (temp!=null && temp->left==node) {
      node = temp;
      temp = temp->parent;
        }
        node = temp;
    }
    return node != null ? node : NULL;
}

int pj_rbtree_insert( pj_rbtree *tree,
                pj_rbtree_node *element )
{
    int rv = 0;
    pj_rbtree_node *node, *parent = tree->null,
         *null = tree->null;
    pj_rbtree_comp *comp = tree->comp;
  
    PJ_CHECK_STACK();

    node = tree->root;   
    while (node != null) {
        rv = (*comp)(element->key, node->key);
        if (rv == 0) {
      /* found match, i.e. entry with equal key already exist */
      return -1;
  }
  parent = node;
        node = rv < 0 ? node->left : node->right;
    }

    element->color = PJ_RBCOLOR_RED;
    element->left = element->right = null;

    node = element;
    if (parent != null) {
        node->parent = parent;
        if (rv < 0)
     parent->left = node;
        else
     parent->right = node;
        insert_fixup( tree, node);
    } else {
        tree->root = node;
        node->parent = null;
        node->color = PJ_RBCOLOR_BLACK;
    }
  
    ++tree->size;
    return 0;
}


pj_rbtree_node* pj_rbtree_find( pj_rbtree *tree,
                  const void *key )
{
    int rv;
    pj_rbtree_node *node = tree->root;
    pj_rbtree_node *null = tree->null;
    pj_rbtree_comp *comp = tree->comp;

    while (node != null) {
        rv = (*comp)(key, node->key);
        if (rv == 0)
      return node;
        node = rv < 0 ? node->left : node->right;
    }
    return node != null ? node : NULL;
}

pj_rbtree_node* pj_rbtree_erase( pj_rbtree *tree,
                   pj_rbtree_node *node )
{
    pj_rbtree_node *succ;
    pj_rbtree_node *null = tree->null;
    pj_rbtree_node *child;
    pj_rbtree_node *parent;

    PJ_CHECK_STACK();

    if (node->left == null || node->right == null) {
        succ = node;
    } else {
        for (succ=node->right; succ->left!=null; succ=succ->left)
     /* void */;
    }

    child = succ->left != null ? succ->left : succ->right;
    parent = succ->parent;
    child->parent = parent;

    if (parent != null) {
  if (parent->left == succ)
      parent->left = child;
        else
     parent->right = child;
    } else
        tree->root = child;

    if (succ != node) {
        succ->parent = node->parent;
        succ->left = node->left;
        succ->right = node->right;
        succ->color = node->color;

        parent = node->parent;
        if (parent != null) {
     if (parent->left==node)
          parent->left=succ;
     else
      parent->right=succ;
        }
        if (node->left != null)
     node->left->parent = succ;;
        if (node->right != null)
      node->right->parent = succ;

        if (tree->root == node)
     tree->root = succ;
    }

    if (succ->color == PJ_RBCOLOR_BLACK) {
  if (child != null)
      delete_fixup(tree, child);
        tree->null->color = PJ_RBCOLOR_BLACK;
    }

    --tree->size;
    return node;
}


unsigned pj_rbtree_max_height( pj_rbtree *tree,
                     pj_rbtree_node *node )
{
    unsigned l, r;

    PJ_CHECK_STACK();

    if (node==NULL)
  node = tree->root;

    l = node->left != tree->null ? pj_rbtree_max_height(tree,node->left)+1 : 0;
    r = node->right != tree->null ? pj_rbtree_max_height(tree,node->right)+1 : 0;
    return l > r ? l : r;
}

unsigned pj_rbtree_min_height( pj_rbtree *tree,
                     pj_rbtree_node *node )
{
    unsigned l, r;

    PJ_CHECK_STACK();

    if (node==NULL)
  node=tree->root;

    l = (node->left != tree->null) ? pj_rbtree_max_height(tree,node->left)+1 : 0;
    r = (node->right != tree->null) ? pj_rbtree_max_height(tree,node->right)+1 : 0;
    return l > r ? r : l;
}

Comments