fixed ultra fail + renamed files from mpool to mempool

Sat, 31 Dec 2011 22:46:27 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Dec 2011 22:46:27 +0100
changeset 16
b4769e4eb4d1
parent 15
2dc4c688c262
child 17
2e7050c3a18e

fixed ultra fail + renamed files from mpool to mempool

.hgignore file | annotate | diff | comparison | revisions
ucx/Makefile file | annotate | diff | comparison | revisions
ucx/mempool.c file | annotate | diff | comparison | revisions
ucx/mempool.h file | annotate | diff | comparison | revisions
ucx/mpool.c file | annotate | diff | comparison | revisions
ucx/mpool.h file | annotate | diff | comparison | revisions
     1.1 --- a/.hgignore	Sat Dec 31 22:41:16 2011 +0100
     1.2 +++ b/.hgignore	Sat Dec 31 22:46:27 2011 +0100
     1.3 @@ -1,3 +1,4 @@
     1.4  syntax:regexp
     1.5  ^nbproject/.*$
     1.6  ^build/.*$
     1.7 +^core$
     2.1 --- a/ucx/Makefile	Sat Dec 31 22:41:16 2011 +0100
     2.2 +++ b/ucx/Makefile	Sat Dec 31 22:46:27 2011 +0100
     2.3 @@ -29,7 +29,7 @@
     2.4  include ../$(CONF).mk
     2.5  
     2.6  # list of source files
     2.7 -SRC = list.c dlist.c map.c mpool.c
     2.8 +SRC = list.c dlist.c map.c mempool.c
     2.9  
    2.10  OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
    2.11  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/ucx/mempool.c	Sat Dec 31 22:46:27 2011 +0100
     3.3 @@ -0,0 +1,119 @@
     3.4 +/*
     3.5 + *
     3.6 + */
     3.7 +
     3.8 +#include <stdlib.h>
     3.9 +#include <string.h>
    3.10 +#include <stdio.h>
    3.11 +#include <errno.h>
    3.12 +
    3.13 +#include "mpool.h"
    3.14 +
    3.15 +typedef struct {
    3.16 +    ucx_destructor destructor;
    3.17 +    char c;
    3.18 +} ucx_memchunk;
    3.19 +
    3.20 +typedef struct {
    3.21 +    ucx_destructor destructor;
    3.22 +    void           *ptr;
    3.23 +} ucx_regdestr;
    3.24 +
    3.25 +void ucx_mempool_shared_destr(void* ptr) {
    3.26 +    ucx_regdestr *rd = (ucx_regdestr*)ptr;
    3.27 +    rd->destructor(rd->ptr);
    3.28 +}
    3.29 +
    3.30 +UcxMempool *ucx_mempool_new(size_t n) {
    3.31 +    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
    3.32 +    if (pool == NULL) return NULL;
    3.33 +    
    3.34 +    pool->data = malloc(n * sizeof(void*));
    3.35 +    if (pool->data == NULL) {
    3.36 +        free(pool);
    3.37 +        return NULL;
    3.38 +    }
    3.39 +    
    3.40 +    pool->ndata = 0;
    3.41 +    pool->size = n;
    3.42 +    return pool;
    3.43 +}
    3.44 +
    3.45 +int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
    3.46 +    void **data = realloc(pool->data, newcap*sizeof(void*));
    3.47 +    if (data == NULL) {
    3.48 +        return ENOMEM;
    3.49 +    } else {
    3.50 +        pool->data = data; 
    3.51 +        pool->size = newcap;
    3.52 +        return EXIT_SUCCESS;
    3.53 +    }
    3.54 +}
    3.55 +
    3.56 +void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    3.57 +    ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    3.58 +    if (mem == NULL) return NULL;
    3.59 +
    3.60 +    if (pool->ndata >= pool->size) {
    3.61 +        ucx_mempool_chcap(pool, pool->size + 16);
    3.62 +     }
    3.63 +
    3.64 +    mem->destructor = NULL;
    3.65 +    pool->data[pool->ndata] = mem;
    3.66 +    pool->ndata++;
    3.67 +
    3.68 +    return &mem->c;
    3.69 +}
    3.70 +
    3.71 +void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
    3.72 +    void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
    3.73 +    if(ptr == NULL) {
    3.74 +        return NULL;
    3.75 +    }
    3.76 +    memset(ptr, 0, nelem * elsize);
    3.77 +    return ptr;
    3.78 +}
    3.79 +
    3.80 +void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
    3.81 +    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
    3.82 +    char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
    3.83 +    if (newm == NULL) return NULL;
    3.84 +    if (mem != newm) {
    3.85 +        for(int i=0;i<pool->ndata;i++) {
    3.86 +            if(pool->data[i] == mem) {
    3.87 +                pool->data[i] = newm;
    3.88 +                return ((char*) newm) + sizeof(ucx_destructor);
    3.89 +            }
    3.90 +        }
    3.91 +        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
    3.92 +        exit(1);
    3.93 +    } else {
    3.94 +        return ((char*) newm) + sizeof(ucx_destructor);
    3.95 +    }
    3.96 +}
    3.97 +
    3.98 +void ucx_mempool_free(UcxMempool *pool) {
    3.99 +    ucx_memchunk *chunk;
   3.100 +    for(int i=0;i<pool->ndata;i++) {
   3.101 +        chunk = (ucx_memchunk*) pool->data[i];
   3.102 +        if(chunk->destructor != NULL) {
   3.103 +            chunk->destructor(&chunk->c);
   3.104 +        }
   3.105 +        free(chunk);
   3.106 +    }
   3.107 +    free(pool->data);
   3.108 +    free(pool);
   3.109 +}
   3.110 +
   3.111 +void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
   3.112 +    *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
   3.113 +}
   3.114 +
   3.115 +void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
   3.116 +    ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
   3.117 +            pool,
   3.118 +            sizeof(ucx_regdestr));
   3.119 +    rd->destructor = destr;
   3.120 +    rd->ptr = ptr;
   3.121 +    ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
   3.122 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/ucx/mempool.h	Sat Dec 31 22:46:27 2011 +0100
     4.3 @@ -0,0 +1,39 @@
     4.4 +/* 
     4.5 + *
     4.6 + */
     4.7 +
     4.8 +#ifndef MPOOL_H
     4.9 +#define	MPOOL_H
    4.10 +
    4.11 +#ifdef	__cplusplus
    4.12 +extern "C" {
    4.13 +#endif
    4.14 +
    4.15 +typedef void(*ucx_destructor)(void*);
    4.16 +
    4.17 +typedef struct {
    4.18 +    void   **data;
    4.19 +    size_t ndata;
    4.20 +    size_t size;
    4.21 +} UcxMempool;
    4.22 +
    4.23 +#define ucx_mempool_new_default() ucx_mempool_new(16)
    4.24 +UcxMempool *ucx_mempool_new(size_t n);
    4.25 +int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
    4.26 +
    4.27 +void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
    4.28 +void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
    4.29 +void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
    4.30 +
    4.31 +void ucx_mempool_free(UcxMempool *pool);
    4.32 +
    4.33 +void ucx_mempool_set_destr(void *ptr, ucx_destructor func);
    4.34 +void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
    4.35 +
    4.36 +
    4.37 +#ifdef	__cplusplus
    4.38 +}
    4.39 +#endif
    4.40 +
    4.41 +#endif	/* MPOOL_H */
    4.42 +
     5.1 --- a/ucx/mpool.c	Sat Dec 31 22:41:16 2011 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,118 +0,0 @@
     5.4 -/*
     5.5 - *
     5.6 - */
     5.7 -
     5.8 -#include <stdlib.h>
     5.9 -#include <string.h>
    5.10 -#include <stdio.h>
    5.11 -#include <errno.h>
    5.12 -
    5.13 -#include "mpool.h"
    5.14 -
    5.15 -typedef struct {
    5.16 -    ucx_destructor destructor;
    5.17 -    char c;
    5.18 -} ucx_memchunk;
    5.19 -
    5.20 -typedef struct {
    5.21 -    ucx_destructor destructor;
    5.22 -    void           *ptr;
    5.23 -} ucx_regdestr;
    5.24 -
    5.25 -void ucx_mempool_shared_destr(void* ptr) {
    5.26 -    ucx_regdestr *rd = (ucx_regdestr*)ptr;
    5.27 -    rd->destructor(rd->ptr);
    5.28 -}
    5.29 -
    5.30 -UcxMempool *ucx_mempool_new(size_t n) {
    5.31 -    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
    5.32 -    if (pool == NULL) return NULL;
    5.33 -    
    5.34 -    pool->data = malloc(n * sizeof(void*));
    5.35 -    if (pool->data == NULL) {
    5.36 -        free(pool);
    5.37 -        return NULL;
    5.38 -    }
    5.39 -    
    5.40 -    pool->ndata = 0;
    5.41 -    pool->size = n;
    5.42 -    return pool;
    5.43 -}
    5.44 -
    5.45 -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
    5.46 -    void **data = realloc(pool->data, newcap*sizeof(void*));
    5.47 -    if (data == NULL) {
    5.48 -        return ENOMEM;
    5.49 -    } else {
    5.50 -        pool->data = data; 
    5.51 -        pool->size = newcap;
    5.52 -        return EXIT_SUCCESS;
    5.53 -    }
    5.54 -}
    5.55 -
    5.56 -void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    5.57 -    ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    5.58 -    if (mem == NULL) return NULL;
    5.59 -
    5.60 -    if (pool->ndata >= pool->size) {
    5.61 -        ucx_mempool_chcap(pool, pool->size + 16);
    5.62 -     }
    5.63 -
    5.64 -    mem->destructor = NULL;
    5.65 -    pool->data[pool->ndata] = mem;
    5.66 -    pool->ndata++;
    5.67 -
    5.68 -    return &mem->c;
    5.69 -}
    5.70 -
    5.71 -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
    5.72 -    void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
    5.73 -    if(ptr == NULL) {
    5.74 -        return NULL;
    5.75 -    }
    5.76 -    memset(ptr, 0, nelem * elsize);
    5.77 -    return ptr;
    5.78 -}
    5.79 -
    5.80 -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
    5.81 -    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
    5.82 -    char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
    5.83 -    if (newm == NULL) return NULL;
    5.84 -    if (mem != newm) {
    5.85 -        for(int i=0;i<pool->ndata;i++) {
    5.86 -            if(pool->data[i] == mem) {
    5.87 -                pool->data[i] = newm;
    5.88 -                break;
    5.89 -            }
    5.90 -        }
    5.91 -        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
    5.92 -        exit(1);
    5.93 -    }
    5.94 -    return ((char*) newm) + sizeof(ucx_destructor);
    5.95 -}
    5.96 -
    5.97 -void ucx_mempool_free(UcxMempool *pool) {
    5.98 -    ucx_memchunk *chunk;
    5.99 -    for(int i=0;i<pool->ndata;i++) {
   5.100 -        chunk = (ucx_memchunk*) pool->data[i];
   5.101 -        if(chunk->destructor != NULL) {
   5.102 -            chunk->destructor(&chunk->c);
   5.103 -        }
   5.104 -        free(chunk);
   5.105 -    }
   5.106 -    free(pool->data);
   5.107 -    free(pool);
   5.108 -}
   5.109 -
   5.110 -void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
   5.111 -    *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
   5.112 -}
   5.113 -
   5.114 -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
   5.115 -    ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
   5.116 -            pool,
   5.117 -            sizeof(ucx_regdestr));
   5.118 -    rd->destructor = destr;
   5.119 -    rd->ptr = ptr;
   5.120 -    ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
   5.121 -}
     6.1 --- a/ucx/mpool.h	Sat Dec 31 22:41:16 2011 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,39 +0,0 @@
     6.4 -/* 
     6.5 - *
     6.6 - */
     6.7 -
     6.8 -#ifndef MPOOL_H
     6.9 -#define	MPOOL_H
    6.10 -
    6.11 -#ifdef	__cplusplus
    6.12 -extern "C" {
    6.13 -#endif
    6.14 -
    6.15 -typedef void(*ucx_destructor)(void*);
    6.16 -
    6.17 -typedef struct {
    6.18 -    void   **data;
    6.19 -    size_t ndata;
    6.20 -    size_t size;
    6.21 -} UcxMempool;
    6.22 -
    6.23 -#define ucx_mempool_new_default() ucx_mempool_new(16)
    6.24 -UcxMempool *ucx_mempool_new(size_t n);
    6.25 -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
    6.26 -
    6.27 -void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
    6.28 -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
    6.29 -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
    6.30 -
    6.31 -void ucx_mempool_free(UcxMempool *pool);
    6.32 -
    6.33 -void ucx_mempool_set_destr(void *ptr, ucx_destructor func);
    6.34 -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
    6.35 -
    6.36 -
    6.37 -#ifdef	__cplusplus
    6.38 -}
    6.39 -#endif
    6.40 -
    6.41 -#endif	/* MPOOL_H */
    6.42 -

mercurial