memb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2004, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the uIP TCP/IP stack
  30. *
  31. * Author: Adam Dunkels <adam@sics.se>
  32. *
  33. * $Id: memb.c,v 1.1 2006/06/12 08:21:43 adam Exp $
  34. */
  35. /**
  36. * \addtogroup memb
  37. * @{
  38. */
  39. /**
  40. * \file
  41. * Memory block allocation routines.
  42. * \author Adam Dunkels <adam@sics.se>
  43. */
  44. #include <string.h>
  45. #include "memb.h"
  46. /*---------------------------------------------------------------------------*/
  47. void
  48. memb_init(struct memb_blocks *m)
  49. {
  50. memset(m->count, 0, m->num);
  51. memset(m->mem, 0, m->size * m->num);
  52. }
  53. /*---------------------------------------------------------------------------*/
  54. void *
  55. memb_alloc(struct memb_blocks *m)
  56. {
  57. int i;
  58. for(i = 0; i < m->num; ++i) {
  59. if(m->count[i] == 0) {
  60. /* If this block was unused, we increase the reference count to
  61. indicate that it now is used and return a pointer to the
  62. memory block. */
  63. ++(m->count[i]);
  64. return (void *)((char *)m->mem + (i * m->size));
  65. }
  66. }
  67. /* No free block was found, so we return NULL to indicate failure to
  68. allocate block. */
  69. return NULL;
  70. }
  71. /*---------------------------------------------------------------------------*/
  72. char
  73. memb_free(struct memb_blocks *m, void *ptr)
  74. {
  75. int i;
  76. char *ptr2;
  77. /* Walk through the list of blocks and try to find the block to
  78. which the pointer "ptr" points to. */
  79. ptr2 = (char *)m->mem;
  80. for(i = 0; i < m->num; ++i) {
  81. if(ptr2 == (char *)ptr) {
  82. /* We've found to block to which "ptr" points so we decrease the
  83. reference count and return the new value of it. */
  84. if(m->count[i] > 0) {
  85. /* Make sure that we don't deallocate free memory. */
  86. --(m->count[i]);
  87. }
  88. return m->count[i];
  89. }
  90. ptr2 += m->size;
  91. }
  92. return -1;
  93. }
  94. /*---------------------------------------------------------------------------*/
  95. /** @} */