blockpool_init.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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,
  48. size_t blocksize,
  49. unsigned blockcount)
  50. {
  51. unsigned i;
  52. char *ptr = memory;
  53. /* check that this is a multiple of sizeof(void*) */
  54. assert((blocksize & (sizeof(void*)-1)) == 0);
  55. /* start off with nothing in the list */
  56. this->m_head = this->m_tail = NULL;
  57. /* run through all the blocks placing them into the free list */
  58. for(i = 0;
  59. i < blockcount;
  60. ++i)
  61. {
  62. lub_blockpool_free(this,ptr);
  63. ptr += blocksize;
  64. }
  65. /* intialise the stats */
  66. this->m_block_size = blocksize;
  67. this->m_num_blocks = blockcount;
  68. this->m_alloc_blocks = 0;
  69. this->m_alloc_total_blocks = 0;
  70. this->m_alloc_hightide_blocks = 0;
  71. this->m_alloc_failures = 0;
  72. }
  73. /*--------------------------------------------------------- */