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 
21 NLIB_NAMESPACE_BEGIN
22 
24  public:
25  DynamicAlignedStorage() NLIB_NOEXCEPT : storage_(NULL) {}
26  NLIB_MOVE_MEMBER_HELPER_1(DynamicAlignedStorage, storage_)
27  errno_t Init(size_t size, size_t align) NLIB_NOEXCEPT {
28  if (align == 0) return EINVAL;
29  if ((align & (align - 1)) != 0) return EINVAL;
30 #ifdef NN_PLATFORM_CTR
31  void* p = nlib_malloc(size + align);
32 #elif defined(_MSC_VER)
33  void* p = _aligned_malloc(size, align);
34 #else
35  void* p = nlib_memalign(align, size);
36 #endif
37  if (!p) return ENOMEM;
38  storage_ = p;
39 #ifdef NN_PLATFORM_CTR
40  align_ = align;
41 #endif
42  return 0;
43  }
45  void* tmp = rhs.storage_;
46  rhs.storage_ = storage_;
47  storage_ = tmp;
48  }
50 #ifdef _MSC_VER
51  _aligned_free(storage_);
52 #else
53  nlib_free(storage_);
54 #endif
55  }
56  void* Get() NLIB_NOEXCEPT {
57 #ifndef NN_PLATFORM_CTR
58  return storage_;
59 #else
60  uintptr_t pos = (reinterpret_cast<uintptr_t>(storage_) + (align_ - 1)) & ~(align_ - 1);
61  return reinterpret_cast<void*>(pos);
62 #endif
63  }
64 
65  private:
66  void* storage_;
67 #ifdef NN_PLATFORM_CTR
68  size_t align_;
69 #endif
71 };
72 
75  lhs.swap(rhs);
76 }
77 
78 NLIB_NAMESPACE_END
79 
80 #endif // INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:97
NLIB_CHECK_RESULT 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:163
~DynamicAlignedStorage() noexcept
Destructor.
A class for obtaining aligned memory.
NLIB_CHECK_RESULT void * nlib_malloc(size_t size)
A weak function that calls the C standard function malloc. nlib calls malloc via this function...
void swap(DynamicAlignedStorage &rhs) noexcept
Swaps objects.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
void * Get() noexcept
Returns a pointer to the allocated region.
A file that contains the configuration information for each development environment.
DynamicAlignedStorage() noexcept
Instantiates the object with default parameters (default 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