Search
lxdream.org :: lxdream/src/watch.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/watch.c
changeset 736:a02d1475ccfd
prev561:533f6b478071
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
file annotate diff log raw
nkeynes@561
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@561
     3
 *
nkeynes@561
     4
 * Watchpoint support (for debugging)
nkeynes@561
     5
 *
nkeynes@561
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@561
     7
 *
nkeynes@561
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@561
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@561
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@561
    11
 * (at your option) any later version.
nkeynes@561
    12
 *
nkeynes@561
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@561
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@561
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@561
    16
 * GNU General Public License for more details.
nkeynes@561
    17
 */
nkeynes@561
    18
nkeynes@2
    19
#include <stdlib.h>
nkeynes@480
    20
#include <string.h>
nkeynes@2
    21
#include "mem.h"
nkeynes@2
    22
nkeynes@2
    23
struct watch_point {
nkeynes@2
    24
    uint32_t start;
nkeynes@2
    25
    uint32_t end;
nkeynes@2
    26
    int flags;
nkeynes@2
    27
};
nkeynes@2
    28
nkeynes@2
    29
struct watch_point *watch_arr = NULL;
nkeynes@2
    30
int watch_count = 0, watch_capacity = 0;
nkeynes@2
    31
nkeynes@2
    32
nkeynes@2
    33
watch_point_t mem_new_watch( uint32_t start, uint32_t end, int flags )
nkeynes@2
    34
{
nkeynes@2
    35
    int num;
nkeynes@2
    36
    if( watch_arr == NULL ) {
nkeynes@2
    37
        watch_capacity = 10;
nkeynes@2
    38
        watch_arr = calloc( sizeof(struct watch_point), watch_capacity );
nkeynes@2
    39
        num = 0;
nkeynes@2
    40
    } else if( watch_count == watch_capacity ) {
nkeynes@2
    41
        struct watch_point *tmp = realloc( watch_arr, sizeof(struct watch_point) * watch_capacity * 2 );
nkeynes@2
    42
        if( tmp == NULL )
nkeynes@2
    43
            return NULL;
nkeynes@2
    44
        watch_arr = tmp;
nkeynes@2
    45
        memset( &watch_arr[watch_capacity], 0, sizeof( struct watch_point ) * watch_capacity );
nkeynes@2
    46
        num = watch_capacity;
nkeynes@2
    47
        watch_capacity *= 2;
nkeynes@2
    48
    } else {
nkeynes@2
    49
        for( num=0; num<watch_capacity; num++ ) {
nkeynes@2
    50
            if( watch_arr[num].flags == 0 )
nkeynes@2
    51
                break;
nkeynes@2
    52
        }
nkeynes@2
    53
    }
nkeynes@2
    54
    watch_arr[num].start = start & 0x1FFFFFFF;
nkeynes@2
    55
    watch_arr[num].end = end & 0x1FFFFFFF;
nkeynes@2
    56
    watch_arr[num].flags = flags;
nkeynes@2
    57
    watch_count++;
nkeynes@2
    58
    return &watch_arr[num];
nkeynes@2
    59
}
nkeynes@2
    60
nkeynes@2
    61
void mem_delete_watch( watch_point_t watch )
nkeynes@2
    62
{
nkeynes@2
    63
    if( watch_arr == NULL )
nkeynes@2
    64
        return;
nkeynes@2
    65
    int num = watch - watch_arr;
nkeynes@2
    66
    if( num < 0 || num >= watch_capacity )
nkeynes@6
    67
        return;
nkeynes@2
    68
    watch->start = watch->end = 0;
nkeynes@2
    69
    watch->flags = 0;
nkeynes@2
    70
    watch_count--;
nkeynes@2
    71
}
nkeynes@2
    72
nkeynes@2
    73
nkeynes@2
    74
watch_point_t mem_is_watched( uint32_t addr, int size, int op )
nkeynes@2
    75
{
nkeynes@2
    76
    int i, count;
nkeynes@2
    77
    addr &= 0x1FFFFFFF;
nkeynes@2
    78
    for( i=0, count=0; count< watch_count; i++ ) {
nkeynes@2
    79
        if( watch_arr[i].flags == 0 )
nkeynes@2
    80
            continue;
nkeynes@2
    81
        count++;
nkeynes@2
    82
        if( watch_arr[i].flags & op &&
nkeynes@736
    83
                watch_arr[i].start < addr+size &&
nkeynes@736
    84
                watch_arr[i].end >= addr ) {
nkeynes@2
    85
            return &watch_arr[i];
nkeynes@2
    86
        }
nkeynes@2
    87
    }
nkeynes@2
    88
    return NULL;
nkeynes@2
    89
}
nkeynes@2
    90
.