Skip to content

Commit d45d831

Browse files
committed
1
1 parent f816fa1 commit d45d831

File tree

3 files changed

+202
-7
lines changed

3 files changed

+202
-7
lines changed

cc/cc/cc/allocator/cc_simple_segregated_storage_dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ cc_api void cc_simple_segregated_storage_dump(cc_simple_segregated_storage_t* si
8787
(int64_t)free_chunk_next_index
8888
);
8989

90+
9091
free_chunk_count++;
9192

9293

cc/cc/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,16 @@ static void test_case_cc_string()
353353
//===========================================================================
354354
static void test_run()
355355
{
356-
#if 0
357356
test_suite_cc_version();
358357

359358
// test_suite_cc_code_template();
360359
// test_suite_cc_assert();
361360

361+
#if 1
362362
test_suite_cc_vector();
363+
#endif
363364

365+
#if 1
364366
test_suite_cc_deque();
365367
test_suite_cc_queue();
366368
test_suite_cc_stack();
@@ -382,7 +384,9 @@ static void test_run()
382384
test_case_cc_lf_heap_1();
383385
#endif
384386

387+
#if 1
385388
test_case_cc_string();
389+
#endif
386390
}
387391

388392

cc/cc/test_cc/test_case_cc_vector_1.cpp

Lines changed: 196 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//===========================================================================
1111
typedef struct _item_t
1212
{
13+
size_t next_free_index;
14+
1315
int value;
1416
} item_t;
1517

@@ -18,22 +20,199 @@ typedef struct _item_t
1820

1921
/////////////////////////////////////////////////////////////////////////////
2022
//===========================================================================
21-
#define item_max_count 32
23+
#define item_max_count 5
2224

2325

2426

2527

2628

2729
/////////////////////////////////////////////////////////////////////////////
2830
//===========================================================================
31+
typedef struct _item_pool_t
32+
{
33+
item_t memory[item_max_count];
34+
size_t free_index_head;
35+
size_t max_count;
36+
size_t count;
37+
}
38+
item_pool_t;
39+
40+
//===========================================================================
41+
static item_pool_t _item_pool;
42+
43+
//===========================================================================
44+
static const size_t item_null_index = (size_t)-1;
45+
46+
static const size_t item_allocated_bit_number = (sizeof(size_t)*8-1);
47+
static const size_t item_allocated_bit_mask = ((size_t)1 << item_allocated_bit_number);
48+
49+
//===========================================================================
50+
static void item_set_allocated(item_t* ctx, size_t my_index)
51+
{
52+
ctx->next_free_index = my_index | item_allocated_bit_mask;
53+
}
54+
55+
static bool item_is_allocated(item_t* ctx)
56+
{
57+
return (0 != (ctx->next_free_index & item_allocated_bit_mask));
58+
}
59+
60+
static size_t item_allocated_index(item_t* ctx)
61+
{
62+
if (!item_is_allocated(ctx))
63+
{
64+
return item_null_index;
65+
}
66+
67+
return ctx->next_free_index & (~item_allocated_bit_mask);
68+
}
69+
70+
static size_t item_get_next_free_index(item_t* ctx)
71+
{
72+
if (item_is_allocated(ctx))
73+
{
74+
return item_null_index;
75+
}
76+
77+
return ctx->next_free_index;
78+
}
79+
80+
//===========================================================================
81+
static void item_pool_dump(void)
82+
{
83+
test_out
84+
<< "# item_pool_dump()" << test_tendl
85+
;
86+
87+
if (_item_pool.free_index_head == item_null_index)
88+
{
89+
test_out << "- free_index_head = null" << test_tendl;
90+
}
91+
else
92+
{
93+
test_out << "- free_index_head = " << _item_pool.free_index_head << test_tendl;
94+
}
95+
test_out << "- max_count = " << _item_pool.max_count << test_tendl;
96+
test_out << "- count = " << _item_pool.count << test_tendl;
97+
98+
99+
size_t free_index = _item_pool.free_index_head;
100+
size_t free_count = 0;
101+
while (free_index != item_null_index)
102+
{
103+
item_t* pointer = &_item_pool.memory[free_index];
104+
105+
size_t next_free_index = item_get_next_free_index(pointer);
106+
107+
if (next_free_index == item_null_index)
108+
{
109+
test_out
110+
<< "- [" << free_count
111+
<< "] free_index:" << free_index
112+
<< " -> next_free_index: null"
113+
<< test_tendl
114+
;
115+
}
116+
else
117+
{
118+
test_out
119+
<< "- [" << free_count
120+
<< "] free_index:" << free_index
121+
<< " -> next_free_index:" << next_free_index
122+
<< test_tendl
123+
;
124+
}
125+
126+
127+
free_count++;
128+
129+
130+
free_index = next_free_index;
131+
}
132+
133+
test_out << "- free_count = " << free_count << test_tendl;
134+
test_out << test_tendl;
135+
}
136+
137+
//===========================================================================
138+
static bool item_pool_initialize()
139+
{
140+
size_t index;
141+
size_t count = item_max_count -1;
142+
143+
144+
for (index = 0; index < count; index++)
145+
{
146+
_item_pool.memory[index].next_free_index = (int)(index + 1);
147+
}
148+
_item_pool.memory[index].next_free_index = item_null_index;
149+
150+
151+
_item_pool.max_count = item_max_count;
152+
153+
_item_pool.count = 0;
154+
155+
return true;
156+
}
157+
158+
static void item_pool_uninitialize()
159+
{
160+
161+
}
162+
29163
static item_t* item_pool_allocate()
30164
{
31-
return new item_t();
165+
//-----------------------------------------------------------------------
166+
if (item_null_index == _item_pool.free_index_head)
167+
{
168+
return NULL;
169+
}
170+
171+
172+
//-----------------------------------------------------------------------
173+
item_t* pointer;
174+
size_t pointer_index = _item_pool.free_index_head;
175+
pointer = &_item_pool.memory[pointer_index];
176+
177+
178+
_item_pool.free_index_head = _item_pool.memory[pointer_index].next_free_index;
179+
180+
181+
item_set_allocated(pointer, pointer_index);
182+
183+
184+
//-----------------------------------------------------------------------
185+
_item_pool.count++;
186+
187+
188+
//-----------------------------------------------------------------------
189+
item_pool_dump();
190+
191+
192+
return pointer;
32193
}
33194

34-
static void item_pool_free(item_t* item)
195+
static void item_pool_free(item_t* pointer)
35196
{
36-
delete item;
197+
//-----------------------------------------------------------------------
198+
test_assert(pointer != NULL);
199+
test_assert(item_is_allocated(pointer) == true);
200+
201+
202+
//-----------------------------------------------------------------------
203+
size_t free_index = _item_pool.free_index_head;
204+
205+
206+
_item_pool.free_index_head = item_allocated_index(pointer);
207+
_item_pool.memory[_item_pool.free_index_head].next_free_index = free_index;
208+
209+
210+
//-----------------------------------------------------------------------
211+
_item_pool.count--;
212+
213+
214+
//-----------------------------------------------------------------------
215+
item_pool_dump();
37216
}
38217

39218

@@ -60,6 +239,15 @@ static bool items_initialize()
60239
;
61240

62241

242+
bool rv;
243+
244+
rv = item_pool_initialize();
245+
if (rv == false)
246+
{
247+
test_assert(0);
248+
return false;
249+
}
250+
63251
cc_vector_initialize(&_items.container, _items.elements, item_max_count, sizeof(item_t));
64252

65253
return true;
@@ -73,6 +261,8 @@ static void items_uninitialize()
73261

74262

75263
test_out << "cc_vector_count():" << cc_vector_count(&_items.container) << test_tendl;
264+
265+
item_pool_uninitialize();
76266
}
77267

78268

@@ -136,7 +326,7 @@ static void erase(void)
136326

137327

138328
size_t i;
139-
i = 9;
329+
i = 2;
140330

141331

142332
element_pointer = cc_vector_at(&_items.container, i);
@@ -172,7 +362,7 @@ static void insert(void)
172362

173363

174364
size_t i;
175-
i = 5;
365+
i = 3;
176366

177367

178368
item_pointer = item_pool_allocate();

0 commit comments

Comments
 (0)