memory_block_overlap pool_contains_block(memory_pool *pool, void *ptr, size_t size, void **found_block, size_t *found_block_size) { memory_block_overlap overlap = OVERLAP_NONE; const UINT8 *this_memory = (const UINT8 *) ptr; size_t this_memory_size = size; pool_entry *entry; const UINT8 *that_memory = NULL; size_t that_memory_size = 0; /* loop through the memory blocks in this pool */ for (entry = pool->first; entry != NULL; entry = entry->next) { that_memory = (const UINT8 *) entry->buffer; that_memory_size = entry->size; /* check for overlap */ if (pointer_in_block(this_memory, that_memory, that_memory_size)) { if (pointer_in_block(this_memory + this_memory_size - 1, that_memory, that_memory_size)) overlap = OVERLAP_FULL; else overlap = OVERLAP_PARTIAL; break; } else if (pointer_in_block(that_memory, this_memory, this_memory_size)) { if (pointer_in_block(that_memory + that_memory_size - 1, this_memory, this_memory_size)) overlap = OVERLAP_FULL; else overlap = OVERLAP_PARTIAL; break; } that_memory = NULL; that_memory_size = 0; } if (found_block != NULL) *found_block = (void *) that_memory; if (found_block_size != NULL) *found_block_size = that_memory_size; return overlap; }
void validate_auto_malloc_memory(void *memory, size_t memory_size) { int i; int tag = 0; const UINT8 *this_memory = (const UINT8 *) memory; size_t this_memory_size = memory_size; assert(memory_size > 0); for (i = 0; i < malloc_list_size; i++) { if (malloc_list[i].memory != NULL) { const UINT8 *that_memory = (const UINT8 *) malloc_list[i].memory; size_t that_memory_size = malloc_list[i].size; if (pointer_in_block(this_memory, that_memory, that_memory_size)) { if (!pointer_in_block(this_memory + this_memory_size - 1, that_memory, that_memory_size)) fatalerror("Memory block [0x%p-0x%p] partially overlaps with allocated block [0x%p-0x%p]", this_memory, this_memory + this_memory_size - 1, that_memory, that_memory + that_memory_size - 1); return; } else if (pointer_in_block(that_memory, this_memory, this_memory_size)) { if (!pointer_in_block(that_memory + that_memory_size - 1, this_memory, this_memory_size)) fatalerror("Memory block [0x%p-0x%p] partially overlaps with allocated block [0x%p-0x%p]", this_memory, this_memory + this_memory_size - 1, that_memory, that_memory + that_memory_size - 1); return; } } else { tag++; } } fatalerror("Memory block [0x%p-0x%p] not found", this_memory, this_memory + this_memory_size - 1); }