Search
lxdream.org :: lxdream/src/watch.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/watch.c
changeset 6:6e3663e58565
prev2:42349f6ea216
next480:d28c2992f5ee
author nkeynes
date Sat Oct 02 05:49:39 2004 +0000 (15 years ago)
permissions -rw-r--r--
last change compile fix
view annotate diff log raw
     1 #include <stdlib.h>
     2 #include "mem.h"
     4 struct watch_point {
     5     uint32_t start;
     6     uint32_t end;
     7     int flags;
     8 };
    10 struct watch_point *watch_arr = NULL;
    11 int watch_count = 0, watch_capacity = 0;
    14 watch_point_t mem_new_watch( uint32_t start, uint32_t end, int flags )
    15 {
    16     int num;
    17     if( watch_arr == NULL ) {
    18         watch_capacity = 10;
    19         watch_arr = calloc( sizeof(struct watch_point), watch_capacity );
    20         num = 0;
    21     } else if( watch_count == watch_capacity ) {
    22         struct watch_point *tmp = realloc( watch_arr, sizeof(struct watch_point) * watch_capacity * 2 );
    23         if( tmp == NULL )
    24             return NULL;
    25         watch_arr = tmp;
    26         memset( &watch_arr[watch_capacity], 0, sizeof( struct watch_point ) * watch_capacity );
    27         num = watch_capacity;
    28         watch_capacity *= 2;
    29     } else {
    30         for( num=0; num<watch_capacity; num++ ) {
    31             if( watch_arr[num].flags == 0 )
    32                 break;
    33         }
    34     }
    35     watch_arr[num].start = start & 0x1FFFFFFF;
    36     watch_arr[num].end = end & 0x1FFFFFFF;
    37     watch_arr[num].flags = flags;
    38     watch_count++;
    39     return &watch_arr[num];
    40 }
    42 void mem_delete_watch( watch_point_t watch )
    43 {
    44     if( watch_arr == NULL )
    45         return;
    46     int num = watch - watch_arr;
    47     if( num < 0 || num >= watch_capacity )
    48         return;
    49     watch->start = watch->end = 0;
    50     watch->flags = 0;
    51     watch_count--;
    52 }
    55 watch_point_t mem_is_watched( uint32_t addr, int size, int op )
    56 {
    57     int i, count;
    58     addr &= 0x1FFFFFFF;
    59     for( i=0, count=0; count< watch_count; i++ ) {
    60         if( watch_arr[i].flags == 0 )
    61             continue;
    62         count++;
    63         if( watch_arr[i].flags & op &&
    64             watch_arr[i].start < addr+size &&
    65             watch_arr[i].end >= addr ) {
    66             return &watch_arr[i];
    67         }
    68     }
    69     return NULL;
    70 }
.