Skip to content

Commit

Permalink
version 1.3-beta Added on memory alloc free hooker
Browse files Browse the repository at this point in the history
  • Loading branch information
cdgwoon committed Sep 17, 2018
1 parent 21497d6 commit e095b67
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lfqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ static void __lfq_check_free(lfqueue_t *);
static void *_dequeue(lfqueue_t *);
static void *_single_dequeue(lfqueue_t *);
static int _enqueue(lfqueue_t *, void* );
static inline void* _lfqueue_malloc(void* pl, size_t sz) {
return malloc(sz);
}
static inline void _lfqueue_free(void* pl, void* ptr) {
free(ptr);
}

static void *
_dequeue(lfqueue_t *lfqueue) {
Expand Down Expand Up @@ -163,7 +169,7 @@ _single_dequeue(lfqueue_t *lfqueue) {
if (next) {
val = next->value;
if (__LFQ_BOOL_COMPARE_AND_SWAP(&lfqueue->head, head, next)) {
free(head);
lfqueue->_free(lfqueue->pl, head);
break;
}
} else {
Expand All @@ -178,7 +184,7 @@ _single_dequeue(lfqueue_t *lfqueue) {
static int
_enqueue(lfqueue_t *lfqueue, void* value) {
lfqueue_cas_node_t *tail, *node;
node = (lfqueue_cas_node_t*) malloc(sizeof(lfqueue_cas_node_t));
node = (lfqueue_cas_node_t*) lfqueue->_malloc(lfqueue->pl, sizeof(lfqueue_cas_node_t));
if (node == NULL) {
perror("malloc");
return errno;
Expand Down Expand Up @@ -223,7 +229,7 @@ __lfq_check_free(lfqueue_t *lfqueue) {
nextfree = rtfree->nextfree;
if ( lfq_diff_time(curr_time, rtfree->_deactivate_tm) > 2) {
// printf("%p\n", rtfree);
free(rtfree);
lfqueue->_free(lfqueue->pl, rtfree);
rtfree = nextfree;
} else {
break;
Expand All @@ -237,9 +243,17 @@ __lfq_check_free(lfqueue_t *lfqueue) {

int
lfqueue_init(lfqueue_t *lfqueue) {
return lfqueue_init_mf(lfqueue, NULL, _lfqueue_malloc, _lfqueue_free);
}

lfqueue_cas_node_t *base = malloc(sizeof(lfqueue_cas_node_t));
lfqueue_cas_node_t *freebase = malloc(sizeof(lfqueue_cas_node_t));
int
lfqueue_init_mf(lfqueue_t *lfqueue, void* pl, lfqueue_malloc_fn lfqueue_malloc, lfqueue_free_fn lfqueue_free) {
lfqueue->_malloc = lfqueue_malloc;
lfqueue->_free = lfqueue_free;
lfqueue->pl = pl;

lfqueue_cas_node_t *base = lfqueue_malloc(pl, sizeof(lfqueue_cas_node_t));
lfqueue_cas_node_t *freebase = lfqueue_malloc(pl, sizeof(lfqueue_cas_node_t));
if (base == NULL || freebase == NULL) {
perror("malloc");
return errno;
Expand All @@ -266,20 +280,20 @@ void
lfqueue_destroy(lfqueue_t *lfqueue) {
void* p;
while ((p = lfqueue_deq(lfqueue))) {
free(p);
lfqueue->_free(lfqueue->pl, p);
}
// Clear the recycle chain nodes
lfqueue_cas_node_t *rtfree = lfqueue->root_free, *nextfree;
while (rtfree && (rtfree != lfqueue->move_free) ) {
nextfree = rtfree->nextfree;
free(rtfree);
lfqueue->_free(lfqueue->pl, rtfree);
rtfree = nextfree;
}
if (rtfree) {
free(rtfree);
lfqueue->_free(lfqueue->pl, rtfree);
}

free(lfqueue->tail); // Last free
lfqueue->_free(lfqueue->pl, lfqueue->tail); // Last free

lfqueue->size = 0;
}
Expand Down Expand Up @@ -362,4 +376,4 @@ lfqueue_sleep(unsigned int milisec) {

#ifdef __cplusplus
}
#endif
#endif
6 changes: 6 additions & 0 deletions lfqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern "C" {
#endif

typedef struct lfqueue_cas_node_s lfqueue_cas_node_t;
typedef void* (*lfqueue_malloc_fn)(void*, size_t);
typedef void (*lfqueue_free_fn)(void*, void*);

#if defined __GNUC__ || defined __CYGWIN__ || defined __MINGW32__ || defined __APPLE__
#define lfq_bool_t int
Expand All @@ -54,9 +56,13 @@ typedef struct {
lfqueue_cas_node_t *head, *tail, *root_free, *move_free;
volatile size_t size;
volatile lfq_bool_t in_free_mode;
lfqueue_malloc_fn _malloc;
lfqueue_free_fn _free;
void *pl;
} lfqueue_t;

extern int lfqueue_init(lfqueue_t *lfqueue);
extern int lfqueue_init_mf(lfqueue_t *lfqueue, void* pl, lfqueue_malloc_fn lfqueue_malloc, lfqueue_free_fn lfqueue_free);
extern int lfqueue_enq(lfqueue_t *lfqueue, void *value);
extern void* lfqueue_deq(lfqueue_t *lfqueue);
extern void* lfqueue_single_deq(lfqueue_t *lfqueue);
Expand Down

0 comments on commit e095b67

Please sign in to comment.