Skip to content

Commit bce888d

Browse files
committed
pthread lock
1 parent e865639 commit bce888d

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### Memory Pool
22

33

4-
Basic memory pool implementation, for reusable memory blocks.
4+
Dynamic memory pool implementation, for reusable memory blocks, using `pthread mutex` locks
55

66

77
#### Usage:
@@ -35,4 +35,4 @@ If everything is finished, then actually free all the memory allocated:
3535
pool_destroy(pool);
3636
```
3737

38-
For a quick check, run `make test` or `make test-valgrind`
38+
For a quick check, run `make test` or `make test-valgrind`

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "mem-pool",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"repo": "isty001/mem-pool",
55
"description": "Fix member sized, growing memory pool",
66
"keywords": [
77
"memory",
8-
"poll",
8+
"popl",
9+
"pthread",
10+
"mutex",
911
"memory pool"
1012
],
1113
"license": "MIT",

src/mem_pool.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
#include <stdio.h>
22
#include <stdalign.h>
33
#include <stddef.h>
4+
#include <pthread.h>
45
#include "mem_pool.h"
56

67

8+
#define check(res) \
9+
if (0 != res) { \
10+
fprintf(stderr, "pthread failure"); \
11+
exit(EXIT_FAILURE); \
12+
}
13+
14+
#define lock(pool) \
15+
check(pthread_mutex_lock(&pool->mutex));
16+
17+
#define unlock(pool) \
18+
check(pthread_cond_broadcast(&pool->cond)); \
19+
check(pthread_mutex_unlock(&pool->mutex));
20+
21+
722
typedef struct buffer Buffer;
823

924
typedef struct block Block;
1025

1126

12-
struct buffer
13-
{
27+
struct buffer {
1428
void *memory;
1529
void *end;
1630
Buffer *next;
1731
};
1832

19-
struct block
20-
{
33+
struct block {
2134
Block *next;
2235
};
2336

24-
struct mem_pool
25-
{
37+
struct mem_pool {
2638
size_t memb_size;
2739
size_t buff_size;
2840
Buffer *buff_head;
2941
Buffer *buff_last;
3042
Block *block_head;
43+
pthread_mutex_t mutex;
44+
pthread_cond_t cond;
3145
};
3246

3347

@@ -62,13 +76,17 @@ MemPool *pool_init(size_t block_size, size_t increase_count)
6276
pool->buff_last = pool->buff_head;
6377
pool->block_head = NULL;
6478

79+
check(pthread_mutex_init(&pool->mutex, NULL));
80+
check(pthread_cond_init(&pool->cond, NULL));
81+
6582
return pool;
6683
}
6784

6885
static void *from_free_list(MemPool *pool)
6986
{
7087
Block *tmp = pool->block_head;
7188
pool->block_head = tmp->next;
89+
unlock(pool);
7290

7391
return tmp;
7492
}
@@ -77,12 +95,15 @@ static void *from_buffer(MemPool *pool, Buffer *buff)
7795
{
7896
void *ptr = buff->memory;
7997
buff->memory += pool->memb_size;
98+
unlock(pool);
8099

81100
return ptr;
82101
}
83102

84103
void *pool_alloc(MemPool *pool)
85104
{
105+
lock(pool);
106+
86107
if (pool->block_head) {
87108
return from_free_list(pool);
88109
}
@@ -99,17 +120,23 @@ void *pool_alloc(MemPool *pool)
99120

100121
bool pool_has_ptr(MemPool *pool, void *ptr)
101122
{
123+
lock(pool);
124+
102125
void *from;
103126
Buffer *buff = pool->buff_head;
104127

105128
while (buff) {
106129
from = buff->end - pool->buff_size;
107130

108131
if (ptr >= from && ptr <= buff->end) {
132+
unlock(pool);
133+
109134
return true;
110135
}
111136
buff = buff->next;
112137
}
138+
unlock(pool);
139+
113140
return false;
114141
}
115142

@@ -119,10 +146,13 @@ int pool_free(MemPool *pool, void *ptr)
119146
return -1;
120147
}
121148

149+
lock(pool);
150+
122151
Block *new = (Block *) ptr;
123152
Block *tmp = pool->block_head;
124153
pool->block_head = new;
125154
new->next = tmp;
155+
unlock(pool);
126156

127157
return 0;
128158
}
@@ -137,5 +167,8 @@ void pool_destroy(MemPool *pool)
137167
free(buff->end - pool->buff_size);
138168
free(buff);
139169
}
170+
171+
pthread_mutex_destroy(&pool->mutex);
172+
pthread_cond_destroy(&pool->cond);
140173
free(pool);
141174
}

0 commit comments

Comments
 (0)