- for (i = 0; i < AO_LISP_NCHUNK; i++) {
- if (ao_lisp_chunk[i].size && ao_lisp_chunk[i].old_addr == addr) {
- if (ao_lisp_chunk[i].size != size)
- ao_lisp_abort();
- return;
- }
- if (ao_lisp_chunk[i].old_addr > addr) {
- memmove(&ao_lisp_chunk[i+1],
- &ao_lisp_chunk[i],
- (AO_LISP_NCHUNK - (i+1)) * sizeof (struct ao_lisp_chunk));
- ao_lisp_chunk[i].size = 0;
- }
- if (ao_lisp_chunk[i].size == 0) {
- ao_lisp_chunk[i].old_addr = addr;
- ao_lisp_chunk[i].size = size;
- return;
- }
- }
+ l = find_chunk(offset);
+
+ /*
+ * The correct location is always in 'l', with r = l-1 being
+ * the entry before the right one
+ */
+
+#if DBG_MEM
+ /* Off the right side */
+ if (l >= AO_LISP_NCHUNK)
+ ao_lisp_abort();
+
+ /* Off the left side */
+ if (l == 0 && chunk_last && offset > ao_lisp_chunk[0].old_offset)
+ ao_lisp_abort();
+#endif
+
+ /* Shuffle existing entries right */
+ int end = min(AO_LISP_NCHUNK, chunk_last + 1);
+
+ memmove(&ao_lisp_chunk[l+1],
+ &ao_lisp_chunk[l],
+ (end - (l+1)) * sizeof (struct ao_lisp_chunk));
+
+ /* Add new entry */
+ ao_lisp_chunk[l].old_offset = offset;
+ ao_lisp_chunk[l].size = size;
+
+ /* Increment the number of elements up to the size of the array */
+ if (chunk_last < AO_LISP_NCHUNK)
+ chunk_last++;
+
+ /* Set the top address if the array is full */
+ if (chunk_last == AO_LISP_NCHUNK)
+ chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
+ ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
+}
+
+static void
+reset_chunks(void)
+{
+ chunk_high = ao_lisp_top;
+ chunk_last = 0;
+ chunk_first = 0;