Skip to content

Commit

Permalink
update some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
liexusong committed Dec 13, 2013
1 parent dde5bb9 commit e7e3a91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
22 changes: 13 additions & 9 deletions beast_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static int beast_mm_allocate(void *shmaddr, int size)

/* Realsize must be aligned to a word boundary on some architectures. */
realsize = beast_mm_alignmem(max(size + beast_mm_alignmem(sizeof(int)),
sizeof(beast_block_t)));
sizeof(beast_block_t)));

/*
* First, insure that the segment contains at least realsize free bytes,
Expand All @@ -84,7 +84,7 @@ static int beast_mm_allocate(void *shmaddr, int size)
header = (beast_header_t *)shmaddr;
if (header->avail < realsize) {
beast_write_log(beast_log_error,
"Not enough memory for beast_mm_alloc()");
"Not enough memory for beast_mm_alloc()");
return -1;
}

Expand Down Expand Up @@ -197,8 +197,8 @@ int beast_mm_init(int block_size)
}

/* init memory manager lock */
mm_lock = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
mm_lock = (int *)mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANON, -1, 0);
if (!mm_lock) {
beast_write_log(beast_log_error,
"Unable alloc share memory for memory manager lock");
Expand All @@ -225,7 +225,7 @@ int beast_mm_init(int block_size)
header->segsize = beast_mm_block_size;
/* avail size */
header->avail = beast_mm_block_size - sizeof(beast_header_t) -
sizeof(beast_block_t) - beast_mm_alignmem(sizeof(int));
sizeof(beast_block_t) - beast_mm_alignmem(sizeof(int));

/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
Expand Down Expand Up @@ -267,9 +267,9 @@ void *beast_mm_calloc(int size)
int offset;
void *p = NULL;
int pid = (int)getpid();

beast_spinlock(mm_lock, pid);

offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
Expand All @@ -296,7 +296,9 @@ void beast_mm_free(void *p)
}

beast_spinlock(mm_lock, pid);

beast_mm_deallocate(beast_mm_block, offset);

beast_spinunlock(mm_lock, pid);
}

Expand All @@ -313,7 +315,7 @@ void beast_mm_flush()
shmaddr = beast_mm_block;
header = (beast_header_t *)shmaddr;
header->avail = beast_mm_block_size - sizeof(beast_header_t) -
sizeof(beast_block_t) - beast_mm_alignmem(sizeof(int));
sizeof(beast_block_t) - beast_mm_alignmem(sizeof(int));

/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
Expand All @@ -339,7 +341,9 @@ int beast_mm_availspace()
int pid = (int)getpid();

beast_spinlock(mm_lock, pid);

size = header->avail;

beast_spinunlock(mm_lock, pid);

return size;
Expand All @@ -361,7 +365,7 @@ int beast_mm_realspace()
void beast_mm_destroy()
{
if (beast_mm_initialized) {
munmap(beast_mm_block, beast_mm_block_size); /* free caches */
munmap(beast_mm_block, beast_mm_block_size); /* free cache's memory */
munmap(mm_lock, sizeof(int)); /* free memory lock */
beast_mm_initialized = 0;
}
Expand Down
23 changes: 5 additions & 18 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int beast_cache_init(int size)
return -1;
}

/* init cache lock */
cache_lock = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
if (!cache_lock) {
Expand All @@ -63,6 +64,7 @@ int beast_cache_init(int size)
}
*cache_lock = 0;

/* init cache buckets's memory */
bucket_size = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;
beast_cache_buckets = (cache_item_t **)mmap(NULL, bucket_size,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
Expand Down Expand Up @@ -151,10 +153,10 @@ cache_item_t *beast_cache_create(cache_key_t *key, int size)
beast_cache_buckets[index] = NULL;
}

beast_spinunlock(cache_lock, pid);

beast_mm_flush();

beast_spinunlock(cache_lock, pid);

item = beast_mm_malloc(msize);
if (!item) {
return NULL;
Expand Down Expand Up @@ -231,26 +233,11 @@ int beast_cache_destroy()
return 0;
}

beast_spinlock(cache_lock, pid);

#if 0 /* not need free the item cache, because beast_mm_destroy() would free */
for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
item = beast_cache_buckets[index];
while (item) {
next = item->next;
beast_mm_free(item);
item = next;
}
}
#endif

beast_mm_destroy();
beast_mm_destroy(); /* destroy memory manager */

/* free cache buckets's mmap memory */
munmap(beast_cache_buckets, sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE);

beast_spinunlock(cache_lock, pid);

munmap(cache_lock, sizeof(int));

beast_cache_initialization = 0;
Expand Down

0 comments on commit e7e3a91

Please sign in to comment.