ucx/mempool.c

Mon, 15 Jul 2013 16:59:52 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 15 Jul 2013 16:59:52 +0200
changeset 113
8693d7874773
parent 103
08018864fb91
child 116
234920008754
permissions
-rw-r--r--

added mempool allocator

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include <stdlib.h>
    30 #include <string.h>
    31 #include <stdio.h>
    32 #ifdef __cplusplus
    33 #define __STDC_FORMAT_MACROS
    34 #endif
    35 #include <inttypes.h>
    37 #include "mempool.h"
    39 typedef struct {
    40     ucx_destructor destructor;
    41     char c;
    42 } ucx_memchunk;
    44 typedef struct {
    45     ucx_destructor destructor;
    46     void           *ptr;
    47 } ucx_regdestr;
    49 void ucx_mempool_shared_destr(void* ptr) {
    50     ucx_regdestr *rd = (ucx_regdestr*)ptr;
    51     rd->destructor(rd->ptr);
    52 }
    54 UcxMempool *ucx_mempool_new(size_t n) {
    55     UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
    56     if (pool == NULL) return NULL;
    58     pool->data = (void**) malloc(n * sizeof(void*));
    59     if (pool->data == NULL) {
    60         free(pool);
    61         return NULL;
    62     }
    64     pool->ndata = 0;
    65     pool->size = n;
    66     return pool;
    67 }
    69 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
    70     void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
    71     if (data == NULL) {
    72         return 1;
    73     } else {
    74         pool->data = data; 
    75         pool->size = newcap;
    76         return EXIT_SUCCESS;
    77     }
    78 }
    80 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    81     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    82     if (mem == NULL) return NULL;
    84     if (pool->ndata >= pool->size) {
    85         ucx_mempool_chcap(pool, pool->size + 16);
    86      }
    88     mem->destructor = NULL;
    89     pool->data[pool->ndata] = mem;
    90     pool->ndata++;
    92     return &mem->c;
    93 }
    95 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
    96     void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
    97     if(ptr == NULL) {
    98         return NULL;
    99     }
   100     memset(ptr, 0, nelem * elsize);
   101     return ptr;
   102 }
   104 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
   105     char *mem = ((char*)ptr) - sizeof(ucx_destructor);
   106     char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
   107     if (newm == NULL) return NULL;
   108     if (mem != newm) {
   109         for(size_t i=0 ; i < pool->ndata ; i++) {
   110             if(pool->data[i] == mem) {
   111                 pool->data[i] = newm;
   112                 return newm + sizeof(ucx_destructor);
   113             }
   114         }
   115         fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
   116           (intptr_t)ptr, (intptr_t)pool);
   117         exit(1);
   118     } else {
   119         return newm + sizeof(ucx_destructor);
   120     }
   121 }
   123 void ucx_mempool_free(UcxMempool *pool, void *ptr) {
   124     ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
   125     for(size_t i=0 ; i<pool->ndata ; i++) {
   126         if(chunk == pool->data[i]) {
   127             if(chunk->destructor != NULL) {
   128                 chunk->destructor(&chunk->c);
   129             }
   130             free(chunk);
   131             size_t last_index = pool->ndata - 1;
   132             if(i != last_index) {
   133                 pool->data[i] = pool->data[last_index];
   134             }
   135             pool->ndata--;
   136             return;
   137         }
   138     }
   139     fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
   140             (intptr_t)ptr, (intptr_t)pool);
   141     exit(1);
   142 }
   144 void ucx_mempool_destroy(UcxMempool *pool) {
   145     ucx_memchunk *chunk;
   146     for(size_t i=0 ; i<pool->ndata ; i++) {
   147         chunk = (ucx_memchunk*) pool->data[i];
   148         if(chunk) {
   149             if(chunk->destructor != NULL) {
   150                 chunk->destructor(&chunk->c);
   151             }
   152             free(chunk);
   153         }
   154     }
   155     free(pool->data);
   156     free(pool);
   157 }
   159 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
   160     *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
   161 }
   163 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
   164     ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
   165             pool,
   166             sizeof(ucx_regdestr));
   167     rd->destructor = destr;
   168     rd->ptr = ptr;
   169     ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
   170 }
   172 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) {
   173     UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc(
   174             pool, sizeof(UcxAllocator));
   175     if(!allocator) {
   176         return NULL;
   177     }
   178     allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
   179     allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
   180     allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
   181     allocator->free = (ucx_allocator_free)ucx_mempool_free;
   182     allocator->pool = pool;
   183     return allocator;
   184 }

mercurial