File: offloading_queue.h

package info (click to toggle)
freecell-solver 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,332 kB
  • sloc: ansic: 29,493; perl: 8,911; xml: 5,162; python: 1,124; sh: 777; ruby: 358; cpp: 304; makefile: 150
file content (327 lines) | stat: -rw-r--r-- 9,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Copyright (c) 2000 Shlomi Fish
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
/*
 * offloading_queue.h - header file for the offloading-to-hard-disk
 * queue.
 */

#ifndef FC_SOLVE__OFFLOADING_QUEUE_H
#define FC_SOLVE__OFFLOADING_QUEUE_H

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>

#include "config.h"
#include "state.h"
#include "inline.h"
#include "bool.h"

#include "delta_states.h"

#if 0
typedef struct { unsigned char s[24]; } fcs_offloading_queue_item_t;
#endif

typedef const unsigned char * fcs_offloading_queue_item_t;

typedef struct
{
    int num_items_per_page;
    long page_index;
    int write_to_idx;
    int read_from_idx;
    unsigned char * data;
} fcs_offloading_queue_page_t;

static GCC_INLINE void fcs_offloading_queue_page__recycle(
    fcs_offloading_queue_page_t * page
    )
{
    page->write_to_idx = 0;
    page->read_from_idx = 0;
}

static GCC_INLINE void fcs_offloading_queue_page__init(
    fcs_offloading_queue_page_t * page,
    int num_items_per_page,
    long page_index
    )
{
    page->num_items_per_page = num_items_per_page;
    page->page_index = page_index;

    page->data = malloc(
        sizeof(fcs_offloading_queue_item_t) * num_items_per_page
    );
    fcs_offloading_queue_page__recycle(page);

    return;
}

static GCC_INLINE void fcs_offloading_queue_page__destroy(
    fcs_offloading_queue_page_t * page
)
{
    free(page->data);
    page->data = NULL;
}

static GCC_INLINE fcs_bool_t fcs_offloading_queue_page__can_extract(
    fcs_offloading_queue_page_t * page
)
{
    return (page->read_from_idx < page->write_to_idx);
}

static GCC_INLINE void fcs_offloading_queue_page__extract(
    fcs_offloading_queue_page_t * page,
    fcs_offloading_queue_item_t * out_item
)
{
    int read_from_idx;

    read_from_idx = ((page->read_from_idx)++);

    memcpy(out_item, page->data + sizeof(fcs_offloading_queue_item_t) * read_from_idx, sizeof(fcs_offloading_queue_item_t));
}

static GCC_INLINE fcs_bool_t fcs_offloading_queue_page__can_insert(
    fcs_offloading_queue_page_t * page
)
{
    return (page->write_to_idx < page->num_items_per_page);
}

static GCC_INLINE void fcs_offloading_queue_page__insert(
    fcs_offloading_queue_page_t * page,
    const fcs_offloading_queue_item_t const * in_item
    )
{
    int write_to_idx = ((page->write_to_idx)++);

    memcpy(
        page->data+write_to_idx * sizeof(fcs_offloading_queue_item_t),
        in_item,
        sizeof(*in_item)
    );
}

static GCC_INLINE const char * fcs_offloading_queue_page__calc_filename(
    fcs_offloading_queue_page_t * page,
    char * buffer,
    const char * offload_dir_path)
{
    sprintf(buffer, "%s/fcs_queue_%020lX.page", offload_dir_path, page->page_index);

    return buffer;
}

static GCC_INLINE void fcs_offloading_queue_page__start_after(
    fcs_offloading_queue_page_t * page,
    fcs_offloading_queue_page_t * other_page
    )
{
    page->page_index = other_page->page_index+1;
    fcs_offloading_queue_page__recycle(page);
}

static GCC_INLINE void fcs_offloading_queue_page__bump(
    fcs_offloading_queue_page_t * page
)
{
    fcs_offloading_queue_page__start_after(page, page);
}

static GCC_INLINE void fcs_offloading_queue_page__read_next_from_disk(
    fcs_offloading_queue_page_t * page,
    const char * offload_dir_path
    )
{
    FILE * f;
    char page_filename[PATH_MAX+1];

    fcs_offloading_queue_page__bump(page);

    fcs_offloading_queue_page__calc_filename(page, page_filename, offload_dir_path);

    f = fopen(page_filename, "rb");
    fread( page->data, sizeof(fcs_offloading_queue_item_t),
           page->num_items_per_page, f
    );
    fclose(f);

    /* We need to set this limit because it's a read-only page that we
     * retrieve from the disk and otherwise ->can_extract() will return
     * false for most items.
     * */
    page->write_to_idx = page->num_items_per_page;

    unlink(page_filename);
}

static GCC_INLINE void fcs_offloading_queue_page__offload(
    fcs_offloading_queue_page_t * page,
    const char * offload_dir_path
    )
{
    FILE * f;
    char page_filename[PATH_MAX+1];

    fcs_offloading_queue_page__calc_filename(page, page_filename, offload_dir_path);

    f = fopen(page_filename, "wb");
    fwrite( page->data, sizeof(fcs_offloading_queue_item_t),
           page->num_items_per_page, f
    );
    fclose(f);
}

typedef struct
{
    int num_items_per_page;
    const char * offload_dir_path;
    long num_inserted, num_items_in_queue, num_extracted;
    /*
     * page_to_write_to, page_for_backup and page_to_read_from always
     * point to the two "pages" below, but they can be swapped and
     * page_for_backup may be NULL.
     */
    fcs_offloading_queue_page_t * page_to_write_to, * page_for_backup,
                                * page_to_read_from;
    fcs_offloading_queue_page_t pages[2];
} fcs_offloading_queue_t;

static GCC_INLINE void fcs_offloading_queue__init(
    fcs_offloading_queue_t * queue,
    int num_items_per_page,
    const char * offload_dir_path
    )
{
    queue->num_items_per_page = num_items_per_page;
    queue->offload_dir_path = offload_dir_path;
    queue->num_inserted = queue->num_items_in_queue = queue->num_extracted = 0;

    fcs_offloading_queue_page__init(&(queue->pages[0]), num_items_per_page, 0);
    fcs_offloading_queue_page__init(&(queue->pages[1]), num_items_per_page, 0);

    queue->page_to_read_from = queue->page_to_write_to = &(queue->pages[0]);
    queue->page_for_backup = &(queue->pages[1]);
}

static GCC_INLINE void fcs_offloading_queue__destroy(
    fcs_offloading_queue_t * queue
    )
{
    fcs_offloading_queue_page__destroy(&(queue->pages[0]));
    fcs_offloading_queue_page__destroy(&(queue->pages[1]));
}

static GCC_INLINE void fcs_offloading_queue__insert(
    fcs_offloading_queue_t * queue,
    const fcs_offloading_queue_item_t * item
)
{
    if (! fcs_offloading_queue_page__can_insert(queue->page_to_write_to))
    {
        if (queue->page_to_read_from->page_index != queue->page_to_write_to->page_index)
        {
            fcs_offloading_queue_page__offload(
                queue->page_to_write_to,
                queue->offload_dir_path
            );
            fcs_offloading_queue_page__bump(queue->page_to_write_to);
        }
        else
        {
            queue->page_to_write_to = queue->page_for_backup;
            fcs_offloading_queue_page__start_after(
                queue->page_to_write_to,
                queue->page_to_read_from
            );
            queue->page_for_backup = NULL;
        }
    }

    fcs_offloading_queue_page__insert(
        queue->page_to_write_to,
        item
    );

    queue->num_inserted++;
    queue->num_items_in_queue++;
}

static GCC_INLINE fcs_bool_t fcs_offloading_queue__extract(
    fcs_offloading_queue_t * queue,
    fcs_offloading_queue_item_t * return_item
)
{
    if (queue->num_items_in_queue == 0)
    {
        return FALSE;
    }

    if (! fcs_offloading_queue_page__can_extract(queue->page_to_read_from))
    {
        /* Cannot really happen due to the num_items_in_queue check.
         *
         * if (queue->_page_to_read_from->page_index ==
         *     queue->_page_to_write_to->page_index)
        */
        if (queue->page_to_read_from->page_index + 1 == queue->page_to_write_to->page_index)
        {
            queue->page_for_backup = queue->page_to_read_from;
            queue->page_to_read_from = queue->page_to_write_to;
        }
        else
        {
            fcs_offloading_queue_page__read_next_from_disk(
                queue->page_to_read_from,
                queue->offload_dir_path
            );
        }
    }

    queue->num_items_in_queue--;
    queue->num_extracted++;

    fcs_offloading_queue_page__extract(
        queue->page_to_read_from,
        return_item
    );

    return TRUE;
}

#ifdef __cplusplus
}
#endif

#endif  /* FC_SOLVE__OFFLOADING_QUEUE_H */