11
2+ // ===========================================================================
3+ //
4+ // Array_ExpandIf()
5+ // Takes array "arr" which supposedly is large enough to store "valid_count"
6+ // number of elements. If "need_capacity" is greater than "valid_count", then
7+ // creates a new array, copies old contents there, and returns it.
8+ // Otherwise simply returns existing array.
9+ //
10+ // ===========================================================================
211int [] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz)
312{
413 if (valid_count >= need_capacity) { return arr; }
@@ -12,6 +21,12 @@ int[] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz)
1221}
1322
1423
24+ // ===========================================================================
25+ //
26+ // ObjectPool::AddObjects()
27+ // Registers range of IDs.
28+ //
29+ // ===========================================================================
1530void ObjectPool::AddObjects (int from, int to) {
1631 if (this ._capacity <= to) {
1732 int new_capacity = to + 1 ;
@@ -35,19 +50,12 @@ void ObjectPool::AddObjects(int from, int to) {
3550 }
3651}
3752
38- void ObjectPool::RemoveAll () {
39- this ._numFreeObj = 0 ;
40- for (int i = 0 ; i < this ._capacity ; i++) {
41- this ._usingObj [i] = false ;
42- this ._isFree [i] = false ;
43- }
44- this ._usingObj = null;
45- this ._isFree = null;
46- this ._freeObj = null;
47- this ._capacity = 0 ;
48- this ._numUsed = 0 ;
49- }
50-
53+ // ===========================================================================
54+ //
55+ // ObjectPool::RemoveObjects()
56+ // Unregisters range of IDs.
57+ //
58+ // ===========================================================================
5159void ObjectPool::RemoveObjects (int from, int to) {
5260 for (int i = from; i <= to; i++) {
5361 if (this ._usingObj [i]) {
@@ -66,6 +74,31 @@ void ObjectPool::RemoveObjects(int from, int to) {
6674 }
6775}
6876
77+ // ===========================================================================
78+ //
79+ // ObjectPool::RemoveAll()
80+ // Unregisters all IDs.
81+ //
82+ // ===========================================================================
83+ void ObjectPool::RemoveAll () {
84+ this ._numFreeObj = 0 ;
85+ for (int i = 0 ; i < this ._capacity ; i++) {
86+ this ._usingObj [i] = false ;
87+ this ._isFree [i] = false ;
88+ }
89+ this ._usingObj = null;
90+ this ._isFree = null;
91+ this ._freeObj = null;
92+ this ._capacity = 0 ;
93+ this ._numUsed = 0 ;
94+ }
95+
96+ // ===========================================================================
97+ //
98+ // ObjectPool::Acquire()
99+ // Returns first found free ID and marks it as used.
100+ //
101+ // ===========================================================================
69102int ObjectPool::Acquire () {
70103 if (this ._numFreeObj == 0 ) { return -1 ; }
71104 this ._numFreeObj --;
@@ -74,13 +107,25 @@ int ObjectPool::Acquire() {
74107 return id;
75108}
76109
110+ // ===========================================================================
111+ //
112+ // ObjectPool::Release()
113+ // Marks given ID as free.
114+ //
115+ // ===========================================================================
77116void ObjectPool::Release (int id) {
78117 if (!this ._usingObj [id] || this ._isFree [id]) { return ; }
79118 this ._freeObj [this ._numFreeObj ] = id;
80119 this ._numFreeObj ++;
81120 this ._isFree [id] = true ;
82121}
83122
123+ // ===========================================================================
124+ //
125+ // ObjectPool::ReleaseAll()
126+ // Marks all known IDs as free.
127+ //
128+ // ===========================================================================
84129void ObjectPool::ReleaseAll () {
85130 this ._numFreeObj = 0 ;
86131 for (int i = 0 ; i < this ._capacity ; i++) {
@@ -92,19 +137,38 @@ void ObjectPool::ReleaseAll() {
92137 }
93138}
94139
140+ // ===========================================================================
141+ //
142+ // ObjectPool::GetAcquiredNum()
143+ //
144+ // ===========================================================================
95145int ObjectPool::GetAcquiredNum () {
96146 return this ._numUsed - this ._numFreeObj ;
97147}
98148
149+ // ===========================================================================
150+ //
151+ // ObjectPool::GetFreeNum()
152+ //
153+ // ===========================================================================
99154int ObjectPool::GetFreeNum () {
100155 return this ._numFreeObj ;
101156}
102157
158+ // ===========================================================================
159+ //
160+ // ObjectPool::GetTotalNum()
161+ //
162+ // ===========================================================================
103163int ObjectPool::GetTotalNum () {
104164 return this ._numUsed ;
105165}
106166
107-
167+ // ===========================================================================
168+ //
169+ // ObjectPool::GetPoolSize()
170+ //
171+ // ===========================================================================
108172int ObjectPool::GetPoolSize () {
109173 return this ._capacity ;
110174}
0 commit comments