Skip to content

Commit

Permalink
pthread lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Isty001 committed Feb 11, 2017
1 parent e865639 commit bce888d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### Memory Pool


Basic memory pool implementation, for reusable memory blocks.
Dynamic memory pool implementation, for reusable memory blocks, using `pthread mutex` locks


#### Usage:
Expand Down Expand Up @@ -35,4 +35,4 @@ If everything is finished, then actually free all the memory allocated:
pool_destroy(pool);
```

For a quick check, run `make test` or `make test-valgrind`
For a quick check, run `make test` or `make test-valgrind`
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "mem-pool",
"version": "0.0.1",
"version": "0.1.0",
"repo": "isty001/mem-pool",
"description": "Fix member sized, growing memory pool",
"keywords": [
"memory",
"poll",
"popl",
"pthread",
"mutex",
"memory pool"
],
"license": "MIT",
Expand Down
45 changes: 39 additions & 6 deletions src/mem_pool.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
#include <stdio.h>
#include <stdalign.h>
#include <stddef.h>
#include <pthread.h>
#include "mem_pool.h"


#define check(res) \
if (0 != res) { \
fprintf(stderr, "pthread failure"); \
exit(EXIT_FAILURE); \
}

#define lock(pool) \
check(pthread_mutex_lock(&pool->mutex));

#define unlock(pool) \
check(pthread_cond_broadcast(&pool->cond)); \
check(pthread_mutex_unlock(&pool->mutex));


typedef struct buffer Buffer;

typedef struct block Block;


struct buffer
{
struct buffer {
void *memory;
void *end;
Buffer *next;
};

struct block
{
struct block {
Block *next;
};

struct mem_pool
{
struct mem_pool {
size_t memb_size;
size_t buff_size;
Buffer *buff_head;
Buffer *buff_last;
Block *block_head;
pthread_mutex_t mutex;
pthread_cond_t cond;
};


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

check(pthread_mutex_init(&pool->mutex, NULL));
check(pthread_cond_init(&pool->cond, NULL));

return pool;
}

static void *from_free_list(MemPool *pool)
{
Block *tmp = pool->block_head;
pool->block_head = tmp->next;
unlock(pool);

return tmp;
}
Expand All @@ -77,12 +95,15 @@ static void *from_buffer(MemPool *pool, Buffer *buff)
{
void *ptr = buff->memory;
buff->memory += pool->memb_size;
unlock(pool);

return ptr;
}

void *pool_alloc(MemPool *pool)
{
lock(pool);

if (pool->block_head) {
return from_free_list(pool);
}
Expand All @@ -99,17 +120,23 @@ void *pool_alloc(MemPool *pool)

bool pool_has_ptr(MemPool *pool, void *ptr)
{
lock(pool);

void *from;
Buffer *buff = pool->buff_head;

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

if (ptr >= from && ptr <= buff->end) {
unlock(pool);

return true;
}
buff = buff->next;
}
unlock(pool);

return false;
}

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

lock(pool);

Block *new = (Block *) ptr;
Block *tmp = pool->block_head;
pool->block_head = new;
new->next = tmp;
unlock(pool);

return 0;
}
Expand All @@ -137,5 +167,8 @@ void pool_destroy(MemPool *pool)
free(buff->end - pool->buff_size);
free(buff);
}

pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->cond);
free(pool);
}

0 comments on commit bce888d

Please sign in to comment.