Lighttpd核心结构分析

Lighttpd源代码中涉及到的几类核心数据结构,虽然采用的大量结构化语言,但是到处体现的是面向对象的事项。

1:buffer(字符串函数)
内部定义如下:
typedef struct {
	char *ptr; //字符串指针

	size_t used;//已用大小
	size_t size;//总共大小
} buffer;
从该结构衍生出,二维字符串数组结构

typedef struct {
	buffer **ptr;

	size_t used;
	size_t size;
} buffer_array;

1.1buffer初始化内存特点
使用一个已经存在BUFFER初始化当前BUFFER的时候,采取措施分配的内存都是64的整数倍。
/**
 *
 * allocate (if neccessary) enough space for 'size' bytes and
 * set the 'used' counter to 0
 *
 */
#define BUFFER_PIECE_SIZE 64

int buffer_prepare_copy(buffer *b, size_t size) {
	if (!b) return -1;

	if ((0 == b->size) ||
	    (size > b->size)) {
		if (b->size) free(b->ptr);

		b->size = size;

		/* always allocate a multiply of BUFFER_PIECE_SIZE */
		b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE);//设置为PIECE的整数倍

		b->ptr = malloc(b->size);
		assert(b->ptr);
	}
	b->used = 0;
}


2:dataset类型(对象模板,通用结构)
typedef enum { TYPE_UNSET, TYPE_STRING, TYPE_COUNT, TYPE_ARRAY, TYPE_INTEGER, TYPE_FASTCGI, TYPE_CONFIG } data_type_t;
#define DATA_UNSET \
	data_type_t type; \
	buffer *key; \
	int is_index_key; /* 1 if key is a array index (autogenerated keys) */ \
	struct data_unset *(*copy)(const struct data_unset *src); \
	void (* free)(struct data_unset *p); \
	void (* reset)(struct data_unset *p); \
	int (*insert_dup)(struct data_unset *dst, struct data_unset *src); \
	void (*print)(const struct data_unset *p, int depth)

typedef struct data_unset {
	DATA_UNSET;
} data_unset;

/*data_unset 实际的类型,有点类似于函数
typedef struct data_unset
{
	data_type_t type;
	buffer * key;
	int is_index_key;
	struct data_unset *(*copy)(const struct data_unset *src); 
	void (* free)(struct data_unset *p); 
	void (* reset)(struct data_unset *p); 
	int (*insert_dup)(struct data_unset *dst, struct data_unset *src); \
	void (*print)(const struct data_unset *p, int depth)
}
*/

data_unset可以是整形,字符串行,以及其他类型,但是通过函数指针,定于几个函数free,
reset,print等等。相当于data_unset是一个虚基类

3:array(实际上data_unset的二维结构)
array定于如下:
typedef struct {
	data_unset  **data;--二维data

	size_t *sorted;

	size_t used;
	size_t size;

	size_t unique_ndx;

	size_t next_power_of_2;
	int is_weakref; /* data is weakref, don't bother the data */
} array;

你可能感兴趣的:(java,数据结构,lighttpd)