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