1
0

blockpool_alloc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 *
  47. lub_blockpool_alloc(lub_blockpool_t *this)
  48. {
  49. lub_blockpool_block_t *newblock = this->m_head;
  50. if(newblock)
  51. {
  52. if(newblock == this->m_tail)
  53. {
  54. /* remove the last item from the pool */
  55. this->m_head = NULL;
  56. this->m_tail = NULL;
  57. }
  58. else
  59. {
  60. this->m_head = newblock->next;
  61. }
  62. /* updated the stats */
  63. ++this->m_alloc_blocks;
  64. ++this->m_alloc_total_blocks;
  65. if(this->m_alloc_blocks > this->m_alloc_hightide_blocks)
  66. {
  67. this->m_alloc_hightide_blocks = this->m_alloc_blocks;
  68. }
  69. }
  70. else
  71. {
  72. ++this->m_alloc_failures;
  73. }
  74. return newblock;
  75. }
  76. /*--------------------------------------------------------- */