Heap.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 IBM Corp. and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - use tree data structure instead of list
  16. *******************************************************************************/
  17. #if !defined(HEAP_H)
  18. #define HEAP_H
  19. #if defined(HIGH_PERFORMANCE)
  20. #define NO_HEAP_TRACKING 1
  21. #endif
  22. #define PAHO_MEMORY_ERROR -99
  23. #include "MQTTExportDeclarations.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #if !defined(NO_HEAP_TRACKING)
  27. #if !defined(TREE_C)
  28. /**
  29. * redefines malloc to use "mymalloc" so that heap allocation can be tracked
  30. * @param x the size of the item to be allocated
  31. * @return the pointer to the item allocated, or NULL
  32. */
  33. #define malloc(x) mymalloc(__FILE__, __LINE__, x)
  34. /**
  35. * redefines realloc to use "myrealloc" so that heap allocation can be tracked
  36. * @param a the heap item to be reallocated
  37. * @param b the new size of the item
  38. * @return the new pointer to the heap item
  39. */
  40. #define realloc(a, b) myrealloc(__FILE__, __LINE__, a, b)
  41. /**
  42. * redefines free to use "myfree" so that heap allocation can be tracked
  43. * @param x the size of the item to be freed
  44. */
  45. #define free(x) myfree(__FILE__, __LINE__, x)
  46. #endif
  47. /**
  48. * Information about the state of the heap.
  49. */
  50. typedef struct
  51. {
  52. size_t current_size; /**< current size of the heap in bytes */
  53. size_t max_size; /**< max size the heap has reached in bytes */
  54. } heap_info;
  55. #if defined(__cplusplus)
  56. extern "C" {
  57. #endif
  58. void* mymalloc(char*, int, size_t size);
  59. void* myrealloc(char*, int, void* p, size_t size);
  60. void myfree(char*, int, void* p);
  61. void Heap_scan(FILE* file);
  62. int Heap_initialize(void);
  63. void Heap_terminate(void);
  64. LIBMQTT_API heap_info* Heap_get_info(void);
  65. int HeapDump(FILE* file);
  66. int HeapDumpString(FILE* file, char* str);
  67. void* Heap_findItem(void* p);
  68. void Heap_unlink(char* file, int line, void* p);
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #endif
  73. #endif