Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffers: optimize mmap() usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Dec 17, 2012
1 parent 1c265c5 commit 8dc9dca
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/node_buffer.cc
Expand Up @@ -31,7 +31,6 @@

#ifdef __POSIX__
# include <sys/mman.h> // mmap
# include <unistd.h> // sysconf
# include <stdio.h> // perror
#endif

Expand Down Expand Up @@ -209,8 +208,12 @@ static char* cached_pool_buffers[16];


static inline void free_buf_mem(char* buf, size_t len) {
if (len == Buffer::kPoolSize &&
num_pool_buffers < ARRAY_SIZE(cached_pool_buffers)) {
if (len != Buffer::kPoolSize) {
delete[] buf;
return;
}

if (num_pool_buffers < ARRAY_SIZE(cached_pool_buffers)) {
cached_pool_buffers[num_pool_buffers++] = buf;
return;
}
Expand All @@ -223,10 +226,11 @@ static inline void free_buf_mem(char* buf, size_t len) {


static inline char* alloc_buf_mem(size_t len) {
size_t pagesize = sysconf(_SC_PAGESIZE);
if (len != Buffer::kPoolSize) {
return new char[len];
}

len = ROUND_UP(len, pagesize);
if (len == Buffer::kPoolSize && num_pool_buffers > 0) {
if (num_pool_buffers > 0) {
return cached_pool_buffers[--num_pool_buffers];
}

Expand Down

0 comments on commit 8dc9dca

Please sign in to comment.