version 1.2, 2015/04/20 18:10:13
|
version 1.3, 2015/04/20 18:19:53
|
|
|
#include "linkage.h" | #include "linkage.h" |
#include "atomic.h" | #include "atomic.h" |
#include "memory.h" | #include "memory.h" |
#include "string.h" |
#include "micxx_string.h" |
#include "field.h" | #include "field.h" |
#include "array.h" | #include "array.h" |
#include "types.h" | #include "types.h" |
|
|
| |
Instance& operator=(const Instance& x); | Instance& operator=(const Instance& x); |
| |
|
inline MI_Instance* GetInstance() const |
|
{ |
|
return m_instance; |
|
} |
|
|
String GetNamespace() const; | String GetNamespace() const; |
| |
void SetNamespace(const String& nameSpace); | void SetNamespace(const String& nameSpace); |
|
|
| |
static const MI_ClassDecl* GetClassDecl(); | static const MI_ClassDecl* GetClassDecl(); |
| |
|
// Sets whether to use copy-on-write behavior. This function is |
|
// experimental (as indicated by the leading double underscore). |
|
void __setCopyOnWrite(bool flag) |
|
{ |
|
GetHeader(m_instance)->u.s.m_disableCopyOnWrite = flag ? 0 : 1; |
|
} |
|
|
|
// Gets the copy-on-write flag from the MI_Instance header. |
|
bool __getCopyOnWrite() const |
|
{ |
|
return GetHeader(m_instance)->u.s.m_disableCopyOnWrite ? false : true; |
|
} |
|
|
|
// Forces a copy-on-write operation (if refrence count is not 1). After |
|
// calling this function, the instance will have a pointer to its own |
|
// copy of MI_Instance whose reference count is 1. |
|
void __forceCopyOnWrite() |
|
{ |
|
Header* header = GetHeader(m_instance); |
|
|
|
// Avoid the copy on write if sharing has been enabled. |
|
|
|
if (AtomicGet(header->u.s.m_refCounter) != 1) |
|
{ |
|
MI_Instance* current = m_instance; |
|
m_instance = Clone(); |
|
AddRef(m_instance); |
|
Release(current); |
|
} |
|
} |
|
|
protected: | protected: |
| |
Instance( | Instance( |
|
|
| |
void COW() | void COW() |
{ | { |
if (AtomicGet(GetHeader(m_instance)->u.s.m_refCounter) != 1) |
Header* header = GetHeader(m_instance); |
|
|
|
// Avoid the copy on write if sharing has been enabled. |
|
|
|
if (header->u.s.m_disableCopyOnWrite == 0 && |
|
AtomicGet(header->u.s.m_refCounter) != 1) |
{ | { |
MI_Instance* current = m_instance; | MI_Instance* current = m_instance; |
m_instance = Clone(); | m_instance = Clone(); |
|
|
| |
private: | private: |
| |
// helper data - placed before MI_Instance strucutre |
// helper data - placed before MI_Instance structure |
struct Header | struct Header |
{ | { |
union | union |
|
|
struct _HeaderData | struct _HeaderData |
{ | { |
AtomicType m_refCounter; | AtomicType m_refCounter; |
|
unsigned int m_disableCopyOnWrite; |
} | } |
s; | s; |
char alignment[(sizeof(struct _HeaderData) + 7) & ~7]; |
char alignment[(sizeof(struct _HeaderData) + 15) & ~15]; |
} | } |
u; | u; |
}; | }; |