/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include <stdbool.h>
+
#include "mc_private.h"
#include "mc_mmu.h"
}
}
+/** Compare memory between snapshots (with known regions)
+ *
+ * @param addr1 Address in the first snapshot
+ * @param snapshot2 Region of the address in the first snapshot
+ * @param addr2 Address in the second snapshot
+ * @param snapshot2 Region of the address in the second snapshot
+ * @return same as memcmp
+ * */
+int mc_snapshot_region_memcp(
+ void* addr1, mc_mem_region_t region1,
+ void* addr2, mc_mem_region_t region2, size_t size)
+{
+ // Using alloca() for large allocations may trigger stack overflow:
+ // use malloc if the buffer is too big.
+
+ bool stack_alloc = size < 64;
+ void* buffer = stack_alloc ? alloca(2*size) : malloc(2*size);
+ void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer, size);
+ void* buffer2 = mc_snapshot_read_region(addr2, region2, (char*) buffer + size, size);
+ int res;
+ if (buffer1 == buffer2) {
+ res = 0;
+ } else {
+ res = memcmp(buffer1, buffer2, size);
+ }
+ if (!stack_alloc) {
+ free(buffer);
+ }
+ return res;
+}
+
+/** Compare memory between snapshots
+ *
+ * @param addr1 Address in the first snapshot
+ * @param snapshot1 First snapshot
+ * @param addr2 Address in the second snapshot
+ * @param snapshot2 Second snapshot
+ * @return same as memcmp
+ * */
int mc_snapshot_memcp(
void* addr1, mc_snapshot_t snapshot1,
void* addr2, mc_snapshot_t snapshot2, size_t size)
{
- void* buffer1 = mc_snapshot_read(addr1, snapshot1, alloca(size), size);
- void* buffer2 = mc_snapshot_read(addr2, snapshot2, alloca(size), size);
- if (buffer1 == buffer2) {
- return 0;
- }
- return memcmp(buffer1, buffer2, size);
+ mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
+ mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
+ return mc_snapshot_region_memcp(addr1, region1, addr2, region2, size);
}