+void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
+{
+ memset(l, 0, sizeof(*l));
+ l->mempool = mempool;
+ l->recordsize = recordsize;
+ l->numrecordsperarray = numrecordsperarray;
+}
+
+void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
+{
+ size_t i;
+ if (l->maxarrays)
+ {
+ for (i = 0;i != l->numarrays;i++)
+ Mem_Free(l->arrays[i].data);
+ Mem_Free(l->arrays);
+ }
+ memset(l, 0, sizeof(*l));
+}
+
+// VorteX: hacked Mem_ExpandableArray_AllocRecord, it does allocate record at certain index
+void *Mem_ExpandableArray_AllocRecordAtIndex(memexpandablearray_t *l, size_t index)
+{
+ size_t j;
+ if (index == l->numarrays)
+ {
+ if (l->numarrays == l->maxarrays)
+ {
+ memexpandablearray_array_t *oldarrays = l->arrays;
+ l->maxarrays = max(l->maxarrays * 2, 128);
+ l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
+ if (oldarrays)
+ {
+ memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
+ Mem_Free(oldarrays);
+ }
+ }
+ l->arrays[index].numflaggedrecords = 0;
+ l->arrays[index].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
+ l->arrays[index].allocflags = l->arrays[index].data + l->recordsize * l->numrecordsperarray;
+ l->numarrays++;
+ }
+ if (l->arrays[index].numflaggedrecords < l->numrecordsperarray)
+ {
+ for (j = 0;j < l->numrecordsperarray;j++)
+ {
+ if (!l->arrays[index].allocflags[j])
+ {
+ l->arrays[index].allocflags[j] = true;
+ l->arrays[index].numflaggedrecords++;
+ memset(l->arrays[index].data + l->recordsize * j, 0, l->recordsize);
+ return (void *)(l->arrays[index].data + l->recordsize * j);
+ }
+ }
+ }
+ return NULL;
+}
+
+void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
+{
+ size_t i, j;
+ for (i = 0;;i++)
+ {
+ if (i == l->numarrays)
+ {
+ if (l->numarrays == l->maxarrays)
+ {
+ memexpandablearray_array_t *oldarrays = l->arrays;
+ l->maxarrays = max(l->maxarrays * 2, 128);
+ l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
+ if (oldarrays)
+ {
+ memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
+ Mem_Free(oldarrays);
+ }
+ }
+ l->arrays[i].numflaggedrecords = 0;
+ l->arrays[i].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
+ l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
+ l->numarrays++;
+ }
+ if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
+ {
+ for (j = 0;j < l->numrecordsperarray;j++)
+ {
+ if (!l->arrays[i].allocflags[j])
+ {
+ l->arrays[i].allocflags[j] = true;
+ l->arrays[i].numflaggedrecords++;
+ memset(l->arrays[i].data + l->recordsize * j, 0, l->recordsize);
+ return (void *)(l->arrays[i].data + l->recordsize * j);
+ }
+ }
+ }
+ }
+}
+
+/*****************************************************************************
+ * IF YOU EDIT THIS:
+ * If this function was to change the size of the "expandable" array, you have
+ * to update r_shadow.c
+ * Just do a search for "range =", R_ShadowClearWorldLights would be the first
+ * function to look at. (And also seems like the only one?) You might have to
+ * move the call to Mem_ExpandableArray_IndexRange back into for(...) loop's
+ * condition
+ */
+void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record) // const!
+{
+ size_t i, j;
+ unsigned char *p = (unsigned char *)record;
+ for (i = 0;i != l->numarrays;i++)
+ {
+ if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
+ {
+ j = (p - l->arrays[i].data) / l->recordsize;
+ if (p != l->arrays[i].data + j * l->recordsize)
+ Sys_Error("Mem_ExpandableArray_FreeRecord: no such record %p\n", p);
+ if (!l->arrays[i].allocflags[j])
+ Sys_Error("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", p);
+ l->arrays[i].allocflags[j] = false;
+ l->arrays[i].numflaggedrecords--;
+ return;
+ }
+ }
+}
+
+size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l)
+{
+ size_t i, j, k, end = 0;
+ for (i = 0;i < l->numarrays;i++)
+ {
+ for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
+ {
+ if (l->arrays[i].allocflags[j])
+ {
+ end = l->numrecordsperarray * i + j + 1;
+ k++;
+ }
+ }
+ }
+ return end;
+}
+
+void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index)
+{
+ size_t i, j;
+ i = index / l->numrecordsperarray;
+ j = index % l->numrecordsperarray;
+ if (i >= l->numarrays || !l->arrays[i].allocflags[j])
+ return NULL;
+ return (void *)(l->arrays[i].data + j * l->recordsize);
+}
+