~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/segment/pagein.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 *
10
10
 * \author GRASS GIS Development Team
11
11
 *
12
 
 * \date 2005-2006
 
12
 * \date 2005-2009
13
13
 */
14
14
 
15
15
#include <stdio.h>
16
16
#include <unistd.h>
17
17
#include <string.h>
18
18
#include <errno.h>
19
 
#include <grass/segment.h>
20
 
 
21
 
 
22
 
static int segment_select(SEGMENT *, int);
 
19
#include <grass/gis.h>
 
20
#include "local_proto.h"
23
21
 
24
22
 
25
23
/**
26
 
 * \fn int segment_pagein (SEGMENT *SEG, int n)
27
 
 *
28
 
 * \brief Segment pagein.
 
24
 * \brief Internal use only
 
25
 * 
 
26
 * Segment pagein.
29
27
 *
30
28
 * Finds <b>n</b> in the segment file, <b>seg</b>, and selects it as the 
31
29
 * current segment.
32
30
 *
33
 
 * \param[in] seg segment
 
31
 * \param[in] SEG segment
34
32
 * \param[in] n segment number
35
33
 * \return 1 if successful
36
34
 * \return -1 if unable to seek or read segment file
37
35
 */
38
36
 
39
 
int segment_pagein(SEGMENT * SEG, int n)
 
37
int seg_pagein(SEGMENT * SEG, int n)
40
38
{
41
 
    int age;
42
39
    int cur;
43
 
    int i;
44
40
    int read_result;
45
41
 
46
42
    /* is n the current segment? */
47
43
    if (n == SEG->scb[SEG->cur].n)
48
44
        return SEG->cur;
49
45
 
50
 
    /* search the in memory segments */
51
 
    for (i = 0; i < SEG->nseg; i++)
52
 
        if (n == SEG->scb[i].n)
53
 
            return segment_select(SEG, i);
 
46
    /* segment n is in memory ? */
 
47
 
 
48
    if (SEG->load_idx[n] >= 0) {
 
49
        cur = SEG->load_idx[n];
 
50
 
 
51
        if (SEG->scb[cur].age != SEG->youngest) {
 
52
            /* splice out */
 
53
            SEG->scb[cur].age->younger->older = SEG->scb[cur].age->older;
 
54
            SEG->scb[cur].age->older->younger = SEG->scb[cur].age->younger;
 
55
            /* splice in */
 
56
            SEG->scb[cur].age->younger = SEG->youngest->younger;
 
57
            SEG->scb[cur].age->older = SEG->youngest;
 
58
            SEG->scb[cur].age->older->younger = SEG->scb[cur].age;
 
59
            SEG->scb[cur].age->younger->older = SEG->scb[cur].age;
 
60
            /* make it youngest */
 
61
            SEG->youngest = SEG->scb[cur].age;
 
62
        }
 
63
 
 
64
        return SEG->cur = cur;
 
65
    }
54
66
 
55
67
    /* find a slot to use to hold segment */
56
 
    age = 0;
57
 
    cur = 0;
58
 
    for (i = 0; i < SEG->nseg; i++)
59
 
        if (SEG->scb[i].n < 0) {        /* free slot */
60
 
            cur = i;
61
 
            break;
62
 
        }
63
 
        else if (age < SEG->scb[i].age) {       /* find oldest segment */
64
 
            cur = i;
65
 
            age = SEG->scb[i].age;
66
 
        }
67
 
 
68
 
    /* if slot is used, write it out, if dirty */
69
 
    if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty)
70
 
        if (segment_pageout(SEG, cur) < 0)
71
 
            return -1;
 
68
    if (!SEG->nfreeslots) {
 
69
        /* use oldest segment */
 
70
        SEG->oldest = SEG->oldest->younger;
 
71
        cur = SEG->oldest->cur;
 
72
        SEG->oldest->cur = -1;
 
73
 
 
74
        /* unload segment */
 
75
        if (SEG->scb[cur].n >= 0) {
 
76
            SEG->load_idx[SEG->scb[cur].n] = -1;
 
77
 
 
78
            /* write it out if dirty */
 
79
            if (SEG->scb[cur].dirty) {
 
80
                if (seg_pageout(SEG, cur) < 0)
 
81
                    return -1;
 
82
            }
 
83
        }
 
84
    }
 
85
    else {
 
86
        /* free slots left */
 
87
        cur = SEG->freeslot[--SEG->nfreeslots];
 
88
    }
72
89
 
73
90
    /* read in the segment */
74
91
    SEG->scb[cur].n = n;
75
92
    SEG->scb[cur].dirty = 0;
76
 
    segment_seek(SEG, SEG->scb[cur].n, 0);
 
93
    SEG->seek(SEG, SEG->scb[cur].n, 0);
77
94
 
78
95
    read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
79
96
    if (read_result != SEG->size) {
80
 
        G_debug(2, "segment_pagein: read_result=%d  SEG->size=%d",
 
97
        G_debug(2, "Segment pagein: read_result=%d  SEG->size=%d",
81
98
                read_result, SEG->size);
82
99
 
83
100
        if (read_result < 0)
84
 
            G_warning("segment_pagein: %s", strerror(errno));
 
101
            G_warning("Segment pagein: %s", strerror(errno));
85
102
        else if (read_result == 0)
86
 
            G_warning("segment_pagein: read EOF");
 
103
            G_warning("Segment pagein: read EOF");
87
104
        else
88
105
            G_warning
89
 
                ("segment_pagein: short count during read(), got %d, expected %d",
 
106
                ("Segment pagein: short count during read(), got %d, expected %d",
90
107
                 read_result, SEG->size);
91
108
 
92
109
        return -1;
93
110
    }
94
111
 
95
 
    return segment_select(SEG, cur);
96
 
}
97
 
 
98
 
 
99
 
static int segment_select(SEGMENT * SEG, int n)
100
 
{
101
 
    int i;
102
 
 
103
 
    SEG->scb[n].age = 0;
104
 
    for (i = 0; i < SEG->nseg; i++)
105
 
        SEG->scb[i].age++;
106
 
 
107
 
    return SEG->cur = n;
 
112
    /* add loaded segment to index */
 
113
    SEG->load_idx[n] = cur;
 
114
 
 
115
    /* make it youngest segment */
 
116
    SEG->youngest = SEG->youngest->younger;
 
117
    SEG->scb[cur].age = SEG->youngest;
 
118
    SEG->youngest->cur = cur;
 
119
 
 
120
    return SEG->cur = cur;
108
121
}