qpb_allocator.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * qpb_allocator.h
  3. *
  4. * @copyright Copyright (C) 2016 Sproute Networks, Inc.
  5. *
  6. * @author Avneesh Sachdev <avneesh@sproute.com>
  7. *
  8. * This file is part of Quagga.
  9. *
  10. * Quagga is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * Quagga is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Quagga; see the file COPYING. If not, write to the Free
  22. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. /*
  26. * Header file for quagga protobuf memory management code.
  27. */
  28. #ifndef _QPB_ALLOCATOR_H_
  29. #define _QPB_ALLOCATOR_H_
  30. #include <google/protobuf-c/protobuf-c.h>
  31. struct linear_allocator_t_;
  32. /*
  33. * Alias for ProtobufCAllocator that is easier on the fingers.
  34. */
  35. typedef ProtobufCAllocator qpb_allocator_t;
  36. /*
  37. * qpb_alloc
  38. */
  39. static inline void *
  40. qpb_alloc (qpb_allocator_t *allocator, size_t size)
  41. {
  42. return allocator->alloc (allocator->allocator_data, size);
  43. }
  44. /*
  45. * qpb_alloc_ptr_array
  46. *
  47. * Allocate space for the specified number of pointers.
  48. */
  49. static inline void *
  50. qpb_alloc_ptr_array (qpb_allocator_t *allocator, size_t num_ptrs)
  51. {
  52. return qpb_alloc (allocator, num_ptrs * sizeof (void *));
  53. }
  54. /*
  55. * qpb_free
  56. */
  57. static inline void
  58. qpb_free (qpb_allocator_t *allocator, void *ptr)
  59. {
  60. allocator->free (allocator->allocator_data, ptr);
  61. }
  62. /*
  63. * QPB_ALLOC
  64. *
  65. * Convenience macro to reduce the probability of allocating memory of
  66. * incorrect size. It returns enough memory to store the given type,
  67. * and evaluates to an appropriately typed pointer.
  68. */
  69. #define QPB_ALLOC(allocator, type) \
  70. (type *) qpb_alloc(allocator, sizeof(type))
  71. /*
  72. * Externs.
  73. */
  74. extern void qpb_allocator_init_linear (qpb_allocator_t *,
  75. struct linear_allocator_t_ *);
  76. /*
  77. * The following macros are for the common case where a qpb allocator
  78. * is being used alongside a linear allocator that allocates memory
  79. * off of the stack.
  80. */
  81. #define QPB_DECLARE_STACK_ALLOCATOR(allocator, size) \
  82. qpb_allocator_t allocator; \
  83. linear_allocator_t lin_ ## allocator; \
  84. char lin_ ## allocator ## _buf[size]
  85. #define QPB_INIT_STACK_ALLOCATOR(allocator) \
  86. do \
  87. { \
  88. linear_allocator_init(&(lin_ ## allocator), \
  89. lin_ ## allocator ## _buf, \
  90. sizeof(lin_ ## allocator ## _buf)); \
  91. qpb_allocator_init_linear(&allocator, &(lin_ ## allocator)); \
  92. } while (0)
  93. #define QPB_RESET_STACK_ALLOCATOR(allocator) \
  94. do \
  95. { \
  96. linear_allocator_reset (&(lin_ ## allocator)); \
  97. } while (0)
  98. #endif /* _QPB_ALLOCATOR_H_ */