-
Notifications
You must be signed in to change notification settings - Fork 2
/
block.c
152 lines (137 loc) · 2.92 KB
/
block.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include "bgzf.h"
#include "util.h"
#include "block.h"
block_t*
block_init()
{
block_t *b = calloc(1, sizeof(block_t));
b->buffer = malloc(sizeof(int8_t) * MAX_BLOCK_SIZE);
b->id = -1;
return b;
}
void
block_destroy(block_t *block)
{
if(NULL == block) return;
free(block->buffer);
free(block);
}
block_pool_t*
block_pool_init2(int32_t m)
{
int32_t i;
block_pool_t *pool = calloc(1, sizeof(block_pool_t));
pool->m = m;
pool->n = 0;
pool->head = 0;
pool->blocks = calloc(pool->m, sizeof(block_pool_t*));
for(i=0;i<pool->m;i++) {
pool->blocks[i] = NULL;
}
return pool;
}
block_pool_t*
block_pool_init(int32_t m)
{
int32_t i;
block_pool_t *pool = NULL;
pool = block_pool_init2(m);
for(i=0;i<pool->m;i++) {
pool->blocks[i] = block_init();
}
pool->n = m;
pool->mut = calloc(1, sizeof(pthread_mutex_t));
if(0 != pthread_mutex_init(pool->mut, NULL)) {
fprintf(stderr, "Could not create mutex\n");
exit(1);
}
return pool;
}
static void
block_pool_shift(block_pool_t *pool)
{
int32_t i;
/*
fprintf(stderr, "SHIFTING head=%d n=%d m=%d\n",
pool->head, pool->n, pool->m);
*/
if(0 == pool->head) return;
for(i=0;i<pool->n;i++) {
pool->blocks[i] = pool->blocks[i+pool->head];
pool->blocks[i+pool->head] = NULL;
}
pool->head = 0;
/*
fprintf(stderr, "SHIFTED head=%d n=%d m=%d\n",
pool->head, pool->n, pool->m);
*/
}
int32_t
block_pool_add(block_pool_t *pool, block_t *block)
{
if(NULL == block) return 0;
if(NULL != pool->mut) safe_mutex_lock(pool->mut);
if(pool->n < pool->m) {
if(pool->head + pool->n == pool->m) {
block_pool_shift(pool);
}
pool->blocks[pool->head+pool->n] = block;
pool->n++;
}
else { // ignore
block_destroy(block);
}
if(NULL != pool->mut) safe_mutex_unlock(pool->mut);
return 1;
}
block_t*
block_pool_get(block_pool_t *pool)
{
block_t *b = NULL;
if(NULL != pool->mut) safe_mutex_lock(pool->mut);
if(0 < pool->n) {
b = pool->blocks[pool->head];
pool->blocks[pool->head] = NULL;
pool->n--;
pool->head++;
if(pool->head == pool->m) pool->head = 0; // NB: pool->n == 0
}
if(NULL != pool->mut) safe_mutex_unlock(pool->mut);
return b;
}
block_t*
block_pool_peek(block_pool_t *pool)
{
block_t *b = NULL;
if(NULL != pool->mut) safe_mutex_lock(pool->mut);
if(0 < pool->n) {
b = pool->blocks[pool->head];
}
if(NULL != pool->mut) safe_mutex_unlock(pool->mut);
return b;
}
void
block_pool_destroy(block_pool_t *pool)
{
int32_t i;
if(NULL == pool) return;
for(i=0;i<pool->m;i++) {
block_destroy(pool->blocks[i]);
}
free(pool->blocks);
free(pool->mut);
free(pool);
}
void
block_pool_reset(block_pool_t *pool)
{
int32_t i;
for(i=0;i<pool->m;i++) {
block_destroy(pool->blocks[i]);
pool->blocks[i] = NULL;
}
pool->n = 0;
}