+ // too big, allocate it directly
+#endif
+#if MEMCLUMPING == 2
+ return NULL;
+#else
+ base = (unsigned char *)attempt_malloc(size);
+ if (base && developer_memorydebug.integer)
+ memset(base, 0xAF, size);
+ return base;
+#endif
+}
+static void Clump_FreeBlock(void *base, size_t size)
+{
+#if MEMCLUMPING
+ unsigned int needbits;
+ unsigned int startbit;
+ unsigned int endbit;
+ unsigned int bit;
+ memclump_t **clumpchainpointer;
+ memclump_t *clump;
+ unsigned char *start = (unsigned char *)base;
+ for (clumpchainpointer = &clumpchain;(clump = *clumpchainpointer);clumpchainpointer = &(*clumpchainpointer)->chain)
+ {
+ if (start >= clump->block && start < clump->block + MEMCLUMPSIZE)
+ {
+ if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
+ Sys_Error("Clump_FreeBlock: trashed sentinel1\n");
+ if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
+ Sys_Error("Clump_FreeBlock: trashed sentinel2\n");
+ if (start + size > clump->block + MEMCLUMPSIZE)
+ Sys_Error("Clump_FreeBlock: block overrun\n");
+ // the block belongs to this clump, clear the range
+ needbits = (size + MEMUNIT - 1) / MEMUNIT;
+ startbit = (start - clump->block) / MEMUNIT;
+ endbit = startbit + needbits;
+ // first verify all bits are set, otherwise this may be misaligned or a double free
+ for (bit = startbit;bit < endbit;bit++)
+ if ((clump->bits[bit>>5] & (1<<(bit & 31))) == 0)
+ Sys_Error("Clump_FreeBlock: double free\n");
+ for (bit = startbit;bit < endbit;bit++)
+ clump->bits[bit>>5] &= ~(1<<(bit & 31));
+ clump->blocksinuse -= needbits;
+ memset(base, 0xFF, needbits * MEMUNIT);
+ // if all has been freed, free the clump itself
+ if (clump->blocksinuse == 0)
+ {
+ *clumpchainpointer = clump->chain;
+ if (developer_memorydebug.integer)
+ memset(clump, 0xFF, sizeof(*clump));
+#if MEMCLUMPING != 2
+ free(clump);
+#endif
+ }
+ return;
+ }
+ }
+ // does not belong to any known chunk... assume it was a direct allocation
+#endif
+#if MEMCLUMPING != 2
+ memset(base, 0xFF, size);
+ free(base);
+#endif