"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Memory.cpp" (10 Oct 2018, 2003 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Memory.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18_Source_vs_1.19_Source.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "Common/Tcdefs.h"
   14 #include "Memory.h"
   15 #include "Exception.h"
   16 #include <stdlib.h>
   17 
   18 namespace VeraCrypt
   19 {
   20     void *Memory::Allocate (std::size_t size)
   21     {
   22         if (size < 1)
   23             throw ParameterIncorrect (SRC_POS);
   24 
   25         void *bufPtr = malloc (size);
   26         if (!bufPtr)
   27             throw bad_alloc();
   28 
   29         return bufPtr;
   30     }
   31     
   32     void *Memory::AllocateAligned (std::size_t size, std::size_t alignment)
   33     {
   34         if (size < 1)
   35             throw ParameterIncorrect (SRC_POS);
   36 #ifdef TC_WINDOWS
   37         void *bufPtr = _aligned_malloc (size, alignment);
   38 #else
   39         void *bufPtr = NULL;
   40         if (0 != posix_memalign (&bufPtr, alignment, size))
   41             bufPtr = NULL;
   42 #endif
   43         if (!bufPtr)
   44             throw bad_alloc();
   45 
   46         return bufPtr;
   47     }
   48 
   49     int Memory::Compare (const void *memory1, size_t size1, const void *memory2, size_t size2)
   50     {
   51         if (size1 > size2)
   52             return 1;
   53         else if (size1 < size2)
   54             return -1;
   55 
   56         return memcmp (memory1, memory2, size1);
   57     }
   58 
   59     void Memory::Copy (void *memoryDestination, const void *memorySource, size_t size)
   60     {
   61         assert (memoryDestination != nullptr && memorySource != nullptr);
   62         memcpy (memoryDestination, memorySource, size);
   63     }
   64 
   65     void Memory::Erase (void *memory, size_t size)
   66     {
   67         burn (memory, size);
   68     }
   69 
   70     void Memory::Zero (void *memory, size_t size)
   71     {
   72         memset (memory, 0, size);
   73     }
   74 
   75     void Memory::Free (void *memory)
   76     {
   77         assert (memory != nullptr);
   78         free (memory);
   79     }
   80     
   81     void Memory::FreeAligned (void *memory)
   82     {
   83         assert (memory != nullptr);
   84 #ifdef TC_WINDOWS
   85         _aligned_free (memory);
   86 #else
   87         free (memory);
   88 #endif
   89     }
   90 }