Hi, This is an attempt to make a virtual memory space inside the pool.
MT.
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ #ifndef APR_MALLOC_H #define APR_MALLOC_H #include "apr_pools.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ typedef struct apr_malloc_t apr_malloc_t; /* Initialize the memory management from the current pool */ APR_DECLARE(apr_status_t) apr_malloc_init(apr_malloc_t **m, apr_size_t len, apr_pool_t *contxt); APR_DECLARE(void *) apr_malloc(apr_malloc_t *m, apr_size_t reqsize); APR_DECLARE(void *) apr_calloc(apr_malloc_t *m, apr_size_t reqsize); APR_DECLARE(void *) apr_realloc(apr_malloc_t *m, void *mem, apr_size_t newsize); APR_DECLARE(void) apr_free(apr_malloc_t *m, void *mem); #ifdef __cplusplus } #endif #endif /* ! APR_MALLOC_H */
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ #include "apr_pools.h" #include "apr_malloc.h" #define MALLOC_GRANUALITY 128 #define MALLOC_SENTINEL 0xfed41b76 struct apr_malloc_t { apr_uint32_t *bitmap; apr_size_t nbits; apr_pool_t *pool; char *memory; }; typedef struct apr_mchunk_t apr_mchunk_t; struct apr_mchunk_t { apr_uint32_t sentinel; apr_uint32_t nbits; char memory[1]; }; static int getbit(apr_uint32_t *set, int number) { set += number / 32; return (*set & (1 << (number % 32))) != 0; /* 0 or 1 */ } static void setbit(apr_uint32_t *set, int number) { set += number / 32; *set |= 1 << (number % 32); /* set bit */ } static void clearbit(apr_uint32_t *set, int number) { set += number / 32; *set &= ~(1 << (number % 32)); /* clear bit*/ } static void setbits(apr_uint32_t *set, int from, int to) { int x; for (x = from; x < to; x++) setbit(set, x); } static void clearbits(apr_uint32_t *set, int from, int to) { int x; for (x = from; x < to; x++) clearbit(set, x); } static int findseq(apr_uint32_t *set, int size, int nbits) { register int x, y, cnt; if ((size * 32) < nbits) return 0; for ( x = 0; x < (size * 32 - nbits); x++) { if (!getbit(set, x)) { if (!getbit(set, x + nbits)) { cnt = 1; for (y = 1; y < nbits; y++) { if (!getbit(set, x + y)) cnt++; } if (cnt == nbits) return x; } else x += nbits; } } return -1; } APR_DECLARE(apr_status_t) apr_malloc_init(apr_malloc_t **new, apr_size_t len, apr_pool_t *cont) { (*new) = (apr_malloc_t *)apr_pcalloc(cont, sizeof(apr_malloc_t)); if (!(*new)) return APR_ENOMEM; (*new)->nbits = len / MALLOC_GRANUALITY + 1; (*new)->bitmap = apr_pcalloc(cont, (*new)->nbits * 4); (*new)->memory = apr_palloc(cont, (*new)->nbits * MALLOC_GRANUALITY); if (!(*new)->memory) return APR_ENOMEM; return APR_SUCCESS; } APR_DECLARE(void *) apr_malloc(apr_malloc_t *m, apr_size_t reqsize) { void *r = NULL; apr_size_t nbits = (reqsize + sizeof(apr_mchunk_t)) / MALLOC_GRANUALITY + 1; int ff; ff = findseq(m->bitmap, m->nbits, nbits); if (ff >= 0) { apr_mchunk_t *c; r = m->memory + ff * MALLOC_GRANUALITY; setbits(m->bitmap, ff, ff + nbits); c = r; c->nbits = nbits; c->sentinel = MALLOC_SENTINEL; return c->memory; } return r; } APR_DECLARE(void *) apr_calloc(apr_malloc_t *m, apr_size_t reqsize) { void *r = apr_malloc(m, reqsize); if (r) memset(r, 0, reqsize); return r; } APR_DECLARE(void *) apr_realloc(apr_malloc_t *m, void *mem, apr_size_t newsize) { char *addr = (char *)mem; apr_mchunk_t *c; void *r = NULL; c = (apr_mchunk_t *)(addr - sizeof(apr_mchunk_t) + sizeof(char *)); if (c->sentinel == MALLOC_SENTINEL) { apr_size_t start = (addr - m->memory) / MALLOC_GRANUALITY; r = apr_malloc(m, newsize); if (r) { memcpy(r, c->memory, c->nbits * MALLOC_GRANUALITY); clearbits(m->bitmap, start, c->nbits); c->sentinel = 0; } } return r; } APR_DECLARE(void) apr_free(apr_malloc_t *m, void *mem) { apr_mchunk_t *c; char *addr = (char *)mem; c = (apr_mchunk_t *)(addr - sizeof(apr_mchunk_t) + sizeof(char *)); if (c->sentinel == MALLOC_SENTINEL) { apr_size_t start = (addr - m->memory) / MALLOC_GRANUALITY; if (start >= 0 && start < m->nbits) { clearbits(m->bitmap, start, c->nbits); c->sentinel = 0; } } }
