2 * UCW Library -- Universal Sorter: Operations on Contexts, Buffers and Buckets
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
12 #include "lib/mempool.h"
13 #include "lib/stkstring.h"
14 #include "lib/sorter/common.h"
19 sorter_alloc(struct sort_context *ctx, uns size)
21 return mp_alloc_zero(ctx->pool, size);
25 sbuck_new(struct sort_context *ctx)
27 struct sort_bucket *b = sorter_alloc(ctx, sizeof(struct sort_bucket));
33 sbuck_drop(struct sort_bucket *b)
37 ASSERT(!(b->flags & SBF_DESTROYED));
42 b->flags = SBF_DESTROYED;
47 sbuck_size(struct sort_bucket *b)
49 if ((b->flags & SBF_OPEN_WRITE) && !(b->flags & SBF_SWAPPED_OUT))
56 sbuck_have(struct sort_bucket *b)
58 return b && sbuck_size(b);
62 sbuck_has_file(struct sort_bucket *b)
64 return (b->fb || (b->flags & SBF_SWAPPED_OUT));
68 sbuck_swap_in(struct sort_bucket *b)
70 if (b->flags & SBF_SWAPPED_OUT)
72 if (sorter_stream_bufsize) /* FIXME: Needs better configuration, probably semi-automatic one */
73 b->fb = bopen(b->filename, O_RDWR, sorter_stream_bufsize);
75 b->fb = fbdir_open(b->filename, O_RDWR, NULL);
76 if (b->flags & SBF_OPEN_WRITE)
77 bseek(b->fb, 0, SEEK_END);
78 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1);
79 b->flags &= ~SBF_SWAPPED_OUT;
80 SORT_XTRACE(2, "Swapped in %s", b->filename);
85 sbuck_read(struct sort_bucket *b)
88 if (b->flags & SBF_OPEN_READ)
90 else if (b->flags & SBF_OPEN_WRITE)
92 b->size = btell(b->fb);
93 b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
102 sbuck_write(struct sort_bucket *b)
105 if (b->flags & SBF_OPEN_WRITE)
109 ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
110 if (sorter_stream_bufsize)
111 b->fb = bopen_tmp(sorter_stream_bufsize);
113 b->fb = fbdir_open_tmp(NULL);
114 if (sorter_debug & SORT_DEBUG_KEEP_BUCKETS)
115 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
116 b->flags |= SBF_OPEN_WRITE;
117 b->filename = mp_strdup(b->ctx->pool, b->fb->name);
123 sbuck_swap_out(struct sort_bucket *b)
125 if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb && !(b->flags & SBF_SOURCE))
127 if (b->flags & SBF_OPEN_WRITE)
128 b->size = btell(b->fb);
129 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
132 b->flags |= SBF_SWAPPED_OUT;
133 SORT_XTRACE(2, "Swapped out %s", b->filename);
138 sorter_prepare_buf(struct sort_context *ctx)
140 u64 bs = MAX(sorter_bufsize/2, 1);
141 bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE);
142 ctx->big_buf_size = 2*bs;
143 ctx->big_buf_half_size = bs;
147 sorter_alloc_buf(struct sort_context *ctx)
151 ctx->big_buf = big_alloc(ctx->big_buf_size);
152 ctx->big_buf_half = ((byte*) ctx->big_buf) + ctx->big_buf_half_size;
153 SORT_XTRACE(2, "Allocated sorting buffer (2*%s)", stk_fsize(ctx->big_buf_half_size));
157 sorter_free_buf(struct sort_context *ctx)
161 big_free(ctx->big_buf, ctx->big_buf_size);
163 SORT_XTRACE(2, "Freed sorting buffer");