Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
liexusong committed Sep 6, 2013
1 parent e9c37fe commit bd6d5eb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 98 deletions.
11 changes: 9 additions & 2 deletions beast.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,18 @@ ZEND_INI_MH(php_beast_lock_path)
return FAILURE;
}

beast_lock_path = strdup(new_value);
while (new_value[new_value_length] == '/') {
new_value_length--;
}

beast_lock_path = malloc(new_value_length + 1);
if (beast_lock_path == NULL) {
return FAILURE;
}

memcpy(beast_lock_path, new_value, new_value_length);
beast_lock_path[new_value_length] = '\0';

return SUCCESS;
}

Expand All @@ -438,7 +445,7 @@ ZEND_INI_MH(php_beast_enable)
PHP_INI_BEGIN()
PHP_INI_ENTRY("beast.cache_size", "1048576", PHP_INI_ALL,
php_beast_cache_size)
PHP_INI_ENTRY("beast.log_file", "/home/beast.log", PHP_INI_ALL,
PHP_INI_ENTRY("beast.log_file", "/tmp/beast.log", PHP_INI_ALL,
php_beast_log_file)
PHP_INI_ENTRY("beast.lock_path", "/tmp/", PHP_INI_ALL,
php_beast_lock_path)
Expand Down
20 changes: 12 additions & 8 deletions beast_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static inline int __do_lock(beast_locker_t *locker, int type)
do {
ret = fcntl(locker->fd, F_SETLKW, &lock);
tries--;
} while (ret < 0 && tries > 0 &&
} while (ret < 0 && tries > 0 &&
(errno == EINTR || errno == EAGAIN));

return ret;
Expand All @@ -38,13 +38,13 @@ beast_locker_t *beast_locker_create(char *path)

locker = malloc(sizeof(beast_locker_t));
if (!locker) {
return NULL;
goto failed;
}

fd = open(path, O_RDWR|O_CREAT, 0666);
if (fd == -1) {
free(locker);
return NULL;
goto failed;
}

locker->fd = fd;
Expand All @@ -53,18 +53,23 @@ beast_locker_t *beast_locker_create(char *path)
if (!locker->path) {
close(fd);
free(locker);
return NULL;
goto failed;
}

return locker;

failed:
beast_write_log(beast_log_notice,
"beast_locker_create() failed errno(%d)", errno);
return NULL;
}


int beast_locker_wrlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_WRLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_wrlock failed errno(%d)", errno);
"beast_locker_wrlock() failed errno(%d)", errno);
return -1;
}
return 0;
Expand All @@ -75,7 +80,7 @@ int beast_locker_rdlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_RDLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_rdlock failed errno(%d)", errno);
"beast_locker_rdlock() failed errno(%d)", errno);
return -1;
}
return 0;
Expand All @@ -86,7 +91,7 @@ int beast_locker_unlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_UNLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_unlock failed errno(%d)", errno);
"beast_locker_unlock() failed errno(%d)", errno);
return -1;
}
return 0;
Expand All @@ -96,7 +101,6 @@ int beast_locker_unlock(beast_locker_t *locker)
void beast_locker_destroy(beast_locker_t *locker)
{
close(locker->fd);
unlink(locker->path);
free(locker->path);
free(locker);
}
10 changes: 2 additions & 8 deletions beast_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ extern char *beast_log_file;

void beast_write_log(beast_log_level level, const char *fmt, ...)
{
char *headers[] = {"{debug}", "{notice}", "{error}"};
char *headers[] = {"[debug]", "[notice]", "[error]"};
va_list ap;
FILE *fp;
time_t now;
char buf[64];

if (level > beast_log_error ||
level < beast_log_debug) {
Expand All @@ -25,13 +23,9 @@ void beast_write_log(beast_log_level level, const char *fmt, ...)
return;
}

now = time(NULL);
strftime(buf, sizeof(buf), "%d %b %H:%M:%S", localtime(&now));

fprintf(fp, "[%s] %s ", buf, headers[level]);

va_start(ap, fmt);

fprintf(fp, "%s ", headers[level]);
vfprintf(fp, fmt, ap);
fprintf(fp, "\n");
fflush(fp);
Expand Down
54 changes: 14 additions & 40 deletions beast_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ int beast_mm_init(int block_size)
return 0;
}

sprintf(lock_file, "%s.beast.mlock", beast_lock_path);
sprintf(lock_file, "%s/beast.mlock", beast_lock_path);

beast_mm_locker = beast_locker_create(lock_file);
if (beast_mm_locker == -1) {
if (beast_mm_locker == NULL) {
beast_write_log(beast_log_error, "Unable create memory "
"manager locker for beast");
return -1;
Expand All @@ -212,7 +212,7 @@ int beast_mm_init(int block_size)
}

shmaddr = beast_mm_block = (void *)mmap(NULL, beast_mm_block_size,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
if (!beast_mm_block) {
beast_locker_destroy(beast_mm_locker);
beast_write_log(beast_log_error, "Unable create share "
Expand Down Expand Up @@ -246,12 +246,8 @@ void *beast_mm_malloc(int size)
int offset;
void *p = NULL;

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_malloc() failed, unable get locker");
return NULL;
}

beast_locker_lock(beast_mm_locker);

offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
Expand All @@ -268,17 +264,13 @@ void *beast_mm_calloc(int size)
int offset;
void *p = NULL;

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_calloc() failed, unable get locker");
return NULL;
}

beast_locker_lock(beast_mm_locker);

offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
}

beast_locker_unlock(beast_mm_locker);

if (NULL != p) {
Expand All @@ -289,39 +281,28 @@ void *beast_mm_calloc(int size)
}


int beast_mm_free(void *p)
void beast_mm_free(void *p)
{
int offset;

offset = (unsigned int)((char *)p - (char *)beast_mm_block);
if (offset <= 0) {
return -1;
}

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_free() failed, unable get locker");
return -1;
return;
}

beast_locker_lock(beast_mm_locker);
beast_mm_deallocate(beast_mm_block, offset);

beast_locker_unlock(beast_mm_locker);
return 0;
}


int beast_mm_flush()
void beast_mm_flush()
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_flush() failed, unable get locker");
return -1;
}
beast_locker_lock(beast_mm_locker);

shmaddr = beast_mm_block;
header = (beast_header_t *)shmaddr;
Expand All @@ -339,8 +320,6 @@ int beast_mm_flush()
block->next = 0;

beast_locker_unlock(beast_mm_locker);

return 0;
}


Expand All @@ -352,12 +331,7 @@ int beast_mm_availspace()
int size;
beast_header_t *header = (beast_header_t *)beast_mm_block;

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_availspace() failed, unable get locker");
return -1;
}

beast_locker_lock(beast_mm_locker);
size = header->avail;
beast_locker_unlock(beast_mm_locker);

Expand Down
4 changes: 2 additions & 2 deletions beast_mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
int beast_mm_init(int block_size);
void *beast_mm_malloc(int size);
void *beast_mm_calloc(int size);
int beast_mm_free(void *p);
int beast_mm_flush();
void beast_mm_free(void *p);
void beast_mm_flush();
int beast_mm_availspace();
int beast_mm_realspace();
void beast_mm_destroy();
Expand Down
Loading

0 comments on commit bd6d5eb

Please sign in to comment.