Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2013 Google Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "wtf/ArrayBufferContents.h"
     29 
     30 #include "wtf/Assertions.h"
     31 #include "wtf/PartitionAlloc.h"
     32 #include "wtf/WTF.h"
     33 #include <string.h>
     34 
     35 namespace WTF {
     36 
     37 ArrayBufferContents::ArrayBufferContents()
     38     : m_data(0)
     39     , m_sizeInBytes(0)
     40     , m_deallocationObserver(0) { }
     41 
     42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
     43     : m_data(0)
     44     , m_sizeInBytes(0)
     45     , m_deallocationObserver(0)
     46 {
     47     // Do not allow 32-bit overflow of the total size.
     48     if (numElements) {
     49         unsigned totalSize = numElements * elementByteSize;
     50         if (totalSize / numElements != elementByteSize) {
     51             m_data = 0;
     52             return;
     53         }
     54     }
     55     allocateMemory(numElements * elementByteSize, policy, m_data);
     56     m_sizeInBytes = numElements * elementByteSize;
     57 }
     58 
     59 ArrayBufferContents::ArrayBufferContents(
     60     void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer)
     61     : m_data(data)
     62     , m_sizeInBytes(sizeInBytes)
     63     , m_deallocationObserver(observer)
     64 {
     65     if (!m_data) {
     66         ASSERT(!m_sizeInBytes);
     67         m_sizeInBytes = 0;
     68         // Allow null data if size is 0 bytes, make sure m_data is valid pointer.
     69         // (partitionAllocGeneric guarantees valid pointer for size 0)
     70         allocateMemory(0, ZeroInitialize, m_data);
     71     }
     72 }
     73 
     74 ArrayBufferContents::~ArrayBufferContents()
     75 {
     76     freeMemory(m_data, m_sizeInBytes);
     77     clear();
     78 }
     79 
     80 void ArrayBufferContents::clear()
     81 {
     82     if (m_data && m_deallocationObserver)
     83         m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes);
     84     m_data = 0;
     85     m_sizeInBytes = 0;
     86     m_deallocationObserver = 0;
     87 }
     88 
     89 void ArrayBufferContents::transfer(ArrayBufferContents& other)
     90 {
     91     ASSERT(!other.m_data);
     92     other.m_data = m_data;
     93     other.m_sizeInBytes = m_sizeInBytes;
     94     clear();
     95 }
     96 
     97 void ArrayBufferContents::copyTo(ArrayBufferContents& other)
     98 {
     99     ASSERT(!other.m_sizeInBytes);
    100     other.freeMemory(other.m_data, other.m_sizeInBytes);
    101     allocateMemory(m_sizeInBytes, DontInitialize, other.m_data);
    102     if (!other.m_data)
    103         return;
    104     memcpy(other.m_data, m_data, m_sizeInBytes);
    105     other.m_sizeInBytes = m_sizeInBytes;
    106 }
    107 
    108 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy policy, void*& data)
    109 {
    110     data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), PartitionAllocReturnNull, size);
    111     if (policy == ZeroInitialize && data)
    112         memset(data, '\0', size);
    113 }
    114 
    115 void ArrayBufferContents::freeMemory(void* data, size_t)
    116 {
    117     partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data);
    118 }
    119 
    120 } // namespace WTF
    121