2 * UCW Library -- Universal Sorter: Internal Sorting Module
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.
12 // FIXME: Add the hash here to save cache misses
15 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
16 #define ASORT_KEY_TYPE P(internal_item_t)
17 #define ASORT_ELT(i) ary[i]
18 #define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
19 #define ASORT_EXTRA_ARGS , P(internal_item_t) *ary
20 #include "lib/arraysort.h"
22 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
24 sorter_alloc_buf(ctx);
25 struct fastbuf *in = sbuck_read(bin);
27 P(key) key, *keybuf = ctx->key_buf;
29 keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
35 else if (!P(read_key)(in, &key))
39 if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
41 SORT_XTRACE("s-internal: Generating a giant run");
42 struct fastbuf *out = sorter_open_write(bout); /* FIXME: Using a non-direct buffer would be nice here */
43 P(copy_data)(&key, in, out);
45 return 1; // We don't know, but 1 is always safe
49 size_t bufsize = ctx->big_buf_half_size; /* FIXME: In some cases, we can use the whole buffer */
50 bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t))); // The number of records must fit in uns
52 SORT_XTRACE("s-internal: Reading (bufsize=%zd)", bufsize);
53 P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
54 byte *end = (byte *) ctx->big_buf + bufsize;
57 uns ksize = SORT_KEY_SIZE(key);
59 uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
61 uns ksize_aligned = ksize;
63 uns dsize = SORT_DATA_SIZE(key);
64 uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
65 if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
72 memcpy(end, &key, ksize);
74 breadb(in, end + ksize_aligned, dsize);
76 item->key = (P(key)*) end;
79 while (P(read_key)(in, &key));
82 uns count = last_item - item_array;
83 SORT_XTRACE("s-internal: Sorting %d items", count);
84 P(array_sort)(count, item_array);
86 SORT_XTRACE("s-internal: Writing");
89 struct fastbuf *out = sbuck_write(bout);
91 /* FIXME: No unification done yet */
92 for (item = item_array; item < last_item; item++)
94 P(write_key)(out, item->key);
96 uns ksize = SORT_KEY_SIZE(*item->key);
98 ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
100 bwrite(out, (byte *) item->key + ksize, SORT_DATA_SIZE(*item->key));
104 return ctx->more_keys;