memb.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.h,v 1.1 2006/06/12 08:21:43 adam Exp $
  34. */
  35. /**
  36. * \defgroup memb Memory block management functions
  37. *
  38. * The memory block allocation routines provide a simple yet powerful
  39. * set of functions for managing a set of memory blocks of fixed
  40. * size. A set of memory blocks is statically declared with the
  41. * MEMB() macro. Memory blocks are allocated from the declared
  42. * memory by the memb_alloc() function, and are deallocated with the
  43. * memb_free() function.
  44. *
  45. * \note Because of namespace clashes only one MEMB() can be
  46. * declared per C module, and the name scope of a MEMB() memory
  47. * block is local to each C module.
  48. *
  49. * The following example shows how to declare and use a memory block
  50. * called "cmem" which has 8 chunks of memory with each memory chunk
  51. * being 20 bytes large.
  52. *
  53. * @{
  54. */
  55. /**
  56. * \file
  57. * Memory block allocation routines.
  58. * \author
  59. * Adam Dunkels <adam@sics.se>
  60. *
  61. */
  62. #ifndef __MEMB_H__
  63. #define __MEMB_H__
  64. /*
  65. * Here we define a C preprocessing macro for concatenating to
  66. * strings. We need use two macros in order to allow concatenation of
  67. * two #defined macros.
  68. */
  69. #define MEMB_CONCAT2(s1, s2) s1##s2
  70. #define MEMB_CONCAT(s1, s2) MEMB_CONCAT2(s1, s2)
  71. /**
  72. * Declare a memory block.
  73. *
  74. * This macro is used to staticall declare a block of memory that can
  75. * be used by the block allocation functions. The macro statically
  76. * declares a C array with a size that matches the specified number of
  77. * blocks and their individual sizes.
  78. *
  79. * Example:
  80. \code
  81. MEMB(connections, sizeof(struct connection), 16);
  82. \endcode
  83. *
  84. * \param name The name of the memory block (later used with
  85. * memb_init(), memb_alloc() and memb_free()).
  86. *
  87. * \param size The size of each memory chunk, in bytes.
  88. *
  89. * \param num The total number of memory chunks in the block.
  90. *
  91. */
  92. #define MEMB(name, structure, num) \
  93. static char MEMB_CONCAT(name,_memb_count)[num]; \
  94. static structure MEMB_CONCAT(name,_memb_mem)[num]; \
  95. static struct memb_blocks name = {sizeof(structure), num, \
  96. MEMB_CONCAT(name,_memb_count), \
  97. (void *)MEMB_CONCAT(name,_memb_mem)}
  98. struct memb_blocks {
  99. unsigned short size;
  100. unsigned short num;
  101. char *count;
  102. void *mem;
  103. };
  104. /**
  105. * Initialize a memory block that was declared with MEMB().
  106. *
  107. * \param m A memory block previosly declared with MEMB().
  108. */
  109. void memb_init(struct memb_blocks *m);
  110. /**
  111. * Allocate a memory block from a block of memory declared with MEMB().
  112. *
  113. * \param m A memory block previosly declared with MEMB().
  114. */
  115. void *memb_alloc(struct memb_blocks *m);
  116. /**
  117. * Deallocate a memory block from a memory block previously declared
  118. * with MEMB().
  119. *
  120. * \param m m A memory block previosly declared with MEMB().
  121. *
  122. * \param ptr A pointer to the memory block that is to be deallocated.
  123. *
  124. * \return The new reference count for the memory block (should be 0
  125. * if successfully deallocated) or -1 if the pointer "ptr" did not
  126. * point to a legal memory block.
  127. */
  128. char memb_free(struct memb_blocks *m, void *ptr);
  129. /** @} */
  130. #endif /* __MEMB_H__ */