source: trunk/src/user32/gen_object.cpp@ 5935

Last change on this file since 5935 was 5935, checked in by sandervl, 24 years ago

reference count (window + class objects) rewrite

File size: 3.1 KB
Line 
1/* $Id: gen_object.cpp,v 1.8 2001-06-09 14:50:17 sandervl Exp $ */
2/*
3 * Generic Object Class for OS/2
4 *
5 * Allocated in shared memory, so other processes can access the objects
6 *
7 * NOTE: Requires safety precautions to use objects in multiple threads or processes
8 *
9 * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
10 *
11 */
12#define INCL_DOSSEMAPHORES
13#include <os2wrap.h>
14#include <stdlib.h>
15#include <misc.h>
16#include <win32type.h>
17#include <gen_object.h>
18
19#define DBG_LOCALLOG DBG_gen_object
20#include "dbglocal.h"
21
22//******************************************************************************
23//******************************************************************************
24GenericObject::GenericObject(GenericObject **head, CRITICAL_SECTION *pLock)
25{
26 this->pLock = pLock;
27 this->head = head;
28 this->next = NULL;
29 refCount = 1;
30
31 fLinked = FALSE;
32 fDeletePending = FALSE;
33
34 link();
35}
36//******************************************************************************
37//******************************************************************************
38GenericObject::~GenericObject()
39{
40 unlink();
41}
42//******************************************************************************
43//******************************************************************************
44void GenericObject::link()
45{
46 lock();
47 if(*head == NULL) {
48 *head = this;
49 }
50 else {
51 GenericObject *cur = *head;
52 while(cur->next)
53 {
54 cur = cur->next;
55 }
56 cur->next = this;
57 }
58 fLinked = TRUE;
59 unlock();
60}
61//******************************************************************************
62//******************************************************************************
63void GenericObject::unlink()
64{
65 if(!fLinked) return;
66
67 //remove from linked list
68 lock();
69 if(*head == this) {
70 *head = next;
71 }
72 else {
73 GenericObject *cur = *head;
74 while(cur->next != this)
75 {
76 cur = cur->next;
77 if(cur == NULL) {
78 dprintf(("GenericObject dtor: cur == NULL!!"));
79 unlock();
80 DebugInt3();
81 return;
82 }
83 }
84 cur->next = next;
85 }
86 unlock();
87}
88//******************************************************************************
89//******************************************************************************
90LONG GenericObject::release()
91{
92//// dprintf(("release -> refcount %x", refCount));
93#ifdef DEBUG
94 if(refCount-1 < 0) {
95 DebugInt3();
96 }
97#endif
98 if(InterlockedDecrement(&refCount) == 0 && fDeletePending) {
99 delete this;
100 return 0;
101 }
102 return refCount;
103}
104//******************************************************************************
105//******************************************************************************
106void GenericObject::DestroyAll(GenericObject *head)
107{
108 GenericObject *cur, *next;
109
110 cur = head;
111 while(cur) {
112 next = cur->next;
113 if(cur->getRefCount() != 0) {
114 dprintf(("Refcount %d for object %x", cur->getRefCount(), cur));
115 }
116 delete cur;
117 cur = next;
118 }
119}
120//******************************************************************************
121//******************************************************************************
Note: See TracBrowser for help on using the repository browser.