Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/librecsort/rsopen.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1996-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
#include "rskeyhdr.h"
22
23
/* Opening sorting contexts
24
**
25
** Written by Kiem-Phong Vo (07/08/96)
26
*/
27
28
static const char id[] = "\n@(#)$Id: recsort library (AT&T Research) 2011-10-11 $\0\n";
29
30
#if __STD_C
31
Rs_t* rsnew(Rsdisc_t* disc)
32
#else
33
Rs_t* rsnew(disc)
34
Rsdisc_t* disc; /* discipline describing record types */
35
#endif
36
{
37
reg Rs_t* rs;
38
39
if(rs = (Rs_t*)vmresize(Vmheap,NIL(Void_t*),sizeof(Rs_t),VM_RSZERO))
40
rsdisc(rs,disc,RS_DISC);
41
return rs;
42
}
43
44
#if __STD_C
45
int rsinit(reg Rs_t* rs, Rsmethod_t* meth, ssize_t c_max, int type, Rskey_t* key)
46
#else
47
int rsinit(rs, meth, c_max, type, key)
48
Rs_t* rs; /* handle from rsnew() */
49
Rsmethod_t* meth; /* sorting method */
50
ssize_t c_max; /* process about this much per chain */
51
int type; /* sort controls */
52
Rskey_t* key; /* key coder state */
53
#endif
54
{
55
Rsdisc_t* disc;
56
ssize_t round;
57
58
if((round = c_max) > 0)
59
round /= 4;
60
rs->vmdisc.memoryf = Vmdcheap->memoryf;
61
rs->vmdisc.exceptf = Vmdcheap->exceptf;
62
if(!(rs->vm = (Vmalloc_t*)vmopen(&rs->vmdisc, Vmbest, 0)) )
63
{ vmfree(Vmheap,(void*)rs);
64
return -1;
65
}
66
rs->vmdisc.round = round <= 0 ? RS_RESERVE : round;
67
if(!(rs->vm = (Vmalloc_t*)vmopen(&rs->vmdisc, Vmbest, 0)) )
68
{ vmfree(Vmheap,(void*)rs);
69
return -1;
70
}
71
72
if(!(rs->methdata = (Void_t*)vmresize(Vmheap,NIL(Void_t*),meth->size,VM_RSZERO)) )
73
goto bad;
74
75
rs->meth = meth;
76
rs->c_max = c_max;
77
rs->type = rs->disc->type | (type&RS_TYPES);
78
rs->key = rs->disc->version < 20111011L ? (Rskey_t*)((char*)rs->disc - sizeof(Rskey_t)) : key;
79
80
rs->events = 0;
81
for (disc = rs->disc; disc; disc = disc->disc)
82
rs->events |= disc->events;
83
84
if (RSNOTIFY(rs, RS_OPEN, 0, 0, rs->disc) < 0)
85
goto bad;
86
87
return 0;
88
bad:
89
vmclose(rs->vm);
90
vmfree(Vmheap,rs);
91
return -1;
92
}
93
94
#if __STD_C
95
Rs_t* rsopen(Rsdisc_t* disc, Rsmethod_t* meth, ssize_t c_max, int type)
96
#else
97
Rs_t* rsopen(disc, meth, c_max, type)
98
Rsdisc_t* disc; /* discipline describing record types */
99
Rsmethod_t* meth; /* sorting method */
100
ssize_t c_max; /* process about this much per chain */
101
int type; /* sort controls */
102
#endif
103
{
104
reg Rs_t* rs;
105
106
if((rs = rsnew(disc)) && rsinit(rs, meth, c_max, type, NiL))
107
{ vmclose(rs->vm);
108
vmfree(Vmheap,rs);
109
rs = 0;
110
}
111
return rs;
112
}
113
114