-void *
-sorter_alloc(struct sort_context *ctx, uns size)
-{
- return mp_alloc_zero(ctx->pool, size);
-}
-
-struct sort_bucket *
-sbuck_new(struct sort_context *ctx)
-{
- return sorter_alloc(ctx, sizeof(struct sort_bucket));
-}
-
-void
-sbuck_drop(struct sort_bucket *b)
-{
- if (b)
- {
- ASSERT(!(b->flags & SBF_DESTROYED));
- if (b->n.prev)
- clist_remove(&b->n);
- bclose(b->fb);
- bzero(b, sizeof(*b));
- b->flags = SBF_DESTROYED;
- }
-}
-
-sh_off_t
-sbuck_size(struct sort_bucket *b)
-{
- if (b->flags & SBF_OPEN_WRITE)
- return btell(b->fb);
- else
- return b->size;
-}
-
-int
-sbuck_have(struct sort_bucket *b)
-{
- return b && sbuck_size(b);
-}
-
-struct fastbuf *
-sbuck_read(struct sort_bucket *b)
-{
- /* FIXME: These functions should handle buckets with no fb and only name. */
- ASSERT(b->fb);
- if (b->flags & SBF_OPEN_READ)
- return b->fb;
- else if (b->flags & SBF_OPEN_WRITE)
- {
- b->size = btell(b->fb);
- b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
- brewind(b->fb);
- return b->fb;
- }
- else
- ASSERT(0);
-}
-
-struct fastbuf *
-sbuck_write(struct sort_bucket *b)
-{
- if (b->flags & SBF_OPEN_WRITE)
- ASSERT(b->fb);
- else
- {
- ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
- b->fb = bopen_tmp(sorter_stream_bufsize);
- b->flags |= SBF_OPEN_WRITE;
- }
- return b->fb;
-}
-
-void
-sorter_alloc_buf(struct sort_context *ctx)
-{
- if (ctx->big_buf)
- return;
- u64 bs = MAX(sorter_bufsize/2, 1);
- bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE);
- ctx->big_buf = big_alloc(2*bs);
- ctx->big_buf_size = 2*bs;
- ctx->big_buf_half = ((byte*) ctx->big_buf) + bs;
- ctx->big_buf_half_size = bs;
- SORT_XTRACE("Allocated sorting buffer (%jd bytes)", (uintmax_t) bs);
-}
-
-void
-sorter_free_buf(struct sort_context *ctx)
-{
- if (!ctx->big_buf)
- return;
- big_free(ctx->big_buf, ctx->big_buf_size);
- ctx->big_buf = NULL;
- SORT_XTRACE("Freed sorting buffer");
-}