[RFA] rda/samples: improve memory allocation

Michael Snyder
2004-07-09T21:39:56Z

[-- Attachment #1: Type: text/plain, Size: 173 bytes --]

Two problems with existing code:
1) Grows the simulated memory one byte at a time, and
2) If target_mem.base ever moves down, doesn't compensate
by moving the contents up.

[-- Attachment #2: diff5 --]
[-- Type: text/plain, Size: 2499 bytes --]

2004-07-09 Michael Snyder <msnyder@redhat.com>

* samples/demo-target.c (demo_set_thread_mem): Allocate new
simulated memory in hunks, rather than one byte at a time.
If target_mem.base moves down, copy contents up.

Index: demo-target.c
===================================================================
RCS file: /cvs/cvsfiles/devo/rda/samples/demo-target.c,v
retrieving revision 1.19
diff -p -r1.19 demo-target.c
*** demo-target.c 21 Aug 2002 22:43:14 -0000 1.19
--- demo-target.c 9 Jul 2004 21:30:22 -0000
*************** demo_get_mem (struct gdbserv* serv,
*** 385,390 ****
--- 385,393 ----
return n;
}

+ #define ALLOC_UNIT 0x1000;
+ #define alloc_roundup(LEN) ((LEN / ALLOC_UNIT) + ALLOC_UNIT)
+

static long
demo_set_thread_mem (struct gdbserv *serv,
*************** demo_set_thread_mem (struct gdbserv *ser
*** 398,420 ****
gdbserv_reg_to_ulong (serv, addr, &request_base);
if (target_mem.len == 0)
{
! target_mem.buf = malloc (len);
! target_mem.len = len;
! gdbserv_reg_to_ulong (serv, addr, &target_mem.base);
}
else
{
if (request_base < target_mem.base)
{
! target_mem.len += target_mem.base - request_base;
target_mem.base = request_base;
target_mem.buf = realloc (target_mem.buf, target_mem.len);
}
if (request_base + len >
target_mem.base + target_mem.len)
{
! target_mem.len +=
! (request_base + len) - (target_mem.base + target_mem.len);
target_mem.buf = realloc (target_mem.buf, target_mem.len);
}
}
--- 401,427 ----
gdbserv_reg_to_ulong (serv, addr, &request_base);
if (target_mem.len == 0)
{
! target_mem.len = alloc_roundup (len);
! target_mem.buf = malloc (target_mem.len);
! target_mem.base = request_base;
}
else
{
if (request_base < target_mem.base)
{
! unsigned long oldlen = target_mem.len;
! unsigned long movlen = target_mem.base - request_base;
!
! target_mem.len += alloc_roundup (target_mem.base - request_base);
target_mem.base = request_base;
target_mem.buf = realloc (target_mem.buf, target_mem.len);
+ memmove (target_mem.buf + movlen, target_mem.buf, oldlen);
}
if (request_base + len >
target_mem.base + target_mem.len)
{
! target_mem.len += alloc_roundup ((request_base + len) -
! (target_mem.base + target_mem.len));
target_mem.buf = realloc (target_mem.buf, target_mem.len);
}
}