]> diplodocus.org Git - nmh/blob - sbr/folder_realloc.c
827d3b17722c2b10d2feacaac4d759571e23564d
[nmh] / sbr / folder_realloc.c
1 /* folder_realloc.c -- realloc a folder/msgs structure
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include "error.h"
10 #include <h/utils.h>
11
12 /*
13 * Reallocate some of the space in the folder
14 * structure (currently just message status array).
15 *
16 * Return pointer to new folder structure.
17 * If error, return NULL.
18 */
19
20 struct msgs *
21 folder_realloc (struct msgs *mp, int lo, int hi)
22 {
23 struct bvector *tmpstats, *t;
24 size_t i;
25 int msgnum;
26
27 /* sanity checks */
28 if (lo < 1)
29 die("BUG: called folder_realloc with lo (%d) < 1", lo);
30 if (hi < 1)
31 die("BUG: called folder_realloc with hi (%d) < 1", hi);
32 if (mp->nummsg > 0 && lo > mp->lowmsg)
33 die("BUG: called folder_realloc with lo (%d) > mp->lowmsg (%d)",
34 lo, mp->lowmsg);
35 if (mp->nummsg > 0 && hi < mp->hghmsg)
36 die("BUG: called folder_realloc with hi (%d) < mp->hghmsg (%d)",
37 hi, mp->hghmsg);
38
39 /* Check if we really need to reallocate anything */
40 if (lo == mp->lowoff && hi == mp->hghoff)
41 return mp;
42
43 /* first allocate the new message status space */
44 mp->num_msgstats = MSGSTATNUM (lo, hi);
45 tmpstats = mh_xmalloc (MSGSTATSIZE(mp));
46 for (i = 0, t = tmpstats; i < mp->num_msgstats; ++i, ++t) {
47 bvector_init(t);
48 }
49
50 /* then copy messages status array with shift */
51 if (mp->nummsg > 0) {
52 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
53 bvector_copy (tmpstats + msgnum - lo, msgstat (mp, msgnum));
54 }
55 free(mp->msgstats);
56 mp->msgstats = tmpstats;
57 mp->lowoff = lo;
58 mp->hghoff = hi;
59
60 return mp;
61 }