nlib
DynamicAlignedStorage.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
17 #define INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 
25  public:
27 #ifdef __cpp_rvalue_references
28  DynamicAlignedStorage(DynamicAlignedStorage&& rhs) NLIB_NOEXCEPT : storage_(rhs.storage_) {
29  rhs.storage_ = nullptr;
30  }
32  storage_ = rhs.storage_;
33  rhs.storage_ = nullptr;
34  return *this;
35  }
36 #endif
38 #ifdef NN_PLATFORM_CTR
39  : storage_(rhs.storage_),
40  align_(rhs.align_)
41 #else
42  : storage_(rhs.storage_)
43 #endif
44  {
45  rhs.storage_ = nullptr;
46  }
48 #ifdef NN_PLATFORM_CTR
49  align_ = rhs.align_;
50 #endif
51  storage_ = rhs.storage_;
52  rhs.storage_ = nullptr;
53  return *this;
54  }
55  errno_t Init(size_t size, size_t align) NLIB_NOEXCEPT {
56  if (align == 0) return EINVAL;
57  if ((align & (align - 1)) != 0) return EINVAL;
58 #ifdef NN_PLATFORM_CTR
59  void* p = nlib_malloc(size + align);
60 #elif defined(_MSC_VER)
61  void* p = _aligned_malloc(size, align);
62 #else
63  void* p = nlib_memalign(align, size);
64 #endif
65  if (!p) return ENOMEM;
66  storage_ = p;
67 #ifdef NN_PLATFORM_CTR
68  align_ = align;
69 #endif
70  return 0;
71  }
73 #ifdef _MSC_VER
74  _aligned_free(storage_);
75 #else
76  nlib_free(storage_);
77 #endif
78  }
79  void* Get() NLIB_NOEXCEPT {
80 #ifndef NN_PLATFORM_CTR
81  return storage_;
82 #else
83  uintptr_t pos = (reinterpret_cast<uintptr_t>(storage_) + (align_ - 1)) & ~(align_ - 1);
84  return reinterpret_cast<void*>(pos);
85 #endif
86  }
87 
88  private:
89  void* storage_;
90 #ifdef NN_PLATFORM_CTR
91  size_t align_;
92 #endif
94 };
95 
96 NLIB_NAMESPACE_END
97 
98 NLIB_DEFINE_STD_SWAP(NLIB_NS::DynamicAlignedStorage)
99 
100 #endif // INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
DynamicAlignedStorage & operator=(DynamicAlignedStorage &&rhs) noexcept
Move assignment operator.
DynamicAlignedStorage(DynamicAlignedStorage &rhs, move_tag) noexcept
Corresponds to a move constructor.
void * nlib_memalign(size_t alignment, size_t size)
A weak function that calls the C standard function memalign. nlib calls memalign via this function...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
errno_t Init(size_t size, size_t align) noexcept
Allocates memory.
DynamicAlignedStorage & assign(DynamicAlignedStorage &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
~DynamicAlignedStorage() noexcept
Destructor.
A class for obtaining aligned memory.
void * nlib_malloc(size_t size)
A weak function that calls the C standard function malloc. nlib calls malloc via this function...
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:270
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
void * Get() noexcept
Returns a pointer to the allocated region.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
A file that contains the configuration information for each development environment.
constexpr DynamicAlignedStorage() noexcept
Instantiates the object with default parameters (default constructor).
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
DynamicAlignedStorage(DynamicAlignedStorage &&rhs) noexcept
Instantiates the object (move constructor).
void nlib_free(void *ptr)
A weak function that calls the C standard function free. nlib calls free via this function...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37