]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sbuck.c
lib: added {big,page}_alloc_zero routines
[libucw.git] / lib / sorter / sbuck.c
1 /*
2  *      UCW Library -- Universal Sorter: Operations on Contexts, Buffers and Buckets
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/mempool.h"
13 #include "lib/stkstring.h"
14 #include "lib/sorter/common.h"
15
16 #include <fcntl.h>
17
18 void *
19 sorter_alloc(struct sort_context *ctx, uns size)
20 {
21   return mp_alloc_zero(ctx->pool, size);
22 }
23
24 struct sort_bucket *
25 sbuck_new(struct sort_context *ctx)
26 {
27   struct sort_bucket *b = sorter_alloc(ctx, sizeof(struct sort_bucket));
28   b->ctx = ctx;
29   return b;
30 }
31
32 void
33 sbuck_drop(struct sort_bucket *b)
34 {
35   if (b)
36     {
37       ASSERT(!(b->flags & SBF_DESTROYED));
38       if (b->n.prev)
39         clist_remove(&b->n);
40       bclose(b->fb);
41       bzero(b, sizeof(*b));
42       b->flags = SBF_DESTROYED;
43     }
44 }
45
46 sh_off_t
47 sbuck_size(struct sort_bucket *b)
48 {
49   if ((b->flags & SBF_OPEN_WRITE) && !(b->flags & SBF_SWAPPED_OUT))
50     return btell(b->fb);
51   else
52     return b->size;
53 }
54
55 int
56 sbuck_have(struct sort_bucket *b)
57 {
58   return b && sbuck_size(b);
59 }
60
61 int
62 sbuck_has_file(struct sort_bucket *b)
63 {
64   return (b->fb || (b->flags & SBF_SWAPPED_OUT));
65 }
66
67 static void
68 sbuck_swap_in(struct sort_bucket *b)
69 {
70   if (b->flags & SBF_SWAPPED_OUT)
71     {
72       b->fb = bopen_file(b->filename, O_RDWR, &sorter_fb_params);       /* FIXME: Something different for small buckets? */
73       if (b->flags & SBF_OPEN_WRITE)
74         bseek(b->fb, 0, SEEK_END);
75       bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1);
76       b->flags &= ~SBF_SWAPPED_OUT;
77       SORT_XTRACE(2, "Swapped in %s", b->filename);
78     }
79 }
80
81 struct fastbuf *
82 sbuck_read(struct sort_bucket *b)
83 {
84   sbuck_swap_in(b);
85   if (b->flags & SBF_OPEN_READ)
86     return b->fb;
87   else if (b->flags & SBF_OPEN_WRITE)
88     {
89       b->size = btell(b->fb);
90       b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
91       brewind(b->fb);
92       return b->fb;
93     }
94   else
95     ASSERT(0);
96 }
97
98 struct fastbuf *
99 sbuck_write(struct sort_bucket *b)
100 {
101   sbuck_swap_in(b);
102   if (b->flags & SBF_OPEN_WRITE)
103     ASSERT(b->fb);
104   else
105     {
106       ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
107       b->fb = bopen_tmp_file(&sorter_fb_params);
108       if (sorter_debug & SORT_DEBUG_KEEP_BUCKETS)
109         bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
110       b->flags |= SBF_OPEN_WRITE;
111       b->filename = mp_strdup(b->ctx->pool, b->fb->name);
112     }
113   return b->fb;
114 }
115
116 void
117 sbuck_swap_out(struct sort_bucket *b)
118 {
119   if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb && !(b->flags & SBF_SOURCE))
120     {
121       if (b->flags & SBF_OPEN_WRITE)
122         b->size = btell(b->fb);
123       bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
124       bclose(b->fb);
125       b->fb = NULL;
126       b->flags |= SBF_SWAPPED_OUT;
127       SORT_XTRACE(2, "Swapped out %s", b->filename);
128     }
129 }
130
131 void
132 sorter_prepare_buf(struct sort_context *ctx)
133 {
134   u64 bs = sorter_bufsize;
135   bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE);
136   bs = MAX(bs, 2*(u64)CPU_PAGE_SIZE);
137   ctx->big_buf_size = bs;
138 }
139
140 void
141 sorter_alloc_buf(struct sort_context *ctx)
142 {
143   if (ctx->big_buf)
144     return;
145   ctx->big_buf = big_alloc(ctx->big_buf_size);
146   SORT_XTRACE(2, "Allocated sorting buffer (%s)", stk_fsize(ctx->big_buf_size));
147 }
148
149 void
150 sorter_free_buf(struct sort_context *ctx)
151 {
152   if (!ctx->big_buf)
153     return;
154   big_free(ctx->big_buf, ctx->big_buf_size);
155   ctx->big_buf = NULL;
156   SORT_XTRACE(2, "Freed sorting buffer");
157 }