blockpool_alloc.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*********************** -*- Mode: C -*- ***********************
  2. * File : blockpool_alloc.c
  3. *---------------------------------------------------------------
  4. * Description
  5. * ===========
  6. *---------------------------------------------------------------
  7. * This operation allocates a "block" of memory from a "blockpool"
  8. *
  9. * blockpool - the "blockpool" instance to invoke this operation upon
  10. *
  11. * PRE-CONDITIONS
  12. * The blockpool must have been initialised.
  13. *
  14. * RETURNS
  15. * A pointer to a "block" of memory, or NULL if there is none left for
  16. * allocation.
  17. *
  18. * POST-CONDITIONS
  19. * The behaviour is undefined if the "blockpool" is uninitialised.
  20. *
  21. * If an initialisation operation has been registered then this will
  22. * have been invoked with the block pointer before returning from this
  23. * call.
  24. *---------------------------------------------------------------
  25. * Author : Graeme McKerrell
  26. * Created On : Thu Jan 29 14:11:00 2004
  27. * Status : TESTED
  28. *---------------------------------------------------------------
  29. * HISTORY
  30. * 7-Dec-2004 Graeme McKerrell
  31. * updated to use the "lub_" namespace
  32. * 5-May-2004 Graeme McKerrell
  33. * updates following review
  34. * 1-Apr-2004 Peter Kennedy
  35. * updated to fix last entry removal test - use tail
  36. * 6-Feb-2004 Graeme McKerrell
  37. * updated to use FIFO allocation of blocks
  38. * removed initialisation function call.
  39. * 29-Jan-2004 Graeme McKerrell
  40. * Initial version
  41. *---------------------------------------------------------------
  42. * Copyright (C) 2004 3Com Corporation. All Rights Reserved.
  43. **************************************************************** */
  44. #include "private.h"
  45. /*--------------------------------------------------------- */
  46. void *lub_blockpool_alloc(lub_blockpool_t * this)
  47. {
  48. lub_blockpool_block_t *newblock = this->m_head;
  49. if (newblock) {
  50. if (newblock == this->m_tail) {
  51. /* remove the last item from the pool */
  52. this->m_head = NULL;
  53. this->m_tail = NULL;
  54. } else {
  55. this->m_head = newblock->next;
  56. }
  57. /* updated the stats */
  58. ++this->m_alloc_blocks;
  59. ++this->m_alloc_total_blocks;
  60. if (this->m_alloc_blocks > this->m_alloc_hightide_blocks) {
  61. this->m_alloc_hightide_blocks = this->m_alloc_blocks;
  62. }
  63. } else {
  64. ++this->m_alloc_failures;
  65. }
  66. return newblock;
  67. }
  68. /*--------------------------------------------------------- */