Skip to content

Commit 97ab9e9

Browse files
committed
ObjectPool: tidied and prepared for release
1 parent 08f0858 commit 97ab9e9

3 files changed

Lines changed: 152 additions & 49 deletions

File tree

scripts/_dump/ObjectPool.ash

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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+
//===========================================================================
211
int[] 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+
//===========================================================================
1530
void 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+
//===========================================================================
5159
void 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+
//===========================================================================
69102
int 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+
//===========================================================================
77116
void 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+
//===========================================================================
84129
void 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+
//===========================================================================
95145
int ObjectPool::GetAcquiredNum() {
96146
return this._numUsed - this._numFreeObj;
97147
}
98148

149+
//===========================================================================
150+
//
151+
// ObjectPool::GetFreeNum()
152+
//
153+
//===========================================================================
99154
int ObjectPool::GetFreeNum() {
100155
return this._numFreeObj;
101156
}
102157

158+
//===========================================================================
159+
//
160+
// ObjectPool::GetTotalNum()
161+
//
162+
//===========================================================================
103163
int ObjectPool::GetTotalNum() {
104164
return this._numUsed;
105165
}
106166

107-
167+
//===========================================================================
168+
//
169+
// ObjectPool::GetPoolSize()
170+
//
171+
//===========================================================================
108172
int ObjectPool::GetPoolSize() {
109173
return this._capacity;
110174
}

scripts/util/ObjectPool.ash

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// ObjectPool is open source under the MIT License.
2+
//
3+
// TERMS OF USE - ObjectPool MODULE
4+
//
5+
// Copyright (c) 2020-present Ivan Mogilko
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
// this software and associated documentation files (the "Software"), to deal in
9+
// the Software without restriction, including without limitation the rights to
10+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
// the Software, and to permit persons to whom the Software is furnished to do so,
12+
// subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
//
24+
//////////////////////////////////////////////////////////////////////////////////////////
25+
//
26+
// Module implements ObjectPool class which helps to keep track of a list of reusable
27+
// objects. It does not matter what kind of objects are these so long as they have
28+
// integer IDs.
29+
//
30+
// In simple words, ObjectPool remembers which IDs are free and which are in use.
31+
// It gives you next free ID by demand, and lets you mark used ID as a free one when you
32+
// are no longer using it.
33+
//
34+
//////////////////////////////////////////////////////////////////////////////////////////
35+
36+
#ifndef __OBJECTPOOL_MODULE__
37+
#define __OBJECTPOOL_MODULE__
38+
39+
#define OBJECTPOOL_VERSION_00_01_00_00
40+
41+
struct ObjectPool {
42+
// Adds a range of IDs into the list. You can keep adding more later and all the previous
43+
// ones will be kept unless you call RemoveObjects or RemoveAll.
44+
import void AddObjects(int from, int to);
45+
// Removes a range of IDs from the list.
46+
import void RemoveObjects(int from, int to);
47+
// Removes all IDs.
48+
import void RemoveAll();
49+
50+
// Gives next free ID and marks it as "used". Returns -1 if no more free IDs are available.
51+
import int Acquire();
52+
// Marks given ID as "free".
53+
import void Release(int id);
54+
// Marks all the known IDs as "free".
55+
import void ReleaseAll();
56+
57+
// Gets number of acquired ("used") IDs
58+
import int GetAcquiredNum();
59+
// Gets number of available free IDs
60+
import int GetFreeNum();
61+
// Gets total number of registered IDs
62+
import int GetTotalNum();
63+
// Gets pool capacity (may include empty slots!). This is for test purposes only.
64+
import int GetPoolSize();
65+
66+
protected int _capacity;
67+
protected int _numUsed;
68+
protected bool _usingObj[];
69+
protected bool _isFree[];
70+
protected int _freeObj[];
71+
protected int _numFreeObj;
72+
};
73+
74+
#endif // __OBJECTPOOL_MODULE__

0 commit comments

Comments
 (0)