| 1010 | */ |
| 1011 | |
| 1012 | static int /* O - 1 on success, 0 on failure */ |
| 1013 | cups_array_add(cups_array_t *a, /* I - Array */ |
| 1014 | void *e, /* I - Element to add */ |
| 1015 | int insert) /* I - 1 = insert, 0 = append */ |
| 1016 | { |
| 1017 | int i, /* Looping var */ |
| 1018 | current; /* Current element */ |
| 1019 | int diff; /* Comparison with current element */ |
| 1020 | |
| 1021 | |
| 1022 | DEBUG_printf(("7cups_array_add(a=%p, e=%p, insert=%d)", (void *)a, e, insert)); |
| 1023 | |
| 1024 | /* |
| 1025 | * Verify we have room for the new element... |
| 1026 | */ |
| 1027 | |
| 1028 | if (a->num_elements >= a->alloc_elements) |
| 1029 | { |
| 1030 | /* |
| 1031 | * Allocate additional elements; start with 16 elements, then |
| 1032 | * double the size until 1024 elements, then add 1024 elements |
| 1033 | * thereafter... |
| 1034 | */ |
| 1035 | |
| 1036 | void **temp; /* New array elements */ |
| 1037 | int count; /* New allocation count */ |
| 1038 | |
| 1039 | |
| 1040 | if (a->alloc_elements == 0) |
| 1041 | { |
| 1042 | count = 16; |
| 1043 | temp = malloc((size_t)count * sizeof(void *)); |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | if (a->alloc_elements < 1024) |
| 1048 | count = a->alloc_elements * 2; |
| 1049 | else |
| 1050 | count = a->alloc_elements + 1024; |
| 1051 | |
| 1052 | temp = realloc(a->elements, (size_t)count * sizeof(void *)); |
| 1053 | } |
| 1054 | |
| 1055 | DEBUG_printf(("9cups_array_add: count=" CUPS_LLFMT, CUPS_LLCAST count)); |
| 1056 | |
| 1057 | if (!temp) |
| 1058 | { |
| 1059 | DEBUG_puts("9cups_array_add: allocation failed, returning 0"); |
| 1060 | return (0); |
| 1061 | } |
| 1062 | |
| 1063 | a->alloc_elements = count; |
| 1064 | a->elements = temp; |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * Find the insertion point for the new element; if there is no |
| 1069 | * compare function or elements, just add it to the beginning or end... |
no test coverage detected