Common support for independent_X routines, handling all of the combinations that can result. The opts arg has: bit 0 set if all elements are same size (using sizes[0]) bit 1 set if elements should be zeroed */
| 5063 | bit 1 set if elements should be zeroed |
| 5064 | */ |
| 5065 | static void** ialloc(mstate m, |
| 5066 | size_t n_elements, |
| 5067 | size_t* sizes, |
| 5068 | int opts, |
| 5069 | void* chunks[]) { |
| 5070 | |
| 5071 | size_t element_size; /* chunksize of each element, if all same */ |
| 5072 | size_t contents_size; /* total size of elements */ |
| 5073 | size_t array_size; /* request size of pointer array */ |
| 5074 | void* mem; /* malloced aggregate space */ |
| 5075 | mchunkptr p; /* corresponding chunk */ |
| 5076 | size_t remainder_size; /* remaining bytes while splitting */ |
| 5077 | void** marray; /* either "chunks" or malloced ptr array */ |
| 5078 | mchunkptr array_chunk; /* chunk for malloced ptr array */ |
| 5079 | flag_t was_enabled; /* to disable mmap */ |
| 5080 | size_t size; |
| 5081 | size_t i; |
| 5082 | |
| 5083 | ensure_initialization(); |
| 5084 | /* compute array length, if needed */ |
| 5085 | if (chunks != 0) { |
| 5086 | if (n_elements == 0) |
| 5087 | return chunks; /* nothing to do */ |
| 5088 | marray = chunks; |
| 5089 | array_size = 0; |
| 5090 | } |
| 5091 | else { |
| 5092 | /* if empty req, must still return chunk representing empty array */ |
| 5093 | if (n_elements == 0) |
| 5094 | return (void**)internal_malloc(m, 0); |
| 5095 | marray = 0; |
| 5096 | array_size = request2size(n_elements * (sizeof(void*))); |
| 5097 | } |
| 5098 | |
| 5099 | /* compute total element size */ |
| 5100 | if (opts & 0x1) { /* all-same-size */ |
| 5101 | element_size = request2size(*sizes); |
| 5102 | contents_size = n_elements * element_size; |
| 5103 | } |
| 5104 | else { /* add up all the sizes */ |
| 5105 | element_size = 0; |
| 5106 | contents_size = 0; |
| 5107 | for (i = 0; i != n_elements; ++i) |
| 5108 | contents_size += request2size(sizes[i]); |
| 5109 | } |
| 5110 | |
| 5111 | size = contents_size + array_size; |
| 5112 | |
| 5113 | /* |
| 5114 | Allocate the aggregate chunk. First disable direct-mmapping so |
| 5115 | malloc won't use it, since we would not be able to later |
| 5116 | free/realloc space internal to a segregated mmap region. |
| 5117 | */ |
| 5118 | was_enabled = use_mmap(m); |
| 5119 | disable_mmap(m); |
| 5120 | mem = internal_malloc(m, size - CHUNK_OVERHEAD); |
| 5121 | if (was_enabled) |
| 5122 | enable_mmap(m); |
no test coverage detected