00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #define INCL_DOSSEMAPHORES
00013 #include <os2wrap.h>
00014 #include <stdlib.h>
00015 #include <misc.h>
00016 #include <win32type.h>
00017 #include <gen_object.h>
00018
00019 #define DBG_LOCALLOG DBG_gen_object
00020 #include "dbglocal.h"
00021
00022
00023
00024 GenericObject::GenericObject(GenericObject **head, CRITICAL_SECTION *pLock)
00025 {
00026 this->pLock = pLock;
00027 this->head = head;
00028 this->next = NULL;
00029 refCount = 1;
00030
00031 fLinked = FALSE;
00032 fDeletePending = FALSE;
00033
00034 link();
00035 }
00036
00037
00038 GenericObject::~GenericObject()
00039 {
00040 unlink();
00041 }
00042
00043
00044 void GenericObject::link()
00045 {
00046 lock();
00047 if(*head == NULL) {
00048 *head = this;
00049 }
00050 else {
00051 GenericObject *cur = *head;
00052 while(cur->next)
00053 {
00054 cur = cur->next;
00055 }
00056 cur->next = this;
00057 }
00058 fLinked = TRUE;
00059 unlock();
00060 }
00061
00062
00063 void GenericObject::unlink()
00064 {
00065 if(!fLinked) return;
00066
00067
00068 lock();
00069 if(*head == this) {
00070 *head = next;
00071 }
00072 else {
00073 GenericObject *cur = *head;
00074 while(cur->next != this)
00075 {
00076 cur = cur->next;
00077 if(cur == NULL) {
00078 dprintf(("GenericObject dtor: cur == NULL!!"));
00079 unlock();
00080 DebugInt3();
00081 return;
00082 }
00083 }
00084 cur->next = next;
00085 }
00086 unlock();
00087 }
00088
00089
00090 #ifdef DEBUG
00091 LONG GenericObject::addRef()
00092 {
00093
00094 return InterlockedIncrement(&refCount);
00095 }
00096 #endif
00097
00098
00099 LONG GenericObject::release()
00100 {
00101
00102 #ifdef DEBUG
00103 if(refCount-1 < 0) {
00104 DebugInt3();
00105 }
00106 #endif
00107 if(InterlockedDecrement(&refCount) == 0 && fDeletePending) {
00108 dprintf2(("marked for deletion -> delete now"));
00109 delete this;
00110 return 0;
00111 }
00112 return refCount;
00113 }
00114
00115
00116 void GenericObject::DestroyAll(GenericObject *head)
00117 {
00118 GenericObject *cur, *next;
00119
00120 cur = head;
00121 while(cur) {
00122 next = cur->next;
00123 if(cur->getRefCount() != 0) {
00124 dprintf(("Refcount %d for object %x", cur->getRefCount(), cur));
00125 }
00126 delete cur;
00127 cur = next;
00128 }
00129 }
00130
00131