00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "stdinc.h"
00054 #ifdef HAVE_MMAP
00055 #include <sys/mman.h>
00056
00057
00058 #ifdef MAP_ANONYMOUS
00059 #ifndef MAP_ANON
00060 #define MAP_ANON MAP_ANONYMOUS
00061 #endif
00062 #endif
00063 #endif
00064
00065 #include "ircd.h"
00066 #include "balloc.h"
00067 #include "irc_string.h"
00068 #include "tools.h"
00069 #include "client.h"
00070 #include "send.h"
00071 #include "numeric.h"
00072 #include "fdlist.h"
00073 #include "event.h"
00074
00075
00076 static BlockHeap *heap_list = NULL;
00077
00078 static int BlockHeapGarbageCollect(BlockHeap *);
00079 static void heap_garbage_collection(void *);
00080
00081
00082
00083
00084
00085
00086 static inline void
00087 free_block(void *ptr, size_t size)
00088 {
00089 #ifdef HAVE_MMAP
00090 munmap(ptr, size);
00091 #else
00092 free(ptr);
00093 #endif
00094 }
00095
00096 #ifdef HAVE_MMAP
00097 #ifndef MAP_ANON
00098
00099 static fde_t dpfd;
00100 #endif
00101 #endif
00102
00103
00104
00105
00106 void
00107 initBlockHeap(void)
00108 {
00109 #ifdef HAVE_MMAP
00110 #ifndef MAP_ANON
00111 int zero_fd = open("/dev/zero", O_RDWR);
00112
00113 if (zero_fd < 0)
00114 outofmemory();
00115 fd_open(&dpfd, zero_fd, 0, "Anonymous mmap()");
00116 #endif
00117 eventAdd("heap_garbage_collection", &heap_garbage_collection, NULL, 119);
00118 #endif
00119 }
00120
00121
00122
00123
00124
00125 static inline void *
00126 get_block(size_t size)
00127 {
00128 #ifdef HAVE_MMAP
00129 void *ptr = NULL;
00130
00131 #ifndef MAP_ANON
00132 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, dpfd.fd, 0);
00133 #else
00134 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
00135 #endif
00136 return ptr == MAP_FAILED ? NULL : ptr;
00137 #else
00138 return malloc(size);
00139 #endif
00140 }
00141
00142 static void
00143 heap_garbage_collection(void *arg)
00144 {
00145 BlockHeap *bh;
00146
00147 for (bh = heap_list; bh != NULL; bh = bh->next)
00148 BlockHeapGarbageCollect(bh);
00149 }
00150
00151
00152
00153
00154
00155 static int
00156 newblock(BlockHeap *bh)
00157 {
00158 MemBlock *newblk = NULL;
00159 Block *b = NULL;
00160 int i = 0;
00161 void *offset = NULL;
00162
00163
00164 if ((b = calloc(1, sizeof(Block))) == NULL)
00165 return 1;
00166
00167 b->freeElems = bh->elemsPerBlock;
00168 b->next = bh->base;
00169 b->alloc_size = bh->elemsPerBlock * (bh->elemSize + sizeof(MemBlock));
00170 b->elems = get_block(b->alloc_size);
00171
00172 if (b->elems == NULL)
00173 return 1;
00174
00175 offset = b->elems;
00176
00177
00178 for (; i < bh->elemsPerBlock; ++i)
00179 {
00180 void *data;
00181
00182 newblk = offset;
00183 newblk->block = b;
00184 data = (void *)((size_t)offset + sizeof(MemBlock));
00185
00186 dlinkAdd(data, &newblk->self, &b->free_list);
00187 offset = (void *)((size_t)offset + bh->elemSize + sizeof(MemBlock));
00188 }
00189
00190 ++bh->blocksAllocated;
00191 bh->freeElems += bh->elemsPerBlock;
00192 bh->base = b;
00193
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 BlockHeap *
00211 BlockHeapCreate(const char *const name, size_t elemsize, int elemsperblock)
00212 {
00213 BlockHeap *bh = NULL;
00214 assert(elemsize > 0 && elemsperblock > 0);
00215
00216
00217 if ((elemsize <= 0) || (elemsperblock <= 0))
00218 outofmemory();
00219
00220
00221 if ((bh = calloc(1, sizeof(BlockHeap))) == NULL)
00222 outofmemory();
00223
00224 if ((elemsize % sizeof(void *)) != 0)
00225 {
00226
00227 elemsize += sizeof(void *);
00228 elemsize &= ~(sizeof(void *) - 1);
00229 }
00230
00231 bh->name = name;
00232 bh->elemSize = elemsize;
00233 bh->elemsPerBlock = elemsperblock;
00234
00235
00236 if (newblock(bh))
00237 {
00238 if (bh != NULL)
00239 free(bh);
00240
00241 outofmemory();
00242 }
00243
00244 assert(bh);
00245
00246 bh->next = heap_list;
00247 heap_list = bh;
00248
00249 return bh;
00250 }
00251
00252
00253
00254
00255
00256
00257 void *
00258 BlockHeapAlloc(BlockHeap *bh)
00259 {
00260 Block *walker = NULL;
00261 dlink_node *new_node = NULL;
00262
00263 assert(bh != NULL);
00264
00265 if (bh->freeElems == 0)
00266 {
00267
00268
00269 if (newblock(bh))
00270 {
00271
00272 BlockHeapGarbageCollect(bh);
00273
00274 if (newblock(bh))
00275 outofmemory();
00276 }
00277 }
00278
00279 for (walker = bh->base; walker != NULL; walker = walker->next)
00280 {
00281 if (walker->freeElems > 0)
00282 {
00283 --bh->freeElems;
00284 --walker->freeElems;
00285 new_node = walker->free_list.head;
00286
00287 dlinkDelete(new_node, &walker->free_list);
00288 assert(new_node->data != NULL);
00289
00290 memset(new_node->data, 0, bh->elemSize);
00291 return new_node->data;
00292 }
00293 }
00294
00295 assert(0 == 1);
00296 outofmemory();
00297 return NULL;
00298 }
00299
00300
00301
00302
00303
00304
00305 int
00306 BlockHeapFree(BlockHeap *bh, void *ptr)
00307 {
00308 Block *block = NULL;
00309 struct MemBlock *memblock = NULL;
00310
00311 assert(bh != NULL);
00312 assert(ptr != NULL);
00313
00314 memblock = (void *)((size_t)ptr - sizeof(MemBlock));
00315 assert(memblock->block != NULL);
00316
00317 if (memblock->block == NULL)
00318 outofmemory();
00319
00320 block = memblock->block;
00321 ++bh->freeElems;
00322 ++block->freeElems;
00323 mem_frob(ptr, bh->elemSize);
00324
00325 dlinkAdd(ptr, &memblock->self, &block->free_list);
00326 return 0;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 static int
00339 BlockHeapGarbageCollect(BlockHeap *bh)
00340 {
00341 Block *walker = NULL, *last = NULL;
00342
00343 assert(bh != NULL);
00344
00345 if (bh->freeElems < bh->elemsPerBlock || bh->blocksAllocated == 1)
00346 {
00347
00348 return 0;
00349 }
00350
00351 walker = bh->base;
00352
00353 while (walker != NULL)
00354 {
00355 if (walker->freeElems == bh->elemsPerBlock)
00356 {
00357 free_block(walker->elems, walker->alloc_size);
00358
00359 if (last != NULL)
00360 {
00361 last->next = walker->next;
00362
00363 if (walker != NULL)
00364 free(walker);
00365 walker = last->next;
00366 }
00367 else
00368 {
00369 bh->base = walker->next;
00370
00371 if (walker != NULL)
00372 free(walker);
00373 walker = bh->base;
00374 }
00375
00376 --bh->blocksAllocated;
00377 bh->freeElems -= bh->elemsPerBlock;
00378 }
00379 else
00380 {
00381 last = walker;
00382 walker = walker->next;
00383 }
00384 }
00385
00386 return 0;
00387 }
00388
00389
00390
00391
00392
00393 int
00394 BlockHeapDestroy(BlockHeap *bh)
00395 {
00396 Block *walker = NULL, *next = NULL;
00397
00398 if (bh == NULL)
00399 return 1;
00400
00401 for (walker = bh->base; walker != NULL; walker = next)
00402 {
00403 next = walker->next;
00404 free_block(walker->elems, walker->alloc_size);
00405
00406 if (walker != NULL)
00407 free(walker);
00408 }
00409
00410 if (heap_list == bh)
00411 heap_list = bh->next;
00412 else {
00413 BlockHeap *prev;
00414
00415 for (prev = heap_list; prev->next != bh; prev = prev->next)
00416 ;
00417 prev->next = bh->next;
00418 }
00419
00420 free(bh);
00421 return 0;
00422 }
00423
00424
00425
00426
00427
00428 static size_t
00429 block_heap_get_used_mem(const BlockHeap *const bh)
00430 {
00431 return(((bh->blocksAllocated *
00432 bh->elemsPerBlock)-bh->freeElems) *
00433 (bh->elemSize + sizeof(MemBlock)));
00434 }
00435
00436
00437
00438
00439
00440 static size_t
00441 block_heap_get_free_mem(const BlockHeap *const bh)
00442 {
00443 return(bh->freeElems * (bh->elemSize + sizeof(MemBlock)));
00444 }
00445
00446
00447
00448
00449
00450 static size_t
00451 block_heap_get_size_mem(const BlockHeap *const bh)
00452 {
00453 return(((bh->blocksAllocated *
00454 bh->elemsPerBlock)) *
00455 (bh->elemSize + sizeof(MemBlock)));
00456 }
00457
00458
00459
00460
00461
00462 static unsigned int
00463 block_heap_get_used_elm(const BlockHeap *const bh)
00464 {
00465 return((bh->blocksAllocated *
00466 bh->elemsPerBlock)-bh->freeElems);
00467 }
00468
00469
00470
00471
00472
00473 static unsigned int
00474 block_heap_get_free_elm(const BlockHeap *const bh)
00475 {
00476 return(bh->freeElems);
00477 }
00478
00479
00480
00481
00482
00483
00484 static unsigned int
00485 block_heap_get_size_elm(const BlockHeap *const bh)
00486 {
00487 return(bh->blocksAllocated * bh->elemsPerBlock);
00488 }
00489
00490 void
00491 block_heap_report_stats(struct Client *client_p)
00492 {
00493 const BlockHeap *bh = NULL;
00494
00495 for (bh = heap_list; bh != NULL; bh = bh->next)
00496 sendto_one(client_p, ":%s %d %s z :%s mempool: used %u/%u free %u/%u (size %u/%u)",
00497 me.name, RPL_STATSDEBUG, client_p->name, bh->name,
00498 block_heap_get_used_elm(bh),
00499 block_heap_get_used_mem(bh),
00500 block_heap_get_free_elm(bh),
00501 block_heap_get_free_mem(bh),
00502 block_heap_get_size_elm(bh),
00503 block_heap_get_size_mem(bh));
00504 }