1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_HEAP_MEMORY_MEASUREMENT_INL_H_
6#define V8_HEAP_MEMORY_MEASUREMENT_INL_H_
7
8#include "src/heap/memory-measurement.h"
9#include "src/objects/contexts-inl.h"
10#include "src/objects/contexts.h"
11#include "src/objects/instance-type-inl.h"
12#include "src/objects/instance-type.h"
13#include "src/objects/map-inl.h"
14#include "src/objects/map.h"
15
16namespace v8 {
17namespace internal {
18
19bool NativeContextInferrer::Infer(Isolate* isolate, Map map, HeapObject object,
20                                  Address* native_context) {
21  switch (map.visitor_id()) {
22    case kVisitContext:
23      return InferForContext(isolate, Context::cast(object), native_context);
24    case kVisitNativeContext:
25      *native_context = object.ptr();
26      return true;
27    case kVisitJSFunction:
28      return InferForJSFunction(isolate, JSFunction::cast(object),
29                                native_context);
30    case kVisitJSApiObject:
31    case kVisitJSArrayBuffer:
32    case kVisitJSFinalizationRegistry:
33    case kVisitJSObject:
34    case kVisitJSObjectFast:
35    case kVisitJSTypedArray:
36    case kVisitJSWeakCollection:
37      return InferForJSObject(isolate, map, JSObject::cast(object),
38                              native_context);
39    default:
40      return false;
41  }
42}
43
44V8_INLINE bool NativeContextStats::HasExternalBytes(Map map) {
45  InstanceType instance_type = map.instance_type();
46  return (instance_type == JS_ARRAY_BUFFER_TYPE ||
47          InstanceTypeChecker::IsExternalString(instance_type));
48}
49
50V8_INLINE void NativeContextStats::IncrementSize(Address context, Map map,
51                                                 HeapObject object,
52                                                 size_t size) {
53  size_by_context_[context] += size;
54  if (HasExternalBytes(map)) {
55    IncrementExternalSize(context, map, object);
56  }
57}
58
59}  // namespace internal
60}  // namespace v8
61
62#endif  // V8_HEAP_MEMORY_MEASUREMENT_INL_H_
63