initialise the allocator and return its main block */
| 528 | |
| 529 | /* initialise the allocator and return its main block */ |
| 530 | static struct hp_block *hp_malloc_init(char *address, unsigned long size, |
| 531 | char *name) |
| 532 | { |
| 533 | char *start; |
| 534 | char *end; |
| 535 | struct hp_block *hpb; |
| 536 | unsigned long init_overhead; |
| 537 | |
| 538 | /* make address and size multiple of 8*/ |
| 539 | start = (char *)ROUNDUP((unsigned long) address); |
| 540 | LM_DBG("HP_OPTIMIZE=%lu, HP_LINEAR_HASH_SIZE=%lu, %lu-bytes aligned\n", |
| 541 | HP_MALLOC_OPTIMIZE, HP_LINEAR_HASH_SIZE, (unsigned long)ROUNDTO); |
| 542 | LM_DBG("HP_HASH_SIZE=%lu, HP_EXTRA_HASH_SIZE=%lu, hp_block size=%zu, " |
| 543 | "frag_size=%zu\n", HP_HASH_SIZE, HP_EXTRA_HASH_SIZE, |
| 544 | sizeof(struct hp_block), sizeof(struct hp_frag)); |
| 545 | LM_DBG("params (%p, %lu), start=%p\n", address, size, start); |
| 546 | |
| 547 | if (size < (unsigned long)(start - address)) |
| 548 | return NULL; |
| 549 | |
| 550 | size -= start - address; |
| 551 | |
| 552 | if (size < (MIN_FRAG_SIZE+FRAG_OVERHEAD)) |
| 553 | return NULL; |
| 554 | |
| 555 | size = ROUNDDOWN(size); |
| 556 | |
| 557 | init_overhead = ROUNDUP(sizeof(struct hp_block)) + 2 * FRAG_OVERHEAD; |
| 558 | if (size < init_overhead) |
| 559 | { |
| 560 | LM_ERR("not enough memory for the basic structures! " |
| 561 | "need %lu bytes\n", init_overhead); |
| 562 | /* not enough mem to create our control structures !!!*/ |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | end = start + size; |
| 567 | hpb = (struct hp_block *)(void *)start; |
| 568 | memset(hpb, 0, sizeof(struct hp_block)); |
| 569 | hpb->name = name; |
| 570 | hpb->size = size; |
| 571 | |
| 572 | hpb->used = 0; |
| 573 | hpb->real_used = init_overhead; |
| 574 | hpb->max_real_used = init_overhead; |
| 575 | hpb->total_fragments = 2; |
| 576 | gettimeofday(&hpb->last_updated, NULL); |
| 577 | |
| 578 | hpb->first_frag = (struct hp_frag *)(void *)(start + ROUNDUP(sizeof(struct hp_block))); |
| 579 | hpb->last_frag = (struct hp_frag *)(void *)end - 1; |
| 580 | hpb->last_frag->size = 0; |
| 581 | |
| 582 | /* init initial fragment */ |
| 583 | hpb->first_frag->size = size - init_overhead; |
| 584 | hpb->first_frag->prev = NULL; |
| 585 | hpb->last_frag->prev = NULL; |
| 586 | |
| 587 | hp_frag_attach(hpb, hpb->first_frag); |
no test coverage detected