1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_SharedMemory.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 24156 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_OS_OS_SHAREDMEMORY_H_
17 #define NN_OS_OS_SHAREDMEMORY_H_
18 
19 #include <nn/config.h>
20 #if NN_PLATFORM_HAS_MMU
21 
22 #include <nn/os/os_MemoryBlockBase.h>
23 #include <nn/os/os_HandleObject.h>
24 
25 #ifdef __cplusplus
26 
27 namespace nn{ namespace os{
28 
29     namespace detail
30     {
31         void InitializeSharedMemory();
32         uptr AllocateFromSharedMemorySpace(MemoryBlockBase* p, size_t s);
33         void FreeToSharedMemorySpace(MemoryBlockBase* p);
34     }
35 
36 class SharedMemoryBlock : public MemoryBlockBase, public HandleObject
37 {
38 public:
SharedMemoryBlock()39     SharedMemoryBlock() : m_SpaceAllocated(false) {}
40     explicit SharedMemoryBlock(size_t size, bool readOnly = false, bool otherReadOnly = false, bool noMap = false) { Initialize(size, otherReadOnly, readOnly, noMap); }
41 
42     void Initialize(size_t size, bool readOnly = false, bool otherReadOnly = false, bool noMap = false);
43 
44     Result TryInitialize(size_t size, bool readOnly = false, bool otherReadOnly = false, bool noMap = false);
45 
~SharedMemoryBlock()46     ~SharedMemoryBlock() { Finalize(); }
47 
48     void Finalize();
49 
50 
51 private:
52 
53     friend class HandleManager;
54 
55     void AttachAndMap(Handle handle, size_t size, bool readOnly);
56     void Map(size_t size, bool readOnly);
57     void Unmap(void);
58 
59 private:
60     bool    m_SpaceAllocated;
61     NN_PADDING3;
62     NN_PADDING4;
63 };
64 
65 }} // namespace nn::os
66 
67 #endif
68 
69 // 以下、C 用宣言
70 
71 #include <nn/util/detail/util_CLibImpl.h>
72 
73 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosSharedMemoryBlock, nn::os::SharedMemoryBlock, 32, u32);
74 NN_EXTERN_C void nnosSharedMemoryBlockAllocate(nnosSharedMemoryBlock* this_, size_t size, bool readOnly, bool otherReadOnly, bool noMap);
75 NN_EXTERN_C void nnosSharedMemoryBlockInitializeNoAllocate(nnosSharedMemoryBlock* this_);
76 NN_EXTERN_C void nnosSharedMemoryBlockFree(nnosSharedMemoryBlock* this_);
77 NN_EXTERN_C uptr nnosSharedMemoryBlockGetAddress(nnosSharedMemoryBlock* this_);
78 NN_EXTERN_C size_t nnosSharedMemoryBlockGetSize(nnosSharedMemoryBlock* this_);
79 NN_EXTERN_C bool nnosSharedMemoryBlockIsReadOnly(nnosSharedMemoryBlock* this_);
80 
81 #endif  // if NN_PLATFORM_HAS_MMU
82 #endif /* NN_OS_OS_SHAREDMEMORY_H_ */
83