Sort the blocks database in reverse order, allowing us to remove the first block without iterating over them. Output the block number to debug.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
diff --git a/cgminer.c b/cgminer.c
index 89d75ae..c9114b2 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2814,7 +2814,7 @@ static inline bool from_existing_block(struct work *work)
static int block_sort(struct block *blocka, struct block *blockb)
{
- return blockb->block_no - blocka->block_no;
+ return blocka->block_no - blockb->block_no;
}
static void test_work_current(struct work *work)
@@ -2834,29 +2834,29 @@ static void test_work_current(struct work *work)
* new block and set the current block details to this one */
if (!block_exists(hexstr)) {
struct block *s = calloc(sizeof(struct block), 1);
+ int deleted_block = 0;
if (unlikely(!s))
quit (1, "test_work_current OOM");
strcpy(s->hash, hexstr);
s->block_no = new_blocks++;
wr_lock(&blk_lock);
- /* Only keep the last 6 blocks in memory since work from blocks
- * before this is virtually impossible and we want to prevent
- * memory usage from continually rising */
- if (HASH_COUNT(blocks) > 5) {
- struct block *blocka, *blockb;
- int count = 0;
+ /* Only keep the last hour's worth of blocks in memory since
+ * work from blocks before this is virtually impossible and we
+ * want to prevent memory usage from continually rising */
+ if (HASH_COUNT(blocks) > 6) {
+ struct block *oldblock;
HASH_SORT(blocks, block_sort);
- HASH_ITER(hh, blocks, blocka, blockb) {
- if (count++ < 6)
- continue;
- HASH_DEL(blocks, blocka);
- free(blocka);
- }
+ oldblock = blocks;
+ deleted_block = oldblock->block_no;
+ HASH_DEL(blocks, oldblock);
+ free(oldblock);
}
HASH_ADD_STR(blocks, hash, s);
wr_unlock(&blk_lock);
+ if (deleted_block)
+ applog(LOG_DEBUG, "Deleted block %d from database", deleted_block);
set_curblock(hexstr, work->data);
if (unlikely(new_blocks == 1))
goto out_free;