Changeset 2cdc84 in git
- Timestamp:
- Aug 31, 2012, 11:33:33 AM (12 years ago)
- Branches:
- (u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
- Children:
- 9373f05657970e6e7e18aac67a18d1245fb6bb4e
- Parents:
- b8346741b9c72d735f018ce57120e6782a5d4d2d
- git-author:
- Alexander Dreyer <alexander.dreyer@itwm.fraunhofer.de>2012-08-31 11:33:33+02:00
- git-committer:
- Oleksandr Motsak <motsak@mathematik.uni-kl.de>2012-09-05 15:52:17+02:00
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/countedref.cc
rb83467 r2cdc84 12 12 **/ 13 13 //***************************************************************************** 14 15 14 16 15 … … 143 142 144 143 public: 145 static idhdl idify(leftv head, idhdl* root) { 144 static leftv idify(leftv head, idhdl* root) { 145 idhdl handle = newid(head, root); 146 leftv res = (leftv)omAlloc0(sizeof(*res)); 147 res->data =(void*) handle; 148 res->rtyp = IDHDL; 149 return res; 150 } 151 152 static idhdl newid(leftv head, idhdl* root) { 153 146 154 static unsigned int counter = 0; 147 155 char* name = (char*) omAlloc0(512); … … 175 183 inline void CountedRefPtr_kill(ring r) { rKill(r); } 176 184 177 178 class LeftvShallow { 185 class LeftvHelper { 186 public: 187 template <class Type> 188 static Type* cpy(Type* result, Type* data) { 189 return (Type*)memcpy(result, data, sizeof(Type)); 190 } 191 template <class Type> 192 static Type* cpy(Type* data) { 193 return cpy((Type*)omAlloc0(sizeof(Type)), data); 194 } 195 template <class Type> 196 static Type* recursivecpy(Type* data) { 197 if (data == NULL) return data; 198 Type* result = cpy(data); 199 result->next = recursivecpy(data->next); 200 return result; 201 } 202 template <class Type> 203 static Type* shallowcpy(Type* result, Type* data) { 204 cpy(result, data)->e = recursivecpy(data->e); 205 return result; 206 } 207 template <class Type> 208 static Type* shallowcpy(Type* data) { 209 return shallowcpy((Type*) omAlloc0(sizeof(Type)), data); 210 } 211 template <class Type> 212 static void recursivekill(Type* current) { 213 if(current == NULL) return; 214 recursivekill(current->next); 215 omFree(current); 216 } 217 static leftv allocate() { return (leftv)omAlloc0(sizeof(sleftv)); } 218 219 }; 220 221 class LeftvShallow: 222 public LeftvHelper { 179 223 typedef LeftvShallow self; 180 224 181 225 public: 182 226 LeftvShallow(): m_data(allocate()) { } 183 LeftvShallow(leftv data): 184 m_data(allocate()) { init(data); } 185 186 LeftvShallow(const self& rhs): 187 m_data(allocate()) { 188 copy(m_data, rhs.m_data); 189 } 227 LeftvShallow(leftv data): m_data(shallowcpy(data)) { } 228 LeftvShallow(const self& rhs): m_data(shallowcpy(rhs.m_data)) { } 190 229 191 230 ~LeftvShallow() { 192 kill(m_data->e);231 recursivekill(m_data->e); 193 232 omFree(m_data); 194 233 } 195 234 self& operator=(leftv rhs) { 196 kill(m_data->e); 197 return init(rhs); 235 recursivekill(m_data->e); 236 shallowcpy(m_data, rhs); 237 return *this; 198 238 } 199 239 200 240 self& operator=(const self& rhs) { return (*this) = rhs.m_data; } 201 202 203 204 241 205 242 /// Access to object … … 208 245 209 246 protected: 210 static leftv allocate() { return (leftv)omAlloc0(sizeof(sleftv)); }211 212 self& init(leftv data) {213 memcpy(m_data, data, sizeof(sleftv));214 data->e = NULL;215 m_data->next = NULL;216 return *this;217 }218 219 static void copy(leftv result, leftv data) {220 memcpy(result, data, sizeof(sleftv));221 copy(result->e, data->e);222 }223 224 static void copy(Subexpr& current, Subexpr rhs) {225 if (rhs == NULL) return;226 current = (Subexpr)memcpy(omAlloc0Bin(sSubexpr_bin), rhs, sizeof(*rhs));227 copy(current->next, rhs->next);228 }229 230 static void kill(Subexpr current) {231 if(current == NULL) return;232 kill(current->next);233 omFree(current);234 }235 236 protected:237 247 leftv m_data; 238 248 }; 239 249 240 250 241 class LeftvDeep: 242 protected LeftvShallow { 251 class LeftvDeep: public LeftvHelper { 243 252 typedef LeftvDeep self; 244 typedef LeftvShallow base; 245 253 254 /// @name Do not permit copying 255 //@{ 246 256 self& operator=(const self&); 247 public: 248 LeftvDeep(): base() {} 249 LeftvDeep(leftv data): base(data) { if(!isid()) m_data->data=data->CopyD(); } 250 251 LeftvDeep(leftv data,int,int): base() { m_data->Copy(data); } 252 253 LeftvDeep(const self& rhs): base(rhs) { 254 if(m_data->rtyp != IDHDL) 255 m_data->Copy(rhs.m_data); 256 } 257 LeftvDeep(const self&); 258 //@} 259 260 public: 261 LeftvDeep(): m_data(allocate()) {} 262 LeftvDeep(leftv data): m_data(cpy(data)) { 263 data->e = NULL; // occupy subexpression 264 if(!isid()) m_data->data=data->CopyD(); 265 } 266 267 LeftvDeep(leftv data,int,int): m_data(allocate()) { m_data->Copy(data); } 257 268 258 269 ~LeftvDeep() { m_data->CleanUp(); } 259 operator const base&() { return static_cast<const base&>(*this);}270 operator LeftvShallow() { return m_data;} 260 271 261 272 bool like(const self& rhs) const { return m_data->data == rhs.m_data->data; } … … 289 300 result->CleanUp(); 290 301 291 copy(result, m_data);302 shallowcpy(result, m_data); 292 303 result->next = next; 293 304 return FALSE; 294 305 } 306 307 BOOLEAN retrieve(leftv res) { 308 if (res->data == m_data->data) { 309 if(m_data->e != res->e) recursivekill(m_data->e); 310 cpy(m_data, res); 311 res->Init(); 312 return TRUE; 313 } 314 return FALSE; 315 } 316 317 295 318 296 319 BOOLEAN isid() const { return m_data->rtyp==IDHDL;} 297 320 BOOLEAN ringed() { return m_data->RingDependend(); } 298 321 BOOLEAN unassigned() const { return m_data->Typ()==0; } 322 323 leftv idify(idhdl* root) { 324 leftv res = (isid()? m_data: CountedRefEnv::idify(m_data, root)); 325 ++(((idhdl)res->data)->ref); 326 return res; 327 } 328 329 330 void clearid(idhdl* root) { 331 assume(isid()); 332 if (--((idhdl)m_data->data)->ref <= 0) // clear only if we own 333 CountedRefEnv::clearid((idhdl)m_data->data, root); 334 } 335 336 private: 337 leftv m_data; 299 338 }; 300 339 … … 311 350 /// Subscripted object 312 351 CountedRefData(leftv wrapped, CountedRefPtr<self*> back): 313 base(), m_data(wrapped), m_ring(back->m_ring), m_back(back) { 314 315 assume(wrapped->rtyp == IDHDL); 316 ++((idhdl)m_data.access()->data)->ref; 317 } 318 319 /// Disallow assignment 352 base(), m_data(wrapped), m_ring(back->m_ring), m_back(back) { } 353 354 /// Disallow copying to avoid inconsistence 355 //@{ 320 356 self& operator=(const self&); 357 CountedRefData(const self&); 358 //@} 321 359 public: 322 360 /// Construct shared memory empty Singular object … … 331 369 base(), m_data(data, global,0), m_ring(parent(data)), m_back() { } 332 370 333 /// Construct deep copy334 CountedRefData(const self& rhs):335 base(), m_data(rhs.m_data), m_ring(rhs.m_ring), m_back() { }336 337 371 /// Destruct 338 ~CountedRefData() { 339 if (m_back && (--((idhdl)m_data.access()->data)->ref <= 0)) // clear only if we own 340 CountedRefEnv::clearid((idhdl)m_data.access()->data, root()); 341 } 342 372 ~CountedRefData() { if (m_back) m_data.clearid(root()); } 373 374 /// Generate 343 375 CountedRefPtr<self*> subscripted() { 344 if (m_back) 345 return new self(operator*().operator->(), m_back); 346 347 return new self(init(CountedRefEnv::idify(m_data.access(), root())), this); 376 return new self(m_data.idify(root()), 377 m_back? m_back: CountedRefPtr<self*>(this)); 348 378 } 349 379 … … 361 391 LeftvShallow operator*() const { return (broken()? LeftvShallow(): (const LeftvShallow&)m_data); } 362 392 363 393 /// Determine active ring when ring dependency changes 364 394 BOOLEAN rering() { 365 395 if (m_ring ^ m_data.ringed()) m_ring = (m_ring? NULL: currRing); … … 386 416 } 387 417 418 /// Reassign actual object 388 419 BOOLEAN assign(leftv result, leftv arg) { 389 420 if (!m_data.isid()) { … … 393 424 return get(result) || iiAssign(result, arg) || rering(); 394 425 } 395 396 BOOLEAN retrieve(leftv res) { 397 if (res->data == m_data.access()->data) { 398 memcpy(m_data.access(), res, sizeof(sleftv)); 399 res->Init(); 400 return TRUE; 401 } 402 return FALSE; 403 } 404 426 /// Recover additional information (e.g. subexpression) from likewise object 427 BOOLEAN retrieve(leftv res) { return m_data.retrieve(res); } 428 429 /// Check whether data is all-zero 405 430 BOOLEAN unassigned() const { return m_data.unassigned(); } 431 406 432 private: 407 408 433 /// Raise error message and return @c TRUE 409 434 BOOLEAN complain(const char* text) const { … … 412 437 } 413 438 414 415 416 439 /// Store ring for ring-dependent objects 417 440 static ring parent(leftv rhs) { 418 441 return (rhs->RingDependend()? currRing: NULL); 419 }420 421 static leftv init(idhdl handle) {422 assume(handle);423 leftv res = (leftv)omAlloc0(sizeof(*res));424 res->data =(void*) handle;425 res->rtyp = IDHDL;426 return res;427 442 } 428 443 … … 574 589 return (arg->next != NULL) && resolve(arg->next); 575 590 } 576 // int Typ() const {return m_data->Typ(); } 591 577 592 protected: 578 593 /// Construct integer value
Note: See TracChangeset
for help on using the changeset viewer.