Free Electron
RecordSB.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __data_RecordSB_h__
8 #define __data_RecordSB_h__
9 
10 namespace fe
11 {
12 
13 #define FE_RECORD_COUNT_DEBUG FALSE
14 
15 class LayoutSB;
16 
17 template <class T>
18 class Accessor;
19 
20 /* TODO:
21  private RecordArray data
22  remove RecordArray friend from Record
23  remove WeakRecord friend from Record
24  remove offset table exposure from layout
25 */
26 
27 /** @brief Reference to an instance of a Layout
28 
29  @ingroup data
30 
31  A record is simply a pointer to a Layout and a pointer to a
32  state block. A state block is a block of memory with Attributes laid
33  out as specified by a Layout object.
34  */
35 class FE_DL_EXPORT RecordSB
36 {
37  friend class WeakRecordSB;
38  friend class LayoutSB;
39  friend class RecordArraySB;
40  friend class SegmentStore;
41  public:
42  RecordSB(I32 ignored=0);
43  RecordSB(const RecordSB &other);
44 virtual ~RecordSB(void);
45 
46  RecordSB &operator=(const RecordSB &r_other);
47 
48  bool operator==(const RecordSB &r_other) const;
49  bool operator!=(const RecordSB &r_other) const;
50 
51  /** Return a unique runtime id (id-runtime, idr, since 'rid' has
52  other meaning. */
53  FE_UWORD idr(void) const;
54 
55  /** Return true if the Record points to a valid state block. */
56  bool isValid(void) const;
57  /** Return the Layout. */
58  sp<Layout> layout(void) const;
59 
60  bool extractInstance(Instance &instance, const String &attrName);
61 
62  RecordSB clone(void);
63  //RecordSB deepclone(void);
64 
65 
66  template <class T>
67  T &accessAttribute(FE_UWORD aLocator) const;
68 
69  void *rawAttribute(FE_UWORD aLocator) const;
70 
71  private:
72  /** Return the state block. */
73  void *data(void) const;
74  /** Return a raw pointer to the Layout. Mainly intended for Accessor
75  for speed. */
76  LayoutSB *rawLayout(void) const;
77 
78  private:
79  void acquire(void);
80  void release(void);
81  /** Set the state block. */
82  void set(void *datablock);
83 
84  private:
85  sp<LayoutSB> m_spLayout;
86  void *m_pStateBlock;
87 };
88 
89 struct hash_record_sb
90 { FE_UWORD operator()(const RecordSB r_rec) const
91  { return r_rec.idr() % 17191; } };
92 
93 inline bool operator<(const RecordSB &lhs, const RecordSB &rhs)
94 {
95  return (lhs.idr() < rhs.idr());
96 }
97 
98 class RecordSBInfo : public BaseType::Info
99 {
100  public:
101 virtual String print(void *instance);
102 virtual IWORD output(std::ostream &ostrm, void *instance, t_serialMode mode);
103 virtual void input(std::istream &istrm, void *instance, t_serialMode mode);
104 virtual IWORD iosize(void);
105 virtual bool getConstruct(void);
106 virtual void construct(void *instance);
107 virtual void destruct(void *instance);
108 };
109 
110 inline RecordSB::RecordSB(I32)
111 {
112  m_pStateBlock = NULL;
113 }
114 
115 inline RecordSB::RecordSB(const RecordSB &other)
116 {
117  m_spLayout = other.m_spLayout;
118  m_pStateBlock = other.m_pStateBlock;
119  acquire();
120 }
121 
122 /*
123 inline Record::Record(void *datablock)
124 {
125  m_pStateBlock = datablock;
126  acquire();
127  m_spLayout = FE_SB_TO_HDR(m_pStateBlock)->m_pStore->getLayout();
128 }
129 */
130 
131 inline RecordSB::~RecordSB(void)
132 {
133  release();
134 }
135 
136 inline LayoutSB *RecordSB::rawLayout(void) const
137 {
138  return m_spLayout.raw();
139 }
140 
141 inline RecordSB &RecordSB::operator=(const RecordSB &other)
142 {
143  if(this != &other)
144  {
145  release();
146  m_spLayout = other.m_spLayout;
147  m_pStateBlock = other.m_pStateBlock;
148  acquire();
149  }
150  return *this;
151 }
152 
153 inline bool RecordSB::operator==(const RecordSB &other) const
154 {
155  return (m_pStateBlock == other.m_pStateBlock);
156 }
157 
158 inline bool RecordSB::operator!=(const RecordSB &other) const
159 {
160  return (m_pStateBlock != other.m_pStateBlock);
161 }
162 
163 inline void *RecordSB::data(void) const
164 {
165  return m_pStateBlock;
166 }
167 
168 inline FE_UWORD RecordSB::idr(void) const
169 {
170  return reinterpret_cast<FE_UWORD>(m_pStateBlock);
171 }
172 
173 template <class T>
174 inline T &RecordSB::accessAttribute(FE_UWORD aLocator) const
175 {
176  return *((T *)((char *)(data()) + m_spLayout->offsetTable()[aLocator]));
177 }
178 
179 inline void *RecordSB::rawAttribute(FE_UWORD aLocator) const
180 {
181  return (void *)((char *)(data()) + m_spLayout->offsetTable()[aLocator]);
182 }
183 
184 inline bool RecordSB::isValid(void) const
185 {
186  return (m_pStateBlock != NULL);
187 }
188 
189 inline sp<Layout> RecordSB::layout(void) const
190 {
191  return m_spLayout;
192 }
193 
194 inline void RecordSB::set(void *datablock)
195 {
196  release();
197  m_pStateBlock = datablock;
198  acquire();
199  m_spLayout = FE_SB_TO_HDR(m_pStateBlock)->m_pStore->getLayout();
200 }
201 
202 inline void RecordSB::release(void)
203 {
204 #if FE_RECORD_COUNT_DEBUG
205  register FE_UWORD index;
206  if(m_spLayout.isValid())
207  {
208  m_spLayout->scope()->findAttribute(FE_C,index);
209  if(m_spLayout->offsetTable()[index] != offsetNone)
210  {
211  IWORD &count= *((IWORD *)((char *)(data()) +
212  layout()->offsetTable()[index]));
213 
214  fe_fprintf(stderr,"Release %p %d->%d %s\n",(U32)this,count,count-1,
215  m_spLayout->name().c_str());
216  }
217  }
218 #endif
219 
220  if(m_pStateBlock)
221  {
222 #if FE_COUNTED_TRACK
223  Counted::untrackReference(FE_SB_TO_HDR(m_pStateBlock),&m_pStateBlock);
224 // Counted::untrackReference(this,this);
225 // Counted::deregisterRegion(this);
226 #endif
227  FE_SB_TO_HDR(m_pStateBlock)->m_pStore->releaseSB(*this);
228  }
229 }
230 
231 inline void RecordSB::acquire(void)
232 {
233 #if FE_RECORD_COUNT_DEBUG
234  register FE_UWORD index;
235  if(m_spLayout.isValid())
236  {
237  m_spLayout->scope()->findAttribute(FE_C,index);
238  if(m_spLayout->offsetTable()[index] != offsetNone)
239  {
240  IWORD &count= *((IWORD *)((char *)(data()) +
241  m_spLayout->offsetTable()[index]));
242 
243  fe_fprintf(stderr,"Acquire %p %d->%d %s\n",(U32)this,count,count+1,
244  m_spLayout->name().c_str());
245  }
246  }
247 #endif
248 
249  if(m_pStateBlock)
250  {
251  FE_SB_TO_HDR(m_pStateBlock)->m_pStore->acquireSB(m_pStateBlock);
252 #if FE_COUNTED_TRACK
253 // Counted::registerRegion(this,sizeof(Record),"Record "+
254 // (m_spLayout.isValid()? m_spLayout->name(): "<invalid>"));
255 // Counted::trackReference(this,this);
256  Counted::trackReference(FE_SB_TO_HDR(m_pStateBlock),&m_pStateBlock,
257  "RecordSB");
258 #endif
259  }
260 }
261 
262 } /* namespace */
263 
264 #endif /* __data_RecordSB_h__ */
Memory manager for state blocks.
Definition: SegmentStore.h:17
bool isValid(void) const
Return true if the Record points to a valid state block.
Definition: RecordSB.h:184
Smart pointer used with types represented by BaseType.
Definition: Instance.h:28
kernel
Definition: namespace.dox:3
void * data(void) const
Return the state block.
Definition: RecordSB.h:163
sp< Layout > layout(void) const
Return the Layout.
Definition: RecordSB.h:189
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s (reverse logic)
Definition: DualString.h:229
void set(void *datablock)
Set the state block.
Definition: RecordSB.h:194
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
FE_UWORD idr(void) const
Return a unique runtime id (id-runtime, idr, since &#39;rid&#39; has other meaning.
Definition: RecordSB.h:168
LayoutSB * rawLayout(void) const
Return a raw pointer to the Layout.
Definition: RecordSB.h:136
Automatically reference-counted string container.
Definition: String.h:128
Reference to an instance of a Layout.
Definition: RecordSB.h:35
Record "type" definition.
Definition: LayoutSB.h:32
Homogeneous collection of Records.
Definition: RecordArraySB.h:40
Non-persistent reference to an instance of a Layout.
Definition: WeakRecordSB.h:17