Make sure there is room for one more item in the item list. If there * is not, expand the list as indicated by the value of "incr": * - if incr < 0 then increase the malloced size by -1 * incr * - if incr >= 0 then either make the malloced size equal to "incr" * or (if that's not large enough) double the malloced size * After the size check, the list's count is incremented by 1 and a po
| 1765 | * to the "new" list item is returned. |
| 1766 | */ |
| 1767 | void *expand_item_list(item_list *lp, size_t item_size, const char *desc, int incr) |
| 1768 | { |
| 1769 | /* First time through, 0 <= 0, so list is expanded. */ |
| 1770 | if (lp->malloced <= lp->count) { |
| 1771 | void *new_ptr; |
| 1772 | size_t expand_size; |
| 1773 | if (incr < 0) |
| 1774 | expand_size = -incr; /* increase slowly */ |
| 1775 | else if (lp->malloced < (size_t)incr) |
| 1776 | expand_size = incr - lp->malloced; |
| 1777 | else if (lp->malloced) |
| 1778 | expand_size = lp->malloced; /* double in size */ |
| 1779 | else |
| 1780 | expand_size = 1; |
| 1781 | if (SIZE_MAX/item_size - expand_size < lp->malloced) |
| 1782 | overflow_exit("expand_item_list"); |
| 1783 | expand_size += lp->malloced; |
| 1784 | new_ptr = realloc_buf(lp->items, expand_size * item_size); |
| 1785 | if (DEBUG_GTE(FLIST, 3)) { |
| 1786 | rprintf(FINFO, "[%s] expand %s to %s bytes, did%s move\n", |
| 1787 | who_am_i(), desc, big_num(expand_size * item_size), |
| 1788 | new_ptr == lp->items ? " not" : ""); |
| 1789 | } |
| 1790 | |
| 1791 | lp->items = new_ptr; |
| 1792 | lp->malloced = expand_size; |
| 1793 | } |
| 1794 | return (char*)lp->items + (lp->count++ * item_size); |
| 1795 | } |
| 1796 | |
| 1797 | /* This zeroing of memory won't be optimized away by the compiler. */ |
| 1798 | void force_memzero(void *buf, size_t len) |