Skip to content

Commit

Permalink
beautify base code
Browse files Browse the repository at this point in the history
  • Loading branch information
guangqianpeng committed Mar 2, 2018
1 parent d04b2df commit ed17f68
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 36 deletions.
16 changes: 8 additions & 8 deletions base/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ void *array_alloc(array *a)
size_next = a->elems + a->size * a->elem_size;
capacity_next = a->elems + a->capacity * a->elem_size;

// capacity足够
if (a->capacity > a->size) {
// capacity is enough
++a->size;
}
// capacity不够,但内存池足够
else if (capacity_next == a->pool->last
&& a->pool->last + a->elem_size <= a->pool->end) {
// capacity is't enough, but memory pool has more space
a->pool->last += a->elem_size;
++a->size;
++a->capacity;
}
// 重新分配
else {
// new memory block is needed
new_elems = palloc(a->pool, 2 * a->capacity * a->elem_size);
if (new_elems == NULL) {
return NULL;
}

memcpy(new_elems, a->elems, a->size * a->elem_size);

/* 回收旧数组的elems */
/* free old elements */
if (a->elems + a->capacity * a->elem_size == a->pool->last) {
a->pool->last -= a->capacity * a->elem_size;
}
Expand All @@ -103,19 +103,19 @@ void *array_n_alloc(array *a, size_t n)
capacity_next = a->elems + a->capacity * a->elem_size;
new_size = a->size + n;

// capacity足够
if (a->capacity >= a->size + n) {
// capacity is enough
a->size = new_size;
}
// capacity不够,但内存池足够
else if (capacity_next == a->pool->last
&& a->pool->last + n * a->elem_size <= a->pool->end) {
// new memory block is needed
a->pool->last += n * a->elem_size;
a->size += n;
a->capacity = a->size;
}
// 重新分配
else {
// new memory block is needed
size_t s = max(2 * a->capacity, new_size);

new_elems = palloc(a->pool, s * a->elem_size);
Expand All @@ -125,8 +125,8 @@ void *array_n_alloc(array *a, size_t n)

memcpy(new_elems, a->elems, a->size * a->elem_size);

/* 回收旧数组的elems */
if ((char*)a->elems + a->capacity * a->elem_size == a->pool->last) {
/* free old elements */
a->pool->last -= a->capacity * a->elem_size;
}

Expand Down
15 changes: 8 additions & 7 deletions base/array.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Created by frank on 17-2-10.
// dynamic array
//

#ifndef FANCY_ARRAY_H
Expand All @@ -10,16 +11,16 @@
typedef struct array array;

struct array {
char *elems; // 元素指针
size_t elem_size; // 每个元素大小
size_t size; // 元素个数
size_t capacity; // 容量
mem_pool *pool; // 内存池
char *elems; // element pointer
size_t elem_size; // size of each elements
size_t size; // number of elements
size_t capacity; // capacity of elements
mem_pool *pool; // associated memory pool
};

array *array_create(mem_pool *pool, size_t capacity, size_t elem_size);
/* 可以手动destroy
* */

/* destroy a array is ok */
void array_destroy(array *a);

void *array_alloc(array *a);
Expand Down
1 change: 1 addition & 0 deletions base/list.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Created by frank on 17-2-12.
// simple double-linked list
//

#ifndef FANCY_LIST_H
Expand Down
13 changes: 6 additions & 7 deletions base/palloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "base.h"
#include "palloc.h"

/* 追加一个新的空闲内存块 */
/* append a new memory block */
static mem_pool *mem_pool_append(mem_pool *pool);

mem_pool *mem_pool_create(size_t size)
Expand Down Expand Up @@ -43,14 +43,13 @@ void mem_pool_destroy(mem_pool *pool)
void *palloc(mem_pool *pool, size_t size)
{
char *last;
mem_pool *p;
mem_pool *p;

for (p = pool->current; p; p = p->next) {

/* 对齐指针 */
last = align_ptr(p->last, MEM_POOL_ALIGNMENT);

if ((size_t)(p->end - last) >= size) {
if (p->end - last >= size) {
p->last = last + size;
return last;
}
Expand Down Expand Up @@ -100,11 +99,11 @@ static mem_pool *mem_pool_append(mem_pool *pool)
new->failed = 0;
new->next = NULL;

/* 若内存空闲不足发生5次,则不再使用它分配
* failed最大值为6
/* for memory block failed more than 5 times,
* jump to the next block
* */
for (p = pool->current; p->next; p = p->next) {
if (p->failed++ > 4) {
if (++p->failed >= 5) {
pool->current = p->next;
}
}
Expand Down
12 changes: 7 additions & 5 deletions base/palloc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//
// Created by frank on 17-2-10.
// 简单的内存池
// 一次最多分配MEM_POOL_DEFAULT_SIZE字节数
// simple memory pool
//

#ifndef FANCY_MEM_POOL_H
Expand All @@ -10,9 +9,12 @@
#include <sys/types.h>

#define MEM_POOL_DEFAULT_SIZE (128 * 1024)

/* memory alignment you can change it before compile
* (e.g., 64 bytes for cache line alignment) */
#define MEM_POOL_ALIGNMENT sizeof(unsigned long)

/* 向上取对齐指针 */
/* align pointer to nearest lower bound */
#define align_ptr(ptr, alignment) \
((typeof(ptr)) (((u_int64_t)ptr + (alignment - 1)) & ~(alignment - 1)))

Expand All @@ -24,14 +26,14 @@ struct mem_pool {
u_int failed;
mem_pool *next;

/* 以下字段为mem_pool头结点独有 */
/* header node only */
mem_pool *current;
};

mem_pool *mem_pool_create(size_t size);
void mem_pool_destroy(mem_pool *pool);

/* 返回对齐指针,同malloc */
/* return aligned pointer, just like malloc */
void *palloc(mem_pool *pool, size_t size);
void *pcalloc(mem_pool *pool, size_t size);

Expand Down
18 changes: 10 additions & 8 deletions base/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void rbtree_init(rbtree *tree, rbtree_node *sentinel)
root->left = NULL;
root->right = NULL;

/* 该字段在删除节点时有用 */
/* this field is useful when deleting */
root->parent = NULL;
}

Expand All @@ -51,7 +51,7 @@ void rbtree_insert(rbtree *tree, rbtree_node *node)
{
rbtree_node *temp;

/* 空树 */
/* empty tree */
if (tree->root == tree->sentinel) {
set_black(node);
node->left = tree->sentinel;
Expand All @@ -63,7 +63,8 @@ void rbtree_insert(rbtree *tree, rbtree_node *node)

insert(tree, node);

/* 因为is_black(sentinel) && root.parent==sentinel,所以node==root时也会跳出循环 */
/* since is_black(sentinel) && root.parent==sentinel,
* node==root can break the loop */
while (is_red(node->parent)) {
if (node->parent == node->parent->parent->left) {
temp = node->parent->parent->right;
Expand All @@ -82,7 +83,7 @@ void rbtree_insert(rbtree *tree, rbtree_node *node)
set_black(node->parent);
set_red(node->parent->parent);
rotate_right(tree, node->parent->parent);
break; /* 此时is_black(node.parent), 用break避免多余测试 */
break; /* now is_black(node.parent), we break */
}
}
else {
Expand Down Expand Up @@ -112,7 +113,7 @@ void rbtree_insert(rbtree *tree, rbtree_node *node)

void rbtree_delete(rbtree *tree, rbtree_node *node)
{
/* subt是需要移动的节点, temp是subt的子树
/* subt is the node that needs to be moved, temp is a subtree of subt
* color==subt.color
* */
rbtree_node *sentinel, *temp, *subt;
Expand Down Expand Up @@ -178,6 +179,7 @@ static void rotate_left(rbtree *tree, rbtree_node *node)
temp = node->right;

node->right = temp->left;

/* 此处的检查是必须的,因为sentinel.parent字段在删除节点时有特殊用途
* rotate_right也一样
* */
Expand Down Expand Up @@ -376,15 +378,15 @@ static int is_regular(rbtree_node *node, rbtree_node *sentinel)
if (node == sentinel)
return 1;

/* 不能有相连的红色节点 */
/* no connected red node */
if (is_red(node) && is_red(node->parent))
return 0;

/* 黑高完美平衡 */
/* black height is perfectly balanced */
if (balck_height(node->left, sentinel) != balck_height(node->right, sentinel))
return 0;

/* 二叉树基本性质 */
/* must be binary search tree */
if ( (node->parent->left == node && node->key > node->parent->key)
|| (node->parent->right == node && node->key < node->parent->key))
return 0;
Expand Down
3 changes: 2 additions & 1 deletion base/rbtree.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Created by frank on 17-2-9.
// red black implementation
//

#ifndef FANCY_RBTREE_H
Expand Down Expand Up @@ -30,7 +31,7 @@ void rbtree_insert(rbtree *tree, rbtree_node *node);
void rbtree_delete(rbtree *tree, rbtree_node *node);
rbtree_node* rbtree_min(rbtree *tree);

/* debug */
/* for debug */
int rbtree_is_regular(rbtree *tree);

#endif //FANCY_RBTREE_H

0 comments on commit ed17f68

Please sign in to comment.