OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "config.h" | 27 #include "config.h" |
28 #include "wtf/ArrayBufferContents.h" | 28 #include "wtf/ArrayBufferContents.h" |
29 | 29 |
30 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
31 #include "wtf/PartitionAlloc.h" | 31 #include "wtf/PartitionAlloc.h" |
32 #include "wtf/Partitions.h" | 32 #include "wtf/Partitions.h" |
33 #include <string.h> | 33 #include <string.h> |
34 | 34 |
35 namespace WTF { | 35 namespace WTF { |
36 | 36 |
| 37 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun
tOfExternalAllocatedMemoryFunction; |
| 38 |
37 ArrayBufferContents::ArrayBufferContents() | 39 ArrayBufferContents::ArrayBufferContents() |
38 : m_data(0) | 40 : m_data(0) |
39 , m_sizeInBytes(0) | 41 , m_sizeInBytes(0) { } |
40 , m_deallocationObserver(0) { } | |
41 | 42 |
42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) | 43 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) |
43 : m_data(0) | 44 : m_data(0) |
44 , m_sizeInBytes(0) | 45 , m_sizeInBytes(0) |
45 , m_deallocationObserver(0) | |
46 { | 46 { |
47 // Do not allow 32-bit overflow of the total size. | 47 // Do not allow 32-bit overflow of the total size. |
48 if (numElements) { | 48 if (numElements) { |
49 unsigned totalSize = numElements * elementByteSize; | 49 unsigned totalSize = numElements * elementByteSize; |
50 if (totalSize / numElements != elementByteSize) { | 50 if (totalSize / numElements != elementByteSize) { |
51 m_data = 0; | 51 m_data = 0; |
52 return; | 52 return; |
53 } | 53 } |
54 } | 54 } |
55 allocateMemory(numElements * elementByteSize, policy, m_data); | 55 allocateMemory(numElements * elementByteSize, policy, m_data); |
56 m_sizeInBytes = numElements * elementByteSize; | 56 m_sizeInBytes = numElements * elementByteSize; |
57 } | 57 } |
58 | 58 |
59 ArrayBufferContents::ArrayBufferContents( | 59 ArrayBufferContents::ArrayBufferContents(void* data, unsigned sizeInBytes) |
60 void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer) | |
61 : m_data(data) | 60 : m_data(data) |
62 , m_sizeInBytes(sizeInBytes) | 61 , m_sizeInBytes(sizeInBytes) |
63 , m_deallocationObserver(observer) | |
64 { | 62 { |
65 if (!m_data) { | 63 if (!m_data) { |
66 ASSERT(!m_sizeInBytes); | 64 ASSERT(!m_sizeInBytes); |
67 m_sizeInBytes = 0; | 65 m_sizeInBytes = 0; |
68 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. | 66 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. |
69 // (partitionAllocGeneric guarantees valid pointer for size 0) | 67 // (partitionAllocGeneric guarantees valid pointer for size 0) |
70 allocateMemory(0, ZeroInitialize, m_data); | 68 allocateMemory(0, ZeroInitialize, m_data); |
71 } | 69 } |
72 } | 70 } |
73 | 71 |
74 ArrayBufferContents::~ArrayBufferContents() | 72 ArrayBufferContents::~ArrayBufferContents() |
75 { | 73 { |
76 freeMemory(m_data, m_sizeInBytes); | 74 freeMemory(m_data, m_sizeInBytes); |
77 clear(); | 75 clear(); |
78 } | 76 } |
79 | 77 |
80 void ArrayBufferContents::clear() | 78 void ArrayBufferContents::clear() |
81 { | 79 { |
82 if (m_data && m_deallocationObserver) | |
83 m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes); | |
84 m_data = 0; | 80 m_data = 0; |
85 m_sizeInBytes = 0; | 81 m_sizeInBytes = 0; |
86 m_deallocationObserver = 0; | |
87 } | 82 } |
88 | 83 |
89 void ArrayBufferContents::transfer(ArrayBufferContents& other) | 84 void ArrayBufferContents::transfer(ArrayBufferContents& other) |
90 { | 85 { |
91 ASSERT(!other.m_data); | 86 ASSERT(!other.m_data); |
92 other.m_data = m_data; | 87 other.m_data = m_data; |
93 other.m_sizeInBytes = m_sizeInBytes; | 88 other.m_sizeInBytes = m_sizeInBytes; |
94 clear(); | 89 clear(); |
95 } | 90 } |
96 | 91 |
97 void ArrayBufferContents::copyTo(ArrayBufferContents& other) | 92 void ArrayBufferContents::copyTo(ArrayBufferContents& other) |
98 { | 93 { |
99 ASSERT(!other.m_sizeInBytes); | 94 ASSERT(!other.m_sizeInBytes); |
100 other.freeMemory(other.m_data, other.m_sizeInBytes); | 95 other.freeMemory(other.m_data, other.m_sizeInBytes); |
101 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); | 96 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); |
102 if (!other.m_data) | 97 if (!other.m_data) |
103 return; | 98 return; |
104 memcpy(other.m_data, m_data, m_sizeInBytes); | 99 memcpy(other.m_data, m_data, m_sizeInBytes); |
105 other.m_sizeInBytes = m_sizeInBytes; | 100 other.m_sizeInBytes = m_sizeInBytes; |
106 } | 101 } |
107 | 102 |
108 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) | 103 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) |
109 { | 104 { |
| 105 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
| 106 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); |
110 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par
titionAllocReturnNull, size); | 107 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par
titionAllocReturnNull, size); |
111 if (policy == ZeroInitialize && data) | 108 if (policy == ZeroInitialize && data) |
112 memset(data, '\0', size); | 109 memset(data, '\0', size); |
113 } | 110 } |
114 | 111 |
115 void ArrayBufferContents::freeMemory(void* data, size_t) | 112 void ArrayBufferContents::freeMemory(void* data, size_t size) |
116 { | 113 { |
117 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); | 114 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); |
| 115 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
| 116 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size))
; |
118 } | 117 } |
119 | 118 |
120 } // namespace WTF | 119 } // namespace WTF |
OLD | NEW |