xref: /aosp_15_r20/external/llvm/lib/Support/SmallVector.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the SmallVector class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
15*9880d681SAndroid Build Coastguard Worker using namespace llvm;
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker /// grow_pod - This is an implementation of the grow() method which only works
18*9880d681SAndroid Build Coastguard Worker /// on POD-like datatypes and is out of line to reduce code duplication.
grow_pod(void * FirstEl,size_t MinSizeInBytes,size_t TSize)19*9880d681SAndroid Build Coastguard Worker void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,
20*9880d681SAndroid Build Coastguard Worker                                size_t TSize) {
21*9880d681SAndroid Build Coastguard Worker   size_t CurSizeBytes = size_in_bytes();
22*9880d681SAndroid Build Coastguard Worker   size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
23*9880d681SAndroid Build Coastguard Worker   if (NewCapacityInBytes < MinSizeInBytes)
24*9880d681SAndroid Build Coastguard Worker     NewCapacityInBytes = MinSizeInBytes;
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker   void *NewElts;
27*9880d681SAndroid Build Coastguard Worker   if (BeginX == FirstEl) {
28*9880d681SAndroid Build Coastguard Worker     NewElts = malloc(NewCapacityInBytes);
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker     // Copy the elements over.  No need to run dtors on PODs.
31*9880d681SAndroid Build Coastguard Worker     memcpy(NewElts, this->BeginX, CurSizeBytes);
32*9880d681SAndroid Build Coastguard Worker   } else {
33*9880d681SAndroid Build Coastguard Worker     // If this wasn't grown from the inline copy, grow the allocated space.
34*9880d681SAndroid Build Coastguard Worker     NewElts = realloc(this->BeginX, NewCapacityInBytes);
35*9880d681SAndroid Build Coastguard Worker   }
36*9880d681SAndroid Build Coastguard Worker   assert(NewElts && "Out of memory");
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker   this->EndX = (char*)NewElts+CurSizeBytes;
39*9880d681SAndroid Build Coastguard Worker   this->BeginX = NewElts;
40*9880d681SAndroid Build Coastguard Worker   this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
41*9880d681SAndroid Build Coastguard Worker }
42