Added mempool

Sat, 31 Dec 2011 21:05:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 21:05:59 +0100
changeset 13
98ac89e3aa37
parent 12
fe50a85e69e7
child 14
b78e174b6814

Added mempool

gcc.mk file | annotate | diff | comparison | revisions
osx.mk file | annotate | diff | comparison | revisions
suncc.mk file | annotate | diff | comparison | revisions
test/Makefile file | annotate | diff | comparison | revisions
test/list_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
test/mpool_tests.c file | annotate | diff | comparison | revisions
test/mpool_tests.h file | annotate | diff | comparison | revisions
ucx/Makefile file | annotate | diff | comparison | revisions
ucx/dlist.c file | annotate | diff | comparison | revisions
ucx/mpool.c file | annotate | diff | comparison | revisions
ucx/mpool.h file | annotate | diff | comparison | revisions
     1.1 --- a/gcc.mk	Sat Dec 31 19:10:29 2011 +0100
     1.2 +++ b/gcc.mk	Sat Dec 31 21:05:59 2011 +0100
     1.3 @@ -31,7 +31,7 @@
     1.4  AR = ar
     1.5  RM = rm
     1.6  
     1.7 -CFLAGS  = -std=gnu99
     1.8 +CFLAGS  = -std=gnu99 -g
     1.9  LDFLAGS = 
    1.10  ARFLAGS = -r
    1.11  RMFLAGS = -f
     2.1 --- a/osx.mk	Sat Dec 31 19:10:29 2011 +0100
     2.2 +++ b/osx.mk	Sat Dec 31 21:05:59 2011 +0100
     2.3 @@ -31,7 +31,7 @@
     2.4  AR = ar
     2.5  RM = rm
     2.6  
     2.7 -CFLAGS  = -std=gnu99
     2.8 +CFLAGS  = -std=gnu99 -g
     2.9  LDFLAGS = 
    2.10  ARFLAGS = -r
    2.11  RMFLAGS = -f
     3.1 --- a/suncc.mk	Sat Dec 31 19:10:29 2011 +0100
     3.2 +++ b/suncc.mk	Sat Dec 31 21:05:59 2011 +0100
     3.3 @@ -31,7 +31,7 @@
     3.4  AR = ar
     3.5  RM = rm
     3.6  
     3.7 -CFLAGS  = 
     3.8 +CFLAGS  = -g
     3.9  LDFLAGS = 
    3.10  ARFLAGS = -r
    3.11  RMFLAGS = -f
     4.1 --- a/test/Makefile	Sat Dec 31 19:10:29 2011 +0100
     4.2 +++ b/test/Makefile	Sat Dec 31 21:05:59 2011 +0100
     4.3 @@ -28,7 +28,7 @@
     4.4  
     4.5  include ../$(CONF).mk
     4.6  
     4.7 -SRC = main.c list_tests.c
     4.8 +SRC = main.c list_tests.c mpool_tests.c
     4.9  
    4.10  OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
    4.11  
     5.1 --- a/test/list_tests.h	Sat Dec 31 19:10:29 2011 +0100
     5.2 +++ b/test/list_tests.h	Sat Dec 31 21:05:59 2011 +0100
     5.3 @@ -13,7 +13,7 @@
     5.4  #endif
     5.5  
     5.6  int dlist_tests();
     5.7 -int list_tests();;
     5.8 +int list_tests();
     5.9  
    5.10  
    5.11  #ifdef	__cplusplus
     6.1 --- a/test/main.c	Sat Dec 31 19:10:29 2011 +0100
     6.2 +++ b/test/main.c	Sat Dec 31 21:05:59 2011 +0100
     6.3 @@ -30,17 +30,24 @@
     6.4  #include <stdlib.h>
     6.5  
     6.6  #include "list_tests.h"
     6.7 +#include "mpool_tests.h"
     6.8  
     6.9  int main(int argc, char **argv) {
    6.10 -    printf("UCX Tests\n---------\n\n");
    6.11 +    printf("UCX Tests\n---------\n");
    6.12  
    6.13 -    printf("UcxDlist Tests\n");
    6.14 +    printf("\nUcxDlist Tests\n");
    6.15      if(dlist_tests()) {
    6.16 +        fprintf(stderr, "dlist_tests failed\n");
    6.17 +    }
    6.18 +
    6.19 +    printf("\nUcxList Tests\n");
    6.20 +    if(list_tests()) {
    6.21          fprintf(stderr, "list_tests failed\n");
    6.22      }
    6.23 -    printf("UcxList Tests\n");
    6.24 -    if(list_tests()) {
    6.25 -        fprintf(stderr, "list_tests failed\n");
    6.26 +
    6.27 +    printf("\nUcxMemPool Tests\n");
    6.28 +    if(mpool_tests()) {
    6.29 +        fprintf(stderr, "mpool_tests failed\n");
    6.30      }
    6.31      
    6.32      return EXIT_SUCCESS; 
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/mpool_tests.c	Sat Dec 31 21:05:59 2011 +0100
     7.3 @@ -0,0 +1,79 @@
     7.4 +/*
     7.5 + *
     7.6 + */
     7.7 +
     7.8 +#include <stdio.h>
     7.9 +#include <stdlib.h>
    7.10 +#include <string.h>
    7.11 +
    7.12 +#include "mpool_tests.h"
    7.13 +
    7.14 +int cmp_value = 2;
    7.15 +
    7.16 +void int_destructor(int *ptr) {
    7.17 +    if(*ptr != cmp_value) {
    7.18 +        fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
    7.19 +        return;
    7.20 +    }
    7.21 +    cmp_value += 2;
    7.22 +}
    7.23 +
    7.24 +void hello_destructor(char *str) {
    7.25 +    if(strcmp(str, "Hello World!") != 0) {
    7.26 +        fprintf(stderr, "ucx_mempool_reg_destr failed\n");
    7.27 +    }
    7.28 +}
    7.29 +
    7.30 +int mpool_tests() {
    7.31 +    printf("   Test ucx_mempool_new\n");
    7.32 +    UcxMempool *pool = ucx_mempool_new(16);
    7.33 +
    7.34 +    printf("   Test ucx_mempool_malloc\n");
    7.35 +    int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    7.36 +    for(int i=0;i<256;i++) {
    7.37 +        ucx_mempool_malloc(pool, i);
    7.38 +    }
    7.39 +    int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    7.40 +    int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    7.41 +    for(int i=0;i<256;i++) {
    7.42 +        ucx_mempool_malloc(pool, i);
    7.43 +    }
    7.44 +    int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    7.45 +
    7.46 +    *ptr1 = 2;
    7.47 +    *ptr2 = 4;
    7.48 +    *ptr3 = 6;
    7.49 +    *ptr4 = 8;
    7.50 +
    7.51 +    printf("   Test ucx_mempool_set_destr\n");
    7.52 +    ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
    7.53 +    ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
    7.54 +    ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
    7.55 +    ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
    7.56 +
    7.57 +    printf("   Test ucx_mempool_calloc\n");
    7.58 +    char *str = ucx_mempool_calloc(pool, 1, 3);
    7.59 +    if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
    7.60 +        fprintf(stderr, "ucx_mempool_calloc failed\n");
    7.61 +    }
    7.62 +    str[0] = 'O';
    7.63 +    str[1] = 'K';
    7.64 +
    7.65 +    printf("   Test ucx_mempool_realloc\n");
    7.66 +    str = ucx_mempool_realloc(pool, str, 4);
    7.67 +    str[2] = '!';
    7.68 +    str[3] = 0;
    7.69 +    if(strcmp(str, "OK!") != 0) {
    7.70 +        fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
    7.71 +    }
    7.72 +
    7.73 +    printf("   Test ucx_mempool_reg_destr\n");
    7.74 +    char *hello = "Hello World!";
    7.75 +    ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
    7.76 +
    7.77 +    printf("   Test ucx_mempool_free\n");
    7.78 +    ucx_mempool_free(pool);
    7.79 +    
    7.80 +
    7.81 +    return 0;
    7.82 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/mpool_tests.h	Sat Dec 31 21:05:59 2011 +0100
     8.3 @@ -0,0 +1,23 @@
     8.4 +/* 
     8.5 + *
     8.6 + */
     8.7 +
     8.8 +#ifndef MPOOL_TESTS_H
     8.9 +#define	MPOOL_TESTS_H
    8.10 +
    8.11 +#include "ucx/mpool.h"
    8.12 +
    8.13 +#ifdef	__cplusplus
    8.14 +extern "C" {
    8.15 +#endif
    8.16 +
    8.17 +int mpool_tests();
    8.18 +
    8.19 +
    8.20 +
    8.21 +#ifdef	__cplusplus
    8.22 +}
    8.23 +#endif
    8.24 +
    8.25 +#endif	/* MPOOL_TESTS_H */
    8.26 +
     9.1 --- a/ucx/Makefile	Sat Dec 31 19:10:29 2011 +0100
     9.2 +++ b/ucx/Makefile	Sat Dec 31 21:05:59 2011 +0100
     9.3 @@ -29,7 +29,7 @@
     9.4  include ../$(CONF).mk
     9.5  
     9.6  # list of source files
     9.7 -SRC = list.c dlist.c map.c
     9.8 +SRC = list.c dlist.c map.c mpool.c
     9.9  
    9.10  OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
    9.11  
    10.1 --- a/ucx/dlist.c	Sat Dec 31 19:10:29 2011 +0100
    10.2 +++ b/ucx/dlist.c	Sat Dec 31 21:05:59 2011 +0100
    10.3 @@ -99,4 +99,4 @@
    10.4          e = e->prev;
    10.5      }
    10.6      return e;
    10.7 -}
    10.8 \ No newline at end of file
    10.9 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/ucx/mpool.c	Sat Dec 31 21:05:59 2011 +0100
    11.3 @@ -0,0 +1,101 @@
    11.4 +/*
    11.5 + *
    11.6 + */
    11.7 +
    11.8 +#include <stdlib.h>
    11.9 +#include <string.h>
   11.10 +#include <stdio.h>
   11.11 +
   11.12 +#include "mpool.h"
   11.13 +
   11.14 +typedef struct {
   11.15 +    ucx_destructor destructor;
   11.16 +    char c;
   11.17 +} ucx_memchunk;
   11.18 +
   11.19 +typedef struct {
   11.20 +    ucx_destructor destructor;
   11.21 +    void           *ptr;
   11.22 +} ucx_regdestr;
   11.23 +
   11.24 +void ucx_mempool_shared_destr(void* ptr) {
   11.25 +    ucx_regdestr *rd = (ucx_regdestr*)ptr;
   11.26 +    rd->destructor(rd->ptr);
   11.27 +}
   11.28 +
   11.29 +UcxMempool *ucx_mempool_new(size_t n) {
   11.30 +    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool*));
   11.31 +    pool->data = malloc(n * sizeof(void*));
   11.32 +    pool->ndata = 0;
   11.33 +    pool->size = n;
   11.34 +    return pool;
   11.35 +}
   11.36 +
   11.37 +void ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {    
   11.38 +    void **data = realloc(pool->data, newcap*sizeof(void*));
   11.39 +    pool->data = data; 
   11.40 +    pool->size = newcap;
   11.41 +}
   11.42 +
   11.43 +void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
   11.44 +    ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
   11.45 +    if(mem == NULL) {
   11.46 +        return NULL;
   11.47 +    }
   11.48 +
   11.49 +    if (pool->ndata >= pool->size) {
   11.50 +        ucx_mempool_chcap(pool, pool->size + 16);
   11.51 +    }
   11.52 +
   11.53 +    mem->destructor = NULL;
   11.54 +    pool->data[pool->ndata] = mem;
   11.55 +    pool->ndata++;
   11.56 +
   11.57 +    return &mem->c;
   11.58 +}
   11.59 +
   11.60 +void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
   11.61 +    void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
   11.62 +    if(ptr == NULL) {
   11.63 +        return NULL;
   11.64 +    }
   11.65 +    memset(ptr, 0, nelem * elsize);
   11.66 +    return ptr;
   11.67 +}
   11.68 +
   11.69 +void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
   11.70 +    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
   11.71 +    for(int i=0;i<pool->ndata;i++) {
   11.72 +        if(pool->data[i] == mem) {
   11.73 +            mem == realloc(mem, n);
   11.74 +            pool->data[i] = mem;
   11.75 +            return mem;
   11.76 +        }
   11.77 +    }
   11.78 +}
   11.79 +
   11.80 +void ucx_mempool_free(UcxMempool *pool) {
   11.81 +    ucx_memchunk *chunk;
   11.82 +    for(int i=0;i<pool->ndata;i++) {
   11.83 +        chunk = (ucx_memchunk*) pool->data[i];
   11.84 +        if(chunk->destructor != NULL) {
   11.85 +            chunk->destructor(&chunk->c);
   11.86 +        }
   11.87 +        free(chunk);
   11.88 +    }
   11.89 +    free(pool->data);
   11.90 +    free(pool);
   11.91 +}
   11.92 +
   11.93 +void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
   11.94 +    *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
   11.95 +}
   11.96 +
   11.97 +void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
   11.98 +    ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
   11.99 +            pool,
  11.100 +            sizeof(ucx_regdestr));
  11.101 +    rd->destructor = destr;
  11.102 +    rd->ptr;
  11.103 +    ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
  11.104 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/ucx/mpool.h	Sat Dec 31 21:05:59 2011 +0100
    12.3 @@ -0,0 +1,39 @@
    12.4 +/* 
    12.5 + *
    12.6 + */
    12.7 +
    12.8 +#ifndef MPOOL_H
    12.9 +#define	MPOOL_H
   12.10 +
   12.11 +#ifdef	__cplusplus
   12.12 +extern "C" {
   12.13 +#endif
   12.14 +
   12.15 +typedef void(*ucx_destructor)(void*);
   12.16 +
   12.17 +typedef struct {
   12.18 +    void   **data;
   12.19 +    size_t ndata;
   12.20 +    size_t size;
   12.21 +} UcxMempool;
   12.22 +
   12.23 +#define ucx_mempool_new_default() ucx_mempool_new(16)
   12.24 +UcxMempool *ucx_mempool_new(size_t n);
   12.25 +void ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
   12.26 +
   12.27 +void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
   12.28 +void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
   12.29 +void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
   12.30 +
   12.31 +void ucx_mempool_free(UcxMempool *pool);
   12.32 +
   12.33 +void ucx_mempool_set_destr(void *ptr, ucx_destructor func);
   12.34 +void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
   12.35 +
   12.36 +
   12.37 +#ifdef	__cplusplus
   12.38 +}
   12.39 +#endif
   12.40 +
   12.41 +#endif	/* MPOOL_H */
   12.42 +

mercurial