blob: 74313151c76279b7cd73c9b926103adf7119cee8 [file] [log] [blame]
Chris Lattner4ea86c42009-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.
Richard Smith24f09cc2012-08-22 00:11:07 +000019void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,
20 size_t TSize) {
Chris Lattner4ea86c42009-12-16 08:44:24 +000021 size_t CurSizeBytes = size_in_bytes();
John McCall7f55c252010-09-02 21:55:03 +000022 size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
Chris Lattner4ea86c42009-12-16 08:44:24 +000023 if (NewCapacityInBytes < MinSizeInBytes)
24 NewCapacityInBytes = MinSizeInBytes;
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000025
26 void *NewElts;
Richard Smith24f09cc2012-08-22 00:11:07 +000027 if (BeginX == FirstEl) {
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000028 NewElts = malloc(NewCapacityInBytes);
Matthias Braunc20b3382017-07-20 01:30:39 +000029 if (NewElts == nullptr)
30 report_bad_alloc_error("Allocation of SmallVector element failed.");
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000031
32 // Copy the elements over. No need to run dtors on PODs.
33 memcpy(NewElts, this->BeginX, CurSizeBytes);
34 } else {
35 // If this wasn't grown from the inline copy, grow the allocated space.
36 NewElts = realloc(this->BeginX, NewCapacityInBytes);
Matthias Braunc20b3382017-07-20 01:30:39 +000037 if (NewElts == nullptr)
38 report_bad_alloc_error("Reallocation of SmallVector element failed.");
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000039 }
40
Chris Lattner4ea86c42009-12-16 08:44:24 +000041 this->EndX = (char*)NewElts+CurSizeBytes;
42 this->BeginX = NewElts;
43 this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
44}