use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-vector.cpp
index fec1b2a..4acbe52 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ namespace Dali
 {
 
 VectorBase::VectorBase()
-  : mData( NULL )
+  : mData( nullptr )
 {
 }
 
@@ -51,10 +51,9 @@ void VectorBase::Release()
   {
     // adjust pointer to real beginning
     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
-    // TODO would be nice to memset to a bitpattern to catch illegal use of container after release
-    // but that would require knowledge of the itemsize
-    free( metadata - 2u );
-    mData = 0u;
+
+    delete [] ( metadata - 2u );
+    mData = nullptr;
   }
 }
 
@@ -75,7 +74,9 @@ void VectorBase::Reserve( SizeType capacity, SizeType elementSize )
   if( capacity > oldCapacity )
   {
     const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
-    void* wholeData = (void*)malloc( wholeAllocation );
+    void* wholeData = reinterpret_cast< void* >( new uint8_t[ wholeAllocation ] );
+    DALI_ASSERT_ALWAYS( wholeData && "VectorBase::Reserve - Memory allocation failed" );
+
 #if defined( DEBUG_ENABLED )
     // in debug build this will help identify a vector of uninitialized data
     memset( wholeData, 0xaa, wholeAllocation );
@@ -119,8 +120,8 @@ void VectorBase::Erase( char* address, SizeType elementSize )
   // erase can be called on an unallocated vector
   if( mData )
   {
-    char* startAddress = address + elementSize;
-    const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
+    uint8_t* startAddress = reinterpret_cast< uint8_t* >( address ) + elementSize;
+    const uint8_t* endAddress = reinterpret_cast< uint8_t* >( mData ) + Count() * elementSize;
     SizeType numberOfBytes = endAddress - startAddress;
     // addresses overlap so use memmove
     memmove( address, startAddress, numberOfBytes );
@@ -130,12 +131,12 @@ void VectorBase::Erase( char* address, SizeType elementSize )
 
 char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
 {
-  char* next = NULL;
+  char* next = nullptr;
 
   if( mData )
   {
-    char* startAddress = last;
-    const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
+    uint8_t* startAddress = reinterpret_cast< uint8_t* >( last );
+    const uint8_t* endAddress = reinterpret_cast< uint8_t* >( mData ) + Count() * elementSize;
     SizeType numberOfBytes = endAddress - startAddress;
     // addresses overlap so use memmove
     memmove( first, startAddress, numberOfBytes );