1
0

blockpool_init.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*********************** -*- Mode: C -*- ***********************
  2. * File : blockpool_init.c
  3. *---------------------------------------------------------------
  4. * Description
  5. * ===========
  6. * This operation initialises an instance of a blockpool.
  7. *
  8. * blockpool - the "blockpool" instance to initialise.
  9. * memory - the memory to be managed.
  10. * blocksize - The size in bytes of each block.
  11. * blockcount - The number of blocks to be managed. NB the client is
  12. * responsible for ensuring that (blocksize x blockcount)
  13. * bytes of memory are available for use.
  14. *
  15. * PRE-CONDITION
  16. * 'blocksize' must be an multiple of 'sizeof(void *)'.
  17. * (If the client declares a structure for the block this should be handled
  18. * automatically by the compiler)
  19. *
  20. * POST-CONDITION
  21. * If the size constraint is not met an assert will fire.
  22. *
  23. * Following initialisation the allocation of memory can be performed.
  24. *
  25. *---------------------------------------------------------------
  26. * Author : Graeme McKerrell
  27. * Created On : Thu Jan 29 13:57:54 2004
  28. * Status : TESTED
  29. *---------------------------------------------------------------
  30. * HISTORY
  31. * 7-Dec-2004 Graeme McKerrell
  32. * updated to use the "lub_" namespace
  33. * 5-May-2004 Graeme McKerrell
  34. * updates following review
  35. * 6-Feb-2004 Graeme McKerrell
  36. * removed extra init_fn parameter from the initialisation call
  37. * 28-Jan-2004 Graeme McKerrell
  38. * Initial version
  39. *---------------------------------------------------------------
  40. * Copyright (C) 2004 3Com Corporation. All Rights Reserved.
  41. **************************************************************** */
  42. #include <assert.h>
  43. #include "private.h"
  44. /*--------------------------------------------------------- */
  45. void
  46. lub_blockpool_init(lub_blockpool_t * this,
  47. void *memory, size_t blocksize, unsigned blockcount)
  48. {
  49. unsigned i;
  50. char *ptr = memory;
  51. /* check that this is a multiple of sizeof(void*) */
  52. assert((blocksize & (sizeof(void *) - 1)) == 0);
  53. /* start off with nothing in the list */
  54. this->m_head = this->m_tail = NULL;
  55. /* run through all the blocks placing them into the free list */
  56. for (i = 0; i < blockcount; ++i) {
  57. lub_blockpool_free(this, ptr);
  58. ptr += blocksize;
  59. }
  60. /* intialise the stats */
  61. this->m_block_size = blocksize;
  62. this->m_num_blocks = blockcount;
  63. this->m_alloc_blocks = 0;
  64. this->m_alloc_total_blocks = 0;
  65. this->m_alloc_hightide_blocks = 0;
  66. this->m_alloc_failures = 0;
  67. }
  68. /*--------------------------------------------------------- */