UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 5 * 6 * THE BSD LICENSE 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * Neither the name of the nor the names of its contributors may be 18 * used to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #ifndef BASE_POOL_PVT_H 35 #define BASE_POOL_PVT_H 36 37 #ifndef BASE_POOL_H 38 #include "pool.h" 39 #include <inttypes.h> 40 #endif /* BASE_POOL_H */ 41 42 /* 43 * pool_pvt.h - private definitions for memory pools 44 */ 45 46 /* Definitions */ 47 48 /* 49 * Define PER_POOL_STATISTICS to get detailed statistics for each 50 * pool. 51 */ 52 #ifdef DEBUG 53 #define PER_POOL_STATISTICS 54 #endif 55 56 /* Define POOL_GLOBAL_STATISTICS to get global pool statistics */ 57 //define POOL_GLOBAL_STATISTICS // TODO: redefine 58 59 /* 60 * When POOL_ZERO_DEBUG is defined, overwrite the contents of freed 61 * pool data structures and memory with the POOL_ZERO_DEBUG byte value. 62 */ 63 #ifdef DEBUG 64 #define POOL_ZERO_DEBUG 0xa 65 #endif 66 67 /* 68 * DEFAULT_BLOCK_SIZE specifies the minimum granularity, in bytes, used 69 * in allocating memory from the heap for use with a pool. A request 70 * for more than DEFAULT_BLOCK_SIZE bytes from a pool will cause a block 71 * of that size to be allocated from the heap. 72 */ 73 #define DEFAULT_BLOCK_SIZE (32 * 1024) 74 75 /* 76 * The pool_recycle() mechanism keeps a list of free blocks associated 77 * with each pool, in order to avoid global locking when doing the 78 * pool_recycle() operation, or when subsequently allocating memory 79 * from the pool. DEFAULT_RETENTION_SIZE and DEFAULT_RETENTION_NUM 80 * specify the maximum number of bytes and blocks, respectively, that 81 * will be kept on a per-pool free list. 82 */ 83 #define DEFAULT_RETENTION_SIZE (DEFAULT_BLOCK_SIZE * 2) 84 #define DEFAULT_RETENTION_NUM 2 85 86 /* WORD_SIZE 8 sets us up for 8 byte alignment. */ 87 #define WORD_SIZE 8 88 #undef ALIGN 89 #define ALIGN(x) ( (x + WORD_SIZE-1) & (~(WORD_SIZE-1)) ) 90 91 /* Types */ 92 93 /* 94 * pool_stats_t 95 * This structure contains per pool statistics. 96 */ 97 #ifdef PER_POOL_STATISTICS 98 typedef struct pool_stats_t pool_stats_t; 99 struct pool_stats_t { 100 PRUint32 poolId; /* pool id */ 101 PRUint32 maxAlloc; /* maximum bytes ever used from pool */ 102 PRUint32 allocCnt; /* count of memory allocations from pool */ 103 PRUint32 freeCnt; /* count of pool memory free operations */ 104 PRUint32 blkAlloc; /* count of block allocations */ 105 PRUint32 blkFree; /* count of blocks freed (on pool_recycle) */ 106 PRThread *thread; /* last thread to use pool */ 107 PRTime created; /* time of pool creation */ 108 }; 109 #endif /* PER_POOL_STATISTICS */ 110 111 typedef struct pool_config_t pool_config_t; 112 struct pool_config_t { 113 int32_t block_size; /* size of blocks to allocate */ 114 int32_t retain_size; /* maximum bytes kept on per-pool free list */ 115 int32_t retain_num; /* maximum blocks kept on per-pool free list */ 116 }; 117 118 #define POOL_CONFIG_INIT { \ 119 DEFAULT_BLOCK_SIZE, /* block_size */ \ 120 DEFAULT_RETENTION_SIZE, /* retain_size */ \ 121 DEFAULT_RETENTION_NUM, /* retain_num */ \ 122 } 123 124 /* 125 * block_t 126 * When the user allocates space, a DEFAULT_BLOCK_SIZE (or larger) block 127 * is created in the pool. This block is used until all the space is 128 * eaten within it. When all the space is gone, a new block is created. 129 */ 130 typedef struct block_t block_t; 131 struct block_t { 132 char *data; /* the real alloc'd space */ 133 char *start; /* first free byte in block */ 134 char *end; /* ptr to end of block */ 135 block_t *next; /* ptr to next block */ 136 }; 137 138 #define POOL_PTR_IN_BLOCK(blk, ptr) \ 139 (((char *)(ptr) < (blk)->end) && ((char *)(ptr) >= (blk)->data)) 140 141 /* 142 * pool_t 143 * A pool is a collection of blocks. The blocks consist of multiple 144 * allocations of memory, but a single allocation cannot be freed by 145 * itself. Once the memory is allocated it is allocated until the 146 * entire pool is freed. 147 */ 148 typedef struct pool_t pool_t; 149 struct pool_t { 150 block_t *curr_block; /* current block being used */ 151 block_t *used_blocks; /* blocks that are all used up */ 152 block_t *free_blocks; /* blocks that are free */ 153 int32_t free_size; /* number of bytes in free_blocks */ 154 int32_t free_num; /* number of blocks in free_blocks */ 155 size_t size; /* size of memory in pool */ 156 pool_t *next; /* known_pools list */ 157 #ifdef PER_POOL_STATISTICS 158 pool_stats_t stats; /* statistics for this pool */ 159 #endif /* PER_POOL_STATISTICS */ 160 }; 161 162 typedef struct pool_global_stats_t pool_global_stats_t; 163 struct pool_global_stats_t { 164 //PRLock *lock; /* lock for access to poolList */ // TODO: remove 165 pool_t *poolList; /* list of known pools */ 166 int32_t createCnt; /* count of pools created */ 167 int32_t destroyCnt; /* count of pools destroyed */ 168 #ifdef POOL_GLOBAL_STATISTICS 169 PRUint32 blkAlloc; /* count of block allocations from heap */ 170 PRUint32 blkFree; /* count of blocks freed to heap */ 171 #endif /* POOL_GLOBAL_STATISTICS */ 172 }; 173 174 /* Private functions for inspecting pool configuration/statistics */ 175 176 NSAPI_PUBLIC pool_config_t *pool_getConfig(void); 177 178 NSAPI_PUBLIC pool_global_stats_t *pool_getGlobalStats(void); 179 180 #ifdef PER_POOL_STATISTICS 181 NSAPI_PUBLIC pool_stats_t *pool_getPoolStats(pool_handle_t *pool_handle); 182 #endif 183 184 #endif /* BASE_POOL_PVT_H */ 185