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 #include <cx/allocator.h> 41 #endif /* BASE_POOL_H */ 42 43 /* 44 * pool_pvt.h - private definitions for memory pools 45 */ 46 47 /* Definitions */ 48 49 /* 50 * Define PER_POOL_STATISTICS to get detailed statistics for each 51 * pool. 52 */ 53 #ifdef DEBUG 54 #define PER_POOL_STATISTICS 55 #endif 56 57 /* Define POOL_GLOBAL_STATISTICS to get global pool statistics */ 58 //define POOL_GLOBAL_STATISTICS // TODO: redefine 59 60 /* 61 * When POOL_ZERO_DEBUG is defined, overwrite the contents of freed 62 * pool data structures and memory with the POOL_ZERO_DEBUG byte value. 63 */ 64 #ifdef DEBUG 65 #define POOL_ZERO_DEBUG 0xa 66 #endif 67 68 /* 69 * DEFAULT_BLOCK_SIZE specifies the minimum granularity, in bytes, used 70 * in allocating memory from the heap for use with a pool. A request 71 * for more than DEFAULT_BLOCK_SIZE bytes from a pool will cause a block 72 * of that size to be allocated from the heap. 73 */ 74 #define DEFAULT_BLOCK_SIZE (32 * 1024) 75 76 /* 77 * The pool_recycle() mechanism keeps a list of free blocks associated 78 * with each pool, in order to avoid global locking when doing the 79 * pool_recycle() operation, or when subsequently allocating memory 80 * from the pool. DEFAULT_RETENTION_SIZE and DEFAULT_RETENTION_NUM 81 * specify the maximum number of bytes and blocks, respectively, that 82 * will be kept on a per-pool free list. 83 */ 84 #define DEFAULT_RETENTION_SIZE (DEFAULT_BLOCK_SIZE * 2) 85 #define DEFAULT_RETENTION_NUM 2 86 87 /* WORD_SIZE 8 sets us up for 8 byte alignment. */ 88 #define WORD_SIZE 8 89 #undef ALIGN 90 #define ALIGN(x) ( (x + WORD_SIZE-1) & (~(WORD_SIZE-1)) ) 91 92 /* Types */ 93 94 /* 95 * pool_stats_t 96 * This structure contains per pool statistics. 97 */ 98 #ifdef PER_POOL_STATISTICS 99 typedef struct pool_stats_t pool_stats_t; 100 struct pool_stats_t { 101 PRUint32 poolId; /* pool id */ 102 PRUint32 maxAlloc; /* maximum bytes ever used from pool */ 103 PRUint32 allocCnt; /* count of memory allocations from pool */ 104 PRUint32 freeCnt; /* count of pool memory free operations */ 105 PRUint32 blkAlloc; /* count of block allocations */ 106 PRUint32 blkFree; /* count of blocks freed (on pool_recycle) */ 107 PRThread *thread; /* last thread to use pool */ 108 PRTime created; /* time of pool creation */ 109 }; 110 #endif /* PER_POOL_STATISTICS */ 111 112 typedef struct pool_config_t pool_config_t; 113 struct pool_config_t { 114 int32_t block_size; /* size of blocks to allocate */ 115 int32_t retain_size; /* maximum bytes kept on per-pool free list */ 116 int32_t retain_num; /* maximum blocks kept on per-pool free list */ 117 }; 118 119 #define POOL_CONFIG_INIT { \ 120 DEFAULT_BLOCK_SIZE, /* block_size */ \ 121 DEFAULT_RETENTION_SIZE, /* retain_size */ \ 122 DEFAULT_RETENTION_NUM, /* retain_num */ \ 123 } 124 125 /* 126 * block_t 127 * When the user allocates space, a DEFAULT_BLOCK_SIZE (or larger) block 128 * is created in the pool. This block is used until all the space is 129 * eaten within it. When all the space is gone, a new block is created. 130 */ 131 typedef struct block_t block_t; 132 struct block_t { 133 char *data; /* the real alloc'd space */ 134 char *start; /* first free byte in block */ 135 char *end; /* ptr to end of block */ 136 block_t *next; /* ptr to next block */ 137 }; 138 139 #define POOL_PTR_IN_BLOCK(blk, ptr) \ 140 (((char *)(ptr) < (blk)->end) && ((char *)(ptr) >= (blk)->data)) 141 142 /* 143 * pool_t 144 * A pool is a collection of blocks. The blocks consist of multiple 145 * allocations of memory, but a single allocation cannot be freed by 146 * itself. Once the memory is allocated it is allocated until the 147 * entire pool is freed. 148 */ 149 typedef struct pool_t pool_t; 150 struct pool_t { 151 CxAllocator allocator; /* ucx allocator interface */ 152 block_t *curr_block; /* current block being used */ 153 block_t *used_blocks; /* blocks that are all used up */ 154 block_t *free_blocks; /* blocks that are free */ 155 int32_t free_size; /* number of bytes in free_blocks */ 156 int32_t free_num; /* number of blocks in free_blocks */ 157 size_t size; /* size of memory in pool */ 158 pool_t *next; /* known_pools list */ 159 #ifdef PER_POOL_STATISTICS 160 pool_stats_t stats; /* statistics for this pool */ 161 #endif /* PER_POOL_STATISTICS */ 162 }; 163 164 typedef struct pool_global_stats_t pool_global_stats_t; 165 struct pool_global_stats_t { 166 //PRLock *lock; /* lock for access to poolList */ // TODO: remove 167 pool_t *poolList; /* list of known pools */ 168 int32_t createCnt; /* count of pools created */ 169 int32_t destroyCnt; /* count of pools destroyed */ 170 #ifdef POOL_GLOBAL_STATISTICS 171 PRUint32 blkAlloc; /* count of block allocations from heap */ 172 PRUint32 blkFree; /* count of blocks freed to heap */ 173 #endif /* POOL_GLOBAL_STATISTICS */ 174 }; 175 176 /* Private functions for inspecting pool configuration/statistics */ 177 178 NSAPI_PUBLIC pool_config_t *pool_getConfig(void); 179 180 NSAPI_PUBLIC pool_global_stats_t *pool_getGlobalStats(void); 181 182 #ifdef PER_POOL_STATISTICS 183 NSAPI_PUBLIC pool_stats_t *pool_getPoolStats(pool_handle_t *pool_handle); 184 #endif 185 186 #endif /* BASE_POOL_PVT_H */ 187