Skip to content

Commit

Permalink
have done some testing and midify:mem_pool,log
Browse files Browse the repository at this point in the history
  • Loading branch information
yalog committed Jun 4, 2011
1 parent 7955d7f commit 5ee98d9
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 40 deletions.
4 changes: 2 additions & 2 deletions core/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void event_process()
log_debug("connection overload and discard");
}
else {
http_init_connection(c); //初始化一个HTTP连接
http_init_connection(c); //初始化一个HTTP连接,功能的衔接点(link)
log_access("connection reached");
}
}while(1);
Expand All @@ -266,7 +266,7 @@ void event_process()
//将发生的事件,放入事件队列中
log_debug("A event Occurred");
c = event_list[i].data.ptr;
//在这里debug一下events的值
//在这里debug一下events的值,这里可以对event增加一个ready字段,发生事件后就标志,处理完后就取消
if ((event_list[i].events & EPOLLIN) && c->read.active) {
event_posted_add(c.read);
}
Expand Down
42 changes: 21 additions & 21 deletions core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>

#define DEBUG 0
#define ERROR 1
#define LOG_LEVEL DEBUG
#include "log.h"

//这是日志文件的配置
//#define error_log "/var/log/mihttpd_error"
Expand All @@ -27,7 +26,9 @@
#define access_log "./mihttpd_access"

FILE *mi_error;
FILE *mi_debug;
#ifdef DEBUG
FILE *mi_debug;
#endif
FILE *mi_access;

/*
Expand Down Expand Up @@ -69,25 +70,24 @@ void log_init()
}
}

if (LOG_LEVEL == DEBUG ) {
if (stat(debug_log, &s) == -1) {
if (errno == ENOENT) { //文件不存在,创建
if ((mi_debug = fopen(debug_log, "w")) == NULL) {
printf("debug_log init failure\n");
exit(1);
}
}
}
else {
if ((mi_debug = fopen(debug_log, "a")) == NULL) {
#ifdef DEBUG
if (stat(debug_log, &s) == -1) {
if (errno == ENOENT) { //文件不存在,创建
if ((mi_debug = fopen(debug_log, "w")) == NULL) {
printf("debug_log init failure\n");
exit(1);
}
}
}
else {
if ((mi_debug = fopen(debug_log, "a")) == NULL) {
printf("debug_log init failure\n");
exit(1);
}
}
#endif

fd = open(error_log, O_APPEND);
dup2(fd, STDIN_FILENO);
fd = open("/dev/null", O_WRONLY);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}
Expand All @@ -102,13 +102,13 @@ void _log_error(int error_no, char *msg, char *filename, int line)

void _log_debug(char *msg, char *filename,int line)
{
if (LOG_LEVEL == DEBUG) {
fprintf(mi_debug, "%s:%d %s\n", filename, line, msg);
}
fprintf(mi_debug, "%s:%d %s\n", filename, line, msg);
}

void log_access(char *msg)
{
//记录访问时间
fprintf(mi_access, "2011-6-2 %s\n", msg);
time_t timep;
time(&timep);
fprintf(mi_access, "%s ::%s\n", msg, ctime(&timep));
}
9 changes: 8 additions & 1 deletion core/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
#ifndef _LOG_H_
#define _LOG_H_

#define DEBUG 1

#define log_error(error_no, msg) _log_error(error_no, msg, __FILE__, __LINE__)
#define log_debug(msg) _log_debug(msg, __FILE__, __LINE__)

#ifdef DEBUG
#define log_debug(msg) _log_debug(msg, __FILE__, __LINE__)
#else
#define log_debug(msg)
#endif

void _log_error(int error_no, char *msg, char *filename, int line);
void _log_debug(char *msg, char *filename,int line);
Expand Down
39 changes: 27 additions & 12 deletions core/mem_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <stdlib.h>

#include "mem_pool.h"
#include "log.h"

#define MEM_POOL_SIZE 4096*4 //内存池创建时为1024*8
#define MEM_POOL_SIZE 4096*4 //内存池创建时为16k

static mem_chunk_t *create_chunk(mem_size_t);

Expand All @@ -28,26 +29,30 @@ mem_pool_t *mem_pool_create()
log_error(0, "malloc() memery use up");
return NULL;
}
pool = (mem_pool_t *)(chunk + sizeof(mem_chunk_t));
pool = (mem_pool_t *)((char *)chunk + sizeof(mem_chunk_t));

chunk->size = MEM_POOL_SIZE;
chunk->free_size = MEM_POOL_SIZE - sizeof(mem_pool_t) - sizeof(mem_chunk_t);
chunk->next = NULL;
chunk->first_avail = pool + sizeof(mem_pool_t);
chunk->endp = chunk + MEM_POOL_SIZE - 1;
chunk->first_avail = (char *)(pool) + sizeof(mem_pool_t);
chunk->endp = (char *)(chunk) + MEM_POOL_SIZE - 1;

pool->first = chunk;
pool->last = chunk;
pool->current = chunk;
pthread_mutex_init(&pool->lock, 0);

log_debug("memory pool created");

return pool;
}

/*
* 销毁一个内存池
*/
void mem_pool_destroy(mem_pool_t *pool)
{
mem_chunk_t *cur_chunk, next_chunk;
mem_chunk_t *cur_chunk, *next_chunk;

next_chunk = pool->first;
do {
Expand All @@ -56,6 +61,8 @@ void mem_pool_destroy(mem_pool_t *pool)

free(cur_chunk);
} while(next_chunk != NULL);

pthread_mutex_destroy(&pool->lock);
}

/*
Expand Down Expand Up @@ -84,9 +91,9 @@ void *mem_palloc(mem_pool_t *pool, mem_size_t size)
}
}

mem_addr = chunk->fist_avail;
mem_addr = chunk->first_avail;
chunk->first_avail += size;
chunk->free -=size;
chunk->free_size -= size;

return mem_addr;
}
Expand All @@ -98,16 +105,24 @@ static mem_chunk_t *create_chunk(mem_size_t size)
{
mem_chunk_t *chunk;

chunk = (mem_chunk_t *)malloc(MEM_ALIGN(size + sizeof(mem_chunk_t)));
if (size + sizeof(mem_chunk_t) <= MEM_CHUNK_DEFAULT_SIZE) {
size = MEM_CHUNK_DEFAULT_SIZE; //保证内存块的最小分配
}
else {
size = MEM_ALIGN(size + sizeof(mem_chunk_t));
}

chunk = (mem_chunk_t *)malloc(size);
if (chunk == NULL) {
log_error(0, "malloc() memery use up");
return NULL;
}
chunk->size =MEM_ALIGN(size + sizeof(mem_chunk_t));
chunk->free_size = MEM_ALIGN(size + sizeof(mem_chunk_t)) - sizeof(mem_chunk_t);
chunk->size = size;
chunk->free_size = size - sizeof(mem_chunk_t);
chunk->next = NULL;
chunk->first_avail = chunk + sizeof(mem_chunk_t);
chunk->endp = chunk + MEM_POOL_SIZE - 1;
chunk->first_avail = (char *)chunk + sizeof(mem_chunk_t);
chunk->endp = (char *)chunk + size - 1;

log_debug("memory chunk alloced");
return chunk;
}
2 changes: 1 addition & 1 deletion core/mem_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef int mem_size_t;
typedef struct mem_chunk_s{
mem_size_t size; //块的尺寸
mem_size_t free_size; //块剩余的
mem_chunk_s *next;
struct mem_chunk_s *next;
char *first_avail; //标志可以用内存的起始位置
char *endp; //标志可以用内存的结束位置
}mem_chunk_t;
Expand Down
9 changes: 8 additions & 1 deletion core/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
*/
/*
* 这个文件主要实现对信号的处理
*/
*/

/*
* 信号初始化
*/
void sig_init()
{
}
2 changes: 1 addition & 1 deletion core/thread_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void *worker(void *data)
event_t *ev;

for (;;) { //循环处理事件
sem_wait(&posted_event_num); //没有需要处理的事件时,就等待
sem_wait(&posted_event_num); //没有需要处理的事件时,就等待,当然这会引起线程切换导致一些开销
log_debug("process a new event");

ev = event_posted_get();
Expand Down
21 changes: 21 additions & 0 deletions main/http_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,33 @@
/*
* 这个是关于整个服务器启动和初始化相关操作的文件
*/
#include <unistd.h>
#include <stdio.h>


#define WORK_DIR "/var/www/"

/*
* 整个系统的入口函数;
* 暂时还没有想好处理什么启动参数;
*/
int main(int agrc, char *agrv[])
{
int pid;

//创建守护进程
pid = fork();
if (pid > 0) {
exit(0);
}
else if (pid < 0) {
printf("fork() Mihttpd start abort\n");
exit(1);
}
setsid();

//底层初始化


return 0;
}
12 changes: 11 additions & 1 deletion main/http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@
*/
/*
* 所有与请求信息及处理相关才操作的实现
*/
*/

#include "../core/event.h"

/*
* 初始化请求
*/
void http_init_request(event_t *ev)
{
}

19 changes: 19 additions & 0 deletions modules/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Mihttpd/modules/module.h
*
* (c) 2011 yalog
*/
/*
* 这个文件主要用来注册http扩展处理模块
*/
#include ""

struct module {
void (* handler)(request_t *); //模块的处理挂载钩子
};

//这里注册模块挂载表
struct module modules[] = {
&handler_cgi,
NULL
};

0 comments on commit 5ee98d9

Please sign in to comment.