File: binding.cc

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (54 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <node.h>
#include <v8.h>
#include <stdlib.h>

#ifdef _AIX
// AIX allows over-allocation, and will SIGKILL when the allocated pages are
// used if there is not enough VM. Check for available space until
// out-of-memory.  Don't allow more then some (large) proportion of it to be
// used for the test strings, so Node & V8 have some space for allocations.
#include <signal.h>
#include <sys/vminfo.h>

static void* Allowed(size_t size) {
  blkcnt_t allowedBlocks = psdanger(SIGKILL);

  if (allowedBlocks < 1) {
    // Already OOM
    return nullptr;
  }
  const size_t BYTES_PER_BLOCK = 512;
  size_t allowed = (allowedBlocks * BYTES_PER_BLOCK * 4) / 5;
  if (size < allowed) {
    return malloc(size);
  }
  return nullptr;
}
#else
// Other systems also allow over-allocation, but this malloc-and-free approach
// seems to be working for them.
static void* Allowed(size_t size) {
  return malloc(size);
}
#endif  // _AIX

void EnsureAllocation(const v8::FunctionCallbackInfo<v8::Value> &args) {
  v8::Isolate* isolate = args.GetIsolate();
  uintptr_t size = args[0].As<v8::Integer>()->Value();
  v8::Local<v8::Boolean> success;

  void* buffer = Allowed(size);
  if (buffer) {
    success = v8::Boolean::New(isolate, true);
    free(buffer);
  } else {
    success = v8::Boolean::New(isolate, false);
  }
  args.GetReturnValue().Set(success);
}

void init(v8::Local<v8::Object> exports) {
  NODE_SET_METHOD(exports, "ensureAllocation", EnsureAllocation);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)