| | @@ -1723,10 +1723,38 @@ |
| 1723 | 1723 | If CSON_LOG_ALLOC is true then the cson_malloc/realloc/free() routines |
| 1724 | 1724 | will log a message to stderr. |
| 1725 | 1725 | */ |
| 1726 | 1726 | #define CSON_LOG_ALLOC 0 |
| 1727 | 1727 | |
| 1728 | + |
| 1729 | +/** |
| 1730 | + CSON_FOSSIL_MODE is only for use in the Fossil |
| 1731 | + source tree, so that we can plug in to its allocators. |
| 1732 | + We can't do this by, e.g., defining macros for the |
| 1733 | + malloc/free funcs because fossil's lack of header files |
| 1734 | + means we would have to #include "main.c" here to |
| 1735 | + get the declarations. |
| 1736 | + */ |
| 1737 | +#if defined(CSON_FOSSIL_MODE) |
| 1738 | +void *fossil_malloc(size_t n); |
| 1739 | +void fossil_free(void *p); |
| 1740 | +void *fossil_realloc(void *p, size_t n); |
| 1741 | +# define CSON_MALLOC_IMPL fossil_malloc |
| 1742 | +# define CSON_FREE_IMPL fossil_free |
| 1743 | +# define CSON_REALLOC_IMPL fossil_realloc |
| 1744 | +#endif |
| 1745 | + |
| 1746 | +#if !defined CSON_MALLOC_IMPL |
| 1747 | +# define CSON_MALLOC_IMPL malloc |
| 1748 | +#endif |
| 1749 | +#if !defined CSON_FREE_IMPL |
| 1750 | +# define CSON_FREE_IMPL free |
| 1751 | +#endif |
| 1752 | +#if !defined CSON_REALLOC_IMPL |
| 1753 | +# define CSON_REALLOC_IMPL realloc |
| 1754 | +#endif |
| 1755 | + |
| 1728 | 1756 | /** |
| 1729 | 1757 | A test/debug macro for simulating an OOM after the given number of |
| 1730 | 1758 | bytes have been allocated. |
| 1731 | 1759 | */ |
| 1732 | 1760 | #define CSON_SIMULATE_OOM 0 |
| | @@ -1745,11 +1773,11 @@ |
| 1745 | 1773 | if( cson_totalAlloced > CSON_SIMULATE_OOM ) |
| 1746 | 1774 | { |
| 1747 | 1775 | return NULL; |
| 1748 | 1776 | } |
| 1749 | 1777 | #endif |
| 1750 | | - return malloc(n); |
| 1778 | + return CSON_MALLOC_IMPL(n); |
| 1751 | 1779 | } |
| 1752 | 1780 | |
| 1753 | 1781 | /** Simple proxy for free(). descr is a description of the memory being freed. */ |
| 1754 | 1782 | static void cson_free( void * p, char const * descr ) |
| 1755 | 1783 | { |
| | @@ -1756,11 +1784,11 @@ |
| 1756 | 1784 | #if CSON_LOG_ALLOC |
| 1757 | 1785 | fprintf(stderr, "Freeing @%p [%s].\n", p, descr); |
| 1758 | 1786 | #endif |
| 1759 | 1787 | if( !cson_value_is_builtin(p) ) |
| 1760 | 1788 | { |
| 1761 | | - free( p ); |
| 1789 | + CSON_FREE_IMPL( p ); |
| 1762 | 1790 | } |
| 1763 | 1791 | } |
| 1764 | 1792 | /** Simple proxy for realloc(). descr is a description of the (re)allocation. */ |
| 1765 | 1793 | static void * cson_realloc( void * hint, size_t n, char const * descr ) |
| 1766 | 1794 | { |
| | @@ -1781,11 +1809,11 @@ |
| 1781 | 1809 | cson_free(hint, descr); |
| 1782 | 1810 | return NULL; |
| 1783 | 1811 | } |
| 1784 | 1812 | else |
| 1785 | 1813 | { |
| 1786 | | - return realloc( hint, n ); |
| 1814 | + return CSON_REALLOC_IMPL( hint, n ); |
| 1787 | 1815 | } |
| 1788 | 1816 | } |
| 1789 | 1817 | |
| 1790 | 1818 | |
| 1791 | 1819 | #undef CSON_LOG_ALLOC |
| | @@ -4876,10 +4904,13 @@ |
| 4876 | 4904 | #undef CSON_DBL |
| 4877 | 4905 | #undef CSON_STR |
| 4878 | 4906 | #undef CSON_OBJ |
| 4879 | 4907 | #undef CSON_ARRAY |
| 4880 | 4908 | #undef CSON_VCAST |
| 4909 | +#undef CSON_MALLOC_IMPL |
| 4910 | +#undef CSON_FREE_IMPL |
| 4911 | +#undef CSON_REALLOC_IMPL |
| 4881 | 4912 | /* end file ./cson.c */ |
| 4882 | 4913 | /* begin file ./cson_lists.h */ |
| 4883 | 4914 | /* Auto-generated from cson_list.h. Edit at your own risk! */ |
| 4884 | 4915 | unsigned int cson_value_list_reserve( cson_value_list * self, unsigned int n ) |
| 4885 | 4916 | { |
| 4886 | 4917 | |