summaryrefslogtreecommitdiff
path: root/src/slab_allocator.cc
blob: 0e643f8c9f70ec7e1dbeabd471da25b4dcd2461b (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "v8.h"
#include "node.h"
#include "node_buffer.h"
#include "slab_allocator.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Value;


namespace node {

SlabAllocator::SlabAllocator(unsigned int size) {
  size_ = ROUND_UP(size ? size : 1, 8192);
  initialized_ = false;
}


SlabAllocator::~SlabAllocator() {
  if (!initialized_) return;
  slab_sym_.Dispose();
  slab_sym_.Clear();
  slab_.Dispose();
  slab_.Clear();
}


void SlabAllocator::Initialize() {
  HandleScope scope;
  char sym[256];
  snprintf(sym, sizeof(sym), "slab_%p", this); // namespace object key
  offset_ = 0;
  last_ptr_ = NULL;
  initialized_ = true;
  slab_sym_ = Persistent<String>::New(String::New(sym));
}


static Local<Object> NewSlab(unsigned int size) {
  HandleScope scope;
  Local<Value> arg = Integer::NewFromUnsigned(ROUND_UP(size, 16));
  Local<Object> buf = Buffer::constructor_template
                      ->GetFunction()
                      ->NewInstance(1, &arg);
  return scope.Close(buf);
}


char* SlabAllocator::Allocate(Handle<Object> obj, unsigned int size) {
  HandleScope scope;

  assert(!obj.IsEmpty());

  if (size == 0) return NULL;
  if (!initialized_) Initialize();

  if (size > size_) {
    Local<Object> buf = NewSlab(size);
    obj->SetHiddenValue(slab_sym_, buf);
    return Buffer::Data(buf);
  }

  if (slab_.IsEmpty() || offset_ + size > size_) {
    slab_.Dispose();
    slab_.Clear();
    slab_ = Persistent<Object>::New(NewSlab(size_));
    offset_ = 0;
    last_ptr_ = NULL;
  }

  obj->SetHiddenValue(slab_sym_, slab_);
  last_ptr_ = Buffer::Data(slab_) + offset_;
  offset_ += size;

  return last_ptr_;
}


Local<Object> SlabAllocator::Shrink(Handle<Object> obj,
                                    char* ptr,
                                    unsigned int size) {
  HandleScope scope;
  Local<Value> slab_v = obj->GetHiddenValue(slab_sym_);
  obj->SetHiddenValue(slab_sym_, Null());
  assert(!slab_v.IsEmpty());
  assert(slab_v->IsObject());
  Local<Object> slab = slab_v->ToObject();
  assert(ptr != NULL);
  if (ptr == last_ptr_) {
    last_ptr_ = NULL;
    offset_ = ptr - Buffer::Data(slab) + ROUND_UP(size, 16);
  }
  return scope.Close(slab);
}


} // namespace node