| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* |
| 4 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file balloc.h |
| 23 |
* \brief A block allocator |
| 24 |
* \version $Id$ |
| 25 |
* \todo Get rid of all typedefs in this file |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef NOBALLOC |
| 29 |
|
| 30 |
/*! \brief Block contains status information for |
| 31 |
* an allocated block in our heap. |
| 32 |
*/ |
| 33 |
struct Block { |
| 34 |
int freeElems; /*!< Number of available elems */ |
| 35 |
size_t alloc_size; /*!< Size of data space for each block */ |
| 36 |
struct Block* next; /*!< Next in our chain of blocks */ |
| 37 |
void* elems; /*!< Points to allocated memory */ |
| 38 |
dlink_list free_list; /*!< Chain of free memory blocks */ |
| 39 |
dlink_list used_list; /*!< Chain of used memory blocks */ |
| 40 |
}; |
| 41 |
|
| 42 |
typedef struct Block Block; |
| 43 |
|
| 44 |
struct MemBlock { |
| 45 |
dlink_node self; /*!< Node for linking into free_list or used_list */ |
| 46 |
Block *block; /*!< Which block we belong to */ |
| 47 |
}; |
| 48 |
typedef struct MemBlock MemBlock; |
| 49 |
|
| 50 |
/*! \brief BlockHeap contains the information for the root node of the |
| 51 |
* memory heap. |
| 52 |
*/ |
| 53 |
struct BlockHeap { |
| 54 |
size_t elemSize; /*!< Size of each element to be stored */ |
| 55 |
int elemsPerBlock; /*!< Number of elements per block */ |
| 56 |
int blocksAllocated; /*!< Number of blocks allocated */ |
| 57 |
int freeElems; /*!< Number of free elements */ |
| 58 |
Block* base; /*!< Pointer to first block */ |
| 59 |
const char *name; /*!< Name of the heap */ |
| 60 |
struct BlockHeap *next; /*!< Pointer to next heap */ |
| 61 |
}; |
| 62 |
|
| 63 |
typedef struct BlockHeap BlockHeap; |
| 64 |
|
| 65 |
|
| 66 |
LIBIO_EXTERN int BlockHeapFree(BlockHeap *, void *); |
| 67 |
LIBIO_EXTERN void * BlockHeapAlloc(BlockHeap *); |
| 68 |
|
| 69 |
LIBIO_EXTERN BlockHeap* BlockHeapCreate(const char *const, size_t, int); |
| 70 |
LIBIO_EXTERN int BlockHeapDestroy(BlockHeap *); |
| 71 |
#ifdef IN_MISC_C |
| 72 |
extern void initBlockHeap(void); |
| 73 |
#endif |
| 74 |
|
| 75 |
LIBIO_EXTERN size_t block_heap_get_used_mem(const BlockHeap *); |
| 76 |
LIBIO_EXTERN size_t block_heap_get_free_mem(const BlockHeap *); |
| 77 |
LIBIO_EXTERN size_t block_heap_get_size_mem(const BlockHeap *); |
| 78 |
LIBIO_EXTERN unsigned int block_heap_get_used_elm(const BlockHeap *); |
| 79 |
LIBIO_EXTERN unsigned int block_heap_get_free_elm(const BlockHeap *); |
| 80 |
LIBIO_EXTERN unsigned int block_heap_get_size_elm(const BlockHeap *); |
| 81 |
|
| 82 |
#else /* NOBALLOC */ |
| 83 |
|
| 84 |
typedef struct BlockHeap BlockHeap; |
| 85 |
/* This is really kludgy, passing ints as pointers is always bad. */ |
| 86 |
#define BlockHeapCreate(blah, es, epb) ((BlockHeap*)(es)) |
| 87 |
#define BlockHeapAlloc(x) MyMalloc((int)x) |
| 88 |
#define BlockHeapFree(x,y) MyFree(y) |
| 89 |
|
| 90 |
#endif /* NOBALLOC */ |
| 91 |
|
| 92 |
LIBIO_EXTERN BlockHeap *heap_list; |