block_pool.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright (c) 2009 Joseph A. Adams
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The name of the author may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CCAN_BLOCK_POOL
  26. #define CCAN_BLOCK_POOL
  27. #include <ccan/talloc/talloc.h>
  28. #include <string.h>
  29. struct block_pool;
  30. /* Construct a new block pool.
  31. ctx is a talloc context (or NULL if you don't know what talloc is ;) ) */
  32. struct block_pool *block_pool_new(void *ctx);
  33. /* Same as block_pool_alloc, but allows you to manually specify alignment.
  34. For instance, strings need not be aligned, so set align=1 for them.
  35. align must be a power of two. */
  36. void *block_pool_alloc_align(struct block_pool *bp, size_t size, size_t align);
  37. /* Allocate a block of a given size. The returned pointer will remain valid
  38. for the life of the block_pool. The block cannot be resized or
  39. freed individually. */
  40. static inline void *block_pool_alloc(struct block_pool *bp, size_t size) {
  41. size_t align = size & -size; //greatest power of two by which size is divisible
  42. if (align > 16)
  43. align = 16;
  44. return block_pool_alloc_align(bp, size, align);
  45. }
  46. static inline void block_pool_free(struct block_pool *bp) {
  47. talloc_free(bp);
  48. }
  49. char *block_pool_strdup(struct block_pool *bp, const char *str);
  50. static inline void *block_pool_memdup(struct block_pool *bp, const void *src, size_t size) {
  51. void *ret = block_pool_alloc(bp, size);
  52. memcpy(ret, src, size);
  53. return ret;
  54. }
  55. #endif