Atrinik Server 2.5
include/mempool.h
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #ifndef MEMPOOL_H
00031 #define MEMPOOL_H
00032 
00034 #define MEMPOOL_TRACKING
00035 
00042 /*#define MEMPOOL_OBJECT_TRACKING*/
00043 
00049 struct mempool_chunk
00050 {
00051     /* This struct must always be padded for longword alignment of the data coming behind it.
00052      * Not a problem as long as we only keep a single pointer here, but be careful
00053      * if adding more data. */
00054 
00058     struct mempool_chunk *next;
00059 
00060 #ifdef MEMPOOL_OBJECT_TRACKING
00061 
00062     struct mempool_chunk *obj_prev;
00063 
00065     struct mempool_chunk *obj_next;
00066 
00068     uint32 flags;
00069 
00071     uint32 pool_id;
00072 
00074     uint32 id;
00075 #endif
00076 };
00077 
00078 /* Optional initialisator to be called when expanding */
00079 typedef void (*chunk_initialisator) (void *ptr);
00080 /* Optional deinitialisator to be called when freeing */
00081 typedef void (*chunk_deinitialisator) (void *ptr);
00082 /* Optional constructor to be called when getting chunks */
00083 typedef void (*chunk_constructor) (void *ptr);
00084 /* Optional destructor to be called when returning chunks */
00085 typedef void (*chunk_destructor) (void *ptr);
00086 
00087 /* Definitions used for array handling */
00088 #define MEMPOOL_NROF_FREELISTS 8
00089 /* = 256 if NROF_FREELISTS == 8 */
00090 #define MEMPOOL_MAX_ARRAYSIZE (1 << MEMPOOL_NROF_FREELISTS)
00091 
00093 struct mempool
00094 {
00096     const char *chunk_description;
00097 
00099     uint32 expand_size;
00100 
00102     uint32 chunksize;
00103 
00105     uint32 flags;
00106 
00108     chunk_initialisator initialisator;
00109 
00111     chunk_deinitialisator deinitialisator;
00112 
00114     chunk_constructor constructor;
00115 
00117     chunk_destructor destructor;
00118 
00120     struct mempool_chunk *freelist[MEMPOOL_NROF_FREELISTS];
00121 
00123     uint32 nrof_free[MEMPOOL_NROF_FREELISTS];
00124 
00126     uint32 nrof_allocated[MEMPOOL_NROF_FREELISTS];
00127 #ifdef MEMPOOL_TRACKING
00128 
00129     struct puddle_info *first_puddle_info;
00130 #endif
00131 };
00132 
00133 #ifdef MEMPOOL_TRACKING
00134 
00135 struct puddle_info
00136 {
00138     struct puddle_info *next;
00139 
00141     struct mempool_chunk *first_chunk;
00142 
00144     struct mempool_chunk *first_free;
00145 
00147     struct mempool_chunk *last_free;
00148 
00150     uint32 nrof_free;
00151 };
00152 
00153 extern struct mempool *pool_puddle;
00154 #endif
00155 
00157 #define MAX_NROF_MEMPOOLS 32
00158 
00160 #define MEM_POOLDATA(ptr) (((struct mempool_chunk *)(ptr)) - 1)
00161 
00162 #define MEM_USERDATA(ptr) ((void *)(((struct mempool_chunk *)(ptr)) + 1))
00163 
00164 #define CHUNK_FREE(ptr) (MEM_POOLDATA(ptr)->next != NULL)
00165 
00172 #define MEMPOOL_ALLOW_FREEING 1
00173 
00174 #define MEMPOOL_BYPASS_POOLS  2
00175 
00177 extern struct mempool *mempools[];
00178 extern struct mempool_chunk end_marker;
00179 extern struct mempool *pool_object, *pool_objectlink, *pool_player, *pool_bans, *pool_parties;
00180 extern int nrof_mempools;
00181 
00182 #define get_poolchunk(_pool_) get_poolchunk_array_real((_pool_), 0)
00183 #define get_poolarray(_pool_, _arraysize_) get_poolchunk_array_real((_pool_), nearest_pow_two_exp(_arraysize_))
00184 
00185 #define return_poolchunk(_data_, _pool_) return_poolchunk_array_real((_data_), 0, (_pool_))
00186 #define return_poolarray(_data_, _arraysize_, _pool_) return_poolchunk_array_real((_data_), nearest_pow_two_exp(_arraysize_), (_pool_))
00187 
00188 #endif