Skip to content

Commit 6c5356b

Browse files
committed
Revert "buffer: allocate memory with mmap()"
Also Revert "buffer: use MAP_ANON, fix OS X build" This reverts commit ddb1560. This reverts commit 2433ec8.
1 parent 1c265c5 commit 6c5356b

File tree

4 files changed

+5
-114
lines changed

4 files changed

+5
-114
lines changed

lib/buffer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
var SlowBuffer = process.binding('buffer').SlowBuffer;
23-
var kPoolSize = process.binding('buffer').kPoolSize;
2423
var assert = require('assert');
2524

2625
exports.INSPECT_MAX_BYTES = 50;
@@ -298,7 +297,7 @@ Buffer.isEncoding = function(encoding) {
298297

299298

300299

301-
Buffer.poolSize = kPoolSize; // see src/node_buffer.h
300+
Buffer.poolSize = 8 * 1024;
302301
var pool;
303302

304303
function allocPool() {

src/node_buffer.cc

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
#include <assert.h>
3030
#include <string.h> // memcpy
3131

32-
#ifdef __POSIX__
33-
# include <sys/mman.h> // mmap
34-
# include <unistd.h> // sysconf
35-
# include <stdio.h> // perror
36-
#endif
37-
3832
#define MIN(a,b) ((a) < (b) ? (a) : (b))
3933

4034
#define BUFFER_CLASS_ID (0xBABE)
@@ -202,67 +196,6 @@ Buffer::~Buffer() {
202196
}
203197

204198

205-
#if defined(__POSIX__)
206-
207-
static unsigned int num_pool_buffers;
208-
static char* cached_pool_buffers[16];
209-
210-
211-
static inline void free_buf_mem(char* buf, size_t len) {
212-
if (len == Buffer::kPoolSize &&
213-
num_pool_buffers < ARRAY_SIZE(cached_pool_buffers)) {
214-
cached_pool_buffers[num_pool_buffers++] = buf;
215-
return;
216-
}
217-
218-
if (munmap(buf, len)) {
219-
perror("munmap");
220-
abort();
221-
}
222-
}
223-
224-
225-
static inline char* alloc_buf_mem(size_t len) {
226-
size_t pagesize = sysconf(_SC_PAGESIZE);
227-
228-
len = ROUND_UP(len, pagesize);
229-
if (len == Buffer::kPoolSize && num_pool_buffers > 0) {
230-
return cached_pool_buffers[--num_pool_buffers];
231-
}
232-
233-
int prot = PROT_READ | PROT_WRITE;
234-
int flags = MAP_ANON | MAP_PRIVATE; // OS X doesn't know MAP_ANONYMOUS...
235-
char* buf = static_cast<char*>(mmap(NULL, len, prot, flags, -1, 0));
236-
237-
if (buf == NULL) {
238-
TryCatch try_catch;
239-
char errmsg[256];
240-
snprintf(errmsg,
241-
sizeof(errmsg),
242-
"Out of memory, mmap(len=%lu) failed.",
243-
static_cast<unsigned long>(len));
244-
ThrowError(errmsg);
245-
FatalException(try_catch);
246-
abort();
247-
}
248-
249-
return buf;
250-
}
251-
252-
#else // !__POSIX__
253-
254-
static inline void free_buf_mem(char* buf, size_t len) {
255-
delete[] buf;
256-
}
257-
258-
259-
static inline char* alloc_buf_mem(size_t len) {
260-
return new char[len];
261-
}
262-
263-
#endif // __POSIX__
264-
265-
266199
// if replace doesn't have a callback, data must be copied
267200
// const_cast in Buffer::New requires this
268201
void Buffer::Replace(char *data, size_t length,
@@ -272,7 +205,7 @@ void Buffer::Replace(char *data, size_t length,
272205
if (callback_) {
273206
callback_(data_, callback_hint_);
274207
} else if (length_) {
275-
free_buf_mem(data_, length_);
208+
delete [] data_;
276209
V8::AdjustAmountOfExternalAllocatedMemory(
277210
-static_cast<intptr_t>(sizeof(Buffer) + length_));
278211
}
@@ -284,8 +217,9 @@ void Buffer::Replace(char *data, size_t length,
284217
if (callback_) {
285218
data_ = data;
286219
} else if (length_) {
287-
data_ = alloc_buf_mem(length_);
288-
if (data != NULL) memcpy(data_, data, length_);
220+
data_ = new char[length_];
221+
if (data)
222+
memcpy(data_, data, length_);
289223
V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer) + length_);
290224
} else {
291225
data_ = NULL;
@@ -896,8 +830,6 @@ void Buffer::Initialize(Handle<Object> target) {
896830
Buffer::MakeFastBuffer);
897831

898832
target->Set(String::NewSymbol("SlowBuffer"), constructor_template->GetFunction());
899-
target->Set(String::NewSymbol("kPoolSize"),
900-
Integer::NewFromUnsigned(kPoolSize));
901833

902834
HeapProfiler::DefineWrapperClass(BUFFER_CLASS_ID, WrapperInfo);
903835
}

src/node_buffer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ class NODE_EXTERN Buffer: public ObjectWrap {
6868
// mirrors deps/v8/src/objects.h
6969
static const unsigned int kMaxLength = 0x3fffffff;
7070

71-
// exported in lib/buffer.js as Buffer.poolSize
72-
static const unsigned int kPoolSize = 32768;
73-
7471
static v8::Persistent<v8::FunctionTemplate> constructor_template;
7572

7673
static bool HasInstance(v8::Handle<v8::Value> val);

test/simple/test-buffer-release.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)