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
29  : storage_(rhs.storage_) {
30  rhs.storage_ = nullptr;
31  }
33  storage_ = rhs.storage_;
34  rhs.storage_ = nullptr;
35  return *this;
36  }
37 #endif
39 #ifdef NN_PLATFORM_CTR
40  : storage_(rhs.storage_), 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  }
56  void* tmp = rhs.storage_;
57  rhs.storage_ = storage_;
58  storage_ = tmp;
59  }
60  errno_t Init(size_t size, size_t align) NLIB_NOEXCEPT {
61  if (align == 0) return EINVAL;
62  if ((align & (align - 1)) != 0) return EINVAL;
63 #ifdef NN_PLATFORM_CTR
64  void* p = nlib_malloc(size + align);
65 #elif defined(_MSC_VER)
66  void* p = _aligned_malloc(size, align);
67 #else
68  void* p = nlib_memalign(align, size);
69 #endif
70  if (!p) return ENOMEM;
71  storage_ = p;
72 #ifdef NN_PLATFORM_CTR
73  align_ = align;
74 #endif
75  return 0;
76  }
78 #ifdef _MSC_VER
79  _aligned_free(storage_);
80 #else
81  nlib_free(storage_);
82 #endif
83  }
84  void* Get() NLIB_NOEXCEPT {
85 #ifndef NN_PLATFORM_CTR
86  return storage_;
87 #else
88  uintptr_t pos = (reinterpret_cast<uintptr_t>(storage_) + (align_ - 1)) & ~(align_ - 1);
89  return reinterpret_cast<void*>(pos);
90 #endif
91  }
92 
93  private:
94  void* storage_;
95 #ifdef NN_PLATFORM_CTR
96  size_t align_;
97 #endif
99 };
100 
101 NLIB_NAMESPACE_END
102 
103 NLIB_DEFINE_STD_SWAP(NLIB_NS::DynamicAlignedStorage)
104 
105 #endif // INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
DynamicAlignedStorage & operator=(DynamicAlignedStorage &&rhs) noexcept
Move assignment operator. This function is useful when using C++11.
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:179
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.
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
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:265
void swap(DynamicAlignedStorage &rhs) noexcept
Swaps objects.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
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:107
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:245
DynamicAlignedStorage(DynamicAlignedStorage &&rhs) noexcept
Instantiates the object (move constructor). This function is useful when using C++11.
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