Example #1
0
 static void* reallocate(void* pointer, CFIndex newSize, CFOptionFlags, void*)
 {
     size_t newAllocationSize = sizeof(StringImpl*) + newSize;
     StringImpl** header = static_cast<StringImpl**>(pointer) - 1;
     ASSERT(!*header);
     header = static_cast<StringImpl**>(fastRealloc(header, newAllocationSize));
     return header + 1;
 }
Example #2
0
uint8_t* ArgumentEncoder::grow(unsigned alignment, size_t size)
{
    size_t alignedSize = roundUpToAlignment(m_bufferSize, alignment);
    
    if (alignedSize + size > m_bufferCapacity) {
        size_t newCapacity = std::max(alignedSize + size, std::max(static_cast<size_t>(32), m_bufferCapacity + m_bufferCapacity / 4 + 1));
        if (!m_buffer)
            m_buffer = static_cast<uint8_t*>(fastMalloc(newCapacity));
        else
            m_buffer = static_cast<uint8_t*>(fastRealloc(m_buffer, newCapacity));
        
        // FIXME: What should we do if allocating memory fails?

        m_bufferCapacity = newCapacity;        
    }

    m_bufferSize = alignedSize + size;
    m_bufferPointer = m_buffer + alignedSize + size;
    
    return m_buffer + alignedSize;
}
Example #3
0
PassOwnPtr<GenericCompressedData> GenericCompressedData::create(const uint8_t* data, size_t dataLength)
{
    enum { MinimumSize = sizeof(GenericCompressedData) * 8 };

    if (!data || dataLength < MinimumSize)
        return nullptr;

    z_stream stream;
    memset(&stream, 0, sizeof(stream));
    stream.zalloc = zAlloc;
    stream.zfree = zFree;
    stream.data_type = Z_BINARY;
    stream.opaque = Z_NULL;
    stream.avail_in = dataLength;
    stream.next_in = const_cast<uint8_t*>(data);

    size_t currentOffset = OBJECT_OFFSETOF(GenericCompressedData, m_data);
    size_t currentCapacity = fastMallocGoodSize(MinimumSize);
    Bytef* compressedData = static_cast<Bytef*>(fastMalloc(currentCapacity));
    memset(compressedData, 0, sizeof(GenericCompressedData));
    stream.next_out = compressedData + currentOffset;
    stream.avail_out = currentCapacity - currentOffset;

    deflateInit(&stream, Z_BEST_COMPRESSION);

    while (true) {
        int deflateResult = deflate(&stream, Z_FINISH);
        if (deflateResult == Z_OK || !stream.avail_out) {
            size_t newCapacity = 0;
            currentCapacity -= stream.avail_out;
            if (!stream.avail_in)
                newCapacity = currentCapacity + 8;
            else {
                // Determine average capacity
                size_t compressedContent = stream.next_in - data;
                double expectedSize = static_cast<double>(dataLength) * compressedContent / currentCapacity;

                // Expand capacity by at least 8 bytes so we're always growing, and to
                // compensate for any exaggerated ideas of how effectively we'll compress
                // data in the future.
                newCapacity = std::max(static_cast<size_t>(expectedSize + 8), currentCapacity + 8);
            }
            newCapacity = fastMallocGoodSize(newCapacity);
            if (newCapacity >= dataLength)
                goto fail;
            compressedData = static_cast<Bytef*>(fastRealloc(compressedData, newCapacity));
            currentOffset = currentCapacity - stream.avail_out;
            stream.next_out = compressedData + currentOffset;
            stream.avail_out = newCapacity - currentCapacity;
            currentCapacity = newCapacity;
            continue;
        }

        if (deflateResult == Z_STREAM_END) {
            ASSERT(!stream.avail_in);
            break;
        }

        ASSERT_NOT_REACHED();
    fail:
        deflateEnd(&stream);
        fastFree(compressedData);
        return nullptr;
    }
    deflateEnd(&stream);
    static int64_t totalCompressed = 0;
    static int64_t totalInput = 0;

    totalCompressed += currentCapacity;
    totalInput += dataLength;
    GenericCompressedData* result = new (compressedData) GenericCompressedData(dataLength, stream.total_out);
    return adoptPtr(result);
}
Example #4
0
void* tryFastRealloc(void* p, size_t n)
{
    MemoryAllocationCanFail canFail;
    return fastRealloc(p, n);
}