blob: a89f14957635e59c120cb1e6155f27db65c6341a [file] [log] [blame]
Chris Lattnerb3649db2009-12-16 08:44:24 +00001//===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SmallVector class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/SmallVector.h"
15using namespace llvm;
16
17/// grow_pod - This is an implementation of the grow() method which only works
18/// on POD-like datatypes and is out of line to reduce code duplication.
19void SmallVectorBase::grow_pod(size_t MinSizeInBytes, size_t TSize) {
20 size_t CurSizeBytes = size_in_bytes();
John McCall2a9a2db2010-09-02 21:55:03 +000021 size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
Chris Lattnerb3649db2009-12-16 08:44:24 +000022 if (NewCapacityInBytes < MinSizeInBytes)
23 NewCapacityInBytes = MinSizeInBytes;
Benjamin Kramerd164d3d2010-06-08 11:44:30 +000024
25 void *NewElts;
26 if (this->isSmall()) {
27 NewElts = malloc(NewCapacityInBytes);
28
29 // Copy the elements over. No need to run dtors on PODs.
30 memcpy(NewElts, this->BeginX, CurSizeBytes);
31 } else {
32 // If this wasn't grown from the inline copy, grow the allocated space.
33 NewElts = realloc(this->BeginX, NewCapacityInBytes);
34 }
35
Chris Lattnerb3649db2009-12-16 08:44:24 +000036 this->EndX = (char*)NewElts+CurSizeBytes;
37 this->BeginX = NewElts;
38 this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
39}
40